Skip to content

Commit

Permalink
simplify parent genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Sep 22, 2021
1 parent 8117883 commit c92c55c
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 490 deletions.
18 changes: 0 additions & 18 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
- [ChildGenesisState](#ibc.applications.ccv.v1.ChildGenesisState)
- [ChildState](#ibc.applications.ccv.v1.ChildState)
- [ParentGenesisState](#ibc.applications.ccv.v1.ParentGenesisState)
- [UnbondingGenesis](#ibc.applications.ccv.v1.UnbondingGenesis)
- [UnbondingSequence](#ibc.applications.ccv.v1.UnbondingSequence)

- [ibc/applications/transfer/v1/transfer.proto](#ibc/applications/transfer/v1/transfer.proto)
Expand Down Expand Up @@ -849,7 +848,6 @@ ChildState defines the state that the parent chain stores for each child chain
| `chain_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |
| `status` | [Status](#ibc.applications.ccv.v1.Status) | | |
| `unbonding_packets` | [UnbondingGenesis](#ibc.applications.ccv.v1.UnbondingGenesis) | repeated | |



Expand All @@ -871,22 +869,6 @@ ParentGenesisState defines the CCV parent chain genesis state



<a name="ibc.applications.ccv.v1.UnbondingGenesis"></a>

### UnbondingGenesis
UnbondingGenesis defines the genesis state for an unbonding packet


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `sequence` | [uint64](#uint64) | | |
| `unbonding_changes` | [tendermint.abci.ValidatorUpdate](#tendermint.abci.ValidatorUpdate) | repeated | |






<a name="ibc.applications.ccv.v1.UnbondingSequence"></a>

### UnbondingSequence
Expand Down
20 changes: 3 additions & 17 deletions modules/apps/ccv/parent/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.ParentGenesisState)
k.SetChainToChannel(ctx, cc.ChainId, cc.ChannelId)
k.SetChannelToChain(ctx, cc.ChannelId, cc.ChainId)
k.SetChannelStatus(ctx, cc.ChannelId, cc.Status)
for _, up := range cc.UnbondingPackets {
packetData := types.NewValidatorSetChangePacketData(up.UnbondingChanges)
k.SetUnbondingPacketData(ctx, cc.ChainId, up.Sequence, packetData)
}
}
}

Expand All @@ -48,22 +44,12 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) types.ParentGenesisState {
for ; iterator.Valid(); iterator.Next() {
channelID := string(iterator.Key())
chainID := string(iterator.Value())
var unbondingPackets []types.UnbondingGenesis

k.IterateUnbondingPacketData(ctx, chainID, func(_ string, seq uint64, packetData types.ValidatorSetChangePacketData) (stop bool) {
unbondingPackets = append(unbondingPackets, types.UnbondingGenesis{
Sequence: seq,
UnbondingChanges: packetData.ValidatorUpdates,
})
return false
})

status := k.GetChannelStatus(ctx, channelID)
cc := types.ChildState{
ChainId: chainID,
ChannelId: channelID,
Status: status,
UnbondingPackets: unbondingPackets,
ChainId: chainID,
ChannelId: channelID,
Status: status,
}
childStates = append(childStates, cc)
}
Expand Down
28 changes: 0 additions & 28 deletions modules/apps/ccv/parent/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,16 @@ package keeper_test
import (
"fmt"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/ibc-go/modules/apps/ccv/types"
abci "github.com/tendermint/tendermint/abci/types"
)

func (suite *KeeperTestSuite) TestGenesis() {
pk1, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
suite.Require().NoError(err)
pk2, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
suite.Require().NoError(err)

pd := types.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{
{
PubKey: pk1,
Power: 30,
},
{
PubKey: pk2,
Power: 20,
},
},
)

// 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))
for i := 3; i < 6; i++ {
suite.parentChain.GetSimApp().ParentKeeper.SetUnbondingPacketData(ctx, fmt.Sprintf("chainid-%d", i), uint64(i), pd)
}
}

genState := suite.parentChain.GetSimApp().ParentKeeper.ExportGenesis(suite.parentChain.GetContext())
Expand All @@ -56,9 +32,5 @@ func (suite *KeeperTestSuite) TestGenesis() {

status := suite.childChain.GetSimApp().ParentKeeper.GetChannelStatus(ctx, channelID)
suite.Require().Equal(int32(i), status, "status is unexpected for given channel id: %s", channelID)

for j := 3; j < 6; j++ {
suite.childChain.GetSimApp().ParentKeeper.GetUnbondingPacketData(ctx, chainID, uint64(j))
}
}
}
56 changes: 0 additions & 56 deletions modules/apps/ccv/parent/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package keeper

import (
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -17,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"
)

Expand Down Expand Up @@ -168,58 +164,6 @@ func (k Keeper) IterateChannelToChain(ctx sdk.Context, cb func(ctx sdk.Context,
}
}

// SetUnbondingPacketData sets the unbonding packet data for a given baby chain and sequence
func (k Keeper) SetUnbondingPacketData(ctx sdk.Context, chainID string, seq uint64, packetData ccv.ValidatorSetChangePacketData) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&packetData)
store.Set(types.UnbondingPacketData(chainID, seq), bz)
}

// GetUnbondingPacketData gets the unbonding packet for a given baby chain and sequence
func (k Keeper) GetUnbondingPacketData(ctx sdk.Context, chainID string, seq uint64) []abci.ValidatorUpdate {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.UnbondingPacketData(chainID, seq))
if bz != nil {
return nil
}
var packetData ccv.ValidatorSetChangePacketData
k.cdc.MustUnmarshal(bz, &packetData)
return packetData.ValidatorUpdates
}

// DeleteUnbondingPacketData deletes the unbonding packet for a given baby chain and sequence
func (k Keeper) DeleteUnbondingPacketData(ctx sdk.Context, chainID string, seq uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.UnbondingPacketData(chainID, seq))
}

// IterateUnbondingPacketData iterates over the unbonding packet data for a given chainID
// and calls the callback provided until the stop boolean returns true.
func (k Keeper) IterateUnbondingPacketData(ctx sdk.Context, chainID string, cb func(chainID string, seq uint64, packetData ccv.ValidatorSetChangePacketData) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(fmt.Sprintf("%s/%s/", types.UnbondingPacketDataPrefix, chainID)))
defer iterator.Close()

if !iterator.Valid() {
return
}

for ; iterator.Valid(); iterator.Next() {
seq, err := strconv.Atoi(string(iterator.Key()))
if err != nil {
panic("sequence is not a number")
}

bz := iterator.Value()
var packetData ccv.ValidatorSetChangePacketData
k.cdc.MustUnmarshal(bz, &packetData)

if cb(chainID, uint64(seq), packetData) {
break
}
}
}

// SetChannelStatus sets the status of a CCV channel with the given status
func (k Keeper) SetChannelStatus(ctx sdk.Context, channelID string, status ccv.Status) {
store := ctx.KVStore(k.storeKey)
Expand Down
2 changes: 0 additions & 2 deletions modules/apps/ccv/parent/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (k Keeper) SendPacket(ctx sdk.Context, chainID string, valUpdates []abci.Va
return err
}

k.SetUnbondingPacketData(ctx, chainID, sequence, packetData)
return nil
}

Expand All @@ -61,7 +60,6 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
return err
}
k.registryKeeper.UnbondValidators(ctx, chainID, data.ValidatorUpdates)
k.DeleteUnbondingPacketData(ctx, chainID, packet.Sequence)
return nil
}

Expand Down
Loading

0 comments on commit c92c55c

Please sign in to comment.