Skip to content

Commit

Permalink
op-program: Nasty start to fetching preimages.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsutton committed Apr 14, 2023
1 parent 069e021 commit d383e68
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 82 deletions.
27 changes: 14 additions & 13 deletions op-program/client/l1/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l1/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -13,58 +14,58 @@ var _ Oracle = (*CachingOracle)(nil)

func TestCachingOracle_HeaderByBlockHash(t *testing.T) {
rng := rand.New(rand.NewSource(1))
stub := newStubOracle(t)
stub := test.NewStubOracle(t)
oracle := NewCachingOracle(stub)
block := testutils.RandomBlockInfo(rng)

// Initial call retrieves from the stub
stub.blocks[block.Hash()] = block
stub.Blocks[block.Hash()] = block
result := oracle.HeaderByBlockHash(block.Hash())
require.Equal(t, block, result)

// Later calls should retrieve from cache
delete(stub.blocks, block.Hash())
delete(stub.Blocks, block.Hash())
result = oracle.HeaderByBlockHash(block.Hash())
require.Equal(t, block, result)
}

func TestCachingOracle_TransactionsByBlockHash(t *testing.T) {
rng := rand.New(rand.NewSource(1))
stub := newStubOracle(t)
stub := test.NewStubOracle(t)
oracle := NewCachingOracle(stub)
block, _ := testutils.RandomBlock(rng, 3)

// Initial call retrieves from the stub
stub.blocks[block.Hash()] = block
stub.txs[block.Hash()] = block.Transactions()
stub.Blocks[block.Hash()] = block
stub.Txs[block.Hash()] = block.Transactions()
actualBlock, actualTxs := oracle.TransactionsByBlockHash(block.Hash())
require.Equal(t, block, actualBlock)
require.Equal(t, block.Transactions(), actualTxs)

// Later calls should retrieve from cache
delete(stub.blocks, block.Hash())
delete(stub.txs, block.Hash())
delete(stub.Blocks, block.Hash())
delete(stub.Txs, block.Hash())
actualBlock, actualTxs = oracle.TransactionsByBlockHash(block.Hash())
require.Equal(t, block, actualBlock)
require.Equal(t, block.Transactions(), actualTxs)
}

