saving work for tommorow TO debug serialization

pull/76/head
alok 6 years ago
parent 5b1d0c1d81
commit 04b4fbb00c
  1. 3
      beaconchain/beaconchain.go
  2. 34
      node/node.go

@ -51,7 +51,8 @@ func generateIDCKeys() kyber.Point {
//AcceptConnections welcomes new connections
func (IDC *BeaconChain) AcceptConnections(b []byte) {
Node := node.DeserializeNode(b)
NewNode := node.DeserializeNode(b)
Node := &node.Node{SelfPeer: NewNode.SelfPeer, IDCPeer: NewNode.IDCPeer}
IDC.registerNode(Node) //This copies lock value of sync.mutex, we need to have a way around it by creating auxiliary data struct.
}

@ -19,6 +19,11 @@ import (
"github.com/simple-rules/harmony-benchmark/syncing"
)
type NetworkNode struct {
SelfPeer p2p.Peer
IDCPeer p2p.Peer
}
// Node represents a program (machine) participating in the network
// TODO(minhdoan, rj): consider using BlockChannel *chan blockchain.Block for efficiency.
type Node struct {
@ -127,40 +132,41 @@ func (node *Node) countNumTransactionsInBlockchain() int {
//ConnectIdentityChain connects to identity chain
func (node *Node) ConnectBeaconChain() {
msg := node.SerializeNode()
Nnode := &NetworkNode{SelfPeer: node.SelfPeer, IDCPeer: node.IDCPeer}
msg := node.SerializeNode(Nnode)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg)
p2p.SendMessage(node.IDCPeer, msgToSend)
}
// SerializeNode serializes the node
// https://stackoverflow.com/questions/12854125/how-do-i-dump-the-struct-into-the-byte-array-without-reflection/12854659#12854659
func (node *Node) SerializeNode() []byte {
func (node *Node) SerializeNode(nnode *NetworkNode) []byte {
//Needs to escape the serialization of unexported fields
result := new(bytes.Buffer)
encoder := gob.NewEncoder(result)
err := encoder.Encode(node.SelfPeer)
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
err := encoder.Encode(nnode)
if err != nil {
fmt.Println("Could not serialize node")
fmt.Println("ERROR", err)
//node.log.Error("Could not serialize node")
}
err = encoder.Encode(node.IDCPeer)
//err = encoder.Encode(node.IDCPeer)
return result.Bytes()
}
// DeserializeNode deserializes the node
func DeserializeNode(d []byte) *Node {
var wn Node
func DeserializeNode(d []byte) *NetworkNode {
var wn NetworkNode
r := bytes.NewBuffer(d)
decoder := gob.NewDecoder(r)
err := decoder.Decode(&wn.SelfPeer)
if err != nil {
log.Error("Could not de-serialize node")
}
err = decoder.Decode(&wn.IDCPeer)
err := decoder.Decode(&wn)
if err != nil {
log.Error("Could not de-serialize node")
log.Error("Could not de-serialize node 1")
}
// err = decoder.Decode(&wn.IDCPeer)
// if err != nil {
// log.Error("Could not de-serialize node 2")
// }
return &wn
}

Loading…
Cancel
Save