Skip to content

Commit 0b93e66

Browse files
fix(seidb): keep the memIAVL nonce when state sync restores a mid-mig… (#4463)
## Summary State sync from a snapshot taken during the last phase of the EVM migration writes nonce 0 over the real nonce of contracts in the state store (SS). This PR stops the SS import from writing a zero nonce from a FlatKV account row. Only the SS import path changes. State commitment, the snapshot format, and the AppHash are unchanged, so the fix is safe for a v6.7.x patch. The window: the migration moves keys in key order, so all 0x08 (code hash) keys move before any 0x0a (nonce) key. FlatKV stores the nonce and the code hash in one account row. Between the two prefixes, each contract has a FlatKV row with a correct code hash and nonce 0, while its real nonce is still in memIAVL. The snapshot puts the memIAVL sections first and the FlatKV section after them. So on import the SS gets the real nonce first, and then a zero nonce from the FlatKV row that overwrites it. The wrong value stays until a transaction writes that nonce again. Non-pending `eth_getTransactionCount`, `eth_call` and `eth_estimateGas` for contract-creating calls, and `debug_trace*` read it. - `sei-db/state_db/ss/composite/store.go`: `convertFlatKVNodes` now omits a zero nonce from a merged account row, the same way it already omits a zero code hash. A non-zero FlatKV nonce is never stale: `shouldForwardWriteToNewDB` sends each write to the database that holds the key, and a migrated key is deleted from memIAVL, so the two backends never both hold a valid nonce for one address. An absent nonce reads as 0 in `Keeper.GetNonce`, `DBImpl.Exist`, and `DBImpl.Empty`. - After state sync, an account with nonce 0 has no nonce key in the SS, but a node that replays blocks can hold 8 zero bytes. Point reads return the same value. A raw scan such as `IterateAllNonces` skips those accounts. - Backport to `release/v6.7`: on that branch `CompositeCommitStore.Commit` takes no argument, so the two `cs.Commit(cs.Version() + 1)` calls in `import_migration_test.go` become `cs.Commit()`. ## Test plan - `sei-db/state_db/ss/composite/store_test.go` - `TestImport_FlatKVZeroNonceKeepsMemIAVLNonce`: the stream holds a memIAVL nonce (5) and then a FlatKV account row with nonce 0 and a code hash. The SS keeps nonce 5 and stores the code hash, with `EVMSplit` true and false. - `TestImport_FlatKVZeroNonceIsAbsent`: a FlatKV account row with nonce 0 alone leaves no nonce key in the SS. - `sei-db/state_db/ss/composite/import_migration_test.go` - `TestImport_MidMigrationSnapshotRestoresNonce`: builds a real mid-migration commit store (MemiavlOnly commit, then `MigrateEVM` with batch size 1) and exports a snapshot. It asserts that the stream holds both the memIAVL nonce and the FlatKV account row, then imports it into the cosmos, evm-split, and evm-separate-sub-dbs SS layouts and checks the nonce and the code hash. - Without the `nonce != 0` guard, all three tests fail. With it, they pass. - `scripts/ramtest.sh ./sei-db/state_db/ss/... ./sei-cosmos/storev2/... -count=1` passes, and `-race` on `ss/composite` `TestImport*` passes. - `make fmtcheck` and `make dblint` are clean. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent dced959 commit 0b93e66

3 files changed

Lines changed: 219 additions & 4 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package composite
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"errors"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
errorutils "github.com/sei-protocol/sei-chain/sei-db/common/errors"
12+
"github.com/sei-protocol/sei-chain/sei-db/common/keys"
13+
"github.com/sei-protocol/sei-chain/sei-db/config"
14+
"github.com/sei-protocol/sei-chain/sei-db/db_engine/types"
15+
"github.com/sei-protocol/sei-chain/sei-db/proto"
16+
sccomposite "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/composite"
17+
"github.com/sei-protocol/sei-chain/sei-db/state_db/sc/flatkv/ktype"
18+
sctypes "github.com/sei-protocol/sei-chain/sei-db/state_db/sc/types"
19+
)
20+
21+
// TestImport_MidMigrationSnapshotRestoresNonce restores a snapshot taken while
22+
// the EVM migration boundary sits between an address's code hash key and its
23+
// nonce key, and checks that the state store serves the committed nonce.
24+
func TestImport_MidMigrationSnapshotRestoresNonce(t *testing.T) {
25+
addr := make([]byte, 20)
26+
for i := range addr {
27+
addr[i] = 0xAB
28+
}
29+
codeHashKey := keys.BuildEVMKey(keys.EVMKeyCodeHash, addr)
30+
nonceKey := keys.BuildEVMKey(keys.EVMKeyNonce, addr)
31+
codeHash := make([]byte, 32)
32+
for i := range codeHash {
33+
codeHash[i] = 0x11
34+
}
35+
nonce := make([]byte, 8)
36+
binary.BigEndian.PutUint64(nonce, 5)
37+
38+
version, nodes := exportMidMigrationSnapshot(t, []*proto.KVPair{
39+
{Key: codeHashKey, Value: codeHash},
40+
{Key: nonceKey, Value: nonce},
41+
})
42+
43+
// The stream must carry the nonce in memIAVL and the account row in FlatKV,
44+
// or the restore below passes without exercising the conflict.
45+
acctKey := ktype.EVMPhysicalKey(keys.EVMKeyNonce, addr)
46+
var sawMemIAVLNonce, sawFlatKVAccount bool
47+
for _, n := range nodes {
48+
sawMemIAVLNonce = sawMemIAVLNonce || (n.StoreKey == keys.EVMStoreKey && bytes.Equal(n.Key, nonceKey))
49+
sawFlatKVAccount = sawFlatKVAccount || (n.StoreKey == keys.FlatKVStoreKey && bytes.Equal(n.Key, acctKey))
50+
}
51+
require.True(t, sawMemIAVLNonce && sawFlatKVAccount, "snapshot is not mid-migration")
52+
53+
for _, tc := range []struct {
54+
name string
55+
evmSplit bool
56+
separateEVMSubDBs bool
57+
}{
58+
{name: "cosmos", evmSplit: false},
59+
{name: "evm-split", evmSplit: true},
60+
{name: "evm-separate-sub-dbs", evmSplit: true, separateEVMSubDBs: true},
61+
} {
62+
t.Run(tc.name, func(t *testing.T) {
63+
ssCfg := config.DefaultStateStoreConfig()
64+
ssCfg.Backend = config.PebbleDBBackend
65+
ssCfg.EVMSplit = tc.evmSplit
66+
ssCfg.SeparateEVMSubDBs = tc.separateEVMSubDBs
67+
store, err := NewCompositeStateStore(ssCfg, t.TempDir())
68+
require.NoError(t, err)
69+
defer func() { _ = store.Close() }()
70+
71+
ch := make(chan types.SnapshotNode, len(nodes))
72+
for _, n := range nodes {
73+
ch <- n
74+
}
75+
close(ch)
76+
require.NoError(t, store.Import(version, ch))
77+
78+
got, err := store.Get(keys.EVMStoreKey, version, nonceKey)
79+
require.NoError(t, err)
80+
require.Equal(t, nonce, got)
81+
82+
got, err = store.Get(keys.EVMStoreKey, version, codeHashKey)
83+
require.NoError(t, err)
84+
require.Equal(t, codeHash, got)
85+
})
86+
}
87+
}
88+
89+
// exportMidMigrationSnapshot commits pairs to the evm store on memIAVL, migrates
90+
// exactly one key to FlatKV, and returns the export version with the leaf nodes
91+
// the snapshot stream carries to the state store importer.
92+
func exportMidMigrationSnapshot(t *testing.T, pairs []*proto.KVPair) (int64, []types.SnapshotNode) {
93+
t.Helper()
94+
dir := t.TempDir()
95+
stores := []string{keys.BankStoreKey, keys.EVMStoreKey}
96+
97+
memCfg := config.DefaultStateCommitConfig()
98+
memCfg.WriteMode = sctypes.MemiavlOnly
99+
memCfg.MemIAVLConfig.AsyncCommitBuffer = 0
100+
cs, err := sccomposite.NewCompositeCommitStore(t.Context(), dir, memCfg)
101+
require.NoError(t, err)
102+
require.NoError(t, cs.Initialize(stores))
103+
require.NoError(t, cs.LoadLatest())
104+
require.NoError(t, cs.ApplyChangeSets([]*proto.NamedChangeSet{{
105+
Name: keys.EVMStoreKey,
106+
Changeset: proto.ChangeSet{Pairs: pairs},
107+
}}))
108+
_, err = cs.Commit(cs.Version() + 1)
109+
require.NoError(t, err)
110+
require.NoError(t, cs.Close())
111+
112+
migCfg := config.DefaultStateCommitConfig()
113+
migCfg.WriteMode = sctypes.MigrateEVM
114+
migCfg.MemIAVLConfig.AsyncCommitBuffer = 0
115+
cs, err = sccomposite.NewCompositeCommitStore(t.Context(), dir, migCfg)
116+
require.NoError(t, err)
117+
defer func() { _ = cs.Close() }()
118+
require.NoError(t, cs.SetMigrationBatchSize(1))
119+
require.NoError(t, cs.Initialize(stores))
120+
require.NoError(t, cs.LoadLatest())
121+
require.NoError(t, cs.ApplyChangeSets(nil))
122+
version, err := cs.Commit(cs.Version() + 1)
123+
require.NoError(t, err)
124+
125+
exporter, err := cs.Exporter(version)
126+
require.NoError(t, err)
127+
defer func() { require.NoError(t, exporter.Close()) }()
128+
var nodes []types.SnapshotNode
129+
module := ""
130+
for {
131+
item, err := exporter.Next()
132+
if errors.Is(err, errorutils.ErrorExportDone) {
133+
break
134+
}
135+
require.NoError(t, err)
136+
switch v := item.(type) {
137+
case string:
138+
module = v
139+
case *sctypes.SnapshotNode:
140+
if v.Height == 0 {
141+
nodes = append(nodes, types.SnapshotNode{StoreKey: module, Key: v.Key, Value: v.Value})
142+
}
143+
}
144+
}
145+
return version, nodes
146+
}

