-
Notifications
You must be signed in to change notification settings - Fork 657
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
Create Parent Genesis and Tests #418
Changes from all commits
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 |
---|---|---|
|
@@ -322,24 +322,21 @@ func (am AppModule) OnRecvPacket( | |
_ sdk.AccAddress, | ||
) ibcexported.Acknowledgement { | ||
var ( | ||
ack ibcexported.Acknowledgement | ||
ack *channeltypes.Acknowledgement | ||
data ccv.ValidatorSetChangePacketData | ||
) | ||
if err := ccv.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { | ||
ack = channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal CCV packet data: %s", err.Error())) | ||
errAck := channeltypes.NewErrorAcknowledgement(fmt.Sprintf("cannot unmarshal CCV packet data: %s", err.Error())) | ||
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. The error acknowledgement message cannot contain the unmarshal json error string |
||
ack = &errAck | ||
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. why is |
||
} else { | ||
var err error | ||
ack, err = am.keeper.OnRecvPacket(ctx, packet, data) | ||
if err != nil { | ||
ack = channeltypes.NewErrorAcknowledgement(err.Error()) | ||
} | ||
ack = am.keeper.OnRecvPacket(ctx, packet, data) | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
ccv.EventTypePacket, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), | ||
sdk.NewAttribute(ccv.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())), | ||
sdk.NewAttribute(ccv.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack != nil)), | ||
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. This returns true even on an error acknowledgement |
||
), | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,55 @@ import ( | |
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/ibc-go/modules/apps/ccv/parent/types" | ||
parenttypes "github.com/cosmos/ibc-go/modules/apps/ccv/parent/types" | ||
"github.com/cosmos/ibc-go/modules/apps/ccv/types" | ||
) | ||
|
||
func (k Keeper) InitGenesis(ctx sdk.Context) { | ||
k.SetPort(ctx, types.PortID) | ||
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.ParentGenesisState) { | ||
k.SetPort(ctx, parenttypes.PortID) | ||
|
||
// Only try to bind to port if it is not already bound, since we may already own | ||
// port capability from capability InitGenesis | ||
if !k.IsBound(ctx, types.PortID) { | ||
if !k.IsBound(ctx, parenttypes.PortID) { | ||
// transfer module binds to the transfer port on InitChain | ||
// and claims the returned capability | ||
err := k.BindPort(ctx, types.PortID) | ||
err := k.BindPort(ctx, parenttypes.PortID) | ||
if err != nil { | ||
panic(fmt.Sprintf("could not claim port capability: %v", err)) | ||
} | ||
} | ||
|
||
// Set initial state for each child chain | ||
for _, cc := range genState.ChildStates { | ||
k.SetChainToChannel(ctx, cc.ChainId, cc.ChannelId) | ||
k.SetChannelToChain(ctx, cc.ChannelId, cc.ChainId) | ||
k.SetChannelStatus(ctx, cc.ChannelId, cc.Status) | ||
} | ||
} | ||
|
||
func (k Keeper) ExportGenesis(ctx sdk.Context) types.ParentGenesisState { | ||
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. godoc |
||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, []byte(parenttypes.ChannelToChainKeyPrefix+"/")) | ||
defer iterator.Close() | ||
|
||
if !iterator.Valid() { | ||
return types.DefaultParentGenesisState() | ||
} | ||
|
||
var childStates []types.ChildState | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
channelID := string(iterator.Key()) | ||
chainID := string(iterator.Value()) | ||
|
||
status := k.GetChannelStatus(ctx, channelID) | ||
cc := types.ChildState{ | ||
ChainId: chainID, | ||
ChannelId: channelID, | ||
Status: status, | ||
} | ||
childStates = append(childStates, cc) | ||
} | ||
|
||
return types.NewParentGenesisState(childStates) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/ibc-go/modules/apps/ccv/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestGenesis() { | ||
// set some chain-channel pairs before exporting | ||
ctx := suite.parentChain.GetContext() | ||
for i := 0; i < 4; i++ { | ||
suite.parentChain.GetSimApp().ParentKeeper.SetChainToChannel(ctx, fmt.Sprintf("chainid-%d", i), fmt.Sprintf("channel-%d", i)) | ||
suite.parentChain.GetSimApp().ParentKeeper.SetChannelToChain(ctx, fmt.Sprintf("channel-%d", i), fmt.Sprintf("chainid-%d", i)) | ||
suite.parentChain.GetSimApp().ParentKeeper.SetChannelStatus(ctx, fmt.Sprintf("channel-%d", i), types.Status(i)) | ||
} | ||
|
||
genState := suite.parentChain.GetSimApp().ParentKeeper.ExportGenesis(suite.parentChain.GetContext()) | ||
|
||
suite.childChain.GetSimApp().ParentKeeper.InitGenesis(suite.childChain.GetContext(), genState) | ||
|
||
ctx = suite.childChain.GetContext() | ||
for i := 0; i < 4; i++ { | ||
expectedChainId := fmt.Sprintf("chainid-%d", i) | ||
expectedChannelId := fmt.Sprintf("channelid-%d", i) | ||
channelID, channelOk := suite.childChain.GetSimApp().ParentKeeper.GetChainToChannel(ctx, expectedChainId) | ||
chainID, chainOk := suite.childChain.GetSimApp().ParentKeeper.GetChannelToChain(ctx, expectedChannelId) | ||
suite.Require().True(channelOk) | ||
suite.Require().True(chainOk) | ||
suite.Require().Equal(expectedChainId, chainID, "did not store correct chain id for given channel id") | ||
suite.Require().Equal(expectedChannelId, channelID, "did not store correct channel id for given chain id") | ||
|
||
status := suite.childChain.GetSimApp().ParentKeeper.GetChannelStatus(ctx, channelID) | ||
suite.Require().Equal(int32(i), status, "status is unexpected for given channel id: %s", channelID) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ import ( | |
host "github.com/cosmos/ibc-go/modules/core/24-host" | ||
ibcexported "github.com/cosmos/ibc-go/modules/core/exported" | ||
ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
) | ||
|
||
|
@@ -144,20 +143,25 @@ func (k Keeper) GetChannelToChain(ctx sdk.Context, channelID string) (string, bo | |
return string(bz), true | ||
} | ||
|
||
// SetUnbondingChanges sets the unbonding changes for a given baby chain and sequence | ||
func (k Keeper) SetUnbondingChanges(ctx sdk.Context, chainID string, seq uint64, valUpdates []abci.ValidatorUpdate) { | ||
// TODO | ||
} | ||
// IterateChannelToChain iterates over the channel to chain mappings and calls the provided callback until the iteration ends | ||
// or the callback returns stop=true | ||
func (k Keeper) IterateChannelToChain(ctx sdk.Context, cb func(ctx sdk.Context, channelID, chainID string) (stop bool)) { | ||
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. this func needs a test |
||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, []byte(types.ChannelToChainKeyPrefix+"/")) | ||
defer iterator.Close() | ||
|
||
// GetUnbondingChanges gets the unbonding changes for a given baby chain and sequence | ||
func (k Keeper) GetUnbondingChanges(ctx sdk.Context, chainID string, seq uint64) []abci.ValidatorUpdate { | ||
// TODO | ||
return nil | ||
} | ||
if !iterator.Valid() { | ||
return | ||
} | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
channelID := string(iterator.Key()) | ||
chainID := string(iterator.Value()) | ||
|
||
// DeleteUnbondingChanges deletes the unbonding changes for a given baby chain and sequence | ||
func (k Keeper) DeleteUnbondingChanges(ctx sdk.Context, chainID string, seq uint64) { | ||
// TODO | ||
if cb(ctx, channelID, chainID) { | ||
break | ||
} | ||
} | ||
} | ||
|
||
// SetChannelStatus sets the status of a CCV channel with the given status | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
childtypes "github.com/cosmos/ibc-go/modules/apps/ccv/child/types" | ||
parenttypes "github.com/cosmos/ibc-go/modules/apps/ccv/parent/types" | ||
"github.com/cosmos/ibc-go/modules/apps/ccv/types" | ||
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" | ||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||
commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" | ||
ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" | ||
ibctesting "github.com/cosmos/ibc-go/testing" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
|
||
coordinator *ibctesting.Coordinator | ||
|
||
// testing chains | ||
parentChain *ibctesting.TestChain | ||
childChain *ibctesting.TestChain | ||
|
||
parentClient *ibctmtypes.ClientState | ||
parentConsState *ibctmtypes.ConsensusState | ||
|
||
path *ibctesting.Path | ||
|
||
ctx sdk.Context | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) | ||
suite.parentChain = suite.coordinator.GetChain(ibctesting.GetChainID(0)) | ||
suite.childChain = suite.coordinator.GetChain(ibctesting.GetChainID(1)) | ||
|
||
tmConfig := ibctesting.NewTendermintConfig() | ||
|
||
// commit a block on parent chain before creating client | ||
suite.coordinator.CommitBlock(suite.parentChain) | ||
|
||
// create client and consensus state of parent chain to initialize child chain genesis. | ||
height := suite.parentChain.LastHeader.GetHeight().(clienttypes.Height) | ||
UpgradePath := []string{"upgrade", "upgradedIBCState"} | ||
|
||
suite.parentClient = ibctmtypes.NewClientState( | ||
suite.parentChain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift, | ||
height, commitmenttypes.GetSDKSpecs(), UpgradePath, tmConfig.AllowUpdateAfterExpiry, tmConfig.AllowUpdateAfterMisbehaviour, | ||
) | ||
suite.parentConsState = suite.parentChain.LastHeader.ConsensusState() | ||
|
||
childGenesis := types.NewInitialChildGenesisState(suite.parentClient, suite.parentConsState) | ||
suite.childChain.GetSimApp().ChildKeeper.InitGenesis(suite.childChain.GetContext(), childGenesis) | ||
|
||
suite.ctx = suite.parentChain.GetContext() | ||
|
||
suite.path = ibctesting.NewPath(suite.childChain, suite.parentChain) | ||
suite.path.EndpointA.ChannelConfig.PortID = childtypes.PortID | ||
suite.path.EndpointB.ChannelConfig.PortID = parenttypes.PortID | ||
suite.path.EndpointA.ChannelConfig.Version = types.Version | ||
suite.path.EndpointB.ChannelConfig.Version = types.Version | ||
suite.path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED | ||
suite.path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED | ||
parentClient, ok := suite.childChain.GetSimApp().ChildKeeper.GetParentClient(suite.childChain.GetContext()) | ||
if !ok { | ||
panic("must already have parent client on child chain") | ||
} | ||
// set child endpoint's clientID | ||
suite.path.EndpointA.ClientID = parentClient | ||
} |
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.
why is this a pointer?