From 083bfdd27984153b2f080a96f2d1839d18e284ae Mon Sep 17 00:00:00 2001 From: Dzung Do Date: Fri, 10 Jan 2025 13:08:30 +0700 Subject: [PATCH] fix saving validator --- database/multistaking.go | 5 ++-- .../handle_additional_operations.go | 12 ++++++++- modules/multistaking/utils.go | 25 +++++++++++++++++++ modules/staking/handle_msg.go | 14 +++++++---- modules/staking/utils_validators.go | 10 ++++++++ types/multistaking.go | 13 ++++++++++ 6 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 modules/multistaking/utils.go create mode 100644 types/multistaking.go diff --git a/database/multistaking.go b/database/multistaking.go index c9d479b1f..937be6eb9 100644 --- a/database/multistaking.go +++ b/database/multistaking.go @@ -5,6 +5,7 @@ import ( cosmossdk_io_math "cosmossdk.io/math" dbtypes "github.com/forbole/callisto/v4/database/types" + "github.com/forbole/callisto/v4/types" "github.com/lib/pq" multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types" @@ -239,7 +240,7 @@ WHERE token_bonded.height <= excluded.height` return nil } -func (db *Db) SaveValidatorDenom(height int64, validatorInfo []multistakingtypes.ValidatorInfo) error { +func (db *Db) SaveValidatorDenom(height int64, validatorInfo []types.MSValidatorInfo) error { if len(validatorInfo) == 0 { return nil } @@ -250,7 +251,7 @@ func (db *Db) SaveValidatorDenom(height int64, validatorInfo []multistakingtypes for i, info := range validatorInfo { vi := i * 3 query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3) - param = append(param, info.OperatorAddress, info.BondDenom, height) + param = append(param, info.ConsensusAddress, info.Denom, height) } query = query[:len(query)-1] // Remove trailing "," diff --git a/modules/multistaking/handle_additional_operations.go b/modules/multistaking/handle_additional_operations.go index f998cd392..41de3323a 100644 --- a/modules/multistaking/handle_additional_operations.go +++ b/modules/multistaking/handle_additional_operations.go @@ -3,6 +3,7 @@ package multistaking import ( "fmt" + "github.com/forbole/callisto/v4/types" "github.com/rs/zerolog/log" ) @@ -82,5 +83,14 @@ func (m *Module) UpdateValidatorInfo(height int64) error { return err } - return m.db.SaveValidatorDenom(height, validatorInfo) + msValInfos := []types.MSValidatorInfo{} + for _, val := range validatorInfo { + valInfo, err := m.convertValidatorInfo(&val) + if err != nil { + return err + } + msValInfos = append(msValInfos, valInfo) + } + + return m.db.SaveValidatorDenom(height, msValInfos) } diff --git a/modules/multistaking/utils.go b/modules/multistaking/utils.go new file mode 100644 index 000000000..1cf69f907 --- /dev/null +++ b/modules/multistaking/utils.go @@ -0,0 +1,25 @@ +package multistaking + +import ( + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/forbole/callisto/v4/types" + + multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types" +) + +func (m *Module) convertValidatorInfo(info *multistakingtypes.ValidatorInfo) (types.MSValidatorInfo, error) { + var pubKey cryptotypes.PubKey + err := m.cdc.UnpackAny(info.ConsensusPubkey, &pubKey) + if err != nil { + return types.MSValidatorInfo{}, err + } + return types.MSValidatorInfo{ + ConsensusAddress: convertPubkeyToAddr(pubKey), + Denom: info.BondDenom, + }, nil +} + +func convertPubkeyToAddr(pubkey cryptotypes.PubKey) string { + return sdk.ConsAddress(pubkey.Address()).String() +} \ No newline at end of file diff --git a/modules/staking/handle_msg.go b/modules/staking/handle_msg.go index 750695c35..8d82c7353 100644 --- a/modules/staking/handle_msg.go +++ b/modules/staking/handle_msg.go @@ -9,8 +9,8 @@ import ( juno "github.com/forbole/juno/v6/types" dbtypes "github.com/forbole/callisto/v4/database/types" + "github.com/forbole/callisto/v4/types" "github.com/forbole/callisto/v4/utils" - multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types" ) var msgFilter = map[string]bool{ @@ -76,10 +76,14 @@ func (m *Module) handleMsgCreateValidator(height int64, msg *stakingtypes.MsgCre return fmt.Errorf("error while refreshing validator from MsgCreateValidator: %s", err) } - var infos []multistakingtypes.ValidatorInfo - validatorInfo := multistakingtypes.ValidatorInfo{ - OperatorAddress: msg.ValidatorAddress, - BondDenom: msg.Value.Denom, + var infos []types.MSValidatorInfo + consPubkey, err := m.getValidatorConsPubKeyByCreateMsg(msg) + if err != nil { + return err + } + validatorInfo := types.MSValidatorInfo{ + ConsensusAddress: convertPubkeyToAddr(consPubkey), + Denom: msg.Value.Denom, } infos = append(infos, validatorInfo) diff --git a/modules/staking/utils_validators.go b/modules/staking/utils_validators.go index 1a5c0c438..6a2ff2ace 100644 --- a/modules/staking/utils_validators.go +++ b/modules/staking/utils_validators.go @@ -14,6 +14,16 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) +func (m *Module) getValidatorConsPubKeyByCreateMsg(msg *stakingtypes.MsgCreateValidator) (cryptotypes.PubKey, error) { + var pubKey cryptotypes.PubKey + err := m.cdc.UnpackAny(msg.Pubkey, &pubKey) + return pubKey, err +} + +func convertPubkeyToAddr(pubkey cryptotypes.PubKey) string { + return sdk.ConsAddress(pubkey.Address()).String() +} + // getValidatorConsPubKey returns the consensus public key of the given validator func (m *Module) getValidatorConsPubKey(validator stakingtypes.Validator) (cryptotypes.PubKey, error) { var pubKey cryptotypes.PubKey diff --git a/types/multistaking.go b/types/multistaking.go new file mode 100644 index 000000000..47fed1d48 --- /dev/null +++ b/types/multistaking.go @@ -0,0 +1,13 @@ +package types + +type MSValidatorInfo struct { + ConsensusAddress string + Denom string +} + +func NewMSValidatorInfo(consAddr, denom string) MSValidatorInfo { + return MSValidatorInfo{ + ConsensusAddress: consAddr, + Denom: denom, + } +}