Skip to content

Commit

Permalink
Merge pull request #698 from oasisprotocol/andrew7234/consensus-accou…
Browse files Browse the repository at this point in the history
…nt-stats

Andrew7234/consensus account stats
  • Loading branch information
Andrew7234 authored May 22, 2024
2 parents 68cfa93 + 92ccea0 commit c6ebbe7
Show file tree
Hide file tree
Showing 19 changed files with 2,479 additions and 608 deletions.
1 change: 1 addition & 0 deletions .changelog/698.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api: add num_txns stat for consensus accounts
6 changes: 4 additions & 2 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,7 @@ components:

Account:
type: object
required: [address, nonce, available, escrow, debonding, allowances]
required: [address, nonce, available, escrow, debonding, allowances, stats]
properties:
address:
type: string
Expand Down Expand Up @@ -2128,6 +2128,8 @@ components:
items:
allOf: [$ref: '#/components/schemas/Allowance']
description: The allowances made by this account.
stats:
allOf: [$ref: '#/components/schemas/AccountStats']
description: |
A consensus layer account.
Expand Down Expand Up @@ -2966,7 +2968,7 @@ components:
AccountStats:
type: object
required: [total_sent, total_received, num_txns]
required: [num_txns]
properties:
total_sent:
allOf: [$ref: '#/components/schemas/TextBigInt']
Expand Down
15 changes: 13 additions & 2 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,17 @@ func (c *StorageClient) Account(ctx context.Context, address staking.Address) (*
a.Allowances = append(a.Allowances, al)
}

err = c.db.QueryRow(
ctx,
queries.AccountStats,
address.String(),
).Scan(
&a.Stats.NumTxns,
)
if err != nil {
return nil, wrapError(err)
}

return &a, nil
}

Expand Down Expand Up @@ -1736,8 +1747,8 @@ func (c *StorageClient) RuntimeAccount(ctx context.Context, address staking.Addr
case nil:
case pgx.ErrNoRows:
// If an account address has no activity, default to 0.
a.Stats.TotalSent = common.NewBigInt(0)
a.Stats.TotalReceived = common.NewBigInt(0)
a.Stats.TotalSent = common.Ptr(common.NewBigInt(0))
a.Stats.TotalReceived = common.Ptr(common.NewBigInt(0))
a.Stats.NumTxns = 0
default:
return nil, wrapError(err)
Expand Down
6 changes: 6 additions & 0 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ const (
FROM chain.accounts
WHERE address = $1::text`

AccountStats = `
SELECT
COUNT(*)
FROM chain.accounts_related_transactions
WHERE account_address = $1::text`

AccountAllowances = `
SELECT beneficiary, allowance
FROM chain.allowances
Expand Down
1 change: 1 addition & 0 deletions tests/e2e_regression/common_test_cases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ commonTestCases=(
'blocks /v1/consensus/blocks'
'bad_account /v1/consensus/accounts/oasis1aaaaaaa'
'account /v1/consensus/accounts/oasis1qp0302fv0gz858azasg663ax2epakk5fcssgza7j'
'account_with_tx /v1/consensus/accounts/oasis1qpn83e8hm3gdhvpfv66xj3qsetkj3ulmkugmmxn3'
'runtime-only_account /v1/consensus/accounts/oasis1qphyxz5csvprhnn09r49nuyzl0jdw0wsj5xpvsg2'
'delegations /v1/consensus/accounts/oasis1qpk366qvtjrfrthjp3xuej5mhvvtnkr8fy02hm2s/delegations'
'delegations_to /v1/consensus/accounts/oasis1qp0j5v5mkxk3eg4kxfdsk8tj6p22g4685qk76fw6/delegations_to'
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e_regression/damask/expected/account.body
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"debonding_delegations_balance": "0",
"delegations_balance": "0",
"escrow": "0",
"nonce": 5
"nonce": 5,
"stats": {
"num_txns": 0
}
}
13 changes: 13 additions & 0 deletions tests/e2e_regression/damask/expected/account_with_tx.body
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"address": "oasis1qpn83e8hm3gdhvpfv66xj3qsetkj3ulmkugmmxn3",
"allowances": [],
"available": "98499977445996",
"debonding": "12381788591465821",
"debonding_delegations_balance": "0",
"delegations_balance": "6250650662907336",
"escrow": "224562518782971131",
"nonce": 8,
"stats": {
"num_txns": 2
}
}
6 changes: 6 additions & 0 deletions tests/e2e_regression/damask/expected/account_with_tx.headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date: UNINTERESTING
Content-Length: UNINTERESTING

Loading

0 comments on commit c6ebbe7

Please sign in to comment.