diff --git a/bchain/coins/bch/bcashrpc.go b/bchain/coins/bch/bcashrpc.go index bb4292d1..d1f7a9a6 100644 --- a/bchain/coins/bch/bcashrpc.go +++ b/bchain/coins/bch/bcashrpc.go @@ -33,7 +33,7 @@ func NewBCashRPC(config json.RawMessage, pushHandler func(bchain.NotificationTyp // Initialize initializes BCashRPC instance. func (b *BCashRPC) Initialize() error { - chainName, err := b.GetChainInfoAndInitializeMempool() + chainName, err := b.GetChainInfoAndInitializeMempool(b) if err != nil { return err } diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index cac247e8..59ff2777 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -89,6 +89,11 @@ func (c *blockChainWithMetrics) GetSubversion() string { return c.b.GetSubversion() } +func (c *blockChainWithMetrics) GetBlockChainInfo() (v string, err error) { + defer func(s time.Time) { c.observeRPCLatency("GetBlockChainInfo", s, err) }(time.Now()) + return c.b.GetBlockChainInfo() +} + func (c *blockChainWithMetrics) GetBestBlockHash() (v string, err error) { defer func(s time.Time) { c.observeRPCLatency("GetBestBlockHash", s, err) }(time.Now()) return c.b.GetBestBlockHash() diff --git a/bchain/coins/btc/bitcoinrpc.go b/bchain/coins/btc/bitcoinrpc.go index 8a54927d..76a0a864 100644 --- a/bchain/coins/btc/bitcoinrpc.go +++ b/bchain/coins/btc/bitcoinrpc.go @@ -75,9 +75,9 @@ func NewBitcoinRPC(config json.RawMessage, pushHandler func(bchain.NotificationT // GetChainInfoAndInitializeMempool is called by Initialize and reused by other coins // it contacts the blockchain rpc interface for the first time // and if successful it connects to ZeroMQ and creates mempool handler -func (b *BitcoinRPC) GetChainInfoAndInitializeMempool() (string, error) { +func (b *BitcoinRPC) GetChainInfoAndInitializeMempool(bc bchain.BlockChain) (string, error) { // try to connect to block chain and get some info - chainName, err := b.GetBlockChainInfo() + chainName, err := bc.GetBlockChainInfo() if err != nil { return "", err } @@ -89,7 +89,7 @@ func (b *BitcoinRPC) GetChainInfoAndInitializeMempool() (string, error) { } b.mq = mq - b.Mempool = bchain.NewUTXOMempool(b) + b.Mempool = bchain.NewUTXOMempool(bc) return chainName, nil } @@ -97,7 +97,7 @@ func (b *BitcoinRPC) GetChainInfoAndInitializeMempool() (string, error) { // Initialize initializes BitcoinRPC instance. func (b *BitcoinRPC) Initialize() error { - chainName, err := b.GetChainInfoAndInitializeMempool() + chainName, err := b.GetChainInfoAndInitializeMempool(b) if err != nil { return err } diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index 249a7cb9..66ef6219 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -244,6 +244,18 @@ func (b *EthereumRPC) GetSubversion() string { return "" } +// GetBlockChainInfo returns the NetworkID of the ethereum network +func (b *EthereumRPC) GetBlockChainInfo() (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), b.timeout) + defer cancel() + + id, err := b.client.NetworkID(ctx) + if err != nil { + return "", err + } + return id.String(), nil +} + func (b *EthereumRPC) getBestHeader() (*ethtypes.Header, error) { b.bestHeaderMu.Lock() defer b.bestHeaderMu.Unlock() diff --git a/bchain/coins/zec/zcashrpc.go b/bchain/coins/zec/zcashrpc.go index 3d3010f1..0d22f81c 100644 --- a/bchain/coins/zec/zcashrpc.go +++ b/bchain/coins/zec/zcashrpc.go @@ -26,7 +26,7 @@ func NewZCashRPC(config json.RawMessage, pushHandler func(bchain.NotificationTyp // Initialize initializes ZCashRPC instance. func (z *ZCashRPC) Initialize() error { - _, err := z.GetChainInfoAndInitializeMempool() + _, err := z.GetChainInfoAndInitializeMempool(z) if err != nil { return err } diff --git a/bchain/types.go b/bchain/types.go index 2a2abe8d..4e573fd6 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -128,6 +128,7 @@ type BlockChain interface { GetNetworkName() string GetSubversion() string // requests + GetBlockChainInfo() (string, error) GetBestBlockHash() (string, error) GetBestBlockHeight() (uint32, error) GetBlockHash(height uint32) (string, error)