Skip to content

Commit 2bcda23

Browse files
authored
Use circular buffer for BLOCKHASH history (ethereum#402)
* eip2935: use ring buffer Signed-off-by: Ignacio Hagopian <[email protected]> * limit resolving scope Signed-off-by: Ignacio Hagopian <[email protected]> --------- Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent fcdcd3b commit 2bcda23

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

core/state_processor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,9 @@ func InsertBlockHashHistoryAtEip2935Fork(statedb *state.StateDB, prevNumber uint
378378
}
379379

380380
func ProcessParentBlockHash(statedb *state.StateDB, prevNumber uint64, prevHash common.Hash) {
381+
ringIndex := prevNumber % 256
381382
var key common.Hash
382-
binary.BigEndian.PutUint64(key[24:], prevNumber)
383+
binary.BigEndian.PutUint64(key[24:], ringIndex)
383384
statedb.SetState(params.HistoryStorageAddress, key, prevHash)
384385
index, suffix := utils.GetTreeKeyStorageSlotTreeIndexes(key[:])
385386
statedb.Witness().TouchAddressOnWriteAndComputeGas(params.HistoryStorageAddress[:], *index, suffix)

core/vm/instructions.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,9 @@ func opGasprice(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
517517
}
518518

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

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

542538
var upper, lower uint64
543539
upper = interpreter.evm.Context.BlockNumber.Uint64()
@@ -547,7 +543,12 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
547543
lower = upper - 256
548544
}
549545
if num64 >= lower && num64 < upper {
550-
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
546+
// if Prague is active, read it from the history contract (EIP 2935).
547+
if evm.chainRules.IsPrague {
548+
num.SetBytes(getBlockHashFromContract(num64, evm.StateDB, evm.Accesses).Bytes())
549+
} else {
550+
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
551+
}
551552
} else {
552553
num.Clear()
553554
}

0 commit comments

Comments
 (0)