Skip to content
Merged
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
28 changes: 8 additions & 20 deletions sei-db/common/evm/keys.go → sei-db/common/keys/evm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package evm
package keys

import (
"bytes"
"errors"
)

const (
Expand All @@ -13,10 +12,10 @@ const (
// EVMStoreKey is the cosmos store/module name for EVM state.
const EVMStoreKey = "evm"

// EVMFlatKVStoreKey is the module name used when exporting/importing FlatKV
// EVM data as a separate module in state-sync snapshots. Both the SC and SS
// layers need to recognise this name and treat it as EVM data.
const EVMFlatKVStoreKey = "evm_flatkv"
// FlatKVStoreKey is the module name used when exporting/importing data from
// the FlatKV backend. Treated as a separate module in state-sync snapshots
// so that import routes data exclusively to FlatKV.
const FlatKVStoreKey = "flatkv"

// EVM key prefixes — mirrored from x/evm/types/keys.go.
// These are immutable on-disk format markers; changing them would break
Expand All @@ -26,19 +25,13 @@ var (
stateKeyPrefix = []byte{0x03}
codeKeyPrefix = []byte{0x07}
codeHashKeyPrefix = []byte{0x08}
codeSizeKeyPrefix = []byte{0x09}
nonceKeyPrefix = []byte{0x0a}
)

// StateKeyPrefix returns the storage state key prefix (0x03).
// Exported for callers that need the raw prefix (e.g. iterator bounds).
func StateKeyPrefix() []byte { return stateKeyPrefix }

var (
// ErrMalformedEVMKey indicates invalid EVM key encoding.
ErrMalformedEVMKey = errors.New("sei-db: malformed evm key")
)

// EVMKeyKind identifies an EVM key family.
type EVMKeyKind uint8

Expand All @@ -48,14 +41,9 @@ const (
EVMKeyCodeHash // Stripped key: 20-byte address
EVMKeyCode // Stripped key: 20-byte address
EVMKeyStorage // Stripped key: addr||slot (20+32 bytes)
// TODO: rename this to MiscKey, it's a catch-all that gets potentially non-evm data
EVMKeyLegacy // Full original key preserved (address mappings, codesize, etc.)
EVMKeyLegacy // Full original key preserved (address mappings, codesize, etc.)
)

// EVMKeyUnknown is an alias for EVMKeyEmpty, used by FlatKV to test for
// unrecognised/empty keys.
const EVMKeyUnknown = EVMKeyEmpty

// ParseEVMKey parses an EVM key from the x/evm store keyspace.
//
// For optimized keys (nonce, code, codehash, storage), keyBytes is the stripped key.
Expand Down Expand Up @@ -113,13 +101,13 @@ func EVMKeyPrefixByte(kind EVMKeyKind) (byte, bool) {
}
}

// BuildMemIAVLEVMKey builds a memiavl key from internal bytes.
// BuildEVMKey builds a memiavl key from internal bytes.
// This is the reverse of ParseEVMKey for optimized key types.
//
// NOTE: This is primarily used for tests and temporary compatibility.
// FlatKV stores data in internal format; this function converts back to
// memiavl format for Iterator/Exporter output.
func BuildMemIAVLEVMKey(kind EVMKeyKind, keyBytes []byte) []byte {
func BuildEVMKey(kind EVMKeyKind, keyBytes []byte) []byte {
prefix, ok := EVMKeyPrefixByte(kind)
if !ok {
return nil
Expand Down
23 changes: 2 additions & 21 deletions sei-db/common/evm/keys_test.go → sei-db/common/keys/evm_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evm
package keys

import (
"testing"
Expand Down Expand Up @@ -52,12 +52,6 @@ func TestParseEVMKey(t *testing.T) {
wantKind: EVMKeyCodeHash,
wantBytes: addr,
},
{
name: "CodeSize goes to Legacy",
key: concat(codeSizeKeyPrefix, addr),
wantKind: EVMKeyLegacy,
wantBytes: concat(codeSizeKeyPrefix, addr), // Full key preserved
},
{
name: "Code",
key: concat(codeKeyPrefix, addr),
Expand Down Expand Up @@ -183,17 +177,11 @@ func TestBuildMemIAVLEVMKey(t *testing.T) {
keyBytes: concat(addr, slot),
want: concat(stateKeyPrefix, concat(addr, slot)),
},
{
name: "Unknown",
kind: EVMKeyUnknown,
keyBytes: addr,
want: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := BuildMemIAVLEVMKey(tc.kind, tc.keyBytes)
got := BuildEVMKey(tc.kind, tc.keyBytes)
require.Equal(t, tc.want, got)
})
}
Expand All @@ -204,11 +192,4 @@ func TestInternalKeyLen(t *testing.T) {
require.Equal(t, addressLen, InternalKeyLen(EVMKeyNonce))
require.Equal(t, addressLen, InternalKeyLen(EVMKeyCodeHash))
require.Equal(t, addressLen, InternalKeyLen(EVMKeyCode))
require.Equal(t, 0, InternalKeyLen(EVMKeyUnknown))
}

func TestEVMKeyUnknownAlias(t *testing.T) {
// Verify EVMKeyUnknown == EVMKeyEmpty so FlatKV's "skip unknown" checks
// still work correctly after introducing EVMKeyLegacy.
require.Equal(t, EVMKeyEmpty, EVMKeyUnknown)
}
6 changes: 3 additions & 3 deletions sei-db/config/sc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"fmt"

"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv"
"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/config"
"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/memiavl"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ type StateCommitConfig struct {
MemIAVLConfig memiavl.Config

// FlatKVConfig is the configuration for the FlatKV (EVM) backend
FlatKVConfig flatkv.Config
FlatKVConfig config.Config

// Max concurrent historical proof queries (RPC /store path).
HistoricalProofMaxInFlight int `mapstructure:"historical-proof-max-inflight"`
Expand All @@ -66,7 +66,7 @@ func DefaultStateCommitConfig() StateCommitConfig {
ReadMode: CosmosOnlyRead,
EnableLatticeHash: false,
MemIAVLConfig: memiavl.DefaultConfig(),
FlatKVConfig: *flatkv.DefaultConfig(),
FlatKVConfig: *config.DefaultConfig(),
HistoricalProofMaxInFlight: DefaultSCHistoricalProofMaxInFlight,
HistoricalProofRateLimit: DefaultSCHistoricalProofRateLimit,
HistoricalProofBurst: DefaultSCHistoricalProofBurst,
Expand Down
6 changes: 3 additions & 3 deletions sei-db/state_db/bench/cryptosim/cryptosim_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/sei-protocol/sei-chain/sei-db/config"
"github.com/sei-protocol/sei-chain/sei-db/state_db/bench/wrappers"
"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv"
flatkvConfig "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/config"
)

const (
Expand Down Expand Up @@ -163,7 +163,7 @@ type CryptoSimConfig struct {
DeleteLogDirOnShutdown bool

// Configures the FlatKV database. Ignored if Backend is not "FlatKV".
FlatKVConfig *flatkv.Config
FlatKVConfig *flatkvConfig.Config

// The capacity of the channel that holds blocks awaiting execution.
BlockChannelCapacity int
Expand Down Expand Up @@ -287,7 +287,7 @@ func DefaultCryptoSimConfig() *CryptoSimConfig {
DeleteLogDirOnStartup: false,
DeleteDataDirOnShutdown: false,
DeleteLogDirOnShutdown: false,
FlatKVConfig: flatkv.DefaultConfig(),
FlatKVConfig: flatkvConfig.DefaultConfig(),
BlockChannelCapacity: 8,
GenerateReceipts: false,
RecieptChannelCapacity: 32,
Expand Down
20 changes: 10 additions & 10 deletions sei-db/state_db/bench/cryptosim/data_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"
"fmt"

"github.com/sei-protocol/sei-chain/sei-db/common/evm"
"github.com/sei-protocol/sei-chain/sei-db/common/keys"
)

const (
Expand All @@ -18,7 +18,7 @@ const (
// Use the code hash as a proxy. There is currently no mechanism to force FlatKV to update the account balance
// field, and code hash keys will cause the account DB to get updated, which is the important part for this
// simulation.
accountKeyPrefix = evm.EVMKeyCodeHash
accountKeyPrefix = keys.EVMKeyCodeHash
)

// Generates random data for the benchmark. This is not a thread safe utility.
Expand Down Expand Up @@ -107,7 +107,7 @@ func NewDataGenerator(

fmt.Printf("Next block number: %s.\n", int64Commas(int64(nextBlockNumber))) //nolint:gosec

feeCollectionAddress := evm.BuildMemIAVLEVMKey(
feeCollectionAddress := keys.BuildEVMKey(
accountKeyPrefix,
rand.Address(accountPrefix, 0, AddressLen),
)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (d *DataGenerator) CreateNewAccount(
d.nextAccountID++

addr := d.rand.Address(accountPrefix, accountID, AddressLen)
address = evm.BuildMemIAVLEVMKey(accountKeyPrefix, addr)
address = keys.BuildEVMKey(accountKeyPrefix, addr)

isCold = d.rand.Float64() >= d.config.NewAccountDormancyProbability

Expand Down Expand Up @@ -211,7 +211,7 @@ func (d *DataGenerator) CreateNewErc20Contract(
d.nextErc20ContractID++

erc20Address := d.rand.Address(contractPrefix, erc20ContractID, AddressLen)
address = evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, erc20Address)
address = keys.BuildEVMKey(keys.EVMKeyCode, erc20Address)

if !write {
return erc20ContractID, address, nil
Expand Down Expand Up @@ -240,7 +240,7 @@ func (d *DataGenerator) RandomAccount() (id int64, address []byte, isNew bool, e
lastHotAccountID := d.config.NumberOfHotAccounts
accountID := d.rand.Int64Range(int64(firstHotAccountID), int64(lastHotAccountID+1))
addr := d.rand.Address(accountPrefix, accountID, AddressLen)
return accountID, evm.BuildMemIAVLEVMKey(accountKeyPrefix, addr), false, nil
return accountID, keys.BuildEVMKey(accountKeyPrefix, addr), false, nil
} else {

new := d.rand.Float64() < d.config.NewAccountProbability
Expand All @@ -260,7 +260,7 @@ func (d *DataGenerator) RandomAccount() (id int64, address []byte, isNew bool, e

accountID := d.rand.Int64Range(firstLegalColdAccountID, lastLegalColdAccountID)
addr := d.rand.Address(accountPrefix, accountID, AddressLen)
return accountID, evm.BuildMemIAVLEVMKey(accountKeyPrefix, addr), false, nil
return accountID, keys.BuildEVMKey(accountKeyPrefix, addr), false, nil
}
}

Expand All @@ -271,7 +271,7 @@ func (d *DataGenerator) randomAccountSlot(accountID int64) ([]byte, error) {
slotID := accountID*int64(d.config.Erc20InteractionsPerAccount) + slotNumber

storageKeyBytes := d.rand.Address(ethStoragePrefix, slotID, StorageKeyLen)
return evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, storageKeyBytes), nil
return keys.BuildEVMKey(keys.EVMKeyStorage, storageKeyBytes), nil
}

// Selects a random ERC20 contract for a transaction.
Expand All @@ -289,7 +289,7 @@ func (d *DataGenerator) randomErc20Contract() ([]byte, error) {
}
erc20ContractID := d.rand.Int64Range(0, hotMax)
addr := d.rand.Address(contractPrefix, erc20ContractID, AddressLen)
return evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, addr), nil
return keys.BuildEVMKey(keys.EVMKeyCode, addr), nil
}

// Otherwise, select a cold ERC20 contract at random.
Expand All @@ -301,7 +301,7 @@ func (d *DataGenerator) randomErc20Contract() ([]byte, error) {
int64(d.config.HotErc20ContractSetSize),
d.nextErc20ContractID)
addr := d.rand.Address(contractPrefix, erc20ContractID, AddressLen)
return evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, addr), nil
return keys.BuildEVMKey(keys.EVMKeyCode, addr), nil
}

// Close the data generator and release any resources.
Expand Down
50 changes: 25 additions & 25 deletions sei-db/state_db/bench/cryptosim/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import (
"testing"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/sei-protocol/sei-chain/sei-db/common/evm"
"github.com/sei-protocol/sei-chain/sei-db/common/keys"
)

func makeTestKeys(t *testing.T) (feeAccount, srcAccount, dstAccount, senderSlot, receiverSlot, erc20Contract []byte) {
t.Helper()
keyRand := NewCannedRandom(4096, 1)

feeAccount = evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
feeAccount = keys.BuildEVMKey(keys.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
srcAddr := keyRand.Address(accountPrefix, 1, AddressLen)
srcAccount = evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, srcAddr)
srcAccount = keys.BuildEVMKey(keys.EVMKeyCodeHash, srcAddr)
dstAddr := keyRand.Address(accountPrefix, 2, AddressLen)
dstAccount = evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, dstAddr)
dstAccount = keys.BuildEVMKey(keys.EVMKeyCodeHash, dstAddr)

senderSlotBytes := make([]byte, StorageKeyLen)
copy(senderSlotBytes[:AddressLen], srcAddr)
copy(senderSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 11))
senderSlot = evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, senderSlotBytes)
senderSlot = keys.BuildEVMKey(keys.EVMKeyStorage, senderSlotBytes)

receiverSlotBytes := make([]byte, StorageKeyLen)
copy(receiverSlotBytes[:AddressLen], dstAddr)
copy(receiverSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 12))
receiverSlot = evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, receiverSlotBytes)
receiverSlot = keys.BuildEVMKey(keys.EVMKeyStorage, receiverSlotBytes)

erc20Contract = evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))
erc20Contract = keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))
return
}

