2020-08-03 20:01:38 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2020-08-19 19:26:48 +00:00
|
|
|
"flag"
|
2020-08-03 20:01:38 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/config"
|
2020-08-07 02:42:09 +00:00
|
|
|
consensus "github.com/Secured-Finance/p2p-oracle-node/consensus"
|
2020-08-03 20:01:38 +00:00
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/handler"
|
2020-08-07 02:42:09 +00:00
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/rpc"
|
|
|
|
"github.com/Secured-Finance/p2p-oracle-node/rpcclient"
|
2020-08-03 20:01:38 +00:00
|
|
|
mapset "github.com/deckarep/golang-set"
|
|
|
|
"github.com/ipfs/go-log"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
crypto "github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Host host.Host
|
|
|
|
PubSub *pubsub.PubSub
|
|
|
|
GlobalCtx context.Context
|
|
|
|
GlobalCtxCancel context.CancelFunc
|
|
|
|
OracleTopic string
|
|
|
|
networkTopics mapset.Set
|
|
|
|
handler *handler.Handler
|
|
|
|
Config *config.Config
|
|
|
|
Logger *log.ZapEventLogger
|
2020-08-07 02:42:09 +00:00
|
|
|
Consensus *consensus.RaftConsensus
|
|
|
|
Lotus *rpc.LotusClient
|
|
|
|
Ethereum *rpcclient.EthereumClient
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
func NewNode(configPath string) (*Node, error) {
|
|
|
|
cfg, err := config.NewConfig(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
node := &Node{
|
2020-08-07 02:42:09 +00:00
|
|
|
OracleTopic: "p2p_oracle",
|
2020-08-19 19:26:48 +00:00
|
|
|
Config: cfg,
|
|
|
|
Logger: log.Logger("node"),
|
2020-08-03 20:01:38 +00:00
|
|
|
networkTopics: mapset.NewSet(),
|
|
|
|
}
|
2020-08-06 21:47:58 +00:00
|
|
|
log.SetAllLoggers(log.LevelInfo)
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
return node, nil
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) setupNode(ctx context.Context, prvKey crypto.PrivKey) {
|
|
|
|
listenMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s", node.Config.ListenAddr, node.Config.ListenPort))
|
|
|
|
if err != nil {
|
|
|
|
node.Logger.Fatal("Failed to generate new node multiaddress:", err)
|
|
|
|
}
|
|
|
|
host, err := libp2p.New(
|
|
|
|
ctx,
|
|
|
|
libp2p.ListenAddrs(listenMultiAddr),
|
|
|
|
libp2p.Identity(prvKey),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
node.Logger.Fatal("Failed to set a new libp2p node:", err)
|
|
|
|
}
|
|
|
|
node.Host = host
|
|
|
|
node.startPubSub(ctx, host)
|
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
func Start() error {
|
|
|
|
configPath := flag.String("config", "", "Path to config")
|
|
|
|
verbose := flag.Bool("verbose", false, "Verbose logging")
|
|
|
|
flag.Parse()
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
if *configPath == "" {
|
|
|
|
return fmt.Errorf("no config path provided")
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:48 +00:00
|
|
|
node, err := NewNode(*configPath)
|
|
|
|
if *verbose {
|
|
|
|
log.SetAllLoggers(log.LevelDebug)
|
|
|
|
} else {
|
|
|
|
log.SetAllLoggers(log.LevelInfo)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Logger("node").Panic(err)
|
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
|
|
|
|
r := rand.Reader
|
|
|
|
|
|
|
|
// Creates a new RSA key pair for this host.
|
|
|
|
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
|
|
|
|
if err != nil {
|
|
|
|
node.Logger.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
node.GlobalCtx = ctx
|
|
|
|
node.GlobalCtxCancel = ctxCancel
|
|
|
|
|
|
|
|
node.setupNode(ctx, prvKey)
|
2020-08-19 19:26:48 +00:00
|
|
|
return nil
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|