• protolambda's avatar
    FP: add KZG point-evaluation preimage-oracle type (#9461) · a0e47e5a
    protolambda authored
    * FP: add KZG point-evaluation preimage-oracle type, to process L2 oracle call with
    
    * op-program: Integrate kzg precompile oracle
    
    * fix PreimageOracle.sol
    
    * store kzg precompile input preimage
    
    * update bindings and go.mod
    
    * fix go.mod
    
    * resolve TODOs
    
    * s/Hex2Bytes/FromHex
    
    * fix typo
    
    * ctb: set kzg preimage length to 1
    
    * op-challenger: Load kzg point evaluations to PreimageOracle (#9497)
    
    * op-challenger: load KZG point evaluation into oracle
    
    Also: increase max game depth for devnet.
    This is a temporary solution to avoid flakes in e2e tests that use the kzg precompile.
    The current execution trace max step of 2**31 steps isn't sufficient.
    
    ---------
    Co-authored-by: default avatarinphi <mlaw2501@gmail.com>
    Co-authored-by: default avatarrefcell <abigger87@gmail.com>
    a0e47e5a
cache_test.go 3.64 KB
package l1

import (
	"math/rand"
	"testing"

	"github.com/ethereum-optimism/optimism/op-program/client/l1/test"
	"github.com/ethereum-optimism/optimism/op-service/eth"
	"github.com/ethereum-optimism/optimism/op-service/testutils"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/stretchr/testify/require"
)

// Should implement Oracle
var _ Oracle = (*CachingOracle)(nil)

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

	// Initial call retrieves from the stub
	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())
	result = oracle.HeaderByBlockHash(block.Hash())
	require.Equal(t, block, result)
}

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

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

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

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

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

	// Later calls should retrieve from cache
	delete(stub.Blocks, block.Hash())
	delete(stub.Rcpts, block.Hash())
	actualBlock, actualRcpts = oracle.ReceiptsByBlockHash(block.Hash())
	require.Equal(t, eth.BlockToInfo(block), actualBlock)
	require.EqualValues(t, rcpts, actualRcpts)
}

func TestCachingOracle_GetBlobs(t *testing.T) {
	stub := test.NewStubOracle(t)
	oracle := NewCachingOracle(stub)

	l1BlockRef := eth.L1BlockRef{Time: 0}
	indexedBlobHash := eth.IndexedBlobHash{Hash: [32]byte{0xFA, 0xCA, 0xDE}, Index: 0}
	blob := eth.Blob{0xFF}

	// Initial call retrieves from the stub
	stub.Blobs[l1BlockRef] = make(map[eth.IndexedBlobHash]*eth.Blob)
	stub.Blobs[l1BlockRef][indexedBlobHash] = &blob
	actualBlob := oracle.GetBlob(l1BlockRef, indexedBlobHash)
	require.Equal(t, &blob, actualBlob)

	// Later calls should retrieve from cache
	delete(stub.Blobs[l1BlockRef], indexedBlobHash)
	actualBlob = oracle.GetBlob(l1BlockRef, indexedBlobHash)
	require.Equal(t, &blob, actualBlob)
}

func TestCachingOracle_KZGPointEvaluation(t *testing.T) {
	stub := test.NewStubOracle(t)
	oracle := NewCachingOracle(stub)

	input := []byte{0x01, 0x02, 0x03, 0x04}

	// Initial call retrieves from the stub
	stub.PtEvals[crypto.Keccak256Hash(input)] = true
	actualPtEval := oracle.KZGPointEvaluation(input)
	require.True(t, actualPtEval)

	// Later calls should retrieve from cache
	delete(stub.PtEvals, crypto.Keccak256Hash(input))
	actualPtEval = oracle.KZGPointEvaluation(input)
	require.True(t, actualPtEval)
}