Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
refactor: simpify the taiko-client choose process
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshliu committed Sep 20, 2023
1 parent 2f2007d commit 381941f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 deletions.
9 changes: 3 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (

"github.com/taikoxyz/taiko-client/cmd/flags"
"github.com/taikoxyz/taiko-client/cmd/utils"
"github.com/taikoxyz/taiko-client/driver"
"github.com/taikoxyz/taiko-client/proposer"
"github.com/taikoxyz/taiko-client/prover"
"github.com/taikoxyz/taiko-client/version"
"github.com/urfave/cli/v2"
)
Expand All @@ -31,21 +28,21 @@ func main() {
Flags: flags.DriverFlags,
Usage: "Starts the driver software",
Description: "Taiko driver software",
Action: utils.SubcommandAction(new(driver.Driver)),
Action: utils.StartServer,
},
{
Name: "proposer",
Flags: flags.ProposerFlags,
Usage: "Starts the proposer software",
Description: "Taiko proposer software",
Action: utils.SubcommandAction(new(proposer.Proposer)),
Action: utils.StartServer,
},
{
Name: "prover",
Flags: flags.ProverFlags,
Usage: "Starts the prover software",
Description: "Taiko prover software",
Action: utils.SubcommandAction(new(prover.Prover)),
Action: utils.StartServer,
},
}

Expand Down
98 changes: 54 additions & 44 deletions cmd/utils/sub_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,65 @@ import (

"github.com/ethereum/go-ethereum/log"
"github.com/taikoxyz/taiko-client/cmd/logger"
"github.com/taikoxyz/taiko-client/driver"
"github.com/taikoxyz/taiko-client/metrics"
"github.com/taikoxyz/taiko-client/node"
"github.com/taikoxyz/taiko-client/proposer"
"github.com/taikoxyz/taiko-client/prover"
"github.com/urfave/cli/v2"
)

type SubcommandApplication interface {
InitFromCli(context.Context, *cli.Context) error
Name() string
Start() error
Close(context.Context)
func StartServer(c *cli.Context) error {
logger.InitLogger(c)

s := getServer(c)
ctx, ctxClose := context.WithCancel(context.Background())
defer func() { ctxClose() }()

if err := s.InitFromCli(ctx, c); err != nil {
return err
}

log.Info("Starting Taiko client application", "name", s.Name())

if err := s.Start(); err != nil {
log.Error("Starting application error", "name", s.Name(), "error", err)
return err
}

if err := metrics.Serve(ctx, c); err != nil {
log.Error("Starting metrics server error", "error", err)
return err
}

defer func() {
ctxClose()
s.Close(ctx)
log.Info("Application stopped", "name", s.Name())
}()

quitCh := make(chan os.Signal, 1)
signal.Notify(quitCh, []os.Signal{
os.Interrupt,
os.Kill,
syscall.SIGTERM,
syscall.SIGQUIT,
}...)
<-quitCh

return nil
}

func SubcommandAction(app SubcommandApplication) cli.ActionFunc {
return func(c *cli.Context) error {
logger.InitLogger(c)

ctx, ctxClose := context.WithCancel(context.Background())
defer func() { ctxClose() }()

if err := app.InitFromCli(ctx, c); err != nil {
return err
}

log.Info("Starting Taiko client application", "name", app.Name())

if err := app.Start(); err != nil {
log.Error("Starting application error", "name", app.Name(), "error", err)
return err
}

if err := metrics.Serve(ctx, c); err != nil {
log.Error("Starting metrics server error", "error", err)
return err
}

defer func() {
ctxClose()
app.Close(ctx)
log.Info("Application stopped", "name", app.Name())
}()

quitCh := make(chan os.Signal, 1)
signal.Notify(quitCh, []os.Signal{
os.Interrupt,
os.Kill,
syscall.SIGTERM,
syscall.SIGQUIT,
}...)
<-quitCh

return nil
// getServer returns an instance of node.Server based on the command name provided in the cli.Context parameter.
func getServer(ctx *cli.Context) node.Service {
switch ctx.Command.Name {
case "driver":
return new(driver.Driver)
case "proposer":
return new(proposer.Proposer)
case "prover":
return new(prover.Prover)
default:
panic("Unknown command name")
}
}
15 changes: 15 additions & 0 deletions node/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package node

import (
"context"

"github.com/urfave/cli/v2"
)

// Service is the interface for the server that the node runs.
type Service interface {
InitFromCli(context.Context, *cli.Context) error
Name() string
Start() error
Close(context.Context)
}
4 changes: 2 additions & 2 deletions testutils/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/cmd/utils"
"github.com/taikoxyz/taiko-client/node"
)

type CalldataSyncer interface {
ProcessL1Blocks(ctx context.Context, l1End *types.Header) error
}

type Proposer interface {
utils.SubcommandApplication
node.Service
ProposeOp(ctx context.Context) error
ProposeEmptyBlockOp(ctx context.Context) error
L2SuggestedFeeRecipient() common.Address
Expand Down

0 comments on commit 381941f

Please sign in to comment.