Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [cheqd] add height column to top accounts table #555

Merged
merged 7 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ''"
9 changes: 1 addition & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion database/schema/13-top_accounts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down
21 changes: 12 additions & 9 deletions database/top_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

}
Expand Down
14 changes: 7 additions & 7 deletions database/top_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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})
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions database/types/top_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +22,7 @@ func NewTopAccountsRow(
Unbonding: unbonding,
Reward: reward,
Sum: sum,
Height: height,
}
}

Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ select_permissions:
- unbonding
- reward
- sum
- height
filter: {}
limit: 100
role: anonymous
10 changes: 5 additions & 5 deletions modules/top_accounts/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/top_accounts/handle_periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions modules/top_accounts/utils_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion modules/top_accounts/utils_refresh_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down