Skip to content

Commit

Permalink
analyzer/runtime: limit total_{received,sent} to native denomination
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Mar 28, 2024
1 parent b789dca commit 2594c86
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changelog/679.bugfix.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
analyzer/runtime: limit total\_{received,sent} to native denomination

Previously, those metrics tracked the sum of all SDK denominations. This bug
did not impact Sapphire, Emerald, or Cipher; they each only have one SDK
denomination, i.e. the native one.
3 changes: 2 additions & 1 deletion analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,12 @@ var (
ON CONFLICT (runtime, address) DO UPDATE
SET total_received = accounts.total_received + $3`

// $3 should be the symbol of the _native_ token.
RuntimeAccountTotalReceivedRecompute = `
WITH agg AS (
SELECT runtime, receiver, sum(amount) AS total_received
FROM chain.runtime_transfers
WHERE runtime = $1::runtime AND round <= $2::bigint AND receiver IS NOT NULL
WHERE runtime = $1::runtime AND round <= $2::bigint AND receiver IS NOT NULL AND symbol = $3
GROUP BY 1, 2
)
INSERT INTO chain.runtime_accounts as accts (runtime, address, total_received)
Expand Down
28 changes: 16 additions & 12 deletions analyzer/runtime/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ func (m *processor) queueMint(batch *storage.QueryBatch, round uint64, e account
m.StringifyDenomination(e.Amount.Denomination),
e.Amount.Amount.String(),
)
batch.Queue(
queries.RuntimeAccountTotalReceivedUpsert,
m.runtime,
e.Owner.String(),
e.Amount.Amount.String(),
)
if e.Amount.Denomination.IsNative() {
batch.Queue(
queries.RuntimeAccountTotalReceivedUpsert,
m.runtime,
e.Owner.String(),
e.Amount.Amount.String(),
)
}
}
}

Expand All @@ -89,12 +91,14 @@ func (m *processor) queueBurn(batch *storage.QueryBatch, round uint64, e account
m.StringifyDenomination(e.Amount.Denomination),
(&big.Int{}).Neg(e.Amount.Amount.ToBigInt()).String(),
)
batch.Queue(
queries.RuntimeAccountTotalSentUpsert,
m.runtime,
e.Owner.String(),
e.Amount.Amount.String(),
)
if e.Amount.Denomination.IsNative() {
batch.Queue(
queries.RuntimeAccountTotalSentUpsert,
m.runtime,
e.Owner.String(),
e.Amount.Amount.String(),
)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (m *processor) FinalizeFastSync(ctx context.Context, lastFastSyncHeight int
batch.Queue(queries.RuntimeAccountTotalSentRecompute, m.runtime, lastFastSyncHeight)

m.logger.Info("recomputing total_received for every account")
batch.Queue(queries.RuntimeAccountTotalReceivedRecompute, m.runtime, lastFastSyncHeight)
batch.Queue(queries.RuntimeAccountTotalReceivedRecompute, m.runtime, lastFastSyncHeight, m.nativeTokenSymbol())

m.logger.Info("recomputing gas_for_calling for every contract")
batch.Queue(queries.RuntimeAccountGasForCallingRecompute, m.runtime, lastFastSyncHeight)
Expand Down
4 changes: 2 additions & 2 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2843,10 +2843,10 @@ components:
properties:
total_sent:
allOf: [$ref: '#/components/schemas/TextBigInt']
description: The total number of tokens sent, in base units.
description: The total amount of native tokens sent, in base units.
total_received:
allOf: [$ref: '#/components/schemas/TextBigInt']
description: The total number of tokens received, in base units.
description: The total amount of native tokens received, in base units.
num_txns:
description: The total number of transactions this account was involved with.
type: integer
Expand Down
2 changes: 1 addition & 1 deletion scripts/git-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ staged_fragments="$(git diff --cached --name-only -- .changelog/*.md)"
# Re-wraps file $1 to 80 characters per line, using prettier, in-place.
rewrap() {
# Suppress the output of prettier, as it's overly verbose. Show only on errors.
prettier --no-config --no-editorconfig --prose-wrap always --print-width 80 --write "$f" >/tmp/prettier.out \
prettier --no-config --no-editorconfig --prose-wrap always --print-width 78 --write "$f" >/tmp/prettier.out \
|| { cat /tmp/prettier.out; exit 1; }
}

Expand Down

0 comments on commit 2594c86

Please sign in to comment.