From 04b4fbb00c2a3dfb3ddbc0112c4197861e03fab2 Mon Sep 17 00:00:00 2001 From: alok Date: Wed, 14 Nov 2018 23:20:09 -0800 Subject: [PATCH] saving work for tommorow TO debug serialization --- beaconchain/beaconchain.go | 3 ++- node/node.go | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/beaconchain/beaconchain.go b/beaconchain/beaconchain.go index 48b12c121..98a2e2d9f 100644 --- a/beaconchain/beaconchain.go +++ b/beaconchain/beaconchain.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. } diff --git a/node/node.go b/node/node.go index f2e060001..2a4a709cc 100644 --- a/node/node.go +++ b/node/node.go @@ -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 }