Skip to content

Commit

Permalink
Merge pull request ethereum#36 from OffchainLabs/callbacks-for-blockn…
Browse files Browse the repository at this point in the history
…um-hash

Implement NUMBER and BLOCKHASH opcodes as callbacks
  • Loading branch information
PlasmaPower authored Jan 14, 2022
2 parents f00de6c + 686284f commit 00396ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 13 additions & 1 deletion core/vm/evm_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package vm

import "github.com/ethereum/go-ethereum/common"
import (
"github.com/ethereum/go-ethereum/common"
)

// Depth returns the current depth
func (evm *EVM) Depth() int {
Expand All @@ -30,6 +32,8 @@ type TxProcessingHook interface {
NonrefundableGas() uint64
PushCaller(addr common.Address)
PopCaller()
L1BlockNumber(blockCtx BlockContext) (uint64, error)
L1BlockHash(blockCtx BlockContext, l1BlocKNumber uint64) (common.Hash, error)
}

type DefaultTxProcessor struct{}
Expand Down Expand Up @@ -57,3 +61,11 @@ func (p DefaultTxProcessor) PushCaller(addr common.Address) {
func (p DefaultTxProcessor) PopCaller() {
return
}

func (p DefaultTxProcessor) L1BlockNumber(blockCtx BlockContext) (uint64, error) {
return blockCtx.BlockNumber.Uint64(), nil
}

func (p DefaultTxProcessor) L1BlockHash(blockCtx BlockContext, l1BlocKNumber uint64) (common.Hash, error) {
return blockCtx.GetHash(l1BlocKNumber), nil
}
13 changes: 11 additions & 2 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"golang.org/x/crypto/sha3"
"math/big"
)

func opAdd(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down Expand Up @@ -445,7 +446,11 @@ func opBlockhash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
lower = upper - 256
}
if num64 >= lower && num64 < upper {
num.SetBytes(interpreter.evm.Context.GetHash(num64).Bytes())
h, err := interpreter.evm.ProcessingHook.L1BlockHash(interpreter.evm.Context, num64)
if err != nil {
return nil, err
}
num.SetBytes(h.Bytes())
} else {
num.Clear()
}
Expand All @@ -464,7 +469,11 @@ func opTimestamp(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) (
}

func opNumber(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
v, _ := uint256.FromBig(interpreter.evm.Context.BlockNumber)
bnum, err := interpreter.evm.ProcessingHook.L1BlockNumber(interpreter.evm.Context)
if err != nil {
return nil, err
}
v, _ := uint256.FromBig(new(big.Int).SetUint64(bnum))
scope.Stack.push(v)
return nil, nil
}
Expand Down

0 comments on commit 00396ce

Please sign in to comment.