Cleanup and optimize code in node_test

This commit is contained in:
ChronosX88 2020-10-22 18:37:52 +04:00
parent 97a4e7a7ad
commit 3331300b49
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -27,56 +27,49 @@ func TestConsensus(t *testing.T) {
//cfg.BootstrapNodeMultiaddr = "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" //cfg.BootstrapNodeMultiaddr = "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN"
// setup first node // setup first node
privKey, err := generatePrivateKey() node1 := newNode(cfg)
if err != nil {
t.Error(err)
}
ctx, ctxCancel := context.WithCancel(context.Background())
node1 := &Node{
OracleTopic: "dione",
Config: cfg,
GlobalCtx: ctx,
GlobalCtxCancel: ctxCancel,
}
node1.setupNode(ctx, privKey)
// setup second node // setup second node
privKey, err = generatePrivateKey()
if err != nil {
t.Error(err)
}
ctx, ctxCancel = context.WithCancel(context.Background())
cfg.ListenPort = "1235" cfg.ListenPort = "1235"
cfg.Bootstrap = false cfg.Bootstrap = false
cfg.BootstrapNodeMultiaddr = node1.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", node1.Host.ID().String()) cfg.BootstrapNodeMultiaddr = node1.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", node1.Host.ID().String())
node2 := newNode(cfg)
node2 := &Node{
OracleTopic: "dione",
Config: cfg,
GlobalCtx: ctx,
GlobalCtxCancel: ctxCancel,
}
node2.setupNode(ctx, privKey)
// setup third node // setup third node
privKey, err = generatePrivateKey()
if err != nil {
t.Error(err)
}
ctx, ctxCancel = context.WithCancel(context.Background())
cfg.ListenPort = "1236" cfg.ListenPort = "1236"
node3 := &Node{ node3 := newNode(cfg)
OracleTopic: "dione",
Config: cfg, cfg.ListenPort = "1237"
GlobalCtx: ctx, node4 := newNode(cfg)
GlobalCtxCancel: ctxCancel, cfg.ListenPort = "1238"
} node5 := newNode(cfg)
node3.setupNode(ctx, privKey) cfg.ListenPort = "1239"
node6 := newNode(cfg)
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
go node2.ConsensusManager.NewTestConsensus("test", "123") go node2.ConsensusManager.NewTestConsensus("test", "123")
go node1.ConsensusManager.NewTestConsensus("test1", "123") go node1.ConsensusManager.NewTestConsensus("test1", "123")
go node3.ConsensusManager.NewTestConsensus("test", "123") go node3.ConsensusManager.NewTestConsensus("test", "123")
go node4.ConsensusManager.NewTestConsensus("test1", "123")
go node5.ConsensusManager.NewTestConsensus("test", "123")
go node6.ConsensusManager.NewTestConsensus("test2", "123")
select{} select{}
}
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,
}
node.setupNode(ctx, privKey)
return node
} }