Skip to content

Database Optimizations#3294

Open
ouziel-slama wants to merge 33 commits into
developfrom
hashes
Open

Database Optimizations#3294
ouziel-slama wants to merge 33 commits into
developfrom
hashes

Conversation

@ouziel-slama

@ouziel-slama ouziel-slama commented May 12, 2026

Copy link
Copy Markdown
Member
  • Double-check the spelling and grammar of all strings, code comments, etc.
  • Double-check that all code is deterministic that needs to be
  • Add tests to cover any new or revised logic
  • Ensure that the test suite passes
  • Update the project release notes
  • Update the project documentation, as appropriate, with a corresponding Pull Request in the Documentation repository

@ouziel-slama ouziel-slama marked this pull request as draft May 12, 2026 16:38
@codecov

codecov Bot commented May 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.81456% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.11%. Comparing base (7fff4f7) to head (d963ea4).

Files with missing lines Patch % Lines
...rparty-core/counterpartycore/lib/api/apiwatcher.py 58.13% 18 Missing ⚠️
...nterparty-core/counterpartycore/lib/api/queries.py 90.34% 17 Missing ⚠️
...lib/ledger/migrations/0010.compact_hash_storage.py 92.10% 9 Missing ⚠️
...rparty-core/counterpartycore/lib/utils/database.py 93.02% 3 Missing ⚠️
...grations/0002.create_and_populate_parsed_events.py 81.81% 2 Missing ⚠️
...ations/0003.create_and_populate_all_expirations.py 81.81% 2 Missing ⚠️
...erparty-core/counterpartycore/lib/ledger/events.py 95.91% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3294      +/-   ##
===========================================
- Coverage    91.34%   89.11%   -2.24%     
===========================================
  Files          113      117       +4     
  Lines        15608    16100     +492     
===========================================
+ Hits         14257    14347      +90     
- Misses        1351     1753     +402     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/ledger/caches.py Fixed
Comment thread counterparty-core/counterpartycore/lib/parser/mempool.py Fixed
Comment thread counterparty-core/counterpartycore/lib/parser/mempool.py Fixed
Comment thread counterparty-core/counterpartycore/lib/utils/database.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
Comment thread counterparty-core/counterpartycore/lib/api/queries.py Fixed
@codecov

codecov Bot commented May 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.30550% with 450 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.62%. Comparing base (3f08503) to head (38bbc37).

Files with missing lines Patch % Lines
...nterparty-core/counterpartycore/lib/api/queries.py 8.95% 244 Missing ⚠️
...rparty-core/counterpartycore/lib/api/apiwatcher.py 48.52% 35 Missing ⚠️
...party-core/counterpartycore/lib/utils/hashcodec.py 46.29% 29 Missing ⚠️
...lib/ledger/migrations/0010.compact_hash_storage.py 79.10% 28 Missing ⚠️
...rparty-core/counterpartycore/lib/ledger/markets.py 50.00% 20 Missing ⚠️
...rparty-core/counterpartycore/lib/utils/database.py 92.63% 14 Missing ⚠️
...erparty-core/counterpartycore/lib/ledger/events.py 88.07% 13 Missing ⚠️
...erparty-core/counterpartycore/lib/parser/blocks.py 15.38% 11 Missing ⚠️
...erparty-core/counterpartycore/lib/ledger/blocks.py 25.00% 9 Missing ⚠️
...rparty-core/counterpartycore/lib/parser/mempool.py 27.27% 8 Missing ⚠️
... and 12 more

❌ Your patch check has failed because the patch coverage (57.30%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.
❌ Your project check has failed because the head coverage (54.62%) is below the target coverage (90.00%). You can increase the head coverage or adjust the target coverage.

❗ There is a different number of reports uploaded between BASE (3f08503) and HEAD (38bbc37). Click for more details.

HEAD has 13 uploads less than BASE
Flag BASE (3f08503) HEAD (38bbc37)
18 5
Additional details and impacted files
@@             Coverage Diff              @@
##           develop    #3294       +/-   ##
============================================
- Coverage    91.38%   54.62%   -36.77%     
============================================
  Files          114      116        +2     
  Lines        16296    17132      +836     
============================================
- Hits         14892     9358     -5534     
- Misses        1404     7774     +6370     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ouziel-slama ouziel-slama marked this pull request as ready for review May 14, 2026 15:54
@ouziel-slama ouziel-slama changed the base branch from develop to feature/fairmint-pool May 27, 2026 08:20
@ouziel-slama ouziel-slama changed the base branch from feature/fairmint-pool to develop May 27, 2026 14:49
Ouziel added 7 commits May 27, 2026 16:59
…dex) pairs

