oracle.go 4.44 KB
Newer Older
1 2
package l2

3
import (
4 5
	"fmt"

6
	interopTypes "github.com/ethereum-optimism/optimism/op-program/client/interop/types"
7 8
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
9 10
	"github.com/ethereum/go-ethereum/rlp"

11
	preimage "github.com/ethereum-optimism/optimism/op-preimage"
12
	"github.com/ethereum-optimism/optimism/op-program/client/mpt"
13
	"github.com/ethereum-optimism/optimism/op-service/eth"
14
)
15 16 17 18 19 20 21

// StateOracle defines the high-level API used to retrieve L2 state data pre-images
// The returned data is always the preimage of the requested hash.
type StateOracle interface {
	// NodeByHash retrieves the merkle-patricia trie node pre-image for a given hash.
	// Trie nodes may be from the world state trie or any account storage trie.
	// Contract code is not stored as part of the trie and must be retrieved via CodeByHash
22
	NodeByHash(nodeHash common.Hash) []byte
23 24 25

	// CodeByHash retrieves the contract code pre-image for a given hash.
	// codeHash should be retrieved from the world state account for a contract.
26
	CodeByHash(codeHash common.Hash) []byte
27
}
28 29 30 31 32 33 34

// Oracle defines the high-level API used to retrieve L2 data.
// The returned data is always the preimage of the requested hash.
type Oracle interface {
	StateOracle

	// BlockByHash retrieves the block with the given hash.
35
	BlockByHash(blockHash common.Hash) *types.Block
36 37

	OutputByRoot(root common.Hash) eth.Output
38 39 40

	// BlockDataByHash retrieves the block, including all data used to construct it.
	BlockDataByHash(agreedBlockHash, blockHash common.Hash, chainID uint64) *types.Block
41 42

	TransitionStateByRoot(root common.Hash) *interopTypes.TransitionState
43
}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

// PreimageOracle implements Oracle using by interfacing with the pure preimage.Oracle
// to fetch pre-images to decode into the requested data.
type PreimageOracle struct {
	oracle preimage.Oracle
	hint   preimage.Hinter
}

var _ Oracle = (*PreimageOracle)(nil)

func NewPreimageOracle(raw preimage.Oracle, hint preimage.Hinter) *PreimageOracle {
	return &PreimageOracle{
		oracle: raw,
		hint:   hint,
	}
}

func (p *PreimageOracle) headerByBlockHash(blockHash common.Hash) *types.Header {
	p.hint.Hint(BlockHeaderHint(blockHash))
	headerRlp := p.oracle.Get(preimage.Keccak256Key(blockHash))
	var header types.Header
	if err := rlp.DecodeBytes(headerRlp, &header); err != nil {
		panic(fmt.Errorf("invalid block header %s: %w", blockHash, err))
	}
	return &header
}

func (p *PreimageOracle) BlockByHash(blockHash common.Hash) *types.Block {
	header := p.headerByBlockHash(blockHash)
73 74
	txs := p.LoadTransactions(blockHash, header.TxHash)

75
	return types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
76 77 78
}

func (p *PreimageOracle) LoadTransactions(blockHash common.Hash, txHash common.Hash) []*types.Transaction {
79 80
	p.hint.Hint(TransactionsHint(blockHash))

81
	opaqueTxs := mpt.ReadTrie(txHash, func(key common.Hash) []byte {
82 83 84 85 86 87 88
		return p.oracle.Get(preimage.Keccak256Key(key))
	})

	txs, err := eth.DecodeTransactions(opaqueTxs)
	if err != nil {
		panic(fmt.Errorf("failed to decode list of txs: %w", err))
	}
89
	return txs
90 91 92 93 94 95 96 97 98 99 100
}

func (p *PreimageOracle) NodeByHash(nodeHash common.Hash) []byte {
	p.hint.Hint(StateNodeHint(nodeHash))
	return p.oracle.Get(preimage.Keccak256Key(nodeHash))
}

func (p *PreimageOracle) CodeByHash(codeHash common.Hash) []byte {
	p.hint.Hint(CodeHint(codeHash))
	return p.oracle.Get(preimage.Keccak256Key(codeHash))
}
101 102 103 104 105 106 107 108 109 110

func (p *PreimageOracle) OutputByRoot(l2OutputRoot common.Hash) eth.Output {
	p.hint.Hint(L2OutputHint(l2OutputRoot))
	data := p.oracle.Get(preimage.Keccak256Key(l2OutputRoot))
	output, err := eth.UnmarshalOutput(data)
	if err != nil {
		panic(fmt.Errorf("invalid L2 output data for root %s: %w", l2OutputRoot, err))
	}
	return output
}
111 112 113 114 115 116 117 118 119 120 121 122

func (p *PreimageOracle) BlockDataByHash(agreedBlockHash, blockHash common.Hash, chainID uint64) *types.Block {
	hint := L2BlockDataHint{
		AgreedBlockHash: agreedBlockHash,
		BlockHash:       blockHash,
		ChainID:         chainID,
	}
	p.hint.Hint(hint)
	header := p.headerByBlockHash(blockHash)
	txs := p.LoadTransactions(blockHash, header.TxHash)
	return types.NewBlockWithHeader(header).WithBody(types.Body{Transactions: txs})
}
123 124 125 126

func (p *PreimageOracle) TransitionStateByRoot(root common.Hash) *interopTypes.TransitionState {
	p.hint.Hint(AgreedPrestateHint(root))
	data := p.oracle.Get(preimage.Keccak256Key(root))
127
	output, err := interopTypes.UnmarshalTransitionState(data)
128 129 130 131 132
	if err != nil {
		panic(fmt.Errorf("invalid agreed prestate data for root %s: %w", root, err))
	}
	return output
}