!5072starsquid/volume-logging → develop6a6145f70c

Pool volume: count base-layer pools only

AI-generatedEverything on this page was written by an AI agent from the diff: the summaries, the narration, the diagrams, the flags and the glossary. It can be wrong or incomplete, so check it against the code before you rely on it.

2commits
6files
111lines changed
30lines to read
2call sites filtered
0migrations

Where the diff is

3-minute explainer

What changed

Base-layer filter

isBaseLayerPool: L1, TCY, whitelisted native in; synth, derived, TOR out.

BTC.BTCL1 ✓THOR.TCY✓THOR.RUJIwhitelist ✓BTC/BTCsynth ✗THOR.BTCderived ✗

same set /thorchain/pools lists

Swap path

Derived-pool swaps no longer write a volume record.

swap event base layer? SetVolume skip THOR.BTC → skip

End block

Non-base pools skipped; derived records freeze in state.

BTC.BTC rollBTC/BTC skipTHOR.BTC skipETH.ETH roll

savers pass was a no-op replay of the L1 record; derived pass wrote state

Missing record

ErrVolumeNotFound sentinel; no-record pools skip without an error log.

  • GetVolume wraps ErrVolumeNotFound
  • end block: errors.Is → continue, silent
  • decode errors still logged

Tests

Filter table, log capture, skip of synth and derived records.

TestIsBaseLayerPoolTestMissingVolumeDoesNotLogErrorTestSkipsSyntheticAndDerivedPoolsTestVolume

Look here first

