2020-10-21 19:54:40 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2020-10-21 22:05:12 +00:00
|
|
|
"time"
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/config"
|
2020-10-21 20:36:05 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-21 19:54:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConsensus(t *testing.T) {
|
2020-10-21 20:36:05 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2020-10-21 22:05:12 +00:00
|
|
|
//log.SetAllLoggers(log.LevelDebug)
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
cfg := &config.Config{
|
|
|
|
ListenPort: "1234",
|
2020-10-21 22:05:12 +00:00
|
|
|
ListenAddr: "0.0.0.0",
|
2020-10-21 19:54:40 +00:00
|
|
|
Rendezvous: "dione",
|
|
|
|
PubSub: config.PubSubConfig{
|
|
|
|
ProtocolID: "/test/1.0",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-04 17:46:35 +00:00
|
|
|
//cfg.BootstrapNodes = "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN"
|
2020-10-21 22:05:12 +00:00
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
// setup first node
|
2020-10-22 14:37:52 +00:00
|
|
|
node1 := newNode(cfg)
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
// setup second node
|
|
|
|
cfg.ListenPort = "1235"
|
2020-11-04 17:46:35 +00:00
|
|
|
cfg.BootstrapNodes = []string{node1.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", node1.Host.ID().String())}
|
2020-10-22 14:37:52 +00:00
|
|
|
node2 := newNode(cfg)
|
2020-10-21 19:54:40 +00:00
|
|
|
|
|
|
|
// setup third node
|
|
|
|
cfg.ListenPort = "1236"
|
2020-10-22 14:37:52 +00:00
|
|
|
node3 := newNode(cfg)
|
|
|
|
|
|
|
|
cfg.ListenPort = "1237"
|
|
|
|
node4 := newNode(cfg)
|
|
|
|
cfg.ListenPort = "1238"
|
|
|
|
node5 := newNode(cfg)
|
|
|
|
cfg.ListenPort = "1239"
|
|
|
|
node6 := newNode(cfg)
|
2020-10-21 19:54:40 +00:00
|
|
|
|
2020-10-21 22:05:12 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
go node2.ConsensusManager.NewTestConsensus("test", "123")
|
|
|
|
go node1.ConsensusManager.NewTestConsensus("test1", "123")
|
|
|
|
go node3.ConsensusManager.NewTestConsensus("test", "123")
|
2020-10-22 14:37:52 +00:00
|
|
|
go node4.ConsensusManager.NewTestConsensus("test1", "123")
|
|
|
|
go node5.ConsensusManager.NewTestConsensus("test", "123")
|
|
|
|
go node6.ConsensusManager.NewTestConsensus("test2", "123")
|
2020-10-28 17:35:56 +00:00
|
|
|
select {}
|
2020-10-22 14:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newNode(cfg *config.Config) *Node {
|
|
|
|
privKey, err := generatePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
node := &Node{
|
|
|
|
OracleTopic: "dione",
|
|
|
|
Config: cfg,
|
|
|
|
GlobalCtx: ctx,
|
|
|
|
GlobalCtxCancel: ctxCancel,
|
|
|
|
}
|
2020-11-04 17:46:35 +00:00
|
|
|
node.setupNode(ctx, privKey, 1*time.Second, 3)
|
2020-10-22 14:37:52 +00:00
|
|
|
return node
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|