From c731d2d84678fbc85cb6fdba1238c1ae741e5920 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Thu, 27 Apr 2023 12:01:52 +0900 Subject: [PATCH] delete unecessary ethereum event attestation signatures --- module/x/gravity/abci.go | 3 ++- module/x/gravity/handler_test.go | 16 +++++++++---- module/x/gravity/keeper/batch.go | 14 ++++++----- module/x/gravity/keeper/contract_call.go | 2 ++ module/x/gravity/keeper/keeper.go | 30 ++++++++++++------------ 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/module/x/gravity/abci.go b/module/x/gravity/abci.go index cd1654523..91a326f25 100644 --- a/module/x/gravity/abci.go +++ b/module/x/gravity/abci.go @@ -156,7 +156,7 @@ func eventVoteRecordPruneAndTally(ctx sdk.Context, k keeper.Keeper) { // This order is not important. for _, att := range attmap[nonce] { // delete all before the last nonce - if nonce < lastNonce { + if nonce <= lastNonce { k.DeleteEthereumEventVoteRecord(ctx, att) } // We check if the event nonce is exactly 1 higher than the last attestation that was @@ -318,6 +318,7 @@ func cleanupTimedOutContractCallTxs(ctx sdk.Context, k keeper.Keeper) { k.IterateOutgoingTxsByType(ctx, types.ContractCallTxPrefixByte, func(_ []byte, otx types.OutgoingTx) bool { cctx, _ := otx.(*types.ContractCallTx) if cctx.Timeout < ethereumHeight { + k.DeleteEthereumSignatures(ctx, cctx) k.DeleteOutgoingTx(ctx, cctx.GetStoreIndex()) } return true diff --git a/module/x/gravity/handler_test.go b/module/x/gravity/handler_test.go index 5d6667a89..85463a183 100644 --- a/module/x/gravity/handler_test.go +++ b/module/x/gravity/handler_test.go @@ -274,18 +274,24 @@ func TestMsgSubmitEthreumEventSendToCosmosMultiValidator(t *testing.T) { balance2 := input.BankKeeper.GetAllBalances(ctx, myCosmosAddr) require.Equal(t, sdk.Coins{sdk.NewInt64Coin(denom, 12)}, balance2) + // and vouchers added to the account + // this happens when 2/3 of the voting power have submitted claims, so the third isn't required + balance3 := input.BankKeeper.GetAllBalances(ctx, myCosmosAddr) + require.Equal(t, sdk.Coins{sdk.NewInt64Coin(denom, 12)}, balance3) + // when ctx = ctx.WithBlockTime(myBlockTime) _, err = h(ctx, ethClaim3Msg) gravity.EndBlocker(ctx, input.GravityKeeper) require.NoError(t, err) - // and attestation persisted + // and attestations pruned + a1 = input.GravityKeeper.GetEthereumEventVoteRecord(ctx, myNonce, ethClaim1.Hash()) + a2 = input.GravityKeeper.GetEthereumEventVoteRecord(ctx, myNonce, ethClaim2.Hash()) a3 := input.GravityKeeper.GetEthereumEventVoteRecord(ctx, myNonce, ethClaim3.Hash()) - require.NotNil(t, a3) - // and no additional added to the account - balance3 := input.BankKeeper.GetAllBalances(ctx, myCosmosAddr) - require.Equal(t, sdk.Coins{sdk.NewInt64Coin(denom, 12)}, balance3) + require.Nil(t, a1) + require.Nil(t, a2) + require.Nil(t, a3) } func TestMsgSetDelegateAddresses(t *testing.T) { diff --git a/module/x/gravity/keeper/batch.go b/module/x/gravity/keeper/batch.go index 0152ac18b..816e72fbd 100644 --- a/module/x/gravity/keeper/batch.go +++ b/module/x/gravity/keeper/batch.go @@ -13,12 +13,12 @@ import ( ) // BuildBatchTx starts the following process chain: -// - find bridged denominator for given voucher type -// - determine if a an unexecuted batch is already waiting for this token type, if so confirm the new batch would -// have a higher total fees. If not exit withtout creating a batch -// - select available transactions from the outgoing transaction pool sorted by fee desc -// - persist an outgoing batch object with an incrementing ID = nonce -// - emit an event +// - find bridged denominator for given voucher type +// - determine if a an unexecuted batch is already waiting for this token type, if so confirm the new batch would +// have a higher total fees. If not exit withtout creating a batch +// - select available transactions from the outgoing transaction pool sorted by fee desc +// - persist an outgoing batch object with an incrementing ID = nonce +// - emit an event func (k Keeper) BuildBatchTx(ctx sdk.Context, contractAddress common.Address, maxElements int) *types.BatchTx { if maxElements == 0 { return nil @@ -102,6 +102,7 @@ func (k Keeper) batchTxExecuted(ctx sdk.Context, tokenContract common.Address, n } } + k.DeleteEthereumSignatures(ctx, batchTx) k.DeleteOutgoingTx(ctx, batchTx.GetStoreIndex()) return nil } @@ -145,6 +146,7 @@ func (k Keeper) CancelBatchTx(ctx sdk.Context, batch *types.BatchTx) { } // Delete batch since it is finished + k.DeleteEthereumSignatures(ctx, batch) k.DeleteOutgoingTx(ctx, batch.GetStoreIndex()) ctx.EventManager().EmitEvent( diff --git a/module/x/gravity/keeper/contract_call.go b/module/x/gravity/keeper/contract_call.go index 1b8602572..cb725fc16 100644 --- a/module/x/gravity/keeper/contract_call.go +++ b/module/x/gravity/keeper/contract_call.go @@ -23,10 +23,12 @@ func (k Keeper) contractCallExecuted(ctx sdk.Context, invalidationScope []byte, cctx, _ := otx.(*types.ContractCallTx) if (cctx.InvalidationNonce < completedCallTx.InvalidationNonce) && bytes.Equal(cctx.InvalidationScope, completedCallTx.InvalidationScope) { + k.DeleteEthereumSignatures(ctx, cctx) k.DeleteOutgoingTx(ctx, cctx.GetStoreIndex()) } return false }) + k.DeleteEthereumSignatures(ctx, completedCallTx) k.DeleteOutgoingTx(ctx, completedCallTx.GetStoreIndex()) } diff --git a/module/x/gravity/keeper/keeper.go b/module/x/gravity/keeper/keeper.go index 791d1d1ac..62b55973c 100644 --- a/module/x/gravity/keeper/keeper.go +++ b/module/x/gravity/keeper/keeper.go @@ -130,19 +130,19 @@ func (k Keeper) GetLastUnbondingBlockHeight(ctx sdk.Context) uint64 { // ETHEREUM SIGNATURES // /////////////////////////////// -// getEthereumSignature returns a valset confirmation by a nonce and validator address +// getEthereumSignature returns an ethereum signature by a nonce and validator address func (k Keeper) getEthereumSignature(ctx sdk.Context, storeIndex []byte, validator sdk.ValAddress) []byte { return ctx.KVStore(k.storeKey).Get(types.MakeEthereumSignatureKey(storeIndex, validator)) } -// SetEthereumSignature sets a valset confirmation +// SetEthereumSignature sets an ethereum signature func (k Keeper) SetEthereumSignature(ctx sdk.Context, sig types.EthereumTxConfirmation, val sdk.ValAddress) []byte { key := types.MakeEthereumSignatureKey(sig.GetStoreIndex(), val) ctx.KVStore(k.storeKey).Set(key, sig.GetSignature()) return key } -// GetEthereumSignatures returns all etherum signatures for a given outgoing tx by store index +// GetEthereumSignatures returns all ethereum signatures for a given outgoing tx by store index func (k Keeper) GetEthereumSignatures(ctx sdk.Context, storeIndex []byte) map[string][]byte { var signatures = make(map[string][]byte) k.iterateEthereumSignatures(ctx, storeIndex, func(val sdk.ValAddress, h []byte) bool { @@ -152,7 +152,18 @@ func (k Keeper) GetEthereumSignatures(ctx sdk.Context, storeIndex []byte) map[st return signatures } -// iterateEthereumSignatures iterates through all valset confirms by nonce in ASC order +// DeleteEthereumSignatures deletes the ethereum signatures for a specific outgoing tx +func (k Keeper) DeleteEthereumSignatures(ctx sdk.Context, otx types.OutgoingTx) { + prefixStoreSig := prefix.NewStore(ctx.KVStore(k.storeKey), append([]byte{types.EthereumSignatureKey}, otx.GetStoreIndex()...)) + iterSig := prefixStoreSig.Iterator(nil, nil) + defer iterSig.Close() + + for ; iterSig.Valid(); iterSig.Next() { + prefixStoreSig.Delete(iterSig.Key()) + } +} + +// iterateEthereumSignatures iterates through all ethereum signatures confirms by nonce in ASC order func (k Keeper) iterateEthereumSignatures(ctx sdk.Context, storeIndex []byte, cb func(sdk.ValAddress, []byte) bool) { prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), append([]byte{types.EthereumSignatureKey}, storeIndex...)) iter := prefixStore.Iterator(nil, nil) @@ -643,17 +654,6 @@ func (k Keeper) IterateEthereumHeightVotes(ctx sdk.Context, cb func(val sdk.ValA } } -// DeleteEthereumSignatures deletes the ethereum signatures for a specific outgoing tx -func (k Keeper) DeleteEthereumSignatures(ctx sdk.Context, otx types.OutgoingTx) { - prefixStoreSig := prefix.NewStore(ctx.KVStore(k.storeKey), append([]byte{types.EthereumSignatureKey}, otx.GetStoreIndex()...)) - iterSig := prefixStoreSig.Iterator(nil, nil) - defer iterSig.Close() - - for ; iterSig.Valid(); iterSig.Next() { - prefixStoreSig.Delete(iterSig.Key()) - } -} - ///////////////// // MIGRATE // /////////////////