fix comments and clean up

pull/531/head
Minh Doan 6 years ago committed by Minh Doan
parent 5105d65e55
commit 0a12dfb626
  1. 65
      api/proto/bcconn/bcconn.go
  2. 54
      api/proto/bcconn/bcconn_test.go
  3. 2
      core/resharding.go
  4. 17
      core/types/block.go
  5. 4
      core/types/shard_state.go

@ -1,65 +0,0 @@
package bcconn
import (
"bytes"
"encoding/gob"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/api/proto/node"
)
//ResponseRandomNumber struct for exchanging random information
type ResponseRandomNumber struct {
NumberOfShards int
NumberOfNodesAdded int
Leaders []*node.Info
}
// SerializeNodeInfo is for serializing nodeinfo
func SerializeNodeInfo(nodeinfo *node.Info) []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 nodeinfo
func DeserializeNodeInfo(d []byte) *node.Info {
var wn node.Info
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 serializes random number informations
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 random informations
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
}

@ -1,54 +0,0 @@
package bcconn
import (
"fmt"
"reflect"
"testing"
"github.com/harmony-one/harmony/api/proto/node"
"github.com/harmony-one/harmony/internal/utils"
)
func TestSerializeDeserializeNodeInfo(t *testing.T) {
var ip, port string
ip = "127.0.0.1"
port = "8080"
_, pk := utils.GenKey(ip, port)
pkb := pk.Serialize()
nodeInfo := &node.Info{IP: ip, Port: port, PubKey: 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"
_, pk := utils.GenKey(ip, port)
pkb := pk.Serialize()
nodeInfo1 := &node.Info{IP: ip, Port: port, PubKey: pkb}
ip = "127.0.0.1"
port = "9080"
_, pk2 := utils.GenKey(ip, port)
pkb2 := pk2.Serialize()
nodeInfo2 := &node.Info{IP: ip, Port: port, PubKey: pkb2}
leaders := make([]*node.Info, 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")
}
}

@ -74,7 +74,7 @@ func (ss *ShardingState) cuckooResharding(percent float64) {
}
}
// UpdateShardState will first add new nodes into shards, then use cuckoo rule to reshard to get new shard state
// assignLeaders will first add new nodes into shards, then use cuckoo rule to reshard to get new shard state
func (ss *ShardingState) assignLeaders() {
for i := 0; i < ss.numShards; i++ {
Shuffle(ss.shardState[i].NodeList)

@ -92,15 +92,14 @@ type Header struct {
MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" gencodec:"required"`
// Additional Fields
ShardID ShardID `json:"shardID" gencodec:"required"`
PrepareSignature [48]byte `json:"signature" gencodec:"required"`
PrepareBitmap []byte `json:"bitmap" gencodec:"required"` // Contains which validator signed
CommitSignature [48]byte `json:"signature" gencodec:"required"`
CommitBitmap []byte `json:"bitmap" gencodec:"required"` // Contains which validator signed
RandPreimage [32]byte `json:"randPreimage"`
RandSeed [32]byte `json:"randSeed"`
ShardStateHash common.Hash `json:"shardStateRoot"`
ShardID ShardID `json:"shardID" gencodec:"required"`
PrepareSignature [48]byte `json:"prepareSignature" gencodec:"required"`
PrepareBitmap []byte `json:"prepareBitmap" gencodec:"required"` // Contains which validator signed
CommitSignature [48]byte `json:"commitSignature" gencodec:"required"`
CommitBitmap []byte `json:"commitBitmap" gencodec:"required"` // Contains which validator signed
RandPreimage [32]byte `json:"randPreimage"`
RandSeed [32]byte `json:"randSeed"`
ShardStateHash common.Hash `json:"shardStateRoot"`
}
// field type overrides for gencodec

@ -59,6 +59,6 @@ func CompareNodeID(n1 NodeID, n2 NodeID) int {
}
// Serialize serialize NodeID into bytes
func (n *NodeID) Serialize() []byte {
return []byte(string(*n))
func (n NodeID) Serialize() []byte {
return []byte(string(n))
}

Loading…
Cancel
Save