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] update validators VP #550

Merged
merged 4 commits into from
Mar 29, 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

#### [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
Expand Down
2 changes: 1 addition & 1 deletion database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ VALUES `

stmt += fmt.Sprintf("($%d,$%d,$%d,$%d,$%d,$%d),", si+1, si+2, si+3, si+4, si+5, si+6)
args = append(args,
snapshot.ProposalID, snapshot.ValidatorConsAddress, snapshot.ValidatorVotingPower,
snapshot.ProposalID, snapshot.ValidatorConsAddress, snapshot.ValidatorVotingPower.String(),
snapshot.ValidatorStatus, snapshot.ValidatorJailed, snapshot.Height)
}

Expand Down
44 changes: 21 additions & 23 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"fmt"
"time"

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/gogo/protobuf/proto"

sdkmath "cosmossdk.io/math"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

"github.com/forbole/bdjuno/v4/types"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/forbole/bdjuno/v4/types"
"github.com/gogo/protobuf/proto"

dbtypes "github.com/forbole/bdjuno/v4/database/types"
)
Expand Down Expand Up @@ -640,15 +638,15 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
types.NewProposalValidatorStatusSnapshot(
1,
validator1.GetConsAddr(),
100,
sdkmath.NewInt(100),
int(stakingtypes.Bonded),
false,
10,
),
types.NewProposalValidatorStatusSnapshot(
1,
validator2.GetConsAddr(),
100,
sdkmath.NewInt(100),
int(stakingtypes.Unbonding),
true,
10,
Expand All @@ -666,7 +664,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
1,
1,
validator1.GetConsAddr(),
100,
"100",
3,
false,
10,
Expand All @@ -675,7 +673,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
2,
1,
validator2.GetConsAddr(),
100,
"100",
2,
true,
10,
Expand All @@ -689,15 +687,15 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
types.NewProposalValidatorStatusSnapshot(
1,
validator1.GetConsAddr(),
10,
sdkmath.NewInt(10),
int(stakingtypes.Bonded),
true,
9,
),
types.NewProposalValidatorStatusSnapshot(
1,
validator2.GetConsAddr(),
700,
sdkmath.NewInt(700),
int(stakingtypes.Unbonding),
true,
9,
Expand All @@ -715,7 +713,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
1,
1,
validator1.GetConsAddr(),
100,
"100",
3,
false,
10,
Expand All @@ -724,7 +722,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
2,
1,
validator2.GetConsAddr(),
100,
"100",
2,
true,
10,
Expand All @@ -738,15 +736,15 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
types.NewProposalValidatorStatusSnapshot(
1,
validator1.GetConsAddr(),
10,
sdkmath.NewInt(10),
int(stakingtypes.Bonded),
true,
10,
),
types.NewProposalValidatorStatusSnapshot(
1,
validator2.GetConsAddr(),
700,
sdkmath.NewInt(700),
int(stakingtypes.Unbonding),
true,
10,
Expand All @@ -764,7 +762,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
1,
1,
validator1.GetConsAddr(),
10,
"10",
3,
true,
10,
Expand All @@ -773,7 +771,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
2,
1,
validator2.GetConsAddr(),
700,
"700",
2,
true,
10,
Expand All @@ -787,15 +785,15 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
types.NewProposalValidatorStatusSnapshot(
1,
validator1.GetConsAddr(),
100000,
sdkmath.NewInt(100000),
int(stakingtypes.Unspecified),
false,
11,
),
types.NewProposalValidatorStatusSnapshot(
1,
validator2.GetConsAddr(),
700000,
sdkmath.NewInt(700000),
int(stakingtypes.Unbonded),
false,
11,
Expand All @@ -813,7 +811,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
1,
1,
validator1.GetConsAddr(),
100000,
"100000",
0,
false,
11,
Expand All @@ -822,7 +820,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot
2,
1,
validator2.GetConsAddr(),
700000,
"700000",
1,
false,
11,
Expand Down
8 changes: 4 additions & 4 deletions database/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveInflation() {
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "no duplicated inflation rows should be inserted")

expected := dbtypes.NewInflationRow(100.50, 100)
expected := dbtypes.NewInflationRow(0.01, 100)
suite.Require().True(expected.Equal(rows[0]))

// ---------------------------------------------------------------------------------------------------------------
Expand All @@ -38,7 +38,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveInflation() {
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "no duplicated inflation rows should be inserted")

expected = dbtypes.NewInflationRow(100.50, 100)
expected = dbtypes.NewInflationRow(0.01, 100)
suite.Require().True(expected.Equal(rows[0]), "data should not change with lower height")

// ---------------------------------------------------------------------------------------------------------------
Expand All @@ -53,7 +53,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveInflation() {
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "no duplicated inflation rows should be inserted")

expected = dbtypes.NewInflationRow(300.00, 100)
expected = dbtypes.NewInflationRow(0.03, 100)
suite.Require().True(expected.Equal(rows[0]), "data should change with same height")

// ---------------------------------------------------------------------------------------------------------------
Expand All @@ -68,7 +68,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveInflation() {
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "no duplicated inflation rows should be inserted")

expected = dbtypes.NewInflationRow(400.00, 110)
expected = dbtypes.NewInflationRow(0.04, 110)
suite.Require().True(expected.Equal(rows[0]), "data should change with higher height")
}

Expand Down
2 changes: 1 addition & 1 deletion database/schema/03-staking.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CREATE INDEX validator_commission_height_index ON validator_commission (height);
CREATE TABLE validator_voting_power
(
validator_address TEXT NOT NULL REFERENCES validator (consensus_address) PRIMARY KEY,
voting_power BIGINT NOT NULL,
voting_power TEXT NOT NULL,
height BIGINT NOT NULL REFERENCES block (height)
);
CREATE INDEX validator_voting_power_height_index ON validator_voting_power (height);
Expand Down
2 changes: 1 addition & 1 deletion database/schema/08-gov.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ CREATE TABLE proposal_validator_status_snapshot
id SERIAL PRIMARY KEY NOT NULL,
proposal_id INTEGER REFERENCES proposal (id),
validator_address TEXT NOT NULL REFERENCES validator (consensus_address),
voting_power BIGINT NOT NULL,
voting_power TEXT NOT NULL,
status INT NOT NULL,
jailed BOOLEAN NOT NULL,
height BIGINT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion database/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (db *Db) SaveValidatorsVotingPowers(entries []types.ValidatorVotingPower) e
for i, entry := range entries {
pi := i * 3
stmt += fmt.Sprintf("($%d,$%d,$%d),", pi+1, pi+2, pi+3)
params = append(params, entry.ConsensusAddress, entry.VotingPower, entry.Height)
params = append(params, entry.ConsensusAddress, entry.VotingPower.String(), entry.Height)
}

stmt = stmt[:len(stmt)-1]
Expand Down
23 changes: 11 additions & 12 deletions database/staking_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package database_test
import (
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/forbole/bdjuno/v4/types"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

dbtypes "github.com/forbole/bdjuno/v4/database/types"
"github.com/forbole/bdjuno/v4/types"
)

func newDecPts(value int64, prec int64) *sdk.Dec {
dec := sdk.NewDecWithPrec(value, prec)
return &dec
}

func newIntPtr(value int64) *sdk.Int {
func newIntPtr(value int64) *sdkmath.Int {
val := sdk.NewInt(value)
return &val
}
Expand Down Expand Up @@ -571,15 +570,15 @@ func (suite *DbTestSuite) TestSaveValidatorsVotingPowers() {

// Save data
err := suite.database.SaveValidatorsVotingPowers([]types.ValidatorVotingPower{
types.NewValidatorVotingPower(validator1.GetConsAddr(), 1000, 10),
types.NewValidatorVotingPower(validator2.GetConsAddr(), 2000, 10),
types.NewValidatorVotingPower(validator1.GetConsAddr(), sdkmath.NewInt(1000), 10),
types.NewValidatorVotingPower(validator2.GetConsAddr(), sdkmath.NewInt(2000), 10),
})
suite.Require().NoError(err)

// Verify the data
expected := []dbtypes.ValidatorVotingPowerRow{
dbtypes.NewValidatorVotingPowerRow(validator1.GetConsAddr(), 1000, 10),
dbtypes.NewValidatorVotingPowerRow(validator2.GetConsAddr(), 2000, 10),
dbtypes.NewValidatorVotingPowerRow(validator1.GetConsAddr(), "1000", 10),
dbtypes.NewValidatorVotingPowerRow(validator2.GetConsAddr(), "2000", 10),
}

var result []dbtypes.ValidatorVotingPowerRow
Expand All @@ -592,15 +591,15 @@ func (suite *DbTestSuite) TestSaveValidatorsVotingPowers() {

// Update the data
err = suite.database.SaveValidatorsVotingPowers([]types.ValidatorVotingPower{
types.NewValidatorVotingPower(validator1.GetConsAddr(), 5, 9),
types.NewValidatorVotingPower(validator2.GetConsAddr(), 10, 11),
types.NewValidatorVotingPower(validator1.GetConsAddr(), sdkmath.NewInt(5), 9),
types.NewValidatorVotingPower(validator2.GetConsAddr(), sdkmath.NewInt(10), 11),
})
suite.Require().NoError(err)

// Verify the data
expected = []dbtypes.ValidatorVotingPowerRow{
dbtypes.NewValidatorVotingPowerRow(validator1.GetConsAddr(), 1000, 10),
dbtypes.NewValidatorVotingPowerRow(validator2.GetConsAddr(), 10, 11),
dbtypes.NewValidatorVotingPowerRow(validator1.GetConsAddr(), "1000", 10),
dbtypes.NewValidatorVotingPowerRow(validator2.GetConsAddr(), "10", 11),
}

result = []dbtypes.ValidatorVotingPowerRow{}
Expand Down
4 changes: 2 additions & 2 deletions database/types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ type ProposalValidatorVotingPowerSnapshotRow struct {
ID int64 `db:"id"`
ProposalID int64 `db:"proposal_id"`
ValidatorAddress string `db:"validator_address"`
VotingPower int64 `db:"voting_power"`
VotingPower string `db:"voting_power"`
Status int `db:"status"`
Jailed bool `db:"jailed"`
Height int64 `db:"height"`
}

func NewProposalValidatorVotingPowerSnapshotRow(
id int64, proposalID int64, validatorAddr string, votingPower int64, status int, jailed bool, height int64,
id int64, proposalID int64, validatorAddr string, votingPower string, status int, jailed bool, height int64,
) ProposalValidatorVotingPowerSnapshotRow {
return ProposalValidatorVotingPowerSnapshotRow{
ID: id,
Expand Down
4 changes: 2 additions & 2 deletions database/types/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ func (v ValidatorCommissionRow) Equal(w ValidatorCommissionRow) bool {
// ValidatorVotingPowerRow represents a single row of the validator_voting_power database table
type ValidatorVotingPowerRow struct {
ValidatorAddress string `db:"validator_address"`
VotingPower int64 `db:"voting_power"`
VotingPower string `db:"voting_power"`
Height int64 `db:"height"`
}

// NewValidatorVotingPowerRow allows to easily build a new ValidatorVotingPowerRow instance
func NewValidatorVotingPowerRow(address string, votingPower int64, height int64) ValidatorVotingPowerRow {
func NewValidatorVotingPowerRow(address string, votingPower string, height int64) ValidatorVotingPowerRow {
return ValidatorVotingPowerRow{
ValidatorAddress: address,
VotingPower: votingPower,
Expand Down
12 changes: 1 addition & 11 deletions modules/staking/utils_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package staking
import (
"fmt"

juno "github.com/forbole/juno/v4/types"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/forbole/bdjuno/v4/modules/staking/keybase"
Expand Down Expand Up @@ -218,20 +217,11 @@ func (m *Module) GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultVa
return nil, err
}

// Find the voting power of this validator
var votingPower int64 = 0
for _, blockVal := range vals.Validators {
blockValConsAddr := juno.ConvertValidatorAddressToBech32String(blockVal.Address)
if blockValConsAddr == consAddr.String() {
votingPower = blockVal.VotingPower
}
}

if found, _ := m.db.HasValidator(consAddr.String()); !found {
continue
}

votingPowers[index] = types.NewValidatorVotingPower(consAddr.String(), votingPower, height)
votingPowers[index] = types.NewValidatorVotingPower(consAddr.String(), validator.Tokens, height)
}

return votingPowers, nil
Expand Down
Loading