Fix some syntax issues
This commit is contained in:
parent
a42776be67
commit
e3931c23a2
@ -4,19 +4,18 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
ftypes "github.com/Secured-Finance/dione/rpc/filecoin/types"
|
||||||
|
|
||||||
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
|
oracleEmitter "github.com/Secured-Finance/dione/contracts/oracleemitter"
|
||||||
|
|
||||||
"github.com/Secured-Finance/dione/node"
|
"github.com/Secured-Finance/dione/node"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-state-types/crypto"
|
"github.com/filecoin-project/go-state-types/crypto"
|
||||||
|
|
||||||
ftypes "github.com/Secured-Finance/dione/rpc/filecoin/types"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/filecoin-project/go-state-types/crypto"
|
|
||||||
|
|
||||||
types2 "github.com/Secured-Finance/dione/consensus/types"
|
types2 "github.com/Secured-Finance/dione/consensus/types"
|
||||||
"github.com/Secured-Finance/dione/sigs"
|
"github.com/Secured-Finance/dione/sigs"
|
||||||
"github.com/Secured-Finance/dione/types"
|
"github.com/Secured-Finance/dione/types"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/mitchellh/hashstructure/v2"
|
"github.com/mitchellh/hashstructure/v2"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -95,18 +94,6 @@ func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
|
|||||||
|
|
||||||
logrus.Errorf("the incoming task and cached request event don't match!")
|
logrus.Errorf("the incoming task and cached request event don't match!")
|
||||||
return false
|
return false
|
||||||
// === verify filecoin message signature ===
|
|
||||||
if consensusMsg.Task.RequestType == "GetTransaction" && consensusMsg.Task.OriginChain == 1 {
|
|
||||||
var msg ftypes.SignedMessage
|
|
||||||
if err := msg.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
|
||||||
if err := msg.Message.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = sigs.Verify(msg.Signature, msg.Message.From.Bytes(), msg.Message.Cid().Bytes()); err != nil {
|
|
||||||
logrus.Errorf("Couldn't verify transaction %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
|
|
||||||
@ -178,6 +165,21 @@ func (ppp *PrePreparePool) IsValidPrePrepare(prePrepare *types2.Message) bool {
|
|||||||
}
|
}
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
// === verify filecoin message signature ===
|
||||||
|
if consensusMsg.Task.RequestType == "GetTransaction" && consensusMsg.Task.OriginChain == 1 {
|
||||||
|
var msg ftypes.SignedMessage
|
||||||
|
if err := msg.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
||||||
|
if err := msg.Message.UnmarshalCBOR(bytes.NewReader(consensusMsg.Task.Payload)); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = sigs.Verify(msg.Signature, msg.Message.From.Bytes(), msg.Message.Cid().Bytes()); err != nil {
|
||||||
|
logrus.Errorf("Couldn't verify transaction %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/////////////////////////////////
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,106 +1,107 @@
|
|||||||
package node
|
package node
|
||||||
|
|
||||||
import (
|
//
|
||||||
"context"
|
//import (
|
||||||
"fmt"
|
// "context"
|
||||||
"math/rand"
|
// "fmt"
|
||||||
"testing"
|
// "math/rand"
|
||||||
"time"
|
// "testing"
|
||||||
|
// "time"
|
||||||
"github.com/Secured-Finance/dione/config"
|
//
|
||||||
"github.com/sirupsen/logrus"
|
// "github.com/Secured-Finance/dione/config"
|
||||||
)
|
// "github.com/sirupsen/logrus"
|
||||||
|
//)
|
||||||
func TestConsensus(t *testing.T) {
|
//
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
//func TestConsensus(t *testing.T) {
|
||||||
//log.SetAllLoggers(log.LevelDebug)
|
// logrus.SetLevel(logrus.DebugLevel)
|
||||||
|
// //log.SetAllLoggers(log.LevelDebug)
|
||||||
// boolgen := newBoolgen()
|
//
|
||||||
rand.Seed(time.Now().UnixNano())
|
// // boolgen := newBoolgen()
|
||||||
port := rand.Intn(100) + 10000
|
// rand.Seed(time.Now().UnixNano())
|
||||||
|
// port := rand.Intn(100) + 10000
|
||||||
cfg := &config.Config{
|
//
|
||||||
ListenPort: port,
|
// cfg := &config.Config{
|
||||||
ListenAddr: "0.0.0.0",
|
// ListenPort: port,
|
||||||
Rendezvous: "dione",
|
// ListenAddr: "0.0.0.0",
|
||||||
PubSub: config.PubSubConfig{
|
// Rendezvous: "dione",
|
||||||
ProtocolID: "/dione/1.0",
|
// PubSub: config.PubSubConfig{
|
||||||
},
|
// ProtocolID: "/dione/1.0",
|
||||||
ConsensusMinApprovals: 3,
|
// },
|
||||||
}
|
// ConsensusMinApprovals: 3,
|
||||||
|
// }
|
||||||
var nodes []*Node
|
//
|
||||||
|
// var nodes []*Node
|
||||||
bNode := newNode(cfg)
|
//
|
||||||
t.Logf("Bootstrap ID: %s", bNode.Host.ID())
|
// bNode := newNode(cfg)
|
||||||
cfg.BootstrapNodes = []string{bNode.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", bNode.Host.ID().String())}
|
// t.Logf("Bootstrap ID: %s", bNode.Host.ID())
|
||||||
nodes = append(nodes, bNode)
|
// cfg.BootstrapNodes = []string{bNode.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", bNode.Host.ID().String())}
|
||||||
|
// nodes = append(nodes, bNode)
|
||||||
maxNodes := 10
|
//
|
||||||
|
// maxNodes := 10
|
||||||
for i := 1; i <= maxNodes; i++ {
|
//
|
||||||
cfg.ListenPort += 1
|
// for i := 1; i <= maxNodes; i++ {
|
||||||
node := newNode(cfg)
|
// cfg.ListenPort += 1
|
||||||
nodes = append(nodes, node)
|
// node := newNode(cfg)
|
||||||
}
|
// nodes = append(nodes, node)
|
||||||
|
// }
|
||||||
time.Sleep(5 * time.Second)
|
//
|
||||||
|
// time.Sleep(5 * time.Second)
|
||||||
// var wg sync.WaitGroup
|
//
|
||||||
|
// // var wg sync.WaitGroup
|
||||||
// wg.Add(len(nodes))
|
//
|
||||||
// for _, n := range nodes {
|
// // wg.Add(len(nodes))
|
||||||
// var testData string
|
// // for _, n := range nodes {
|
||||||
// if boolgen.Bool() {
|
// // var testData string
|
||||||
// testData = "test"
|
// // if boolgen.Bool() {
|
||||||
// } else {
|
// // testData = "test"
|
||||||
// testData = "test1"
|
// // } else {
|
||||||
// }
|
// // testData = "test1"
|
||||||
// n.ConsensusManager.NewTestConsensus(testData, "123", func(finalData string) {
|
// // }
|
||||||
// if finalData != "test" {
|
// // n.ConsensusManager.NewTestConsensus(testData, "123", func(finalData string) {
|
||||||
// t.Errorf("Expected final data %s, not %s", "test", finalData)
|
// // if finalData != "test" {
|
||||||
// }
|
// // t.Errorf("Expected final data %s, not %s", "test", finalData)
|
||||||
// wg.Done()
|
// // }
|
||||||
// })
|
// // wg.Done()
|
||||||
// }
|
// // })
|
||||||
// wg.Wait()
|
// // }
|
||||||
}
|
// // wg.Wait()
|
||||||
|
//}
|
||||||
func newNode(cfg *config.Config) *Node {
|
//
|
||||||
privKey, err := generatePrivateKey()
|
//func newNode(cfg *config.Config) *Node {
|
||||||
if err != nil {
|
// privKey, err := generatePrivateKey()
|
||||||
logrus.Fatal(err)
|
// if err != nil {
|
||||||
}
|
// logrus.Fatal(err)
|
||||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
// }
|
||||||
|
// ctx, ctxCancel := context.WithCancel(context.Background())
|
||||||
node := &Node{
|
//
|
||||||
OracleTopic: "dione",
|
// node := &Node{
|
||||||
Config: cfg,
|
// OracleTopic: "dione",
|
||||||
GlobalCtx: ctx,
|
// Config: cfg,
|
||||||
GlobalCtxCancel: ctxCancel,
|
// GlobalCtx: ctx,
|
||||||
}
|
// GlobalCtxCancel: ctxCancel,
|
||||||
node.setupNode(ctx, privKey, 1*time.Second)
|
// }
|
||||||
return node
|
// node.setupNode(ctx, privKey, 1*time.Second)
|
||||||
}
|
// return node
|
||||||
|
//}
|
||||||
type boolgen struct {
|
//
|
||||||
src rand.Source
|
//type boolgen struct {
|
||||||
cache int64
|
// src rand.Source
|
||||||
remaining int
|
// cache int64
|
||||||
}
|
// remaining int
|
||||||
|
//}
|
||||||
func newBoolgen() *boolgen {
|
//
|
||||||
return &boolgen{src: rand.NewSource(time.Now().UnixNano())}
|
//func newBoolgen() *boolgen {
|
||||||
}
|
// return &boolgen{src: rand.NewSource(time.Now().UnixNano())}
|
||||||
|
//}
|
||||||
func (b *boolgen) Bool() bool {
|
//
|
||||||
if b.remaining == 0 {
|
//func (b *boolgen) Bool() bool {
|
||||||
b.cache, b.remaining = b.src.Int63(), 63
|
// if b.remaining == 0 {
|
||||||
}
|
// b.cache, b.remaining = b.src.Int63(), 63
|
||||||
|
// }
|
||||||
result := b.cache&0x01 == 1
|
//
|
||||||
b.cache >>= 1
|
// result := b.cache&0x01 == 1
|
||||||
b.remaining--
|
// b.cache >>= 1
|
||||||
|
// b.remaining--
|
||||||
return result
|
//
|
||||||
}
|
// return result
|
||||||
|
//}
|
||||||
|
@ -100,13 +100,11 @@ func (psr *PubSubRouter) handleMessage(p *pubsub.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (psr *PubSubRouter) Hook(messageType types.MessageType, handler Handler) {
|
func (psr *PubSubRouter) Hook(messageType types.MessageType, handler Handler) {
|
||||||
handlers, ok := psr.handlers[messageType]
|
_, ok := psr.handlers[messageType]
|
||||||
if !ok {
|
if !ok {
|
||||||
emptyArray := []Handler{}
|
psr.handlers[messageType] = []Handler{}
|
||||||
psr.handlers[messageType] = emptyArray
|
|
||||||
handlers = emptyArray
|
|
||||||
}
|
}
|
||||||
psr.handlers[messageType] = append(handlers, handler)
|
psr.handlers[messageType] = append(psr.handlers[messageType], handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (psr *PubSubRouter) BroadcastToServiceTopic(msg *types.Message) error {
|
func (psr *PubSubRouter) BroadcastToServiceTopic(msg *types.Message) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user