Merge branch 'master' of github.com:harmony-one/harmony into rj_branch

pull/530/head
Rongjian Lan 6 years ago
commit f50e9343e3
  1. 3
      api/consensus/consensus.go
  2. 165
      api/consensus/consensus.pb.go
  3. 21
      api/consensus/consensus.proto
  4. 65
      api/proto/bcconn/bcconn.go
  5. 54
      api/proto/bcconn/bcconn_test.go
  6. 5
      core/resharding.go
  7. 9
      core/types/block.go
  8. 4
      core/types/shard_state.go
  9. 5
      node/node_newblock.go

@ -1,3 +0,0 @@
package consensus
//go:generate protoc consensus.proto --go_out=plugins=grpc:.

@ -1,165 +0,0 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: consensus.proto
package consensus
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type MessageType int32
const (
MessageType_UNKNOWN MessageType = 0
MessageType_ANNOUNCE MessageType = 1
MessageType_PREPARE MessageType = 2
MessageType_PREPARED MessageType = 3
MessageType_COMMIT MessageType = 4
MessageType_COMMITTED MessageType = 5
)
var MessageType_name = map[int32]string{
0: "UNKNOWN",
1: "ANNOUNCE",
2: "PREPARE",
3: "PREPARED",
4: "COMMIT",
5: "COMMITTED",
}
var MessageType_value = map[string]int32{
"UNKNOWN": 0,
"ANNOUNCE": 1,
"PREPARE": 2,
"PREPARED": 3,
"COMMIT": 4,
"COMMITTED": 5,
}
func (x MessageType) String() string {
return proto.EnumName(MessageType_name, int32(x))
}
func (MessageType) EnumDescriptor() ([]byte, []int) {
return fileDescriptor_56f0f2c53b3de771, []int{0}
}
type Message struct {
Type MessageType `protobuf:"varint,1,opt,name=type,proto3,enum=consensus.MessageType" json:"type,omitempty"`
ConsensusId uint32 `protobuf:"varint,2,opt,name=consensus_id,json=consensusId,proto3" json:"consensus_id,omitempty"`
SenderId uint32 `protobuf:"varint,3,opt,name=sender_id,json=senderId,proto3" json:"sender_id,omitempty"`
BlockHash []byte `protobuf:"bytes,4,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"`
Payload []byte `protobuf:"bytes,5,opt,name=payload,proto3" json:"payload,omitempty"`
Signature []byte `protobuf:"bytes,6,opt,name=signature,proto3" json:"signature,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *Message) Reset() { *m = Message{} }
func (m *Message) String() string { return proto.CompactTextString(m) }
func (*Message) ProtoMessage() {}
func (*Message) Descriptor() ([]byte, []int) {
return fileDescriptor_56f0f2c53b3de771, []int{0}
}
func (m *Message) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_Message.Unmarshal(m, b)
}
func (m *Message) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_Message.Marshal(b, m, deterministic)
}
func (m *Message) XXX_Merge(src proto.Message) {
xxx_messageInfo_Message.Merge(m, src)
}
func (m *Message) XXX_Size() int {
return xxx_messageInfo_Message.Size(m)
}
func (m *Message) XXX_DiscardUnknown() {
xxx_messageInfo_Message.DiscardUnknown(m)
}
var xxx_messageInfo_Message proto.InternalMessageInfo
func (m *Message) GetType() MessageType {
if m != nil {
return m.Type
}
return MessageType_UNKNOWN
}
func (m *Message) GetConsensusId() uint32 {
if m != nil {
return m.ConsensusId
}
return 0
}
func (m *Message) GetSenderId() uint32 {
if m != nil {
return m.SenderId
}
return 0
}
func (m *Message) GetBlockHash() []byte {
if m != nil {
return m.BlockHash
}
return nil
}
func (m *Message) GetPayload() []byte {
if m != nil {
return m.Payload
}
return nil
}
func (m *Message) GetSignature() []byte {
if m != nil {
return m.Signature
}
return nil
}
func init() {
proto.RegisterEnum("consensus.MessageType", MessageType_name, MessageType_value)
proto.RegisterType((*Message)(nil), "consensus.Message")
}
func init() { proto.RegisterFile("consensus.proto", fileDescriptor_56f0f2c53b3de771) }
var fileDescriptor_56f0f2c53b3de771 = []byte{
// 263 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0xc1, 0x6a, 0xb3, 0x40,
0x14, 0x85, 0xff, 0x49, 0x8c, 0xc6, 0xab, 0xf9, 0x3b, 0xdc, 0x45, 0x19, 0x68, 0x0b, 0xb6, 0x2b,
0xc9, 0x22, 0x8b, 0xf6, 0x09, 0x42, 0x22, 0x54, 0x8a, 0x63, 0x10, 0x43, 0x77, 0x0d, 0x93, 0x38,
0xc4, 0xd0, 0xa0, 0xe2, 0x98, 0x85, 0xef, 0xd8, 0x87, 0x2a, 0x8e, 0xa9, 0xed, 0x6e, 0xce, 0xf7,
0x9d, 0x03, 0xc3, 0x85, 0x9b, 0x43, 0x59, 0x28, 0x59, 0xa8, 0x8b, 0x5a, 0x54, 0x75, 0xd9, 0x94,
0x68, 0x0f, 0xe0, 0xe9, 0x8b, 0x80, 0x15, 0x49, 0xa5, 0xc4, 0x51, 0xe2, 0x1c, 0x8c, 0xa6, 0xad,
0x24, 0x23, 0x1e, 0xf1, 0xff, 0x3f, 0xdf, 0x2e, 0x7e, 0x67, 0xd7, 0x46, 0xda, 0x56, 0x32, 0xd1,
0x1d, 0x7c, 0x04, 0x77, 0xd0, 0xbb, 0x53, 0xc6, 0x46, 0x1e, 0xf1, 0x67, 0x89, 0x33, 0xb0, 0x30,
0xc3, 0x3b, 0xb0, 0x95, 0x2c, 0x32, 0x59, 0x77, 0x7e, 0xac, 0xfd, 0xb4, 0x07, 0x61, 0x86, 0x0f,
0x00, 0xfb, 0x73, 0x79, 0xf8, 0xdc, 0xe5, 0x42, 0xe5, 0xcc, 0xf0, 0x88, 0xef, 0x26, 0xb6, 0x26,
0xaf, 0x42, 0xe5, 0xc8, 0xc0, 0xaa, 0x44, 0x7b, 0x2e, 0x45, 0xc6, 0x26, 0xda, 0xfd, 0x44, 0xbc,
0x07, 0x5b, 0x9d, 0x8e, 0x85, 0x68, 0x2e, 0xb5, 0x64, 0x66, 0xbf, 0x1b, 0xc0, 0xfc, 0x03, 0x9c,
0x3f, 0x7f, 0x45, 0x07, 0xac, 0x2d, 0x7f, 0xe3, 0xf1, 0x3b, 0xa7, 0xff, 0xd0, 0x85, 0xe9, 0x92,
0xf3, 0x78, 0xcb, 0x57, 0x01, 0x25, 0x9d, 0xda, 0x24, 0xc1, 0x66, 0x99, 0x04, 0x74, 0xd4, 0xa9,
0x6b, 0x58, 0xd3, 0x31, 0x02, 0x98, 0xab, 0x38, 0x8a, 0xc2, 0x94, 0x1a, 0x38, 0x03, 0xbb, 0x7f,
0xa7, 0xc1, 0x9a, 0x4e, 0xf6, 0xa6, 0x3e, 0xe0, 0xcb, 0x77, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd5,
0x30, 0x8a, 0xaf, 0x53, 0x01, 0x00, 0x00,
}

@ -1,21 +0,0 @@
syntax = "proto3";
package consensus;
enum MessageType {
UNKNOWN = 0;
ANNOUNCE = 1;
PREPARE = 2;
PREPARED = 3;
COMMIT = 4;
COMMITTED = 5;
}
message Message {
MessageType type = 1;
uint32 consensus_id = 2;
uint32 sender_id = 3; // TODO: make it public key
bytes block_hash = 4;
bytes payload = 5;
bytes signature = 6;
}

@ -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")
}
}

@ -14,7 +14,8 @@ const (
// InitialSeed is the initial random seed, a magic number to answer everything, remove later
InitialSeed uint32 = 42
// FirstEpoch is the number of the first epoch.
FirstEpoch = 0
// TODO(minhdoan): we should design the first epoch as 0. Please figure out how to change other logic to make it 0
FirstEpoch = 1
)
// ShardingState is data structure hold the sharding state
@ -73,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)

@ -93,11 +93,10 @@ type Header struct {
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
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"`

@ -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))
}

@ -11,6 +11,7 @@ import (
const (
DefaultThreshold = 1
FirstTimeThreshold = 2
ConsensusTimeOut = 10
)
// WaitForConsensusReady listen for the readiness signal from consensus and generate new block for consensus.
@ -30,10 +31,10 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}, stopChan chan
select {
case <-readySignal:
time.Sleep(100 * time.Millisecond) // Delay a bit so validator is catched up (test-only).
case <-time.After(300 * time.Second):
case <-time.After(ConsensusTimeOut * time.Second):
node.Consensus.ResetState()
timeoutCount++
utils.GetLogInstance().Debug("Consensus timeout, retry!", "count", timeoutCount, "node", node)
utils.GetLogInstance().Debug("Consensus timeout, retry!", "count", timeoutCount)
// FIXME: retry is not working, there is no retry logic here. It will only wait for new transaction.
case <-stopChan:
return

Loading…
Cancel
Save