Commit 28be4fa2 authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #7057 from ethereum-optimism/aj/l2-transactions

op-program: Support prefetching l2-transactions
parents 73f2c2b3 c6d0829c
...@@ -64,9 +64,15 @@ func (p *PreimageOracle) headerByBlockHash(blockHash common.Hash) *types.Header ...@@ -64,9 +64,15 @@ func (p *PreimageOracle) headerByBlockHash(blockHash common.Hash) *types.Header
func (p *PreimageOracle) BlockByHash(blockHash common.Hash) *types.Block { func (p *PreimageOracle) BlockByHash(blockHash common.Hash) *types.Block {
header := p.headerByBlockHash(blockHash) header := p.headerByBlockHash(blockHash)
txs := p.LoadTransactions(blockHash, header.TxHash)
return types.NewBlockWithHeader(header).WithBody(txs, nil)
}
func (p *PreimageOracle) LoadTransactions(blockHash common.Hash, txHash common.Hash) []*types.Transaction {
p.hint.Hint(TransactionsHint(blockHash)) p.hint.Hint(TransactionsHint(blockHash))
opaqueTxs := mpt.ReadTrie(header.TxHash, func(key common.Hash) []byte { opaqueTxs := mpt.ReadTrie(txHash, func(key common.Hash) []byte {
return p.oracle.Get(preimage.Keccak256Key(key)) return p.oracle.Get(preimage.Keccak256Key(key))
}) })
...@@ -74,8 +80,7 @@ func (p *PreimageOracle) BlockByHash(blockHash common.Hash) *types.Block { ...@@ -74,8 +80,7 @@ func (p *PreimageOracle) BlockByHash(blockHash common.Hash) *types.Block {
if err != nil { if err != nil {
panic(fmt.Errorf("failed to decode list of txs: %w", err)) panic(fmt.Errorf("failed to decode list of txs: %w", err))
} }
return txs
return types.NewBlockWithHeader(header).WithBody(txs, nil)
} }
func (p *PreimageOracle) NodeByHash(nodeHash common.Hash) []byte { func (p *PreimageOracle) NodeByHash(nodeHash common.Hash) []byte {
......
...@@ -99,7 +99,7 @@ func (p *Prefetcher) prefetch(ctx context.Context, hint string) error { ...@@ -99,7 +99,7 @@ func (p *Prefetcher) prefetch(ctx context.Context, hint string) error {
return fmt.Errorf("failed to fetch L1 block %s receipts: %w", hash, err) return fmt.Errorf("failed to fetch L1 block %s receipts: %w", hash, err)
} }
return p.storeReceipts(receipts) return p.storeReceipts(receipts)
case l2.HintL2BlockHeader: case l2.HintL2BlockHeader, l2.HintL2Transactions:
header, txs, err := p.l2Fetcher.InfoAndTxsByHash(ctx, hash) header, txs, err := p.l2Fetcher.InfoAndTxsByHash(ctx, hash)
if err != nil { if err != nil {
return fmt.Errorf("failed to fetch L2 block %s: %w", hash, err) return fmt.Errorf("failed to fetch L2 block %s: %w", hash, err)
......
...@@ -180,6 +180,31 @@ func TestFetchL2Block(t *testing.T) { ...@@ -180,6 +180,31 @@ func TestFetchL2Block(t *testing.T) {
}) })
} }
func TestFetchL2Transactions(t *testing.T) {
rng := rand.New(rand.NewSource(123))
block, rcpts := testutils.RandomBlock(rng, 10)
hash := block.Hash()
t.Run("AlreadyKnown", func(t *testing.T) {
prefetcher, _, _, kv := createPrefetcher(t)
storeBlock(t, kv, block, rcpts)
oracle := l2.NewPreimageOracle(asOracleFn(t, prefetcher), asHinter(t, prefetcher))
result := oracle.LoadTransactions(hash, block.TxHash())
assertTransactionsEqual(t, block.Transactions(), result)
})
t.Run("Unknown", func(t *testing.T) {
prefetcher, _, l2Cl, _ := createPrefetcher(t)
l2Cl.ExpectInfoAndTxsByHash(hash, eth.BlockToInfo(block), block.Transactions(), nil)
defer l2Cl.MockL2Client.AssertExpectations(t)
oracle := l2.NewPreimageOracle(asOracleFn(t, prefetcher), asHinter(t, prefetcher))
result := oracle.LoadTransactions(hash, block.TxHash())
assertTransactionsEqual(t, block.Transactions(), result)
})
}
func TestFetchL2Node(t *testing.T) { func TestFetchL2Node(t *testing.T) {
rng := rand.New(rand.NewSource(123)) rng := rand.New(rand.NewSource(123))
node := testutils.RandomData(rng, 30) node := testutils.RandomData(rng, 30)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment