Skip to content

Commit

Permalink
fix saving validator
Browse files Browse the repository at this point in the history
  • Loading branch information
neitdung committed Jan 10, 2025
1 parent b6882cf commit 083bfdd
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
5 changes: 3 additions & 2 deletions database/multistaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 ","
Expand Down
12 changes: 11 additions & 1 deletion modules/multistaking/handle_additional_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multistaking
import (
"fmt"

"github.com/forbole/callisto/v4/types"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -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)
}
25 changes: 25 additions & 0 deletions modules/multistaking/utils.go
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 9 additions & 5 deletions modules/staking/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions modules/staking/utils_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions types/multistaking.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 083bfdd

Please sign in to comment.