2020-10-21 19:54:40 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-11-04 18:17:01 +00:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2020-10-21 19:54:40 +00:00
|
|
|
"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
|
|
|
|
2020-11-04 18:17:01 +00:00
|
|
|
boolgen := newBoolgen()
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
port := rand.Intn(100) + 10000
|
|
|
|
|
2020-10-21 19:54:40 +00:00
|
|
|
cfg := &config.Config{
|
2020-11-04 18:17:01 +00:00
|
|
|
ListenPort: port,
|
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{
|
2020-11-04 18:17:01 +00:00
|
|
|
ProtocolID: "/dione/1.0",
|
2020-10-21 19:54:40 +00:00
|
|
|
},
|
2020-11-15 13:46:58 +00:00
|
|
|
ConsensusMinApprovals: 3,
|
2020-10-21 19:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 18:17:01 +00:00
|
|
|
var nodes []*Node
|
|
|
|
|
|
|
|
bNode := newNode(cfg)
|
|
|
|
t.Logf("Bootstrap ID: %s", bNode.Host.ID())
|
|
|
|
cfg.BootstrapNodes = []string{bNode.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", bNode.Host.ID().String())}
|
|
|
|
nodes = append(nodes, bNode)
|
|
|
|
|
|
|
|
maxNodes := 10
|
|
|
|
|
|
|
|
for i := 1; i <= maxNodes; i++ {
|
|
|
|
cfg.ListenPort += 1
|
|
|
|
node := newNode(cfg)
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:46:58 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2020-11-04 18:17:01 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(len(nodes))
|
|
|
|
for _, n := range nodes {
|
|
|
|
var testData string
|
|
|
|
if boolgen.Bool() {
|
|
|
|
testData = "test"
|
|
|
|
} else {
|
|
|
|
testData = "test1"
|
|
|
|
}
|
|
|
|
n.ConsensusManager.NewTestConsensus(testData, "123", func(finalData string) {
|
|
|
|
if finalData != "test" {
|
|
|
|
t.Errorf("Expected final data %s, not %s", "test", finalData)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
wg.Wait()
|
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 18:17:01 +00:00
|
|
|
node.setupNode(ctx, privKey, 1*time.Second)
|
2020-10-22 14:37:52 +00:00
|
|
|
return node
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
2020-11-04 18:17:01 +00:00
|
|
|
|
|
|
|
type boolgen struct {
|
|
|
|
src rand.Source
|
|
|
|
cache int64
|
|
|
|
remaining int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBoolgen() *boolgen {
|
|
|
|
return &boolgen{src: rand.NewSource(time.Now().UnixNano())}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *boolgen) Bool() bool {
|
|
|
|
if b.remaining == 0 {
|
|
|
|
b.cache, b.remaining = b.src.Int63(), 63
|
|
|
|
}
|
|
|
|
|
|
|
|
result := b.cache&0x01 == 1
|
|
|
|
b.cache >>= 1
|
|
|
|
b.remaining--
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|