Take coin name from rpc config json

This commit is contained in:
Martin Boehm 2018-06-05 16:14:46 +02:00
parent 5d3b1faa68
commit eb716d69ed
5 changed files with 62 additions and 26 deletions

View File

@ -22,22 +22,34 @@ type blockChainFactory func(config json.RawMessage, pushHandler func(bchain.Noti
var blockChainFactories = make(map[string]blockChainFactory) var blockChainFactories = make(map[string]blockChainFactory)
func init() { func init() {
blockChainFactories["btc"] = btc.NewBitcoinRPC blockChainFactories["Bitcoin"] = btc.NewBitcoinRPC
blockChainFactories["btc-testnet"] = btc.NewBitcoinRPC blockChainFactories["Testnet"] = btc.NewBitcoinRPC
blockChainFactories["zec"] = zec.NewZCashRPC blockChainFactories["zec"] = zec.NewZCashRPC
blockChainFactories["zec-testnet"] = zec.NewZCashRPC blockChainFactories["zec-testnet"] = zec.NewZCashRPC
blockChainFactories["eth"] = eth.NewEthereumRPC blockChainFactories["Ethereum"] = eth.NewEthereumRPC
blockChainFactories["eth-testnet"] = eth.NewEthereumRPC blockChainFactories["Ethereum Testnet Ropsten"] = eth.NewEthereumRPC
blockChainFactories["bch"] = bch.NewBCashRPC blockChainFactories["bch"] = bch.NewBCashRPC
blockChainFactories["bch-testnet"] = bch.NewBCashRPC blockChainFactories["bch-testnet"] = bch.NewBCashRPC
} }
// GetCoinNameFromConfig gets coin name from config file
func GetCoinNameFromConfig(configfile string) (string, error) {
data, err := ioutil.ReadFile(configfile)
if err != nil {
return "", errors.Annotatef(err, "Error reading file %v", configfile)
}
var cn struct {
CoinName string `json:"coin_name"`
}
err = json.Unmarshal(data, &cn)
if err != nil {
return "", errors.Annotatef(err, "Error parsing file %v", configfile)
}
return cn.CoinName, nil
}
// NewBlockChain creates bchain.BlockChain of type defined by parameter coin // NewBlockChain creates bchain.BlockChain of type defined by parameter coin
func NewBlockChain(coin string, configfile string, pushHandler func(bchain.NotificationType), metrics *common.Metrics) (bchain.BlockChain, error) { func NewBlockChain(coin string, configfile string, pushHandler func(bchain.NotificationType), metrics *common.Metrics) (bchain.BlockChain, error) {
bcf, ok := blockChainFactories[coin]
if !ok {
return nil, errors.New(fmt.Sprint("Unsupported coin ", coin, ". Must be one of ", reflect.ValueOf(blockChainFactories).MapKeys()))
}
data, err := ioutil.ReadFile(configfile) data, err := ioutil.ReadFile(configfile)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "Error reading file %v", configfile) return nil, errors.Annotatef(err, "Error reading file %v", configfile)
@ -47,6 +59,10 @@ func NewBlockChain(coin string, configfile string, pushHandler func(bchain.Notif
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "Error parsing file %v", configfile) return nil, errors.Annotatef(err, "Error parsing file %v", configfile)
} }
bcf, ok := blockChainFactories[coin]
if !ok {
return nil, errors.New(fmt.Sprint("Unsupported coin '", coin, "'. Must be one of ", reflect.ValueOf(blockChainFactories).MapKeys()))
}
bc, err := bcf(config, pushHandler) bc, err := bcf(config, pushHandler)
if err != nil { if err != nil {
return nil, err return nil, err
@ -87,6 +103,10 @@ func (c *blockChainWithMetrics) GetNetworkName() string {
return c.b.GetNetworkName() return c.b.GetNetworkName()
} }
func (c *blockChainWithMetrics) GetCoinName() string {
return c.b.GetCoinName()
}
func (c *blockChainWithMetrics) GetSubversion() string { func (c *blockChainWithMetrics) GetSubversion() string {
return c.b.GetSubversion() return c.b.GetSubversion()
} }

View File

@ -35,6 +35,7 @@ type BitcoinRPC struct {
} }
type Configuration struct { type Configuration struct {
CoinName string `json:"coin_name"`
RPCURL string `json:"rpcURL"` RPCURL string `json:"rpcURL"`
RPCUser string `json:"rpcUser"` RPCUser string `json:"rpcUser"`
RPCPass string `json:"rpcPass"` RPCPass string `json:"rpcPass"`
@ -154,6 +155,10 @@ func (b *BitcoinRPC) GetNetworkName() string {
return b.Network return b.Network
} }
func (b *BitcoinRPC) GetCoinName() string {
return b.ChainConfig.CoinName
}
func (b *BitcoinRPC) GetSubversion() string { func (b *BitcoinRPC) GetSubversion() string {
return b.ChainConfig.Subversion return b.ChainConfig.Subversion
} }

View File

@ -29,6 +29,12 @@ const (
TestNet EthereumNet = 3 TestNet EthereumNet = 3
) )
type Configuration struct {
CoinName string `json:"coin_name"`
RPCURL string `json:"rpcURL"`
RPCTimeout int `json:"rpcTimeout"`
}
// EthereumRPC is an interface to JSON-RPC eth service. // EthereumRPC is an interface to JSON-RPC eth service.
type EthereumRPC struct { type EthereumRPC struct {
client *ethclient.Client client *ethclient.Client
@ -36,6 +42,7 @@ type EthereumRPC struct {
timeout time.Duration timeout time.Duration
rpcURL string rpcURL string
Parser *EthereumParser Parser *EthereumParser
CoinName string
Testnet bool Testnet bool
Network string Network string
Mempool *bchain.NonUTXOMempool Mempool *bchain.NonUTXOMempool
@ -45,17 +52,13 @@ type EthereumRPC struct {
newBlockSubscription *rpc.ClientSubscription newBlockSubscription *rpc.ClientSubscription
chanNewTx chan ethcommon.Hash chanNewTx chan ethcommon.Hash
newTxSubscription *rpc.ClientSubscription newTxSubscription *rpc.ClientSubscription
} ChainConfig *Configuration
type configuration struct {
RPCURL string `json:"rpcURL"`
RPCTimeout int `json:"rpcTimeout"`
} }
// NewEthereumRPC returns new EthRPC instance. // NewEthereumRPC returns new EthRPC instance.
func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
var err error var err error
var c configuration var c Configuration
err = json.Unmarshal(config, &c) err = json.Unmarshal(config, &c)
if err != nil { if err != nil {
return nil, errors.Annotatef(err, "Invalid configuration file") return nil, errors.Annotatef(err, "Invalid configuration file")
@ -67,9 +70,9 @@ func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.Notification
ec := ethclient.NewClient(rc) ec := ethclient.NewClient(rc)
s := &EthereumRPC{ s := &EthereumRPC{
client: ec, client: ec,
rpc: rc, rpc: rc,
rpcURL: c.RPCURL, ChainConfig: &c,
} }
// always create parser // always create parser
@ -240,6 +243,10 @@ func (b *EthereumRPC) GetNetworkName() string {
return b.Network return b.Network
} }
func (b *EthereumRPC) GetCoinName() string {
return b.ChainConfig.CoinName
}
func (b *EthereumRPC) GetSubversion() string { func (b *EthereumRPC) GetSubversion() string {
return "" return ""
} }

View File

@ -121,6 +121,7 @@ type BlockChain interface {
IsTestnet() bool IsTestnet() bool
GetNetworkName() string GetNetworkName() string
GetSubversion() string GetSubversion() string
GetCoinName() string
// requests // requests
GetBlockChainInfo() (string, error) GetBlockChainInfo() (string, error)
GetBestBlockHash() (string, error) GetBestBlockHash() (string, error)

View File

@ -67,8 +67,6 @@ var (
explorerURL = flag.String("explorer", "", "address of blockchain explorer") explorerURL = flag.String("explorer", "", "address of blockchain explorer")
coin = flag.String("coin", "btc", "coin name")
noTxCache = flag.Bool("notxcache", false, "disable tx cache") noTxCache = flag.Bool("notxcache", false, "disable tx cache")
) )
@ -141,16 +139,21 @@ func main() {
return return
} }
metrics, err := common.GetMetrics(*coin)
if err != nil {
glog.Fatal("GetMetrics: ", err)
}
if *blockchain == "" { if *blockchain == "" {
glog.Fatal("Missing blockchaincfg configuration parameter") glog.Fatal("Missing blockchaincfg configuration parameter")
} }
if chain, err = getBlockChainWithRetry(*coin, *blockchain, pushSynchronizationHandler, metrics, 60); err != nil { coin, err := coins.GetCoinNameFromConfig(*blockchain)
if err != nil {
glog.Fatal("config: ", err)
}
metrics, err := common.GetMetrics(coin)
if err != nil {
glog.Fatal("metrics: ", err)
}
if chain, err = getBlockChainWithRetry(coin, *blockchain, pushSynchronizationHandler, metrics, 60); err != nil {
glog.Fatal("rpc: ", err) glog.Fatal("rpc: ", err)
} }
@ -160,7 +163,7 @@ func main() {
} }
defer index.Close() defer index.Close()
internalState, err = newInternalState(*coin, index) internalState, err = newInternalState(coin, index)
if err != nil { if err != nil {
glog.Fatal("internalState: ", err) glog.Fatal("internalState: ", err)
} }