Commit fedab2a6 authored by refcell.eth's avatar refcell.eth Committed by GitHub

op-challenger: touchup alphabet trace provider tests (#8860)

parent a7a1775b
...@@ -24,8 +24,10 @@ var ( ...@@ -24,8 +24,10 @@ var (
ErrIndexTooLarge = errors.New("index is larger than the maximum index") ErrIndexTooLarge = errors.New("index is larger than the maximum index")
) )
// AlphabetTraceProvider is a [TraceProvider] that provides claims for specific var _ types.TraceProvider = (*AlphabetTraceProvider)(nil)
// indices in the given trace.
// AlphabetTraceProvider is a [TraceProvider] that monotonically increments
// the starting l2 block number as the claim value.
type AlphabetTraceProvider struct { type AlphabetTraceProvider struct {
AlphabetPrestateProvider AlphabetPrestateProvider
startingBlockNumber *big.Int startingBlockNumber *big.Int
...@@ -93,8 +95,8 @@ func (ap *AlphabetTraceProvider) Get(ctx context.Context, i types.Position) (com ...@@ -93,8 +95,8 @@ func (ap *AlphabetTraceProvider) Get(ctx context.Context, i types.Position) (com
} }
// BuildAlphabetPreimage constructs the claim bytes for the index and claim. // BuildAlphabetPreimage constructs the claim bytes for the index and claim.
func BuildAlphabetPreimage(i *big.Int, blockNumber *big.Int) []byte { func BuildAlphabetPreimage(traceIndex *big.Int, claim *big.Int) []byte {
return append(i.FillBytes(make([]byte, 32)), blockNumber.FillBytes(make([]byte, 32))...) return append(traceIndex.FillBytes(make([]byte, 32)), claim.FillBytes(make([]byte, 32))...)
} }
func alphabetStateHash(state []byte) common.Hash { func alphabetStateHash(state []byte) common.Hash {
......
...@@ -80,62 +80,127 @@ func TestAlphabetProvider_Get_ClaimsByTraceIndex(t *testing.T) { ...@@ -80,62 +80,127 @@ func TestAlphabetProvider_Get_ClaimsByTraceIndex(t *testing.T) {
} }
} }
// TestGetPreimage_Succeeds tests the GetPreimage function // TestAlphabetProvider_GetStepData tests the GetStepData function.
// returns the correct pre-image for a index. func TestAlphabetProvider_GetStepData(t *testing.T) {
func TestGetStepData_Succeeds(t *testing.T) {
depth := types.Depth(2) depth := types.Depth(2)
startingL2BlockNumber := big.NewInt(1) startingL2BlockNumber := big.NewInt(1)
ap := NewTraceProvider(startingL2BlockNumber, depth) ap := NewTraceProvider(startingL2BlockNumber, depth)
expected := absolutePrestate
pos := types.NewPosition(depth, big.NewInt(0))
retrieved, proof, data, err := ap.GetStepData(context.Background(), pos)
require.NoError(t, err)
require.Equal(t, expected, retrieved)
require.Empty(t, proof)
key := preimage.LocalIndexKey(L2ClaimBlockNumberLocalIndex).PreimageKey() key := preimage.LocalIndexKey(L2ClaimBlockNumberLocalIndex).PreimageKey()
expectedLocalContextData := types.NewPreimageOracleData(key[:], startingL2BlockNumber.Bytes(), 0) expectedPreimageData := types.NewPreimageOracleData(key[:], startingL2BlockNumber.Bytes(), 0)
require.Equal(t, expectedLocalContextData, data)
} tests := []struct {
name string
// TestGetPreimage_TooLargeIndex_Fails tests the GetPreimage indexAtDepth *big.Int
// function errors if the index is too large. expectedResult []byte
func TestGetStepData_TooLargeIndex_Fails(t *testing.T) { expectedPreimageData *types.PreimageOracleData
depth := types.Depth(2) expectedError error
startingL2BlockNumber := big.NewInt(1) }{
ap := NewTraceProvider(startingL2BlockNumber, depth) {
pos := types.NewPosition(depth, big.NewInt(5)) name: "AbsolutePrestate",
_, _, _, err := ap.GetStepData(context.Background(), pos) indexAtDepth: big.NewInt(0),
require.ErrorIs(t, err, ErrIndexTooLarge) expectedResult: absolutePrestate,
} expectedPreimageData: expectedPreimageData,
expectedError: nil,
},
{
name: "SecondStep",
indexAtDepth: big.NewInt(1),
expectedResult: BuildAlphabetPreimage(big.NewInt(17), big.NewInt(113)),
expectedPreimageData: expectedPreimageData,
expectedError: nil,
},
{
name: "LastStep",
indexAtDepth: big.NewInt(4),
expectedResult: BuildAlphabetPreimage(big.NewInt(20), big.NewInt(116)),
expectedPreimageData: expectedPreimageData,
expectedError: nil,
},
{
name: "IndexTooLarge",
indexAtDepth: big.NewInt(5),
expectedResult: nil,
expectedPreimageData: nil,
expectedError: ErrIndexTooLarge,
},
}
// TestGet_Succeeds tests the Get function. for _, test := range tests {
func TestGet_Succeeds(t *testing.T) { test := test
depth := types.Depth(2) t.Run(test.name, func(t *testing.T) {
startingL2BlockNumber := big.NewInt(1)
ap := NewTraceProvider(startingL2BlockNumber, depth) result, proof, data, err := ap.GetStepData(context.Background(), types.NewPosition(depth, test.indexAtDepth))
pos := types.NewPosition(depth, big.NewInt(0)) require.Equal(t, test.expectedResult, result)
claim, err := ap.Get(context.Background(), pos) require.Empty(t, proof)
require.NoError(t, err) require.Equal(t, test.expectedPreimageData, data)
expected := alphabetClaim(big.NewInt(17), big.NewInt(113)) if test.expectedError != nil {
require.Equal(t, expected, claim) require.ErrorIs(t, err, test.expectedError)
} else {
require.NoError(t, err)
}
})
}
} }
// TestGet_IndexTooLarge tests the Get function with an index // TestAlphabetProvider_Get tests the Get function.
// greater than the number of indices: 2^depth - 1. func TestAlphabetProvider_Get(t *testing.T) {
func TestGet_IndexTooLarge(t *testing.T) { tests := []struct {
depth := types.Depth(2) name string
startingL2BlockNumber := big.NewInt(1) depth types.Depth
ap := NewTraceProvider(startingL2BlockNumber, depth) indexAtDepth *big.Int
pos := types.NewPosition(depth, big.NewInt(4)) expectedClaim common.Hash
_, err := ap.Get(context.Background(), pos) expectedError error
require.ErrorIs(t, err, ErrIndexTooLarge) }{
} {
name: "AbsolutePrestate",
depth: types.Depth(2),
indexAtDepth: big.NewInt(0),
expectedClaim: alphabetClaim(big.NewInt(17), big.NewInt(113)),
expectedError: nil,
},
{
name: "SecondStep",
depth: types.Depth(2),
indexAtDepth: big.NewInt(1),
expectedClaim: alphabetClaim(big.NewInt(18), big.NewInt(114)),
expectedError: nil,
},
{
name: "LastStep",
depth: types.Depth(2),
indexAtDepth: big.NewInt(3),
expectedClaim: alphabetClaim(big.NewInt(20), big.NewInt(116)),
expectedError: nil,
},
{
name: "IndexTooLarge",
depth: types.Depth(2),
indexAtDepth: big.NewInt(5),
expectedClaim: common.Hash{},
expectedError: ErrIndexTooLarge,
},
{
name: "DepthTooLarge",
depth: types.Depth(3),
indexAtDepth: big.NewInt(0),
expectedClaim: common.Hash{},
expectedError: ErrIndexTooLarge,
},
}
func TestGet_DepthTooLarge(t *testing.T) { for _, test := range tests {
depth := types.Depth(2) test := test
startingL2BlockNumber := big.NewInt(1) t.Run(test.name, func(t *testing.T) {
ap := NewTraceProvider(startingL2BlockNumber, depth) startingL2BlockNumber := big.NewInt(1)
pos := types.NewPosition(depth+1, big.NewInt(0)) ap := NewTraceProvider(startingL2BlockNumber, types.Depth(2))
_, err := ap.Get(context.Background(), pos)
require.ErrorIs(t, err, ErrIndexTooLarge) result, err := ap.Get(context.Background(), types.NewPosition(test.depth, test.indexAtDepth))
require.Equal(t, test.expectedClaim, result)
if test.expectedError != nil {
require.ErrorIs(t, err, test.expectedError)
} else {
require.NoError(t, err)
}
})
}
} }
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