Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): add transport switch in flag start daemon #1681

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@
// The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.
// TODO: write test for me.
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
*node.Node, *wallet.Wallet, error,
) {
conf, gen, err := MakeConfig(workingDir)
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool),
confModifiers ...config.Modifier,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
confModifiers ...config.Modifier,
modifier func(*config.Config) *config.Config,

Copy link
Contributor Author

@Ja7ad Ja7ad Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b00f Why modifier return Config? Modifier just applied changes on loaded configuration.

) (*node.Node, *wallet.Wallet, error) {
conf, gen, err := MakeConfig(workingDir, confModifiers...)

Check warning on line 376 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L375-L376

Added lines #L375 - L376 were not covered by tests
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -448,7 +448,7 @@
// The genesis document is required to determine the chain type, which influences the configuration settings.
// The function sets various private configurations, such as the "wallets directory" and chain-specific HRP values.
// If the configuration file cannot be loaded, it tries to recover or restore the configuration.
func MakeConfig(workingDir string) (*config.Config, *genesis.Genesis, error) {
func MakeConfig(workingDir string, confModifiers ...config.Modifier) (*config.Config, *genesis.Genesis, error) {
gen, err := genesis.LoadFromFile(PactusGenesisPath(workingDir))
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -498,6 +498,10 @@
conf.WalletManager.ChainType = chainType
conf.WalletManager.WalletsDir = walletsDir

for _, modify := range confModifiers {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if modifier != nil {
conf = modifier(conf)
}

modify(conf)
}

Check warning on line 503 in cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/cmd.go#L502-L503

Added lines #L502 - L503 were not covered by tests

if err := conf.BasicCheck(); err != nil {
return nil, nil, err
}
Expand Down
29 changes: 28 additions & 1 deletion cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"github.com/gofrs/flock"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/config"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/wallet"
"github.com/spf13/cobra"
Expand All @@ -29,6 +30,23 @@
passwordFromFileOpt := startCmd.Flags().String("password-from-file", "",
"the file containing the wallet password")

gRPCOpt := startCmd.Flags().String("grpc", "",
"enable gRPC transport, example: localhost:50051")

gRPCWalletOpt := startCmd.Flags().Bool("grpc-wallet", false, "enable gRPC wallet service")

zmqBlockInfoOpt := startCmd.Flags().String("zmq-block-info", "",
"enable zeromq block info publisher, example: tcp://127.0.0.1:28332")

zmqTxInfoOpt := startCmd.Flags().String("zmq-tx-info", "",
"enable zeromq transaction info publisher, example: tcp://127.0.0.1:28332")

zmqRawBlockOpt := startCmd.Flags().String("zmq-raw-block", "",
"enable zeromq raw block publisher, example: tcp://127.0.0.1:28332")

zmqRawTxOpt := startCmd.Flags().String("zmq-raw-tx", "",
"enable zeromq raw transaction publisher, example: tcp://127.0.0.1:28332")

Check warning on line 49 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L33-L49

Added lines #L33 - L49 were not covered by tests
startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
// change working directory
Expand Down Expand Up @@ -68,8 +86,17 @@

return password, true
}

confModifiers := []config.Modifier{
config.EnableGRPCTransport(*gRPCOpt, *gRPCWalletOpt),
config.EnableZMQBlockInfoPub(*zmqBlockInfoOpt),
config.EnableZMQTxInfoPub(*zmqTxInfoOpt),
config.EnableZMQRawBlockPub(*zmqRawBlockOpt),
config.EnableZMQRawTxPub(*zmqRawTxOpt),
}

Check warning on line 97 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L90-L97

Added lines #L90 - L97 were not covered by tests
node, _, err := cmd.StartNode(
workingDir, passwordFetcher)
workingDir, passwordFetcher, confModifiers...)

Check warning on line 99 in cmd/daemon/start.go

View check run for this annotation

Codecov / codecov/patch

cmd/daemon/start.go#L99

Added line #L99 was not covered by tests
cmd.FatalErrorCheck(err)

cmd.TrapSignal(func() {
Expand Down
47 changes: 47 additions & 0 deletions config/modify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package config

// Modifier override some configuration after load configuration.
type Modifier func(cfg *Config)

func EnableGRPCTransport(listen string, enableWallet bool) Modifier {
return func(cfg *Config) {
if listen != "" {
cfg.GRPC.Enable = true
cfg.GRPC.Listen = listen
}

Check warning on line 11 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L6-L11

Added lines #L6 - L11 were not covered by tests

cfg.GRPC.EnableWallet = enableWallet

Check warning on line 13 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L13

Added line #L13 was not covered by tests
}
}

func EnableZMQBlockInfoPub(listen string) Modifier {
return func(cfg *Config) {
if listen != "" {
cfg.ZeroMq.ZmqPubBlockInfo = listen
}

Check warning on line 21 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L17-L21

Added lines #L17 - L21 were not covered by tests
}
}

func EnableZMQTxInfoPub(listen string) Modifier {
return func(cfg *Config) {
if listen != "" {
cfg.ZeroMq.ZmqPubTxInfo = listen
}

Check warning on line 29 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L25-L29

Added lines #L25 - L29 were not covered by tests
}
}

func EnableZMQRawBlockPub(listen string) Modifier {
return func(cfg *Config) {
if listen != "" {
cfg.ZeroMq.ZmqPubRawBlock = listen
}

Check warning on line 37 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L33-L37

Added lines #L33 - L37 were not covered by tests
}
}

func EnableZMQRawTxPub(listen string) Modifier {
return func(cfg *Config) {
if listen != "" {
cfg.ZeroMq.ZmqPubRawTx = listen
}

Check warning on line 45 in config/modify.go

View check run for this annotation

Codecov / codecov/patch

config/modify.go#L41-L45

Added lines #L41 - L45 were not covered by tests
}
}
Loading