diff --git a/cmd/cmd.go b/cmd/cmd.go index 5f5e9e4ee..699ae9bc0 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -370,10 +370,10 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, // 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, +) (*node.Node, *wallet.Wallet, error) { + conf, gen, err := MakeConfig(workingDir, confModifiers...) if err != nil { return nil, nil, err } @@ -448,7 +448,7 @@ func makeLocalGenesis(wlt wallet.Wallet) *genesis.Genesis { // 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 @@ -498,6 +498,10 @@ func MakeConfig(workingDir string) (*config.Config, *genesis.Genesis, error) { conf.WalletManager.ChainType = chainType conf.WalletManager.WalletsDir = walletsDir + for _, modify := range confModifiers { + modify(conf) + } + if err := conf.BasicCheck(); err != nil { return nil, nil, err } diff --git a/cmd/daemon/start.go b/cmd/daemon/start.go index 2d0616ad8..f76b40c76 100644 --- a/cmd/daemon/start.go +++ b/cmd/daemon/start.go @@ -7,6 +7,7 @@ import ( "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" @@ -29,6 +30,23 @@ func buildStartCmd(parentCmd *cobra.Command) { 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") + startCmd.Run = func(_ *cobra.Command, _ []string) { workingDir, _ := filepath.Abs(*workingDirOpt) // change working directory @@ -68,8 +86,17 @@ func buildStartCmd(parentCmd *cobra.Command) { return password, true } + + confModifiers := []config.Modifier{ + config.EnableGRPCTransport(*gRPCOpt, *gRPCWalletOpt), + config.EnableZMQBlockInfoPub(*zmqBlockInfoOpt), + config.EnableZMQTxInfoPub(*zmqTxInfoOpt), + config.EnableZMQRawBlockPub(*zmqRawBlockOpt), + config.EnableZMQRawTxPub(*zmqRawTxOpt), + } + node, _, err := cmd.StartNode( - workingDir, passwordFetcher) + workingDir, passwordFetcher, confModifiers...) cmd.FatalErrorCheck(err) cmd.TrapSignal(func() { diff --git a/config/modify.go b/config/modify.go new file mode 100644 index 000000000..608cd9be0 --- /dev/null +++ b/config/modify.go @@ -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 + } + + cfg.GRPC.EnableWallet = enableWallet + } +} + +func EnableZMQBlockInfoPub(listen string) Modifier { + return func(cfg *Config) { + if listen != "" { + cfg.ZeroMq.ZmqPubBlockInfo = listen + } + } +} + +func EnableZMQTxInfoPub(listen string) Modifier { + return func(cfg *Config) { + if listen != "" { + cfg.ZeroMq.ZmqPubTxInfo = listen + } + } +} + +func EnableZMQRawBlockPub(listen string) Modifier { + return func(cfg *Config) { + if listen != "" { + cfg.ZeroMq.ZmqPubRawBlock = listen + } + } +} + +func EnableZMQRawTxPub(listen string) Modifier { + return func(cfg *Config) { + if listen != "" { + cfg.ZeroMq.ZmqPubRawTx = listen + } + } +}