Skip to content

Commit

Permalink
Problem: migrate function does not reset properly validator nonce (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy authored Oct 27, 2022
1 parent 06e9460 commit 2107c6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
24 changes: 10 additions & 14 deletions module/x/gravity/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,21 +691,17 @@ func (k Keeper) MigrateGravityContract(ctx sdk.Context, newBridgeAddress string,
store := ctx.KVStore(k.storeKey)
store.Set([]byte{types.LatestSignerSetTxNonceKey}, sdk.Uint64ToBigEndian(0))

// Reset all ethereum event nonces to zero
// Reset ethereum event nonce to zero
k.setLastObservedEventNonce(ctx, 0)
k.iterateEthereumEventVoteRecords(ctx, func(_ []byte, voteRecord *types.EthereumEventVoteRecord) bool {
for _, vote := range voteRecord.Votes {
val, err := sdk.ValAddressFromBech32(vote)

if err != nil {
panic(err)
}

k.setLastEventNonceByValidator(ctx, val, 0)
// Reset all validators ethereum event nonce to zero
delegateKeys := k.getDelegateKeys(ctx)
for _, delegateKey := range delegateKeys {
validator, err := sdk.ValAddressFromBech32(delegateKey.ValidatorAddress)
if err != nil {
panic(err)
}

return false
})
k.setLastEventNonceByValidator(ctx, validator, 0)
}

// Delete all Ethereum Events
prefixStoreEthereumEvent := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{types.EthereumEventVoteRecordKey})
Expand All @@ -719,7 +715,7 @@ func (k Keeper) MigrateGravityContract(ctx sdk.Context, newBridgeAddress string,
prefixStoreEthereumEvent.Delete(key)
}

// Set the Last oberved Ethereum Blockheight to zero
// Set the Last observed Ethereum Blockheight to zero
height := types.LatestEthereumBlockHeight{
EthereumHeight: (bridgeDeploymentHeight - 1),
CosmosHeight: uint64(ctx.BlockHeight()),
Expand Down
9 changes: 4 additions & 5 deletions module/x/gravity/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,8 @@ func TestKeeper_GetEthereumSignatures(t *testing.T) {
}

func TestKeeper_Migration(t *testing.T) {

input := CreateTestEnv(t)
input, ctx := SetupFiveValChain(t)
gk := input.GravityKeeper
ctx := input.Context

stce := &types.SendToCosmosEvent{
EventNonce: 1,
Expand Down Expand Up @@ -545,9 +543,9 @@ func TestKeeper_Migration(t *testing.T) {
evr2 := &types.EthereumEventVoteRecord{
Event: cctxea,
Votes: []string{
ValAddrs[1].String(),
ValAddrs[2].String(),
ValAddrs[3].String(),
ValAddrs[4].String(),
},
}

Expand All @@ -568,7 +566,7 @@ func TestKeeper_Migration(t *testing.T) {
Votes: []string{
ValAddrs[0].String(),
ValAddrs[1].String(),
ValAddrs[2].String(),
ValAddrs[3].String(),
},
Accepted: false,
}
Expand Down Expand Up @@ -675,6 +673,7 @@ func TestKeeper_Migration(t *testing.T) {
nonce2 := gk.GetLastObservedEventNonce(ctx)
require.Equal(t, uint64(0), nonce2)

// make sure all event nonce are reset. Even val5 which did not participate any votes
for _, val := range ValAddrs {
require.Equal(t, uint64(0), gk.getLastEventNonceByValidator(ctx, val))
}
Expand Down

0 comments on commit 2107c6b

Please sign in to comment.