Take coin name from rpc config json

pull/7/head
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)
func init() {
blockChainFactories["btc"] = btc.NewBitcoinRPC
blockChainFactories["btc-testnet"] = btc.NewBitcoinRPC
blockChainFactories["Bitcoin"] = btc.NewBitcoinRPC
blockChainFactories["Testnet"] = btc.NewBitcoinRPC
blockChainFactories["zec"] = zec.NewZCashRPC
blockChainFactories["zec-testnet"] = zec.NewZCashRPC
blockChainFactories["eth"] = eth.NewEthereumRPC
blockChainFactories["eth-testnet"] = eth.NewEthereumRPC
blockChainFactories["Ethereum"] = eth.NewEthereumRPC
blockChainFactories["Ethereum Testnet Ropsten"] = eth.NewEthereumRPC
blockChainFactories["bch"] = 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
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)
if err != nil {
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 {
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)
if err != nil {
return nil, err
@ -87,6 +103,10 @@ func (c *blockChainWithMetrics) GetNetworkName() string {
return c.b.GetNetworkName()
}
func (c *blockChainWithMetrics) GetCoinName() string {
return c.b.GetCoinName()
}
func (c *blockChainWithMetrics) GetSubversion() string {
return c.b.GetSubversion()
}

View File

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

View File

@ -29,6 +29,12 @@ const (
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.
type EthereumRPC struct {
client *ethclient.Client
@ -36,6 +42,7 @@ type EthereumRPC struct {
timeout time.Duration
rpcURL string
Parser *EthereumParser
CoinName string
Testnet bool
Network string
Mempool *bchain.NonUTXOMempool
@ -45,17 +52,13 @@ type EthereumRPC struct {
newBlockSubscription *rpc.ClientSubscription
chanNewTx chan ethcommon.Hash
newTxSubscription *rpc.ClientSubscription
}
type configuration struct {
RPCURL string `json:"rpcURL"`
RPCTimeout int `json:"rpcTimeout"`
ChainConfig *Configuration
}
// NewEthereumRPC returns new EthRPC instance.
func NewEthereumRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
var err error
var c configuration
var c Configuration
err = json.Unmarshal(config, &c)
if err != nil {
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)
s := &EthereumRPC{
client: ec,
rpc: rc,
rpcURL: c.RPCURL,
client: ec,
rpc: rc,
ChainConfig: &c,
}
// always create parser
@ -240,6 +243,10 @@ func (b *EthereumRPC) GetNetworkName() string {
return b.Network
}
func (b *EthereumRPC) GetCoinName() string {
return b.ChainConfig.CoinName
}
func (b *EthereumRPC) GetSubversion() string {
return ""
}

View File

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

View File

@ -67,8 +67,6 @@ var (
explorerURL = flag.String("explorer", "", "address of blockchain explorer")
coin = flag.String("coin", "btc", "coin name")
noTxCache = flag.Bool("notxcache", false, "disable tx cache")
)
@ -141,16 +139,21 @@ func main() {
return
}
metrics, err := common.GetMetrics(*coin)
if err != nil {
glog.Fatal("GetMetrics: ", err)
}
if *blockchain == "" {
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)
}
@ -160,7 +163,7 @@ func main() {
}
defer index.Close()
internalState, err = newInternalState(*coin, index)
internalState, err = newInternalState(coin, index)
if err != nil {
glog.Fatal("internalState: ", err)
}