Skip to content

Commit

Permalink
tapdb: fix incorrect asset balances with mixed versions
Browse files Browse the repository at this point in the history
Because of the versions field being in the GROUP BY clause of the query,
we would get two distinct rows returned for the same asset ID if we had
asset UTXOs with different versions.
But because we use the asset ID as the map key of the data structure we
return, only one of those two entries would be returned, causing the
total amount to be incorrect.

We fix this by removing the asset version field from the GROUP BY
clause, resulting in just one row being returned.
The alternative would be to add the version to the map key and show
balances by asset ID and version instead.
  • Loading branch information
guggero committed Feb 19, 2024
1 parent acde627 commit f604020
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 3 deletions.
1 change: 0 additions & 1 deletion tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ func (a *AssetStore) QueryBalancesByAsset(ctx context.Context,
copy(assetID[:], assetBalance.AssetID[:])

assetIDBalance := AssetBalance{
Version: assetBalance.Version,
Balance: uint64(assetBalance.Balance),
Tag: assetBalance.AssetTag,
Type: asset.Type(assetBalance.AssetType),
Expand Down
2 changes: 1 addition & 1 deletion tapdb/sqlc/assets.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tapdb/sqlc/queries/assets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ LEFT JOIN key_group_info_view
ON assets.genesis_id = key_group_info_view.gen_asset_id
WHERE spent = FALSE
GROUP BY assets.genesis_id, genesis_info_view.asset_id,
version, genesis_info_view.asset_tag, genesis_info_view.meta_hash,
genesis_info_view.asset_tag, genesis_info_view.meta_hash,
genesis_info_view.asset_type, genesis_info_view.output_index,
genesis_info_view.prev_out;

Expand Down

0 comments on commit f604020

Please sign in to comment.