From 1a2df888973ab449d1d37c137ef77db07442744b Mon Sep 17 00:00:00 2001 From: Magic Cat <37407870+MonikaCat@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:56:36 +0700 Subject: [PATCH] feat: [cheqd] add `height` column to top accounts table (#555) ## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch - [ ] provided a link to the relevant issue or specification - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- .github/workflows/lint.yml | 2 +- CHANGELOG.md | 9 +------- database/schema/13-top_accounts.sql | 4 +++- database/top_accounts.go | 21 +++++++++++-------- database/top_accounts_test.go | 14 ++++++------- database/types/top_accounts.go | 7 +++++-- .../bdjuno/tables/public_top_accounts.yaml | 1 + modules/top_accounts/handle_msg.go | 10 ++++----- .../handle_periodic_operations.go | 2 +- modules/top_accounts/utils_refresh.go | 6 +++--- modules/top_accounts/utils_refresh_all.go | 2 +- 11 files changed, 40 insertions(+), 38 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c9ab33910..378d2eded 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -23,7 +23,7 @@ jobs: .sum - uses: golangci/golangci-lint-action@v3.4.0 with: - version: v1.50.1 + version: v1.51.1 args: --timeout 10m github-token: ${{ secrets.GITHUB_TOKEN }} if: "env.GIT_DIFF != ''" diff --git a/CHANGELOG.md b/CHANGELOG.md index f0fbba687..527b850f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,19 +4,12 @@ #### [cheqd] - ([\#545](https://github.com/forbole/bdjuno/pull/545)) Added `x/did` and `x/resource` module handlers to store data in db - ([\#550](https://github.com/forbole/bdjuno/pull/550)) Updated validators VP handlers, `voting_power` column type to TEXT, updated VotingPower type to `sdkmath.Int` for ValidatorVotingPower and ProposalValidatorStatusSnapshot struct - -pdated VotingPower type to sdkmath.Int for ValidatorVotingPower, `voting_power` column type to TEXT - -Updated validators VP handlers, `voting_power` column type to TEXT inside `validator_voting_power` and `proposal_validator_status_snapshot` table and VotingPower type to sdkmath.Int for ValidatorVotingPower type - - - - #### [cheqd] Top Accounts Module - ([\#510](https://github.com/forbole/bdjuno/pull/510)) Implemented `top_accounts` module to store chain native token's balance for ranking - ([\#511](https://github.com/forbole/bdjuno/pull/511)) Implemented parse cmd for `top_accounts` module - ([\#520](https://github.com/forbole/bdjuno/pull/520)) Upgraded `cheqd-node` to `v1.2.2` and proposals parsing to correctly parse `v0.46.x` gov proposals - ([\#547](https://github.com/forbole/bdjuno/pull/547)) Upgraded `cheqd-node` to `v1.3.0` and added MsgDeactivateDidDoc handling +- ([\#555](https://github.com/forbole/bdjuno/pull/555)) Added `height` column to top accounts table #### CI - ([\#508](https://github.com/forbole/bdjuno/pull/508)) Upgrade workflow golangci version to v1.50.1 diff --git a/database/schema/13-top_accounts.sql b/database/schema/13-top_accounts.sql index b5d5ecb68..6343bebcb 100644 --- a/database/schema/13-top_accounts.sql +++ b/database/schema/13-top_accounts.sql @@ -6,9 +6,11 @@ CREATE TABLE top_accounts redelegation BIGINT DEFAULT 0, unbonding BIGINT DEFAULT 0, reward BIGINT DEFAULT 0, - sum BIGINT NOT NULL DEFAULT 0 + sum BIGINT NOT NULL DEFAULT 0, + height BIGINT NOT NULL ); CREATE INDEX top_accounts_sum_index ON top_accounts (sum); +CREATE INDEX top_accounts_height_index ON top_accounts (height); CREATE TABLE top_accounts_params ( diff --git a/database/top_accounts.go b/database/top_accounts.go index 38f480eb5..e64417031 100644 --- a/database/top_accounts.go +++ b/database/top_accounts.go @@ -11,19 +11,19 @@ func (db *Db) SaveTopAccountsBalance(column string, bals []types.NativeTokenAmou return nil } - stmt := fmt.Sprintf("INSERT INTO top_accounts (address, %s) VALUES ", column) + stmt := fmt.Sprintf("INSERT INTO top_accounts (address, %s, height) VALUES ", column) var params []interface{} for i, bal := range bals { - bi := i * 2 - stmt += fmt.Sprintf("($%d, $%d),", bi+1, bi+2) + bi := i * 3 + stmt += fmt.Sprintf("($%d, $%d, $%d),", bi+1, bi+2, bi+3) - params = append(params, bal.Address, bal.Balance.String()) + params = append(params, bal.Address, bal.Balance.String(), bal.Height) } stmt = stmt[:len(stmt)-1] - stmt += fmt.Sprintf("ON CONFLICT (address) DO UPDATE SET %s = excluded.%s ", column, column) + stmt += fmt.Sprintf("ON CONFLICT (address) DO UPDATE SET %s = excluded.%s,height = excluded.height WHERE top_accounts.height <= excluded.height", column, column) _, err := db.SQL.Exec(stmt, params...) return err @@ -44,11 +44,14 @@ as sum FROM top_accounts WHERE address = $1 return rows[0], nil } -func (db *Db) UpdateTopAccountsSum(address, sum string) error { - stmt := `INSERT INTO top_accounts (address, sum) VALUES ($1, $2) -ON CONFLICT (address) DO UPDATE SET sum = excluded.sum` +func (db *Db) UpdateTopAccountsSum(address, sum string, height int64) error { + stmt := `INSERT INTO top_accounts (address, sum, height) VALUES ($1, $2, $3) +ON CONFLICT (address) DO UPDATE SET + sum = excluded.sum, + height = excluded.height +WHERE top_accounts.height <= excluded.height` - _, err := db.SQL.Exec(stmt, address, sum) + _, err := db.SQL.Exec(stmt, address, sum, height) return err } diff --git a/database/top_accounts_test.go b/database/top_accounts_test.go index 28ee5d148..bbe5682f9 100644 --- a/database/top_accounts_test.go +++ b/database/top_accounts_test.go @@ -31,11 +31,11 @@ func (suite *DbTestSuite) TestSaveTopAccountsBalance() { err = suite.database.SaveTopAccountsBalance("reward", []types.NativeTokenAmount{amount}) suite.Require().NoError(err) - err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "500") + err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "500", 100) suite.Require().NoError(err) // Verify data - expected := dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 100, 100, 100, 100, 100, 500) + expected := dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 100, 100, 100, 100, 100, 500, 100) var rows []dbtypes.TopAccountsRow err = suite.database.Sqlx.Select(&rows, `SELECT * FROM top_accounts`) @@ -47,7 +47,7 @@ func (suite *DbTestSuite) TestSaveTopAccountsBalance() { newAmount := types.NewNativeTokenAmount( "cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", sdk.NewInt(200), - 10, + 200, ) err = suite.database.SaveTopAccountsBalance("available", []types.NativeTokenAmount{newAmount}) @@ -65,11 +65,11 @@ func (suite *DbTestSuite) TestSaveTopAccountsBalance() { err = suite.database.SaveTopAccountsBalance("reward", []types.NativeTokenAmount{newAmount}) suite.Require().NoError(err) - err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "1000") + err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "1000", 300) suite.Require().NoError(err) // Verify data - expected = dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 200, 200, 200, 200, 200, 1000) + expected = dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 200, 200, 200, 200, 200, 1000, 300) err = suite.database.Sqlx.Select(&rows, `SELECT * FROM top_accounts`) suite.Require().NoError(err) suite.Require().Len(rows, 1) @@ -120,7 +120,7 @@ func (suite *DbTestSuite) TestUpdateTopAccountsSum() { // Store top accounts sum amount := "100" - err := suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount) + err := suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount, 500) suite.Require().NoError(err) // Verify data @@ -132,7 +132,7 @@ func (suite *DbTestSuite) TestUpdateTopAccountsSum() { // Store different amount amount = "200" - err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount) + err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount, 500) suite.Require().NoError(err) // Verify data diff --git a/database/types/top_accounts.go b/database/types/top_accounts.go index a64871b02..e1315837a 100644 --- a/database/types/top_accounts.go +++ b/database/types/top_accounts.go @@ -8,10 +8,11 @@ type TopAccountsRow struct { Unbonding int64 `db:"unbonding"` Reward int64 `db:"reward"` Sum int64 `db:"sum"` + Height int64 `db:"height"` } func NewTopAccountsRow( - address string, available, delegation, redelegation, unbonding, reward, sum int64, + address string, available, delegation, redelegation, unbonding, reward, sum, height int64, ) TopAccountsRow { return TopAccountsRow{ Address: address, @@ -21,6 +22,7 @@ func NewTopAccountsRow( Unbonding: unbonding, Reward: reward, Sum: sum, + Height: height, } } @@ -32,5 +34,6 @@ func (a TopAccountsRow) Equals(b TopAccountsRow) bool { a.Redelegation == b.Redelegation && a.Unbonding == b.Unbonding && a.Reward == b.Reward && - a.Sum == b.Sum + a.Sum == b.Sum && + a.Height == b.Height } diff --git a/hasura/metadata/databases/bdjuno/tables/public_top_accounts.yaml b/hasura/metadata/databases/bdjuno/tables/public_top_accounts.yaml index 5fe8dcc4b..624200759 100644 --- a/hasura/metadata/databases/bdjuno/tables/public_top_accounts.yaml +++ b/hasura/metadata/databases/bdjuno/tables/public_top_accounts.yaml @@ -16,6 +16,7 @@ select_permissions: - unbonding - reward - sum + - height filter: {} limit: 100 role: anonymous diff --git a/modules/top_accounts/handle_msg.go b/modules/top_accounts/handle_msg.go index 7f7bbc21c..069bbfeab 100644 --- a/modules/top_accounts/handle_msg.go +++ b/modules/top_accounts/handle_msg.go @@ -30,7 +30,7 @@ func (m *Module) HandleMsg(index int, msg sdk.Msg, tx *juno.Tx) error { return fmt.Errorf("error while updating account available balances: %s", err) } - err = m.refreshTopAccountsSum(addresses) + err = m.refreshTopAccountsSum(addresses, tx.Height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum while refreshing balance: %s", err) } @@ -62,7 +62,7 @@ func (m *Module) handleMsgDelegate(height int64, delAddr string) error { return fmt.Errorf("error while refreshing delegations while handling MsgDelegate: %s", err) } - err = m.refreshTopAccountsSum([]string{delAddr}) + err = m.refreshTopAccountsSum([]string{delAddr}, height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum while handling MsgDelegate: %s", err) } @@ -78,7 +78,7 @@ func (m *Module) handleMsgBeginRedelegate( return fmt.Errorf("error while refreshing redelegations while handling MsgBeginRedelegate: %s", err) } - err = m.refreshTopAccountsSum([]string{delAddr}) + err = m.refreshTopAccountsSum([]string{delAddr}, tx.Height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum while handling MsgBeginRedelegate: %s", err) } @@ -112,7 +112,7 @@ func (m *Module) handleMsgUndelegate(tx *juno.Tx, index int, delAddr string) err return fmt.Errorf("error while refreshing undelegations while handling MsgUndelegate: %s", err) } - err = m.refreshTopAccountsSum([]string{delAddr}) + err = m.refreshTopAccountsSum([]string{delAddr}, tx.Height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum while handling MsgUndelegate: %s", err) } @@ -151,7 +151,7 @@ func (m *Module) handleMsgWithdrawDelegatorReward(height int64, delAddr string) return fmt.Errorf("error while updating account available balances with MsgWithdrawDelegatorReward: %s", err) } - err = m.refreshTopAccountsSum([]string{delAddr}) + err = m.refreshTopAccountsSum([]string{delAddr}, height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum while handling MsgWithdrawDelegatorReward: %s", err) } diff --git a/modules/top_accounts/handle_periodic_operations.go b/modules/top_accounts/handle_periodic_operations.go index 70c66af1f..9268c7c97 100644 --- a/modules/top_accounts/handle_periodic_operations.go +++ b/modules/top_accounts/handle_periodic_operations.go @@ -77,7 +77,7 @@ func (m *Module) RefreshRewards() error { return fmt.Errorf("error while refreshing delegators rewards: %s", err) } - err = m.refreshTopAccountsSum(delegators) + err = m.refreshTopAccountsSum(delegators, height) if err != nil { return fmt.Errorf("error while refreshing top accounts sum value: %s", err) } diff --git a/modules/top_accounts/utils_refresh.go b/modules/top_accounts/utils_refresh.go index a57aa82c4..1c2816858 100644 --- a/modules/top_accounts/utils_refresh.go +++ b/modules/top_accounts/utils_refresh.go @@ -7,14 +7,14 @@ import ( "github.com/rs/zerolog/log" ) -func (m *Module) refreshTopAccountsSum(addresses []string) error { +func (m *Module) refreshTopAccountsSum(addresses []string, height int64) error { for _, addr := range addresses { sum, err := m.db.GetAccountBalanceSum(addr) if err != nil { return fmt.Errorf("error while getting account balance sum : %s", err) } - err = m.db.UpdateTopAccountsSum(addr, sum) + err = m.db.UpdateTopAccountsSum(addr, sum, height) if err != nil { return fmt.Errorf("error while updating top accounts sum : %s", err) } @@ -60,7 +60,7 @@ func (m *Module) refreshBalance(height int64, address string) func() { Str("operation", "update balance").Msg("error while updating account available balances") } - err = m.refreshTopAccountsSum([]string{address}) + err = m.refreshTopAccountsSum([]string{address}, height) if err != nil { log.Error().Str("module", "top_accounts").Err(err). Str("operation", "update top accounts sum").Msg("error while refreshing top accounts sum while refreshing balance") diff --git a/modules/top_accounts/utils_refresh_all.go b/modules/top_accounts/utils_refresh_all.go index 9209ab49a..69081bd73 100644 --- a/modules/top_accounts/utils_refresh_all.go +++ b/modules/top_accounts/utils_refresh_all.go @@ -28,7 +28,7 @@ func (m *Module) RefreshAll(address string) error { return fmt.Errorf("error while refreshing rewards of account %s, error: %s", address, err) } - err = m.refreshTopAccountsSum([]string{address}) + err = m.refreshTopAccountsSum([]string{address}, 0) if err != nil { return fmt.Errorf("error while refreshing top accounts sum %s, error: %s", address, err) }