Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(MSFDG): add common datatypes #81

Merged
merged 4 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions op-challenger2/cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func TestLogLevel(t *testing.T) {

func TestDefaultCLIOptionsMatchDefaultConfig(t *testing.T) {
cfg := configForArgs(t, addRequiredArgs(config.TraceTypeAlphabet))
defaultCfg := config.NewConfig(common.HexToAddress(gameFactoryAddressValue), l1EthRpc, l1Beacon, rollupRpc, l2EthRpc, datadir, config.TraceTypeAlphabet)
defaultCfg := config.NewConfig(common.HexToAddress(gameFactoryAddressValue), l1EthRpc, l1Beacon, rollupRpc, l2EthRpc, datadir, config.DACalldata, config.TraceTypeAlphabet)
blockchaindevsh marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, defaultCfg, cfg)
}

func TestDefaultConfigIsValid(t *testing.T) {
cfg := config.NewConfig(common.HexToAddress(gameFactoryAddressValue), l1EthRpc, l1Beacon, rollupRpc, l2EthRpc, datadir, config.TraceTypeAlphabet)
cfg := config.NewConfig(common.HexToAddress(gameFactoryAddressValue), l1EthRpc, l1Beacon, rollupRpc, l2EthRpc, datadir, config.DACalldata, config.TraceTypeAlphabet)
require.NoError(t, cfg.Check())
}

Expand Down
10 changes: 10 additions & 0 deletions op-challenger2/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ const (
TraceTypePermissioned TraceType = "permissioned"
)

const (
DACalldata int64 = 0
DABlob int64 = 1
)

var TraceTypes = []TraceType{TraceTypeAlphabet, TraceTypeCannon, TraceTypePermissioned, TraceTypeAsterisc}

