Skip to content

Commit

Permalink
feat: update proposal snapshots handling on block (forbole#523)
Browse files Browse the repository at this point in the history
Closes: #XXXX

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

---

*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
- [x] 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`
- [x] 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

*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)
  • Loading branch information
MonikaCat authored and ankurdotb committed Feb 17, 2023
1 parent b33d382 commit bb3ff13
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
16 changes: 4 additions & 12 deletions database/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,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 @@ -557,11 +555,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 @@ -580,11 +576,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 @@ -603,11 +597,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 govtypesv1beta1.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 @@ -292,11 +292,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

0 comments on commit bb3ff13

Please sign in to comment.