Skip to content

Commit

Permalink
op-challenger: Extract RegistrationTask to dedeuplicate registration …
Browse files Browse the repository at this point in the history
…code between different game types. (ethereum-optimism#11413)
  • Loading branch information
ajsutton authored Aug 15, 2024
1 parent 46930ea commit cfe3cdd
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 331 deletions.
2 changes: 1 addition & 1 deletion op-challenger/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func CheckRequired(ctx *cli.Context, traceTypes []types.TraceType) error {
}
case types.TraceTypeAlphabet, types.TraceTypeFast:
default:
return fmt.Errorf("invalid trace type. must be one of %v", types.TraceTypes)
return fmt.Errorf("invalid trace type %v. must be one of %v", traceType, types.TraceTypes)
}
}
return nil
Expand Down
314 changes: 11 additions & 303 deletions op-challenger/game/fault/register.go

Large diffs are not rendered by default.

254 changes: 254 additions & 0 deletions op-challenger/game/fault/register_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package fault

import (
"context"
"fmt"
"net/url"
"path/filepath"

"github.com/ethereum-optimism/optimism/op-challenger/config"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/claims"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/contracts"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/alphabet"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/asterisc"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/cannon"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/outputs"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/prestates"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/utils"
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/vm"
faultTypes "github.com/ethereum-optimism/optimism/op-challenger/game/fault/types"
"github.com/ethereum-optimism/optimism/op-challenger/game/scheduler"
"github.com/ethereum-optimism/optimism/op-challenger/game/types"
"github.com/ethereum-optimism/optimism/op-challenger/metrics"
"github.com/ethereum-optimism/optimism/op-service/clock"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum-optimism/optimism/op-service/sources/batching"
"github.com/ethereum-optimism/optimism/op-service/sources/caching"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
)

type RegisterTask struct {
gameType faultTypes.GameType

getPrestateProvider func(prestateHash common.Hash) (faultTypes.PrestateProvider, error)
newTraceAccessor func(
logger log.Logger,
m metrics.Metricer,
l2Client utils.L2HeaderSource,
prestateProvider faultTypes.PrestateProvider,
vmPrestateProvider faultTypes.PrestateProvider,
rollupClient outputs.OutputRollupClient,
dir string,
l1Head eth.BlockID,
splitDepth faultTypes.Depth,
prestateBlock uint64,
poststateBlock uint64) (*trace.Accessor, error)
}

func NewCannonRegisterTask(gameType faultTypes.GameType, cfg *config.Config, m caching.Metrics, serverExecutor vm.OracleServerExecutor) *RegisterTask {
return &RegisterTask{
gameType: gameType,
getPrestateProvider: cachePrestates(
gameType,
m,
cfg.CannonAbsolutePreStateBaseURL,
cfg.CannonAbsolutePreState,
filepath.Join(cfg.Datadir, "cannon-prestates"),
func(path string) faultTypes.PrestateProvider {
return cannon.NewPrestateProvider(path)
}),
newTraceAccessor: func(
logger log.Logger,
m metrics.Metricer,
l2Client utils.L2HeaderSource,
prestateProvider faultTypes.PrestateProvider,
vmPrestateProvider faultTypes.PrestateProvider,
rollupClient outputs.OutputRollupClient,
dir string,
l1Head eth.BlockID,
splitDepth faultTypes.Depth,
prestateBlock uint64,
poststateBlock uint64) (*trace.Accessor, error) {
provider := vmPrestateProvider.(*cannon.CannonPrestateProvider)
return outputs.NewOutputCannonTraceAccessor(logger, m, cfg.Cannon, serverExecutor, l2Client, prestateProvider, provider.PrestatePath(), rollupClient, dir, l1Head, splitDepth, prestateBlock, poststateBlock)
},
}
}

