Skip to content

core/state: fix statedb.Copy #28094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
132 changes: 131 additions & 1 deletion core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type journalEntry interface {

// dirtied returns the Ethereum address modified by this journal entry.
dirtied() *common.Address

// copy returns a deep-copied journal entry.
copy() journalEntry
}

// journal contains the list of state modifications applied since the last state
Expand Down Expand Up @@ -84,6 +87,22 @@ func (j *journal) length() int {
return len(j.entries)
}

// copy returns a deep-copied journal.
func (j *journal) copy() *journal {
entries := make([]journalEntry, 0, j.length())
for i := 0; i < j.length(); i++ {
entries = append(entries, j.entries[i].copy())
}
dirties := make(map[common.Address]int)
for addr, count := range j.dirties {
dirties[addr] = count
}
return &journal{
entries: entries,
dirties: dirties,
}
}

type (
// Changes to the account trie.
createObjectChange struct {
Expand Down Expand Up @@ -137,6 +156,7 @@ type (
touchChange struct {
account *common.Address
}

// Changes to the access list
accessListAddAccountChange struct {
address *common.Address
Expand All @@ -146,6 +166,7 @@ type (
slot *common.Hash
}

// Changes to transient storage
transientStorageChange struct {
account *common.Address
key, prevalue common.Hash
Expand All @@ -154,13 +175,18 @@ type (

func (ch createObjectChange) revert(s *StateDB) {
delete(s.stateObjects, *ch.account)
delete(s.stateObjectsDirty, *ch.account)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's totally wrong to modify the Dirty map, which is the marker for state commit and is only modified at the transaction boundary. An internal state revert should never touch it.

}

func (ch createObjectChange) dirtied() *common.Address {
return ch.account
}

func (ch createObjectChange) copy() journalEntry {
return createObjectChange{
account: ch.account,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You*re copying the pointer here, right?

}
}

func (ch resetObjectChange) revert(s *StateDB) {
s.setStateObject(ch.prev)
if !ch.prevdestruct {
Expand All @@ -184,6 +210,27 @@ func (ch resetObjectChange) dirtied() *common.Address {
return ch.account
}

func (ch resetObjectChange) copy() journalEntry {
prevStorage := make(map[common.Hash][]byte)
for key, slot := range ch.prevStorage {
prevStorage[key] = common.CopyBytes(slot)
}
prevStorageOrigin := make(map[common.Hash][]byte)
for key, slot := range ch.prevStorageOrigin {
prevStorageOrigin[key] = common.CopyBytes(slot)
}
return resetObjectChange{
account: ch.account,
prev: ch.prev.deepCopy(),
prevdestruct: ch.prevdestruct,
prevAccount: common.CopyBytes(ch.prevAccount),
prevStorage: prevStorage,
prevAccountOriginExist: ch.prevAccountOriginExist,
prevAccountOrigin: common.CopyBytes(ch.prevAccountOrigin),
prevStorageOrigin: prevStorageOrigin,
}
}

func (ch selfDestructChange) revert(s *StateDB) {
obj := s.getStateObject(*ch.account)
if obj != nil {
Expand All @@ -196,6 +243,14 @@ func (ch selfDestructChange) dirtied() *common.Address {
return ch.account
}

func (ch selfDestructChange) copy() journalEntry {
return selfDestructChange{
account: ch.account,
prev: ch.prev,
prevbalance: new(big.Int).Set(ch.prevbalance),
}
}

var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")

func (ch touchChange) revert(s *StateDB) {
Expand All @@ -205,6 +260,12 @@ func (ch touchChange) dirtied() *common.Address {
return ch.account
}

func (ch touchChange) copy() journalEntry {
return touchChange{
account: ch.account,
}
}

func (ch balanceChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setBalance(ch.prev)
}
Expand All @@ -213,6 +274,13 @@ func (ch balanceChange) dirtied() *common.Address {
return ch.account
}

func (ch balanceChange) copy() journalEntry {
return balanceChange{
account: ch.account,
prev: new(big.Int).Set(ch.prev),
}
}

func (ch nonceChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setNonce(ch.prev)
}
Expand All @@ -221,6 +289,13 @@ func (ch nonceChange) dirtied() *common.Address {
return ch.account
}

func (ch nonceChange) copy() journalEntry {
return nonceChange{
account: ch.account,
prev: ch.prev,
}
}

func (ch codeChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
}
Expand All @@ -229,6 +304,14 @@ func (ch codeChange) dirtied() *common.Address {
return ch.account
}

func (ch codeChange) copy() journalEntry {
return codeChange{
account: ch.account,
prevhash: common.CopyBytes(ch.prevhash),
prevcode: common.CopyBytes(ch.prevcode),
}
}

func (ch storageChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
}
Expand All @@ -237,6 +320,14 @@ func (ch storageChange) dirtied() *common.Address {
return ch.account
}

func (ch storageChange) copy() journalEntry {
return storageChange{
account: ch.account,
key: ch.key,
prevalue: ch.prevalue,
}
}

func (ch transientStorageChange) revert(s *StateDB) {
s.setTransientState(*ch.account, ch.key, ch.prevalue)
}
Expand All @@ -245,6 +336,14 @@ func (ch transientStorageChange) dirtied() *common.Address {
return nil
}

func (ch transientStorageChange) copy() journalEntry {
return transientStorageChange{
account: ch.account,
key: ch.key,
prevalue: ch.prevalue,
}
}

func (ch refundChange) revert(s *StateDB) {
s.refund = ch.prev
}
Expand All @@ -253,6 +352,12 @@ func (ch refundChange) dirtied() *common.Address {
return nil
}

func (ch refundChange) copy() journalEntry {
return refundChange{
prev: ch.prev,
}
}

func (ch addLogChange) revert(s *StateDB) {
logs := s.logs[ch.txhash]
if len(logs) == 1 {
Expand All @@ -267,6 +372,12 @@ func (ch addLogChange) dirtied() *common.Address {
return nil
}

func (ch addLogChange) copy() journalEntry {
return addLogChange{
txhash: ch.txhash,
}
}

func (ch addPreimageChange) revert(s *StateDB) {
delete(s.preimages, ch.hash)
}
Expand All @@ -275,6 +386,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
return nil
}

func (ch addPreimageChange) copy() journalEntry {
return addPreimageChange{
hash: ch.hash,
}
}

func (ch accessListAddAccountChange) revert(s *StateDB) {
/*
One important invariant here, is that whenever a (addr, slot) is added, if the
Expand All @@ -292,10 +409,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
return nil
}

func (ch accessListAddAccountChange) copy() journalEntry {
return accessListAddAccountChange{
address: ch.address,
}
}

func (ch accessListAddSlotChange) revert(s *StateDB) {
s.accessList.DeleteSlot(*ch.address, *ch.slot)
}

func (ch accessListAddSlotChange) dirtied() *common.Address {
return nil
}

func (ch accessListAddSlotChange) copy() journalEntry {
return accessListAddSlotChange{
address: ch.address,
slot: ch.slot,
}
}
6 changes: 3 additions & 3 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,16 @@ func (s *stateObject) setBalance(amount *big.Int) {
s.data.Balance = amount
}

func (s *stateObject) deepCopy(db *StateDB) *stateObject {
func (s *stateObject) deepCopy() *stateObject {
obj := &stateObject{
db: db,
db: s.db,
address: s.address,
addrHash: s.addrHash,
origin: s.origin,
data: s.data,
}
if s.trie != nil {
obj.trie = db.db.CopyTrie(s.trie)
obj.trie = s.db.db.CopyTrie(s.trie)
}
obj.code = s.code
obj.dirtyStorage = s.dirtyStorage.Copy()
Expand Down
18 changes: 18 additions & 0 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,21 @@ func compareStateObjects(so0, so1 *stateObject, t *testing.T) {
}
}
}

func TestCreateObjectRevert(t *testing.T) {
state, _ := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), nil)
addr := common.BytesToAddress([]byte("so0"))
snap := state.Snapshot()

state.CreateAccount(addr)
so0 := state.getStateObject(addr)
so0.SetBalance(big.NewInt(42))
so0.SetNonce(43)
so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
state.setStateObject(so0)

state.RevertToSnapshot(snap)
if state.Exist(addr) {
t.Error("Unexpected account after revert")
}
}
31 changes: 4 additions & 27 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ func (s *StateDB) Selfdestruct6780(addr common.Address) {
if stateObject == nil {
return
}

if stateObject.created {
s.SelfDestruct(addr)
}
Expand Down Expand Up @@ -702,7 +701,7 @@ func (s *StateDB) Copy() *StateDB {
logs: make(map[common.Hash][]*types.Log, len(s.logs)),
logSize: s.logSize,
preimages: make(map[common.Hash][]byte, len(s.preimages)),
journal: newJournal(),
journal: s.journal.copy(),
hasher: crypto.NewKeccakState(),

// In order for the block producer to be able to use and make additions
Expand All @@ -712,36 +711,14 @@ func (s *StateDB) Copy() *StateDB {
snaps: s.snaps,
snap: s.snap,
}
// Copy the dirty states, logs, and preimages
for addr := range s.journal.dirties {
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
// and in the Finalise-method, there is a case where an object is in the journal but not
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
// nil
if object, exist := s.stateObjects[addr]; exist {
// Even though the original object is dirty, we are not copying the journal,
// so we need to make sure that any side-effect the journal would have caused
// during a commit (or similar op) is already applied to the copy.
state.stateObjects[addr] = object.deepCopy(state)

state.stateObjectsDirty[addr] = struct{}{} // Mark the copy dirty to force internal (code/state) commits
state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
}
// Deep copy cached state objects along with the pending and dirty markers.
for addr, obj := range s.stateObjects {
state.stateObjects[addr] = obj.deepCopy()
}
// Above, we don't copy the actual journal. This means that if the copy
// is copied, the loop above will be a no-op, since the copy's journal
// is empty. Thus, here we iterate over stateObjects, to enable copies
// of copies.
for addr := range s.stateObjectsPending {
if _, exist := state.stateObjects[addr]; !exist {
state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
}
state.stateObjectsPending[addr] = struct{}{}
}
for addr := range s.stateObjectsDirty {
if _, exist := state.stateObjects[addr]; !exist {
state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
}
state.stateObjectsDirty[addr] = struct{}{}
}
// Deep copy the destruction markers.
Expand Down
Loading