Drop the 129-char tx0hash_tx1hash TEXT id on order_matches, bet_matches,
rps_matches and the *_match_id FK on the *_match_expirations, btcpays,
rpsresolves and bet_match_resolutions tables; key matches by the integer
(tx0_index, tx1_index) pair instead. The textual id is reconstructed on
read at the API and consensus boundaries, so the public API and consensus
hashes are unchanged.
@ouziel-slama ouziel-slama changed the title Hashes Storage Optimizations Database Optimizations Jun 16, 2026
Ouziel added 6 commits June 16, 2026 18:42
Add a surrogate `asset_index INTEGER PRIMARY KEY` to `assets` (assigned in
issuance order; the protocol `asset_id` stays TEXT) and replace every
asset-name TEXT column with that compact integer FK across balances,
credits, debits, sends, orders, order_matches, issuances, dividends,
dispensers, dispenses, dispenser_refills, fairminters, fairmints,
destructions, pools and pool_*. Folded into migration 0010.

Asset names stay visible everywhere with no consensus change:
- the rowtracer decodes asset_index -> name on read;
- events._prepare_record_for_insert encodes name -> index on write
  (returning a copy, so OrdersCache still gets names);
- the consensus journal keeps the original names, so ledger_hash and
  messages_hash are unchanged;
- ledger query functions and the API select_rows resolve name -> index
  for WHERE filters (select_rows only on Ledger DB, via a main.assets
  probe; the State DB consolidated tables keep names);
- the API migrations (0004/0006/0014) decode index -> name while
  ledger_db is attached.

lp_asset stays TEXT (the LP token is referenced before it exists).
Invalid records referencing a never-registered asset store NULL.

Order-sensitive consensus paths resolve the asset *name* for ordering so
the pre-normalization processing order is preserved: get_dispensers
(order_by=asset) and get_address_balances, the latter feeding the
msg_index / debit / credit sequence in move, detach and sweep.
…olumns

Finishes the storage-compaction work (issue #2744 points 3 & 4), folded
into the unreleased migration 0010.compact_hash_storage, mirroring the
asset_index normalization.

Address normalization:
- New address_list(address_id INTEGER PRIMARY KEY, address TEXT UNIQUE)
  identity table; every address column (source, destination, address,
  issuer, feed_address, tx0/tx1_address, winner, oracle_address, origin,
  *_address, utxo_address) becomes an INTEGER address_id FK.
- Transparent: the rowtracer decodes address_id -> string on read and the
  write path encodes string -> id (ensure_address, first-seen-on-use), so
  consensus hashes and the public API are unchanged. The State DB keeps
  address strings (consolidation decodes while ledger_db is attached). The
  existing addresses options table is preserved and FK'd; mempool stays TEXT.
- Per-connection address caches are bounded (LRU) since addresses number in
  the millions, and are reset on mempool/reorg rollback.

UTXO compaction:
- balances/credits/debits utxo TEXT (tx_hash:vout) -> (utxo_tx_hash BLOB,
  utxo_vout INTEGER). The tx_hash is stored as a raw BLOB rather than a
  tx_index FK because an attach destination may be any valid bitcoin UTXO
  whose tx is absent from the transactions table; the utxo string is
  reconstructed transparently by the rowtracer.

Also: API prepare_where address/utxo filter rewrites, State DB consolidation
decode (0004/0006/0014), consensus-ordering fixes (get_asset_balances
subquery shadowing, holders() utxo order, held() dedup separator), and
test/mock updates. Full unit suite green (1507 passed).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants