diff --git a/x/ccv/provider/keeper/key_assignment.go b/x/ccv/provider/keeper/key_assignment.go index 5b66fc7b86..92e79b9653 100644 --- a/x/ccv/provider/keeper/key_assignment.go +++ b/x/ccv/provider/keeper/key_assignment.go @@ -547,8 +547,9 @@ func (k Keeper) AssignConsumerKey( return nil } -// MustApplyKeyAssignmentToValUpdates applies the key assignment to the validator updates -// received from the staking module. +// MustApplyKeyAssignmentToValUpdates applies the key assignment to the validator updates received from the +// staking module. For validators that do not have a validator update in `valUpdates`, the method also considers +// key-assignment replacements when the `considerKeyReplacement` predicate evaluates to `true` for this validator. // The method panics if the key-assignment state is corrupted. func (k Keeper) MustApplyKeyAssignmentToValUpdates( ctx sdk.Context, @@ -610,9 +611,9 @@ func (k Keeper) MustApplyKeyAssignmentToValUpdates( for _, replacement := range k.GetAllKeyAssignmentReplacements(ctx, chainID) { providerAddr := types.NewProviderConsAddress(replacement.ProviderAddr) - // only consider updates for validators that are considered here ... + // filter out key-assignment replacements if !considerKeyReplacement(providerAddr) { - return + continue } k.DeleteKeyAssignmentReplacement(ctx, chainID, providerAddr) newUpdates = append(newUpdates, abci.ValidatorUpdate{ diff --git a/x/ccv/provider/keeper/msg_server.go b/x/ccv/provider/keeper/msg_server.go index eaee4dfb8f..4824f9cf8a 100644 --- a/x/ccv/provider/keeper/msg_server.go +++ b/x/ccv/provider/keeper/msg_server.go @@ -139,20 +139,30 @@ func (k msgServer) SubmitConsumerDoubleVoting(goCtx context.Context, msg *types. func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.MsgOptInResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) + providerValidatorAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr) + if err != nil { + return nil, err + } + + // validator must already be registered + validator, found := k.stakingKeeper.GetValidator(ctx, providerValidatorAddr) + if !found { + return nil, stakingtypes.ErrNoValidatorFound + } + + consAddress, err := validator.GetConsAddr() if err != nil { return nil, err } - // FIXME: something is off here .. val to cons ... - providerAddr := types.NewProviderConsAddress(valAddress) + providerConsAddr := types.NewProviderConsAddress(consAddress) if err != nil { return nil, err } if msg.ConsumerKey != "" { - err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, &msg.ConsumerKey) + err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerConsAddr, &msg.ConsumerKey) } else { - err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerAddr, nil) + err = k.Keeper.HandleOptIn(ctx, msg.ChainId, providerConsAddr, nil) } if err != nil { @@ -173,16 +183,27 @@ func (k msgServer) OptIn(goCtx context.Context, msg *types.MsgOptIn) (*types.Msg func (k msgServer) OptOut(goCtx context.Context, msg *types.MsgOptOut) (*types.MsgOptOutResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - valAddress, err := sdk.ConsAddressFromBech32(msg.ProviderAddr) + providerValidatorAddr, err := sdk.ValAddressFromBech32(msg.ProviderAddr) + if err != nil { + return nil, err + } + + // validator must already be registered + validator, found := k.stakingKeeper.GetValidator(ctx, providerValidatorAddr) + if !found { + return nil, stakingtypes.ErrNoValidatorFound + } + + consAddress, err := validator.GetConsAddr() if err != nil { return nil, err } - providerAddr := types.NewProviderConsAddress(valAddress) + providerConsAddr := types.NewProviderConsAddress(consAddress) if err != nil { return nil, err } - err = k.Keeper.HandleOptOut(ctx, msg.ChainId, providerAddr) + err = k.Keeper.HandleOptOut(ctx, msg.ChainId, providerConsAddr) if err != nil { return nil, err } diff --git a/x/ccv/provider/keeper/partial_set_security.go b/x/ccv/provider/keeper/partial_set_security.go index 6337777a31..87fd23f7fd 100644 --- a/x/ccv/provider/keeper/partial_set_security.go +++ b/x/ccv/provider/keeper/partial_set_security.go @@ -75,25 +75,15 @@ func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, providerAddr types return nil } -// getValidatorsPublicKey is a helper function that returns the public key of the validator -func (k Keeper) getValidatorsPublicKey(validator stakingtypes.Validator) (tmprotocrypto.PublicKey, error) { - consAddr, err := validator.GetConsAddr() - if err != nil { - return tmprotocrypto.PublicKey{}, err - } - return tmprotocrypto.PublicKey{ - Sum: &tmprotocrypto.PublicKey_Ed25519{ - Ed25519: consAddr.Bytes(), - }, - }, nil -} - -// getValidatorOperatorAddressAndPublicKey is a helper function that returns the public key of the validator -func (k Keeper) getValidatorOperatorAddressAndPublicKey(ctx sdk.Context, addr types.ProviderConsAddress) (sdk.ValAddress, tmprotocrypto.PublicKey, error) { +// getValAddressAndPublicKey is a helper function that returns the `ValAddress` and the public key of +// the corresponding validator +func (k Keeper) getValAddressAndPublicKey(ctx sdk.Context, addr types.ProviderConsAddress, +) (sdk.ValAddress, tmprotocrypto.PublicKey, error) { validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, addr.ToSdkConsAddr()) if !found { - + return sdk.ValAddress{}, tmprotocrypto.PublicKey{}, stakingtypes.ErrNoValidatorFound } + consAddr, err := validator.GetConsAddr() if err != nil { return sdk.ValAddress{}, tmprotocrypto.PublicKey{}, err @@ -107,170 +97,15 @@ func (k Keeper) getValidatorOperatorAddressAndPublicKey(ctx sdk.Context, addr ty return validator.GetOperator(), pubKey, nil } -//func filterAddresses(addresses []types.ProviderConsAddress, predicate func(types.ProviderConsAddress) bool) []types.ProviderConsAddress { -// var filteredAddresses []types.ProviderConsAddress -// for _, addr := range addresses { -// if predicate(addr) { -// filteredAddresses = append(filteredAddresses, addr) -// } -// } -// return filteredAddresses -//} -// -//func filterOptedInValidators(addresses []OptedInValidator, predicate func(validator OptedInValidator) bool) []OptedInValidator { -// var filteredAddresses []OptedInValidator -// for _, addr := range addresses { -// if predicate(addr) { -// filteredAddresses = append(filteredAddresses, addr) -// } -// } -// return filteredAddresses -//} - -//// GetBondedToBeOptedInValidators returns all the bonded validators that are to be opted in -//// on consumer chain with `chainID` -//func (k Keeper) GetBondedToBeOptedInValidators(ctx sdk.Context, chainID string) []types.ProviderConsAddress { -// var consAddresses []types.ProviderConsAddress -// -// bondedToBeOptedInValidators := -// filterAddresses(k.GetToBeOptedIn(ctx, chainID), func(addr types.ProviderConsAddress) bool { -// validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, addr.ToSdkConsAddr()) -// if !found { -// // a validator was successfully set to be opted in, but we cannot find this validator anymore -// k.Logger(ctx).Error("could not find to-be-opted-in invalidator with consensus address: %s", addr.ToSdkConsAddr().Bytes()) -// return false -// } -// -// if !validator.IsBonded() { -// // only consider validators that are in the active set -// return false -// } -// return true -// }) -// -// return bondedToBeOptedInValidators -// -// //for _, addr := range k.GetToBeOptedIn(ctx, chainID) { -// // validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, addr.ToSdkConsAddr()) -// // if !found { -// // // a validator was successfully set to be opted in, but we cannot find this validator anymore -// // k.Logger(ctx).Error("could not find to-be-opted-in invalidator with consensus address: %s", addr.ToSdkConsAddr().Bytes()) -// // continue -// // } -// // -// // if !validator.IsBonded() { -// // // only consider validators that are in the active set -// // continue -// // } -// // consAddresses = append(consAddresses, addr) -// //} -// -// return consAddresses -//} - -//// GetOptedInValidatorsForValUpdates returns all the addresses of validators that are going to be opted in -//// next on the consumer chain with `chainID` and a validator update would need to be sent for them -//func (k Keeper) GetOptedInValidatorsForValUpdates(ctx sdk.Context, chainID string) []types.ProviderConsAddress { -// var consAddresses []types.ProviderConsAddress -// -// // Create set that contains all the validators that are to be opted out so that we do not reintroduce opted-out -// // validators when going through the already opted in validators. -// isSetToBeOptedOut := make(map[string]bool) -// for _, addr := range k.GetToBeOptedOut(ctx, chainID) { -// isSetToBeOptedOut[addr.ToSdkConsAddr().String()] = true -// } -// -// for _, val := range k.GetOptedIn(ctx, chainID) { -// // go through all the validators that are currently opted in -// validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, val.ProviderAddr.ToSdkConsAddr()) -// if !found { -// // a validator was opted in, but we cannot find this validator anymore -// k.Logger(ctx).Error("could not find opted-in validator with consensus address: %s", -// val.ProviderAddr.ToSdkConsAddr().Bytes()) -// continue -// } -// -// if isSetToBeOptedOut[val.ProviderAddr.ToSdkConsAddr().String()] { -// continue -// } -// -// if val.Power == uint64(k.stakingKeeper.GetLastValidatorPower(ctx, validator.GetOperator())) { -// // Only send an update if an opted-in validator had a power change since the lat time it was sent. -// // If an opted-in validator is not in the active set anymore, then its new power is 0 and hence we -// // do not consider this validator in the next set of opted in validators. -// continue -// } -// -// consAddresses = append(consAddresses, val.ProviderAddr) -// } -// -// // append all the to-be-opted-in validators -// consAddresses = append(consAddresses, k.GetBondedToBeOptedInValidators(ctx, chainID)...) -// return consAddresses -//} - -//// ComputePartialSetValidatorUpdates returns the partial set of `ValidatorUpdate`s that the provider chain sends to the -//// consumer chain. Receives `nextOptedIn` that corresponds to the validators that would be opted in next and -//// `toBeOptedOut` that contains the validators that would be opted out. -//func (k Keeper) ComputePartialSetValidatorUpdates(ctx sdk.Context, optedInValidatorsForValUpdates []types.ProviderConsAddress, -// toBeOptedOut []types.ProviderConsAddress) []abci.ValidatorUpdate { -// var partialSetUpdates []abci.ValidatorUpdate -// -// for _, val := range optedInValidatorsForValUpdates { -// validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, val.ToSdkConsAddr()) -// if !found { -// // any validator in `optedInValidatorsForValUpdates` should be found -// k.Logger(ctx).Error("could not find validator with consensus address: %s", -// val.ToSdkConsAddr().Bytes()) -// continue -// } -// -// pubKey, err := k.getValidatorsPublicKey(validator) -// if err != nil { -// k.Logger(ctx).Error("could retrieve public key from validator with consensus address: %s", -// val.ToSdkConsAddr().Bytes()) -// continue -// } -// -// // if a validator that was opted in, is not in the active set anymore, then `GetLastValidatorPower` returns 0 -// partialSetUpdates = append(partialSetUpdates, abci.ValidatorUpdate{ -// PubKey: pubKey, -// Power: k.stakingKeeper.GetLastValidatorPower(ctx, validator.GetOperator()), -// }) -// } -// -// for _, addr := range toBeOptedOut { -// // go through all the validators that are currently opted in -// validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, addr.ToSdkConsAddr()) -// if !found { -// // a validator was opted in, but we cannot find this validator anymore -// k.Logger(ctx).Error("could not find opted-in validator with consensus address: %s", -// addr.ToSdkConsAddr().Bytes()) -// continue -// } -// pubKey, err := k.getValidatorsPublicKey(validator) -// if err != nil { -// k.Logger(ctx).Error("could retrieve public key from validator with consensus address: %s", -// addr.ToSdkConsAddr().Bytes()) -// continue -// } -// -// // send power 0 so the validator is removed -// partialSetUpdates = append(partialSetUpdates, abci.ValidatorUpdate{ -// PubKey: pubKey, -// Power: 0, -// }) -// } -// -// return partialSetUpdates -//} - -func (k Keeper) ComputeNextValidators(ctx sdk.Context, currentValidators []OptedInValidator, - validatorsToAdd []types.ProviderConsAddress, - validatorsToRemove []types.ProviderConsAddress, +// ComputeNextValidators computes the next validator set that is responsible for validating on a consumer chain. +// The returned opted-in validators by `ComputeNextValidators` constitute the next `currentValidators`. +func (k Keeper) ComputeNextValidators(ctx sdk.Context, + currentValidators []OptedInValidator, + validatorAddressesToAdd []types.ProviderConsAddress, + validatorAddressesToRemove []types.ProviderConsAddress, ) []OptedInValidator { isRemoved := make(map[string]bool) - for _, val := range validatorsToRemove { + for _, val := range validatorAddressesToRemove { isRemoved[val.ToSdkConsAddr().String()] = true } @@ -279,9 +114,8 @@ func (k Keeper) ComputeNextValidators(ctx sdk.Context, currentValidators []Opted if isRemoved[val.ProviderAddr.ToSdkConsAddr().String()] { continue } - valAddress, _, err := k.getValidatorOperatorAddressAndPublicKey(ctx, val.ProviderAddr) + valAddress, _, err := k.getValAddressAndPublicKey(ctx, val.ProviderAddr) if err != nil { - // FIXME continue } @@ -292,10 +126,9 @@ func (k Keeper) ComputeNextValidators(ctx sdk.Context, currentValidators []Opted out = append(out, val) } - for _, addr := range validatorsToAdd { - valAddress, _, err := k.getValidatorOperatorAddressAndPublicKey(ctx, addr) + for _, addr := range validatorAddressesToAdd { + valAddress, _, err := k.getValAddressAndPublicKey(ctx, addr) if err != nil { - // FIXME continue } @@ -314,17 +147,19 @@ func (k Keeper) ComputeNextValidators(ctx sdk.Context, currentValidators []Opted return out } +// ComputeValidatorUpdates computes the validator updates needed to be sent to the consumer chain to capture +// the newly opted-in and opted-out validators, as well as validators that unbonded. func (k Keeper) ComputeValidatorUpdates(ctx sdk.Context, currentValidators []OptedInValidator, - validatorsToAdd []types.ProviderConsAddress, - validatorsToRemove []types.ProviderConsAddress, + validatorAddressesToAdd []types.ProviderConsAddress, + validatorAddressesToRemove []types.ProviderConsAddress, ) []abci.ValidatorUpdate { var m = make(map[string]abci.ValidatorUpdate) for _, val := range currentValidators { - valAddress, pubKey, err := k.getValidatorOperatorAddressAndPublicKey(ctx, val.ProviderAddr) + valAddress, pubKey, err := k.getValAddressAndPublicKey(ctx, val.ProviderAddr) if err != nil { - // FIXME ... + continue } validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, val.ProviderAddr.ToSdkConsAddr()) @@ -336,24 +171,25 @@ func (k Keeper) ComputeValidatorUpdates(ctx sdk.Context, continue } - // if this validator has unbonded, the result would be 0 of power so we still good!! but then if you add, you readd - // no, because we check if bonded when we readd ... you cannot readd if already here + // if `val` has unbonded, its `GetLastValidatorPower` power returns 0. m[pubKey.String()] = abci.ValidatorUpdate{ PubKey: pubKey, Power: k.stakingKeeper.GetLastValidatorPower(ctx, valAddress), } } - for _, addr := range validatorsToAdd { - valAddress, pubKey, err := k.getValidatorOperatorAddressAndPublicKey(ctx, addr) + for _, addr := range validatorAddressesToAdd { + valAddress, pubKey, err := k.getValAddressAndPublicKey(ctx, addr) if err != nil { - + continue } validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, addr.ToSdkConsAddr()) if !found { continue } + + // if a validator is in the active set, we do not add it if !validator.IsBonded() { continue } @@ -364,8 +200,8 @@ func (k Keeper) ComputeValidatorUpdates(ctx sdk.Context, } } - for _, addr := range validatorsToRemove { - _, pubKey, err := k.getValidatorOperatorAddressAndPublicKey(ctx, addr) + for _, addr := range validatorAddressesToRemove { + _, pubKey, err := k.getValAddressAndPublicKey(ctx, addr) if err != nil { } @@ -382,9 +218,7 @@ func (k Keeper) ComputeValidatorUpdates(ctx sdk.Context, out = append(out, update) } - // similar to `AccumulateChanges` - // The list of tendermint updates should hash the same across all consensus nodes - // that means it is necessary to sort for determinism. + // Similarly to `AccumulateChanges`, we sort validators for determinism. sort.Slice(out, func(i, j int) bool { if out[i].Power != out[j].Power { return out[i].Power > out[j].Power diff --git a/x/ccv/provider/keeper/partial_set_security_test.go b/x/ccv/provider/keeper/partial_set_security_test.go index 3f798a9cfb..e02ae0a29b 100644 --- a/x/ccv/provider/keeper/partial_set_security_test.go +++ b/x/ccv/provider/keeper/partial_set_security_test.go @@ -122,309 +122,6 @@ func TestHandleOptOut(t *testing.T) { require.False(t, providerKeeper.IsToBeOptedOut(ctx, "chainID", providerAddr)) } -//func TestGetToBeOptedInValidators(t *testing.T) { -// providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) -// defer ctrl.Finish() -// -// chainID := "chainID" -// -// var expectedAddresses []types.ProviderConsAddress -// // set 10 validators as to-be-opted-in, but only 3 (= 10/3) of those are `Bonded` -// for i := int64(0); i < 10; i++ { -// // generate a consensus public key for the provider -// providerConsPubKey := ed25519.GenPrivKeyFromSecret([]byte{uint8(i)}).PubKey() -// consAddr := sdk.ConsAddress(providerConsPubKey.Address()) -// providerAddr := types.NewProviderConsAddress(consAddr) -// -// var providerValidatorAddr sdk.ValAddress -// providerValidatorAddr = providerAddr.Address.Bytes() -// -// var status stakingtypes.BondStatus -// if i%3 == 0 { -// status = stakingtypes.Unbonded -// } else if i%3 == 1 { -// status = stakingtypes.Bonded -// // we only expect bonded validators to be returned by `GetBondedToBeOptedInValidators` -// expectedAddresses = append(expectedAddresses, providerAddr) -// } else { -// status = stakingtypes.Unbonding -// } -// -// pkAny, _ := codectypes.NewAnyWithValue(providerConsPubKey) -// validator := stakingtypes.Validator{ -// OperatorAddress: providerValidatorAddr.String(), -// ConsensusPubkey: pkAny, -// Status: status, -// } -// -// mocks.MockStakingKeeper.EXPECT(). -// GetValidatorByConsAddr(ctx, consAddr).Return(validator, true).AnyTimes() -// -// providerKeeper.SetToBeOptedIn(ctx, chainID, providerAddr) -// } -// -// actualAddresses := providerKeeper.GetBondedToBeOptedInValidators(ctx, chainID) -// -// // sort before comparing -// sort.Slice(expectedAddresses, func(i int, j int) bool { -// return strings.Compare(expectedAddresses[i].String(), expectedAddresses[j].String()) < 0 -// }) -// sort.Slice(actualAddresses, func(i int, j int) bool { -// return strings.Compare(actualAddresses[i].String(), actualAddresses[j].String()) < 0 -// }) -// require.Equal(t, expectedAddresses, actualAddresses) -//} -// -//func TestGetNextOptedInValidators(t *testing.T) { -// providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) -// defer ctrl.Finish() -// -// chainID := "chainID" -// -// // create 10 validators -// var providerAddresses []types.ProviderConsAddress -// for i := 0; i < 10; i++ { -// // generate a consensus public key for the provider -// providerConsPubKey := ed25519.GenPrivKeyFromSecret([]byte{uint8(i)}).PubKey() -// consAddr := sdk.ConsAddress(providerConsPubKey.Address()) -// providerAddr := types.NewProviderConsAddress(consAddr) -// providerAddresses = append(providerAddresses, providerAddr) -// -// var providerValidatorAddr sdk.ValAddress -// providerValidatorAddr = providerAddr.Address.Bytes() -// -// pkAny, _ := codectypes.NewAnyWithValue(providerConsPubKey) -// validator := stakingtypes.Validator{ -// OperatorAddress: providerValidatorAddr.String(), -// ConsensusPubkey: pkAny, -// Status: stakingtypes.Bonded, -// } -// -// mocks.MockStakingKeeper.EXPECT(). -// GetValidatorByConsAddr(ctx, consAddr).Return(validator, true).AnyTimes() -// -// mocks.MockStakingKeeper.EXPECT(). -// GetLastValidatorPower(ctx, providerValidatorAddr).Return(int64(i)).AnyTimes() -// } -// -// // first 3 validators (i.e., 0, 1, and 2) are already opted in but with the same power as they currently have -// // and therefore should not be returned by `GetOptedInValidatorsForValUpdates` -// for i := 0; i < 3; i++ { -// providerKeeper.SetOptedIn(ctx, chainID, providerAddresses[i], 1, uint64(i)) -// -// } -// -// // validators 3, 4, 5 and 6 are already opted in but with a different voting power -// // and therefore should be returned by `GetOptedInValidatorsForValUpdates` (unless opted out -- see below) -// for i := 3; i < 7; i++ { -// providerKeeper.SetOptedIn(ctx, chainID, providerAddresses[i], 1, uint64(i+1)) -// } -// -// // validators 8 and 9 are to be opted in -// for i := 8; i < 10; i++ { -// providerKeeper.SetToBeOptedIn(ctx, chainID, providerAddresses[i]) -// } -// -// // first 5 validators are to be opted out and hence validators 3 and 4 won't be returned -// // by `GetOptedInValidatorsForValUpdates` -// for i := 0; i < 5; i++ { -// providerKeeper.SetToBeOptedOut(ctx, chainID, providerAddresses[i]) -// } -// -// expectedAddresses := []types.ProviderConsAddress{ -// providerAddresses[5], -// providerAddresses[6], -// providerAddresses[8], -// providerAddresses[9], -// } -// actualAddresses := providerKeeper.GetOptedInValidatorsForValUpdates(ctx, chainID) -// -// // sort before comparing -// sort.Slice(expectedAddresses, func(i int, j int) bool { -// return strings.Compare(expectedAddresses[i].String(), expectedAddresses[j].String()) < 0 -// }) -// sort.Slice(actualAddresses, func(i int, j int) bool { -// return strings.Compare(actualAddresses[i].String(), actualAddresses[j].String()) < 0 -// }) -// require.Equal(t, expectedAddresses, actualAddresses) -//} -// -//func TestComputePartialSetValidatorUpdates(t *testing.T) { -// providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) -// defer ctrl.Finish() -// -// // create 10 validators -// var providerAddresses []types.ProviderConsAddress -// var pubKeys []tmprotocrypto.PublicKey -// for i := 0; i < 10; i++ { -// // generate a consensus public key for the provider -// providerConsPubKey := ed25519.GenPrivKeyFromSecret([]byte{uint8(i)}).PubKey() -// consAddr := sdk.ConsAddress(providerConsPubKey.Address()) -// providerAddr := types.NewProviderConsAddress(consAddr) -// providerAddresses = append(providerAddresses, providerAddr) -// -// var providerValidatorAddr sdk.ValAddress -// providerValidatorAddr = providerAddr.Address.Bytes() -// -// pkAny, _ := codectypes.NewAnyWithValue(providerConsPubKey) -// validator := stakingtypes.Validator{ -// OperatorAddress: providerValidatorAddr.String(), -// ConsensusPubkey: pkAny, -// Status: stakingtypes.Bonded, -// } -// -// pubKey := tmprotocrypto.PublicKey{ -// Sum: &tmprotocrypto.PublicKey_Ed25519{ -// Ed25519: consAddr.Bytes(), -// }, -// } -// pubKeys = append(pubKeys, pubKey) -// -// mocks.MockStakingKeeper.EXPECT(). -// GetValidatorByConsAddr(ctx, consAddr).Return(validator, true).AnyTimes() -// -// mocks.MockStakingKeeper.EXPECT(). -// GetLastValidatorPower(ctx, providerValidatorAddr).Return(int64(i)).AnyTimes() -// } -// -// // first 6 validators to opt in -// var nextOptedIn []types.ProviderConsAddress -// for i := 0; i < 6; i++ { -// nextOptedIn = append(nextOptedIn, providerAddresses[i]) -// } -// -// var optedOut []types.ProviderConsAddress -// for i := 6; i < 10; i++ { -// optedOut = append(optedOut, providerAddresses[i]) -// } -// -// expectedValUpdates := []abci.ValidatorUpdate{ -// {PubKey: pubKeys[0], Power: 0}, -// {PubKey: pubKeys[1], Power: 1}, -// {PubKey: pubKeys[2], Power: 2}, -// {PubKey: pubKeys[3], Power: 3}, -// {PubKey: pubKeys[4], Power: 4}, -// {PubKey: pubKeys[5], Power: 5}, -// {PubKey: pubKeys[6], Power: 0}, -// {PubKey: pubKeys[7], Power: 0}, -// {PubKey: pubKeys[8], Power: 0}, -// {PubKey: pubKeys[9], Power: 0}, -// } -// -// actualValUpdates := providerKeeper.ComputePartialSetValidatorUpdates(ctx, nextOptedIn, optedOut) -// -// sort.Slice(expectedValUpdates, func(i int, j int) bool { -// if expectedValUpdates[i].PubKey.Compare(expectedValUpdates[j].PubKey) < 0 { -// return true -// } else if expectedValUpdates[i].PubKey.Compare(expectedValUpdates[j].PubKey) == 0 { -// return expectedValUpdates[i].Power < expectedValUpdates[j].Power -// } -// return false -// }) -// sort.Slice(actualValUpdates, func(i int, j int) bool { -// if actualValUpdates[i].PubKey.Compare(actualValUpdates[j].PubKey) < 0 { -// return true -// } else if actualValUpdates[i].PubKey.Compare(actualValUpdates[j].PubKey) == 0 { -// return actualValUpdates[i].Power < actualValUpdates[j].Power -// } -// return false -// }) -// -// require.Equal(t, expectedValUpdates, actualValUpdates) -// -//} -// -//func TestResetOptedInSet(t *testing.T) { -// providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) -// defer ctrl.Finish() -// -// chainID := "chainID" -// -// // create 10 validators -// var providerAddresses []types.ProviderConsAddress -// var pubKeys []tmprotocrypto.PublicKey -// for i := 0; i < 10; i++ { -// // generate a consensus public key for the provider -// providerConsPubKey := ed25519.GenPrivKeyFromSecret([]byte{uint8(i)}).PubKey() -// consAddr := sdk.ConsAddress(providerConsPubKey.Address()) -// providerAddr := types.NewProviderConsAddress(consAddr) -// providerAddresses = append(providerAddresses, providerAddr) -// -// var providerValidatorAddr sdk.ValAddress -// providerValidatorAddr = providerAddr.Address.Bytes() -// -// pkAny, _ := codectypes.NewAnyWithValue(providerConsPubKey) -// validator := stakingtypes.Validator{ -// OperatorAddress: providerValidatorAddr.String(), -// ConsensusPubkey: pkAny, -// Status: stakingtypes.Bonded, -// } -// -// pubKey := tmprotocrypto.PublicKey{ -// Sum: &tmprotocrypto.PublicKey_Ed25519{ -// Ed25519: consAddr.Bytes(), -// }, -// } -// pubKeys = append(pubKeys, pubKey) -// -// mocks.MockStakingKeeper.EXPECT(). -// GetValidatorByConsAddr(ctx, consAddr).Return(validator, true).AnyTimes() -// -// if i != 1 { -// mocks.MockStakingKeeper.EXPECT(). -// GetLastValidatorPower(ctx, providerValidatorAddr).Return(int64(i + 1)).AnyTimes() -// } else { -// mocks.MockStakingKeeper.EXPECT(). -// GetLastValidatorPower(ctx, providerValidatorAddr).Return(int64(0)).AnyTimes() -// } -// } -// -// // first 6 validators to opt in -// var toBeOptedIn []types.ProviderConsAddress -// -// providerKeeper.SetOptedIn(ctx, chainID, providerAddresses[0], 1, uint64(1)) -// -// // this validator won't be added because it has a power of 0 .. .see above mock .. not here -// providerKeeper.SetOptedIn(ctx, chainID, providerAddresses[1], 1, uint64(3)) -// -// for i := 2; i < 6; i++ { -// providerKeeper.SetToBeOptedIn(ctx, chainID, providerAddresses[i]) -// toBeOptedIn = append(toBeOptedIn, providerAddresses[i]) -// } -// -// var optedOut []types.ProviderConsAddress -// for i := 6; i < 10; i++ { -// providerKeeper.SetToBeOptedOut(ctx, chainID, providerAddresses[i]) -// optedOut = append(optedOut, providerAddresses[i]) -// } -// -// require.NotEmpty(t, providerKeeper.GetToBeOptedIn(ctx, chainID)) -// require.NotEmpty(t, providerKeeper.GetToBeOptedOut(ctx, chainID)) -// -// providerKeeper.ResetOptedInSet(ctx, chainID) -// -// require.Empty(t, providerKeeper.GetToBeOptedIn(ctx, chainID)) -// require.Empty(t, providerKeeper.GetToBeOptedOut(ctx, chainID)) -// -// actualOptedInValidators := providerKeeper.GetOptedIn(ctx, chainID) -// expectedOptedInValidators := []keeper.OptedInValidator{ -// {ProviderAddr: providerAddresses[0], BlockHeight: 1, Power: 1}, -// {ProviderAddr: providerAddresses[2], BlockHeight: uint64(ctx.BlockHeight()), Power: 3}, -// {ProviderAddr: providerAddresses[3], BlockHeight: uint64(ctx.BlockHeight()), Power: 4}, -// {ProviderAddr: providerAddresses[4], BlockHeight: uint64(ctx.BlockHeight()), Power: 5}, -// {ProviderAddr: providerAddresses[5], BlockHeight: uint64(ctx.BlockHeight()), Power: 6}, -// } -// -// sort.Slice(expectedOptedInValidators, func(i int, j int) bool { -// return strings.Compare(expectedOptedInValidators[i].ProviderAddr.String(), expectedOptedInValidators[j].ProviderAddr.String()) < 0 -// }) -// -// sort.Slice(actualOptedInValidators, func(i int, j int) bool { -// return strings.Compare(actualOptedInValidators[i].ProviderAddr.String(), actualOptedInValidators[j].ProviderAddr.String()) < 0 -// }) -// require.Equal(t, expectedOptedInValidators, actualOptedInValidators) -//} - func TestComputeValidatorUpdates(t *testing.T) { providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) defer ctrl.Finish() diff --git a/x/ccv/provider/keeper/proposal.go b/x/ccv/provider/keeper/proposal.go index 78298e54cc..9ad5ea514d 100644 --- a/x/ccv/provider/keeper/proposal.go +++ b/x/ccv/provider/keeper/proposal.go @@ -268,12 +268,12 @@ func (k Keeper) MakeConsumerGenesis( return gen, nil, err } - validator, found := k.stakingKeeper.GetValidator(ctx, addr) + val, found := k.stakingKeeper.GetValidator(ctx, addr) if !found { return gen, nil, errorsmod.Wrapf(stakingtypes.ErrNoValidatorFound, "error getting validator from LastValidatorPowers: %s", err) } - tmProtoPk, err := validator.TmConsPublicKey() + tmProtoPk, err := val.TmConsPublicKey() if err != nil { return gen, nil, err }