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: update proposal snapshots handling on block #523

Merged
merged 3 commits into from
Feb 14, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#### CI
- ([\#508](https://github.com/forbole/bdjuno/pull/508)) Upgrade workflow golangci version to v1.50.1


#### Parse Command
- ([\#492](https://github.com/forbole/bdjuno/pull/492)) Add parse command for periodic tasks: `x/bank` total supply, `x/distribution` community pool, `x/mint` inflation, `pricefeed` token price and price history, `x/staking` staking pool

Expand All @@ -20,6 +19,7 @@
- ([\#465](https://github.com/forbole/bdjuno/pull/465)) Get open proposal ids in deposit or voting period by block time instead of current time
- ([\#489](https://github.com/forbole/bdjuno/pull/489)) Remove block height foreign key from proposal_vote and proposal_deposit tables and add column timestamp
- ([\#499](https://github.com/forbole/bdjuno/pull/499)) Check if proposal has passed voting end time before marking it invalid
- ([\#523](https://github.com/forbole/bdjuno/pull/523)) Update proposal snapshots handling on block

#### Daily refetch
- ([\#454](https://github.com/forbole/bdjuno/pull/454)) Added `daily refetch` module to refetch missing blocks every day
Expand Down
16 changes: 4 additions & 12 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() {
// ----------------------------------------------------------------------------------------------------------------
// Save snapshot

snapshot := types.NewProposalStakingPoolSnapshot(1, types.NewPool(
snapshot := types.NewProposalStakingPoolSnapshot(1, types.NewPoolSnapshot(
sdk.NewInt(100),
sdk.NewInt(200),
sdk.NewInt(20),
sdk.NewInt(30),
10,
))
err := suite.database.SaveProposalStakingPoolSnapshot(snapshot)
Expand All @@ -552,11 +550,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() {
// ----------------------------------------------------------------------------------------------------------------
// Update with lower height

err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool(
err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPoolSnapshot(
sdk.NewInt(200),
sdk.NewInt(500),
sdk.NewInt(14),
sdk.NewInt(10),
9,
)))
suite.Require().NoError(err)
Expand All @@ -575,11 +571,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() {
// ----------------------------------------------------------------------------------------------------------------
// Update with same height

err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool(
err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPoolSnapshot(
sdk.NewInt(500),
sdk.NewInt(1000),
sdk.NewInt(20),
sdk.NewInt(30),
10,
)))
suite.Require().NoError(err)
Expand All @@ -598,11 +592,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() {
// ----------------------------------------------------------------------------------------------------------------
// Update with higher height

err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool(
err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPoolSnapshot(
sdk.NewInt(1000),
sdk.NewInt(2000),
sdk.NewInt(80),
sdk.NewInt(40),
11,
)))
suite.Require().NoError(err)
Expand Down
1 change: 1 addition & 0 deletions modules/gov/expected_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type SlashingModule interface {

type StakingModule interface {
GetStakingPool(height int64) (*types.Pool, error)
GetStakingPoolSnapshot(height int64) (*types.PoolSnapshot, error)
GetValidatorsWithStatus(height int64, status string) ([]stakingtypes.Validator, []types.Validator, error)
GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultValidators) ([]types.ValidatorVotingPower, error)
GetValidatorsStatuses(height int64, validators []stakingtypes.Validator) ([]types.ValidatorStatus, error)
Expand Down
9 changes: 7 additions & 2 deletions modules/gov/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ func (m *Module) updateProposals(height int64, blockTime time.Time, blockVals *t
return fmt.Errorf("error while updating proposal: %s", err)
}

err = m.UpdateProposalSnapshots(height, blockVals, id)
err = m.UpdateProposalValidatorStatusesSnapshot(height, blockVals, id)
if err != nil {
return fmt.Errorf("error while updating proposal snapshots: %s", err)
return fmt.Errorf("error while updating proposal validator statuses snapshots: %s", err)
}

err = m.UpdateProposalStakingPoolSnapshot(height, blockVals, id)
if err != nil {
return fmt.Errorf("error while updating proposal validator statuses snapshots: %s", err)
}
}
return nil
Expand Down
16 changes: 10 additions & 6 deletions modules/gov/utils_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ func (m *Module) UpdateProposal(height int64, blockTime time.Time, id uint64) er
return nil
}

func (m *Module) UpdateProposalSnapshots(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
err := m.updateProposalStakingPoolSnapshot(height, id)
func (m *Module) UpdateProposalValidatorStatusesSnapshot(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
err := m.updateProposalValidatorStatusesSnapshot(height, id, blockVals)
if err != nil {
return fmt.Errorf("error while updating proposal staking pool snapshot: %s", err)
return fmt.Errorf("error while updating proposal validator statuses snapshot: %s", err)
}

err = m.updateProposalValidatorStatusesSnapshot(height, id, blockVals)
return nil
}

func (m *Module) UpdateProposalStakingPoolSnapshot(height int64, blockVals *tmctypes.ResultValidators, id uint64) error {
err := m.updateProposalStakingPoolSnapshot(height, id)
if err != nil {
return fmt.Errorf("error while updating proposal validator statuses snapshot: %s", err)
return fmt.Errorf("error while updating proposal staking pool snapshot: %s", err)
}

return nil
Expand Down Expand Up @@ -192,7 +196,7 @@ func (m *Module) updateAccounts(proposal govtypes.Proposal) error {
// updateProposalStakingPoolSnapshot updates the staking pool snapshot associated with the gov
// proposal having the provided id
func (m *Module) updateProposalStakingPoolSnapshot(height int64, proposalID uint64) error {
pool, err := m.stakingModule.GetStakingPool(height)
pool, err := m.stakingModule.GetStakingPoolSnapshot(height)
if err != nil {
return fmt.Errorf("error while getting staking pool: %s", err)
}
Expand Down
9 changes: 9 additions & 0 deletions modules/staking/utils_staking_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) {
return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, unbondingTokens, stakedNotBondedTokens, height), nil
}

func (m *Module) GetStakingPoolSnapshot(height int64) (*types.PoolSnapshot, error) {
pool, err := m.source.GetPool(height)
if err != nil {
return nil, fmt.Errorf("error while getting staking pool snapshot: %s", err)
}

return types.NewPoolSnapshot(pool.BondedTokens, pool.NotBondedTokens, height), nil
}

func (m *Module) getTotalUnbondingDelegationsFromValidator(height int64, valOperatorAddress string) []stakingtypes.UnbondingDelegation {
var unbondingDelegations []stakingtypes.UnbondingDelegation
var nextKey []byte
Expand Down
4 changes: 2 additions & 2 deletions types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ func NewTallyResult(
// ProposalStakingPoolSnapshot contains the data about a single staking pool snapshot to be associated with a proposal
type ProposalStakingPoolSnapshot struct {
ProposalID uint64
Pool *Pool
Pool *PoolSnapshot
}

// NewProposalStakingPoolSnapshot returns a new ProposalStakingPoolSnapshot instance
func NewProposalStakingPoolSnapshot(proposalID uint64, pool *Pool) ProposalStakingPoolSnapshot {
func NewProposalStakingPoolSnapshot(proposalID uint64, pool *PoolSnapshot) ProposalStakingPoolSnapshot {
return ProposalStakingPoolSnapshot{
ProposalID: proposalID,
Pool: pool,
Expand Down
16 changes: 16 additions & 0 deletions types/staking_pool_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ func NewPool(bondedTokens, notBondedTokens, unbondingTokens, stakedNotBondedToke
}
}

// PoolSnapshot contains the data of the staking pool snapshot at the given height
type PoolSnapshot struct {
BondedTokens sdk.Int
NotBondedTokens sdk.Int
Height int64
}

// NewPoolSnapshot allows to build a new PoolSnapshot instance
func NewPoolSnapshot(bondedTokens, notBondedTokens sdk.Int, height int64) *PoolSnapshot {
return &PoolSnapshot{
BondedTokens: bondedTokens,
NotBondedTokens: notBondedTokens,
Height: height,
}
}

// --------------------------------------------------------------------------------------------------------------------

// StakingParams represents the parameters of the x/staking module
Expand Down