Commit edab8ed3 authored by Andreas Bigger's avatar Andreas Bigger

Alphabet submodule refactor.

parent 94a25978
package fault package alphabet
import ( import (
"errors"
"math/big" "math/big"
"strings" "strings"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
var _ types.TraceProvider = (*AlphabetProvider)(nil) var (
ErrIndexTooLarge = errors.New("index is larger than the maximum index")
)
// AlphabetProvider is a [TraceProvider] that provides claims for specific // AlphabetProvider is a [TraceProvider] that provides claims for specific
// indices in the given trace. // indices in the given trace.
...@@ -30,7 +32,7 @@ func NewAlphabetProvider(state string, depth uint64) *AlphabetProvider { ...@@ -30,7 +32,7 @@ func NewAlphabetProvider(state string, depth uint64) *AlphabetProvider {
func (ap *AlphabetProvider) GetPreimage(i uint64) ([]byte, []byte, error) { func (ap *AlphabetProvider) GetPreimage(i uint64) ([]byte, []byte, error) {
// The index cannot be larger than the maximum index as computed by the depth. // The index cannot be larger than the maximum index as computed by the depth.
if i >= ap.maxLen { if i >= ap.maxLen {
return nil, nil, types.ErrIndexTooLarge return nil, nil, ErrIndexTooLarge
} }
// We extend the deepest hash to the maximum depth if the trace is not expansive. // We extend the deepest hash to the maximum depth if the trace is not expansive.
if i >= uint64(len(ap.state)) { if i >= uint64(len(ap.state)) {
......
package fault package alphabet
import ( import (
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -72,7 +71,7 @@ func TestGetPreimage_Succeeds(t *testing.T) { ...@@ -72,7 +71,7 @@ func TestGetPreimage_Succeeds(t *testing.T) {
func TestGetPreimage_TooLargeIndex_Fails(t *testing.T) { func TestGetPreimage_TooLargeIndex_Fails(t *testing.T) {
ap := NewAlphabetProvider("abc", 2) ap := NewAlphabetProvider("abc", 2)
_, _, err := ap.GetPreimage(4) _, _, err := ap.GetPreimage(4)
require.ErrorIs(t, err, types.ErrIndexTooLarge) require.ErrorIs(t, err, ErrIndexTooLarge)
} }
// TestGet_Succeeds tests the Get function. // TestGet_Succeeds tests the Get function.
...@@ -89,7 +88,7 @@ func TestGet_Succeeds(t *testing.T) { ...@@ -89,7 +88,7 @@ func TestGet_Succeeds(t *testing.T) {
func TestGet_IndexTooLarge(t *testing.T) { func TestGet_IndexTooLarge(t *testing.T) {
ap := NewAlphabetProvider("abc", 2) ap := NewAlphabetProvider("abc", 2)
_, err := ap.Get(4) _, err := ap.Get(4)
require.ErrorIs(t, err, types.ErrIndexTooLarge) require.ErrorIs(t, err, ErrIndexTooLarge)
} }
// TestGet_Extends tests the Get function with an index that is larger // TestGet_Extends tests the Get function with an index that is larger
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"math/big" "math/big"
"testing" "testing"
"github.com/ethereum-optimism/optimism/op-challenger/fault/alphabet"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types" "github.com/ethereum-optimism/optimism/op-challenger/fault/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
...@@ -20,7 +21,7 @@ func NewClaimBuilder(t *testing.T, maxDepth int) *ClaimBuilder { ...@@ -20,7 +21,7 @@ func NewClaimBuilder(t *testing.T, maxDepth int) *ClaimBuilder {
return &ClaimBuilder{ return &ClaimBuilder{
require: require.New(t), require: require.New(t),
maxDepth: maxDepth, maxDepth: maxDepth,
correct: &alphabetWithProofProvider{NewAlphabetProvider("abcdefghijklmnopqrstuvwxyz", uint64(maxDepth))}, correct: &alphabetWithProofProvider{alphabet.NewAlphabetProvider("abcdefghijklmnopqrstuvwxyz", uint64(maxDepth))},
} }
} }
...@@ -146,7 +147,7 @@ func (s *SequenceBuilder) Get() types.Claim { ...@@ -146,7 +147,7 @@ func (s *SequenceBuilder) Get() types.Claim {
} }
type alphabetWithProofProvider struct { type alphabetWithProofProvider struct {
*AlphabetProvider *alphabet.AlphabetProvider
} }
func (a *alphabetWithProofProvider) GetPreimage(i uint64) ([]byte, []byte, error) { func (a *alphabetWithProofProvider) GetPreimage(i uint64) ([]byte, []byte, error) {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/bindings" "github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-challenger/config" "github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/fault/alphabet"
"github.com/ethereum-optimism/optimism/op-challenger/fault/cannon" "github.com/ethereum-optimism/optimism/op-challenger/fault/cannon"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types" "github.com/ethereum-optimism/optimism/op-challenger/fault/types"
"github.com/ethereum-optimism/optimism/op-service/txmgr" "github.com/ethereum-optimism/optimism/op-service/txmgr"
...@@ -57,7 +58,7 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se ...@@ -57,7 +58,7 @@ func NewService(ctx context.Context, logger log.Logger, cfg *config.Config) (*se
case config.TraceTypeCannon: case config.TraceTypeCannon:
trace = cannon.NewCannonTraceProvider(logger, cfg.CannonDatadir) trace = cannon.NewCannonTraceProvider(logger, cfg.CannonDatadir)
case config.TraceTypeAlphabet: case config.TraceTypeAlphabet:
trace = NewAlphabetProvider(cfg.AlphabetTrace, uint64(cfg.GameDepth)) trace = alphabet.NewAlphabetProvider(cfg.AlphabetTrace, uint64(cfg.GameDepth))
default: default:
return nil, fmt.Errorf("unsupported trace type: %v", cfg.TraceType) return nil, fmt.Errorf("unsupported trace type: %v", cfg.TraceType)
} }
......
package types package types
import ( import (
"errors"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
var (
ErrIndexTooLarge = errors.New("index is larger than the maximum index")
)
type GameStatus uint8 type GameStatus uint8
const ( const (
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/bindings" "github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum-optimism/optimism/op-chain-ops/deployer" "github.com/ethereum-optimism/optimism/op-chain-ops/deployer"
"github.com/ethereum-optimism/optimism/op-challenger/config" "github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/fault" "github.com/ethereum-optimism/optimism/op-challenger/fault/alphabet"
"github.com/ethereum-optimism/optimism/op-challenger/fault/types" "github.com/ethereum-optimism/optimism/op-challenger/fault/types"
"github.com/ethereum-optimism/optimism/op-e2e/e2eutils/challenger" "github.com/ethereum-optimism/optimism/op-e2e/e2eutils/challenger"
"github.com/ethereum-optimism/optimism/op-service/client/utils" "github.com/ethereum-optimism/optimism/op-service/client/utils"
...@@ -67,7 +67,7 @@ func NewFactoryHelper(t *testing.T, ctx context.Context, client *ethclient.Clien ...@@ -67,7 +67,7 @@ func NewFactoryHelper(t *testing.T, ctx context.Context, client *ethclient.Clien
func (h *FactoryHelper) StartAlphabetGame(ctx context.Context, claimedAlphabet string) *FaultGameHelper { func (h *FactoryHelper) StartAlphabetGame(ctx context.Context, claimedAlphabet string) *FaultGameHelper {
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute) ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel() defer cancel()
trace := fault.NewAlphabetProvider(claimedAlphabet, 4) trace := alphabet.NewAlphabetProvider(claimedAlphabet, 4)
rootClaim, err := trace.Get(lastAlphabetTraceIndex) rootClaim, err := trace.Get(lastAlphabetTraceIndex)
h.require.NoError(err) h.require.NoError(err)
tx, err := h.factory.Create(h.opts, faultGameType, rootClaim, alphaExtraData) tx, err := h.factory.Create(h.opts, faultGameType, rootClaim, alphaExtraData)
......
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