Skip to content

Commit

Permalink
Add logic to remove a small number of tx hashes each block (#2048)
Browse files Browse the repository at this point in the history
* Add logic to remove a small number of tx hashes each block

* rebase
  • Loading branch information
codchen authored Feb 20, 2025
1 parent 1232335 commit f875297
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions x/evm/keeper/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/evm/types"
)

const DefaultTxHashesToRemove = 100

func (k *Keeper) RemoveFirstNTxHashes(ctx sdk.Context, n int) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), types.TxHashesPrefix)
iter := store.Iterator(nil, nil)
defer iter.Close()
keysToDelete := make([][]byte, 0, n)
for ; n > 0 && iter.Valid(); iter.Next() {
keysToDelete = append(keysToDelete, iter.Key())
n--
}
for _, k := range keysToDelete {
store.Delete(k)
}
}
42 changes: 42 additions & 0 deletions x/evm/keeper/tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/require"
)

func TestRemoveFirstNTxHashes(t *testing.T) {
k := &testkeeper.EVMTestApp.EvmKeeper
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{})

for i := byte(1); i <= 101; i++ {
setTxHash(ctx, k, i, 102-i)
}

require.Equal(t, 101, getTxHashCount(ctx, k))
k.RemoveFirstNTxHashes(ctx, keeper.DefaultTxHashesToRemove)
require.Equal(t, 1, getTxHashCount(ctx, k))
k.RemoveFirstNTxHashes(ctx, keeper.DefaultTxHashesToRemove)
require.Equal(t, 0, getTxHashCount(ctx, k))
}

func setTxHash(ctx sdk.Context, k *keeper.Keeper, key byte, value byte) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), types.TxHashesPrefix)
store.Set([]byte{key}, []byte{value})
}

func getTxHashCount(ctx sdk.Context, k *keeper.Keeper) (cnt int) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), types.TxHashesPrefix)
iter := store.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
cnt++
}
return
}
3 changes: 3 additions & 0 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
// EndBlock executes all ABCI EndBlock logic respective to the evm module. It
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
// TODO: remove after all TxHashes have been removed
am.keeper.RemoveFirstNTxHashes(ctx, keeper.DefaultTxHashesToRemove)

newBaseFee := am.keeper.AdjustDynamicBaseFeePerGas(ctx, uint64(req.BlockGasUsed))
if newBaseFee != nil {
metrics.GaugeEvmBlockBaseFee(newBaseFee.TruncateInt().BigInt(), req.Height)
Expand Down

0 comments on commit f875297

Please sign in to comment.