Commit 4772d23e authored by refcell.eth's avatar refcell.eth Committed by GitHub

fix(op-challenger): Flatten Alphabet Trace Provider Step Loop (#8853)

* op-challenger: flatten alphabet step loop

* op-challenger: add test for stepping on the max position
parent 771988b6
package alphabet
import (
"bytes"
"context"
"errors"
"fmt"
......@@ -45,38 +44,20 @@ func NewTraceProvider(startingBlockNumber *big.Int, depth types.Depth) *Alphabet
}
func (ap *AlphabetTraceProvider) GetStepData(ctx context.Context, pos types.Position) ([]byte, []byte, *types.PreimageOracleData, error) {
posIndex := pos.TraceIndex(ap.depth)
traceIndex := pos.TraceIndex(ap.depth)
key := preimage.LocalIndexKey(L2ClaimBlockNumberLocalIndex).PreimageKey()
preimageData := types.NewPreimageOracleData(key[:], ap.startingBlockNumber.Bytes(), 0)
if posIndex.Cmp(common.Big0) == 0 {
if traceIndex.Cmp(common.Big0) == 0 {
return absolutePrestate, []byte{}, preimageData, nil
}
// We want the pre-state which is the value prior to the one requested
prestateTraceIndex := new(big.Int).Sub(posIndex, big.NewInt(1))
if prestateTraceIndex.Cmp(new(big.Int).SetUint64(ap.maxLen)) >= 0 {
return nil, nil, nil, fmt.Errorf("%w depth: %v index: %v max: %v", ErrIndexTooLarge, ap.depth, posIndex, ap.maxLen)
if traceIndex.Cmp(new(big.Int).SetUint64(ap.maxLen)) > 0 {
return nil, nil, nil, fmt.Errorf("%w depth: %v index: %v max: %v", ErrIndexTooLarge, ap.depth, traceIndex, ap.maxLen)
}
// First step expands the absolute preimage to its full form. Weird but it's how AlphabetVM works.
claim := ap.step(absolutePrestate)
for i := big.NewInt(0); i.Cmp(prestateTraceIndex) <= 0; i = i.Add(i, big.NewInt(1)) {
claim = ap.step(claim)
}
return claim, []byte{}, preimageData, nil
}
// step accepts the trace index and claim and returns the stepped trace index and claim.
func (ap *AlphabetTraceProvider) step(stateData []byte) []byte {
// Decode the stateData into the trace index and claim
traceIndex := new(big.Int).SetBytes(stateData[:32])
claim := stateData[32:]
if bytes.Equal(stateData, absolutePrestate) {
initTraceIndex := new(big.Int).Lsh(ap.startingBlockNumber, 4)
initClaim := new(big.Int).Add(absolutePrestateInt, initTraceIndex)
return BuildAlphabetPreimage(initTraceIndex, initClaim)
}
stepTraceIndex := new(big.Int).Add(traceIndex, big.NewInt(1))
stepClaim := new(big.Int).Add(new(big.Int).SetBytes(claim), big.NewInt(1))
return BuildAlphabetPreimage(stepTraceIndex, stepClaim)
initialTraceIndex := new(big.Int).Lsh(ap.startingBlockNumber, 4)
initialClaim := new(big.Int).Add(absolutePrestateInt, initialTraceIndex)
newTraceIndex := new(big.Int).Add(initialTraceIndex, traceIndex)
newClaim := new(big.Int).Add(initialClaim, traceIndex)
return BuildAlphabetPreimage(newTraceIndex, newClaim), []byte{}, preimageData, nil
}
// Get returns the claim value at the given index in the trace.
......
......@@ -44,7 +44,26 @@ func TestAlphabetProvider_Prestate(t *testing.T) {
}
}
// TestAlphabetProvider_Get_ClaimsByTraceIndex tests the [fault.AlphabetProvider] Get function.
func TestAlphabetProvider_GetStepData_MaxLen(t *testing.T) {
depth := types.Depth(4)
startingL2BlockNumber := big.NewInt(2)
ap := NewTraceProvider(startingL2BlockNumber, depth)
// Step data for the max position is allowed
maxLen := int64(1 << depth)
maxPos := types.NewPosition(4, big.NewInt(maxLen))
result, _, _, err := ap.GetStepData(context.Background(), maxPos)
require.NoError(t, err)
expected := "00000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000090"
require.Equal(t, expected, common.Bytes2Hex(result))
// Cannot step on a position greater than the max.
oobPos := types.NewPosition(4, big.NewInt(int64(1<<depth)+1))
_, _, _, err = ap.GetStepData(context.Background(), oobPos)
require.ErrorIs(t, err, ErrIndexTooLarge)
}
// TestAlphabetProvider_Get_ClaimsByTraceIndex tests the Get function.
func TestAlphabetProvider_Get_ClaimsByTraceIndex(t *testing.T) {
// Create a new alphabet provider.
depth := types.Depth(3)
......
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