func NewAsteriscRegisterTask(gameType faultTypes.GameType, cfg *config.Config, m caching.Metrics, serverExecutor vm.OracleServerExecutor) *RegisterTask {
return &RegisterTask{
gameType: gameType,
getPrestateProvider: cachePrestates(
gameType,
m,
cfg.AsteriscAbsolutePreStateBaseURL,
cfg.AsteriscAbsolutePreState,
filepath.Join(cfg.Datadir, "asterisc-prestates"),
func(path string) faultTypes.PrestateProvider {
return asterisc.NewPrestateProvider(path)
}),
newTraceAccessor: func(
logger log.Logger,
m metrics.Metricer,
l2Client utils.L2HeaderSource,
prestateProvider faultTypes.PrestateProvider,
vmPrestateProvider faultTypes.PrestateProvider,
rollupClient outputs.OutputRollupClient,
dir string,
l1Head eth.BlockID,
splitDepth faultTypes.Depth,
prestateBlock uint64,
poststateBlock uint64) (*trace.Accessor, error) {
provider := vmPrestateProvider.(*asterisc.AsteriscPreStateProvider)
return outputs.NewOutputAsteriscTraceAccessor(logger, m, cfg.Asterisc, serverExecutor, l2Client, prestateProvider, provider.PrestatePath(), rollupClient, dir, l1Head, splitDepth, prestateBlock, poststateBlock)
},
}
}

func NewAlphabetRegisterTask(gameType faultTypes.GameType) *RegisterTask {
return &RegisterTask{
gameType: gameType,
getPrestateProvider: func(_ common.Hash) (faultTypes.PrestateProvider, error) {
return alphabet.PrestateProvider, nil
},
newTraceAccessor: func(
logger log.Logger,
m metrics.Metricer,
l2Client utils.L2HeaderSource,
prestateProvider faultTypes.PrestateProvider,
vmPrestateProvider faultTypes.PrestateProvider,
rollupClient outputs.OutputRollupClient,
dir string,
l1Head eth.BlockID,
splitDepth faultTypes.Depth,
prestateBlock uint64,
poststateBlock uint64) (*trace.Accessor, error) {
return outputs.NewOutputAlphabetTraceAccessor(logger, m, prestateProvider, rollupClient, l2Client, l1Head, splitDepth, prestateBlock, poststateBlock)
},
}
}

func cachePrestates(
gameType faultTypes.GameType,
m caching.Metrics,
prestateBaseURL *url.URL,
preStatePath string,
prestateDir string,
newPrestateProvider func(path string) faultTypes.PrestateProvider,
) func(prestateHash common.Hash) (faultTypes.PrestateProvider, error) {
prestateSource := prestates.NewPrestateSource(prestateBaseURL, preStatePath, prestateDir)
prestateProviderCache := prestates.NewPrestateProviderCache(m, fmt.Sprintf("prestates-%v", gameType), func(prestateHash common.Hash) (faultTypes.PrestateProvider, error) {
prestatePath, err := prestateSource.PrestatePath(prestateHash)
if err != nil {
return nil, fmt.Errorf("required prestate %v not available: %w", prestateHash, err)
}
return newPrestateProvider(prestatePath), nil
})
return prestateProviderCache.GetOrCreate
}

func (e *RegisterTask) Register(
ctx context.Context,
registry Registry,
oracles OracleRegistry,
systemClock clock.Clock,
l1Clock faultTypes.ClockReader,
logger log.Logger,
m metrics.Metricer,
syncValidator SyncValidator,
rollupClient outputs.OutputRollupClient,
txSender TxSender,
gameFactory *contracts.DisputeGameFactoryContract,
caller *batching.MultiCaller,
l2Client utils.L2HeaderSource,
l1HeaderSource L1HeaderSource,
selective bool,
claimants []common.Address) error {

playerCreator := func(game types.GameMetadata, dir string) (scheduler.GamePlayer, error) {
contract, err := contracts.NewFaultDisputeGameContract(ctx, m, game.Proxy, caller)
if err != nil {
return nil, fmt.Errorf("failed to create fault dispute game contracts: %w", err)
}
requiredPrestatehash, err := contract.GetAbsolutePrestateHash(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load prestate hash for game %v: %w", game.Proxy, err)
}

vmPrestateProvider, err := e.getPrestateProvider(requiredPrestatehash)
if err != nil {
return nil, fmt.Errorf("required prestate %v not available for game %v: %w", requiredPrestatehash, game.Proxy, err)
}

oracle, err := contract.GetOracle(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load oracle for game %v: %w", game.Proxy, err)
}
oracles.RegisterOracle(oracle)
prestateBlock, poststateBlock, err := contract.GetBlockRange(ctx)
if err != nil {
return nil, err
}
splitDepth, err := contract.GetSplitDepth(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load split depth: %w", err)
}
l1HeadID, err := loadL1Head(contract, ctx, l1HeaderSource)
if err != nil {
return nil, err
}
prestateProvider := outputs.NewPrestateProvider(rollupClient, prestateBlock)
creator := func(ctx context.Context, logger log.Logger, gameDepth faultTypes.Depth, dir string) (faultTypes.TraceAccessor, error) {
accessor, err := e.newTraceAccessor(logger, m, l2Client, prestateProvider, vmPrestateProvider, rollupClient, dir, l1HeadID, splitDepth, prestateBlock, poststateBlock)
if err != nil {
return nil, err
}
return accessor, nil
}
prestateValidator := NewPrestateValidator(e.gameType.String(), contract.GetAbsolutePrestateHash, vmPrestateProvider)
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)
}
err := registerOracle(ctx, m, oracles, gameFactory, caller, e.gameType)
if err != nil {
return err
}
registry.RegisterGameType(e.gameType, playerCreator)

contractCreator := func(game types.GameMetadata) (claims.BondContract, error) {
return contracts.NewFaultDisputeGameContract(ctx, m, game.Proxy, caller)
}
registry.RegisterBondContract(e.gameType, contractCreator)
return nil
}

func registerOracle(ctx context.Context, m metrics.Metricer, oracles OracleRegistry, gameFactory *contracts.DisputeGameFactoryContract, caller *batching.MultiCaller, gameType faultTypes.GameType) error {
implAddr, err := gameFactory.GetGameImpl(ctx, gameType)
if err != nil {
return fmt.Errorf("failed to load implementation for game type %v: %w", gameType, err)
}
contract, err := contracts.NewFaultDisputeGameContract(ctx, m, implAddr, caller)
if err != nil {
return fmt.Errorf("failed to create fault dispute game contracts: %w", err)
}
oracle, err := contract.GetOracle(ctx)
if err != nil {
return fmt.Errorf("failed to load oracle address: %w", err)
}
oracles.RegisterOracle(oracle)
return nil
}

func loadL1Head(contract contracts.FaultDisputeGameContract, ctx context.Context, l1HeaderSource L1HeaderSource) (eth.BlockID, error) {
l1Head, err := contract.GetL1Head(ctx)
if err != nil {
return eth.BlockID{}, fmt.Errorf("failed to load L1 head: %w", err)
}
l1Header, err := l1HeaderSource.HeaderByHash(ctx, l1Head)
if err != nil {
return eth.BlockID{}, fmt.Errorf("failed to load L1 header: %w", err)
}
return eth.HeaderBlockID(l1Header), nil
}
4 changes: 4 additions & 0 deletions op-challenger/game/fault/trace/asterisc/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (p *AsteriscPreStateProvider) AbsolutePreStateCommitment(_ context.Context)
p.prestateCommitment = state.StateHash
return state.StateHash, nil
}

func (p *AsteriscPreStateProvider) PrestatePath() string {
return p.prestate
}
2 changes: 1 addition & 1 deletion op-challenger/game/fault/trace/asterisc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func NewTraceProviderForTest(logger log.Logger, m vm.Metricer, cfg *config.Confi
logger: logger,
dir: dir,
prestate: cfg.AsteriscAbsolutePreState,
generator: vm.NewExecutor(logger, m, cfg.Asterisc, vm.NewOpProgramVmConfig(), cfg.AsteriscAbsolutePreState, localInputs),
generator: vm.NewExecutor(logger, m, cfg.Asterisc, vm.NewOpProgramServerExecutor(), cfg.AsteriscAbsolutePreState, localInputs),
gameDepth: gameDepth,
preimageLoader: utils.NewPreimageLoader(kvstore.NewDiskKV(vm.PreimageDir(dir)).Get),
}
Expand Down
4 changes: 4 additions & 0 deletions op-challenger/game/fault/trace/cannon/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ func (p *CannonPrestateProvider) AbsolutePreStateCommitment(_ context.Context) (
p.prestateCommitment = hash
return hash, nil
}

func (p *CannonPrestateProvider) PrestatePath() string {
return p.prestate
}
2 changes: 1 addition & 1 deletion op-challenger/game/fault/trace/cannon/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func NewTraceProviderForTest(logger log.Logger, m vm.Metricer, cfg *config.Confi
logger: logger,
dir: dir,
prestate: cfg.CannonAbsolutePreState,
generator: vm.NewExecutor(logger, m, cfg.Cannon, vm.NewOpProgramVmConfig(), cfg.CannonAbsolutePreState, localInputs),
generator: vm.NewExecutor(logger, m, cfg.Cannon, vm.NewOpProgramServerExecutor(), cfg.CannonAbsolutePreState, localInputs),
gameDepth: gameDepth,
preimageLoader: utils.NewPreimageLoader(kvstore.NewDiskKV(vm.PreimageDir(dir)).Get),
}
Expand Down
4 changes: 2 additions & 2 deletions op-challenger/game/fault/trace/outputs/output_cannon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewOutputCannonTraceAccessor(
logger log.Logger,
m metrics.Metricer,
cfg vm.Config,
vmCfg vm.OracleServerExecutor,
serverExecutor vm.OracleServerExecutor,
l2Client utils.L2HeaderSource,
prestateProvider types.PrestateProvider,
cannonPrestate string,
Expand All @@ -41,7 +41,7 @@ func NewOutputCannonTraceAccessor(
if err != nil {
return nil, fmt.Errorf("failed to fetch cannon local inputs: %w", err)
}
provider := cannon.NewTraceProvider(logger, m, cfg, vmCfg, prestateProvider, cannonPrestate, localInputs, subdir, depth)
provider := cannon.NewTraceProvider(logger, m, cfg, serverExecutor, prestateProvider, cannonPrestate, localInputs, subdir, depth)
return provider, nil
}

Expand Down
2 changes: 1 addition & 1 deletion op-challenger/game/fault/trace/vm/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGenerateProof(t *testing.T) {
}
captureExec := func(t *testing.T, cfg Config, proofAt uint64) (string, string, map[string]string) {
m := &stubVmMetrics{}
executor := NewExecutor(testlog.Logger(t, log.LevelInfo), m, cfg, NewOpProgramVmConfig(), prestate, inputs)
executor := NewExecutor(testlog.Logger(t, log.LevelInfo), m, cfg, NewOpProgramServerExecutor(), prestate, inputs)
executor.selectSnapshot = func(logger log.Logger, dir string, absolutePreState string, i uint64) (string, error) {
return input, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
)

type KonaVmConfig struct {
type KonaServerExecutor struct {
}

var _ OracleServerExecutor = (*KonaVmConfig)(nil)
var _ OracleServerExecutor = (*KonaServerExecutor)(nil)

func NewKonaVmConfig() *KonaVmConfig {
return &KonaVmConfig{}
func NewKonaServerExecutor() *KonaServerExecutor {
return &KonaServerExecutor{}
}

func (s *KonaVmConfig) OracleCommand(cfg Config, dataDir string, inputs utils.LocalGameInputs) ([]string, error) {
func (s *KonaServerExecutor) OracleCommand(cfg Config, dataDir string, inputs utils.LocalGameInputs) ([]string, error) {
if cfg.Network == "" {
return nil, errors.New("network is not defined")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestKonaFillHostCommand(t *testing.T) {
L2Claim: common.Hash{0x44},
L2BlockNumber: big.NewInt(3333),
}
vmConfig := NewKonaVmConfig()
vmConfig := NewKonaServerExecutor()

args, err := vmConfig.OracleCommand(cfg, dir, inputs)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"github.com/ethereum-optimism/optimism/op-challenger/game/fault/trace/utils"
)

type OpProgramVmConfig struct {
type OpProgramServerExecutor struct {
}

var _ OracleServerExecutor = (*OpProgramVmConfig)(nil)
var _ OracleServerExecutor = (*OpProgramServerExecutor)(nil)

func NewOpProgramVmConfig() *OpProgramVmConfig {
return &OpProgramVmConfig{}
func NewOpProgramServerExecutor() *OpProgramServerExecutor {
return &OpProgramServerExecutor{}
}

func (s *OpProgramVmConfig) OracleCommand(cfg Config, dataDir string, inputs utils.LocalGameInputs) ([]string, error) {
func (s *OpProgramServerExecutor) OracleCommand(cfg Config, dataDir string, inputs utils.LocalGameInputs) ([]string, error) {
args := []string{
cfg.Server, "--server",
"--l1", cfg.L1,
Expand Down
Loading

0 comments on commit cfe3cdd

Please sign in to comment.