commit
15abdeaf85
@ -0,0 +1 @@ |
||||
protoc -I proto/ proto/client.proto --go_out=plugins=grpc:proto |
@ -0,0 +1,145 @@ |
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: drand.proto
|
||||
|
||||
package drand |
||||
|
||||
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_INIT MessageType = 1 |
||||
MessageType_COMMIT MessageType = 2 |
||||
) |
||||
|
||||
var MessageType_name = map[int32]string{ |
||||
0: "UNKNOWN", |
||||
1: "INIT", |
||||
2: "COMMIT", |
||||
} |
||||
|
||||
var MessageType_value = map[string]int32{ |
||||
"UNKNOWN": 0, |
||||
"INIT": 1, |
||||
"COMMIT": 2, |
||||
} |
||||
|
||||
func (x MessageType) String() string { |
||||
return proto.EnumName(MessageType_name, int32(x)) |
||||
} |
||||
|
||||
func (MessageType) EnumDescriptor() ([]byte, []int) { |
||||
return fileDescriptor_1d855c36cf2c0c50, []int{0} |
||||
} |
||||
|
||||
type Message struct { |
||||
Type MessageType `protobuf:"varint,1,opt,name=type,proto3,enum=drand.MessageType" json:"type,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_1d855c36cf2c0c50, []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) 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("drand.MessageType", MessageType_name, MessageType_value) |
||||
proto.RegisterType((*Message)(nil), "drand.Message") |
||||
} |
||||
|
||||
func init() { proto.RegisterFile("drand.proto", fileDescriptor_1d855c36cf2c0c50) } |
||||
|
||||
var fileDescriptor_1d855c36cf2c0c50 = []byte{ |
||||
// 214 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x8f, 0xcf, 0x4a, 0x87, 0x40, |
||||
0x10, 0x80, 0xdb, 0xf2, 0xe7, 0x9f, 0xb1, 0x42, 0xe6, 0xb4, 0x50, 0x81, 0x74, 0x08, 0xe9, 0x20, |
||||
0x51, 0x8f, 0xd0, 0xa5, 0x25, 0x5c, 0x41, 0x8c, 0x8e, 0xb2, 0xb6, 0x8b, 0x4a, 0xe2, 0x2e, 0xbb, |
||||
0x76, 0xf0, 0x81, 0x7a, 0xcf, 0x60, 0xad, 0xf8, 0x1d, 0xbf, 0xef, 0x9b, 0x81, 0x19, 0x48, 0xa5, |
||||
0x15, 0x8b, 0x2c, 0x8d, 0xd5, 0xab, 0xc6, 0x83, 0x87, 0xdb, 0x6f, 0x02, 0x51, 0xa5, 0x9c, 0x13, |
||||
0x83, 0xc2, 0x3b, 0x08, 0xd6, 0xcd, 0x28, 0x4a, 0x72, 0x52, 0x5c, 0x3e, 0x62, 0xb9, 0x8f, 0xff, |
||||
0xd6, 0x76, 0x33, 0xaa, 0xf1, 0x1d, 0xaf, 0x20, 0x71, 0x6a, 0x91, 0xca, 0x76, 0x93, 0xa4, 0x67, |
||||
0x39, 0x29, 0x2e, 0x9a, 0x78, 0x17, 0x4c, 0xe2, 0x0d, 0x40, 0x3f, 0xeb, 0x8f, 0xcf, 0x6e, 0x14, |
||||
0x6e, 0xa4, 0x41, 0x4e, 0x8a, 0xf3, 0x26, 0xf1, 0xe6, 0x45, 0xb8, 0x11, 0x29, 0x44, 0x46, 0x6c, |
||||
0xb3, 0x16, 0x92, 0x1e, 0x7c, 0xfb, 0x43, 0xbc, 0x86, 0xc4, 0x4d, 0xc3, 0x22, 0xd6, 0x2f, 0xab, |
||||
0x68, 0xb8, 0xef, 0xfd, 0x8b, 0xfb, 0x07, 0x48, 0x8f, 0x0e, 0xc1, 0x14, 0xa2, 0x37, 0xfe, 0xca, |
||||
0xeb, 0x77, 0x9e, 0x9d, 0x60, 0x0c, 0x01, 0xe3, 0xac, 0xcd, 0x08, 0x02, 0x84, 0xcf, 0x75, 0x55, |
||||
0xb1, 0x36, 0x3b, 0xed, 0x43, 0xff, 0xe7, 0xd3, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x79, 0xfa, |
||||
0xf8, 0x57, 0xf6, 0x00, 0x00, 0x00, |
||||
} |
@ -0,0 +1,17 @@ |
||||
syntax = "proto3"; |
||||
|
||||
package drand; |
||||
|
||||
enum MessageType { |
||||
UNKNOWN = 0; |
||||
INIT = 1; |
||||
COMMIT = 2; |
||||
} |
||||
|
||||
message Message { |
||||
MessageType type = 1; |
||||
uint32 sender_id = 3; // TODO: make it public key |
||||
bytes block_hash = 4; |
||||
bytes payload = 5; |
||||
bytes signature = 6; |
||||
} |
@ -0,0 +1,194 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
"crypto/sha256" |
||||
"encoding/binary" |
||||
"errors" |
||||
"strconv" |
||||
"sync" |
||||
|
||||
protobuf "github.com/golang/protobuf/proto" |
||||
"github.com/harmony-one/bls/ffi/go/bls" |
||||
drand_proto "github.com/harmony-one/harmony/api/drand" |
||||
bls_cosi "github.com/harmony-one/harmony/crypto/bls" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
) |
||||
|
||||
// DRand is the main struct which contains state for the distributed randomness protocol.
|
||||
type DRand struct { |
||||
vrfs *map[uint32][]byte |
||||
bitmap *bls_cosi.Mask |
||||
pRand *[32]byte |
||||
rand *[32]byte |
||||
|
||||
// map of nodeID to validator Peer object
|
||||
// FIXME: should use PubKey of p2p.Peer as the hashkey
|
||||
validators sync.Map // key is uint16, value is p2p.Peer
|
||||
|
||||
// Leader's address
|
||||
leader p2p.Peer |
||||
|
||||
// Public keys of the committee including leader and validators
|
||||
PublicKeys []*bls.PublicKey |
||||
|
||||
// private/public keys of current node
|
||||
priKey *bls.SecretKey |
||||
pubKey *bls.PublicKey |
||||
|
||||
// Whether I am leader. False means I am validator
|
||||
IsLeader bool |
||||
|
||||
// Leader or validator Id - 4 byte
|
||||
nodeID uint32 |
||||
|
||||
// The p2p host used to send/receive p2p messages
|
||||
host p2p.Host |
||||
|
||||
// Shard Id which this node belongs to
|
||||
ShardID uint32 |
||||
|
||||
// Blockhash - 32 byte
|
||||
blockHash [32]byte |
||||
} |
||||
|
||||
// New creates a new dRand object
|
||||
func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer) *DRand { |
||||
dRand := DRand{} |
||||
dRand.host = host |
||||
|
||||
selfPeer := host.GetSelfPeer() |
||||
if leader.Port == selfPeer.Port && leader.IP == selfPeer.IP { |
||||
dRand.IsLeader = true |
||||
} else { |
||||
dRand.IsLeader = false |
||||
} |
||||
|
||||
dRand.leader = leader |
||||
for _, peer := range peers { |
||||
dRand.validators.Store(utils.GetUniqueIDFromPeer(peer), peer) |
||||
} |
||||
|
||||
dRand.vrfs = &map[uint32][]byte{} |
||||
|
||||
// Initialize cosign bitmap
|
||||
allPublicKeys := make([]*bls.PublicKey, 0) |
||||
for _, validatorPeer := range peers { |
||||
allPublicKeys = append(allPublicKeys, validatorPeer.PubKey) |
||||
} |
||||
allPublicKeys = append(allPublicKeys, leader.PubKey) |
||||
|
||||
dRand.PublicKeys = allPublicKeys |
||||
|
||||
bitmap, _ := bls_cosi.NewMask(dRand.PublicKeys, dRand.leader.PubKey) |
||||
dRand.bitmap = bitmap |
||||
|
||||
dRand.pRand = nil |
||||
dRand.rand = nil |
||||
|
||||
// For now use socket address as ID
|
||||
// TODO: populate Id derived from address
|
||||
dRand.nodeID = utils.GetUniqueIDFromPeer(selfPeer) |
||||
|
||||
// Set private key for myself so that I can sign messages.
|
||||
nodeIDBytes := make([]byte, 32) |
||||
binary.LittleEndian.PutUint32(nodeIDBytes, dRand.nodeID) |
||||
privateKey := bls.SecretKey{} |
||||
err := privateKey.SetLittleEndian(nodeIDBytes) |
||||
dRand.priKey = &privateKey |
||||
dRand.pubKey = privateKey.GetPublicKey() |
||||
|
||||
myShardID, err := strconv.Atoi(ShardID) |
||||
if err != nil { |
||||
panic("Unparseable shard Id" + ShardID) |
||||
} |
||||
dRand.ShardID = uint32(myShardID) |
||||
|
||||
return &dRand |
||||
} |
||||
|
||||
// Sign on the drand message signature field.
|
||||
func (dRand *DRand) signDRandMessage(message *drand_proto.Message) error { |
||||
message.Signature = nil |
||||
// TODO: use custom serialization method rather than protobuf
|
||||
marshaledMessage, err := protobuf.Marshal(message) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
// 64 byte of signature on previous data
|
||||
hash := sha256.Sum256(marshaledMessage) |
||||
signature := dRand.priKey.SignHash(hash[:]) |
||||
|
||||
message.Signature = signature.Serialize() |
||||
return nil |
||||
} |
||||
|
||||
// Signs the drand message and returns the marshaled message.
|
||||
func (dRand *DRand) signAndMarshalDRandMessage(message *drand_proto.Message) ([]byte, error) { |
||||
err := dRand.signDRandMessage(message) |
||||
if err != nil { |
||||
return []byte{}, err |
||||
} |
||||
|
||||
marshaledMessage, err := protobuf.Marshal(message) |
||||
if err != nil { |
||||
return []byte{}, err |
||||
} |
||||
return marshaledMessage, nil |
||||
} |
||||
|
||||
func (dRand *DRand) vrf(blockHash [32]byte) (rand [32]byte, proof []byte) { |
||||
// TODO: implement vrf
|
||||
return [32]byte{}, []byte{} |
||||
} |
||||
|
||||
// GetValidatorPeers returns list of validator peers.
|
||||
func (dRand *DRand) GetValidatorPeers() []p2p.Peer { |
||||
validatorPeers := make([]p2p.Peer, 0) |
||||
|
||||
dRand.validators.Range(func(k, v interface{}) bool { |
||||
if peer, ok := v.(p2p.Peer); ok { |
||||
validatorPeers = append(validatorPeers, peer) |
||||
return true |
||||
} |
||||
return false |
||||
}) |
||||
|
||||
return validatorPeers |
||||
} |
||||
|
||||
// Verify the signature of the message are valid from the signer's public key.
|
||||
func verifyMessageSig(signerPubKey *bls.PublicKey, message drand_proto.Message) error { |
||||
signature := message.Signature |
||||
message.Signature = nil |
||||
messageBytes, err := protobuf.Marshal(&message) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
msgSig := bls.Sign{} |
||||
err = msgSig.Deserialize(signature) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
msgHash := sha256.Sum256(messageBytes) |
||||
if !msgSig.VerifyHash(signerPubKey, msgHash[:]) { |
||||
return errors.New("failed to verify the signature") |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// Gets the validator peer based on validator ID.
|
||||
func (dRand *DRand) getValidatorPeerByID(validatorID uint32) *p2p.Peer { |
||||
v, ok := dRand.validators.Load(validatorID) |
||||
if !ok { |
||||
utils.GetLogInstance().Warn("Unrecognized validator", "validatorID", validatorID, "dRand", dRand) |
||||
return nil |
||||
} |
||||
value, ok := v.(p2p.Peer) |
||||
if !ok { |
||||
utils.GetLogInstance().Warn("Invalid validator", "validatorID", validatorID, "dRand", dRand) |
||||
return nil |
||||
} |
||||
return &value |
||||
} |
@ -0,0 +1,93 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
protobuf "github.com/golang/protobuf/proto" |
||||
drand_proto "github.com/harmony-one/harmony/api/drand" |
||||
"github.com/harmony-one/harmony/core/types" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p/host" |
||||
) |
||||
|
||||
// WaitForEpochBlock waits for the first epoch block to run DRG on
|
||||
func (dRand *DRand) WaitForEpochBlock(blockChannel chan *types.Block, stopChan chan struct{}, stoppedChan chan struct{}) { |
||||
go func() { |
||||
defer close(stoppedChan) |
||||
for { |
||||
select { |
||||
default: |
||||
// keep waiting for new blocks
|
||||
newBlock := <-blockChannel |
||||
// TODO: think about potential race condition
|
||||
|
||||
dRand.init(newBlock) |
||||
case <-stopChan: |
||||
return |
||||
} |
||||
} |
||||
}() |
||||
} |
||||
|
||||
func (dRand *DRand) init(epochBlock *types.Block) { |
||||
// Copy over block hash and block header data
|
||||
blockHash := epochBlock.Hash() |
||||
copy(dRand.blockHash[:], blockHash[:]) |
||||
|
||||
msgToSend := dRand.constructInitMessage() |
||||
|
||||
// Leader commit vrf itself
|
||||
rand, proof := dRand.vrf(dRand.blockHash) |
||||
|
||||
(*dRand.vrfs)[dRand.nodeID] = append(rand[:], proof...) |
||||
|
||||
host.BroadcastMessageFromLeader(dRand.host, dRand.GetValidatorPeers(), msgToSend, nil) |
||||
} |
||||
|
||||
// ProcessMessageLeader dispatches messages for the leader to corresponding processors.
|
||||
func (dRand *DRand) ProcessMessageLeader(payload []byte) { |
||||
message := drand_proto.Message{} |
||||
err := protobuf.Unmarshal(payload, &message) |
||||
|
||||
if err != nil { |
||||
utils.GetLogInstance().Error("Failed to unmarshal message payload.", "err", err, "dRand", dRand) |
||||
} |
||||
|
||||
switch message.Type { |
||||
case drand_proto.MessageType_COMMIT: |
||||
dRand.processCommitMessage(message) |
||||
default: |
||||
utils.GetLogInstance().Error("Unexpected message type", "msgType", message.Type, "dRand", dRand) |
||||
} |
||||
} |
||||
|
||||
// ProcessMessageValidator dispatches validator's consensus message.
|
||||
func (dRand *DRand) processCommitMessage(message drand_proto.Message) { |
||||
if message.Type != drand_proto.MessageType_COMMIT { |
||||
utils.GetLogInstance().Error("Wrong message type received", "expected", drand_proto.MessageType_COMMIT, "got", message.Type) |
||||
return |
||||
} |
||||
|
||||
// Verify message signature
|
||||
err := verifyMessageSig(dRand.leader.PubKey, message) |
||||
if err != nil { |
||||
utils.GetLogInstance().Warn("Failed to verify the message signature", "Error", err) |
||||
return |
||||
} |
||||
|
||||
rand := message.Payload[:32] |
||||
proof := message.Payload[32:] |
||||
_ = rand |
||||
_ = proof |
||||
// TODO: check the validity of the vrf commit
|
||||
|
||||
validatorID := message.SenderId |
||||
validatorPeer := dRand.getValidatorPeerByID(validatorID) |
||||
vrfs := dRand.vrfs |
||||
utils.GetLogInstance().Debug("Received new prepare signature", "numReceivedSoFar", len((*vrfs)), "validatorID", validatorID, "PublicKeys", len(dRand.PublicKeys)) |
||||
|
||||
(*vrfs)[validatorID] = message.Payload |
||||
dRand.bitmap.SetKey(validatorPeer.PubKey, true) // Set the bitmap indicating that this validator signed.
|
||||
|
||||
if len((*vrfs)) >= ((len(dRand.PublicKeys))/3 + 1) { |
||||
// Construct pRand and initiate consensus on it
|
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
drand_proto "github.com/harmony-one/harmony/api/drand" |
||||
"github.com/harmony-one/harmony/api/proto" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
) |
||||
|
||||
// Constructs the init message
|
||||
func (drand *DRand) constructInitMessage() []byte { |
||||
message := drand_proto.Message{} |
||||
message.Type = drand_proto.MessageType_INIT |
||||
|
||||
message.BlockHash = drand.blockHash[:] |
||||
// Don't need the payload in init message
|
||||
marshaledMessage, err := drand.signAndMarshalDRandMessage(&message) |
||||
if err != nil { |
||||
utils.GetLogInstance().Error("Failed to sign and marshal the init message", "error", err) |
||||
} |
||||
return proto.ConstructDRandMessage(marshaledMessage) |
||||
} |
@ -0,0 +1,26 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
"github.com/harmony-one/harmony/p2p/p2pimpl" |
||||
) |
||||
|
||||
func TestConstructInitMessage(test *testing.T) { |
||||
leader := p2p.Peer{IP: "127.0.0.1", Port: "19999"} |
||||
validator := p2p.Peer{IP: "127.0.0.1", Port: "55555"} |
||||
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") |
||||
host, err := p2pimpl.NewHost(&leader, priKey) |
||||
if err != nil { |
||||
test.Fatalf("newhost failure: %v", err) |
||||
} |
||||
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader) |
||||
dRand.blockHash = [32]byte{} |
||||
msg := dRand.constructInitMessage() |
||||
|
||||
if len(msg) != 87 { |
||||
test.Errorf("Init message is not constructed in the correct size: %d", len(msg)) |
||||
} |
||||
} |
@ -0,0 +1,24 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
"github.com/harmony-one/harmony/p2p/p2pimpl" |
||||
) |
||||
|
||||
func TestNew(test *testing.T) { |
||||
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"} |
||||
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905"} |
||||
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") |
||||
host, err := p2pimpl.NewHost(&leader, priKey) |
||||
if err != nil { |
||||
test.Fatalf("newhost failure: %v", err) |
||||
} |
||||
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader) |
||||
|
||||
if !dRand.IsLeader { |
||||
test.Error("dRand should belong to a leader") |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
protobuf "github.com/golang/protobuf/proto" |
||||
drand_proto "github.com/harmony-one/harmony/api/drand" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p/host" |
||||
) |
||||
|
||||
// ProcessMessageValidator dispatches messages for the validator to corresponding processors.
|
||||
func (dRand *DRand) ProcessMessageValidator(payload []byte) { |
||||
message := drand_proto.Message{} |
||||
err := protobuf.Unmarshal(payload, &message) |
||||
|
||||
if err != nil { |
||||
utils.GetLogInstance().Error("Failed to unmarshal message payload.", "err", err, "dRand", dRand) |
||||
} |
||||
|
||||
switch message.Type { |
||||
case drand_proto.MessageType_COMMIT: |
||||
dRand.processInitMessage(message) |
||||
default: |
||||
utils.GetLogInstance().Error("Unexpected message type", "msgType", message.Type, "dRand", dRand) |
||||
} |
||||
} |
||||
|
||||
// ProcessMessageValidator dispatches validator's consensus message.
|
||||
func (dRand *DRand) processInitMessage(message drand_proto.Message) { |
||||
if message.Type != drand_proto.MessageType_INIT { |
||||
utils.GetLogInstance().Error("Wrong message type received", "expected", drand_proto.MessageType_INIT, "got", message.Type) |
||||
return |
||||
} |
||||
|
||||
blockHash := message.BlockHash |
||||
|
||||
// Verify message signature
|
||||
err := verifyMessageSig(dRand.leader.PubKey, message) |
||||
if err != nil { |
||||
utils.GetLogInstance().Warn("Failed to verify the message signature", "Error", err) |
||||
return |
||||
} |
||||
|
||||
// TODO: check the blockHash is the block hash of last block of last epoch.
|
||||
copy(dRand.blockHash[:], blockHash[:]) |
||||
|
||||
rand, proof := dRand.vrf(dRand.blockHash) |
||||
|
||||
msgToSend := dRand.constructCommitMessage(rand, proof) |
||||
|
||||
// Send the commit message back to leader
|
||||
host.SendMessage(dRand.host, dRand.leader, msgToSend, nil) |
||||
} |
@ -0,0 +1,22 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
drand_proto "github.com/harmony-one/harmony/api/drand" |
||||
"github.com/harmony-one/harmony/api/proto" |
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
) |
||||
|
||||
// Constructs the init message
|
||||
func (drand *DRand) constructCommitMessage(vrf [32]byte, proof []byte) []byte { |
||||
message := drand_proto.Message{} |
||||
message.Type = drand_proto.MessageType_COMMIT |
||||
|
||||
message.BlockHash = drand.blockHash[:] |
||||
message.Payload = append(vrf[:], proof...) |
||||
|
||||
marshaledMessage, err := drand.signAndMarshalDRandMessage(&message) |
||||
if err != nil { |
||||
utils.GetLogInstance().Error("Failed to sign and marshal the commit message", "error", err) |
||||
} |
||||
return proto.ConstructDRandMessage(marshaledMessage) |
||||
} |
@ -0,0 +1,26 @@ |
||||
package drand |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/harmony-one/harmony/internal/utils" |
||||
"github.com/harmony-one/harmony/p2p" |
||||
"github.com/harmony-one/harmony/p2p/p2pimpl" |
||||
) |
||||
|
||||
func TestConstructCommitMessage(test *testing.T) { |
||||
leader := p2p.Peer{IP: "127.0.0.1", Port: "19999"} |
||||
validator := p2p.Peer{IP: "127.0.0.1", Port: "55555"} |
||||
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") |
||||
host, err := p2pimpl.NewHost(&leader, priKey) |
||||
if err != nil { |
||||
test.Fatalf("newhost failure: %v", err) |
||||
} |
||||
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader) |
||||
dRand.blockHash = [32]byte{} |
||||
msg := dRand.constructCommitMessage([32]byte{}, []byte{}) |
||||
|
||||
if len(msg) != 121 { |
||||
test.Errorf("Commit message is not constructed in the correct size: %d", len(msg)) |
||||
} |
||||
} |
@ -0,0 +1,57 @@ |
||||
package utils |
||||
|
||||
import ( |
||||
"github.com/ethereum/go-ethereum/log" |
||||
net "github.com/libp2p/go-libp2p-net" |
||||
ma "github.com/multiformats/go-multiaddr" |
||||
) |
||||
|
||||
type connLogger struct{} |
||||
|
||||
func (connLogger) Listen(net net.Network, ma ma.Multiaddr) { |
||||
log.Debug("[CONNECTIONS] Listener starting", "net", net, "addr", ma) |
||||
} |
||||
|
||||
func (connLogger) ListenClose(net net.Network, ma ma.Multiaddr) { |
||||
log.Debug("[CONNECTIONS] Listener closing", "net", net, "addr", ma) |
||||
} |
||||
|
||||
func (connLogger) Connected(net net.Network, conn net.Conn) { |
||||
log.Debug("[CONNECTIONS] Connected", "net", net, |
||||
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
||||
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
||||
) |
||||
} |
||||
|
||||
func (connLogger) Disconnected(net net.Network, conn net.Conn) { |
||||
log.Debug("[CONNECTIONS] Disconnected", "net", net, |
||||
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
||||
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
||||
) |
||||
} |
||||
|
||||
func (connLogger) OpenedStream(net net.Network, stream net.Stream) { |
||||
conn := stream.Conn() |
||||
log.Debug("[CONNECTIONS] Stream opened", "net", net, |
||||
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
||||
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
||||
"protocol", stream.Protocol(), |
||||
) |
||||
} |
||||
|
||||
func (connLogger) ClosedStream(net net.Network, stream net.Stream) { |
||||
conn := stream.Conn() |
||||
log.Debug("[CONNECTIONS] Stream closed", "net", net, |
||||
"localPeer", conn.LocalPeer(), "localAddr", conn.LocalMultiaddr(), |
||||
"remotePeer", conn.RemotePeer(), "remoteAddr", conn.RemoteMultiaddr(), |
||||
"protocol", stream.Protocol(), |
||||
) |
||||
} |
||||
|
||||
// ConnLogger is a LibP2P connection logger.
|
||||
// Add on a LibP2P host by calling:
|
||||
//
|
||||
// host.Network().Notify(utils.ConnLogger)
|
||||
//
|
||||
// It logs all listener/connection/stream open/close activities at debug level.
|
||||
var ConnLogger connLogger |
Loading…
Reference in new issue