Skip to content

Commit

Permalink
feat: common launch utilities (#64)
Browse files Browse the repository at this point in the history
* feat: common launch utilities

* fix: create OutDir beforehand
  • Loading branch information
kjessec authored May 3, 2024
1 parent b78d801 commit 53823fe
Show file tree
Hide file tree
Showing 24 changed files with 2,424 additions and 62 deletions.
56 changes: 56 additions & 0 deletions contrib/launchtools/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package launchtools

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func LaunchCmd(
appCreator servertypes.AppCreator,
defaultGenesisGetter func(denom string) map[string]json.RawMessage,
steps []LauncherStepFuncFactory[Input],
) *cobra.Command {
cmd := &cobra.Command{
Use: "launch [path to manifest]",
Short: "Launch a new instance of the app",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)

manifestPath := args[0]
manifest, err := Input{}.FromFile(manifestPath)
if err != nil {
return err
}

launcher := NewLauncher(
cmd,
&clientCtx,
serverCtx,
appCreator,
defaultGenesisGetter(manifest.L2Config.Denom),
)

stepFns := make([]LauncherStepFunc, len(steps))

for stepI, step := range steps {
stepFns[stepI] = step(*manifest)
}

for _, stepFn := range stepFns {
if err := stepFn(launcher); err != nil {
return errors.Wrapf(err, "failed to run launcher step")
}
}

return nil
},
}

return cmd
}
86 changes: 86 additions & 0 deletions contrib/launchtools/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package launchtools

import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"os"
"time"
)

type Input struct {
L2Config L2Config `json:"l2_config"`
L1Config L1Config `json:"l1_config"`
OpBridge OpBridge `json:"op_bridge"`
SystemKeys SystemKeys `json:"system_keys"`
UserKeys []UserKeys `json:"user_keys"`
GenesisAccounts []AccountWithBalance `json:"genesis_accounts"`
}

func (input Input) FromFile(path string) (*Input, error) {
bz, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to read file: %s", path))
}

ret := new(Input)
if err := json.Unmarshal(bz, ret); err != nil {
return nil, err
}
return ret, nil
}

type L2Config struct {
ChainID string `json:"chain_id"`
Denom string `json:"denom"`
Moniker string `json:"moniker"`
AccountPrefix string `json:"account_prefix"`
GasPrices string `json:"gas_prices"`
}

type OpBridge struct {
SubmissionStartTime time.Time `json:"submission_start_time"`
SubmitTarget string `json:"submit_target"`
SubmissionInterval string `json:"submission_interval"`
FinalizationPeriod string `json:"finalization_period"`
}

type L1Config struct {
ChainID string `json:"chain_id"`
RPCURL string `json:"rpc_url"`
Denom string `json:"denom"`
RestURL string `json:"rest_url"`
GrpcURL string `json:"grpc_url"`
WsURL string `json:"ws_url"`
AccountPrefix string `json:"account_prefix"`
GasPrices string `json:"gas_prices"`
}

type Account struct {
Address string `json:"address"`
Mnemonic string `json:"mnemonic"`
}

type AccountWithBalance struct {
Account
Coins string `json:"coins"`
}

type SystemKeys struct {
Validator Account `json:"validator"`
Executor Account `json:"executor"`
Output Account `json:"output"`
Challenger Account `json:"challenger"`
Submitter Account `json:"submitter"`
Relayer Account `json:"relayer"`
}

type UserKeys struct {
Name string `json:"name"`
Address string `json:"address"`
Mnemonic string `json:"mnemonic"`
}

func (i Input) Validate() error {
return nil
}
14 changes: 14 additions & 0 deletions contrib/launchtools/steps/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package steps

import "github.com/initia-labs/OPinit/contrib/launchtools"

// InitializeConfig sets the config for the server context.
func InitializeConfig(manifest launchtools.Input) launchtools.LauncherStepFunc {
return func(ctx launchtools.Launcher) error {
// set config
config := ctx.ServerContext().Config
config.Moniker = manifest.L2Config.Moniker

return nil
}
}
13 changes: 13 additions & 0 deletions contrib/launchtools/steps/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package steps

import "time"

const (
KeyringBackend = "test"
RelayerPathName = "ibc"
RelayerKeyName = "Relayer"
RelayerPathTemp = ".relayer"

CreateEmptyBlocksInterval = 3 * time.Second
OpBridgeIDKey = "bridge_id"
)
Loading

0 comments on commit 53823fe

Please sign in to comment.