Skip to content

Commit 708f319

Browse files
committed
core/state: test to demonstrate flaw in dirty-handling
1 parent 413ebf7 commit 708f319

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

core/state/statedb_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -1419,3 +1419,53 @@ func TestStorageDirtiness(t *testing.T) {
14191419
state.RevertToSnapshot(snap)
14201420
checkDirty(common.Hash{0x1}, common.Hash{0x1}, true)
14211421
}
1422+
1423+
func TestStorageDirtiness2(t *testing.T) {
1424+
var (
1425+
disk = rawdb.NewMemoryDatabase()
1426+
tdb = triedb.NewDatabase(disk, nil)
1427+
db = NewDatabase(tdb, nil)
1428+
state, _ = New(types.EmptyRootHash, db)
1429+
addr = common.HexToAddress("0x1")
1430+
checkDirty = func(key common.Hash, value common.Hash, dirty bool) {
1431+
t.Helper()
1432+
obj := state.getStateObject(addr)
1433+
v, exist := obj.dirtyStorage[key]
1434+
if exist != dirty {
1435+
t.Fatalf("unexpected dirty marker, want: %v, have: %v", dirty, exist)
1436+
}
1437+
if !exist {
1438+
return
1439+
}
1440+
if v != value {
1441+
t.Fatalf("unexpected storage slot, want: %x, have: %x", value, v)
1442+
}
1443+
}
1444+
)
1445+
1446+
{ // Initiate a state, where an account has SLOT(1) = 0xA, +nonzero balance
1447+
state.CreateAccount(addr)
1448+
state.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified) // Prevent empty-delete
1449+
state.SetState(addr, common.Hash{0x1}, common.Hash{0xa})
1450+
root, err := state.Commit(0, true)
1451+
if err != nil {
1452+
t.Fatal(err)
1453+
}
1454+
// Init phase done, load it again
1455+
if state, err = New(root, NewDatabase(tdb, nil)); err != nil {
1456+
t.Fatal(err)
1457+
}
1458+
}
1459+
// A no-op storage change, no dirty marker
1460+
state.SetState(addr, common.Hash{0x1}, common.Hash{0xa})
1461+
checkDirty(common.Hash{0x1}, common.Hash{0xa}, false)
1462+
1463+
// Enter new scope
1464+
snap := state.Snapshot()
1465+
state.SetState(addr, common.Hash{0x1}, common.Hash{0xb}) // SLOT(1) = 0xB
1466+
checkDirty(common.Hash{0x1}, common.Hash{0xb}, true) // Should be flagged dirty
1467+
state.RevertToSnapshot(snap) // Revert scope
1468+
1469+
// the storage change has been set back to original, dirtiness should be revoked
1470+
checkDirty(common.Hash{0x1}, common.Hash{0x1}, false)
1471+
}

0 commit comments

Comments
 (0)