Fix invalid code commitment

This commit is contained in:
Aleksandr Bezobchuk 2018-10-22 22:30:13 -04:00
parent 0c3975ef5c
commit f938f74cf0
2 changed files with 27 additions and 12 deletions

View File

@ -222,6 +222,13 @@ func (so *stateObject) commitState() {
// TODO: Set the account (storage) root (but we probably don't need this)
}
// commitCode persists the state object's code to the KVStore.
func (so *stateObject) commitCode() {
ctx := so.stateDB.ctx
store := ctx.KVStore(so.stateDB.codeKey)
store.Set(so.CodeHash(), so.code)
}
// ----------------------------------------------------------------------------
// Getters
// ----------------------------------------------------------------------------
@ -299,8 +306,6 @@ func (so *stateObject) GetCommittedState(_ ethstate.Database, key ethcmn.Hash) e
store := ctx.KVStore(so.stateDB.storageKey)
rawValue := store.Get(prefixKey.Bytes())
// TODO: Do we need to RLP split/decode?
if len(rawValue) > 0 {
value.SetBytes(rawValue)
}

View File

@ -327,6 +327,7 @@ func (csdb *CommitStateDB) Commit(deleteEmptyObjects bool) (root ethcmn.Hash, er
// set the state objects
for addr, so := range csdb.stateObjects {
_, isDirty := csdb.stateObjectsDirty[addr]
switch {
case so.suicided || (isDirty && deleteEmptyObjects && so.empty()):
// If the state object has been removed, don't bother syncing it and just
@ -336,7 +337,7 @@ func (csdb *CommitStateDB) Commit(deleteEmptyObjects bool) (root ethcmn.Hash, er
case isDirty:
// write any contract code associated with the state object
if so.code != nil && so.dirtyCode {
csdb.SetCode(so.Address(), so.code)
so.commitCode()
so.dirtyCode = false
}
@ -415,7 +416,15 @@ func (csdb *CommitStateDB) deleteStateObject(so *stateObject) {
func (csdb *CommitStateDB) Snapshot() int {
id := csdb.nextRevisionID
csdb.nextRevisionID++
csdb.validRevisions = append(csdb.validRevisions, ethstate.Revision{id, csdb.journal.length()})
csdb.validRevisions = append(
csdb.validRevisions,
ethstate.Revision{
ID: id,
JournalIndex: csdb.journal.length(),
},
)
return id
}
@ -665,12 +674,12 @@ func (csdb *CommitStateDB) setError(err error) {
// Returns nil and sets an error if not found.
func (csdb *CommitStateDB) getStateObject(addr ethcmn.Address) (stateObject *stateObject) {
// prefer 'live' (cached) objects
if obj := csdb.stateObjects[addr]; obj != nil {
if obj.deleted {
if so := csdb.stateObjects[addr]; so != nil {
if so.deleted {
return nil
}
return obj
return so
}
// otherwise, attempt to fetch the account from the account mapper
@ -681,11 +690,12 @@ func (csdb *CommitStateDB) getStateObject(addr ethcmn.Address) (stateObject *sta
}
// insert the state object into the live set
obj := newObject(csdb, acc)
csdb.setStateObject(obj)
return obj
so := newObject(csdb, acc)
csdb.setStateObject(so)
return so
}
func (csdb *CommitStateDB) setStateObject(object *stateObject) {
csdb.stateObjects[object.Address()] = object
func (csdb *CommitStateDB) setStateObject(so *stateObject) {
csdb.stateObjects[so.Address()] = so
}