From db91f3ff66aeb6a7ddc8848b43b39588b222e285 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 18 Oct 2023 19:56:00 +0530 Subject: [PATCH] Refactor in process Nitro node setup --- cmd/serve.go | 59 ++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 646fbc82..82b56770 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -96,38 +96,12 @@ func serve() { var voucherValidator paymentsmanager.VoucherValidator - // TODO: Refactor into a function / subcommand nitroConfig := serverConfig.Nitro if nitroConfig.RunNodeInProcess { log.Info("Running an in-process Nitro node") - nitroNode, err := initNitroNode(&nitroConfig.InProcessNode) - if err != nil { - logWithCommand.Fatal(err) - } - - pm, err := paymentsmanager.NewPaymentsManager(nitroNode) - if err != nil { - logWithCommand.Fatal(err) - } - pm.Start(wg) + pm, nitroRpcServer := initNitroInProcess(wg, nitroConfig) defer pm.Stop() - - tlsCertFilepath := nitroConfig.InProcessNode.TlsCertFilepath - tlsKeyFilepath := nitroConfig.InProcessNode.TlsKeyFilepath - - var cert *tls.Certificate - if tlsCertFilepath != "" && tlsKeyFilepath != "" { - *cert, err = tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath) - if err != nil { - logWithCommand.Fatal(err) - } - } - - nitroRpcServer, err := initNitroRpcServer(nitroNode, pm, cert, nitroConfig.InProcessNode.RpcPort) - if err != nil { - logWithCommand.Fatal(err) - } defer nitroRpcServer.Close() voucherValidator = paymentsmanager.InProcessVoucherValidator{PaymentsManager: pm} @@ -416,6 +390,37 @@ func init() { viper.BindPFlag("validator.everyNthBlock", serveCmd.PersistentFlags().Lookup("validator-every-nth-block")) } +func initNitroInProcess(wg *sync.WaitGroup, nitroConfig *s.NitroConfig) (*paymentsmanager.PaymentsManager, *nitroRpc.RpcServer) { + nitroNode, err := initNitroNode(&nitroConfig.InProcessNode) + if err != nil { + logWithCommand.Fatal(err) + } + + pm, err := paymentsmanager.NewPaymentsManager(nitroNode) + if err != nil { + logWithCommand.Fatal(err) + } + pm.Start(wg) + + tlsCertFilepath := nitroConfig.InProcessNode.TlsCertFilepath + tlsKeyFilepath := nitroConfig.InProcessNode.TlsKeyFilepath + + var cert *tls.Certificate + if tlsCertFilepath != "" && tlsKeyFilepath != "" { + *cert, err = tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath) + if err != nil { + logWithCommand.Fatal(err) + } + } + + nitroRpcServer, err := initNitroRpcServer(nitroNode, pm, cert, nitroConfig.InProcessNode.RpcPort) + if err != nil { + logWithCommand.Fatal(err) + } + + return &pm, nitroRpcServer +} + // https://github.com/cerc-io/go-nitro/blob/release-v0.1.1-ts-port-0.1.7/internal/node/node.go#L17 func initNitroNode(config *s.InProcessNitroNodeConfig) (*nitroNode.Node, error) { pkString := config.Pk