func TestCachingOracle_ReceiptsByBlockHash(t *testing.T) {
rng := rand.New(rand.NewSource(1))
stub := newStubOracle(t)
stub := test.NewStubOracle(t)
oracle := NewCachingOracle(stub)
block, rcpts := testutils.RandomBlock(rng, 3)

// Initial call retrieves from the stub
stub.blocks[block.Hash()] = block
stub.rcpts[block.Hash()] = rcpts
stub.Blocks[block.Hash()] = block
stub.Rcpts[block.Hash()] = rcpts
actualBlock, actualRcpts := oracle.ReceiptsByBlockHash(block.Hash())
require.Equal(t, block, actualBlock)
require.EqualValues(t, rcpts, actualRcpts)

// Later calls should retrieve from cache
delete(stub.blocks, block.Hash())
delete(stub.rcpts, block.Hash())
delete(stub.Blocks, block.Hash())
delete(stub.Rcpts, block.Hash())
actualBlock, actualRcpts = oracle.ReceiptsByBlockHash(block.Hash())
require.Equal(t, block, actualBlock)
require.EqualValues(t, rcpts, actualRcpts)
Expand Down
23 changes: 12 additions & 11 deletions op-program/client/l1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/sources"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l1/test"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -25,7 +26,7 @@ func TestInfoByHash(t *testing.T) {
client, oracle := newClient(t)
hash := common.HexToHash("0xAABBCC")
expected := &sources.HeaderInfo{}
oracle.blocks[hash] = expected
oracle.Blocks[hash] = expected

info, err := client.InfoByHash(context.Background(), hash)
require.NoError(t, err)
Expand All @@ -36,7 +37,7 @@ func TestL1BlockRefByHash(t *testing.T) {
client, oracle := newClient(t)
hash := common.HexToHash("0xAABBCC")
header := &sources.HeaderInfo{}
oracle.blocks[hash] = header
oracle.Blocks[hash] = header
expected := eth.InfoToL1BlockRef(header)

ref, err := client.L1BlockRefByHash(context.Background(), hash)
Expand All @@ -51,8 +52,8 @@ func TestFetchReceipts(t *testing.T) {
expectedReceipts := types.Receipts{
&types.Receipt{},
}
oracle.blocks[hash] = expectedInfo
oracle.rcpts[hash] = expectedReceipts
oracle.Blocks[hash] = expectedInfo
oracle.Rcpts[hash] = expectedReceipts

info, rcpts, err := client.FetchReceipts(context.Background(), hash)
require.NoError(t, err)
Expand All @@ -67,8 +68,8 @@ func TestInfoAndTxsByHash(t *testing.T) {
expectedTxs := types.Transactions{
&types.Transaction{},
}
oracle.blocks[hash] = expectedInfo
oracle.txs[hash] = expectedTxs
oracle.Blocks[hash] = expectedInfo
oracle.Txs[hash] = expectedTxs

info, txs, err := client.InfoAndTxsByHash(context.Background(), hash)
require.NoError(t, err)
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestL1BlockRefByNumber(t *testing.T) {
t.Run("ParentOfHead", func(t *testing.T) {
client, oracle := newClient(t)
parent := blockNum(head.NumberU64() - 1)
oracle.blocks[parent.Hash()] = parent
oracle.Blocks[parent.Hash()] = parent

ref, err := client.L1BlockRefByNumber(context.Background(), parent.NumberU64())
require.NoError(t, err)
Expand All @@ -132,7 +133,7 @@ func TestL1BlockRefByNumber(t *testing.T) {
blocks := []eth.BlockInfo{block}
for i := 0; i < 10; i++ {
block = blockNum(block.NumberU64() - 1)
oracle.blocks[block.Hash()] = block
oracle.Blocks[block.Hash()] = block
blocks = append(blocks, block)
}

Expand All @@ -144,9 +145,9 @@ func TestL1BlockRefByNumber(t *testing.T) {
})
}

func newClient(t *testing.T) (*OracleL1Client, *stubOracle) {
stub := newStubOracle(t)
stub.blocks[head.Hash()] = head
func newClient(t *testing.T) (*OracleL1Client, *test.StubOracle) {
stub := test.NewStubOracle(t)
stub.Blocks[head.Hash()] = head
client := NewOracleL1Client(testlog.Logger(t, log.LvlDebug), stub, head.Hash())
return client, stub
}
Expand Down
54 changes: 0 additions & 54 deletions op-program/client/l1/stub_oracle_test.go

This file was deleted.

54 changes: 54 additions & 0 deletions op-program/client/l1/test/stub_oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"testing"

"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

type StubOracle struct {
t *testing.T

// Blocks maps block hash to eth.BlockInfo
Blocks map[common.Hash]eth.BlockInfo

// Txs maps block hash to transactions
Txs map[common.Hash]types.Transactions

// Rcpts maps Block hash to receipts
Rcpts map[common.Hash]types.Receipts
}

func NewStubOracle(t *testing.T) *StubOracle {
return &StubOracle{
t: t,
Blocks: make(map[common.Hash]eth.BlockInfo),
Txs: make(map[common.Hash]types.Transactions),
Rcpts: make(map[common.Hash]types.Receipts),
}
}
func (o StubOracle) HeaderByBlockHash(blockHash common.Hash) eth.BlockInfo {
info, ok := o.Blocks[blockHash]
if !ok {
o.t.Fatalf("unknown block %s", blockHash)
}
return info
}

func (o StubOracle) TransactionsByBlockHash(blockHash common.Hash) (eth.BlockInfo, types.Transactions) {
txs, ok := o.Txs[blockHash]
if !ok {
o.t.Fatalf("unknown txs %s", blockHash)
}
return o.HeaderByBlockHash(blockHash), txs
}

func (o StubOracle) ReceiptsByBlockHash(blockHash common.Hash) (eth.BlockInfo, types.Receipts) {
rcpts, ok := o.Rcpts[blockHash]
if !ok {
o.t.Fatalf("unknown rcpts %s", blockHash)
}
return o.HeaderByBlockHash(blockHash), rcpts
}
13 changes: 9 additions & 4 deletions op-program/host/l1/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type Source interface {
InfoByHash(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, error)
HeaderByHash(ctx context.Context, blockHash common.Hash) (*types.Header, error)
InfoAndTxsByHash(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Transactions, error)
FetchReceipts(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Receipts, error)
}
Expand All @@ -31,15 +31,20 @@ func NewFetchingL1Oracle(ctx context.Context, logger log.Logger, source Source)
}

func (o *FetchingL1Oracle) HeaderByBlockHash(blockHash common.Hash) eth.BlockInfo {
header := o.RawHeaderByBlockHash(blockHash)
return eth.HeaderBlockInfo(header)
}

func (o *FetchingL1Oracle) RawHeaderByBlockHash(blockHash common.Hash) *types.Header {
o.logger.Trace("HeaderByBlockHash", "hash", blockHash)
info, err := o.source.InfoByHash(o.ctx, blockHash)
header, err := o.source.HeaderByHash(o.ctx, blockHash)
if err != nil {
panic(fmt.Errorf("retrieve block %s: %w", blockHash, err))
}
if info == nil {
if header == nil {
panic(fmt.Errorf("unknown block: %s", blockHash))
}
return info
return header
}

func (o *FetchingL1Oracle) TransactionsByBlockHash(blockHash common.Hash) (eth.BlockInfo, types.Transactions) {
Expand Down
Loading

0 comments on commit d383e68

Please sign in to comment.