Detect ethereum network type
This commit is contained in:
parent
971b6397d6
commit
fc77df3004
@ -5,25 +5,35 @@ import (
|
|||||||
"blockbook/common"
|
"blockbook/common"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"math/big"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
|
|
||||||
|
ethcommon "github.com/ethereum/go-ethereum/common"
|
||||||
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethclient"
|
"github.com/ethereum/go-ethereum/ethclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type EthereumNet uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
MainNet EthereumNet = 1
|
||||||
|
TestNet EthereumNet = 3
|
||||||
|
)
|
||||||
|
|
||||||
// EthRPC is an interface to JSON-RPC eth service.
|
// EthRPC is an interface to JSON-RPC eth service.
|
||||||
type EthRPC struct {
|
type EthRPC struct {
|
||||||
client *ethclient.Client
|
client *ethclient.Client
|
||||||
ctx context.Context
|
timeout time.Duration
|
||||||
ctxCancel context.CancelFunc
|
rpcURL string
|
||||||
rpcURL string
|
Parser *EthParser
|
||||||
Parser *EthParser
|
Testnet bool
|
||||||
Testnet bool
|
Network string
|
||||||
Network string
|
Mempool *bchain.Mempool
|
||||||
Mempool *bchain.Mempool
|
metrics *common.Metrics
|
||||||
metrics *common.Metrics
|
bestHeader *ethtypes.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
type configuration struct {
|
type configuration struct {
|
||||||
@ -43,43 +53,45 @@ func NewEthRPC(config json.RawMessage, pushHandler func(*bchain.MQMessage), metr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.RPCTimeout)*time.Second)
|
|
||||||
s := &EthRPC{
|
s := &EthRPC{
|
||||||
client: ec,
|
client: ec,
|
||||||
ctx: ctx,
|
rpcURL: c.RPCURL,
|
||||||
ctxCancel: cancel,
|
metrics: metrics,
|
||||||
rpcURL: c.RPCURL,
|
|
||||||
metrics: metrics,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// always create parser
|
// always create parser
|
||||||
s.Parser = &EthParser{}
|
s.Parser = &EthParser{}
|
||||||
|
s.timeout = time.Duration(c.RPCTimeout) * time.Second
|
||||||
|
|
||||||
h, err := ec.HeaderByNumber(s.ctx, nil)
|
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
id, err := ec.NetworkID(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
glog.Info("best block ", h.Number)
|
|
||||||
|
|
||||||
// // parameters for getInfo request
|
// parameters for getInfo request
|
||||||
// if s.Parser.Params.Net == wire.MainNet {
|
switch EthereumNet(id.Uint64()) {
|
||||||
// s.Testnet = false
|
case MainNet:
|
||||||
// s.Network = "livenet"
|
s.Testnet = false
|
||||||
// } else {
|
s.Network = "livenet"
|
||||||
// s.Testnet = true
|
break
|
||||||
// s.Network = "testnet"
|
case TestNet:
|
||||||
// }
|
s.Testnet = true
|
||||||
|
s.Network = "testnet"
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return nil, errors.Errorf("Unknown network id %v", id)
|
||||||
|
}
|
||||||
|
glog.Info("rpc: block chain ", s.Network)
|
||||||
|
|
||||||
// s.Mempool = bchain.NewMempool(s, metrics)
|
// s.Mempool = bchain.NewMempool(s, metrics)
|
||||||
|
|
||||||
// glog.Info("rpc: block chain ", s.Parser.Params.Name)
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *EthRPC) Shutdown() error {
|
func (b *EthRPC) Shutdown() error {
|
||||||
if b.ctxCancel != nil {
|
|
||||||
b.ctxCancel()
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user