Read RPC query rates from a json file

This commit is contained in:
neeraj 2023-10-17 17:47:17 +05:30 committed by Prathamesh Musale
parent cb3558d24f
commit b52f83c0d7
5 changed files with 47 additions and 13 deletions

View File

@ -39,6 +39,7 @@ 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-rpc-query-rates-file", "", "nitro rpcQueryRatesFile")
command.PersistentFlags().String("nitro-pk", "", "nitro pk")
command.PersistentFlags().String("nitro-chain-pk", "", "nitro chainPk")
@ -52,13 +53,14 @@ func addNitroFlags(command *cobra.Command) {
command.PersistentFlags().Int("nitro-rpc-port", 4005, "nitro rpcPort")
command.PersistentFlags().Int("nitro-ws-msg-port", 5005, "nitro wsMsgPort")
command.PersistentFlags().Uint("nitro-chain-start-block", 0, "nitro chainStartBlock")
command.PersistentFlags().String("nitro-tls-cert-filepath", "./nitroTLS/statechannels.org.pem", "nitro tlsCertFilepath")
command.PersistentFlags().String("nitro-tls-key-filepath", "./nitroTLS/statechannels.org_key.pem", "nitro tlsKeyFilepath")
command.PersistentFlags().String("nitro-tls-cert-filepath", "", "nitro tlsCertFilepath")
command.PersistentFlags().String("nitro-tls-key-filepath", "", "nitro tlsKeyFilepath")
command.PersistentFlags().String("nitro-endpoint", "", "nitro endpoint")
// nitro flag bindings
viper.BindPFlag("nitro.runNodeInProcess", command.PersistentFlags().Lookup("nitro-run-node-in-process"))
viper.BindPFlag("nitro.rpcQueryRatesFile", command.PersistentFlags().Lookup("nitro-rpc-query-rates-file"))
viper.BindPFlag("nitro.inProcesssNode.pk", command.PersistentFlags().Lookup("nitro-pk"))
viper.BindPFlag("nitro.inProcesssNode.chainPk", command.PersistentFlags().Lookup("nitro-chain-pk"))

View File

@ -17,8 +17,10 @@ package cmd
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/fs"
"math/big"
"net/http"
"net/url"
@ -144,12 +146,9 @@ func serve() {
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),
queryRates, err := readRpcQueryRates(nitroConfig.RpcQueryRatesFile)
if err != nil {
logWithCommand.Fatal(err)
}
if err := startServers(server, serverConfig, voucherValidator, queryRates); err != nil {
@ -513,3 +512,25 @@ func initNitroRpcServer(node *nitroNode.Node, pm paymentsmanager.PaymentsManager
slog.Info("Completed Nitro RPC server initialization")
return rpcServer, nil
}
func readRpcQueryRates(filepath string) (map[string]*big.Int, error) {
jsonFile, err := os.Open(filepath)
defer jsonFile.Close()
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
result := make(map[string]*big.Int)
return result, nil
}
return nil, err
}
var result map[string]*big.Int
decoder := json.NewDecoder(jsonFile)
err = decoder.Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}

View File

@ -34,6 +34,7 @@
[nitro]
runNodeInProcess = false # NITRO_RUN_NODE_IN_PROCESS
rpcQueryRatesFile = "environments/rates.json"
[nitro.inProcesssNode]
pk = "" # NITRO_PK
@ -48,8 +49,8 @@
rpcPort = 4005
wsMsgPort = 5005
chainStartBlock = 0
tlsCertFilepath = "./nitroTLS/statechannels.org.pem"
tlsKeyFilepath = "./nitroTLS/statechannels.org_key.pem"
tlsCertFilepath = ""
tlsKeyFilepath = ""
[nitro.remoteNode]
nitroEndpoint = "127.0.0.1:4005/api/v1" # NITRO_ENDPOINT

6
environments/rates.json Normal file
View File

@ -0,0 +1,6 @@
{
"eth_getBlockByNumber": 50,
"eth_getBlockByHash": 50,
"eth_getStorageAt": 50,
"eth_getLogs": 50
}

View File

@ -81,6 +81,7 @@ const (
DATABASE_MAX_CONN_LIFETIME = "DATABASE_MAX_CONN_LIFETIME"
NITRO_RUN_NODE_IN_PROCESS = "NITRO_RUN_NODE_IN_PROCESS"
NITRO_RPC_QUERY_RATES_FILE = "NITRO_RPC_QUERY_RATES_FILE"
NITRO_PK = "NITRO_PK"
NITRO_CHAIN_PK = "NITRO_CHAIN_PK"
NITRO_CHAIN_URL = "NITRO_CHAIN_URL"
@ -120,9 +121,10 @@ type RemoteNitroNodeConfig struct {
}
type NitroConfig struct {
RunNodeInProcess bool
InProcessNode InProcessNitroNodeConfig
RemoteNode RemoteNitroNodeConfig
RunNodeInProcess bool
RpcQueryRatesFile string
InProcessNode InProcessNitroNodeConfig
RemoteNode RemoteNitroNodeConfig
}
// Config struct
@ -332,6 +334,7 @@ func (c *Config) loadNitroConfig() {
c.Nitro = &NitroConfig{InProcessNode: InProcessNitroNodeConfig{}, RemoteNode: RemoteNitroNodeConfig{}}
viper.BindEnv("nitro.runNodeInProcess", NITRO_RUN_NODE_IN_PROCESS)
viper.BindEnv("nitro.rpcQueryRatesFile", NITRO_RPC_QUERY_RATES_FILE)
viper.BindEnv("nitro.inProcesssNode.pk", NITRO_PK)
viper.BindEnv("nitro.inProcesssNode.chainPk", NITRO_CHAIN_PK)
@ -351,6 +354,7 @@ func (c *Config) loadNitroConfig() {
viper.BindEnv("nitro.remoteNode.nitroEndpoint", NITRO_ENDPOINT)
c.Nitro.RunNodeInProcess = viper.GetBool("nitro.runNodeInProcess")
c.Nitro.RpcQueryRatesFile = viper.GetString("nitro.rpcQueryRatesFile")
c.Nitro.InProcessNode.Pk = viper.GetString("nitro.inProcesssNode.pk")
c.Nitro.InProcessNode.ChainPk = viper.GetString("nitro.inProcesssNode.chainPk")