@@ -4,81 +4,95 @@ import (
44 "context"
55 "time"
66
7+ "github.com/pkg/errors"
8+ "golang.org/x/sync/errgroup"
9+
710 "github.com/cosmos/cosmos-sdk/client"
811 "github.com/cosmos/cosmos-sdk/server"
12+
913 "github.com/initia-labs/OPinit/contrib/launchtools"
10- "github.com/pkg/errors"
11- "golang.org/x/sync/errgroup"
1214)
1315
1416var _ launchtools.LauncherStepFuncFactory [* launchtools.Config ] = RunApp
1517
1618// RunApp runs the in-process application. This routine temporarily allows creation of empty blocks,
1719// in order to expedite IBC channel establishment. It waits until the app generates at least 1 block after the genesis.
18- func RunApp (_ * launchtools.Config ) launchtools.LauncherStepFunc {
19- return func (ctx launchtools.Launcher ) error {
20- // temporarily allow creation of empty blocks
21- // this should help creation of ibc channels.
22- // NOTE: This part is ephemeral only in the context of the launcher.
23- ctx .ServerContext ().Config .Consensus .CreateEmptyBlocks = true
24- ctx .ServerContext ().Config .Consensus .CreateEmptyBlocksInterval = CreateEmptyBlocksInterval
25-
26- // create a channel to synchronize on app creation
27- var syncDone = make (chan interface {})
28-
29- // create cobra command context
30- startCmd := server .StartCmdWithOptions (
31- ctx .AppCreator (),
32- ctx .ClientContext ().HomeDir ,
33-
34- // set up a post setup function to set the app in the context
35- server.StartCmdOptions {
36- PostSetup : func (svrCtx * server.Context , clientCtx client.Context , _ context.Context , g * errgroup.Group ) (err error ) {
37- // set the error group to gracefully shutdown the launch cmd
38- ctx .SetErrorGroup (g )
39-
40- // wait until latest version goes to 2
41- g .Go (func () error {
42- for {
43- ctx .Logger ().Info ("waiting for app to be created" )
44-
45- if ctx .App ().CommitMultiStore ().LatestVersion () > 1 {
46- // Signal that the app is created
47- syncDone <- struct {}{}
48- break
20+ func RunApp (cfg * launchtools.Config ) launchtools.LauncherStepFunc {
21+ return RunAppWithPostAction (nil )(cfg )
22+ }
23+
24+ // RunAppWithPostAction runs the in-process application with a post action.
25+ func RunAppWithPostAction (postAction launchtools.PostAction ) func (cfg * launchtools.Config ) launchtools.LauncherStepFunc {
26+ return func (cfg * launchtools.Config ) launchtools.LauncherStepFunc {
27+ return func (ctx launchtools.Launcher ) error {
28+ // temporarily allow creation of empty blocks
29+ // this should help creation of ibc channels.
30+ // NOTE: This part is ephemeral only in the context of the launcher.
31+ ctx .ServerContext ().Config .Consensus .CreateEmptyBlocks = true
32+ ctx .ServerContext ().Config .Consensus .CreateEmptyBlocksInterval = CreateEmptyBlocksInterval
33+
34+ // create a channel to synchronize on app creation
35+ var syncDone = make (chan interface {})
36+
37+ // create cobra command context
38+ startCmd := server .StartCmdWithOptions (
39+ ctx .AppCreator (),
40+ ctx .ClientContext ().HomeDir ,
41+
42+ // set up a post setup function to set the app in the context
43+ server.StartCmdOptions {
44+ PostSetup : func (svrCtx * server.Context , clientCtx client.Context , _ctx context.Context , g * errgroup.Group ) (err error ) {
45+ // set the error group to gracefully shutdown the launch cmd
46+ ctx .SetErrorGroup (g )
47+
48+ // wait until latest version goes to 2
49+ g .Go (func () error {
50+ for {
51+ ctx .Logger ().Info ("waiting for app to be created" )
52+
53+ if ctx .App ().CommitMultiStore ().LatestVersion () > 1 {
54+ // Signal that the app is created
55+ syncDone <- struct {}{}
56+ break
57+ }
58+
59+ time .Sleep (1 * time .Second )
4960 }
5061
51- time .Sleep (1 * time .Second )
62+ return nil
63+ })
64+
65+ // if post action is set, run it
66+ if postAction != nil {
67+ return postAction (ctx .App (), svrCtx , clientCtx , _ctx , g )
5268 }
5369
5470 return nil
55- })
56-
57- return nil
71+ },
5872 },
59- },
60- )
73+ )
6174
62- // set relevant context; this part is necessary to correctly set up the start command and their start-up flags
63- startCmd .SetContext (ctx .Context ())
75+ // set relevant context; this part is necessary to correctly set up the start command and their start-up flags
76+ startCmd .SetContext (ctx .Context ())
6477
65- // Run PreRunE from startCmd. This step is necessary to correctly set up start-up flags,
66- // as it is done usually with cometbft start command.
67- if err := startCmd .PreRunE (startCmd , nil ); err != nil {
68- return errors .Wrapf (err , "failed to prerun command" )
69- }
70-
71- // Run RunE command - this part fires up the actual chain
72- // Note that the command is run in a separate goroutine, as it is blocking.
73- // App should be later cleaned up in another launcher step
74- go func () {
75- if err := startCmd .RunE (startCmd , nil ); err != nil {
76- panic (errors .Wrapf (err , "failed to run command" ))
78+ // Run PreRunE from startCmd. This step is necessary to correctly set up start-up flags,
79+ // as it is done usually with cometbft start command.
80+ if err := startCmd .PreRunE (startCmd , nil ); err != nil {
81+ return errors .Wrapf (err , "failed to prerun command" )
7782 }
78- }()
7983
80- <- syncDone
84+ // Run RunE command - this part fires up the actual chain
85+ // Note that the command is run in a separate goroutine, as it is blocking.
86+ // App should be later cleaned up in another launcher step
87+ go func () {
88+ if err := startCmd .RunE (startCmd , nil ); err != nil {
89+ panic (errors .Wrapf (err , "failed to run command" ))
90+ }
91+ }()
92+
93+ <- syncDone
8194
82- return nil
95+ return nil
96+ }
8397 }
8498}
0 commit comments