Expand Down Expand Up @@ -128,22 +128,22 @@ func TestBuildERC20TransferReceipt_EVMKeyCodeAccounts(t *testing.T) {
crand := NewCannedRandom(1<<20, 42)
keyRand := NewCannedRandom(4096, 1)

feeAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(accountPrefix, 0, AddressLen))
feeAccount := keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(accountPrefix, 0, AddressLen))
srcAddr := keyRand.Address(accountPrefix, 1, AddressLen)
srcAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, srcAddr)
dstAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(accountPrefix, 2, AddressLen))
srcAccount := keys.BuildEVMKey(keys.EVMKeyCode, srcAddr)
dstAccount := keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(accountPrefix, 2, AddressLen))

senderSlotBytes := make([]byte, StorageKeyLen)
copy(senderSlotBytes[:AddressLen], srcAddr)
copy(senderSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 11))
senderSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, senderSlotBytes)
senderSlot := keys.BuildEVMKey(keys.EVMKeyStorage, senderSlotBytes)

receiverSlotBytes := make([]byte, StorageKeyLen)
copy(receiverSlotBytes[:AddressLen], keyRand.Address(accountPrefix, 2, AddressLen))
copy(receiverSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 12))
receiverSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, receiverSlotBytes)
receiverSlot := keys.BuildEVMKey(keys.EVMKeyStorage, receiverSlotBytes)

