Skip to content

Commit

Permalink
minor fixes with graceful shutdown (#66)
Browse files Browse the repository at this point in the history
* feat: common launch utilities

* fix: create OutDir beforehand

* minor fix with graceful shutdown

* merge

---------

Co-authored-by: Jesse Chung <[email protected]>
  • Loading branch information
beer-1 and kjessec authored May 3, 2024
1 parent 53823fe commit 63d56f6
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 62 deletions.
5 changes: 3 additions & 2 deletions contrib/launchtools/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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,
appCreator AppCreator,
defaultGenesisGetter func(denom string) map[string]json.RawMessage,
steps []LauncherStepFuncFactory[Input],
) *cobra.Command {
Expand Down
17 changes: 7 additions & 10 deletions contrib/launchtools/steps/genesis.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package steps

import (
"cosmossdk.io/log"
"encoding/json"
"fmt"
"strings"

"cosmossdk.io/log"
"github.com/cometbft/cometbft/config"
cometos "github.com/cometbft/cometbft/libs/os"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -18,7 +20,6 @@ import (
"github.com/initia-labs/OPinit/contrib/launchtools"
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
"github.com/pkg/errors"
"strings"
)

type (
Expand Down Expand Up @@ -219,6 +220,9 @@ func initializeGenesis(
appGenesis.AppVersion = version.Version
appGenesis.ChainID = manifest.L2Config.ChainID
appGenesis.AppState, err = json.MarshalIndent(genesisAppState, "", " ")
if err != nil {
return nil, errors.Wrap(err, "failed to marshal app state")
}

// validate genesis
if err := appGenesis.ValidateAndComplete(); err != nil {
Expand Down Expand Up @@ -298,11 +302,6 @@ func addGenesisValidator(
opChildState := opchildtypes.GetGenesisStateFromAppState(cdc, genesisAppState)
opChildState.Validators = append((*opChildState).Validators, validator)

// initialize admin and bridge executor if not set
// default: validator
if opChildState.Params.Admin == "" {
opChildState.Params.Admin = sdk.AccAddress(valPubKey.Address()).String()
}
return opChildState, nil
}

Expand All @@ -311,9 +310,7 @@ func addFeeWhitelists(cdc codec.Codec, genesisAppState map[string]json.RawMessag
error,
) {
opchildState := opchildtypes.GetGenesisStateFromAppState(cdc, genesisAppState)
for _, w := range whitelistAddrs {
opchildState.Params.FeeWhitelist = append(opchildState.Params.FeeWhitelist, w)
}
opchildState.Params.FeeWhitelist = append(opchildState.Params.FeeWhitelist, whitelistAddrs...)

return opchildState, nil
}
Expand Down
13 changes: 7 additions & 6 deletions contrib/launchtools/steps/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package steps

import (
"context"
"cosmossdk.io/log"
"encoding/json"
"fmt"
"os"
"path"
"reflect"

"cosmossdk.io/log"
relayercmd "github.com/cosmos/relayer/v2/cmd"
relayertypes "github.com/cosmos/relayer/v2/relayer"
relayerconfig "github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/initia-labs/OPinit/contrib/launchtools"
"github.com/pkg/errors"
"os"
"path"
"reflect"
)

// EstablishIBCChannelsWithNFTTransfer creates a new IBC channel for fungible transfer, and one with NFT transfer
Expand Down Expand Up @@ -42,7 +43,7 @@ func establishIBCChannels(
initializeConfig,
initializeChains(input, relayerPath),
initializePaths(input, relayerPath),
initializeRelayerKeyring(input, relayerPath),
initializeRelayerKeyring(input),

// create default transfer ports
link,
Expand Down Expand Up @@ -191,7 +192,7 @@ func initializePaths(input launchtools.Input, basePath string) func(*Relayer) er

// initializeRelayerKeyring initializes the keyring for the relayer
// cosmos/relayer uses its own keyring to manage keys. for this, we need to restore the relayer key
func initializeRelayerKeyring(input launchtools.Input, basePath string) func(*Relayer) error {
func initializeRelayerKeyring(input launchtools.Input) func(*Relayer) error {
relayerKeyFromInput := reflect.ValueOf(input.SystemKeys).FieldByName(RelayerKeyName)
if !relayerKeyFromInput.IsValid() {
panic(errors.New("relayer key not found in input"))
Expand Down
4 changes: 3 additions & 1 deletion contrib/launchtools/steps/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func InitializeRPCHelpers(input launchtools.Input) launchtools.LauncherStepFunc
ctx.ClientContext().InterfaceRegistry,
ctx.ClientContext().TxConfig,
)

if err != nil {
return errors.Wrapf(err, "failed to create RPC client for L1")
}
Expand All @@ -44,6 +43,9 @@ func InitializeRPCHelpers(input launchtools.Input) launchtools.LauncherStepFunc
ctx.ClientContext().InterfaceRegistry,
ctx.ClientContext().TxConfig,
)
if err != nil {
return errors.Wrapf(err, "failed to create RPC client for L2")
}

ctx.SetRPCHelpers(l1, l2)

Expand Down
25 changes: 13 additions & 12 deletions contrib/launchtools/steps/runapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package steps

import (
"context"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/initia-labs/OPinit/contrib/launchtools"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"time"
)

// RunApp runs the in-process application. This routine temporarily allows creation of empty blocks,
Expand All @@ -31,25 +31,26 @@ func RunApp(_ launchtools.Input) launchtools.LauncherStepFunc {

// set up a post setup function to set the app in the context
server.StartCmdOptions{
PostSetup: func(svrCtx *server.Context, clientCtx client.Context, _ context.Context, _ *errgroup.Group, app servertypes.Application) (err error) {
// Register the app in the context
if err := ctx.SetApp(app); err != nil {
return errors.Wrap(err, "failed to set app in launch context")
}

// wait until latest versioin goes to 2
go func() {
PostSetup: func(svrCtx *server.Context, clientCtx client.Context, _ context.Context, g *errgroup.Group) (err error) {
// set the error group to gracefully shutdown the launch cmd
ctx.SetErrorGroup(g)

// wait until latest version goes to 2
g.Go(func() error {
for {
ctx.Logger().Info("waiting for app to be created")

if app.CommitMultiStore().LatestVersion() > 1 {
if ctx.App().CommitMultiStore().LatestVersion() > 1 {
// Signal that the app is created
syncDone <- struct{}{}
break
}

time.Sleep(1 * time.Second)
}
}()

return nil
})

return nil
},
Expand Down
9 changes: 9 additions & 0 deletions contrib/launchtools/steps/stopapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package steps

import (
"errors"
"syscall"

launchertypes "github.com/initia-labs/OPinit/contrib/launchtools"
)

Expand All @@ -13,6 +15,13 @@ func StopApp(_ launchertypes.Input) launchertypes.LauncherStepFunc {

log := ctx.Logger()
log.Info("cleanup")
log.Info("waiting for app to stop")

syscall.Kill(syscall.Getpid(), syscall.SIGINT)

// wait for the app to stop
ctx.GetErrorGroup().Wait()
log.Info("cleanup finished")

return nil
}
Expand Down
58 changes: 31 additions & 27 deletions contrib/launchtools/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package launchtools

import (
"context"
"cosmossdk.io/log"
"encoding/json"
"os"
"path"
"sync"

"cosmossdk.io/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand All @@ -16,9 +20,7 @@ import (
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"os"
"path"
"sync"
"golang.org/x/sync/errgroup"
)

const (
Expand All @@ -33,9 +35,6 @@ type LauncherStepFuncFactory[Manifest any] func(Manifest) LauncherStepFunc
// LauncherStepFunc is a function that takes a launcher and returns an error.
type LauncherStepFunc func(ctx Launcher) error

// LauncherCleanupFunc is a function that cleans up the launcher.
type LauncherCleanupFunc func() error

// ExpectedApp is an extended interface that allows to get the ibc keeper from the app.
type ExpectedApp interface {
servertypes.Application
Expand All @@ -44,15 +43,17 @@ type ExpectedApp interface {
GetIBCKeeper() *ibckeeper.Keeper
}

type AppCreator interface {
AppCreator() servertypes.AppCreator
App() servertypes.Application
}

// Launcher is an interface that provides the necessary methods to interact with the launcher.
// It is used to abstract away the underlying contexts, and provide a non-intrusive way to interact with the launcher.
type Launcher interface {
// IsAppInitialized returns true if the app is initialized.
IsAppInitialized() bool

// SetApp sets the app to the launcher.
SetApp(app servertypes.Application) error

// App returns the app. Must only be called after SetApp is called.
App() ExpectedApp

Expand Down Expand Up @@ -85,6 +86,10 @@ type Launcher interface {
GetRPCHelperL1() *utils.RPCHelper
GetRPCHelperL2() *utils.RPCHelper

// SetErrorGroup sets the error group.
SetErrorGroup(g *errgroup.Group)
GetErrorGroup() *errgroup.Group

// WriteToFile writes data to a file under $HOME/out.
WriteToFile(filename string, data string) error
}
Expand All @@ -95,25 +100,26 @@ type LauncherContext struct {
mtx *sync.Mutex

log log.Logger
app ExpectedApp
cleanupFns []LauncherCleanupFunc
defaultGenesis map[string]json.RawMessage

appCreator servertypes.AppCreator
appCreator AppCreator
clientCtx *client.Context
serverCtx *server.Context

cmd *cobra.Command

rpcHelperL1 *utils.RPCHelper
rpcHelperL2 *utils.RPCHelper

// errorgroup is used to manage the lifecycle of the app.
errorgroup *errgroup.Group
}

func NewLauncher(
cmd *cobra.Command,
clientCtx *client.Context,
serverCtx *server.Context,
appCreator servertypes.AppCreator,
appCreator AppCreator,
defaultGenesis map[string]json.RawMessage,
) *LauncherContext {
kr, err := keyring.New("minitia", keyring.BackendTest, clientCtx.HomeDir, nil, clientCtx.Codec)
Expand Down Expand Up @@ -148,25 +154,15 @@ func NewLauncher(
}

func (l *LauncherContext) IsAppInitialized() bool {
return l.app != nil
}

func (l *LauncherContext) SetApp(app servertypes.Application) error {
nextApp, ok := app.(ExpectedApp)
if !ok {
return errors.New("supplied app does not implement expected methods")
}

l.app = nextApp
return nil
return l.appCreator.App() != nil
}

func (l *LauncherContext) App() ExpectedApp {
return l.app
return l.appCreator.App().(ExpectedApp)
}

func (l *LauncherContext) AppCreator() servertypes.AppCreator {
return l.appCreator
return l.appCreator.AppCreator()
}

func (l *LauncherContext) Logger() log.Logger {
Expand Down Expand Up @@ -224,3 +220,11 @@ func (l *LauncherContext) WriteToFile(filename string, data string) error {

return nil
}

func (l *LauncherContext) SetErrorGroup(g *errgroup.Group) {
l.errorgroup = g
}

func (l *LauncherContext) GetErrorGroup() *errgroup.Group {
return l.errorgroup
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ require (

replace github.com/initia-labs/OPinit/api => ./api

replace github.com/cosmos/cosmos-sdk => github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d

replace (
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ github.com/cosmos/cosmos-db v1.0.2 h1:hwMjozuY1OlJs/uh6vddqnk9j7VamLv+0DBlbEXbAK
github.com/cosmos/cosmos-db v1.0.2/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/cosmos-sdk v0.50.6 h1:efR3MsvMHX5sxS3be+hOobGk87IzlZbSpsI2x/Vw3hk=
github.com/cosmos/cosmos-sdk v0.50.6/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down Expand Up @@ -777,8 +779,6 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d h1:8OCL+PBoWydjxP07wE567ySQfY8gbu+rjE04OCNx/9U=
github.com/initia-labs/cosmos-sdk v0.0.0-20240425031032-6bc18cf6e67d/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/initia-labs/slinky v0.0.0-20240418051646-d45167cc66b1 h1:7mXLhI/X+GLdAYNmzXE5CIvTuOgETUMB7tR6VHvX/tY=
github.com/initia-labs/slinky v0.0.0-20240418051646-d45167cc66b1/go.mod h1:ZK3DD0WLDIcBkyo8w2mr/KiIqCovVBNx82jSRhC2DTA=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosmos/cosmos-db v1.0.0/go.mod h1:iBvi1TtqaedwLdcrZVYRSSCb6eSy61NLj4UNmdIgs0U=
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
github.com/cosmos/cosmos-sdk v0.50.6 h1:efR3MsvMHX5sxS3be+hOobGk87IzlZbSpsI2x/Vw3hk=
github.com/cosmos/cosmos-sdk v0.50.6/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/cosmos/gogoproto v1.4.3/go.mod h1:0hLIG5TR7IvV1fme1HCFKjfzW9X2x0Mo+RooWXCnOWU=
github.com/cosmos/gogoproto v1.4.11/go.mod h1:/g39Mh8m17X8Q/GDEs5zYTSNaNnInBSohtaxzQnYq1Y=
github.com/cosmos/iavl v1.1.1/go.mod h1:jLeUvm6bGT1YutCaL2fIar/8vGUE8cPZvh/gXEWDaDM=
Expand Down

0 comments on commit 63d56f6

Please sign in to comment.