From 31e4e43d414d65cc69fc30de6981a05c9b72f3c1 Mon Sep 17 00:00:00 2001 From: ak Date: Fri, 30 Nov 2018 18:25:19 -0800 Subject: [PATCH] beacon chain communication and running --- bcconn/bcconn.go | 67 +++++++++++++++++++++++++++++++++++++++++ bcconn/bcconn_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ runbeacon/run-beacon.go | 16 ++++++++++ 3 files changed, 149 insertions(+) create mode 100644 bcconn/bcconn.go create mode 100644 bcconn/bcconn_test.go create mode 100644 runbeacon/run-beacon.go diff --git a/bcconn/bcconn.go b/bcconn/bcconn.go new file mode 100644 index 000000000..0009fa5e3 --- /dev/null +++ b/bcconn/bcconn.go @@ -0,0 +1,67 @@ +package bcconn + +import ( + "bytes" + "encoding/gob" + + "github.com/harmony-one/harmony/log" + "github.com/harmony-one/harmony/p2p" +) + +type NodeInfo struct { //TODO: to be merged with Leo's key. + Self p2p.Peer + PubK []byte +} +type ResponseRandomNumber struct { + NumberOfShards int + NumberOfNodesAdded int + Leaders []*NodeInfo +} + +//SerializeNodeInfo +func SerializeNodeInfo(nodeinfo *NodeInfo) []byte { + var result bytes.Buffer + encoder := gob.NewEncoder(&result) + err := encoder.Encode(nodeinfo) + if err != nil { + log.Error("Could not serialize node info", err) + } + return result.Bytes() +} + +// DeserializeNodeInfo deserializes the node +func DeserializeNodeInfo(d []byte) *NodeInfo { + var wn NodeInfo + r := bytes.NewBuffer(d) + decoder := gob.NewDecoder(r) + err := decoder.Decode(&wn) + if err != nil { + log.Error("Could not de-serialize node info", err) + } + return &wn +} + +//SerializeRandomInfo +func SerializeRandomInfo(response ResponseRandomNumber) []byte { + //Needs to escape the serialization of unexported fields + var result bytes.Buffer + encoder := gob.NewEncoder(&result) + err := encoder.Encode(response) + if err != nil { + log.Crit("Could not serialize randomn number information", "error", err) + } + + return result.Bytes() +} + +// DeserializeRandomInfo deserializes the node +func DeserializeRandomInfo(d []byte) ResponseRandomNumber { + var wn ResponseRandomNumber + r := bytes.NewBuffer(d) + decoder := gob.NewDecoder(r) + err := decoder.Decode(&wn) + if err != nil { + log.Crit("Could not de-serialize random number information") + } + return wn +} diff --git a/bcconn/bcconn_test.go b/bcconn/bcconn_test.go new file mode 100644 index 000000000..ab8aaf70c --- /dev/null +++ b/bcconn/bcconn_test.go @@ -0,0 +1,66 @@ +package bcconn + +import ( + "fmt" + "reflect" + "testing" + + "github.com/harmony-one/harmony/p2p" + "github.com/harmony-one/harmony/utils" +) + +func TestSerializeDeserializeNodeInfo(t *testing.T) { + var ip, port string + ip = "127.0.0.1" + port = "8080" + self := p2p.Peer{IP: ip, Port: port} + _, pk := utils.GenKey(ip, port) + pkb, err := pk.MarshalBinary() + if err != nil { + fmt.Println("problem marshalling binary from public key") + } + nodeInfo := &NodeInfo{Self: self, PubK: pkb} + serializedNI := SerializeNodeInfo(nodeInfo) + deserializedNI := DeserializeNodeInfo(serializedNI) + if !reflect.DeepEqual(nodeInfo, deserializedNI) { + t.Fatalf("serialized and deserializing nodeinfo does not lead to origina nodeinfo") + } + +} + +func TestSerializeDeserializeRandomInfo(t *testing.T) { + var ip, port string + + ip = "127.0.0.1" + port = "8080" + self := p2p.Peer{IP: ip, Port: port} + _, pk := utils.GenKey(ip, port) + pkb, err := pk.MarshalBinary() + if err != nil { + fmt.Println("problem marshalling binary from public key") + } + nodeInfo1 := &NodeInfo{Self: self, PubK: pkb} + + ip = "127.0.0.1" + port = "9080" + self2 := p2p.Peer{IP: ip, Port: port} + _, pk2 := utils.GenKey(ip, port) + pkb2, err := pk2.MarshalBinary() + if err != nil { + fmt.Println("problem marshalling binary from public key") + } + nodeInfo2 := &NodeInfo{Self: self2, PubK: pkb2} + + leaders := make([]*NodeInfo, 2) + leaders[0] = nodeInfo1 + leaders[1] = nodeInfo2 + + rrn := ResponseRandomNumber{NumberOfShards: 5, NumberOfNodesAdded: 10, Leaders: leaders} + serializedrrn := SerializeRandomInfo(rrn) + deserializedrrn := DeserializeRandomInfo(serializedrrn) + fmt.Println(rrn) + fmt.Println(deserializedrrn) + if !reflect.DeepEqual(rrn, deserializedrrn) { + t.Fatalf("serializin g and deserializing random response does not lead to original randominfo") + } +} diff --git a/runbeacon/run-beacon.go b/runbeacon/run-beacon.go new file mode 100644 index 000000000..39a366cc9 --- /dev/null +++ b/runbeacon/run-beacon.go @@ -0,0 +1,16 @@ +package main + +import ( + "flag" + + "github.com/harmony-one/harmony/beaconchain" +) + +func main() { + numShards := flag.Int("numShards", 1, "number of shards of identity chain") + ip := flag.String("ip", "127.0.0.1", "ip on which beaconchain listens") + port := flag.String("port", "8081", "port on which beaconchain listens") + flag.Parse() + bc := beaconchain.New(*numShards, *ip, *port) + bc.StartServer() +}