Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

fix csdb index error #493

Merged
merged 4 commits into from
Sep 1, 2020
Merged
Changes from 3 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
27 changes: 13 additions & 14 deletions x/evm/types/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (csdb *CommitStateDB) Finalise(deleteEmptyObjects bool) error {
}
}

csdb.stateObjectsDirty[dirty.address] = struct{}{}
delete(csdb.stateObjectsDirty, dirty.address)
}

// invalidate journal because reverting across transactions is not allowed
Expand Down Expand Up @@ -702,38 +702,39 @@ func (csdb *CommitStateDB) Copy() *CommitStateDB {
storeKey: csdb.storeKey,
paramSpace: csdb.paramSpace,
accountKeeper: csdb.accountKeeper,
stateObjects: make([]stateEntry, len(csdb.journal.dirties)),
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),
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.stateObjectsDirty[dirty.address] = struct{}{}
})
delete(state.stateObjectsDirty, dirty.address)
state.addressToObjectIndex[dirty.address] = len(state.stateObjects) - 1
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the fix, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unfortunately not, the fix seemed to be idx < len(csdb.stateObjects) inside getStateObject and setStateObject which I don't really like...

}
}

// 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 csdb.stateObjectsDirty {
if idx, exist := state.addressToObjectIndex[addr]; !exist {
if idx, exist := state.addressToObjectIndex[addr]; exist {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be !exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so but we are using idx in the line right after, which is bad if it doesn't exist

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i see, will update!

state.setStateObject(csdb.stateObjects[idx].stateObject.deepCopy(state))
state.stateObjectsDirty[addr] = struct{}{}
delete(state.stateObjectsDirty, addr)
}
}

Expand Down Expand Up @@ -821,8 +822,7 @@ func (csdb *CommitStateDB) setError(err error) {
// getStateObject attempts to retrieve a state object given by the address.
// Returns nil and sets an error if not found.
func (csdb *CommitStateDB) getStateObject(addr ethcmn.Address) (stateObject *stateObject) {
idx, found := csdb.addressToObjectIndex[addr]
if found {
if idx, found := csdb.addressToObjectIndex[addr]; found && idx < len(csdb.stateObjects) {
// prefer 'live' (cached) objects
if so := csdb.stateObjects[idx].stateObject; so != nil {
if so.deleted {
Expand All @@ -848,8 +848,7 @@ func (csdb *CommitStateDB) getStateObject(addr ethcmn.Address) (stateObject *sta
}

func (csdb *CommitStateDB) setStateObject(so *stateObject) {
idx, found := csdb.addressToObjectIndex[so.Address()]
if found {
if idx, found := csdb.addressToObjectIndex[so.Address()]; found && idx < len(csdb.stateObjects) {
// update the existing object
csdb.stateObjects[idx].stateObject = so
return
Expand Down