Add a flag for in / out of process Nitro nodes

This commit is contained in:
Prathamesh Musale 2023-10-16 17:22:33 +05:30
parent 0834c0a334
commit 083cd37904
4 changed files with 88 additions and 51 deletions

View File

@ -38,6 +38,8 @@ func addDatabaseFlags(command *cobra.Command) {
func addNitroFlags(command *cobra.Command) {
// nitro flags
command.PersistentFlags().Bool("nitro-run-node-in-process", false, "nitro run node in process")
command.PersistentFlags().String("nitro-pk", "", "nitro pk")
command.PersistentFlags().String("nitro-chain-pk", "", "nitro chainPk")
command.PersistentFlags().String("nitro-chain-url", "", "nitro chainUrl")
@ -47,13 +49,19 @@ func addNitroFlags(command *cobra.Command) {
command.PersistentFlags().Bool("nitro-use-durable-store", false, "nitro useDurableStore")
command.PersistentFlags().String("nitro-durable-store-folder", "", "nitro durableStoreFolder")
command.PersistentFlags().String("nitro-endpoint", "", "nitro endpoint")
// nitro flag bindings
viper.BindPFlag("nitro.pk", command.PersistentFlags().Lookup("nitro-pk"))
viper.BindPFlag("nitro.chainPk", command.PersistentFlags().Lookup("nitro-chain-pk"))
viper.BindPFlag("nitro.chainUrl", command.PersistentFlags().Lookup("nitro-chain-url"))
viper.BindPFlag("nitro.naAddress", command.PersistentFlags().Lookup("nitro-na-address"))
viper.BindPFlag("nitro.vpaAddress", command.PersistentFlags().Lookup("nitro-vpa-address"))
viper.BindPFlag("nitro.caAddress", command.PersistentFlags().Lookup("nitro-ca-address"))
viper.BindPFlag("nitro.useDurableStore", command.PersistentFlags().Lookup("nitro-use-durable-store"))
viper.BindPFlag("nitro.durableStoreFolder", command.PersistentFlags().Lookup("nitro-durable-store"))
viper.BindPFlag("nitro.runNodeInProcess", command.PersistentFlags().Lookup("nitro-run-node-in-process"))
viper.BindPFlag("nitro.inProcesssNode.pk", command.PersistentFlags().Lookup("nitro-pk"))
viper.BindPFlag("nitro.inProcesssNode.chainPk", command.PersistentFlags().Lookup("nitro-chain-pk"))
viper.BindPFlag("nitro.inProcesssNode.chainUrl", command.PersistentFlags().Lookup("nitro-chain-url"))
viper.BindPFlag("nitro.inProcesssNode.naAddress", command.PersistentFlags().Lookup("nitro-na-address"))
viper.BindPFlag("nitro.inProcesssNode.vpaAddress", command.PersistentFlags().Lookup("nitro-vpa-address"))
viper.BindPFlag("nitro.inProcesssNode.caAddress", command.PersistentFlags().Lookup("nitro-ca-address"))
viper.BindPFlag("nitro.inProcesssNode.useDurableStore", command.PersistentFlags().Lookup("nitro-use-durable-store"))
viper.BindPFlag("nitro.inProcesssNode.durableStoreFolder", command.PersistentFlags().Lookup("nitro-durable-store"))
viper.BindPFlag("nitro.remoteNode.nitroEndpoint", command.PersistentFlags().Lookup("nitro-endpoint"))
}

View File

@ -92,15 +92,14 @@ func serve() {
server.Serve(wg)
// TODO: Read from config file
runNitroInProcess := false
nitroEndpoint := "127.0.0.1:4005/api/v1"
var voucherValidator paymentsmanager.VoucherValidator
if runNitroInProcess {
// TODO: Refactor into a function / subcommand
nitroConfig := serverConfig.Nitro
if nitroConfig.RunNodeInProcess {
log.Info("Running an in-process Nitro node")
nitroNode, err := initializeNitroNode(serverConfig.Nitro)
nitroNode, err := initNitroNode(&nitroConfig.InProcessNode)
if err != nil {
logWithCommand.Fatal(err)
}
@ -122,7 +121,7 @@ func serve() {
logWithCommand.Fatal(err)
}
nitroRpcServer, err := initializeNitroRpcServer(nitroNode, pm, &cert, rpcPort)
nitroRpcServer, err := initNitroRpcServer(nitroNode, pm, &cert, rpcPort)
if err != nil {
logWithCommand.Fatal(err)
}
@ -132,12 +131,7 @@ func serve() {
} else {
log.Info("Connecting to a remote Nitro node")
clientConnection, err := nitroHttpTransport.NewHttpTransportAsClient(nitroEndpoint, 10*time.Millisecond)
if err != nil {
logWithCommand.Fatal(err)
}
nitroRpcClient, err := nitroRpc.NewRpcClient(clientConnection)
nitroRpcClient, err := initNitroRpcClient(&nitroConfig.RemoteNode)
if err != nil {
logWithCommand.Fatal(err)
}
@ -422,19 +416,19 @@ func init() {
}
// https://github.com/cerc-io/go-nitro/blob/release-v0.1.1-ts-port-0.1.7/internal/node/node.go#L17
func initializeNitroNode(nitroConfig *s.NitroConfig) (*nitroNode.Node, error) {
func initNitroNode(config *s.InProcessNitroNodeConfig) (*nitroNode.Node, error) {
// TODO: Read from config file
pkString := nitroConfig.Pk
useDurableStore := nitroConfig.UseDurableStore
durableStoreFolder := nitroConfig.DurableStoreFolder
pkString := config.Pk
useDurableStore := config.UseDurableStore
durableStoreFolder := config.DurableStoreFolder
msgPort := 3005
wsMsgPort := 5005
chainUrl := nitroConfig.ChainUrl
chainUrl := config.ChainUrl
chainStartBlock := uint64(0)
chainPk := nitroConfig.ChainPk
naAddress := nitroConfig.NaAddress
vpaAddress := nitroConfig.VpaAddress
caAddress := nitroConfig.CaAddress
chainPk := config.ChainPk
naAddress := config.NaAddress
vpaAddress := config.VpaAddress
caAddress := config.CaAddress
chainAuthToken := ""
publicIp := "0.0.0.0"
@ -498,7 +492,7 @@ func initializeNitroNode(nitroConfig *s.NitroConfig) (*nitroNode.Node, error) {
return &node, nil
}
func initializeNitroRpcServer(node *nitroNode.Node, pm paymentsmanager.PaymentsManager, cert *tls.Certificate, rpcPort int) (*nitroRpc.RpcServer, error) {
func initNitroRpcServer(node *nitroNode.Node, pm paymentsmanager.PaymentsManager, cert *tls.Certificate, rpcPort int) (*nitroRpc.RpcServer, error) {
var transport transport.Responder
var err error
@ -516,3 +510,12 @@ func initializeNitroRpcServer(node *nitroNode.Node, pm paymentsmanager.PaymentsM
slog.Info("Completed Nitro RPC server initialization")
return rpcServer, nil
}
func initNitroRpcClient(config *s.RemoteNitroNodeConfig) (nitroRpc.RpcClientApi, error) {
clientConnection, err := nitroHttpTransport.NewHttpTransportAsClient(config.NitroEndpoint, 10*time.Millisecond)
if err != nil {
logWithCommand.Fatal(err)
}
return nitroRpc.NewRpcClient(clientConnection)
}

View File

@ -33,11 +33,17 @@
networkID = "1" # $ETH_NETWORK_ID
[nitro]
runNodeInProcess = false # NITRO_RUN_NODE_IN_PROCESS
[nitro.inProcesssNode]
pk = "" # NITRO_PK
chainPk = "" # NITRO_CHAIN_PK
chainUrl = "" # NITRO_CHAIN_URL
chainUrl = "ws://127.0.0.1:8545" # NITRO_CHAIN_URL
naAddress = "" # NITRO_NA_ADDRESS
vpaAddress = "" # NITRO_VPA_ADDRESS
caAddress = "" # NITRO_CA_ADDRESS
useDurableStore = false # NITRO_USE_DURABLE_STORE
durableStoreFolder = "" # NITRO_DURABLE_STORE_FOLDER
useDurableStore = true # NITRO_USE_DURABLE_STORE
durableStoreFolder = "./data/nitronode" # NITRO_DURABLE_STORE_FOLDER
[nitro.remoteNode]
nitroEndpoint = "127.0.0.1:4005/api/v1" # NITRO_ENDPOINT

View File

@ -80,6 +80,7 @@ const (
DATABASE_MAX_OPEN_CONNECTIONS = "DATABASE_MAX_OPEN_CONNECTIONS"
DATABASE_MAX_CONN_LIFETIME = "DATABASE_MAX_CONN_LIFETIME"
NITRO_RUN_NODE_IN_PROCESS = "NITRO_RUN_NODE_IN_PROCESS"
NITRO_PK = "NITRO_PK"
NITRO_CHAIN_PK = "NITRO_CHAIN_PK"
NITRO_CHAIN_URL = "NITRO_CHAIN_URL"
@ -88,9 +89,10 @@ const (
NITRO_CA_ADDRESS = "NITRO_CA_ADDRESS"
NITRO_USE_DURABLE_STORE = "NITRO_USE_DURABLE_STORE"
NITRO_DURABLE_STORE_FOLDER = "NITRO_DURABLE_STORE_FOLDER"
NITRO_ENDPOINT = "NITRO_ENDPOINT"
)
type NitroConfig struct {
type InProcessNitroNodeConfig struct {
Pk string
ChainPk string
ChainUrl string
@ -101,6 +103,16 @@ type NitroConfig struct {
DurableStoreFolder string
}
type RemoteNitroNodeConfig struct {
NitroEndpoint string
}
type NitroConfig struct {
RunNodeInProcess bool
InProcessNode InProcessNitroNodeConfig
RemoteNode RemoteNitroNodeConfig
}
// Config struct
type Config struct {
DB *sqlx.DB
@ -305,25 +317,33 @@ func (c *Config) dbInit() {
}
func (c *Config) loadNitroConfig() {
c.Nitro = &NitroConfig{}
c.Nitro = &NitroConfig{InProcessNode: InProcessNitroNodeConfig{}, RemoteNode: RemoteNitroNodeConfig{}}
viper.BindEnv("nitro.pk", NITRO_PK)
viper.BindEnv("nitro.chainPk", NITRO_CHAIN_PK)
viper.BindEnv("nitro.chainUrl", NITRO_CHAIN_URL)
viper.BindEnv("nitro.naAddress", NITRO_NA_ADDRESS)
viper.BindEnv("nitro.vpaAddress", NITRO_VPA_ADDRESS)
viper.BindEnv("nitro.caAddress", NITRO_CA_ADDRESS)
viper.BindEnv("nitro.useDurableStore", NITRO_USE_DURABLE_STORE)
viper.BindEnv("nitro.durableStoreFolder", NITRO_DURABLE_STORE_FOLDER)
viper.BindEnv("nitro.runNodeInProcess", NITRO_RUN_NODE_IN_PROCESS)
c.Nitro.Pk = viper.GetString("nitro.pk")
c.Nitro.ChainPk = viper.GetString("nitro.chainPk")
c.Nitro.ChainUrl = viper.GetString("nitro.chainUrl")
c.Nitro.NaAddress = viper.GetString("nitro.naAddress")
c.Nitro.VpaAddress = viper.GetString("nitro.vpaAddress")
c.Nitro.CaAddress = viper.GetString("nitro.caAddress")
c.Nitro.UseDurableStore = viper.GetBool("nitro.useDurableStore")
c.Nitro.DurableStoreFolder = viper.GetString("nitro.durableStoreFolder")
viper.BindEnv("nitro.inProcesssNode.pk", NITRO_PK)
viper.BindEnv("nitro.inProcesssNode.chainPk", NITRO_CHAIN_PK)
viper.BindEnv("nitro.inProcesssNode.chainUrl", NITRO_CHAIN_URL)
viper.BindEnv("nitro.inProcesssNode.naAddress", NITRO_NA_ADDRESS)
viper.BindEnv("nitro.inProcesssNode.vpaAddress", NITRO_VPA_ADDRESS)
viper.BindEnv("nitro.inProcesssNode.caAddress", NITRO_CA_ADDRESS)
viper.BindEnv("nitro.inProcesssNode.useDurableStore", NITRO_USE_DURABLE_STORE)
viper.BindEnv("nitro.inProcesssNode.durableStoreFolder", NITRO_DURABLE_STORE_FOLDER)
viper.BindEnv("nitro.remoteNode.nitroEndpoint", NITRO_ENDPOINT)
c.Nitro.RunNodeInProcess = viper.GetBool("nitro.runNodeInProcess")
c.Nitro.InProcessNode.Pk = viper.GetString("nitro.inProcesssNode.pk")
c.Nitro.InProcessNode.ChainPk = viper.GetString("nitro.inProcesssNode.chainPk")
c.Nitro.InProcessNode.ChainUrl = viper.GetString("nitro.inProcesssNode.chainUrl")
c.Nitro.InProcessNode.NaAddress = viper.GetString("nitro.inProcesssNode.naAddress")
c.Nitro.InProcessNode.VpaAddress = viper.GetString("nitro.inProcesssNode.vpaAddress")
c.Nitro.InProcessNode.CaAddress = viper.GetString("nitro.inProcesssNode.caAddress")
c.Nitro.InProcessNode.UseDurableStore = viper.GetBool("nitro.inProcesssNode.useDurableStore")
c.Nitro.InProcessNode.DurableStoreFolder = viper.GetString("nitro.inProcesssNode.durableStoreFolder")
c.Nitro.RemoteNode.NitroEndpoint = viper.GetString("nitro.remoteNode.nitroEndpoint")
}
func (c *Config) loadGroupCacheConfig() {