2020-08-03 20:01:38 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-21 19:54:40 +00:00
|
|
|
"time"
|
2020-08-03 20:01:38 +00:00
|
|
|
|
2021-08-24 19:41:40 +00:00
|
|
|
"github.com/asaskevich/EventBus"
|
|
|
|
|
2021-07-26 21:18:16 +00:00
|
|
|
"github.com/Secured-Finance/dione/blockchain"
|
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
drand2 "github.com/Secured-Finance/dione/beacon/drand"
|
2021-07-11 00:32:58 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
"github.com/Secured-Finance/dione/pubsub"
|
2021-07-11 00:08:03 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
"github.com/Secured-Finance/dione/consensus"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/blockchain/sync"
|
|
|
|
|
|
|
|
"go.uber.org/fx"
|
2021-06-11 11:40:32 +00:00
|
|
|
|
2021-06-10 20:32:39 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/types"
|
|
|
|
|
2021-06-02 19:48:56 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
|
|
|
|
2021-05-26 21:06:46 +00:00
|
|
|
"github.com/Secured-Finance/dione/blockchain/pool"
|
2020-11-27 18:47:58 +00:00
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/discovery"
|
|
|
|
|
2020-11-23 21:59:27 +00:00
|
|
|
"github.com/Secured-Finance/dione/rpc"
|
2020-11-18 18:33:03 +00:00
|
|
|
|
2020-11-14 00:32:50 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
"github.com/Secured-Finance/dione/config"
|
2020-11-13 15:51:23 +00:00
|
|
|
"github.com/Secured-Finance/dione/ethclient"
|
2020-08-03 20:01:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
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
|
|
|
)
|
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
func runNode(
|
|
|
|
lc fx.Lifecycle,
|
|
|
|
cfg *config.Config,
|
|
|
|
disco discovery.Discovery,
|
2021-08-25 19:10:44 +00:00
|
|
|
ethClient ethclient.EthereumSideAPI,
|
2021-07-21 21:56:58 +00:00
|
|
|
h host.Host,
|
|
|
|
mp *pool.Mempool,
|
|
|
|
syncManager sync.SyncManager,
|
2021-08-24 19:41:40 +00:00
|
|
|
consensusManager *consensus.ConsensusHandler,
|
2021-07-21 21:56:58 +00:00
|
|
|
pubSubRouter *pubsub.PubSubRouter,
|
|
|
|
disputeManager *consensus.DisputeManager,
|
|
|
|
db *drand2.DrandBeacon,
|
|
|
|
) {
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
err := runLibp2pAsync(context.TODO(), h, cfg, disco)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-11 00:08:03 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
err = db.Run(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-27 18:47:58 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
// Run pubsub router
|
|
|
|
pubSubRouter.Run()
|
2021-07-11 23:23:00 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
// Subscribe on new requests event channel from Ethereum
|
|
|
|
err = subscribeOnEthContractsAsync(context.TODO(), ethClient, mp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-17 14:31:27 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
// Run blockchain sync manager
|
|
|
|
syncManager.Run()
|
2020-11-27 18:47:58 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
// Run dispute manager
|
|
|
|
disputeManager.Run(context.TODO())
|
2020-11-27 18:47:58 +00:00
|
|
|
|
|
|
|
return nil
|
2021-07-21 21:56:58 +00:00
|
|
|
},
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
func runLibp2pAsync(ctx context.Context, h host.Host, cfg *config.Config, disco discovery.Discovery) error {
|
2020-11-27 18:47:58 +00:00
|
|
|
logrus.Info("Announcing ourselves...")
|
2021-07-21 21:56:58 +00:00
|
|
|
_, err := disco.Advertise(context.TODO(), cfg.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...")
|
2021-07-21 21:56:58 +00:00
|
|
|
peerChan, err := disco.FindPeers(context.TODO(), cfg.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
|
|
|
|
}
|
2021-07-21 21:56:58 +00:00
|
|
|
if newPeer.ID.String() == h.ID().String() {
|
2020-11-27 18:47:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.WithField("peer", newPeer.ID).Info("Discovered new peer, connecting...")
|
2020-11-27 18:47:58 +00:00
|
|
|
// Connect to the peer
|
2021-07-21 21:56:58 +00:00
|
|
|
if err := h.Connect(ctx, newPeer); err != nil {
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"peer": newPeer.ID,
|
|
|
|
"err": err.Error(),
|
|
|
|
}).Warn("Connection with newly discovered peer has been failed")
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
2021-07-15 20:51:22 +00:00
|
|
|
logrus.WithField("peer", newPeer.ID).Info("Connected to newly discovered peer")
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 19:10:44 +00:00
|
|
|
func subscribeOnEthContractsAsync(ctx context.Context, ethClient ethclient.EthereumSideAPI, mp *pool.Mempool) error {
|
2021-07-21 21:56:58 +00:00
|
|
|
eventChan, subscription, err := ethClient.SubscribeOnOracleEvents(ctx)
|
2020-11-27 18:47:58 +00:00
|
|
|
if err != nil {
|
2021-07-21 21:56:58 +00:00
|
|
|
return err
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
EventLoop:
|
|
|
|
for {
|
|
|
|
select {
|
2021-06-10 20:32:39 +00:00
|
|
|
case event := <-eventChan:
|
2020-11-27 18:47:58 +00:00
|
|
|
{
|
2021-06-10 20:32:39 +00:00
|
|
|
rpcMethod := rpc.GetRPCMethod(event.OriginChain, event.RequestType)
|
|
|
|
if rpcMethod == nil {
|
|
|
|
logrus.Errorf("Invalid RPC method name/type %d/%s for oracle request %s", event.OriginChain, event.RequestType, event.ReqID.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res, err := rpcMethod(event.RequestParams)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to invoke RPC method for oracle request %s: %s", event.ReqID.String(), err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
task := &types.DioneTask{
|
|
|
|
OriginChain: event.OriginChain,
|
|
|
|
RequestType: event.RequestType,
|
|
|
|
RequestParams: event.RequestParams,
|
|
|
|
Payload: res,
|
|
|
|
RequestID: event.ReqID.String(),
|
|
|
|
}
|
|
|
|
data, err := cbor.Marshal(task)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to marshal RPC response for oracle request %s: %s", event.ReqID.String(), err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tx := types2.CreateTransaction(data)
|
2021-07-21 21:56:58 +00:00
|
|
|
err = mp.StoreTx(tx)
|
2021-06-02 19:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to store tx in mempool: %s", err.Error())
|
|
|
|
continue
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break EventLoop
|
2021-07-21 21:56:58 +00:00
|
|
|
case err := <-subscription.Err():
|
|
|
|
logrus.Fatalf("Error has occurred in subscription to Ethereum event channel: %s", err.Error())
|
2020-11-27 18:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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 Start() {
|
2021-07-21 21:56:58 +00:00
|
|
|
fx.New(
|
|
|
|
fx.Provide(
|
|
|
|
provideAppFlags,
|
|
|
|
provideConfig,
|
2021-08-24 19:41:40 +00:00
|
|
|
provideCacheManager,
|
2021-07-21 21:56:58 +00:00
|
|
|
providePrivateKey,
|
|
|
|
provideLibp2pHost,
|
2021-08-25 19:10:44 +00:00
|
|
|
ethclient.NewEthereumClient,
|
2021-08-24 19:41:40 +00:00
|
|
|
providePubsub,
|
2021-07-21 21:56:58 +00:00
|
|
|
providePubsubRouter,
|
|
|
|
provideBootstrapAddrs,
|
|
|
|
providePeerDiscovery,
|
2021-08-24 19:41:40 +00:00
|
|
|
drand2.NewDrandBeacon,
|
|
|
|
pool.NewMempool,
|
2021-08-24 21:02:37 +00:00
|
|
|
provideBlockchainDatabase,
|
2021-07-26 21:18:16 +00:00
|
|
|
blockchain.NewMiner,
|
2021-08-24 21:02:37 +00:00
|
|
|
blockchain.NewBlockChain,
|
2021-08-24 19:41:40 +00:00
|
|
|
sync.NewSyncManager,
|
2021-07-21 21:56:58 +00:00
|
|
|
provideNetworkRPCHost,
|
2021-08-24 19:41:40 +00:00
|
|
|
NewNetworkService,
|
2021-07-21 21:56:58 +00:00
|
|
|
provideDirectRPCClient,
|
2021-08-24 19:41:40 +00:00
|
|
|
func() EventBus.Bus { return EventBus.New() },
|
2021-07-21 21:56:58 +00:00
|
|
|
),
|
2021-08-24 19:41:40 +00:00
|
|
|
consensus.Module,
|
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
fx.Invoke(
|
|
|
|
configureLogger,
|
|
|
|
configureDirectRPC,
|
|
|
|
configureForeignBlockchainRPC,
|
2021-07-26 21:18:16 +00:00
|
|
|
initializeBlockchain,
|
|
|
|
configureMiner,
|
2021-07-21 21:56:58 +00:00
|
|
|
runNode,
|
|
|
|
),
|
2021-07-28 21:48:01 +00:00
|
|
|
fx.NopLogger,
|
2021-07-21 21:56:58 +00:00
|
|
|
).Run()
|
2020-10-20 18:18:36 +00:00
|
|
|
}
|