Do not internally convert errors into panic

pull/1638/head
Eugene Kim 5 years ago
parent 6a3c0b256d
commit 59d1939d40
  1. 27
      api/service/networkinfo/service.go
  2. 5
      api/service/networkinfo/service_test.go
  3. 8
      node/service_setup.go

@ -8,6 +8,7 @@ import (
"time"
manet "github.com/multiformats/go-multiaddr-net"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/rpc"
msg_pb "github.com/harmony-one/harmony/api/proto/message"
@ -17,6 +18,7 @@ import (
coredis "github.com/libp2p/go-libp2p-core/discovery"
libp2pdis "github.com/libp2p/go-libp2p-discovery"
libp2pdht "github.com/libp2p/go-libp2p-kad-dht"
libp2pdhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
peerstore "github.com/libp2p/go-libp2p-peerstore"
)
@ -57,15 +59,22 @@ const (
)
// New returns role conversion service.
func New(h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer, bootnodes utils.AddrList) *Service {
func New(
h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer,
bootnodes utils.AddrList,
) (*Service, error) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), connectionTimeout)
dataStore, err := badger.NewDatastore(fmt.Sprintf(".dht-%s-%s", h.GetSelfPeer().IP, h.GetSelfPeer().Port), nil)
if err != nil {
panic(err)
return nil, errors.Wrapf(err,
"cannot open Badger datastore at %s", dataStorePath)
}
dht := libp2pdht.NewDHT(ctx, h.GetP2PHost(), dataStore)
dht, err := libp2pdht.New(ctx, h.GetP2PHost(), libp2pdhtopts.Datastore(dataStore))
if err != nil {
return nil, errors.Wrapf(err, "cannot create DHT")
}
return &Service{
Host: h,
@ -78,7 +87,19 @@ func New(h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer, bootnodes u
bootnodes: bootnodes,
discovery: nil,
started: false,
}, nil
}
// MustNew is a panic-on-error version of New.
func MustNew(
h p2p.Host, rendezvous p2p.GroupID, peerChan chan p2p.Peer,
bootnodes utils.AddrList,
) *Service {
service, err := New(h, rendezvous, peerChan, bootnodes)
if err != nil {
panic(err)
}
return service
}
// StartService starts network info service.

@ -28,7 +28,10 @@ func TestService(t *testing.T) {
t.Fatal("unable to new host in harmony")
}
s := New(host, p2p.GroupIDBeaconClient, nil, nil)
s, err := New(host, p2p.GroupIDBeaconClient, nil, nil, "")
if err != nil {
t.Fatalf("New() failed: %s", err)
}
s.StartService()

@ -22,7 +22,7 @@ func (node *Node) setupForValidator() {
// Register peer discovery service. No need to do staking for beacon chain node.
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer))
// Register networkinfo service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil))
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.MustNew(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil))
// Register consensus service.
node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus))
// Register new block service.
@ -51,7 +51,7 @@ func (node *Node) setupForNewNode() {
// Register peer discovery service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, node.AddBeaconPeer))
// Register networkinfo service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetBeaconGroupID(), chanPeer, nil))
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.MustNew(node.host, node.NodeConfig.GetBeaconGroupID(), chanPeer, nil))
// Register new metrics service
if node.NodeConfig.GetMetricsFlag() {
node.serviceManager.RegisterService(service.Metrics, metrics.New(&node.SelfPeer, node.NodeConfig.ConsensusPubKey.SerializeToHexStr(), node.NodeConfig.GetPushgatewayIP(), node.NodeConfig.GetPushgatewayPort()))
@ -60,7 +60,7 @@ func (node *Node) setupForNewNode() {
func (node *Node) setupForClientNode() {
// Register networkinfo service. "0" is the beacon shard ID
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, p2p.GroupIDBeacon, nil, nil))
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.MustNew(node.host, p2p.GroupIDBeacon, nil, nil))
}
func (node *Node) setupForExplorerNode() {
@ -69,7 +69,7 @@ func (node *Node) setupForExplorerNode() {
// Register peer discovery service.
node.serviceManager.RegisterService(service.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer, nil))
// Register networkinfo service.
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil))
node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.MustNew(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil))
// Register explorer service.
node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.NodeConfig.GetShardID(), node.Consensus.GetNodeIDs, node.GetBalanceOfAddress))
// Register explorer service.

Loading…
Cancel
Save