diff --git a/cmd/serve.go b/cmd/serve.go index 82b56770..1ee0ce03 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -104,7 +104,7 @@ func serve() { defer pm.Stop() defer nitroRpcServer.Close() - voucherValidator = paymentsmanager.InProcessVoucherValidator{PaymentsManager: pm} + voucherValidator = paymentsmanager.InProcessVoucherValidator{PaymentsManager: *pm} } else { log.Info("Connecting to a remote Nitro node") @@ -123,7 +123,11 @@ func serve() { logWithCommand.Fatal(err) } - if err := startServers(server, serverConfig, voucherValidator, queryRates); err != nil { + paymentMiddleware := func(next http.Handler) http.Handler { + return paymentsmanager.HTTPMiddleware(next, voucherValidator, queryRates) + } + + if err := startServers(server, serverConfig, [](func(next http.Handler) http.Handler){paymentMiddleware}); err != nil { logWithCommand.Fatal(err) } graphQL, err := startEthGraphQL(server, serverConfig) @@ -152,8 +156,7 @@ func serve() { server.Stop() } -// TODO: Absorb voucherValidator and queryRates args into existing ones -func startServers(server s.Server, settings *s.Config, voucherValidator paymentsmanager.VoucherValidator, queryRates map[string]*big.Int) error { +func startServers(server s.Server, settings *s.Config, httpMiddlewares [](func(next http.Handler) http.Handler)) error { if settings.IPCEnabled { logWithCommand.Debug("starting up IPC server") _, _, err := srpc.StartIPCEndpoint(settings.IPCEndpoint, server.APIs()) @@ -176,7 +179,7 @@ func startServers(server s.Server, settings *s.Config, voucherValidator payments if settings.HTTPEnabled { logWithCommand.Debug("starting up HTTP server") - _, err := srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"vdb", "eth", "debug", "net"}, nil, []string{"*"}, rpc.HTTPTimeouts{}, voucherValidator, queryRates) + _, err := srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"vdb", "eth", "debug", "net"}, nil, []string{"*"}, rpc.HTTPTimeouts{}, httpMiddlewares) if err != nil { return err } @@ -390,6 +393,7 @@ func init() { viper.BindPFlag("validator.everyNthBlock", serveCmd.PersistentFlags().Lookup("validator-every-nth-block")) } +// Initializes an in-process Nitro node, payments manager and a Nitro RPC server func initNitroInProcess(wg *sync.WaitGroup, nitroConfig *s.NitroConfig) (*paymentsmanager.PaymentsManager, *nitroRpc.RpcServer) { nitroNode, err := initNitroNode(&nitroConfig.InProcessNode) if err != nil { diff --git a/environments/example.toml b/environments/example.toml index d5f2f603..e6bdcf12 100644 --- a/environments/example.toml +++ b/environments/example.toml @@ -34,7 +34,7 @@ [nitro] runNodeInProcess = false # NITRO_RUN_NODE_IN_PROCESS - rpcQueryRatesFile = "environments/rates.json" + rpcQueryRatesFile = "environments/rpcQueryRates.json" # NITRO_RPC_QUERY_RATES_FILE [nitro.inProcesssNode] pk = "" # NITRO_PK @@ -54,5 +54,4 @@ [nitro.remoteNode] nitroEndpoint = "127.0.0.1:4005/api/v1" # NITRO_ENDPOINT - isSecure = false - + isSecure = false # NITRO_IS_SECURE diff --git a/go.mod b/go.mod index 80f9da3e..c20fed6d 100644 --- a/go.mod +++ b/go.mod @@ -304,4 +304,4 @@ replace ( // TODO: Use release // https://github.com/cerc-io/go-nitro/tree/ts-interop -replace github.com/statechannels/go-nitro v0.1.1 => github.com/cerc-io/go-nitro v0.1.2-ts-port-0.1.8.0.20231018070130-e66ac486a763 +replace github.com/statechannels/go-nitro v0.1.1 => github.com/deep-stack/go-nitro v0.0.0-20231019043713-bb05f6b8510d diff --git a/go.sum b/go.sum index 4717645d..f1fa258e 100644 --- a/go.sum +++ b/go.sum @@ -114,8 +114,6 @@ github.com/ceramicnetwork/go-dag-jose v0.1.0 h1:yJ/HVlfKpnD3LdYP03AHyTvbm3BpPiz2 github.com/ceramicnetwork/go-dag-jose v0.1.0/go.mod h1:qYA1nYt0X8u4XoMAVoOV3upUVKtrxy/I670Dg5F0wjI= github.com/cerc-io/eth-ipfs-state-validator/v5 v5.1.1-alpha.0.20231013075659-56aa03028c43 h1:pkGCN+VWo5Qmu4iDjA7noGrE6wM8VOVeX1Mn6ucYhPg= github.com/cerc-io/eth-ipfs-state-validator/v5 v5.1.1-alpha.0.20231013075659-56aa03028c43/go.mod h1:snThUFpyCrpZhTuz3HibJRLL2XaS+lKNsM3XAE0gB/4= -github.com/cerc-io/go-nitro v0.1.2-ts-port-0.1.8.0.20231018070130-e66ac486a763 h1:RTUK5AquaGw/aMbdgpDuj0IHhqgAg6R2own9lI14ZOQ= -github.com/cerc-io/go-nitro v0.1.2-ts-port-0.1.8.0.20231018070130-e66ac486a763/go.mod h1:YYQvj9es00ZfLTwxZLM1M0ihUrqz8+lU2c10G06My3A= github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28 h1:5FXtMuZXTIXjjzzLdqgyzx9pjD22FB5os2vXayRn+BQ= github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28/go.mod h1:W1C6qTXGsPcsK1HKUYPsXmBORjO2ekdm+101sJkpdNI= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= @@ -186,6 +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/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/deep-stack/go-nitro v0.0.0-20231019043713-bb05f6b8510d h1:dpERaK7+V+nMxv0iZuWYxTuB+4E7KJKTSyhCEq+xWvQ= +github.com/deep-stack/go-nitro v0.0.0-20231019043713-bb05f6b8510d/go.mod h1:YYQvj9es00ZfLTwxZLM1M0ihUrqz8+lU2c10G06My3A= 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/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw= diff --git a/pkg/rpc/http.go b/pkg/rpc/http.go index 02c683c3..42871e77 100644 --- a/pkg/rpc/http.go +++ b/pkg/rpc/http.go @@ -18,20 +18,18 @@ package rpc import ( "fmt" - "math/big" + "net/http" "github.com/cerc-io/ipld-eth-server/v5/pkg/log" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" - "github.com/statechannels/go-nitro/paymentsmanager" "github.com/cerc-io/ipld-eth-server/v5/pkg/prom" ) // StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules. -// TODO: Absorb voucherValidator and queryRates args into existing ones -func StartHTTPEndpoint(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts, voucherValidator paymentsmanager.VoucherValidator, queryRates map[string]*big.Int) (*rpc.Server, error) { +func StartHTTPEndpoint(endpoint string, apis []rpc.API, modules []string, cors []string, vhosts []string, timeouts rpc.HTTPTimeouts, httpMiddlewares [](func(next http.Handler) http.Handler)) (*rpc.Server, error) { srv := rpc.NewServer() err := node.RegisterApis(apis, modules, srv) if err != nil { @@ -39,11 +37,16 @@ func StartHTTPEndpoint(endpoint string, apis []rpc.API, modules []string, cors [ } promHandler := prom.HTTPMiddleware(node.NewHTTPHandlerStack(srv, cors, vhosts, nil)) - paymentHandler := paymentsmanager.HTTPMiddleware(promHandler, voucherValidator, queryRates) + + // Chain the HTTP middlewares + handler := promHandler + for _, middleware := range httpMiddlewares { + handler = middleware(handler) + } // start http server // request -> payments -> metrics -> server - _, addr, err := node.StartHTTPEndpoint(endpoint, rpc.DefaultHTTPTimeouts, paymentHandler) + _, addr, err := node.StartHTTPEndpoint(endpoint, rpc.DefaultHTTPTimeouts, handler) if err != nil { utils.Fatalf("Could not start RPC api: %v", err) }