github.com/Finschia/finschia-sdk@v0.48.1/server/rosetta/config.go (about) 1 package rosetta 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/coinbase/rosetta-sdk-go/types" 9 "github.com/spf13/pflag" 10 11 crg "github.com/Finschia/finschia-sdk/server/rosetta/lib/server" 12 13 "github.com/Finschia/finschia-sdk/codec" 14 codectypes "github.com/Finschia/finschia-sdk/codec/types" 15 ) 16 17 // configuration defaults constants 18 const ( 19 // DefaultBlockchain defines the default blockchain identifier name 20 DefaultBlockchain = "app" 21 // DefaultAddr defines the default rosetta binding address 22 DefaultAddr = ":8080" 23 // DefaultRetries is the default number of retries 24 DefaultRetries = 5 25 // DefaultTendermintEndpoint is the default value for the tendermint endpoint 26 DefaultTendermintEndpoint = "localhost:26657" 27 // DefaultGRPCEndpoint is the default value for the gRPC endpoint 28 DefaultGRPCEndpoint = "localhost:9090" 29 // DefaultNetwork defines the default network name 30 DefaultNetwork = "network" 31 // DefaultOffline defines the default offline value 32 DefaultOffline = false 33 ) 34 35 // configuration flags 36 const ( 37 FlagBlockchain = "blockchain" 38 FlagNetwork = "network" 39 FlagTendermintEndpoint = "tendermint" 40 FlagGRPCEndpoint = "grpc" 41 FlagAddr = "addr" 42 FlagRetries = "retries" 43 FlagOffline = "offline" 44 ) 45 46 // Config defines the configuration of the rosetta server 47 type Config struct { 48 // Blockchain defines the blockchain name 49 // defaults to DefaultBlockchain 50 Blockchain string 51 // Network defines the network name 52 Network string 53 // TendermintRPC defines the endpoint to connect to 54 // tendermint RPC, specifying 'tcp://' before is not 55 // required, usually it's at port 26657 of the 56 TendermintRPC string 57 // GRPCEndpoint defines the cosmos application gRPC endpoint 58 // usually it is located at 9090 port 59 GRPCEndpoint string 60 // Addr defines the default address to bind the rosetta server to 61 // defaults to DefaultAddr 62 Addr string 63 // Retries defines the maximum number of retries 64 // rosetta will do before quitting 65 Retries int 66 // Offline defines if the server must be run in offline mode 67 Offline bool 68 // Codec overrides the default data and construction api client codecs 69 Codec *codec.ProtoCodec 70 // InterfaceRegistry overrides the default data and construction api interface registry 71 InterfaceRegistry codectypes.InterfaceRegistry 72 } 73 74 // NetworkIdentifier returns the network identifier given the configuration 75 func (c *Config) NetworkIdentifier() *types.NetworkIdentifier { 76 return &types.NetworkIdentifier{ 77 Blockchain: c.Blockchain, 78 Network: c.Network, 79 } 80 } 81 82 // validate validates a configuration and sets 83 // its defaults in case they were not provided 84 func (c *Config) validate() error { 85 if (c.Codec == nil) != (c.InterfaceRegistry == nil) { 86 return fmt.Errorf("codec and interface registry must be both different from nil or nil") 87 } 88 89 if c.Addr == "" { 90 c.Addr = DefaultAddr 91 } 92 if c.Blockchain == "" { 93 c.Blockchain = DefaultBlockchain 94 } 95 if c.Retries == 0 { 96 c.Retries = DefaultRetries 97 } 98 // these are must 99 if c.Network == "" { 100 return fmt.Errorf("network not provided") 101 } 102 if c.Offline { 103 return fmt.Errorf("offline mode is not supported for stargate implementation due to how sigv2 works") 104 } 105 106 // these are optional but it must be online 107 if c.GRPCEndpoint == "" { 108 return fmt.Errorf("grpc endpoint not provided") 109 } 110 if c.TendermintRPC == "" { 111 return fmt.Errorf("tendermint rpc not provided") 112 } 113 if !strings.HasPrefix(c.TendermintRPC, "tcp://") { 114 c.TendermintRPC = fmt.Sprintf("tcp://%s", c.TendermintRPC) 115 } 116 117 return nil 118 } 119 120 // WithCodec extends the configuration with a predefined Codec 121 func (c *Config) WithCodec(ir codectypes.InterfaceRegistry, cdc *codec.ProtoCodec) { 122 c.Codec = cdc 123 c.InterfaceRegistry = ir 124 } 125 126 // FromFlags gets the configuration from flags 127 func FromFlags(flags *pflag.FlagSet) (*Config, error) { 128 blockchain, err := flags.GetString(FlagBlockchain) 129 if err != nil { 130 return nil, err 131 } 132 network, err := flags.GetString(FlagNetwork) 133 if err != nil { 134 return nil, err 135 } 136 tendermintRPC, err := flags.GetString(FlagTendermintEndpoint) 137 if err != nil { 138 return nil, err 139 } 140 gRPCEndpoint, err := flags.GetString(FlagGRPCEndpoint) 141 if err != nil { 142 return nil, err 143 } 144 addr, err := flags.GetString(FlagAddr) 145 if err != nil { 146 return nil, err 147 } 148 retries, err := flags.GetInt(FlagRetries) 149 if err != nil { 150 return nil, err 151 } 152 offline, err := flags.GetBool(FlagOffline) 153 if err != nil { 154 return nil, err 155 } 156 conf := &Config{ 157 Blockchain: blockchain, 158 Network: network, 159 TendermintRPC: tendermintRPC, 160 GRPCEndpoint: gRPCEndpoint, 161 Addr: addr, 162 Retries: retries, 163 Offline: offline, 164 } 165 err = conf.validate() 166 if err != nil { 167 return nil, err 168 } 169 return conf, nil 170 } 171 172 func ServerFromConfig(conf *Config) (crg.Server, error) { 173 err := conf.validate() 174 if err != nil { 175 return crg.Server{}, err 176 } 177 client, err := NewClient(conf) 178 if err != nil { 179 return crg.Server{}, err 180 } 181 return crg.NewServer( 182 crg.Settings{ 183 Network: &types.NetworkIdentifier{ 184 Blockchain: conf.Blockchain, 185 Network: conf.Network, 186 }, 187 Client: client, 188 Listen: conf.Addr, 189 Offline: conf.Offline, 190 Retries: conf.Retries, 191 RetryWait: 15 * time.Second, 192 }) 193 } 194 195 // SetFlags sets the configuration flags to the given flagset 196 func SetFlags(flags *pflag.FlagSet) { 197 flags.String(FlagBlockchain, DefaultBlockchain, "the blockchain type") 198 flags.String(FlagNetwork, DefaultNetwork, "the network name") 199 flags.String(FlagTendermintEndpoint, DefaultTendermintEndpoint, "the tendermint rpc endpoint, without tcp://") 200 flags.String(FlagGRPCEndpoint, DefaultGRPCEndpoint, "the app gRPC endpoint") 201 flags.String(FlagAddr, DefaultAddr, "the address rosetta will bind to") 202 flags.Int(FlagRetries, DefaultRetries, "the number of retries that will be done before quitting") 203 flags.Bool(FlagOffline, DefaultOffline, "run rosetta only with construction API") 204 }