This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
evm: debug non-determinism #496
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,9 +59,8 @@ type CommitStateDB struct { | |
|
||
// TODO: Determine if we actually need this as we do not need preimages in | ||
// the SDK, but it seems to be used elsewhere in Geth. | ||
// | ||
// NOTE: it is safe to use map here because it's only used for Copy | ||
preimages map[ethcmn.Hash][]byte | ||
preimages []preimageEntry | ||
hashToPreimageIndex map[ethcmn.Hash]int // map from hash to the index of the preimages slice | ||
|
||
// DB error. | ||
// State objects are used by the consensus core and VM which are | ||
|
@@ -95,7 +94,8 @@ func NewCommitStateDB( | |
stateObjects: []stateEntry{}, | ||
addressToObjectIndex: make(map[ethcmn.Address]int), | ||
stateObjectsDirty: make(map[ethcmn.Address]struct{}), | ||
preimages: make(map[ethcmn.Hash][]byte), | ||
preimages: []preimageEntry{}, | ||
hashToPreimageIndex: make(map[ethcmn.Hash]int), | ||
journal: newJournal(), | ||
} | ||
} | ||
|
@@ -207,12 +207,14 @@ func (csdb *CommitStateDB) AddLog(log *ethtypes.Log) { | |
|
||
// AddPreimage records a SHA3 preimage seen by the VM. | ||
func (csdb *CommitStateDB) AddPreimage(hash ethcmn.Hash, preimage []byte) { | ||
if _, ok := csdb.preimages[hash]; !ok { | ||
if _, ok := csdb.hashToPreimageIndex[hash]; !ok { | ||
csdb.journal.append(addPreimageChange{hash: hash}) | ||
|
||
pi := make([]byte, len(preimage)) | ||
copy(pi, preimage) | ||
csdb.preimages[hash] = pi | ||
|
||
csdb.preimages = append(csdb.preimages, preimageEntry{hash: hash, preimage: pi}) | ||
csdb.hashToPreimageIndex[hash] = len(csdb.preimages) - 1 | ||
} | ||
} | ||
|
||
|
@@ -358,7 +360,12 @@ func (csdb *CommitStateDB) GetRefund() uint64 { | |
|
||
// Preimages returns a list of SHA3 preimages that have been submitted. | ||
func (csdb *CommitStateDB) Preimages() map[ethcmn.Hash][]byte { | ||
return csdb.preimages | ||
preimages := map[ethcmn.Hash][]byte{} | ||
|
||
for _, pe := range csdb.preimages { | ||
preimages[pe.hash] = pe.preimage | ||
} | ||
return preimages | ||
} | ||
|
||
// HasSuicided returns if the given account for the specified address has been | ||
|
@@ -608,7 +615,8 @@ func (csdb *CommitStateDB) Reset(_ ethcmn.Hash) error { | |
csdb.bhash = ethcmn.Hash{} | ||
csdb.txIndex = 0 | ||
csdb.logSize = 0 | ||
csdb.preimages = make(map[ethcmn.Hash][]byte) | ||
csdb.preimages = []preimageEntry{} | ||
csdb.hashToPreimageIndex = make(map[ethcmn.Hash]int) | ||
|
||
csdb.clearJournalAndRefund() | ||
return nil | ||
|
@@ -685,27 +693,29 @@ func (csdb *CommitStateDB) Copy() *CommitStateDB { | |
ctx: csdb.ctx, | ||
storeKey: csdb.storeKey, | ||
accountKeeper: csdb.accountKeeper, | ||
stateObjects: make([]stateEntry, len(csdb.journal.dirties)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed the length/capacity and replaced it with append |
||
addressToObjectIndex: make(map[ethcmn.Address]int, len(csdb.journal.dirties)), | ||
stateObjectsDirty: make(map[ethcmn.Address]struct{}, len(csdb.journal.dirties)), | ||
stateObjects: []stateEntry{}, | ||
addressToObjectIndex: make(map[ethcmn.Address]int), | ||
stateObjectsDirty: make(map[ethcmn.Address]struct{}), | ||
refund: csdb.refund, | ||
logSize: csdb.logSize, | ||
preimages: make(map[ethcmn.Hash][]byte), | ||
preimages: make([]preimageEntry, len(csdb.preimages)), | ||
hashToPreimageIndex: make(map[ethcmn.Hash]int, len(csdb.hashToPreimageIndex)), | ||
journal: newJournal(), | ||
} | ||
|
||
// copy the dirty states, logs, and preimages | ||
for i, dirty := range csdb.journal.dirties { | ||
for _, dirty := range csdb.journal.dirties { | ||
// 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. | ||
// | ||
// Ref: https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527 | ||
if idx, exist := csdb.addressToObjectIndex[dirty.address]; exist { | ||
state.stateObjects[i] = stateEntry{ | ||
state.stateObjects = append(state.stateObjects, stateEntry{ | ||
address: dirty.address, | ||
stateObject: csdb.stateObjects[idx].stateObject.deepCopy(state), | ||
} | ||
}) | ||
state.addressToObjectIndex[dirty.address] = len(state.stateObjects) - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure why append should reduce by 1. |
||
state.stateObjectsDirty[dirty.address] = struct{}{} | ||
} | ||
} | ||
|
@@ -721,8 +731,9 @@ func (csdb *CommitStateDB) Copy() *CommitStateDB { | |
} | ||
|
||
// copy pre-images | ||
for hash, preimage := range csdb.preimages { | ||
state.preimages[hash] = preimage | ||
for i, preimageEntry := range csdb.preimages { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replaced to slice to prevent non-determinism |
||
state.preimages[i] = preimageEntry | ||
state.hashToPreimageIndex[preimageEntry.hash] = i | ||
} | ||
|
||
return state | ||
|
@@ -854,3 +865,9 @@ func (csdb *CommitStateDB) setStateObject(so *stateObject) { | |
func (csdb *CommitStateDB) RawDump() ethstate.Dump { | ||
return ethstate.Dump{} | ||
} | ||
|
||
type preimageEntry struct { | ||
// hash key of the preimage entry | ||
hash ethcmn.Hash | ||
preimage []byte | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we were deleting the object from the slice but we weren't deleting the item from the map nor updating the corresponding indexes of the other elements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably the fix for the issue yesterday, can remove the
idx < len(csdb.stateObjects)
check ingetStateObject
andsetStateObject
in #458There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks! let me add a few tests so that we can merge this to development