func (t TraceType) String() string {
Expand Down Expand Up @@ -154,6 +159,8 @@ type Config struct {
TxMgrConfig txmgr.CLIConfig
MetricsConfig opmetrics.CLIConfig
PprofConfig oppprof.CLIConfig

DAType int64
}

func NewConfig(
Expand All @@ -163,6 +170,7 @@ func NewConfig(
l2RollupRpc string,
l2EthRpc string,
datadir string,
datype int64,
supportedTraceTypes ...TraceType,
) Config {
return Config{
Expand All @@ -189,6 +197,8 @@ func NewConfig(
AsteriscSnapshotFreq: DefaultAsteriscSnapshotFreq,
AsteriscInfoFreq: DefaultAsteriscInfoFreq,
GameWindow: DefaultGameWindow,

DAType: datype,
}
}

Expand Down
2 changes: 1 addition & 1 deletion op-challenger2/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func applyValidConfigForAsterisc(cfg *Config) {
}

func validConfig(traceType TraceType) Config {
cfg := NewConfig(validGameFactoryAddress, validL1EthRpc, validL1BeaconUrl, validRollupRpc, validL2Rpc, validDatadir, traceType)
cfg := NewConfig(validGameFactoryAddress, validL1EthRpc, validL1BeaconUrl, validRollupRpc, validL2Rpc, validDatadir, DACalldata, traceType)
if traceType == TraceTypeCannon || traceType == TraceTypePermissioned {
applyValidConfigForCannon(&cfg)
}
Expand Down
23 changes: 19 additions & 4 deletions op-challenger2/game/fault/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type Responder interface {
type ClaimLoader interface {
GetAllClaims(ctx context.Context, block rpcblock.Block) ([]types.Claim, error)
IsL2BlockNumberChallenged(ctx context.Context, block rpcblock.Block) (bool, error)
GetMaxGameDepth(ctx context.Context) (types.Depth, error)
GetSplitDepth(ctx context.Context) (types.Depth, error)
GetNBits(ctx context.Context) (uint64, error)
GetMaxAttackBranch(ctx context.Context) (uint64, error)
}

type Agent struct {
Expand All @@ -42,7 +46,6 @@ type Agent struct {
responder Responder
selective bool
claimants []common.Address
maxDepth types.Depth
maxClockDuration time.Duration
log log.Logger
}
Expand All @@ -59,17 +62,17 @@ func NewAgent(
log log.Logger,
selective bool,
claimants []common.Address,
daType types.DAType,
) *Agent {
return &Agent{
metrics: m,
systemClock: systemClock,
l1Clock: l1Clock,
solver: solver.NewGameSolver(maxDepth, trace),
solver: solver.NewGameSolver(maxDepth, trace, daType),
loader: loader,
responder: responder,
selective: selective,
claimants: claimants,
maxDepth: maxDepth,
maxClockDuration: maxClockDuration,
log: log,
}
Expand Down Expand Up @@ -239,6 +242,18 @@ func (a *Agent) newGameFromContracts(ctx context.Context) (types.Game, error) {
if len(claims) == 0 {
return nil, errors.New("no claims")
}
game := types.NewGameState(claims, a.maxDepth)
maxDepth, err := a.loader.GetMaxGameDepth(ctx)
dajuguan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("failed to retrieve max game depth: %w", err)
}
splitDepth, err := a.loader.GetSplitDepth(ctx)
if err != nil {
return nil, fmt.Errorf("failed to retrieve split depth: %w", err)
}
nbits, err := a.loader.GetNBits(ctx)
if err != nil {
return nil, fmt.Errorf("failed to retrieve nbits: %w", err)
}
game := types.NewGameState2(claims, maxDepth, nbits, splitDepth)
return game, nil
}
21 changes: 20 additions & 1 deletion op-challenger2/game/fault/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func setupTestAgent(t *testing.T) (*Agent, *stubClaimLoader, *stubResponder) {
responder := &stubResponder{}
systemClock := clock.NewDeterministicClock(time.UnixMilli(120200))
l1Clock := clock.NewDeterministicClock(l1Time)
agent := NewAgent(metrics.NoopMetrics, systemClock, l1Clock, claimLoader, depth, gameDuration, trace.NewSimpleTraceAccessor(provider), responder, logger, false, []common.Address{})
agent := NewAgent(metrics.NoopMetrics, systemClock, l1Clock, claimLoader, depth, gameDuration, trace.NewSimpleTraceAccessor(provider), responder, logger, false, []common.Address{}, types.CallDataType)
return agent, claimLoader, responder
}

Expand All @@ -204,6 +204,9 @@ type stubClaimLoader struct {
maxLoads int
claims []types.Claim
blockNumChallenged bool
maxDepth types.Depth
splitDepth types.Depth
nbits uint64
}

func (s *stubClaimLoader) IsL2BlockNumberChallenged(_ context.Context, _ rpcblock.Block) (bool, error) {
Expand All @@ -218,6 +221,22 @@ func (s *stubClaimLoader) GetAllClaims(_ context.Context, _ rpcblock.Block) ([]t
return s.claims, nil
}

func (s *stubClaimLoader) GetMaxGameDepth(_ context.Context) (types.Depth, error) {
return s.maxDepth, nil
}

func (s *stubClaimLoader) GetSplitDepth(_ context.Context) (types.Depth, error) {
return s.splitDepth, nil
}

func (s *stubClaimLoader) GetNBits(_ context.Context) (uint64, error) {
return s.nbits, nil
}

func (s *stubClaimLoader) GetMaxAttackBranch(_ context.Context) (uint64, error) {
return 1<<s.nbits - 1, nil
}

type stubResponder struct {
l sync.Mutex
callResolveCount int
Expand Down
4 changes: 2 additions & 2 deletions op-challenger2/game/fault/contracts/faultdisputegame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ func TestFaultDisputeGame_UpdateOracleTx(t *testing.T) {
t.Run(version.version, func(t *testing.T) {
t.Run("Local", func(t *testing.T) {
stubRpc, game := setupFaultDisputeGameTest(t, version)
data := faultTypes.NewPreimageOracleDAData(common.Hash{0x01, 0xbc}.Bytes(), []byte{1, 2, 3, 4, 5, 6, 7}, 16, vmStateDA, outputRootDAItem)
data := faultTypes.NewPreimageOracleDataWithDA(common.Hash{0x01, 0xbc}.Bytes(), []byte{1, 2, 3, 4, 5, 6, 7}, 16, vmStateDA, outputRootDAItem)
claimIdx := uint64(6)
stubRpc.SetResponse(fdgAddr, methodAddLocalData, rpcblock.Latest, []interface{}{
data.GetIdent(),
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestAttackV2Tx(t *testing.T) {
claims[i] = common.Hash{0xaa}[i%32]
}
attackBranch := big.NewInt(0)
daType := big.NewInt(1)
daType := faultTypes.CallDataType
parent := faultTypes.Claim{ClaimData: faultTypes.ClaimData{Value: common.Hash{0xbb}}, ContractIndex: 111}
stubRpc.SetResponse(fdgAddr, methodNBits, rpcblock.Latest, nil, []interface{}{new(big.Int).SetUint64(nBits)})
stubRpc.SetResponse(fdgAddr, methodRequiredBond, rpcblock.Latest, []interface{}{parent.Position.MoveN(nBits, attackBranch.Uint64()).ToGIndex()}, []interface{}{bond})
Expand Down
3 changes: 2 additions & 1 deletion op-challenger2/game/fault/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func NewGamePlayer(
l1HeaderSource L1HeaderSource,
selective bool,
claimants []common.Address,
daType types.DAType,
) (*GamePlayer, error) {
logger = logger.New("game", addr)

Expand Down Expand Up @@ -146,7 +147,7 @@ func NewGamePlayer(
return nil, fmt.Errorf("failed to create the responder: %w", err)
}

agent := NewAgent(m, systemClock, l1Clock, loader, gameDepth, maxClockDuration, accessor, responder, logger, selective, claimants)
agent := NewAgent(m, systemClock, l1Clock, loader, gameDepth, maxClockDuration, accessor, responder, logger, selective, claimants, daType)
return &GamePlayer{
act: agent.Act,
loader: loader,
Expand Down
19 changes: 12 additions & 7 deletions op-challenger2/game/fault/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,25 @@ func RegisterGameTypes(
}
syncValidator := newSyncStatusValidator(rollupClient)

datype := faultTypes.NewDaType(cfg.DAType)

if cfg.TraceTypeEnabled(config.TraceTypeCannon) {
if err := registerCannon(faultTypes.CannonGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants); err != nil {
if err := registerCannon(faultTypes.CannonGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants, datype); err != nil {
return nil, fmt.Errorf("failed to register cannon game type: %w", err)
}
}
if cfg.TraceTypeEnabled(config.TraceTypePermissioned) {
if err := registerCannon(faultTypes.PermissionedGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants); err != nil {
if err := registerCannon(faultTypes.PermissionedGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants, datype); err != nil {
return nil, fmt.Errorf("failed to register permissioned cannon game type: %w", err)
}
}
if cfg.TraceTypeEnabled(config.TraceTypeAsterisc) {
if err := registerAsterisc(faultTypes.AsteriscGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants); err != nil {
if err := registerAsterisc(faultTypes.AsteriscGameType, registry, oracles, ctx, systemClock, l1Clock, logger, m, cfg, syncValidator, rollupClient, txSender, gameFactory, caller, l2Client, l1HeaderSource, selective, claimants, datype); err != nil {
return nil, fmt.Errorf("failed to register asterisc game type: %w", err)
}
}
if cfg.TraceTypeEnabled(config.TraceTypeAlphabet) {
if err := registerAlphabet(registry, oracles, ctx, systemClock, l1Clock, logger, m, syncValidator, rollupClient, l2Client, txSender, gameFactory, caller, l1HeaderSource, selective, claimants); err != nil {
if err := registerAlphabet(registry, oracles, ctx, systemClock, l1Clock, logger, m, syncValidator, rollupClient, l2Client, txSender, gameFactory, caller, l1HeaderSource, selective, claimants, datype); err != nil {
return nil, fmt.Errorf("failed to register alphabet game type: %w", err)
}
}
Expand All @@ -113,6 +115,7 @@ func registerAlphabet(
l1HeaderSource L1HeaderSource,
selective bool,
claimants []common.Address,
daType faultTypes.DAType,
) error {
playerCreator := func(game types.GameMetadata, dir string) (scheduler.GamePlayer, error) {
contract, err := contracts.NewFaultDisputeGameContract(ctx, m, game.Proxy, caller)
Expand Down Expand Up @@ -146,7 +149,7 @@ func registerAlphabet(
}
prestateValidator := NewPrestateValidator("alphabet", contract.GetAbsolutePrestateHash, alphabet.PrestateProvider)
startingValidator := NewPrestateValidator("output root", contract.GetStartingRootHash, prestateProvider)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, startingValidator}, creator, l1HeaderSource, selective, claimants)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, startingValidator}, creator, l1HeaderSource, selective, claimants, daType)
}
err := registerOracle(ctx, m, oracles, gameFactory, caller, faultTypes.AlphabetGameType)
if err != nil {
Expand Down Expand Up @@ -197,6 +200,7 @@ func registerAsterisc(
l1HeaderSource L1HeaderSource,
selective bool,
claimants []common.Address,
daType faultTypes.DAType,
) error {
var prestateSource PrestateSource
if cfg.AsteriscAbsolutePreStateBaseURL != nil {
Expand Down Expand Up @@ -256,7 +260,7 @@ func registerAsterisc(
}
prestateValidator := NewPrestateValidator("asterisc", contract.GetAbsolutePrestateHash, asteriscPrestateProvider)
genesisValidator := NewPrestateValidator("output root", contract.GetStartingRootHash, prestateProvider)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, genesisValidator}, creator, l1HeaderSource, selective, claimants)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, genesisValidator}, creator, l1HeaderSource, selective, claimants, daType)
}
err := registerOracle(ctx, m, oracles, gameFactory, caller, gameType)
if err != nil {
Expand Down Expand Up @@ -290,6 +294,7 @@ func registerCannon(
l1HeaderSource L1HeaderSource,
selective bool,
claimants []common.Address,
daType faultTypes.DAType,
) error {
var prestateSource PrestateSource
if cfg.CannonAbsolutePreStateBaseURL != nil {
Expand Down Expand Up @@ -351,7 +356,7 @@ func registerCannon(
}
prestateValidator := NewPrestateValidator("cannon", contract.GetAbsolutePrestateHash, cannonPrestateProvider)
startingValidator := NewPrestateValidator("output root", contract.GetStartingRootHash, prestateProvider)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, startingValidator}, creator, l1HeaderSource, selective, claimants)
return NewGamePlayer(ctx, systemClock, l1Clock, logger, m, dir, game.Proxy, txSender, contract, syncValidator, []Validator{prestateValidator, startingValidator}, creator, l1HeaderSource, selective, claimants, daType)
}
err := registerOracle(ctx, m, oracles, gameFactory, caller, gameType)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions op-challenger2/game/fault/solver/game_solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type GameSolver struct {
claimSolver *claimSolver
}

func NewGameSolver(gameDepth types.Depth, trace types.TraceAccessor) *GameSolver {
func NewGameSolver(gameDepth types.Depth, trace types.TraceAccessor, daType types.DAType) *GameSolver {
return &GameSolver{
claimSolver: newClaimSolver(gameDepth, trace),
claimSolver: newClaimSolver(gameDepth, trace, daType),
}
}

Expand Down
8 changes: 4 additions & 4 deletions op-challenger2/game/fault/solver/game_solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCalculateNextActions_ChallengeL2BlockNumber(t *testing.T) {
}
claimBuilder := faulttest.NewAlphabetClaimBuilder(t, startingBlock, maxDepth)
traceProvider := faulttest.NewAlphabetWithProofProvider(t, startingBlock, maxDepth, nil)
solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(traceProvider))
solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(traceProvider), types.CallDataType)

// Do not challenge when provider returns error indicating l2 block is valid
actions, err := solver.CalculateNextActions(context.Background(), claimBuilder.GameBuilder().Game)
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestCalculateNextActions(t *testing.T) {
test.setupGame(builder)
game := builder.Game

solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(claimBuilder.CorrectTraceProvider()))
solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(claimBuilder.CorrectTraceProvider()), types.CallDataType)
postState, actions := runStep(t, solver, game, claimBuilder.CorrectTraceProvider())
for i, action := range builder.ExpectedActions {
t.Logf("Expect %v: Type: %v, ParentIdx: %v, Attack: %v, Value: %v, PreState: %v, ProofData: %v",
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestMultipleRounds(t *testing.T) {
game := builder.Game

correctTrace := claimBuilder.CorrectTraceProvider()
solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(correctTrace))
solver := NewGameSolver(maxDepth, trace.NewSimpleTraceAccessor(correctTrace), types.CallDataType)

roundNum := 0
done := false
Expand Down Expand Up @@ -356,5 +356,5 @@ func applyActions(game types.Game, claimant common.Address, actions []types.Acti
panic(fmt.Errorf("unknown move type: %v", action.Type))
}
}
return types.NewGameState(claims, game.MaxDepth())
return types.NewGameState2(claims, game.MaxDepth(), game.NBits(), game.SplitDepth())
}
4 changes: 3 additions & 1 deletion op-challenger2/game/fault/solver/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ var (
type claimSolver struct {
trace types.TraceAccessor
gameDepth types.Depth
daType types.DAType
blockchaindevsh marked this conversation as resolved.
Show resolved Hide resolved
}

// newClaimSolver creates a new [claimSolver] using the provided [TraceProvider].
func newClaimSolver(gameDepth types.Depth, trace types.TraceAccessor) *claimSolver {
func newClaimSolver(gameDepth types.Depth, trace types.TraceAccessor, daType types.DAType) *claimSolver {
return &claimSolver{
trace,
gameDepth,
daType,
}
}

Expand Down
2 changes: 1 addition & 1 deletion op-challenger2/game/fault/solver/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestAttemptStep(t *testing.T) {
t.Run(tableTest.name, func(t *testing.T) {
builder := claimBuilder.GameBuilder(faulttest.WithInvalidValue(tableTest.agreeWithOutputRoot))
tableTest.setupGame(builder)
alphabetSolver := newClaimSolver(maxDepth, trace.NewSimpleTraceAccessor(claimBuilder.CorrectTraceProvider()))
alphabetSolver := newClaimSolver(maxDepth, trace.NewSimpleTraceAccessor(claimBuilder.CorrectTraceProvider()), types.CallDataType)
game := builder.Game
claims := game.Claims()
lastClaim := claims[len(claims)-1]
Expand Down
3 changes: 2 additions & 1 deletion op-challenger2/game/fault/trace/asterisc/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestGenerateProof(t *testing.T) {
input := "starting.json"
tempDir := t.TempDir()
dir := filepath.Join(tempDir, "gameDir")
cfg := config.NewConfig(common.Address{0xbb}, "http://localhost:8888", "http://localhost:9000", "http://localhost:9096", "http://localhost:9095", tempDir, config.TraceTypeAsterisc)
datype := config.DACalldata
cfg := config.NewConfig(common.Address{0xbb}, "http://localhost:8888", "http://localhost:9000", "http://localhost:9096", "http://localhost:9095", tempDir, datype, config.TraceTypeAsterisc)
cfg.L2Rpc = "http://localhost:9999"
prestate := "pre.json"
cfg.AsteriscBin = "./bin/asterisc"
Expand Down
3 changes: 2 additions & 1 deletion op-challenger2/game/fault/trace/cannon/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestGenerateProof(t *testing.T) {
input := "starting.json"
tempDir := t.TempDir()
dir := filepath.Join(tempDir, "gameDir")
cfg := config.NewConfig(common.Address{0xbb}, "http://localhost:8888", "http://localhost:9000", "http://localhost:9096", "http://localhost:9095", tempDir, config.TraceTypeCannon)
datype := config.DACalldata
cfg := config.NewConfig(common.Address{0xbb}, "http://localhost:8888", "http://localhost:9000", "http://localhost:9096", "http://localhost:9095", tempDir, datype, config.TraceTypeCannon)
cfg.L2Rpc = "http://localhost:9999"
prestate := "pre.json"
cfg.CannonBin = "./bin/cannon"
Expand Down
Loading