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

The function "Copy" from "CommitStateDB" did not complete the copy of some fields #717

Closed
summerpro opened this issue Jan 13, 2021 · 0 comments · Fixed by #720
Closed

The function "Copy" from "CommitStateDB" did not complete the copy of some fields #717

summerpro opened this issue Jan 13, 2021 · 0 comments · Fixed by #720

Comments

@summerpro
Copy link
Contributor

System info: [Include Ethermint commit, operating system name, and other relevant details]

  • branch development

  • Copy function:

  • The field "thash", "bhash", "txIndex", "validRevisions", "nextRevisionID", "accessList" are not copied to the new object

// Copy creates a deep, independent copy of the state.
//
// NOTE: Snapshots of the copied state cannot be applied to the copy.
func (csdb *CommitStateDB) Copy() *CommitStateDB {

	// copy all the basic fields, initialize the memory ones
	state := &CommitStateDB{}
	CopyCommitStateDB(csdb, state)

	return state
}

func CopyCommitStateDB(from, to *CommitStateDB) {
	from.lock.Lock()
	defer from.lock.Unlock()

	to.ctx = from.ctx
	to.storeKey = from.storeKey
	to.paramSpace = from.paramSpace
	to.accountKeeper = from.accountKeeper
	to.stateObjects = []stateEntry{}
	to.addressToObjectIndex = make(map[ethcmn.Address]int)
	to.stateObjectsDirty = make(map[ethcmn.Address]struct{})
	to.refund = from.refund
	to.logSize = from.logSize
	to.preimages = make([]preimageEntry, len(from.preimages))
	to.hashToPreimageIndex = make(map[ethcmn.Hash]int, len(from.hashToPreimageIndex))
	to.journal = newJournal()

	// copy the dirty states, logs, and preimages
	for _, dirty := range from.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 := from.addressToObjectIndex[dirty.address]; exist {
			to.stateObjects = append(to.stateObjects, stateEntry{
				address:     dirty.address,
				stateObject: from.stateObjects[idx].stateObject.deepCopy(to),
			})
			to.addressToObjectIndex[dirty.address] = len(to.stateObjects) - 1
			to.stateObjectsDirty[dirty.address] = struct{}{}
		}
	}

	// 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 from.stateObjectsDirty {
		if idx, exist := to.addressToObjectIndex[addr]; !exist {
			to.setStateObject(from.stateObjects[idx].stateObject.deepCopy(to))
			to.stateObjectsDirty[addr] = struct{}{}
		}
	}

	// copy pre-images
	for i, preimageEntry := range from.preimages {
		to.preimages[i] = preimageEntry
		to.hashToPreimageIndex[preimageEntry.hash] = i
	}
}
@summerpro summerpro changed the title The function Copy from CommitStateDB did not complete the copy of some fields The function "Copy" from "CommitStateDB" did not complete the copy of some fields Jan 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant