Skip to content

Commit

Permalink
feat: parse gentxs if no staking validators
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Apr 19, 2024
1 parent 8b9b8a2 commit b7777e6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
55 changes: 54 additions & 1 deletion pkg/fetcher/cosmos_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
"strings"
"sync"

"github.com/cosmos/cosmos-sdk/x/auth/tx"
genutilTypes "github.com/cosmos/cosmos-sdk/x/genutil/types"

sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/std"
"github.com/rs/zerolog"

"github.com/cosmos/cosmos-sdk/codec"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptoTypes "github.com/cosmos/cosmos-sdk/crypto/types"
queryTypes "github.com/cosmos/cosmos-sdk/types/query"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradeTypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
Expand All @@ -34,12 +38,15 @@ type CosmosRPCDataFetcher struct {

Registry codecTypes.InterfaceRegistry
ParseCodec *codec.ProtoCodec
TxDecoder sdkTypes.TxDecoder
}

func NewCosmosRPCDataFetcher(config configPkg.Config, logger zerolog.Logger) *CosmosRPCDataFetcher {
interfaceRegistry := codecTypes.NewInterfaceRegistry()
std.RegisterInterfaces(interfaceRegistry)
stakingTypes.RegisterInterfaces(interfaceRegistry) // for MsgCreateValidator for gentx parsing
parseCodec := codec.NewProtoCodec(interfaceRegistry)
txDecoder := tx.NewTxConfig(parseCodec, tx.DefaultSignModes)

return &CosmosRPCDataFetcher{
Config: config,
Expand All @@ -48,6 +55,7 @@ func NewCosmosRPCDataFetcher(config configPkg.Config, logger zerolog.Logger) *Co
Client: http.NewClient(logger, "cosmos_data_fetcher", config.RPCHost),
Registry: interfaceRegistry,
ParseCodec: parseCodec,
TxDecoder: txDecoder.TxJSONDecoder(),
}
}

Expand Down Expand Up @@ -253,7 +261,52 @@ func (f *CosmosRPCDataFetcher) GetGenesisValidators() (*types.ChainValidators, e
return &validators, nil
}

return nil, fmt.Errorf("genesis validators fetching is not yet supported")
// 2. If there's 0 validators in staking module, then we parse genutil module
// and converting validators from their gentxs.
var genutilGenesisState genutilTypes.GenesisState
if err := f.ParseCodec.UnmarshalJSON(genesisStruct.AppState.Genutil, &genutilGenesisState); err != nil {
f.Logger.Error().Err(err).Msg("Error unmarshalling genutil genesis state")
return nil, err
}

validators := make(types.ChainValidators, len(genutilGenesisState.GenTxs))
for index, gentx := range genutilGenesisState.GenTxs {
decodedTx, err := f.TxDecoder(gentx)
if err != nil {
f.Logger.Error().Err(err).Msg("Error decoding gentx")
return nil, err
}

if len(decodedTx.GetMsgs()) != 1 {
f.Logger.Error().
Int("length", len(decodedTx.GetMsgs())).
Msg("Error decoding gentx: expected 1 message")
return nil, err
}

msg := decodedTx.GetMsgs()[0]
msgCreateValidator, ok := msg.(*stakingTypes.MsgCreateValidator)
if !ok {
f.Logger.Error().Msg("gentx msg is not MsgCreateValidator")
return nil, err
}

var pubkey cryptoTypes.PubKey
if err := f.ParseCodec.UnpackAny(msgCreateValidator.Pubkey, &pubkey); err != nil {
f.Logger.Error().Err(err).Msg("Error unpacking pubkey")
return nil, err
}

addr := sdkTypes.ConsAddress(pubkey.Address())

validators[index] = types.ChainValidator{
Moniker: msgCreateValidator.Description.Moniker,
Address: fmt.Sprintf("%X", addr),
RawAddress: addr.String(),
}
}

return &validators, nil
}

func (f *CosmosRPCDataFetcher) GetGenesisChunk(chunk int64) ([]byte, int64, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Genesis struct {

type AppState struct {
Staking json.RawMessage `json:"staking"`
Genutil json.RawMessage `json:"genutil"`
}

0 comments on commit b7777e6

Please sign in to comment.