-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to remove a small number of tx hashes each block (#2048)
* Add logic to remove a small number of tx hashes each block * rebase
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters