diff --git a/drand/drand.go b/drand/drand.go index 7221744b3..11758026d 100644 --- a/drand/drand.go +++ b/drand/drand.go @@ -9,7 +9,7 @@ import ( protobuf "github.com/golang/protobuf/proto" "github.com/harmony-one/bls/ffi/go/bls" - drand_proto "github.com/harmony-one/harmony/api/drand" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" bls_cosi "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/crypto/vrf" @@ -149,9 +149,8 @@ func (dRand *DRand) AddPeers(peers []*p2p.Peer) int { } // Sign on the drand message signature field. -func (dRand *DRand) signDRandMessage(message *drand_proto.Message) error { +func (dRand *DRand) signDRandMessage(message *msg_pb.Message) error { message.Signature = nil - // TODO: use custom serialization method rather than protobuf marshaledMessage, err := protobuf.Marshal(message) if err != nil { return err @@ -165,7 +164,7 @@ func (dRand *DRand) signDRandMessage(message *drand_proto.Message) error { } // Signs the drand message and returns the marshaled message. -func (dRand *DRand) signAndMarshalDRandMessage(message *drand_proto.Message) ([]byte, error) { +func (dRand *DRand) signAndMarshalDRandMessage(message *msg_pb.Message) ([]byte, error) { err := dRand.signDRandMessage(message) if err != nil { return []byte{}, err @@ -199,10 +198,10 @@ func (dRand *DRand) GetValidatorPeers() []p2p.Peer { } // Verify the signature of the message are valid from the signer's public key. -func verifyMessageSig(signerPubKey *bls.PublicKey, message drand_proto.Message) error { +func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) error { signature := message.Signature message.Signature = nil - messageBytes, err := protobuf.Marshal(&message) + messageBytes, err := protobuf.Marshal(message) if err != nil { return err } diff --git a/drand/drand_leader.go b/drand/drand_leader.go index 8bf22867b..aebf13d97 100644 --- a/drand/drand_leader.go +++ b/drand/drand_leader.go @@ -6,6 +6,7 @@ import ( protobuf "github.com/golang/protobuf/proto" drand_proto "github.com/harmony-one/harmony/api/drand" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/crypto/vdf" @@ -80,15 +81,15 @@ func (dRand *DRand) init(epochBlock *types.Block) { // ProcessMessageLeader dispatches messages for the leader to corresponding processors. func (dRand *DRand) ProcessMessageLeader(payload []byte) { - message := drand_proto.Message{} - err := protobuf.Unmarshal(payload, &message) + message := &msg_pb.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: + case msg_pb.MessageType_COMMIT: dRand.processCommitMessage(message) default: utils.GetLogInstance().Error("Unexpected message type", "msgType", message.Type, "dRand", dRand) @@ -96,8 +97,8 @@ func (dRand *DRand) ProcessMessageLeader(payload []byte) { } // ProcessMessageValidator dispatches validator's consensus message. -func (dRand *DRand) processCommitMessage(message drand_proto.Message) { - if message.Type != drand_proto.MessageType_COMMIT { +func (dRand *DRand) processCommitMessage(message *msg_pb.Message) { + if message.Type != msg_pb.MessageType_DRAND_COMMIT { utils.GetLogInstance().Error("Wrong message type received", "expected", drand_proto.MessageType_COMMIT, "got", message.Type) return } @@ -105,7 +106,8 @@ func (dRand *DRand) processCommitMessage(message drand_proto.Message) { dRand.mutex.Lock() defer dRand.mutex.Unlock() - validatorID := message.SenderId + drandMsg := message.GetDrand() + validatorID := drandMsg.SenderId validatorPeer := dRand.getValidatorPeerByID(validatorID) vrfs := dRand.vrfs if len((*vrfs)) >= ((len(dRand.PublicKeys))/3 + 1) { @@ -120,9 +122,9 @@ func (dRand *DRand) processCommitMessage(message drand_proto.Message) { return } - rand := message.Payload[:32] - proof := message.Payload[32 : len(message.Payload)-64] - pubKeyBytes := message.Payload[len(message.Payload)-64:] + rand := drandMsg.Payload[:32] + proof := drandMsg.Payload[32 : len(drandMsg.Payload)-64] + pubKeyBytes := drandMsg.Payload[len(drandMsg.Payload)-64:] _, pubKey := p256.GenerateKey() pubKey.Deserialize(pubKeyBytes) @@ -135,7 +137,7 @@ func (dRand *DRand) processCommitMessage(message drand_proto.Message) { utils.GetLogInstance().Debug("Received new VRF commit", "numReceivedSoFar", len((*vrfs)), "validatorID", validatorID, "PublicKeys", len(dRand.PublicKeys)) - (*vrfs)[validatorID] = message.Payload + (*vrfs)[validatorID] = drandMsg.Payload dRand.bitmap.SetKey(validatorPeer.ConsensusPubKey, true) // Set the bitmap indicating that this validator signed. if len((*vrfs)) >= ((len(dRand.PublicKeys))/3 + 1) { diff --git a/drand/drand_leader_msg.go b/drand/drand_leader_msg.go index 44cefc822..2c5ad8c08 100644 --- a/drand/drand_leader_msg.go +++ b/drand/drand_leader_msg.go @@ -1,20 +1,27 @@ package drand import ( - drand_proto "github.com/harmony-one/harmony/api/drand" "github.com/harmony-one/harmony/api/proto" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "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.SenderId = dRand.nodeID + message := &msg_pb.Message{ + ReceiverType: msg_pb.ReceiverType_VALIDATOR, + ServiceType: msg_pb.ServiceType_DRAND, + Type: msg_pb.MessageType_DRAND_INIT, + Request: &msg_pb.Message_Drand{ + Drand: &msg_pb.DrandRequest{}, + }, + } - message.BlockHash = dRand.blockHash[:] + drandMsg := message.GetDrand() + drandMsg.SenderId = dRand.nodeID + drandMsg.BlockHash = dRand.blockHash[:] // Don't need the payload in init message - marshaledMessage, err := dRand.signAndMarshalDRandMessage(&message) + marshaledMessage, err := dRand.signAndMarshalDRandMessage(message) if err != nil { utils.GetLogInstance().Error("Failed to sign and marshal the init message", "error", err) } diff --git a/drand/drand_leader_msg_test.go b/drand/drand_leader_msg_test.go index 9c98b5cb1..1b90a9b6a 100644 --- a/drand/drand_leader_msg_test.go +++ b/drand/drand_leader_msg_test.go @@ -4,8 +4,8 @@ import ( "testing" protobuf "github.com/golang/protobuf/proto" - drand_proto "github.com/harmony-one/harmony/api/drand" "github.com/harmony-one/harmony/api/proto" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" @@ -25,8 +25,8 @@ func TestConstructInitMessage(test *testing.T) { msgPayload, _ := proto.GetDRandMessagePayload(msg) - message := drand_proto.Message{} - err = protobuf.Unmarshal(msgPayload, &message) + message := &msg_pb.Message{} + err = protobuf.Unmarshal(msgPayload, message) if err != nil { test.Error("Error in extracting Init message from payload", err) @@ -47,12 +47,10 @@ func TestProcessCommitMessage(test *testing.T) { msgPayload, _ := proto.GetDRandMessagePayload(msg) - message := drand_proto.Message{} - err = protobuf.Unmarshal(msgPayload, &message) + message := &msg_pb.Message{} + err = protobuf.Unmarshal(msgPayload, message) if err != nil { test.Error("Error in extracting Commit message from payload", err) } - - dRand.ProcessMessageLeader(msgPayload) } diff --git a/drand/drand_test.go b/drand/drand_test.go index 567e2ac96..705b46135 100644 --- a/drand/drand_test.go +++ b/drand/drand_test.go @@ -7,7 +7,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/bls/ffi/go/bls" - drand_proto "github.com/harmony-one/harmony/api/drand" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" @@ -167,12 +167,20 @@ func TestVerifyMessageSig(test *testing.T) { test.Fatalf("newhost failure: %v", err) } dRand := New(host, "0", []p2p.Peer{leader, validator}, leader, nil, true) - message := drand_proto.Message{} - message.Type = drand_proto.MessageType_INIT - message.SenderId = dRand.nodeID - message.BlockHash = dRand.blockHash[:] - dRand.signDRandMessage(&message) + message := &msg_pb.Message{ + ReceiverType: msg_pb.ReceiverType_VALIDATOR, + ServiceType: msg_pb.ServiceType_DRAND, + Type: msg_pb.MessageType_DRAND_INIT, + Request: &msg_pb.Message_Drand{ + Drand: &msg_pb.DrandRequest{}, + }, + } + drandMsg := message.GetDrand() + drandMsg.SenderId = dRand.nodeID + drandMsg.BlockHash = dRand.blockHash[:] + + dRand.signDRandMessage(message) if verifyMessageSig(dRand.pubKey, message) != nil { test.Error("Failed to verify the signature") diff --git a/drand/drand_validator.go b/drand/drand_validator.go index 795db0ab9..1182c4b69 100644 --- a/drand/drand_validator.go +++ b/drand/drand_validator.go @@ -3,6 +3,7 @@ package drand import ( protobuf "github.com/golang/protobuf/proto" drand_proto "github.com/harmony-one/harmony/api/drand" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/host" @@ -10,31 +11,28 @@ import ( // ProcessMessageValidator dispatches messages for the validator to corresponding processors. func (dRand *DRand) ProcessMessageValidator(payload []byte) { - message := drand_proto.Message{} - err := protobuf.Unmarshal(payload, &message) + message := &msg_pb.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_INIT: + if message.Type == msg_pb.MessageType_DRAND_INIT { dRand.processInitMessage(message) - case drand_proto.MessageType_COMMIT: - // do nothing on the COMMIT message, as it is intended to send to leader - default: + } else { 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) +func (dRand *DRand) processInitMessage(message *msg_pb.Message) { + if message.Type != msg_pb.MessageType_DRAND_INIT { + utils.GetLogInstance().Error("Wrong message type received", "expected", drand_proto.MessageType_COMMIT, "got", message.Type) return } - blockHash := message.BlockHash + drandMsg := message.GetDrand() // Verify message signature err := verifyMessageSig(dRand.leader.ConsensusPubKey, message) @@ -45,6 +43,7 @@ func (dRand *DRand) processInitMessage(message drand_proto.Message) { utils.GetLogInstance().Debug("[DRG] verify the message signature Succeeded") // TODO: check the blockHash is the block hash of last block of last epoch. + blockHash := drandMsg.BlockHash copy(dRand.blockHash[:], blockHash[:]) rand, proof := dRand.vrf(dRand.blockHash) diff --git a/drand/drand_validator_msg.go b/drand/drand_validator_msg.go index 067458015..6d91e1936 100644 --- a/drand/drand_validator_msg.go +++ b/drand/drand_validator_msg.go @@ -1,23 +1,31 @@ package drand import ( - drand_proto "github.com/harmony-one/harmony/api/drand" "github.com/harmony-one/harmony/api/proto" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "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.SenderId = dRand.nodeID + message := &msg_pb.Message{ + ReceiverType: msg_pb.ReceiverType_LEADER, + ServiceType: msg_pb.ServiceType_DRAND, + Type: msg_pb.MessageType_DRAND_COMMIT, + Request: &msg_pb.Message_Drand{ + Drand: &msg_pb.DrandRequest{}, + }, + } - message.BlockHash = dRand.blockHash[:] - message.Payload = append(vrf[:], proof...) + drandMsg := message.GetDrand() + drandMsg.SenderId = dRand.nodeID + drandMsg.BlockHash = dRand.blockHash[:] + drandMsg.BlockHash = dRand.blockHash[:] + drandMsg.Payload = append(vrf[:], proof...) // Adding the public key into payload so leader can verify the vrf // TODO: change the curve to follow the same curve with consensus, so the public key doesn't need to be attached. - message.Payload = append(message.Payload, (*dRand.vrfPubKey).Serialize()...) - marshaledMessage, err := dRand.signAndMarshalDRandMessage(&message) + drandMsg.Payload = append(drandMsg.Payload, (*dRand.vrfPubKey).Serialize()...) + marshaledMessage, err := dRand.signAndMarshalDRandMessage(message) if err != nil { utils.GetLogInstance().Error("Failed to sign and marshal the commit message", "error", err) } diff --git a/drand/drand_validator_msg_test.go b/drand/drand_validator_msg_test.go index 8fee27314..cb9dd973f 100644 --- a/drand/drand_validator_msg_test.go +++ b/drand/drand_validator_msg_test.go @@ -4,8 +4,8 @@ import ( "testing" protobuf "github.com/golang/protobuf/proto" - drand_proto "github.com/harmony-one/harmony/api/drand" "github.com/harmony-one/harmony/api/proto" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" @@ -24,8 +24,8 @@ func TestConstructCommitMessage(test *testing.T) { msg := dRand.constructCommitMessage([32]byte{}, []byte{}) msgPayload, _ := proto.GetDRandMessagePayload(msg) - message := drand_proto.Message{} - err = protobuf.Unmarshal(msgPayload, &message) + message := &msg_pb.Message{} + err = protobuf.Unmarshal(msgPayload, message) if err != nil { test.Error("Error in extracting Commit message from payload", err) @@ -46,8 +46,8 @@ func TestProcessInitMessage(test *testing.T) { msgPayload, _ := proto.GetDRandMessagePayload(msg) - message := drand_proto.Message{} - err = protobuf.Unmarshal(msgPayload, &message) + message := &msg_pb.Message{} + err = protobuf.Unmarshal(msgPayload, message) if err != nil { test.Error("Error in extracting Init message from payload", err)