engine_test.go 7.42 KB
Newer Older
1 2 3 4 5 6 7
package l2

import (
	"context"
	"math/big"
	"testing"

8 9
	"github.com/stretchr/testify/require"

10 11 12 13 14 15 16
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/consensus"
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/core/vm"
	"github.com/ethereum/go-ethereum/params"
	"github.com/ethereum/go-ethereum/trie"
17 18 19 20 21

	"github.com/ethereum-optimism/optimism/op-node/chaincfg"
	"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
	"github.com/ethereum-optimism/optimism/op-node/rollup/engine"
	"github.com/ethereum-optimism/optimism/op-service/eth"
22 23 24
)

// Should implement derive.Engine
25
var _ engine.Engine = (*OracleEngine)(nil)
26 27 28 29 30 31 32 33 34

func TestPayloadByHash(t *testing.T) {
	ctx := context.Background()

	t.Run("KnownBlock", func(t *testing.T) {
		engine, stub := createOracleEngine(t)
		block := stub.head
		payload, err := engine.PayloadByHash(ctx, block.Hash())
		require.NoError(t, err)
Danyal Prout's avatar
Danyal Prout committed
35
		expected, err := eth.BlockAsPayload(block, engine.rollupCfg.CanyonTime)
36
		require.NoError(t, err)
37
		require.Equal(t, &eth.ExecutionPayloadEnvelope{ExecutionPayload: expected}, payload)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	})

	t.Run("UnknownBlock", func(t *testing.T) {
		engine, _ := createOracleEngine(t)
		hash := common.HexToHash("0x878899")
		payload, err := engine.PayloadByHash(ctx, hash)
		require.ErrorIs(t, err, ErrNotFound)
		require.Nil(t, payload)
	})
}

func TestPayloadByNumber(t *testing.T) {
	ctx := context.Background()

	t.Run("KnownBlock", func(t *testing.T) {
		engine, stub := createOracleEngine(t)
		block := stub.head
		payload, err := engine.PayloadByNumber(ctx, block.NumberU64())
		require.NoError(t, err)
Danyal Prout's avatar
Danyal Prout committed
57
		expected, err := eth.BlockAsPayload(block, engine.rollupCfg.CanyonTime)
58
		require.NoError(t, err)
59
		require.Equal(t, &eth.ExecutionPayloadEnvelope{ExecutionPayload: expected}, payload)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	})

	t.Run("NoCanonicalHash", func(t *testing.T) {
		engine, _ := createOracleEngine(t)
		payload, err := engine.PayloadByNumber(ctx, uint64(700))
		require.ErrorIs(t, err, ErrNotFound)
		require.Nil(t, payload)
	})

	t.Run("UnknownBlock", func(t *testing.T) {
		engine, stub := createOracleEngine(t)
		hash := common.HexToHash("0x878899")
		number := uint64(700)
		stub.canonical[number] = hash
		payload, err := engine.PayloadByNumber(ctx, number)
		require.ErrorIs(t, err, ErrNotFound)
		require.Nil(t, payload)
	})
}

func TestL2BlockRefByLabel(t *testing.T) {
	ctx := context.Background()
	engine, stub := createOracleEngine(t)
	tests := []struct {
		name  eth.BlockLabel
		block *types.Block
	}{
		{eth.Unsafe, stub.head},
		{eth.Safe, stub.safe},
		{eth.Finalized, stub.finalized},
	}
	for _, test := range tests {
		t.Run(string(test.name), func(t *testing.T) {
93
			expected, err := derive.L2BlockToBlockRef(engine.rollupCfg, test.block)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			require.NoError(t, err)
			blockRef, err := engine.L2BlockRefByLabel(ctx, test.name)
			require.NoError(t, err)
			require.Equal(t, expected, blockRef)
		})
	}
	t.Run("UnknownLabel", func(t *testing.T) {
		_, err := engine.L2BlockRefByLabel(ctx, "nope")
		require.ErrorContains(t, err, "unknown label")
	})
}

func TestL2BlockRefByHash(t *testing.T) {
	ctx := context.Background()
	engine, stub := createOracleEngine(t)

	t.Run("KnownBlock", func(t *testing.T) {
111
		expected, err := derive.L2BlockToBlockRef(engine.rollupCfg, stub.safe)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		require.NoError(t, err)
		ref, err := engine.L2BlockRefByHash(ctx, stub.safe.Hash())
		require.NoError(t, err)
		require.Equal(t, expected, ref)
	})

	t.Run("UnknownBlock", func(t *testing.T) {
		ref, err := engine.L2BlockRefByHash(ctx, common.HexToHash("0x878899"))
		require.ErrorIs(t, err, ErrNotFound)
		require.Equal(t, eth.L2BlockRef{}, ref)
	})
}

