parent
5b7f07e53e
commit
86486fa054
@ -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)) |
||||
} |
||||
} |
Loading…
Reference in new issue