Skip to content

Commit

Permalink
feat: actually work ok with consumer chains
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 20, 2023
1 parent fb6edc7 commit 8acd107
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 203 deletions.
13 changes: 4 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module main

go 1.20
go 1.19

require (
github.com/BurntSushi/toml v1.2.1
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/bwmarrin/discordgo v0.27.1
github.com/cosmos/cosmos-sdk v0.45.16-ics
github.com/cosmos/interchain-security v1.2.1
github.com/creasty/defaults v1.7.0
github.com/google/uuid v1.3.0
github.com/lib/pq v1.10.7
Expand All @@ -33,9 +34,7 @@ require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
Expand All @@ -49,6 +48,7 @@ require (
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.5 // indirect
github.com/cosmos/ibc-go/v4 v4.2.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -58,7 +58,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.17.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
Expand All @@ -70,7 +69,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
Expand All @@ -79,11 +77,9 @@ require (
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.3 // indirect
Expand All @@ -98,8 +94,7 @@ require (
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
Expand Down
185 changes: 8 additions & 177 deletions go.sum

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions pkg/config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type ChainConfig struct {
QueryEachSigningInfo null.Bool `default:"false" toml:"query-each-signing-info"`
QuerySlashingParams null.Bool `default:"true" toml:"query-slashing-params"`

IsConsumer null.Bool `default:"false" toml:"consumer"`
ProviderRPCEndpoints []string `toml:"provider-rpc-endpoints"`
IsConsumer null.Bool `default:"false" toml:"consumer"`

Check failure on line 19 in pkg/config/chain.go

View workflow job for this annotation

GitHub Actions / lint

tag is not aligned, should be: default:"false" toml:"consumer" (tagalign)
ProviderRPCEndpoints []string `toml:"provider-rpc-endpoints"`
ConsumerValidatorPrefix string `toml:"consumer-validator-prefix"`

MissedBlocksGroups MissedBlocksGroups `toml:"missed-blocks-groups"`
ExplorerConfig ExplorerConfig `toml:"explorer"`
Expand Down Expand Up @@ -46,8 +47,10 @@ func (c *ChainConfig) Validate() error {
return fmt.Errorf("chain has 0 RPC endpoints")
}

if c.IsConsumer.Bool && len(c.ProviderRPCEndpoints) == 0 {
return fmt.Errorf("chain is a consumer, but has 0 provider RPC endpoints")
if c.IsConsumer.Bool {
if len(c.ProviderRPCEndpoints) == 0 {
return fmt.Errorf("chain is a consumer, but has 0 provider RPC endpoints")
}
}

return nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ func TestValidateChainValid(t *testing.T) {
err := config.Validate()
assert.Nil(t, err, "Error should not be present!")
}

func TestValidateConsumerChainValid(t *testing.T) {
t.Parallel()

config := &ChainConfig{
Name: "chain",
RPCEndpoints: []string{"endpoint"},
IsConsumer: null.BoolFrom(true),
ProviderRPCEndpoints: []string{"endpoint"},
}
err := config.Validate()
assert.Nil(t, err, "Error should not be present!")
}
8 changes: 5 additions & 3 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ const (
DiscordReporterName ReporterName = "discord"
TestReporterName ReporterName = "test"

QueryTypeValidators QueryType = "validators"
QueryTypeSigningInfos QueryType = "signing_infos"
QueryTypeSigningInfo QueryType = "signing_info"
QueryTypeValidators QueryType = "validators"
QueryTypeSigningInfos QueryType = "signing_infos"
QueryTypeSigningInfo QueryType = "signing_info"
QueryTypeConsumerAddr QueryType = "consumer_addr"

QueryTypeSlashingParams QueryType = "slashing_params"
QueryTypeHistoricalValidators QueryType = "historical_validators"
QueryTypeBlock QueryType = "block"
Expand Down
101 changes: 101 additions & 0 deletions pkg/data/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func NewManager(
}

func (m *Manager) GetValidators(height int64) (types.Validators, error) {
if m.config.IsConsumer.Bool {
return m.GetValidatorsAndSigningInfoForConsumerChain(height)
}

if m.config.QueryEachSigningInfo.Bool {
return m.GetValidatorsAndEachSigningInfo(height)
}
Expand Down Expand Up @@ -140,3 +144,100 @@ func (m *Manager) GetValidatorsAndEachSigningInfo(height int64) (types.Validator

return validators, nil
}

func (m *Manager) GetValidatorsAndSigningInfoForConsumerChain(height int64) (types.Validators, error) {
var (
wg sync.WaitGroup
validatorsResponse *stakingTypes.QueryValidatorsResponse
validatorsError error
signingInfoResponse *slashingTypes.QuerySigningInfosResponse
signingInfoErr error
mutex sync.Mutex
)

wg.Add(2)
go func() {
validatorsResponse, validatorsError = m.httpManager.GetValidators(0)
wg.Done()
}()

go func() {
signingInfoResponse, signingInfoErr = m.httpManager.GetSigningInfos(height)
wg.Done()
}()

wg.Wait()

if validatorsError != nil {
return nil, validatorsError
}

if signingInfoErr != nil {
return nil, signingInfoErr
}

validators := make([]*types.Validator, len(validatorsResponse.Validators))

for index, validatorRaw := range validatorsResponse.Validators {
if m.config.ConsumerValidatorPrefix != "" {
if newOperatorAddress, convertErr := utils.ConvertBech32Prefix(
validatorRaw.OperatorAddress,
m.config.ConsumerValidatorPrefix,
); convertErr != nil {
m.logger.Error().
Str("operator_address", validatorRaw.OperatorAddress).
Msg("Error converting operator address to a new prefix")
} else {
validatorRaw.OperatorAddress = newOperatorAddress
}
}

wg.Add(1)
go func(validatorRaw stakingTypes.Validator, index int) {
defer wg.Done()

consensusAddrProvider := m.converter.GetConsensusAddress(validatorRaw)
consensusAddr := consensusAddrProvider

consensusAddrConsumer, err := m.httpManager.GetValidatorAssignedConsumerKey(consensusAddrProvider, 0)
if err != nil {
m.logger.Warn().
Str("operator_address", validatorRaw.OperatorAddress).
Err(err).
Msg("Error fetching validator assigned consumer key")
} else if consensusAddrConsumer.ConsumerAddress != "" {
consensusAddr = consensusAddrConsumer.ConsumerAddress
}

signingInfo, ok := utils.Find(signingInfoResponse.Info, func(i slashingTypes.ValidatorSigningInfo) bool {
equal, compareErr := utils.CompareTwoBech32(i.Address, consensusAddr)
if compareErr != nil {
m.logger.Error().
Str("operator_address", validatorRaw.OperatorAddress).
Str("first", i.Address).
Str("second", consensusAddr).
Msg("Error converting bech32 address")
return false
}

return equal
})

if !ok {
m.logger.Warn().
Str("operator_address", validatorRaw.OperatorAddress).
Msg("Could not find signing info for validator")
}

validator := m.converter.ValidatorFromCosmosValidator(validatorRaw, &signingInfo)

mutex.Lock()
validators[index] = validator
mutex.Unlock()
}(validatorRaw, index)
}

wg.Wait()

return validators, nil
}
Loading

0 comments on commit 8acd107

Please sign in to comment.