Skip to content
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

Add logic to remove a small number of tx hashes each block #2048

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 10

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 <= 11; i++ {
setTxHash(ctx, k, i, 12-i)
}

require.Equal(t, 11, 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 @@ -298,6 +298,9 @@
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we do this every 10 blocks? just so we spread it out and it doesn't look like block times are just degrading?

am.keeper.RemoveFirstNTxHashes(ctx, keeper.DefaultTxHashesToRemove)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call

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