Skip to content

Commit

Permalink
test(operator): add genesis validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Apr 2, 2024
1 parent a26eac8 commit ae25a47
Show file tree
Hide file tree
Showing 2 changed files with 568 additions and 6 deletions.
28 changes: 22 additions & 6 deletions x/operator/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (gs GenesisState) Validate() error {
}
// consensus_keys.go
operatorsByKeys := make(map[string]struct{}, len(gs.OperatorRecords))
// keysByChainId stores chain id -> cons keys list. ensure that within a chain id, cons key
// isn't repeated.
keysByChainId := make(map[string](map[string]struct{}))

Check warning on line 43 in x/operator/types/genesis.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

var-naming: var keysByChainId should be keysByChainID (revive)
for _, record := range gs.OperatorRecords {
operatorAddress := record.OperatorAddress
_, err := sdk.AccAddressFromBech32(operatorAddress)
Expand Down Expand Up @@ -72,8 +75,23 @@ func (gs GenesisState) Validate() error {
consKeyString, err,
)
}
// validate chain id is not done, since it is not strictly enforced within Cosmos.
if _, ok := keysByChainId[subRecord.ChainID]; !ok {
keysByChainId[subRecord.ChainID] = make(map[string]struct{})
}
if _, ok := keysByChainId[subRecord.ChainID][consKeyString]; ok {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"duplicate consensus key %s for chain %s",
consKeyString, subRecord.ChainID,
)
}
keysByChainId[subRecord.ChainID][consKeyString] = struct{}{}
}
}
// it may be possible for an operator to opt into an AVS which does not have a consensus
// key requirement, so this check could be removed if we set up the Export case. but i
// think it is better to keep it for now.
if len(operators) != len(operatorsByKeys) {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
Expand Down Expand Up @@ -150,12 +168,10 @@ func (gs GenesisState) Validate() error {
}
}
}
if len(operators) != len(operatorsByStakers) {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"operator addresses in operators and staker records do not match",
)
}
// it is possible that a few operators do not get delegations from stakers. that means
// operatorsByStakers may be smaller than operators.
// operatorsByStakers can never be larger than operators anyway, since we have checked
// that operators are already registered.
// it may also be prudent to validate the sorted (or not) nature of these items
// but it is not critical for the functioning. it is only used for comparison
// of the genesis state stored across all of the validators.
Expand Down
Loading

0 comments on commit ae25a47

Please sign in to comment.