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"
|
2020-11-15 10:11:07 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-10-21 19:54:40 +00:00
|
|
|
"time"
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
pex "github.com/Secured-Finance/go-libp2p-pex"
|
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
"github.com/Secured-Finance/dione/cache"
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/drand"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/discovery"
|
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
"github.com/Secured-Finance/dione/rpc"
|
|
|
|
rtypes "github.com/Secured-Finance/dione/rpc/types"
|
|
|
|
|
2020-11-18 18:33:03 +00:00
|
|
|
solana2 "github.com/Secured-Finance/dione/rpc/solana"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/rpc/filecoin"
|
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
"github.com/Secured-Finance/dione/types"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/wallet"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
"github.com/Secured-Finance/dione/config"
|
|
|
|
"github.com/Secured-Finance/dione/consensus"
|
2020-11-13 15:51:23 +00:00
|
|
|
"github.com/Secured-Finance/dione/ethclient"
|
2020-11-27 18:47:58 +00:00
|
|
|
pubsub2 "github.com/Secured-Finance/dione/pubsub"
|
2020-08-03 20:01:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
crypto "github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2020-10-21 20:36:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-03 20:01:38 +00:00
|
|
|
)
|
|
|
|
|
2020-11-04 17:46:35 +00:00
|
|
|
const (
|
2020-11-15 10:11:07 +00:00
|
|
|
DefaultPEXUpdateTime = 6 * time.Second
|
2020-11-04 17:46:35 +00:00
|
|
|
)
|
|
|
|
|
2020-08-03 20:01:38 +00:00
|
|
|
type Node struct {
|
2020-10-21 19:54:40 +00:00
|
|
|
Host host.Host
|
2020-11-27 18:47:58 +00:00
|
|
|
PeerDiscovery discovery.Discovery
|
|
|
|
PubSubRouter *pubsub2.PubSubRouter
|
2020-10-21 19:54:40 +00:00
|
|
|
GlobalCtx context.Context
|
|
|
|
GlobalCtxCancel context.CancelFunc
|
|
|
|
Config *config.Config
|
2020-11-12 14:18:30 +00:00
|
|
|
Ethereum *ethclient.EthereumClient
|
2020-10-21 19:54:40 +00:00
|
|
|
ConsensusManager *consensus.PBFTConsensusManager
|
2020-11-14 00:32:50 +00:00
|
|
|
Miner *consensus.Miner
|
|
|
|
Beacon beacon.BeaconNetworks
|
|
|
|
Wallet *wallet.LocalWallet
|
2020-12-04 18:30:03 +00:00
|
|
|
EventLogCache *cache.EventLogCache
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func NewNode(config *config.Config, prvKey crypto.PrivKey, pexDiscoveryUpdateTime time.Duration) (*Node, error) {
|
|
|
|
n := &Node{
|
|
|
|
Config: config,
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize libp2p host
|
|
|
|
lhost, err := provideLibp2pHost(n.Config, prvKey, pexDiscoveryUpdateTime)
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
n.Host = lhost
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize ethereum client
|
2020-11-27 18:47:58 +00:00
|
|
|
ethClient, err := provideEthereumClient(n.Config)
|
2020-11-14 00:32:50 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
n.Ethereum = ethClient
|
2020-11-23 21:59:27 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize blockchain rpc clients
|
2020-11-23 21:59:27 +00:00
|
|
|
err = n.setupRPCClients()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize pubsub subsystem
|
2020-11-27 18:47:58 +00:00
|
|
|
psb := providePubsubRouter(lhost, n.Config)
|
|
|
|
n.PubSubRouter = psb
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize peer discovery
|
|
|
|
peerDiscovery, err := providePeerDiscovery(n.Config, lhost, pexDiscoveryUpdateTime)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
n.PeerDiscovery = peerDiscovery
|
|
|
|
|
|
|
|
// get private key of libp2p host
|
2020-11-27 16:16:08 +00:00
|
|
|
rawPrivKey, err := prvKey.Raw()
|
2020-11-15 13:46:58 +00:00
|
|
|
if err != nil {
|
2020-11-27 16:16:08 +00:00
|
|
|
logrus.Fatal(err)
|
2020-11-15 13:46:58 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize random beacon network subsystem
|
|
|
|
randomBeaconNetwork, err := provideBeacon(psb.Pubsub)
|
2020-11-14 00:32:50 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-12-07 15:22:47 +00:00
|
|
|
n.Beacon = randomBeaconNetwork
|
2020-11-27 18:47:58 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize mining subsystem
|
2020-11-27 18:47:58 +00:00
|
|
|
miner := provideMiner(n.Host.ID(), *n.Ethereum.GetEthAddress(), n.Beacon, n.Ethereum, rawPrivKey)
|
|
|
|
n.Miner = miner
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize event log cache subsystem
|
2020-12-02 13:42:02 +00:00
|
|
|
eventLogCache := provideEventLogCache()
|
|
|
|
n.EventLogCache = eventLogCache
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize consensus subsystem
|
2020-12-02 13:42:02 +00:00
|
|
|
cManager := provideConsensusManager(psb, miner, ethClient, rawPrivKey, n.Config.ConsensusMinApprovals, eventLogCache)
|
2020-11-27 18:47:58 +00:00
|
|
|
n.ConsensusManager = cManager
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
// initialize internal eth wallet
|
2020-11-27 18:47:58 +00:00
|
|
|
wallet, err := provideWallet(n.Host.ID(), rawPrivKey)
|
2020-11-14 00:32:50 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
n.Wallet = wallet
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Run(ctx context.Context) error {
|
|
|
|
n.runLibp2pAsync(ctx)
|
|
|
|
n.subscribeOnEthContractsAsync(ctx)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:03:39 +00:00
|
|
|
// return nil
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) runLibp2pAsync(ctx context.Context) error {
|
|
|
|
logrus.Info(fmt.Sprintf("[*] Your Multiaddress Is: /ip4/%s/tcp/%d/p2p/%s", n.Config.ListenAddr, n.Config.ListenPort, n.Host.ID().Pretty()))
|
|
|
|
|
|
|
|
logrus.Info("Announcing ourselves...")
|
|
|
|
_, err := n.PeerDiscovery.Advertise(context.TODO(), n.Config.Rendezvous)
|
2020-11-27 16:16:08 +00:00
|
|
|
if err != nil {
|
2020-11-27 18:47:58 +00:00
|
|
|
return xerrors.Errorf("failed to announce this node to the network: %v", err)
|
2020-11-27 16:16:08 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
logrus.Info("Successfully announced!")
|
|
|
|
|
|
|
|
// Discover unbounded count of peers
|
|
|
|
logrus.Info("Searching for other peers...")
|
|
|
|
peerChan, err := n.PeerDiscovery.FindPeers(context.TODO(), n.Config.Rendezvous)
|
2020-11-14 00:32:50 +00:00
|
|
|
if err != nil {
|
2020-11-27 18:47:58 +00:00
|
|
|
return xerrors.Errorf("failed to find new peers: %v", err)
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
go func() {
|
|
|
|
MainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break MainLoop
|
|
|
|
case newPeer := <-peerChan:
|
|
|
|
{
|
|
|
|
if len(newPeer.Addrs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if newPeer.ID.String() == n.Host.ID().String() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.Infof("Found peer: %s", newPeer)
|
|
|
|
// Connect to the peer
|
|
|
|
if err := n.Host.Connect(ctx, newPeer); err != nil {
|
|
|
|
logrus.Warn("Connection failed: ", err)
|
|
|
|
}
|
|
|
|
logrus.Info("Connected to newly discovered peer: ", newPeer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func (n *Node) subscribeOnEthContractsAsync(ctx context.Context) {
|
|
|
|
eventChan, subscription, err := n.Ethereum.SubscribeOnOracleEvents(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal("Couldn't subscribe on ethereum contracts, exiting... ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
EventLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-eventChan:
|
|
|
|
{
|
2020-11-30 21:38:54 +00:00
|
|
|
err := n.EventLogCache.Store("request_"+event.RequestID.String(), event)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to store new request event to event log cache: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-12-02 13:42:02 +00:00
|
|
|
logrus.Info("Let's wait a little so that all nodes have time to receive the request and cache it")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
task, err := n.Miner.MineTask(context.TODO(), event)
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal("Failed to mine task, exiting... ", err)
|
|
|
|
}
|
|
|
|
if task == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-02 13:42:02 +00:00
|
|
|
logrus.Infof("Proposed new Dione task with ID: %s", event.RequestID.String())
|
|
|
|
err = n.ConsensusManager.Propose(event.RequestID.String(), *task, event)
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to propose task: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break EventLoop
|
|
|
|
case <-subscription.Err():
|
|
|
|
logrus.Fatal("Error with ethereum subscription, exiting... ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
func provideEventLogCache() *cache.EventLogCache {
|
|
|
|
return cache.NewEventLogCache()
|
2020-11-30 21:38:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func provideMiner(peerID peer.ID, ethAddress common.Address, beacon beacon.BeaconNetworks, ethClient *ethclient.EthereumClient, privateKey []byte) *consensus.Miner {
|
|
|
|
return consensus.NewMiner(peerID, ethAddress, beacon, ethClient, privateKey)
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func provideBeacon(ps *pubsub.PubSub) (beacon.BeaconNetworks, error) {
|
|
|
|
networks := beacon.BeaconNetworks{}
|
|
|
|
bc, err := drand.NewDrandBeacon(config.ChainGenesis, config.TaskEpochInterval, ps)
|
2020-11-14 00:32:50 +00:00
|
|
|
if err != nil {
|
2020-11-27 18:47:58 +00:00
|
|
|
return nil, fmt.Errorf("failed to setup drand beacon: %w", err)
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
networks = append(networks, beacon.BeaconNetwork{Start: types.DrandRound(config.ChainGenesis), Beacon: bc})
|
|
|
|
// NOTE: currently we use only one network
|
|
|
|
return networks, nil
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func provideWallet(peerID peer.ID, privKey []byte) (*wallet.LocalWallet, error) {
|
2020-11-14 00:32:50 +00:00
|
|
|
// TODO make persistent keystore
|
|
|
|
kstore := wallet.NewMemKeyStore()
|
|
|
|
keyInfo := types.KeyInfo{
|
|
|
|
Type: types.KTEd25519,
|
2020-11-27 16:16:08 +00:00
|
|
|
PrivateKey: privKey,
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
kstore.Put(wallet.KNamePrefix+peerID.String(), keyInfo)
|
2020-11-14 00:32:50 +00:00
|
|
|
w, err := wallet.NewWallet(kstore)
|
|
|
|
if err != nil {
|
2020-11-27 18:47:58 +00:00
|
|
|
return nil, xerrors.Errorf("failed to setup wallet: %w", err)
|
2020-11-14 00:32:50 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
return w, nil
|
2020-11-12 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func provideEthereumClient(config *config.Config) (*ethclient.EthereumClient, error) {
|
2020-11-12 14:18:30 +00:00
|
|
|
ethereum := ethclient.NewEthereumClient()
|
2020-11-27 18:47:58 +00:00
|
|
|
err := ethereum.Initialize(context.Background(),
|
|
|
|
config.Ethereum.GatewayAddress,
|
|
|
|
config.Ethereum.PrivateKey,
|
|
|
|
config.Ethereum.OracleEmitterContractAddress,
|
|
|
|
config.Ethereum.AggregatorContractAddress,
|
|
|
|
config.Ethereum.DioneStakingContractAddress,
|
2021-03-05 18:40:17 +00:00
|
|
|
config.Ethereum.DisputeContractAddress,
|
2020-10-20 18:18:36 +00:00
|
|
|
)
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to initialize ethereum client: %v", err)
|
|
|
|
}
|
|
|
|
return ethereum, nil
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
func (n *Node) setupRPCClients() error {
|
|
|
|
fc := filecoin.NewLotusClient()
|
2020-12-02 14:03:39 +00:00
|
|
|
rpc.RegisterRPC(rtypes.RPCTypeFilecoin, map[string]func(string) ([]byte, error){
|
2020-11-23 21:59:27 +00:00
|
|
|
"getTransaction": fc.GetTransaction,
|
|
|
|
})
|
|
|
|
|
|
|
|
sl := solana2.NewSolanaClient()
|
2020-12-02 14:03:39 +00:00
|
|
|
rpc.RegisterRPC(rtypes.RPCTypeSolana, map[string]func(string) ([]byte, error){
|
2020-11-23 21:59:27 +00:00
|
|
|
"getTransaction": sl.GetTransaction,
|
|
|
|
})
|
2020-10-20 18:18:36 +00:00
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
return nil
|
2020-11-14 11:25:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func providePubsubRouter(lhost host.Host, config *config.Config) *pubsub2.PubSubRouter {
|
2020-12-07 15:22:47 +00:00
|
|
|
return pubsub2.NewPubSubRouter(lhost, config.PubSub.ServiceTopicName, config.IsBootstrap)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:30:03 +00:00
|
|
|
func provideConsensusManager(psb *pubsub2.PubSubRouter, miner *consensus.Miner, ethClient *ethclient.EthereumClient, privateKey []byte, minApprovals int, evc *cache.EventLogCache) *consensus.PBFTConsensusManager {
|
2020-12-02 13:42:02 +00:00
|
|
|
return consensus.NewPBFTConsensusManager(psb, minApprovals, privateKey, ethClient, miner, evc)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
func provideLibp2pHost(config *config.Config, privateKey crypto.PrivKey, pexDiscoveryUpdateTime time.Duration) (host.Host, error) {
|
2020-11-27 18:47:58 +00:00
|
|
|
listenMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", config.ListenAddr, config.ListenPort))
|
2020-10-21 19:54:40 +00:00
|
|
|
if err != nil {
|
2020-12-07 15:22:47 +00:00
|
|
|
return nil, xerrors.Errorf("failed to parse multiaddress: %v", err)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
host, err := libp2p.New(
|
2020-11-27 18:47:58 +00:00
|
|
|
context.TODO(),
|
2020-10-21 19:54:40 +00:00
|
|
|
libp2p.ListenAddrs(listenMultiAddr),
|
|
|
|
libp2p.Identity(privateKey),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-12-07 15:22:47 +00:00
|
|
|
return nil, xerrors.Errorf("failed to setup libp2p host: %v", err)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func providePeerDiscovery(config *config.Config, h host.Host, pexDiscoveryUpdateTime time.Duration) (discovery.Discovery, error) {
|
2020-11-04 17:46:35 +00:00
|
|
|
var bootstrapMaddrs []multiaddr.Multiaddr
|
2020-11-27 18:47:58 +00:00
|
|
|
for _, a := range config.BootstrapNodes {
|
2020-11-04 17:46:35 +00:00
|
|
|
maddr, err := multiaddr.NewMultiaddr(a)
|
2020-10-20 18:18:36 +00:00
|
|
|
if err != nil {
|
2020-12-07 15:22:47 +00:00
|
|
|
return nil, xerrors.Errorf("invalid multiaddress of bootstrap node: %v", err)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
2020-11-04 17:46:35 +00:00
|
|
|
bootstrapMaddrs = append(bootstrapMaddrs, maddr)
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
|
|
|
|
if config.IsBootstrap {
|
2020-11-15 05:59:46 +00:00
|
|
|
bootstrapMaddrs = nil
|
|
|
|
}
|
2020-12-07 15:22:47 +00:00
|
|
|
|
|
|
|
pexDiscovery, err := pex.NewPEXDiscovery(h, bootstrapMaddrs, pexDiscoveryUpdateTime)
|
2020-11-13 15:51:23 +00:00
|
|
|
if err != nil {
|
2020-12-07 15:22:47 +00:00
|
|
|
return nil, xerrors.Errorf("failed to setup pex pexDiscovery: %v", err)
|
2020-11-13 15:51:23 +00:00
|
|
|
}
|
2020-11-04 17:46:35 +00:00
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
return pexDiscovery, nil
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
func Start() {
|
2020-08-19 19:26:48 +00:00
|
|
|
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 == "" {
|
2020-11-27 18:47:58 +00:00
|
|
|
logrus.Fatal("no config path provided")
|
2020-08-19 19:26:48 +00:00
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
cfg, err := config.NewConfig(*configPath)
|
2020-08-19 19:26:48 +00:00
|
|
|
if err != nil {
|
2020-11-27 18:47:58 +00:00
|
|
|
logrus.Fatalf("failed to load config: %v", err)
|
2020-08-19 19:26:48 +00:00
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-11-15 10:11:07 +00:00
|
|
|
var privateKey crypto.PrivKey
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
if cfg.IsBootstrap {
|
2020-11-15 10:11:07 +00:00
|
|
|
if _, err := os.Stat(".bootstrap_privkey"); os.IsNotExist(err) {
|
|
|
|
privateKey, err = generatePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := os.Create(".bootstrap_privkey")
|
|
|
|
r, _ := privateKey.Raw()
|
|
|
|
f.Write(r)
|
|
|
|
} else {
|
|
|
|
pkey, _ := ioutil.ReadFile(".bootstrap_privkey")
|
|
|
|
privateKey, _ = crypto.UnmarshalEd25519PrivateKey(pkey)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
privateKey, err = generatePrivateKey()
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
node, err := NewNode(cfg, privateKey, DefaultPEXUpdateTime)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
// log
|
|
|
|
if *verbose {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
} else {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:22:47 +00:00
|
|
|
//log.SetDebugLogging()
|
|
|
|
|
2020-11-27 18:47:58 +00:00
|
|
|
//ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
//node.GlobalCtx = ctx
|
|
|
|
//node.GlobalCtxCancel = ctxCancel
|
|
|
|
|
|
|
|
err = node.Run(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
2020-10-20 18:18:36 +00:00
|
|
|
|
|
|
|
func generatePrivateKey() (crypto.PrivKey, error) {
|
|
|
|
r := rand.Reader
|
|
|
|
// Creates a new RSA key pair for this host.
|
2020-11-14 00:32:50 +00:00
|
|
|
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.Ed25519, 2048, r)
|
2020-10-20 18:18:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return prvKey, nil
|
|
|
|
}
|