Skip to content

Commit

Permalink
feat: take validators from staking module from genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Apr 18, 2024
1 parent 01fba7f commit 018b1da
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
76 changes: 61 additions & 15 deletions pkg/fetcher/cosmos_rpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fetcher

import (
"bytes"
"encoding/json"
"errors"
"fmt"
configPkg "main/pkg/config"
Expand Down Expand Up @@ -112,7 +114,26 @@ func (f *CosmosRPCDataFetcher) AbciQuery(
return output.Unmarshal(response.Result.Response.Value)
}

func (f *CosmosRPCDataFetcher) ParseValidator(validator stakingTypes.Validator) (types.ChainValidator, error) {
if err := validator.UnpackInterfaces(f.ParseCodec); err != nil {
return types.ChainValidator{}, err
}

addr, err := validator.GetConsAddr()
if err != nil {
return types.ChainValidator{}, err
}

return types.ChainValidator{
Moniker: validator.GetMoniker(),
Address: fmt.Sprintf("%X", addr),
RawAddress: addr.String(),
}, nil
}

func (f *CosmosRPCDataFetcher) GetValidators() (*types.ChainValidators, error) {
return f.GetGenesisValidators()

query := stakingTypes.QueryValidatorsRequest{

Check failure on line 137 in pkg/fetcher/cosmos_rpc.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)

Check failure on line 137 in pkg/fetcher/cosmos_rpc.go

View workflow job for this annotation

GitHub Actions / go-vet

unreachable code
Pagination: &queryTypes.PageRequest{
Limit: 1000,
Expand All @@ -135,19 +156,10 @@ func (f *CosmosRPCDataFetcher) GetValidators() (*types.ChainValidators, error) {
validators := make(types.ChainValidators, len(validatorsResponse.Validators))

for index, validator := range validatorsResponse.Validators {
if err := validator.UnpackInterfaces(f.ParseCodec); err != nil {
if chainValidator, err := f.ParseValidator(validator); err != nil {
return nil, err
}

addr, err := validator.GetConsAddr()
if err != nil {
return nil, err
}

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

Expand Down Expand Up @@ -190,25 +202,59 @@ func (f *CosmosRPCDataFetcher) GetValidators() (*types.ChainValidators, error) {
func (f *CosmosRPCDataFetcher) GetGenesisValidators() (*types.ChainValidators, error) {
f.Logger.Info().Msg("Fetching genesis validators...")

genesisAsBytes := make([]byte, 0)
genesisChunks := make([][]byte, 0)
var chunk int64 = 0

for {
f.Logger.Info().Int64("chunk", chunk).Msg("Fetching genesis chunk...")
genesisChunk, total, err := f.GetGenesisChunk(chunk)
f.Logger.Info().Int64("chunk", chunk).Int64("total", total).Msg("Fetched genesis chunk...")
if err != nil {
return nil, err
}

genesisAsBytes = append(genesisAsBytes, genesisChunk...)
genesisChunks = append(genesisChunks, genesisChunk)

if chunk <= total {
if chunk >= total-1 {
break
}

chunk++
}

genesisBytes := bytes.Join(genesisChunks, []byte{})
f.Logger.Info().Int("length", len(genesisBytes)).Msg("Fetched genesis")

var genesisStruct types.Genesis

if err := json.Unmarshal(genesisBytes, &genesisStruct); err != nil {
f.Logger.Error().Err(err).Msg("Error unmarshalling genesis")
return nil, err
}

var stakingGenesisState stakingTypes.GenesisState
if err := f.ParseCodec.UnmarshalJSON(genesisStruct.AppState.Staking, &stakingGenesisState); err != nil {
f.Logger.Error().Err(err).Msg("Error unmarshalling staking genesis state")
return nil, err
}

f.Logger.Info().Int("validators", len(stakingGenesisState.Validators)).Msg("Genesis unmarshalled")

// 1. Trying to fetch validators from staking module. Works for chain which did not start
// from the first block but had their genesis as an export from older chain.
if len(stakingGenesisState.Validators) > 0 {
validators := make(types.ChainValidators, len(stakingGenesisState.Validators))
for index, validator := range stakingGenesisState.Validators {
if chainValidator, err := f.ParseValidator(validator); err != nil {
return nil, err
} else {
validators[index] = chainValidator
}
}

return &validators, nil
}

return nil, fmt.Errorf("genesis validators fetching is not yet supported")

Check failure on line 258 in pkg/fetcher/cosmos_rpc.go

View workflow job for this annotation

GitHub Actions / lint

fmt.Errorf can be replaced with errors.New (perfsprint)
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

import "encoding/json"

type Genesis struct {
AppState AppState `json:"app_state"`
}

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

0 comments on commit 018b1da

Please sign in to comment.