erc20Contract := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))
erc20Contract := keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))

_, err := BuildERC20TransferReceipt(crand, feeAccount, srcAccount, dstAccount, senderSlot, receiverSlot, erc20Contract, 1_000_000, 0)
if err != nil {
Expand All @@ -157,13 +157,13 @@ func TestBuildERC20TransferReceipt_DataGeneratorKeyFormats(t *testing.T) {
crand := NewCannedRandom(1<<20, 42)
keyRand := NewCannedRandom(4096, 1)

feeAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
srcAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, keyRand.Address(accountPrefix, 1, AddressLen))
dstAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, keyRand.Address(accountPrefix, 2, AddressLen))
feeAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
srcAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, keyRand.Address(accountPrefix, 1, AddressLen))
dstAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, keyRand.Address(accountPrefix, 2, AddressLen))

senderSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, keyRand.Address(ethStoragePrefix, 10, StorageKeyLen))
receiverSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, keyRand.Address(ethStoragePrefix, 20, StorageKeyLen))
erc20Contract := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))
senderSlot := keys.BuildEVMKey(keys.EVMKeyStorage, keyRand.Address(ethStoragePrefix, 10, StorageKeyLen))
receiverSlot := keys.BuildEVMKey(keys.EVMKeyStorage, keyRand.Address(ethStoragePrefix, 20, StorageKeyLen))
erc20Contract := keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))