Derived-pool swaps stop writing volumeUngated state-write change in a _current file: app hash diverges on derived swaps. Must ship as a consensus (minor) release.swap_current.go:166 ↗verify
157157 telem(evt.SwapSlip),
158158 []metrics.Label{telemetry.NewLabel("pool", evt.Pool.String())},
159159 )
160160 telemetry.IncrCounterWithLabels(
161161 []string{"thornode", "swap", "liquidity_fee"},
162162 telem(evt.LiquidityFeeInRune),
163163 []metrics.Label{telemetry.NewLabel("pool", evt.Pool.String())},
164164 )
165165
166+if !isBaseLayerPool(evt.Pool) {
167+ continue
168+}
169+
166170 volume, err := keeper.GetVolume(ctx, evt.Pool)
167171 if err != nil {
168172 volume = types.NewVolume(evt.Pool)
169173 }
170174
171175 if evt.EmitAsset.Asset.GetLayer1Asset().Equals(evt.Pool) {
172176 volume.ChangeAsset = volume.ChangeAsset.Add(evt.EmitAsset.Amount)
173177 volume.ChangeRune = volume.ChangeRune.Add(evt.InTx.Coins[0].Amount)
174178 } else {
175179 volume.ChangeAsset = volume.ChangeAsset.Add(evt.InTx.Coins[0].Amount)
Derived volume records freeze in stateTHOR.* Volume and bucket records stop rolling and stay forever; no migration prunes them. Mainnet record count not confirmed.manager_volume_current.go:48 ↗verify
2939 }
3040
3141 timestamp := ctx.BlockHeader().Time.Unix()
3242 numBuckets := 60 * 60 * 24 / constants.VolumeBucketSeconds
3343 currentIndex := (timestamp / constants.VolumeBucketSeconds) % numBuckets
3444
3545 for _, pool := range pools {
46+ // Volume is reported for base layer pools. Synthetic and derived pool
47+ // records should not update the same rolling volume state.
48+ if !isBaseLayerPool(pool.Asset) {
49+ continue
50+ }
51+
3652 volume, err := vm.keeper.GetVolume(ctx, pool.Asset)
3753 if err != nil {
54+ // A pool has no volume record until its first swap. This is expected,
55+ // so only report errors that indicate an actual storage problem.
56+ if errors.Is(err, kvTypes.ErrVolumeNotFound) {
57+ continue
58+ }
3859 ctx.Logger().Error("fail to get volume", "asset", pool.Asset, "error", err)
3960 continue
4061 }
Filter must mirror /thorchain/pools skipsDuplicates the querier's synth/derived skip list; a new pool kind must update both. LPUnits==0 pools deliberately stay tracked.manager_volume_current.go:32 ↗careful
2023 return &VolumeMgr{
2124 keeper: keeper,
2225 }
2326 }
2427
28+// isBaseLayerPool identifies the pool types exposed by /thorchain/pools.
29+// Pools with no LP units are still tracked so their rolling volume remains
30+// current if they become visible again.
31+func isBaseLayerPool(asset common.Asset) bool {
32+ return !asset.IsEmpty() && (asset.IsExternalL1Asset() || asset.IsTCY() || asset.IsWhitelisted())
33+}
34+
2535 func (vm *VolumeMgr) EndBlock(ctx cosmos.Context) error {
2636 pools, err := vm.keeper.GetPools(ctx)
2737 if err != nil {
2838 return err
2939 }
3040
3141 timestamp := ctx.BlockHeader().Time.Unix()
3242 numBuckets := 60 * 60 * 24 / constants.VolumeBucketSeconds
3343 currentIndex := (timestamp / constants.VolumeBucketSeconds) % numBuckets
3444
3545 for _, pool := range pools {
querier.go queryPools filter at head
1923 }
1924
1925 // Ignore derived assets (except TOR)
1926 if pool.Asset.IsDerivedAsset() {
1927 continue
1928 }
1929
Missing record now skipped silentlySame control flow as before (continue); only the log is dropped. Unmarshal errors are not the sentinel and still log.manager_volume_current.go:56 ↗careful
47+// records should not update the same rolling volume state.
48+if !isBaseLayerPool(pool.Asset) {
49+ continue
50+}
51+
3652 volume, err := vm.keeper.GetVolume(ctx, pool.Asset)
3753 if err != nil {
54+ // A pool has no volume record until its first swap. This is expected,
55+ // so only report errors that indicate an actual storage problem.
56+ if errors.Is(err, kvTypes.ErrVolumeNotFound) {
57+ continue
58+ }
3859 ctx.Logger().Error("fail to get volume", "asset", pool.Asset, "error", err)
3960 continue
4061 }
4162
4263 initialVolume := volume
4364
4465 // find all buckets between now and last update, in case the chain was
4566 // halted for a time larger than the bucket size
4667 var indexes []int64
4768 if volume.LastBucket >= 0 && volume.LastBucket != currentIndex {
4869 index := volume.LastBucket + 1
GetVolume error text changed'Total volume not found: pool=X' becomes 'total volume not found: pool=X'; no repo code matches the old string.keeper_volume.go:91 ↗look
8383 }
8484
8585 // GetVolume get the total (24h) volume for the given pool from kv store, returns error, if it doesn't exists
8686 func (k KVStore) GetVolume(ctx cosmos.Context, pool common.Asset) (Volume, error) {
8787 var record Volume
8888 pool = pool.GetLayer1Asset()
8989 found, err := k.getVolume(ctx, k.GetKey(prefixVolume, pool.String()), &record)
9090 if !found {
91− return Volume{}, fmt.Errorf("Total volume not found: pool=%s", pool.String())
91+ return Volume{}, fmt.Errorf("%w: pool=%s", types.ErrVolumeNotFound, pool.String())
9292 }
9393 return record, err
9494 }
9595
9696 // SetVolume save the total (24h) volume for the given pool to kv store
9797 func (k KVStore) SetVolume(ctx cosmos.Context, volume Volume) error {
9898 if err := volume.Valid(); err != nil {
9999 return err
100100 }
101101 k.setVolume(ctx, k.GetKey(prefixVolume, volume.Asset.String()), volume)
102102 return nil
103103 }
Synth test has no L1 poolNewVolume maps ETH/ETH to ETH.ETH; with no ETH.ETH pool the test proves the skip, but production's L1 pass rolls it anyway.manager_volume_current_test.go:74 ↗look
65+derivedAsset := common.BTCAsset.GetDerivedAsset()
66+derivedPool := NewPool()
67+derivedPool.Asset = derivedAsset
68+c.Assert(mgr.K.SetPool(ctx, derivedPool), IsNil)
69+
70+derivedVolume := types.NewVolume(derivedAsset)
71+derivedVolume.ChangeRune = cosmos.NewUint(10)
72+c.Assert(mgr.K.SetVolume(ctx, derivedVolume), IsNil)
73+
74+synthAsset := common.ETHAsset.GetSyntheticAsset()
75+synthPool := NewPool()
76+synthPool.Asset = synthAsset
77+c.Assert(mgr.K.SetPool(ctx, synthPool), IsNil)
78+
79+synthVolume := types.NewVolume(synthAsset)
80+synthVolume.ChangeAsset = cosmos.NewUint(20)
81+c.Assert(mgr.K.SetVolume(ctx, synthVolume), IsNil)
82+
83+c.Assert(mgr.volumeManager.EndBlock(ctx), IsNil)
84+
85+derivedVolume, err := mgr.K.GetVolume(ctx, derivedAsset)
86+c.Assert(err, IsNil)
87+c.Assert(derivedVolume.ChangeRune.String(), Equals, "10")

Ask an AI about this change

Chat assistantClaude, ChatGPT, … (reads a link)

Coding agentClaude Code, Cursor, … (reads the repo)

The context file is context.md. Use Copy with context for an AI that can't open links; it pastes the whole file in.

Glossary

Base-layer pool
In this change, a pool whose asset is an external L1 asset, TCY, or a whitelisted native token: the pools /thorchain/pools lists. isBaseLayerPool ↗
Consensus release
A change to state writes alters the app hash, so it must ship in a minor-version upgrade that all validators adopt at one height. docs/release.md ↗
Derived asset
A THOR.ASSET mirror of an L1 asset (THOR.BTC), with its own internal pool; TOR is the derived dollar anchor. Now excluded from volume. asset-notation.md ↗
ErrVolumeNotFound
New sentinel GetVolume wraps with %w when a pool has no volume record, so callers can test it with errors.Is. keeper/types/types.go ↗
GetLayer1Asset
Strips the synth, trade and secured flags to give the L1 asset; leaves derived assets unchanged. GetVolume and NewVolume both apply it. common/asset.go ↗
Synth / savers pool
A CHAIN/ASSET pool record (BTC/BTC) backing savers. GetLayer1Asset maps it to the L1 asset, so it used to re-read the L1 volume record. asset-notation.md ↗
TCY
THOR.TCY, a native THORChain token with its own base-layer pool; IsTCY is one of the three filter branches. common/asset.go ↗
Volume bucket
One of 96 fifteen-minute slots (VolumeBucketSeconds = 900) that end block rolls the change into, subtracting the slot's old amount from the total. manager_volume_current.go ↗
Volume record
Per-pool store entry with this block's change, a rolling 24h total and the last bucket index; surfaced as volume_24h by the pools query. type_volume.go ↗ keeper_volume.go ↗
Whitelisted native asset
Native tokens such as RUJI, NAMI and AUTO that IsWhitelisted allows to have base-layer pools. common/asset.go ↗