Initialize RPC client to connect with a remote Nitro node

This commit is contained in:
Prathamesh Musale 2023-10-16 15:39:33 +05:30
parent fe1f4718e2
commit 0834c0a334
3 changed files with 57 additions and 34 deletions

View File

@ -69,6 +69,8 @@ func serve() {
logWithCommand.Infof("ipld-eth-server version: %s", v.VersionWithMeta) logWithCommand.Infof("ipld-eth-server version: %s", v.VersionWithMeta)
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
defer wg.Wait()
serverConfig, err := s.NewConfig() serverConfig, err := s.NewConfig()
if err != nil { if err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
@ -90,6 +92,14 @@ func serve() {
server.Serve(wg) server.Serve(wg)
// TODO: Read from config file
runNitroInProcess := false
nitroEndpoint := "127.0.0.1:4005/api/v1"
var voucherValidator paymentsmanager.VoucherValidator
if runNitroInProcess {
log.Info("Running an in-process Nitro node")
nitroNode, err := initializeNitroNode(serverConfig.Nitro) nitroNode, err := initializeNitroNode(serverConfig.Nitro)
if err != nil { if err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
@ -99,18 +109,8 @@ func serve() {
if err != nil { if err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
} }
pm.Start(wg) pm.Start(wg)
defer pm.Stop()
voucherValidator := paymentsmanager.InProcessVoucherValidator{PaymentsManager: pm}
// TODO: Read from config file
queryRates := map[string]*big.Int{
"eth_getBlockByNumber": big.NewInt(50),
"eth_getBlockByHash": big.NewInt(50),
"eth_getStorageAt": big.NewInt(50),
"eth_getLogs": big.NewInt(50),
}
// TODO: Read from config file // TODO: Read from config file
rpcPort := 4005 rpcPort := 4005
@ -119,13 +119,40 @@ func serve() {
cert, err := tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath) cert, err := tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath)
if err != nil { if err != nil {
panic(err) logWithCommand.Fatal(err)
} }
nitroRpcServer, err := initializeNitroRpcServer(nitroNode, pm, &cert, rpcPort) nitroRpcServer, err := initializeNitroRpcServer(nitroNode, pm, &cert, rpcPort)
if err != nil { if err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
} }
defer nitroRpcServer.Close()
voucherValidator = paymentsmanager.InProcessVoucherValidator{PaymentsManager: pm}
} 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)
if err != nil {
logWithCommand.Fatal(err)
}
defer nitroRpcClient.Close()
voucherValidator = nitroRpc.RemoteVoucherValidator{Client: nitroRpcClient}
}
// TODO: Read from config file
queryRates := map[string]*big.Int{
"eth_getBlockByNumber": big.NewInt(50),
"eth_getBlockByHash": big.NewInt(50),
"eth_getStorageAt": big.NewInt(50),
"eth_getLogs": big.NewInt(50),
}
if err := startServers(server, serverConfig, voucherValidator, queryRates); err != nil { if err := startServers(server, serverConfig, voucherValidator, queryRates); err != nil {
logWithCommand.Fatal(err) logWithCommand.Fatal(err)
@ -154,10 +181,6 @@ func serve() {
graphQL.Stop() graphQL.Stop()
} }
server.Stop() server.Stop()
pm.Stop()
nitroRpcServer.Close()
wg.Wait()
} }
// TODO: Absorb voucherValidator and queryRates args into existing ones // TODO: Absorb voucherValidator and queryRates args into existing ones

2
go.mod
View File

@ -302,4 +302,4 @@ replace (
github.com/cerc-io/ipfs-ethdb/v5 => github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28 github.com/cerc-io/ipfs-ethdb/v5 => github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28
) )
replace github.com/statechannels/go-nitro v0.1.1 => github.com/deep-stack/go-nitro v0.0.0-20231016053556-1aa955e958a8 replace github.com/statechannels/go-nitro v0.1.1 => github.com/deep-stack/go-nitro v0.0.0-20231016095715-e033149edb71

4
go.sum
View File

@ -184,8 +184,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/deep-stack/go-nitro v0.0.0-20231016053556-1aa955e958a8 h1:vQgSbAAEtf+34DKfvCeOPQKhcigcTaRCSfaVD/sn/JA= github.com/deep-stack/go-nitro v0.0.0-20231016095715-e033149edb71 h1:/AG4iXW0TO8iIRvGrdhOoh42zODMzJ/Zao/jXuJskS0=
github.com/deep-stack/go-nitro v0.0.0-20231016053556-1aa955e958a8/go.mod h1:gkKL37JcSo54ybLTI6VJRmP75bWEu9i1kc9RYmQLp+I= github.com/deep-stack/go-nitro v0.0.0-20231016095715-e033149edb71/go.mod h1:YYQvj9es00ZfLTwxZLM1M0ihUrqz8+lU2c10G06My3A=
github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M= github.com/deepmap/oapi-codegen v1.6.0/go.mod h1:ryDa9AgbELGeB+YEXE1dR53yAjHwFvE9iAUlWl9Al3M=
github.com/deepmap/oapi-codegen v1.8.2 h1:SegyeYGcdi0jLLrpbCMoJxnUUn8GBXHsvr4rbzjuhfU= github.com/deepmap/oapi-codegen v1.8.2 h1:SegyeYGcdi0jLLrpbCMoJxnUUn8GBXHsvr4rbzjuhfU=
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw= github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=