Skip to content

Commit

Permalink
Use circular buffer for BLOCKHASH history (#402)
Browse files Browse the repository at this point in the history
* eip2935: use ring buffer

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* limit resolving scope

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

---------

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign authored and gballet committed May 8, 2024
1 parent 6310993 commit 40d5153
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,9 @@ func InsertBlockHashHistoryAtEip2935Fork(statedb *state.StateDB, prevNumber uint
}

func ProcessParentBlockHash(statedb *state.StateDB, prevNumber uint64, prevHash common.Hash) {
ringIndex := prevNumber % 256
var key common.Hash
binary.BigEndian.PutUint64(key[24:], prevNumber)
binary.BigEndian.PutUint64(key[24:], ringIndex)
statedb.SetState(params.HistoryStorageAddress, key, prevHash)
index, suffix := utils.GetTreeKeyStorageSlotTreeIndexes(key[:])
statedb.Witness().TouchAddressOnWriteAndComputeGas(params.HistoryStorageAddress[:], *index, suffix)
Expand Down
15 changes: 8 additions & 7 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,9 @@ func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
}

func getBlockHashFromContract(number uint64, statedb StateDB, witness *state.AccessWitness) common.Hash {
ringIndex := number % 256
var pnum common.Hash
binary.BigEndian.PutUint64(pnum[24:], number)
binary.BigEndian.PutUint64(pnum[24:], ringIndex)
treeIndex, suffix := utils.GetTreeKeyStorageSlotTreeIndexes(pnum.Bytes())
witness.TouchAddressOnReadAndComputeGas(params.HistoryStorageAddress[:], *treeIndex, suffix)
return statedb.GetState(params.HistoryStorageAddress, pnum)
Expand All @@ -533,11 +534,6 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
}

evm := interpreter.evm
// if Prague is active, read it from the history contract (EIP 2935).
if evm.chainRules.IsPrague {
num.SetBytes(getBlockHashFromContract(num64, evm.StateDB, evm.Accesses).Bytes())
return nil, nil
}

var upper, lower uint64
upper = interpreter.evm.Context.BlockNumber.Uint64()
Expand All @@ -547,7 +543,12 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
lower = upper - 256
}
if num64 >= lower && num64 < upper {
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
// if Prague is active, read it from the history contract (EIP 2935).
if evm.chainRules.IsPrague {
num.SetBytes(getBlockHashFromContract(num64, evm.StateDB, evm.Accesses).Bytes())
} else {
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
}
} else {
num.Clear()
}
Expand Down

0 comments on commit 40d5153

Please sign in to comment.