receipt, err := BuildERC20TransferReceipt(crand, feeAccount, srcAccount, dstAccount, senderSlot, receiverSlot, erc20Contract, 1_000_000, 0)
if err != nil {
Expand All @@ -178,23 +178,23 @@ func BenchmarkBuildERC20TransferReceipt(b *testing.B) {
keyRand := NewCannedRandom(4096, 1)
receiptRand := NewCannedRandom(1<<20, 2)

feeAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
feeAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, keyRand.Address(accountPrefix, 0, AddressLen))
srcAddr := keyRand.Address(accountPrefix, 1, AddressLen)
srcAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, srcAddr)
srcAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, srcAddr)
dstAddr := keyRand.Address(accountPrefix, 2, AddressLen)
dstAccount := evm.BuildMemIAVLEVMKey(evm.EVMKeyCodeHash, dstAddr)
dstAccount := keys.BuildEVMKey(keys.EVMKeyCodeHash, dstAddr)

senderSlotBytes := make([]byte, StorageKeyLen)
copy(senderSlotBytes[:AddressLen], srcAddr)
copy(senderSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 11))
senderSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, senderSlotBytes)
senderSlot := keys.BuildEVMKey(keys.EVMKeyStorage, senderSlotBytes)

receiverSlotBytes := make([]byte, StorageKeyLen)
copy(receiverSlotBytes[:AddressLen], dstAddr)
copy(receiverSlotBytes[AddressLen:], keyRand.SeededBytes(SlotLen, 12))
receiverSlot := evm.BuildMemIAVLEVMKey(evm.EVMKeyStorage, receiverSlotBytes)
receiverSlot := keys.BuildEVMKey(keys.EVMKeyStorage, receiverSlotBytes)

erc20Contract := evm.BuildMemIAVLEVMKey(evm.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))
erc20Contract := keys.BuildEVMKey(keys.EVMKeyCode, keyRand.Address(contractPrefix, 0, AddressLen))

b.ReportAllocs()
b.ResetTimer()
Expand Down
Loading
Loading