genesis

package
v1.5.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: MIT, MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Subcommands = cli.Commands{
	{
		Name:  "l1",
		Usage: "Generates a L1 genesis state file",
		Flags: l1Flags,
		Action: func(ctx *cli.Context) error {
			deployConfig := ctx.String("deploy-config")
			config, err := genesis.NewDeployConfig(deployConfig)
			if err != nil {
				return err
			}

			var deployments *genesis.L1Deployments
			if l1Deployments := ctx.String("l1-deployments"); l1Deployments != "" {
				deployments, err = genesis.NewL1Deployments(l1Deployments)
				if err != nil {
					return err
				}
			}

			if deployments != nil {
				config.SetDeployments(deployments)
			}

			if err := config.Check(); err != nil {
				return fmt.Errorf("deploy config at %s invalid: %w", deployConfig, err)
			}

			if err := config.CheckAddresses(); err != nil {
				return fmt.Errorf("deploy config at %s invalid: %w", deployConfig, err)
			}

			var dump *state.Dump
			if l1Allocs := ctx.String("l1-allocs"); l1Allocs != "" {
				dump, err = genesis.NewStateDump(l1Allocs)
				if err != nil {
					return err
				}
			}

			l1Genesis, err := genesis.BuildL1DeveloperGenesis(config, dump, deployments, true)
			if err != nil {
				return err
			}

			return writeJSONFile(ctx.String("outfile.l1"), l1Genesis)
		},
	},
	{
		Name:  "l2",
		Usage: "Generates an L2 genesis file and rollup config suitable for a deployed network",
		Description: "Generating the L2 genesis depends on knowledge of L1 contract addresses for the bridge to be secure. " +
			"A deploy config and either a deployment directory or an L1 deployments file are used to create the L2 genesis. " +
			"The deploy directory and L1 deployments file are generated by the L1 contract deployments. " +
			"An L1 starting block is necessary, it can either be fetched dynamically using config in the deploy config " +
			"or it can be provided as a JSON file.",
		Flags: l2Flags,
		Action: func(ctx *cli.Context) error {
			deployConfig := ctx.Path("deploy-config")
			log.Info("Deploy config", "path", deployConfig)
			config, err := genesis.NewDeployConfig(deployConfig)
			if err != nil {
				return err
			}

			deployDir := ctx.Path("deployment-dir")
			l1Deployments := ctx.Path("l1-deployments")

			if deployDir != "" && l1Deployments != "" {
				return errors.New("cannot specify both --deployment-dir and --l1-deployments")
			}
			if deployDir == "" && l1Deployments == "" {
				return errors.New("must specify either --deployment-dir or --l1-deployments")
			}

			l1StartBlockPath := ctx.Path("l1-starting-block")
			l1RPC := ctx.String("l1-rpc")

			if l1StartBlockPath == "" && l1RPC == "" {
				return errors.New("must specify either --l1-starting-block or --l1-rpc")
			}
			if l1StartBlockPath != "" && l1RPC != "" {
				return errors.New("cannot specify both --l1-starting-block and --l1-rpc")
			}

			if deployDir != "" {
				log.Info("Deployment directory", "path", deployDir)
				depPath, network := filepath.Split(deployDir)
				hh, err := hardhat.New(network, nil, []string{depPath})
				if err != nil {
					return err
				}

				if err := config.GetDeployedAddresses(hh); err != nil {
					return err
				}
			}

			if l1Deployments != "" {
				deployments, err := genesis.NewL1Deployments(l1Deployments)
				if err != nil {
					return fmt.Errorf("cannot read L1 deployments at %s: %w", l1Deployments, err)
				}
				config.SetDeployments(deployments)
			}

			var l1StartBlock *types.Block
			if l1StartBlockPath != "" {
				if l1StartBlock, err = readBlockJSON(l1StartBlockPath); err != nil {
					return fmt.Errorf("cannot read L1 starting block at %s: %w", l1StartBlockPath, err)
				}
			}

			if l1RPC != "" {
				client, err := ethclient.Dial(l1RPC)
				if err != nil {
					return fmt.Errorf("cannot dial %s: %w", l1RPC, err)
				}

				if config.L1StartingBlockTag == nil {
					l1StartBlock, err = client.BlockByNumber(context.Background(), nil)
					if err != nil {
						return fmt.Errorf("cannot fetch latest block: %w", err)
					}
					tag := rpc.BlockNumberOrHashWithHash(l1StartBlock.Hash(), true)
					config.L1StartingBlockTag = (*genesis.MarshalableRPCBlockNumberOrHash)(&tag)
				} else if config.L1StartingBlockTag.BlockHash != nil {
					l1StartBlock, err = client.BlockByHash(context.Background(), *config.L1StartingBlockTag.BlockHash)
					if err != nil {
						return fmt.Errorf("cannot fetch block by hash: %w", err)
					}
				} else if config.L1StartingBlockTag.BlockNumber != nil {
					l1StartBlock, err = client.BlockByNumber(context.Background(), big.NewInt(config.L1StartingBlockTag.BlockNumber.Int64()))
					if err != nil {
						return fmt.Errorf("cannot fetch block by number: %w", err)
					}
				}
			}

			if l1StartBlock == nil {
				return errors.New("no starting L1 block")
			}

			if err := config.Check(); err != nil {
				return err
			}

			log.Info("Using L1 Start Block", "number", l1StartBlock.Number(), "hash", l1StartBlock.Hash().Hex())

			l2Genesis, err := genesis.BuildL2Genesis(config, l1StartBlock)
			if err != nil {
				return fmt.Errorf("error creating l2 genesis: %w", err)
			}

			l2GenesisBlock := l2Genesis.ToBlock()
			rollupConfig, err := config.RollupConfig(l1StartBlock, l2GenesisBlock.Hash(), l2GenesisBlock.Number().Uint64())
			if err != nil {
				return err
			}
			if err := rollupConfig.Check(); err != nil {
				return fmt.Errorf("generated rollup config does not pass validation: %w", err)
			}

			if err := writeJSONFile(ctx.String("outfile.l2"), l2Genesis); err != nil {
				return err
			}
			return writeJSONFile(ctx.String("outfile.rollup"), rollupConfig)
		},
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL