-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: introduce MsgOptIn and MsgOptOut #1620
Changes from 1 commit
4eac8bf
8f2269f
a32e882
b0d2337
32fed21
45bf863
78e12c8
656adc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package keeper_test | ||
|
||
import ( | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
testkeeper "github.com/cosmos/interchain-security/v4/testutil/keeper" | ||
"github.com/cosmos/interchain-security/v4/x/ccv/provider/types" | ||
ccvtypes "github.com/cosmos/interchain-security/v4/x/ccv/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
@@ -31,6 +37,53 @@ func TestHandleOptIn(t *testing.T) { | |
require.False(t, providerKeeper.IsToBeOptedIn(ctx, "chainID", providerAddr)) | ||
} | ||
|
||
func TestHandleOptInWithConsumerKey(t *testing.T) { | ||
providerKeeper, ctx, ctrl, mocks := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) | ||
defer ctrl.Finish() | ||
|
||
// generate a consensus public key for the provider | ||
providerConsPubKey := ed25519.GenPrivKeyFromSecret([]byte{1}).PubKey() | ||
consAddr := sdk.ConsAddress(providerConsPubKey.Address()) | ||
providerAddr := types.NewProviderConsAddress(consAddr) | ||
|
||
calls := []*gomock.Call{ | ||
mocks.MockStakingKeeper.EXPECT(). | ||
GetValidatorByConsAddr(gomock.Any(), gomock.Any()). | ||
DoAndReturn(func(ctx sdk.Context, addr sdk.ConsAddress) (stakingtypes.Validator, bool) { | ||
if addr.Equals(providerAddr.Address) { | ||
// Given `providerAddr`, `GetValidatorByConsAddr` returns a validator with the | ||
// exact same `ConsensusPubkey` | ||
pkAny, _ := codectypes.NewAnyWithValue(providerConsPubKey) | ||
return stakingtypes.Validator{ConsensusPubkey: pkAny}, true | ||
} else { | ||
// for any other consensus address, we cannot find a validator | ||
return stakingtypes.Validator{}, false | ||
} | ||
}).Times(2), | ||
} | ||
|
||
gomock.InOrder(calls...) | ||
providerKeeper.SetProposedConsumerChain(ctx, "chainID", 1) | ||
|
||
// create a sample consumer key to assign to the `providerAddr` validator | ||
// on the consumer chain with id `chainID` | ||
consumerKey := "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"Ui5Gf1+mtWUdH8u3xlmzdKID+F3PK0sfXZ73GZ6q6is=\"}" | ||
expectedConsumerPubKey, _ := providerKeeper.ParseConsumerKey(consumerKey) | ||
|
||
err := providerKeeper.HandleOptIn(ctx, "chainID", providerAddr, &consumerKey) | ||
require.NoError(t, err) | ||
|
||
// assert that the consumeKey was assigned to `providerAddr` validator on chain with id `chainID` | ||
actualConsumerPubKey, found := providerKeeper.GetValidatorConsumerPubKey(ctx, "chainID", providerAddr) | ||
require.True(t, found) | ||
require.Equal(t, expectedConsumerPubKey, actualConsumerPubKey) | ||
|
||
// assert that the `consumerAddr` to `providerAddr` association was set as well | ||
consumerAddr, _ := ccvtypes.TMCryptoPublicKeyToConsAddr(actualConsumerPubKey) | ||
actualProviderConsAddr, found := providerKeeper.GetValidatorByConsumerAddr(ctx, "chainID", types.NewConsumerConsAddress(consumerAddr)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should definitely test that it is possible to assign a key the "normal way" after doing it like this. not sure if here is the right place to do it, or if it is easier with higher-level tests, e.g. e2e There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conceptually, if we exclude opting in, a |
||
require.Equal(t, providerAddr, actualProviderConsAddr) | ||
} | ||
|
||
func TestHandleOptOut(t *testing.T) { | ||
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t)) | ||
defer ctrl.Finish() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took this method code as is from
msg_server.go
(see below). I chose not to reuse theAssignConsumerKey
method inpartial_set_security.go
when opting in a validator with a consumer key because this would entail:MsgAssign
message insideHandleOptIn
which seems counterintuitive in the sense that a message creates another message internally to do something;AssignConsumerKey
would emit emit an assignment event and potentially this could confuse relayers becuse for oneMsgOptIn
message, relayers get events as if another message was executed as well.