Run in-process go-nitro RPC server

This commit is contained in:
Prathamesh Musale 2023-10-12 15:16:55 +05:30
parent 2200ccaec4
commit 17e8b1a2ee
3 changed files with 43 additions and 27 deletions

View File

@ -17,6 +17,7 @@ package cmd
import (
"errors"
"fmt"
"math/big"
"net/http"
"net/url"
@ -36,6 +37,9 @@ import (
"github.com/statechannels/go-nitro/node/engine/chainservice"
"github.com/statechannels/go-nitro/node/engine/store"
"github.com/statechannels/go-nitro/paymentsmanager"
"github.com/statechannels/go-nitro/rpc/transport"
"github.com/tidwall/buntdb"
"golang.org/x/exp/slog"
"github.com/cerc-io/ipld-eth-server/v5/pkg/graphql"
srpc "github.com/cerc-io/ipld-eth-server/v5/pkg/rpc"
@ -43,6 +47,8 @@ import (
v "github.com/cerc-io/ipld-eth-server/v5/version"
nitroNode "github.com/statechannels/go-nitro/node"
nitrop2pms "github.com/statechannels/go-nitro/node/engine/messageservice/p2p-message-service"
nitroRpc "github.com/statechannels/go-nitro/rpc"
nitroHttpTransport "github.com/statechannels/go-nitro/rpc/transport/http"
)
var ErrNoRpcEndpoints = errors.New("no rpc endpoints is available")
@ -87,12 +93,12 @@ func serve() {
// TODO: Create required config for Nitro node
nitroNode, err := initializeNitroNode(serverConfig.Nitro)
if err != nil {
panic(err)
logWithCommand.Fatal(err)
}
pm, err := paymentsmanager.NewPaymentsManager(nitroNode)
if err != nil {
panic(err)
logWithCommand.Fatal(err)
}
pm.Start(wg)
@ -107,6 +113,12 @@ func serve() {
"eth_getLogs": big.NewInt(50),
}
rpcPort := 4005
nitroRpcServer, err := initializeNitroRpcServer(nitroNode, rpcPort)
if err != nil {
logWithCommand.Fatal(err)
}
if err := startServers(server, serverConfig, voucherValidator, queryRates); err != nil {
logWithCommand.Fatal(err)
}
@ -135,6 +147,7 @@ func serve() {
}
server.Stop()
pm.Stop()
nitroRpcServer.Close()
wg.Wait()
}
@ -403,30 +416,14 @@ func initializeNitroNode(nitroConfig *s.NitroConfig) (*nitroNode.Node, error) {
CaAddress: common.HexToAddress(caAddress),
}
storeOpts := store.StoreOpts{
PkBytes: common.Hex2Bytes(pkString),
UseDurableStore: useDurableStore,
DurableStoreFolder: durableStoreFolder,
}
peerSlice := []string{}
messageOpts := nitrop2pms.MessageOpts{
PkBytes: common.Hex2Bytes(pkString),
TcpPort: msgPort,
WsMsgPort: wsMsgPort,
BootPeers: peerSlice,
PublicIp: publicIp,
}
ourStore, err := store.NewStore(storeOpts)
ourStore, err := store.NewStore(common.Hex2Bytes(pkString), useDurableStore, durableStoreFolder, buntdb.Config{})
if err != nil {
return nil, err
}
log.Info("Initializing message service...", " tcp port=", messageOpts.TcpPort, " web socket port=", messageOpts.WsMsgPort)
messageOpts.SCAddr = *ourStore.GetAddress()
messageService := nitrop2pms.NewMessageService(messageOpts)
bootPeers := []string{}
log.Info("Initializing message service...", " tcp port=", msgPort, " web socket port=", wsMsgPort)
messageService := nitrop2pms.NewMessageService(publicIp, msgPort, wsMsgPort, *ourStore.GetAddress(), common.Hex2Bytes(pkString), bootPeers)
// Compare chainOpts.ChainStartBlock to lastBlockNum seen in store. The larger of the two
// gets passed as an argument when creating NewEthChainService
@ -453,3 +450,22 @@ func initializeNitroNode(nitroConfig *s.NitroConfig) (*nitroNode.Node, error) {
return &node, nil
}
func initializeNitroRpcServer(node *nitroNode.Node, rpcPort int) (*nitroRpc.RpcServer, error) {
var transport transport.Responder
var err error
slog.Info("Initializing Nitro HTTP RPC transport...")
transport, err = nitroHttpTransport.NewHttpTransportAsServer(fmt.Sprint(rpcPort))
if err != nil {
return nil, err
}
rpcServer, err := nitroRpc.NewRpcServer(node, transport)
if err != nil {
return nil, err
}
slog.Info("Completed Nitro RPC server initialization")
return rpcServer, nil
}

6
go.mod
View File

@ -24,6 +24,8 @@ require (
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
github.com/statechannels/go-nitro v0.1.1
github.com/tidwall/buntdb v1.2.10
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
gorm.io/driver/postgres v1.3.7
gorm.io/gorm v1.23.5
)
@ -233,7 +235,6 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/thoas/go-funk v0.9.3 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/tidwall/buntdb v1.2.10 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/grect v0.1.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
@ -264,7 +265,6 @@ require (
go.uber.org/zap v1.25.0 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
@ -300,4 +300,4 @@ replace (
github.com/cerc-io/ipfs-ethdb/v5 => git.vdb.to/deep-stack/ipfs-ethdb/v5 v5.0.1-alpha.0.20231003124335-b6cf70668a07
)
replace github.com/statechannels/go-nitro v0.1.1 => github.com/cerc-io/go-nitro v0.1.2-0.20231012062355-dc2cb28411a5
replace github.com/statechannels/go-nitro v0.1.1 => github.com/cerc-io/go-nitro v0.1.2-0.20231012093240-6954444571ea

4
go.sum
View File

@ -116,8 +116,8 @@ github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/ceramicnetwork/go-dag-jose v0.1.0 h1:yJ/HVlfKpnD3LdYP03AHyTvbm3BpPiz2oZiOeReJRdU=
github.com/ceramicnetwork/go-dag-jose v0.1.0/go.mod h1:qYA1nYt0X8u4XoMAVoOV3upUVKtrxy/I670Dg5F0wjI=
github.com/cerc-io/go-nitro v0.1.2-0.20231012062355-dc2cb28411a5 h1:wMrhik+HPv/cdhyWL0nUImZ0XGFZ0el4eSY7QjwH0aA=
github.com/cerc-io/go-nitro v0.1.2-0.20231012062355-dc2cb28411a5/go.mod h1:gkKL37JcSo54ybLTI6VJRmP75bWEu9i1kc9RYmQLp+I=
github.com/cerc-io/go-nitro v0.1.2-0.20231012093240-6954444571ea h1:iDGWl8i4ul7ACtt/edMZZQkY65bH9FTPeayqltsG4vk=
github.com/cerc-io/go-nitro v0.1.2-0.20231012093240-6954444571ea/go.mod h1:gkKL37JcSo54ybLTI6VJRmP75bWEu9i1kc9RYmQLp+I=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=