From 41c2f006daae606ca8513d525631af088639b0e2 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 14 Sep 2022 20:08:48 +0800 Subject: [PATCH 01/15] refac UpdateProposal --- cmd/parse/staking/validators.go | 13 ++----- modules/gov/expected_modules.go | 1 + modules/gov/utils_proposal.go | 54 +++++++++++++++++------------ modules/staking/utils_validators.go | 19 ++++++++++ 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/cmd/parse/staking/validators.go b/cmd/parse/staking/validators.go index ffe67febe..2497bd786 100644 --- a/cmd/parse/staking/validators.go +++ b/cmd/parse/staking/validators.go @@ -41,18 +41,9 @@ func validatorsCmd(parseConfig *parsecmdtypes.Config) *cobra.Command { return fmt.Errorf("error while getting latest block height: %s", err) } - // Get all validators - validators, err := sources.StakingSource.GetValidatorsWithStatus(height, "") + err = stakingModule.RefreshAllValidatorInfos(height) if err != nil { - return fmt.Errorf("error while getting validators: %s", err) - } - - // Refresh each validator - for _, validator := range validators { - err = stakingModule.RefreshValidatorInfos(height, validator.OperatorAddress) - if err != nil { - return fmt.Errorf("error while refreshing validator: %s", err) - } + return fmt.Errorf("error while refreshing all validators infos: %s", err) } return nil diff --git a/modules/gov/expected_modules.go b/modules/gov/expected_modules.go index 7a22e1f00..d06f7d3df 100644 --- a/modules/gov/expected_modules.go +++ b/modules/gov/expected_modules.go @@ -30,4 +30,5 @@ type StakingModule interface { GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultValidators) ([]types.ValidatorVotingPower, error) GetValidatorsStatuses(height int64, validators []stakingtypes.Validator) ([]types.ValidatorStatus, error) UpdateParams(height int64) error + RefreshAllValidatorInfos(height int64) error } diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index b560b266b..230640a66 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -8,6 +8,7 @@ import ( proposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" tmctypes "github.com/tendermint/tendermint/rpc/core/types" "google.golang.org/grpc/codes" @@ -31,11 +32,6 @@ func (m *Module) UpdateProposal(height int64, id uint64) error { return fmt.Errorf("error while getting proposal: %s", err) } - err = m.handleParamChangeProposal(height, proposal) - if err != nil { - return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) - } - err = m.updateProposalStatus(proposal) if err != nil { return fmt.Errorf("error while updating proposal status: %s", err) @@ -50,6 +46,36 @@ func (m *Module) UpdateProposal(height int64, id uint64) error { if err != nil { return fmt.Errorf("error while updating account: %s", err) } + + // Refresh relevant data while a proposal is passed + if proposal.Status != govtypes.StatusPassed { + return nil + } + + // Unpack proposal + var content govtypes.Content + err = m.db.EncodingConfig.Marshaler.UnpackAny(proposal.Content, &content) + if err != nil { + return fmt.Errorf("error while handling ParamChangeProposal: %s", err) + } + + switch p := content.(type) { + case *proposaltypes.ParameterChangeProposal: + // Update params while parameter change proposal passed + err = m.handleParamChangeProposal(height, p) + if err != nil { + return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) + } + case *upgradetypes.SoftwareUpgradeProposal: + // TODO: Store software upgrade time + p.Plan.Time + // Refresh validator details while software upgrade proposal passed + err = m.stakingModule.RefreshAllValidatorInfos(height) + if err != nil { + return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) + } + } + return nil } @@ -86,23 +112,7 @@ func (m *Module) updateDeletedProposalStatus(id uint64) error { } // handleParamChangeProposal updates params to the corresponding modules if a ParamChangeProposal has passed -func (m *Module) handleParamChangeProposal(height int64, proposal govtypes.Proposal) error { - if proposal.Status != govtypes.StatusPassed { - // If the status of ParamChangeProposal is not passed, do nothing - return nil - } - - var content govtypes.Content - err := m.db.EncodingConfig.Marshaler.UnpackAny(proposal.Content, &content) - if err != nil { - return fmt.Errorf("error while handling ParamChangeProposal: %s", err) - } - - paramChangeProposal, ok := content.(*proposaltypes.ParameterChangeProposal) - if !ok { - return nil - } - +func (m *Module) handleParamChangeProposal(height int64, paramChangeProposal *proposaltypes.ParameterChangeProposal) (err error) { for _, change := range paramChangeProposal.Changes { // Update the params for corresponding modules switch change.Subspace { diff --git a/modules/staking/utils_validators.go b/modules/staking/utils_validators.go index 4c5ee63d9..b899f86c7 100644 --- a/modules/staking/utils_validators.go +++ b/modules/staking/utils_validators.go @@ -80,6 +80,25 @@ func (m *Module) convertValidatorDescription( // -------------------------------------------------------------------------------------------------------------------- +// RefreshAllValidatorInfos refreshes the info of all the validators at the provided height +func (m *Module) RefreshAllValidatorInfos(height int64) error { + // Get all validators + validators, err := m.source.GetValidatorsWithStatus(height, "") + if err != nil { + return fmt.Errorf("error while getting validators: %s", err) + } + + // Refresh each validator + for _, validator := range validators { + err = m.RefreshValidatorInfos(height, validator.OperatorAddress) + if err != nil { + return fmt.Errorf("error while refreshing validator: %s", err) + } + } + + return nil +} + // RefreshValidatorInfos refreshes the info for the validator with the given operator address at the provided height func (m *Module) RefreshValidatorInfos(height int64, valOper string) error { stakingValidator, err := m.source.GetValidator(height, valOper) From eea79bd13a7430ea887a2ed93724c3a5fa550e65 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 21:14:47 +0800 Subject: [PATCH 02/15] store software upgrade plan --- database/gov.go | 35 ++++++++++ database/schema/12-upgrade.sql | 9 +++ .../tables/public_software_upgrade_plan.yaml | 18 +++++ .../databases/bdjuno/tables/tables.yaml | 1 + modules/gov/handle_msg.go | 7 -- modules/gov/utils_proposal.go | 66 +++++++++++-------- modules/upgrade/expected_modules.go | 5 ++ modules/upgrade/handle_block.go | 26 ++++++++ modules/upgrade/module.go | 31 +++++++++ 9 files changed, 165 insertions(+), 33 deletions(-) create mode 100644 database/schema/12-upgrade.sql create mode 100644 hasura/metadata/databases/bdjuno/tables/public_software_upgrade_plan.yaml create mode 100644 modules/upgrade/expected_modules.go create mode 100644 modules/upgrade/handle_block.go create mode 100644 modules/upgrade/module.go diff --git a/database/gov.go b/database/gov.go index 26de439f2..bf0350a41 100644 --- a/database/gov.go +++ b/database/gov.go @@ -6,6 +6,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/gogo/protobuf/proto" "github.com/forbole/bdjuno/v3/types" @@ -396,3 +397,37 @@ WHERE proposal_validator_status_snapshot.height <= excluded.height` return nil } + +// SaveSoftwareUpgradePlan allows to save the given SoftwareUpgradePlan +func (db *Db) SaveSoftwareUpgradePlan(proposalID uint64, plan upgradetypes.Plan, height int64) error { + + stmt := ` +INSERT INTO software_upgrade_plan(proposal_id, plan_name, upgrade_height, info, height) +VALUES ($1, $2, $3, $4, $5, $6, $7) +ON CONFLICT (proposal_id) DO UPDATE SET + plan_name = excluded.plan_name, + upgrade_height = excluded.upgrade_height, + info = excluded.info, + height = excluded.height +WHERE software_upgrade_prooposal.height <= excluded.height` + + _, err := db.Sql.Exec(stmt, + proposalID, plan.Name, plan.Height, plan.Info, height) + if err != nil { + return fmt.Errorf("error while storing software upgrade proposal: %s", err) + } + + return nil +} + +// DeleteSoftwareUpgradePlan allows to delete a SoftwareUpgradePlan with proposal ID +func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { + stmt := `DELETE FROM software_upgrade_plan WHERE proposal_id = $1` + + _, err := db.Sql.Exec(stmt, proposalID) + if err != nil { + return fmt.Errorf("error while deleteing software upgrade proposal: %s", err) + } + + return nil +} diff --git a/database/schema/12-upgrade.sql b/database/schema/12-upgrade.sql new file mode 100644 index 000000000..5d352005c --- /dev/null +++ b/database/schema/12-upgrade.sql @@ -0,0 +1,9 @@ +CREATE TABLE software_upgrade_plan +( + proposal_id INTEGER REFERENCES proposal (id) UNIQUE, + plan_name TEXT NOT NULL, + upgrade_height BIGINT NOT NULL, + info TEXT NOT NULL, + height BIGINT NOT NULL +); +CREATE INDEX software_upgrade_plan_proposal_id_index ON software_upgrade_plan (proposal_id); diff --git a/hasura/metadata/databases/bdjuno/tables/public_software_upgrade_plan.yaml b/hasura/metadata/databases/bdjuno/tables/public_software_upgrade_plan.yaml new file mode 100644 index 000000000..d7ab38f50 --- /dev/null +++ b/hasura/metadata/databases/bdjuno/tables/public_software_upgrade_plan.yaml @@ -0,0 +1,18 @@ +table: + name: software_upgrade_plan + schema: public +object_relationships: +- name: proposal + using: + foreign_key_constraint_on: proposal_id +select_permissions: +- permission: + allow_aggregations: true + columns: + - proposal_id + - plan_name + - upgrade_height + - info + - height + filter: {} + role: anonymous diff --git a/hasura/metadata/databases/bdjuno/tables/tables.yaml b/hasura/metadata/databases/bdjuno/tables/tables.yaml index 919fa44cd..920a84a31 100644 --- a/hasura/metadata/databases/bdjuno/tables/tables.yaml +++ b/hasura/metadata/databases/bdjuno/tables/tables.yaml @@ -23,6 +23,7 @@ - "!include public_proposal_validator_status_snapshot.yaml" - "!include public_proposal_vote.yaml" - "!include public_slashing_params.yaml" +- "!include public_software_upgrade_plan.yaml" - "!include public_staking_params.yaml" - "!include public_staking_pool.yaml" - "!include public_supply.yaml" diff --git a/modules/gov/handle_msg.go b/modules/gov/handle_msg.go index 3f2d920bd..1c50e17c7 100644 --- a/modules/gov/handle_msg.go +++ b/modules/gov/handle_msg.go @@ -56,13 +56,6 @@ func (m *Module) handleMsgSubmitProposal(tx *juno.Tx, index int, msg *govtypes.M return fmt.Errorf("error while getting proposal: %s", err) } - // Unpack the content - var content govtypes.Content - err = m.cdc.UnpackAny(proposal.Content, &content) - if err != nil { - return fmt.Errorf("error while unpacking proposal content: %s", err) - } - // Store the proposal proposalObj := types.NewProposal( proposal.ProposalId, diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index 230640a66..38f6deff4 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -47,33 +47,9 @@ func (m *Module) UpdateProposal(height int64, id uint64) error { return fmt.Errorf("error while updating account: %s", err) } - // Refresh relevant data while a proposal is passed - if proposal.Status != govtypes.StatusPassed { - return nil - } - - // Unpack proposal - var content govtypes.Content - err = m.db.EncodingConfig.Marshaler.UnpackAny(proposal.Content, &content) + err = m.handlePassedProposal(proposal, height) if err != nil { - return fmt.Errorf("error while handling ParamChangeProposal: %s", err) - } - - switch p := content.(type) { - case *proposaltypes.ParameterChangeProposal: - // Update params while parameter change proposal passed - err = m.handleParamChangeProposal(height, p) - if err != nil { - return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) - } - case *upgradetypes.SoftwareUpgradeProposal: - // TODO: Store software upgrade time - p.Plan.Time - // Refresh validator details while software upgrade proposal passed - err = m.stakingModule.RefreshAllValidatorInfos(height) - if err != nil { - return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) - } + return fmt.Errorf("error while handling passed proposals: %s", err) } return nil @@ -289,3 +265,41 @@ func findStatus(consAddr string, statuses []types.ValidatorStatus) (types.Valida } return types.ValidatorStatus{}, fmt.Errorf("cannot find status for validator with consensus address %s", consAddr) } + +func (m *Module) handlePassedProposal(proposal govtypes.Proposal, height int64) error { + if proposal.Status != govtypes.StatusPassed { + // If proposal status is not passed, do nothing + return nil + } + + // Unpack proposal + var content govtypes.Content + err := m.db.EncodingConfig.Marshaler.UnpackAny(proposal.Content, &content) + if err != nil { + return fmt.Errorf("error while handling ParamChangeProposal: %s", err) + } + + switch p := content.(type) { + case *proposaltypes.ParameterChangeProposal: + // Update params while ParameterChangeProposal passed + err = m.handleParamChangeProposal(height, p) + if err != nil { + return fmt.Errorf("error while updating params from ParamChangeProposal: %s", err) + } + + case *upgradetypes.SoftwareUpgradeProposal: + // Store software upgrade plan while SoftwareUpgradeProposal passed + err = m.db.SaveSoftwareUpgradePlan(proposal.ProposalId, p.Plan, height) + if err != nil { + return fmt.Errorf("error while storing passed SoftwareUpgradeProposal: %s", err) + } + + case *upgradetypes.CancelSoftwareUpgradeProposal: + // Delete software upgrade plan while CancelSoftwareUpgradeProposal passed + err = m.db.DeleteSoftwareUpgradePlan(proposal.ProposalId) + if err != nil { + return fmt.Errorf("error while deleting SoftwareUpgradeProposal: %s", err) + } + } + return nil +} diff --git a/modules/upgrade/expected_modules.go b/modules/upgrade/expected_modules.go new file mode 100644 index 000000000..1ee349204 --- /dev/null +++ b/modules/upgrade/expected_modules.go @@ -0,0 +1,5 @@ +package upgrade + +type StakingModule interface { + RefreshAllValidatorInfos(height int64) error +} diff --git a/modules/upgrade/handle_block.go b/modules/upgrade/handle_block.go new file mode 100644 index 000000000..69578d748 --- /dev/null +++ b/modules/upgrade/handle_block.go @@ -0,0 +1,26 @@ +package upgrade + +import ( + "fmt" + + "github.com/forbole/juno/v3/types" + + tmctypes "github.com/tendermint/tendermint/rpc/core/types" +) + +// HandleBlock implements modules.Module +func (m *Module) HandleBlock( + b *tmctypes.ResultBlock, _ *tmctypes.ResultBlockResults, _ []*types.Tx, _ *tmctypes.ResultValidators, +) error { + err := m.refreshDataUponSoftwareUpgrade(b.Block.Height) + if err != nil { + return fmt.Errorf("error while refreshing data upon software upgrade: %s", err) + } + + return nil +} + +func (m *Module) refreshDataUponSoftwareUpgrade(height int64) error { + // refresh validator details + return nil +} diff --git a/modules/upgrade/module.go b/modules/upgrade/module.go new file mode 100644 index 000000000..676f481d3 --- /dev/null +++ b/modules/upgrade/module.go @@ -0,0 +1,31 @@ +package upgrade + +import ( + "github.com/forbole/bdjuno/v3/database" + + "github.com/forbole/juno/v3/modules" +) + +var ( + _ modules.Module = &Module{} + _ modules.BlockModule = &Module{} +) + +// Module implements the consensus utils +type Module struct { + db *database.Db + stakingModule StakingModule +} + +// NewModule builds a new Module instance +func NewModule(db *database.Db, stakingModule StakingModule) *Module { + return &Module{ + stakingModule: stakingModule, + db: db, + } +} + +// Name implements modules.Module +func (m *Module) Name() string { + return "upgrade" +} From 62cabe354af376721a664642a00d4f401a7d8b3d Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 22:35:21 +0800 Subject: [PATCH 03/15] fix sql stmt --- database/gov.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gov.go b/database/gov.go index bf0350a41..5c21d63b3 100644 --- a/database/gov.go +++ b/database/gov.go @@ -403,13 +403,13 @@ func (db *Db) SaveSoftwareUpgradePlan(proposalID uint64, plan upgradetypes.Plan, stmt := ` INSERT INTO software_upgrade_plan(proposal_id, plan_name, upgrade_height, info, height) -VALUES ($1, $2, $3, $4, $5, $6, $7) +VALUES ($1, $2, $3, $4, $5) ON CONFLICT (proposal_id) DO UPDATE SET plan_name = excluded.plan_name, upgrade_height = excluded.upgrade_height, info = excluded.info, height = excluded.height -WHERE software_upgrade_prooposal.height <= excluded.height` +WHERE software_upgrade_plan.height <= excluded.height` _, err := db.Sql.Exec(stmt, proposalID, plan.Name, plan.Height, plan.Info, height) From 5666cee0e8d3f2eecff4fec214183a4a83d44b00 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 22:44:07 +0800 Subject: [PATCH 04/15] lint --- database/gov.go | 4 +++- modules/gov/expected_modules.go | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/database/gov.go b/database/gov.go index 5c21d63b3..069a887da 100644 --- a/database/gov.go +++ b/database/gov.go @@ -426,8 +426,10 @@ func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { _, err := db.Sql.Exec(stmt, proposalID) if err != nil { - return fmt.Errorf("error while deleteing software upgrade proposal: %s", err) + return fmt.Errorf("error while deleting software upgrade proposal: %s", err) } return nil } + +// TODO: check if an upgrade plan happens at certain height diff --git a/modules/gov/expected_modules.go b/modules/gov/expected_modules.go index d06f7d3df..7a22e1f00 100644 --- a/modules/gov/expected_modules.go +++ b/modules/gov/expected_modules.go @@ -30,5 +30,4 @@ type StakingModule interface { GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultValidators) ([]types.ValidatorVotingPower, error) GetValidatorsStatuses(height int64, validators []stakingtypes.Validator) ([]types.ValidatorStatus, error) UpdateParams(height int64) error - RefreshAllValidatorInfos(height int64) error } From f0ca52adff98932f24c6f6d34c69c970b8de077d Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 22:56:53 +0800 Subject: [PATCH 05/15] add get software upgrade db method --- database/gov.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/database/gov.go b/database/gov.go index 069a887da..ab2093c55 100644 --- a/database/gov.go +++ b/database/gov.go @@ -414,7 +414,7 @@ WHERE software_upgrade_plan.height <= excluded.height` _, err := db.Sql.Exec(stmt, proposalID, plan.Name, plan.Height, plan.Info, height) if err != nil { - return fmt.Errorf("error while storing software upgrade proposal: %s", err) + return fmt.Errorf("error while storing software upgrade plan: %s", err) } return nil @@ -426,10 +426,21 @@ func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { _, err := db.Sql.Exec(stmt, proposalID) if err != nil { - return fmt.Errorf("error while deleting software upgrade proposal: %s", err) + return fmt.Errorf("error while deleting software upgrade plan: %s", err) } return nil } -// TODO: check if an upgrade plan happens at certain height +// GetSoftwareUpgradePlan returns true if an upgrade plan exist at given height +func (db *Db) GetSoftwareUpgradePlan(height int64) (bool, error) { + var exist bool + + stmt := `SELECT EXISTS (SELECT 1 FROM software_upgrade_plan WHERE height=$1)` + err := db.Sql.QueryRow(stmt, height).Scan(&exist) + if err != nil { + return exist, fmt.Errorf("error while checking software upgrade plan existence: %s", err) + } + + return exist, nil +} From 6d5c4f67c345d5a7ed706ebae21606841c13a9d7 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 23:00:32 +0800 Subject: [PATCH 06/15] rename GetSoftwareUpgradePlan to CheckSoftwareUpgradePlan --- database/gov.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/gov.go b/database/gov.go index ab2093c55..c8845beda 100644 --- a/database/gov.go +++ b/database/gov.go @@ -432,8 +432,8 @@ func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { return nil } -// GetSoftwareUpgradePlan returns true if an upgrade plan exist at given height -func (db *Db) GetSoftwareUpgradePlan(height int64) (bool, error) { +// CheckSoftwareUpgradePlan returns true if an upgrade plan exist at given height +func (db *Db) CheckSoftwareUpgradePlan(height int64) (bool, error) { var exist bool stmt := `SELECT EXISTS (SELECT 1 FROM software_upgrade_plan WHERE height=$1)` From 3bb2a6449dcf9017e8ce49e1103c37a2a1fa78fc Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 15 Sep 2022 23:08:52 +0800 Subject: [PATCH 07/15] register upgrade module & complete refreshDataUponSoftwareUpgrade() --- modules/registrar.go | 3 +++ modules/upgrade/handle_block.go | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/registrar.go b/modules/registrar.go index c1d753f78..1b4b1f33e 100644 --- a/modules/registrar.go +++ b/modules/registrar.go @@ -30,6 +30,7 @@ import ( "github.com/forbole/bdjuno/v3/modules/modules" "github.com/forbole/bdjuno/v3/modules/pricefeed" "github.com/forbole/bdjuno/v3/modules/staking" + "github.com/forbole/bdjuno/v3/modules/upgrade" ) // UniqueAddressesParser returns a wrapper around the given parser that removes all duplicated addresses @@ -83,6 +84,7 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules { slashingModule := slashing.NewModule(sources.SlashingSource, cdc, db) stakingModule := staking.NewModule(sources.StakingSource, cdc, db) govModule := gov.NewModule(sources.GovSource, authModule, distrModule, mintModule, slashingModule, stakingModule, cdc, db) + upgradeModule := upgrade.NewModule(db, stakingModule) return []jmodules.Module{ messages.NewModule(r.parser, cdc, ctx.Database), @@ -102,5 +104,6 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules { pricefeed.NewModule(ctx.JunoConfig, cdc, db), slashingModule, stakingModule, + upgradeModule, } } diff --git a/modules/upgrade/handle_block.go b/modules/upgrade/handle_block.go index 69578d748..2de67f3a0 100644 --- a/modules/upgrade/handle_block.go +++ b/modules/upgrade/handle_block.go @@ -21,6 +21,19 @@ func (m *Module) HandleBlock( } func (m *Module) refreshDataUponSoftwareUpgrade(height int64) error { - // refresh validator details + exist, err := m.db.CheckSoftwareUpgradePlan(height) + if err != nil { + return fmt.Errorf("error while checking software upgrade plan existence: %s", err) + } + if !exist { + return nil + } + + // Refresh validator details + err = m.stakingModule.RefreshAllValidatorInfos(height) + if err != nil { + return fmt.Errorf("error while refreshing validator infos upon software upgrade: %s", err) + } + return nil } From bc1ac84975ec4617e54dd0a114e17ba6521ebdac Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 16 Sep 2022 15:56:46 +0800 Subject: [PATCH 08/15] minor fixes --- database/gov.go | 4 ++-- database/schema/12-upgrade.sql | 1 + modules/gov/utils_proposal.go | 4 ++-- modules/staking/utils_validators.go | 2 +- modules/upgrade/handle_block.go | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/database/gov.go b/database/gov.go index c8845beda..181978482 100644 --- a/database/gov.go +++ b/database/gov.go @@ -398,7 +398,7 @@ WHERE proposal_validator_status_snapshot.height <= excluded.height` return nil } -// SaveSoftwareUpgradePlan allows to save the given SoftwareUpgradePlan +// SaveSoftwareUpgradePlan allows to save the given software upgrade plan with its proposal id func (db *Db) SaveSoftwareUpgradePlan(proposalID uint64, plan upgradetypes.Plan, height int64) error { stmt := ` @@ -432,7 +432,7 @@ func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { return nil } -// CheckSoftwareUpgradePlan returns true if an upgrade plan exist at given height +// CheckSoftwareUpgradePlan returns true if an upgrade is scheduled at the given height func (db *Db) CheckSoftwareUpgradePlan(height int64) (bool, error) { var exist bool diff --git a/database/schema/12-upgrade.sql b/database/schema/12-upgrade.sql index 5d352005c..982be6a2f 100644 --- a/database/schema/12-upgrade.sql +++ b/database/schema/12-upgrade.sql @@ -7,3 +7,4 @@ CREATE TABLE software_upgrade_plan height BIGINT NOT NULL ); CREATE INDEX software_upgrade_plan_proposal_id_index ON software_upgrade_plan (proposal_id); +CREATE INDEX software_upgrade_plan_height_index ON software_upgrade_plan (height); diff --git a/modules/gov/utils_proposal.go b/modules/gov/utils_proposal.go index 38f6deff4..be57cb3d0 100644 --- a/modules/gov/utils_proposal.go +++ b/modules/gov/utils_proposal.go @@ -291,14 +291,14 @@ func (m *Module) handlePassedProposal(proposal govtypes.Proposal, height int64) // Store software upgrade plan while SoftwareUpgradeProposal passed err = m.db.SaveSoftwareUpgradePlan(proposal.ProposalId, p.Plan, height) if err != nil { - return fmt.Errorf("error while storing passed SoftwareUpgradeProposal: %s", err) + return fmt.Errorf("error while storing software upgrade plan: %s", err) } case *upgradetypes.CancelSoftwareUpgradeProposal: // Delete software upgrade plan while CancelSoftwareUpgradeProposal passed err = m.db.DeleteSoftwareUpgradePlan(proposal.ProposalId) if err != nil { - return fmt.Errorf("error while deleting SoftwareUpgradeProposal: %s", err) + return fmt.Errorf("error while deleting software upgrade plan: %s", err) } } return nil diff --git a/modules/staking/utils_validators.go b/modules/staking/utils_validators.go index b899f86c7..19f9176f9 100644 --- a/modules/staking/utils_validators.go +++ b/modules/staking/utils_validators.go @@ -80,7 +80,7 @@ func (m *Module) convertValidatorDescription( // -------------------------------------------------------------------------------------------------------------------- -// RefreshAllValidatorInfos refreshes the info of all the validators at the provided height +// RefreshAllValidatorInfos refreshes the info of all the validators at the given height func (m *Module) RefreshAllValidatorInfos(height int64) error { // Get all validators validators, err := m.source.GetValidatorsWithStatus(height, "") diff --git a/modules/upgrade/handle_block.go b/modules/upgrade/handle_block.go index 2de67f3a0..ed5321683 100644 --- a/modules/upgrade/handle_block.go +++ b/modules/upgrade/handle_block.go @@ -29,7 +29,7 @@ func (m *Module) refreshDataUponSoftwareUpgrade(height int64) error { return nil } - // Refresh validator details + // Refresh validator infos err = m.stakingModule.RefreshAllValidatorInfos(height) if err != nil { return fmt.Errorf("error while refreshing validator infos upon software upgrade: %s", err) From 57f7cc3128374e1b3683f2e4910e64e10095570b Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 16 Sep 2022 17:48:22 +0800 Subject: [PATCH 09/15] TestBigDipperDb_SaveSoftwareUpgradePlan --- database/gov_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++ database/types/gov.go | 22 +++++++++++ 2 files changed, 109 insertions(+) diff --git a/database/gov_test.go b/database/gov_test.go index 51a56c09c..d284debc9 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -5,6 +5,7 @@ import ( "time" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/gogo/protobuf/proto" @@ -809,3 +810,89 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot ), }) } + +func (suite *DbTestSuite) TestBigDipperDb_SaveSoftwareUpgradePlan() { + _ = suite.getBlock(9) + _ = suite.getBlock(10) + _ = suite.getBlock(11) + _ = suite.getProposalRow(1) + _ = suite.getProposalRow(2) + + // ---------------------------------------------------------------------------------------------------------------- + // Save 2 software upgrade plans at height 10 + + // Upgrade height = 100 + var plan = upgradetypes.Plan{ + Name: "name", + Height: 100, + Info: "info", + } + + err := suite.database.SaveSoftwareUpgradePlan(1, plan, 10) + suite.Require().NoError(err) + + var rows []dbtypes.SoftwareUpgradePlanRow + err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`) + suite.Require().NoError(err) + suite.Require().Len(rows, 1) + suite.Require().Equal(rows, []dbtypes.SoftwareUpgradePlanRow{ + dbtypes.NewSoftwareUpgradePlanRow(1, plan.Name, plan.Height, plan.Info, 10), + }) + + // ---------------------------------------------------------------------------------------------------------------- + // Update software upgrade plan with lower height + planEdit1 := upgradetypes.Plan{ + Name: "name_edit_1", + Height: 101, + Info: "info_edit_1", + } + + err = suite.database.SaveSoftwareUpgradePlan(1, planEdit1, 9) + suite.Require().NoError(err) + + rows = []dbtypes.SoftwareUpgradePlanRow{} + err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`) + suite.Require().NoError(err) + suite.Require().Len(rows, 1) + suite.Require().Equal(rows, []dbtypes.SoftwareUpgradePlanRow{ + dbtypes.NewSoftwareUpgradePlanRow(1, plan.Name, plan.Height, plan.Info, 10), + }) + + // ---------------------------------------------------------------------------------------------------------------- + // Update software upgrade plan with same height + planEdit2 := upgradetypes.Plan{ + Name: "name_edit_2", + Height: 102, + Info: "info_edit_2", + } + + err = suite.database.SaveSoftwareUpgradePlan(1, planEdit2, 10) + suite.Require().NoError(err) + + rows = []dbtypes.SoftwareUpgradePlanRow{} + err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`) + suite.Require().NoError(err) + suite.Require().Len(rows, 1) + suite.Require().Equal(rows, []dbtypes.SoftwareUpgradePlanRow{ + dbtypes.NewSoftwareUpgradePlanRow(1, planEdit2.Name, planEdit2.Height, planEdit2.Info, 10), + }) + + // ---------------------------------------------------------------------------------------------------------------- + // Update software upgrade plan with higher height + planEdit3 := upgradetypes.Plan{ + Name: "name_edit_3", + Height: 103, + Info: "info_edit_3", + } + + err = suite.database.SaveSoftwareUpgradePlan(1, planEdit3, 12) + suite.Require().NoError(err) + + rows = []dbtypes.SoftwareUpgradePlanRow{} + err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`) + suite.Require().NoError(err) + suite.Require().Len(rows, 1) + suite.Require().Equal(rows, []dbtypes.SoftwareUpgradePlanRow{ + dbtypes.NewSoftwareUpgradePlanRow(1, planEdit3.Name, planEdit3.Height, planEdit3.Info, 12), + }) +} diff --git a/database/types/gov.go b/database/types/gov.go index 9d9a2753c..30fc0fd1b 100644 --- a/database/types/gov.go +++ b/database/types/gov.go @@ -221,3 +221,25 @@ func NewProposalValidatorVotingPowerSnapshotRow( Height: height, } } + +// -------------------------------------------------------------------------------------------------------------------- + +type SoftwareUpgradePlanRow struct { + ProposalID uint64 `db:"proposal_id"` + PlanName string `db:"plan_name"` + UpgradeHeight int64 `db:"upgrade_height"` + Info string `db:"info"` + Height int64 `db:"height"` +} + +func NewSoftwareUpgradePlanRow( + proposalID uint64, planName string, upgradeHeight int64, info string, height int64, +) SoftwareUpgradePlanRow { + return SoftwareUpgradePlanRow{ + ProposalID: proposalID, + PlanName: planName, + UpgradeHeight: upgradeHeight, + Info: info, + Height: height, + } +} From edc8988a64deb54daa670eb51376e0cb3287b28e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 16 Sep 2022 19:08:21 +0800 Subject: [PATCH 10/15] TestBigDipperDb_DeleteSoftwareUpgradePlan --- database/gov_test.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/database/gov_test.go b/database/gov_test.go index d284debc9..c21667871 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -812,16 +812,10 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalValidatorsStatusesSnapshot } func (suite *DbTestSuite) TestBigDipperDb_SaveSoftwareUpgradePlan() { - _ = suite.getBlock(9) - _ = suite.getBlock(10) - _ = suite.getBlock(11) _ = suite.getProposalRow(1) - _ = suite.getProposalRow(2) // ---------------------------------------------------------------------------------------------------------------- - // Save 2 software upgrade plans at height 10 - - // Upgrade height = 100 + // Save software upgrade plan at height 10 with upgrade height at 100 var plan = upgradetypes.Plan{ Name: "name", Height: 100, @@ -885,7 +879,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSoftwareUpgradePlan() { Info: "info_edit_3", } - err = suite.database.SaveSoftwareUpgradePlan(1, planEdit3, 12) + err = suite.database.SaveSoftwareUpgradePlan(1, planEdit3, 11) suite.Require().NoError(err) rows = []dbtypes.SoftwareUpgradePlanRow{} @@ -893,6 +887,30 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSoftwareUpgradePlan() { suite.Require().NoError(err) suite.Require().Len(rows, 1) suite.Require().Equal(rows, []dbtypes.SoftwareUpgradePlanRow{ - dbtypes.NewSoftwareUpgradePlanRow(1, planEdit3.Name, planEdit3.Height, planEdit3.Info, 12), + dbtypes.NewSoftwareUpgradePlanRow(1, planEdit3.Name, planEdit3.Height, planEdit3.Info, 11), }) } + +func (suite *DbTestSuite) TestBigDipperDb_DeleteSoftwareUpgradePlan() { + _ = suite.getProposalRow(1) + + // Save software upgrade plan at height 10 with upgrade height at 100 + var plan = upgradetypes.Plan{ + Name: "name", + Height: 100, + Info: "info", + } + + err := suite.database.SaveSoftwareUpgradePlan(1, plan, 10) + suite.Require().NoError(err) + + // Delete software upgrade plan + err = suite.database.DeleteSoftwareUpgradePlan(1) + suite.Require().NoError(err) + + var rows []dbtypes.SoftwareUpgradePlanRow + err = suite.database.Sqlx.Select(&rows, `SELECT * FROM software_upgrade_plan`) + suite.Require().NoError(err) + suite.Require().Len(rows, 0) + +} From 6e909a08d96659b13a9766bf0e49cb1b811875e7 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Mon, 19 Sep 2022 16:22:53 +0800 Subject: [PATCH 11/15] TestBigDipperDb_CheckSoftwareUpgradePlan --- database/gov_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/database/gov_test.go b/database/gov_test.go index c21667871..3fc1c6bf8 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -914,3 +914,27 @@ func (suite *DbTestSuite) TestBigDipperDb_DeleteSoftwareUpgradePlan() { suite.Require().Len(rows, 0) } + +func (suite *DbTestSuite) TestBigDipperDb_CheckSoftwareUpgradePlan() { + _ = suite.getProposalRow(1) + + // Save software upgrade plan at height 10 with upgrade height at 100 + var plan = upgradetypes.Plan{ + Name: "name", + Height: 100, + Info: "info", + } + + err := suite.database.SaveSoftwareUpgradePlan(1, plan, 10) + suite.Require().NoError(err) + + // Check software upgrade plan at existing height + exist, err := suite.database.CheckSoftwareUpgradePlan(10) + suite.Require().NoError(err) + suite.Require().Equal(true, exist) + + // Check software upgrade plan at non-existing height + exist, err = suite.database.CheckSoftwareUpgradePlan(11) + suite.Require().NoError(err) + suite.Require().Equal(false, exist) +} From 4be4591c685a8d119456ce130e7fcdeb9edfc289 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Mon, 19 Sep 2022 16:33:15 +0800 Subject: [PATCH 12/15] change log --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a569a40..d84a363a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased ### Changes +#### Upgrade Module +- ([\#467](https://github.com/forbole/bdjuno/pull/467)) Store software upgrade plan and refresh data at upgrade height + + #### Staking Module - ([\#443](https://github.com/forbole/bdjuno/pull/443)) Remove tombstone status from staking module(already stored in slashing module) - ([\#455](https://github.com/forbole/bdjuno/pull/455)) Added `unbonding_tokens` and `staked_not_bonded_tokens` values to staking pool table From 3fe111bf53b2a5d542102edfdf391df218bb08d0 Mon Sep 17 00:00:00 2001 From: Aaron <76254323+huichiaotsou@users.noreply.github.com> Date: Fri, 23 Sep 2022 18:45:41 +0800 Subject: [PATCH 13/15] Update modules/upgrade/module.go Co-authored-by: Magic Cat <37407870+MonikaCat@users.noreply.github.com> --- modules/upgrade/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/upgrade/module.go b/modules/upgrade/module.go index 676f481d3..6576bff4b 100644 --- a/modules/upgrade/module.go +++ b/modules/upgrade/module.go @@ -11,7 +11,7 @@ var ( _ modules.BlockModule = &Module{} ) -// Module implements the consensus utils +// Module represents the x/upgrade module type Module struct { db *database.Db stakingModule StakingModule From e41d617c2d61cc688eece9d0e3a1c4b2bc660ec1 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 23 Sep 2022 19:03:02 +0800 Subject: [PATCH 14/15] add TruncateSoftwareUpgradePlan --- database/gov.go | 18 +++++++++++++++--- database/types/gov.go | 22 ---------------------- database/types/upgrade.go | 21 +++++++++++++++++++++ modules/upgrade/handle_block.go | 6 ++++++ 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 database/types/upgrade.go diff --git a/database/gov.go b/database/gov.go index 181978482..3f8ed84ae 100644 --- a/database/gov.go +++ b/database/gov.go @@ -433,14 +433,26 @@ func (db *Db) DeleteSoftwareUpgradePlan(proposalID uint64) error { } // CheckSoftwareUpgradePlan returns true if an upgrade is scheduled at the given height -func (db *Db) CheckSoftwareUpgradePlan(height int64) (bool, error) { +func (db *Db) CheckSoftwareUpgradePlan(upgradeHeight int64) (bool, error) { var exist bool - stmt := `SELECT EXISTS (SELECT 1 FROM software_upgrade_plan WHERE height=$1)` - err := db.Sql.QueryRow(stmt, height).Scan(&exist) + stmt := `SELECT EXISTS (SELECT 1 FROM software_upgrade_plan WHERE upgrade_height=$1)` + err := db.Sql.QueryRow(stmt, upgradeHeight).Scan(&exist) if err != nil { return exist, fmt.Errorf("error while checking software upgrade plan existence: %s", err) } return exist, nil } + +// TruncateSoftwareUpgradePlan delete software upgrade plans once the upgrade height passed +func (db *Db) TruncateSoftwareUpgradePlan(height int64) error { + stmt := `DELETE FROM software_upgrade_plan WHERE upgrade_height <= $1` + + _, err := db.Sql.Exec(stmt, height) + if err != nil { + return fmt.Errorf("error while deleting software upgrade plan: %s", err) + } + + return nil +} diff --git a/database/types/gov.go b/database/types/gov.go index 30fc0fd1b..9d9a2753c 100644 --- a/database/types/gov.go +++ b/database/types/gov.go @@ -221,25 +221,3 @@ func NewProposalValidatorVotingPowerSnapshotRow( Height: height, } } - -// -------------------------------------------------------------------------------------------------------------------- - -type SoftwareUpgradePlanRow struct { - ProposalID uint64 `db:"proposal_id"` - PlanName string `db:"plan_name"` - UpgradeHeight int64 `db:"upgrade_height"` - Info string `db:"info"` - Height int64 `db:"height"` -} - -func NewSoftwareUpgradePlanRow( - proposalID uint64, planName string, upgradeHeight int64, info string, height int64, -) SoftwareUpgradePlanRow { - return SoftwareUpgradePlanRow{ - ProposalID: proposalID, - PlanName: planName, - UpgradeHeight: upgradeHeight, - Info: info, - Height: height, - } -} diff --git a/database/types/upgrade.go b/database/types/upgrade.go new file mode 100644 index 000000000..c3abc7407 --- /dev/null +++ b/database/types/upgrade.go @@ -0,0 +1,21 @@ +package types + +type SoftwareUpgradePlanRow struct { + ProposalID uint64 `db:"proposal_id"` + PlanName string `db:"plan_name"` + UpgradeHeight int64 `db:"upgrade_height"` + Info string `db:"info"` + Height int64 `db:"height"` +} + +func NewSoftwareUpgradePlanRow( + proposalID uint64, planName string, upgradeHeight int64, info string, height int64, +) SoftwareUpgradePlanRow { + return SoftwareUpgradePlanRow{ + ProposalID: proposalID, + PlanName: planName, + UpgradeHeight: upgradeHeight, + Info: info, + Height: height, + } +} diff --git a/modules/upgrade/handle_block.go b/modules/upgrade/handle_block.go index ed5321683..9a588b71f 100644 --- a/modules/upgrade/handle_block.go +++ b/modules/upgrade/handle_block.go @@ -35,5 +35,11 @@ func (m *Module) refreshDataUponSoftwareUpgrade(height int64) error { return fmt.Errorf("error while refreshing validator infos upon software upgrade: %s", err) } + // Delete plan after refreshing data + err = m.db.TruncateSoftwareUpgradePlan(height) + if err != nil { + return fmt.Errorf("error while truncating software upgrade plan: %s", err) + } + return nil } From 2913804e4eeaa0f04ecc40ec3a3298c497de2ae3 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Mon, 26 Sep 2022 12:25:14 +0800 Subject: [PATCH 15/15] fix test --- database/gov_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/database/gov_test.go b/database/gov_test.go index 3fc1c6bf8..3f5cf06c1 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -920,7 +920,8 @@ func (suite *DbTestSuite) TestBigDipperDb_CheckSoftwareUpgradePlan() { // Save software upgrade plan at height 10 with upgrade height at 100 var plan = upgradetypes.Plan{ - Name: "name", + Name: "name", + // the Height here is the upgrade height Height: 100, Info: "info", } @@ -929,7 +930,7 @@ func (suite *DbTestSuite) TestBigDipperDb_CheckSoftwareUpgradePlan() { suite.Require().NoError(err) // Check software upgrade plan at existing height - exist, err := suite.database.CheckSoftwareUpgradePlan(10) + exist, err := suite.database.CheckSoftwareUpgradePlan(100) suite.Require().NoError(err) suite.Require().Equal(true, exist)