Repository navigation
taprpc+tapdb: add GenesisInfo to AssetGroupBalance in ListBalances - #2130
kaldun-tech wants to merge 1 commit into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the 'ListBalances' RPC functionality by including comprehensive metadata for grouped assets. By adding genesis information and decimal display settings to the 'AssetGroupBalance' response, clients can now display asset details directly without requiring additional API calls. The changes involve updates to the protocol buffers, database query logic, and the RPC server implementation to populate these new fields efficiently. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request enhances the ListBalances RPC by including representative genesis information and decimal display metadata for asset groups. The changes involve updating the database queries to select anchor genesis data, extending the AssetGroupBalance structures in both the database and RPC layers, and populating these new fields in the RPC server. Feedback was provided regarding a potential N+1 query performance issue when fetching decimal displays and a requirement to use structured logging for debug messages as per the repository style guide.
2b0f3f4 to
48f4267
Compare
|
I believe these CI failures are flakes. @claude please code review and investigate whether the failed CI is due to code changes or flakes |
|
@jtobin @darioAnongba Please re-run the failed CI jobs. My investigation is that the failures were due to flakes but let me know if you want me to look into anything on my end |
| if err == nil { | ||
| groupBalance.DecimalDisplay = fn.MapOptionZ( | ||
| decDisplay, | ||
| func(d uint32) *taprpc.DecimalDisplay { | ||
| return &taprpc.DecimalDisplay{ | ||
| DecimalDisplay: d, | ||
| } | ||
| }, | ||
| ) | ||
| } else { | ||
| rpcsLog.DebugS(ctx, "Failed to fetch decimal display", | ||
| btclog.Fmt("asset_id", "%x", balance.ID[:]), | ||
| "error", err) | ||
| } |
There was a problem hiding this comment.
We can avoid silently logging debug and escalate instead.
| if err == nil { | |
| groupBalance.DecimalDisplay = fn.MapOptionZ( | |
| decDisplay, | |
| func(d uint32) *taprpc.DecimalDisplay { | |
| return &taprpc.DecimalDisplay{ | |
| DecimalDisplay: d, | |
| } | |
| }, | |
| ) | |
| } else { | |
| rpcsLog.DebugS(ctx, "Failed to fetch decimal display", | |
| btclog.Fmt("asset_id", "%x", balance.ID[:]), | |
| "error", err) | |
| } | |
| switch { | |
| case errors.Is(err, address.ErrAssetMetaNotFound): | |
| // Asset legitimately has no meta — leave DecimalDisplay unset. | |
| case err != nil: | |
| return nil, fmt.Errorf("unable to fetch decimal display "+ | |
| "for asset %x: %w", balance.ID[:], err) | |
| default: | |
| groupBalance.DecimalDisplay = fn.MapOptionZ( | |
| decDisplay, | |
| func(d uint32) *taprpc.DecimalDisplay { | |
| return &taprpc.DecimalDisplay{DecimalDisplay: d} | |
| }, | |
| ) | |
| } |
| -- works for locally-minted assets where the anchor is inserted first. | ||
| SELECT | ||
| tweaked_group_key, | ||
| MIN(gen_asset_id) as anchor_gen_id |
There was a problem hiding this comment.
gen_asset_id is a local-DB primary key reflecting local insertion order, not protocol-level ordering or any canonical anchor. The canonical anchor is stored explicitly in asset_groups.genesis_point_id
A fix is to join through asset_groups.genesis_point_id to resolve the actual anchor gen_asset_id. The existing FetchGroupByGroupKey pattern (ORDER BY witness_id LIMIT 1) already does this.
There was a problem hiding this comment.
Agreed that gen_asset_id order was the wrong signal. I first switched to joining through asset_groups.genesis_point_id as you suggested, but George found that pointer can move to a tranche this node has no witness for (e.g. after creating an address for a new tranche), and the inner join then dropped the group from the response. The representative is now chosen from witnessed rows with MIN(witness_id), the same ordering as the FetchGroupByGroupKey query you pointed to. Details are in George's thread on this query.
|
Hey @darioAnongba thanks for the feedback. I'm making a few changes:
|
48f4267 to
44d12a0
Compare
|
@darioAnongba Pushed my changes
|
|
@GeorgeTsagk @jtobin Tagging for review |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
3 similar comments
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@darioAnongba: review reminder |
44d12a0 to
13630f6
Compare
GeorgeTsagk
left a comment
There was a problem hiding this comment.
Thanks for sticking with this one. A few housekeeping items first, they make the PR much easier to pick up for whoever reviews next:
- Rebase on main. The branch is 18 commits behind and GitHub shows it as conflicting.
- Move the release note to release-notes-0.9.0.md, 0.8.x is already out.
- Rewrite the commit message so it matches the code. It still describes the MIN(gen_asset_id) approach and mentions merge conflict fixing.
- Drop the UpsertAssetGroupKey ON CONFLICT change. It is not part of what this PR advertises and it changes behavior for every group upsert path.
Inline I found two correctness issues, one of which drops groups from the response. Details on the lines.
| ctx, balance.ID, | ||
| ) | ||
| switch { | ||
| case errors.Is(err, address.ErrAssetMetaNotFound): |
There was a problem hiding this comment.
This never matches. DecDisplayForAssetID wraps the store error with %v, so errors.Is fails and we fall into the escalation branch. Any grouped asset whose anchor has no meta makes the whole by-group ListBalances call error out. The itest assets all carry meta, which is why it is not caught.
Fix is %w in address/book.go DecDisplayForAssetID plus a test for a group without meta. Also note that a cache miss there goes through FetchAssetMetaForAsset, which with the universe syncer wired in will try a federation sync on every call for such assets.
There was a problem hiding this comment.
Fixed. DecDisplayForAssetID now wraps with %w, so the ErrAssetMetaNotFound branch matches and a group whose representative has no meta leaves decimal_display unset instead of failing the whole call. Added TestBook_DecDisplayForAssetID_NoMeta to assert the errors.Is chain holds.
On the federation sync: agreed. A miss isn't cached, so with the syncer wired in, every call for a meta-less asset retries SyncAssetInfo. That's existing DecDisplayForAssetID behavior shared by the other rpcserver call sites (ListAssets etc.), so I'd suggest a follow-up that caches misses in the address book rather than growing this PR. Can fold it in here if you'd prefer
|
|
||
| -- name: QueryAssetBalancesByGroup :many | ||
| WITH group_anchor AS ( | ||
| -- Select the canonical anchor genesis for each group by joining through |
There was a problem hiding this comment.
Two problems with this CTE.
-
It is an inner join through asset_groups.genesis_point_id and asset_group_witnesses. When the group row was first created via an address for tranche A (no witness) and we later receive tranche B, there is no witness for A and the group disappears from the by-group balances entirely. Main returns the group in that scenario, I reproduced both. Freezing genesis_point_id in the upsert below makes this permanent.
-
When the anchor and another tranche share a genesis point (same mint batch, exactly what the multi asset group itest does) the CTE returns one row per tranche. Balance stays correct but the representative genesis is whichever row the DB emits last, so it differs between runs and backends.
Suggestion: pick the representative from asset_group_witnesses joined to key_group_info_view, collapse to one row per group with MIN over the witness gen ids, and LEFT JOIN so a missing anchor never hides a balance. Add tests for the no-witness anchor and shared genesis point cases, and assert the exact representative.
There was a problem hiding this comment.
Reworked along the lines you suggested. From Opus:
group_anchornow picks the representative fromkey_group_info_view, so only witnessed tranches are considered, collapsed to one row per group withMIN(witness_id). It no longer goes throughasset_groups.genesis_point_id, so repointing the anchor at an unreceived tranche can't drop the group. It's alsoLEFT JOINed, so a missing representative never hides a balance.MIN(witness_id)makes the pick deterministic when tranches share a genesis point, independent of row order or backend.
Tests:
TestQueryAssetBalancesByGroupNoWitnessAnchorreproduces the address-then-receive case and asserts the balance and representative come from the witnessed tranche.TestQueryAssetBalancesByGroupSharedGenesisPointmints two tranches on one genesis point, asserts the first-witnessed tranche is the representative, and checks the pick is stable across calls. It passes on both SQLite and Postgres in CI.
There was a problem hiding this comment.
On the UpsertAssetGroupKey: Reverted. this is now identical to main, and the fix is entirely in QueryAssetBalancesByGroup. The scenario you described is covered by TestQueryAssetBalancesByGroupNoWitnessAnchor, which repoints genesis_point_id using main's existing ON CONFLICT DO UPDATE
| -- Preserve the original genesis_point_id (the anchor's genesis point). | ||
| -- We update version to ensure RETURNING works, but genesis_point_id | ||
| -- must remain unchanged as it identifies the canonical group anchor. | ||
| DO UPDATE SET version = EXCLUDED.version |
There was a problem hiding this comment.
Unrelated to the PR and untested. This changes what every group upsert does (mint, proof import, universe, address book) and now also overwrites version on conflict. Please revert and fix the query instead.
| burns. | ||
|
|
||
| - [PR#2130](https://github.com/lightninglabs/taproot-assets/pull/2130) | ||
| adds `asset_genesis` and `decimal_display` fields to `AssetGroupBalance` |
There was a problem hiding this comment.
Belongs in release-notes-0.9.0.md.
13630f6 to
e66ad6d
Compare
Add GenesisInfo and DecimalDisplay fields to the AssetGroupBalance message, so the group_by=group_key mode of ListBalances returns asset metadata alongside balances without a follow-up call per group. QueryAssetBalancesByGroup gains a group_anchor CTE that selects a representative genesis per group from key_group_info_view, picking the row with MIN(witness_id) for deterministic results when multiple tranches share a genesis point. The CTE is LEFT JOINed so a group whose anchor tranche has no local witness yet still returns its balance, just without genesis fields, instead of being dropped from the response. DecDisplayForAssetID in address/book.go now wraps its store error with %w instead of %v, so errors.Is(err, ErrAssetMetaNotFound) correctly detects assets with no known meta instead of falling through to a hard error. Add tapdb tests for the no-witness-anchor and shared-genesis-point cases, and an address book test for DecDisplayForAssetID on an asset with no meta. Fixes lightninglabs#2066. Signed-off-by: kaldun-tech <tsmereka@protonmail.com>
e66ad6d to
e137e0c
Compare
|
@GeorgeTsagk @darioAnongba thanks for the thorough reviews. Everything should now be addressed Side note, I'm working on an AI startup contract which keeps me pretty busy. Aiming to get more contributions rolling when I figure out time management. Housekeeping:
Correctness:
One open question inline: misses in |
|
@GeorgeTsagk @jtobin could one of you re-run the failed It failed on I couldn't reproduce it locally:
Looks like a load-related flake under |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
3 similar comments
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@kaldun-tech, remember to re-request review from reviewers when ready |
|
@GeorgeTsagk: review reminder |
3 similar comments
|
@GeorgeTsagk: review reminder |
|
@GeorgeTsagk: review reminder |
|
@GeorgeTsagk: review reminder |
|
@GeorgeTsagk: review reminder |
Add
GenesisInfoandDecimalDisplayfields toAssetGroupBalancein theListBalancesRPC response when usinggroup_by=group_keymode.Previously,
AssetGroupBalanceonly containedgroup_keyandbalance, making it impossible for clients to display asset metadata (name, type, decimal display) alongside grouped balances without additional RPC calls.Changes
asset_genesis(field 3) anddecimal_display(field 4) toAssetGroupBalancemessageQueryAssetBalancesByGroupto joingenesis_info_viewand return genesis data for a representative issuance per groupAssetGroupBalancestruct with genesis fields and updated query result mappinglistBalancesByGroupKey, includingDecimalDisplaylookup viaDecDisplayForAssetIDDesign Notes
MIN(gen_asset_id), which gives the earliest-inserted genesis. For locally-minted assets, this is the group anchor. For fungible assets, all tranches share the same name/type, so any genesis provides correct metadata.QueryAssetBalancesByGroupresults (usegroup_by=asset_idmode for those).Test Coverage Gaps
Error handling paths in RPC layer: The
ErrAssetMetaNotFoundand error escalation paths inlistBalancesByGroupKeyare not directly unit tested. This would require mockingAddrBook.DecDisplayForAssetID.Received grouped assets without local anchor: Edge case where we receive a grouped asset but don't have the anchor locally. The new query should still work (joins through
asset_groups.genesis_point_id), but this isn't explicitly tested.Test Plan
make unit pkg=tapdb- verify DB layer changesmake unit pkg=rpcserver- verify RPC marshalingmake itest icase=minting_multi_asset_groupstapcli assets balance --by-groupreturns genesis infoFixes issue #2066