Put rpc config to external file

This commit is contained in:
Martin Boehm 2018-03-19 12:05:16 +01:00
parent f336217c1d
commit 971b6397d6
2 changed files with 30 additions and 6 deletions

View File

@ -4,9 +4,11 @@ import (
"blockbook/bchain"
"blockbook/common"
"context"
"encoding/json"
"time"
"github.com/golang/glog"
"github.com/juju/errors"
"github.com/ethereum/go-ethereum/ethclient"
)
@ -24,25 +26,36 @@ type EthRPC struct {
metrics *common.Metrics
}
type configuration struct {
RPCURL string `json:"rpcURL"`
RPCTimeout int `json:"rpcTimeout"`
}
// NewEthRPC returns new EthRPC instance.
func NewEthRPC(url string, user string, password string, timeout time.Duration, parse bool, metrics *common.Metrics) (bchain.BlockChain, error) {
c, err := ethclient.Dial(url)
func NewEthRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage), metrics *common.Metrics) (bchain.BlockChain, error) {
var err error
var c configuration
err = json.Unmarshal(config, &c)
if err != nil {
return nil, errors.Annotatef(err, "Invalid configuration file")
}
ec, err := ethclient.Dial(c.RPCURL)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.RPCTimeout)*time.Second)
s := &EthRPC{
client: c,
client: ec,
ctx: ctx,
ctxCancel: cancel,
rpcURL: url,
rpcURL: c.RPCURL,
metrics: metrics,
}
// always create parser
s.Parser = &EthParser{}
h, err := c.HeaderByNumber(s.ctx, nil)
h, err := ec.HeaderByNumber(s.ctx, nil)
if err != nil {
return nil, err
}
@ -63,6 +76,13 @@ func NewEthRPC(url string, user string, password string, timeout time.Duration,
return s, nil
}
func (b *EthRPC) Shutdown() error {
if b.ctxCancel != nil {
b.ctxCancel()
}
return nil
}
func (b *EthRPC) IsTestnet() bool {
panic("not implemented")
}

4
configs/eth-testnet.json Normal file
View File

@ -0,0 +1,4 @@
{
"rpcURL": "ws://10.34.3.4:18546",
"rpcTimeout": 25
}