‎sei-db/state_db/ss/composite/store.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ func stripEVMFromChangesets(changesets []*proto.NamedChangeSet) []*proto.NamedCh
478478
}
479479

480480
// convertFlatKVNodes transforms a single FlatKV physical-key snapshot node
481-
// into one or more SS nodes by stripping the module prefix from the key,
482-
// deserializing the vtype metadata from the value, and (for merged account
483-
// rows) splitting into separate nonce and codeHash nodes.
481+
// into zero or more SS nodes by stripping the module prefix from the key and
482+
// deserializing the vtype metadata from the value. A merged account row splits
483+
// into separate nonce and codeHash nodes, omitting each one whose value is zero.
484484
//
485485
// For EVM-specific keys (account, storage, code) the output StoreKey is "evm".
486486
// For legacy keys the original module name is preserved so they route back to
@@ -523,7 +523,10 @@ func convertFlatKVNodes(node types.SnapshotNode) ([]types.SnapshotNode, error) {
523523
return nil, fmt.Errorf("failed to DeserializeAccountData: %w", err)
524524
}
525525
var nodes []types.SnapshotNode
526-
if nonce := acct.GetNonce(); !acct.IsDelete() {
526+
// During the EVM migration an account row exists once its code hash
527+
// migrates, before its nonce does. Its zero nonce must not overwrite the
528+
// memIAVL nonce restored earlier in the stream; an absent nonce reads as zero.
529+
if nonce := acct.GetNonce(); nonce != 0 {
527530
nonceBuf := make([]byte, 8)
528531
binary.BigEndian.PutUint64(nonceBuf, nonce)
529532
nodes = append(nodes, types.SnapshotNode{

‎sei-db/state_db/ss/composite/store_test.go‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,72 @@ func TestImport_EVMSplitDisabled_ConvertsFlatkvToCosmos(t *testing.T) {
11651165
require.Equal(t, []byte("ev_1"), ev)
11661166
}
11671167

1168+
// TestImport_FlatKVZeroNonceKeepsMemIAVLNonce feeds the stream a mid-migration
1169+
// snapshot produces for an address whose code hash has migrated but whose nonce
1170+
// has not: the memIAVL nonce, then a FlatKV account row with a zero nonce.
1171+
func TestImport_FlatKVZeroNonceKeepsMemIAVLNonce(t *testing.T) {
1172+
addr := make([]byte, 20)
1173+
addr[19] = 0x07
1174+
nonceKey := commonevm.BuildEVMKey(commonevm.EVMKeyNonce, addr)
1175+
codeHashKey := commonevm.BuildEVMKey(commonevm.EVMKeyCodeHash, addr)
1176+
memIAVLNonce := make([]byte, 8)
1177+
binary.BigEndian.PutUint64(memIAVLNonce, 5)
1178+
1179+
physAcct := ktype.EVMPhysicalKey(commonevm.EVMKeyNonce, addr)
1180+
acctVal := vtype.NewAccountData().SetCodeHash(&vtype.CodeHash{0: 0xCC}).Serialize()
1181+
1182+
for _, mode := range []bool{true, false} {
1183+
t.Run(fmt.Sprintf("EVMSplit=%v", mode), func(t *testing.T) {
1184+
store, cleanup := setupImportTestStore(t, mode)
1185+
defer cleanup()
1186+
1187+
ch := make(chan types.SnapshotNode, 10)
1188+
nodes := []types.SnapshotNode{
1189+
{StoreKey: commonevm.EVMStoreKey, Key: nonceKey, Value: memIAVLNonce},
1190+
{StoreKey: commonevm.FlatKVStoreKey, Key: physAcct, Value: acctVal},
1191+
}
1192+
go feedNodes(ch, nodes)
1193+
1194+
require.NoError(t, store.Import(1, ch))
1195+
1196+
nonce, err := store.Get(evm.EVMStoreKey, 1, nonceKey)
1197+
require.NoError(t, err)
1198+
require.Equal(t, memIAVLNonce, nonce)
1199+
1200+
codeHash, err := store.Get(evm.EVMStoreKey, 1, codeHashKey)
1201+
require.NoError(t, err)
1202+
require.Equal(t, vtype.CodeHash{0: 0xCC}, vtype.CodeHash(codeHash))
1203+
})
1204+
}
1205+
}
1206+
1207+
func TestImport_FlatKVZeroNonceIsAbsent(t *testing.T) {
1208+
addr := make([]byte, 20)
1209+
addr[19] = 0x08
1210+
nonceKey := commonevm.BuildEVMKey(commonevm.EVMKeyNonce, addr)
1211+
1212+
physAcct := ktype.EVMPhysicalKey(commonevm.EVMKeyNonce, addr)
1213+
acctVal := vtype.NewAccountData().SetCodeHash(&vtype.CodeHash{0: 0xCC}).Serialize()
1214+
1215+
for _, mode := range []bool{true, false} {
1216+
t.Run(fmt.Sprintf("EVMSplit=%v", mode), func(t *testing.T) {
1217+
store, cleanup := setupImportTestStore(t, mode)
1218+
defer cleanup()
1219+
1220+
ch := make(chan types.SnapshotNode, 10)
1221+
go feedNodes(ch, []types.SnapshotNode{
1222+
{StoreKey: commonevm.FlatKVStoreKey, Key: physAcct, Value: acctVal},
1223+
})
1224+
1225+
require.NoError(t, store.Import(1, ch))
1226+
1227+
nonce, err := store.Get(evm.EVMStoreKey, 1, nonceKey)
1228+
require.NoError(t, err)
1229+
require.Nil(t, nonce)
1230+
})
1231+
}
1232+
}
1233+
11681234
func TestImport_FlatKVLegacyKeysPreserveModule(t *testing.T) {
11691235
addr := make([]byte, 20)
11701236
addr[0] = 0xAA

0 commit comments

Comments
 (0)