mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
core/state: reintroduce net sstore tracking, extend tests for it
This commit is contained in:
parent
412c3f84cd
commit
7502810ec9
3 changed files with 70 additions and 22 deletions
|
|
@ -79,9 +79,10 @@ type stateObject struct {
|
||||||
trie Trie // storage trie, which becomes non-nil on first access
|
trie Trie // storage trie, which becomes non-nil on first access
|
||||||
code Code // contract bytecode, which gets set when code is loaded
|
code Code // contract bytecode, which gets set when code is loaded
|
||||||
|
|
||||||
originStorage Storage // Storage cache of original entries to dedup rewrites, reset for every transaction
|
originStorage Storage // Storage cache of original entries to dedup rewrites, reset for every transaction
|
||||||
dirtyStorage Storage // Storage entries that have been modified since the last intermediate root or commit
|
pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block
|
||||||
fakeStorage Storage // Fake storage which constructed by caller for debugging purpose.
|
dirtyStorage Storage // Storage entries that have been modified in the current transaction execution
|
||||||
|
fakeStorage Storage // Fake storage which constructed by caller for debugging purpose.
|
||||||
|
|
||||||
// Cache flags.
|
// Cache flags.
|
||||||
// When an object is marked suicided it will be delete from the trie
|
// When an object is marked suicided it will be delete from the trie
|
||||||
|
|
@ -117,12 +118,13 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
|
||||||
data.Root = emptyRoot
|
data.Root = emptyRoot
|
||||||
}
|
}
|
||||||
return &stateObject{
|
return &stateObject{
|
||||||
db: db,
|
db: db,
|
||||||
address: address,
|
address: address,
|
||||||
addrHash: crypto.Keccak256Hash(address[:]),
|
addrHash: crypto.Keccak256Hash(address[:]),
|
||||||
data: data,
|
data: data,
|
||||||
originStorage: make(Storage),
|
originStorage: make(Storage),
|
||||||
dirtyStorage: make(Storage),
|
pendingStorage: make(Storage),
|
||||||
|
dirtyStorage: make(Storage),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -187,6 +189,9 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
|
||||||
return s.fakeStorage[key]
|
return s.fakeStorage[key]
|
||||||
}
|
}
|
||||||
// If we have a pending write or clean cached, return that
|
// If we have a pending write or clean cached, return that
|
||||||
|
if value, pending := s.pendingStorage[key]; pending {
|
||||||
|
return value
|
||||||
|
}
|
||||||
if value, cached := s.originStorage[key]; cached {
|
if value, cached := s.originStorage[key]; cached {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
@ -255,15 +260,29 @@ func (s *stateObject) setState(key, value common.Hash) {
|
||||||
s.dirtyStorage[key] = value
|
s.dirtyStorage[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// finalise moves all dirty storage slots into the pending area to be hashed or
|
||||||
|
// committed later. It is invoked at the end of every transaction.
|
||||||
|
func (s *stateObject) finalise() {
|
||||||
|
for key, value := range s.dirtyStorage {
|
||||||
|
s.pendingStorage[key] = value
|
||||||
|
}
|
||||||
|
if len(s.dirtyStorage) > 0 {
|
||||||
|
s.dirtyStorage = make(Storage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// updateTrie writes cached storage modifications into the object's storage trie.
|
// updateTrie writes cached storage modifications into the object's storage trie.
|
||||||
func (s *stateObject) updateTrie(db Database) Trie {
|
func (s *stateObject) updateTrie(db Database) Trie {
|
||||||
|
// Make sure all dirty slots are finalized into the pending storage area
|
||||||
|
s.finalise()
|
||||||
|
|
||||||
// Track the amount of time wasted on updating the storge trie
|
// Track the amount of time wasted on updating the storge trie
|
||||||
if metrics.EnabledExpensive {
|
if metrics.EnabledExpensive {
|
||||||
defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
|
defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
|
||||||
}
|
}
|
||||||
// Insert all the pending updates into the trie
|
// Insert all the pending updates into the trie
|
||||||
tr := s.getTrie(db)
|
tr := s.getTrie(db)
|
||||||
for key, value := range s.dirtyStorage {
|
for key, value := range s.pendingStorage {
|
||||||
// Skip noop changes, persist actual changes
|
// Skip noop changes, persist actual changes
|
||||||
if value == s.originStorage[key] {
|
if value == s.originStorage[key] {
|
||||||
continue
|
continue
|
||||||
|
|
@ -278,8 +297,8 @@ func (s *stateObject) updateTrie(db Database) Trie {
|
||||||
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
|
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
|
||||||
s.setError(tr.TryUpdate(key[:], v))
|
s.setError(tr.TryUpdate(key[:], v))
|
||||||
}
|
}
|
||||||
if len(s.dirtyStorage) > 0 {
|
if len(s.pendingStorage) > 0 {
|
||||||
s.dirtyStorage = make(Storage)
|
s.pendingStorage = make(Storage)
|
||||||
}
|
}
|
||||||
return tr
|
return tr
|
||||||
}
|
}
|
||||||
|
|
@ -360,6 +379,7 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
|
||||||
stateObject.code = s.code
|
stateObject.code = s.code
|
||||||
stateObject.dirtyStorage = s.dirtyStorage.Copy()
|
stateObject.dirtyStorage = s.dirtyStorage.Copy()
|
||||||
stateObject.originStorage = s.originStorage.Copy()
|
stateObject.originStorage = s.originStorage.Copy()
|
||||||
|
stateObject.pendingStorage = s.pendingStorage.Copy()
|
||||||
stateObject.suicided = s.suicided
|
stateObject.suicided = s.suicided
|
||||||
stateObject.dirtyCode = s.dirtyCode
|
stateObject.dirtyCode = s.dirtyCode
|
||||||
stateObject.deleted = s.deleted
|
stateObject.deleted = s.deleted
|
||||||
|
|
|
||||||
|
|
@ -672,6 +672,8 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
||||||
}
|
}
|
||||||
if obj.suicided || (deleteEmptyObjects && obj.empty()) {
|
if obj.suicided || (deleteEmptyObjects && obj.empty()) {
|
||||||
obj.deleted = true
|
obj.deleted = true
|
||||||
|
} else {
|
||||||
|
obj.finalise()
|
||||||
}
|
}
|
||||||
s.stateObjectsPending[addr] = struct{}{}
|
s.stateObjectsPending[addr] = struct{}{}
|
||||||
s.stateObjectsDirty[addr] = struct{}{}
|
s.stateObjectsDirty[addr] = struct{}{}
|
||||||
|
|
|
||||||
|
|
@ -473,7 +473,10 @@ func TestCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := state.GetState(addr, skey); val != sval {
|
if val := state.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("initial storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
||||||
|
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
||||||
}
|
}
|
||||||
// Copy the non-committed state database and check pre/post commit balance
|
// Copy the non-committed state database and check pre/post commit balance
|
||||||
copyOne := state.Copy()
|
copyOne := state.Copy()
|
||||||
|
|
@ -484,7 +487,10 @@ func TestCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("first copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("first copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyOne.GetState(addr, skey); val != sval {
|
if val := copyOne.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("first copy pre-commit storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("first copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
||||||
|
t.Fatalf("first copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
||||||
}
|
}
|
||||||
|
|
||||||
copyOne.Commit(false)
|
copyOne.Commit(false)
|
||||||
|
|
@ -495,7 +501,10 @@ func TestCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("first copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("first copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyOne.GetState(addr, skey); val != sval {
|
if val := copyOne.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("first copy post-commit storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("first copy post-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyOne.GetCommittedState(addr, skey); val != sval {
|
||||||
|
t.Fatalf("first copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
}
|
}
|
||||||
// Copy the copy and check the balance once more
|
// Copy the copy and check the balance once more
|
||||||
copyTwo := copyOne.Copy()
|
copyTwo := copyOne.Copy()
|
||||||
|
|
@ -506,7 +515,10 @@ func TestCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("second copy code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("second copy code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyTwo.GetState(addr, skey); val != sval {
|
if val := copyTwo.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("second copy storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("second copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyTwo.GetCommittedState(addr, skey); val != sval {
|
||||||
|
t.Fatalf("second copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -533,7 +545,10 @@ func TestCopyCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("initial code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := state.GetState(addr, skey); val != sval {
|
if val := state.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("initial storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("initial non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := state.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
||||||
|
t.Fatalf("initial committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
||||||
}
|
}
|
||||||
// Copy the non-committed state database and check pre/post commit balance
|
// Copy the non-committed state database and check pre/post commit balance
|
||||||
copyOne := state.Copy()
|
copyOne := state.Copy()
|
||||||
|
|
@ -544,7 +559,10 @@ func TestCopyCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("first copy code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("first copy code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyOne.GetState(addr, skey); val != sval {
|
if val := copyOne.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("first copy storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("first copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyOne.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
||||||
|
t.Fatalf("first copy committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
||||||
}
|
}
|
||||||
// Copy the copy and check the balance once more
|
// Copy the copy and check the balance once more
|
||||||
copyTwo := copyOne.Copy()
|
copyTwo := copyOne.Copy()
|
||||||
|
|
@ -555,9 +573,11 @@ func TestCopyCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("second copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("second copy pre-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyTwo.GetState(addr, skey); val != sval {
|
if val := copyTwo.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("second copy pre-commit storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("second copy pre-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyTwo.GetCommittedState(addr, skey); val != (common.Hash{}) {
|
||||||
|
t.Fatalf("second copy pre-commit committed storage slot mismatch: have %x, want %x", val, common.Hash{})
|
||||||
}
|
}
|
||||||
|
|
||||||
copyTwo.Commit(false)
|
copyTwo.Commit(false)
|
||||||
if balance := copyTwo.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
|
if balance := copyTwo.GetBalance(addr); balance.Cmp(big.NewInt(42)) != 0 {
|
||||||
t.Fatalf("second copy post-commit balance mismatch: have %v, want %v", balance, 42)
|
t.Fatalf("second copy post-commit balance mismatch: have %v, want %v", balance, 42)
|
||||||
|
|
@ -566,7 +586,10 @@ func TestCopyCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("second copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("second copy post-commit code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyTwo.GetState(addr, skey); val != sval {
|
if val := copyTwo.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("second copy post-commit storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("second copy post-commit non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyTwo.GetCommittedState(addr, skey); val != sval {
|
||||||
|
t.Fatalf("second copy post-commit committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
}
|
}
|
||||||
// Copy the copy-copy and check the balance once more
|
// Copy the copy-copy and check the balance once more
|
||||||
copyThree := copyTwo.Copy()
|
copyThree := copyTwo.Copy()
|
||||||
|
|
@ -577,7 +600,10 @@ func TestCopyCopyCommitCopy(t *testing.T) {
|
||||||
t.Fatalf("third copy code mismatch: have %x, want %x", code, []byte("hello"))
|
t.Fatalf("third copy code mismatch: have %x, want %x", code, []byte("hello"))
|
||||||
}
|
}
|
||||||
if val := copyThree.GetState(addr, skey); val != sval {
|
if val := copyThree.GetState(addr, skey); val != sval {
|
||||||
t.Fatalf("third copy storage slot mismatch: have %x, want %x", val, sval)
|
t.Fatalf("third copy non-committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
|
}
|
||||||
|
if val := copyThree.GetCommittedState(addr, skey); val != sval {
|
||||||
|
t.Fatalf("third copy committed storage slot mismatch: have %x, want %x", val, sval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue