|
| 1 | +package interop |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ethereum-optimism/optimism/op-node/rollup" |
| 8 | + "github.com/ethereum-optimism/optimism/op-program/client/boot" |
| 9 | + "github.com/ethereum-optimism/optimism/op-program/client/claim" |
| 10 | + "github.com/ethereum-optimism/optimism/op-program/client/interop/types" |
| 11 | + "github.com/ethereum-optimism/optimism/op-program/client/l1" |
| 12 | + "github.com/ethereum-optimism/optimism/op-program/client/l2" |
| 13 | + "github.com/ethereum-optimism/optimism/op-program/client/tasks" |
| 14 | + "github.com/ethereum-optimism/optimism/op-service/eth" |
| 15 | + "github.com/ethereum/go-ethereum/common" |
| 16 | + "github.com/ethereum/go-ethereum/log" |
| 17 | + "github.com/ethereum/go-ethereum/params" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + ErrIncorrectOutputRootType = errors.New("incorrect output root type") |
| 22 | +) |
| 23 | + |
| 24 | +type taskExecutor interface { |
| 25 | + RunDerivation( |
| 26 | + logger log.Logger, |
| 27 | + rollupCfg *rollup.Config, |
| 28 | + l2ChainConfig *params.ChainConfig, |
| 29 | + l1Head common.Hash, |
| 30 | + agreedOutputRoot eth.Bytes32, |
| 31 | + claimedBlockNumber uint64, |
| 32 | + l1Oracle l1.Oracle, |
| 33 | + l2Oracle l2.Oracle) (tasks.DerivationResult, error) |
| 34 | +} |
| 35 | + |
| 36 | +func RunInteropProgram(logger log.Logger, bootInfo *boot.BootInfo, l1PreimageOracle l1.Oracle, l2PreimageOracle l2.Oracle, validate bool) error { |
| 37 | + return runInteropProgram(logger, bootInfo, l1PreimageOracle, l2PreimageOracle, validate, &interopTaskExecutor{}) |
| 38 | +} |
| 39 | + |
| 40 | +func runInteropProgram(logger log.Logger, bootInfo *boot.BootInfo, l1PreimageOracle l1.Oracle, l2PreimageOracle l2.Oracle, validate bool, tasks taskExecutor) error { |
| 41 | + logger.Info("Interop Program Bootstrapped", "bootInfo", bootInfo) |
| 42 | + |
| 43 | + // For the first step in a timestamp, we would get a SuperRoot as the agreed claim - TransitionStateByRoot will |
| 44 | + // automatically convert it to a TransitionState with Step: 0. |
| 45 | + transitionState := l2PreimageOracle.TransitionStateByRoot(bootInfo.L2OutputRoot) |
| 46 | + if transitionState.Version() != types.IntermediateTransitionVersion { |
| 47 | + return fmt.Errorf("%w: %v", ErrIncorrectOutputRootType, transitionState.Version()) |
| 48 | + } |
| 49 | + |
| 50 | + super, err := eth.UnmarshalSuperRoot(transitionState.SuperRoot) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("invalid super root: %w", err) |
| 53 | + } |
| 54 | + if super.Version() != eth.SuperRootVersionV1 { |
| 55 | + return fmt.Errorf("%w: %v", ErrIncorrectOutputRootType, super.Version()) |
| 56 | + } |
| 57 | + superRoot := super.(*eth.SuperV1) |
| 58 | + claimedBlockNumber, err := bootInfo.RollupConfig.TargetBlockNumber(superRoot.Timestamp + 1) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + derivationResult, err := tasks.RunDerivation( |
| 63 | + logger, |
| 64 | + bootInfo.RollupConfig, |
| 65 | + bootInfo.L2ChainConfig, |
| 66 | + bootInfo.L1Head, |
| 67 | + superRoot.Chains[0].Output, |
| 68 | + claimedBlockNumber, |
| 69 | + l1PreimageOracle, |
| 70 | + l2PreimageOracle, |
| 71 | + ) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + newPendingProgress := make([]types.OptimisticBlock, len(transitionState.PendingProgress)+1) |
| 77 | + copy(newPendingProgress, transitionState.PendingProgress) |
| 78 | + newPendingProgress[len(newPendingProgress)-1] = types.OptimisticBlock{ |
| 79 | + BlockHash: derivationResult.BlockHash, |
| 80 | + OutputRoot: derivationResult.OutputRoot, |
| 81 | + } |
| 82 | + finalState := &types.TransitionState{ |
| 83 | + SuperRoot: transitionState.SuperRoot, |
| 84 | + PendingProgress: newPendingProgress, |
| 85 | + Step: transitionState.Step + 1, |
| 86 | + } |
| 87 | + expected, err := finalState.Hash() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if !validate { |
| 92 | + return nil |
| 93 | + } |
| 94 | + return claim.ValidateClaim(logger, derivationResult.SafeHead, eth.Bytes32(bootInfo.L2Claim), eth.Bytes32(expected)) |
| 95 | +} |
| 96 | + |
| 97 | +type interopTaskExecutor struct { |
| 98 | +} |
| 99 | + |
| 100 | +func (t *interopTaskExecutor) RunDerivation( |
| 101 | + logger log.Logger, |
| 102 | + rollupCfg *rollup.Config, |
| 103 | + l2ChainConfig *params.ChainConfig, |
| 104 | + l1Head common.Hash, |
| 105 | + agreedOutputRoot eth.Bytes32, |
| 106 | + claimedBlockNumber uint64, |
| 107 | + l1Oracle l1.Oracle, |
| 108 | + l2Oracle l2.Oracle) (tasks.DerivationResult, error) { |
| 109 | + return tasks.RunDerivation( |
| 110 | + logger, |
| 111 | + rollupCfg, |
| 112 | + l2ChainConfig, |
| 113 | + l1Head, |
| 114 | + common.Hash(agreedOutputRoot), |
| 115 | + claimedBlockNumber, |
| 116 | + l1Oracle, |
| 117 | + l2Oracle) |
| 118 | +} |
0 commit comments