func TestSystemConfigByL2Hash(t *testing.T) {
	ctx := context.Background()
	engine, stub := createOracleEngine(t)

	t.Run("KnownBlock", func(t *testing.T) {
Danyal Prout's avatar
Danyal Prout committed
130
		payload, err := eth.BlockAsPayload(stub.safe, engine.rollupCfg.CanyonTime)
131
		require.NoError(t, err)
132
		expected, err := derive.PayloadToSystemConfig(engine.rollupCfg, payload)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		require.NoError(t, err)
		cfg, err := engine.SystemConfigByL2Hash(ctx, stub.safe.Hash())
		require.NoError(t, err)
		require.Equal(t, expected, cfg)
	})

	t.Run("UnknownBlock", func(t *testing.T) {
		ref, err := engine.SystemConfigByL2Hash(ctx, common.HexToHash("0x878899"))
		require.ErrorIs(t, err, ErrNotFound)
		require.Equal(t, eth.SystemConfig{}, ref)
	})
}

func createOracleEngine(t *testing.T) (*OracleEngine, *stubEngineBackend) {
	head := createL2Block(t, 4)
	safe := createL2Block(t, 3)
	finalized := createL2Block(t, 2)
	backend := &stubEngineBackend{
		head:      head,
		safe:      safe,
		finalized: finalized,
		blocks: map[common.Hash]*types.Block{
			head.Hash():      head,
			safe.Hash():      safe,
			finalized.Hash(): finalized,
		},
		canonical: map[uint64]common.Hash{
			head.NumberU64():      head.Hash(),
			safe.NumberU64():      safe.Hash(),
			finalized.NumberU64(): finalized.Hash(),
		},
	}
	engine := OracleEngine{
		backend:   backend,
167
		rollupCfg: chaincfg.Sepolia,
168 169 170 171 172
	}
	return &engine, backend
}

func createL2Block(t *testing.T, number int) *types.Block {
173
	tx, err := derive.L1InfoDeposit(chaincfg.Sepolia, eth.SystemConfig{}, uint64(1), eth.HeaderBlockInfo(&types.Header{
174 175
		Number:  big.NewInt(32),
		BaseFee: big.NewInt(7),
176
	}), 0)
177 178 179 180 181
	require.NoError(t, err)
	header := &types.Header{
		Number:  big.NewInt(int64(number)),
		BaseFee: big.NewInt(7),
	}
182 183 184 185
	body := &types.Body{
		Transactions: []*types.Transaction{types.NewTx(tx)},
	}
	return types.NewBlock(header, body, nil, trie.NewStackTrie(nil))
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
}

type stubEngineBackend struct {
	head      *types.Block
	safe      *types.Block
	finalized *types.Block
	blocks    map[common.Hash]*types.Block
	canonical map[uint64]common.Hash
}

func (s stubEngineBackend) CurrentHeader() *types.Header {
	return s.head.Header()
}

func (s stubEngineBackend) CurrentSafeBlock() *types.Header {
	return s.safe.Header()
}

func (s stubEngineBackend) CurrentFinalBlock() *types.Header {
	return s.finalized.Header()
}

func (s stubEngineBackend) GetBlockByHash(hash common.Hash) *types.Block {
	return s.blocks[hash]
}

func (s stubEngineBackend) GetCanonicalHash(n uint64) common.Hash {
	return s.canonical[n]
}

func (s stubEngineBackend) GetBlock(hash common.Hash, number uint64) *types.Block {
	panic("unsupported")
}

func (s stubEngineBackend) HasBlockAndState(hash common.Hash, number uint64) bool {
	panic("unsupported")
}

func (s stubEngineBackend) GetVMConfig() *vm.Config {
	panic("unsupported")
}

func (s stubEngineBackend) Config() *params.ChainConfig {
	panic("unsupported")
}

func (s stubEngineBackend) Engine() consensus.Engine {
	panic("unsupported")
}

func (s stubEngineBackend) StateAt(root common.Hash) (*state.StateDB, error) {
	panic("unsupported")
}

func (s stubEngineBackend) InsertBlockWithoutSetHead(block *types.Block) error {
	panic("unsupported")
}

func (s stubEngineBackend) SetCanonical(head *types.Block) (common.Hash, error) {
	panic("unsupported")
}

func (s stubEngineBackend) SetFinalized(header *types.Header) {
	panic("unsupported")
}

func (s stubEngineBackend) SetSafe(header *types.Header) {
	panic("unsupported")
}

func (s stubEngineBackend) GetHeader(hash common.Hash, number uint64) *types.Header {
	panic("unsupported")
}

func (s stubEngineBackend) GetHeaderByNumber(number uint64) *types.Header {
	panic("unsupported")
}

func (s stubEngineBackend) GetHeaderByHash(hash common.Hash) *types.Header {
	panic("unsupported")
}

func (s stubEngineBackend) GetTd(hash common.Hash, number uint64) *big.Int {
	panic("unsupported")
}