The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/consensus/consensus_validator_msg_tes...

74 lines
2.2 KiB

package consensus
import (
"testing"
6 years ago
protobuf "github.com/golang/protobuf/proto"
6 years ago
"github.com/harmony-one/harmony/api/proto"
6 years ago
msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/consensus/quorum"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
6 years ago
"github.com/harmony-one/harmony/p2p/p2pimpl"
"github.com/harmony-one/harmony/shard"
)
6 years ago
func TestConstructPrepareMessage(test *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9992"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
test.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(quorum.SuperMajorityVote)
consensus, err := New(
host, shard.BeaconChainShardID, leader, bls.RandPrivateKey(), decider,
)
if err != nil {
test.Fatalf("Cannot craeate consensus: %v", err)
}
consensus.blockHash = [32]byte{}
6 years ago
msgBytes := consensus.constructPrepareMessage()
6 years ago
msgBytes, err = proto.GetConsensusMessagePayload(msgBytes)
if err != nil {
test.Error("Error when getting consensus message", "error", err)
}
6 years ago
msg := &msg_pb.Message{}
if err := protobuf.Unmarshal(msgBytes, msg); err != nil {
test.Error("Can not parse the message", err)
} else {
6 years ago
if msg.GetConsensus() == nil {
6 years ago
test.Error("Wrong message")
}
}
}
6 years ago
func TestConstructCommitMessage(test *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
test.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(quorum.SuperMajorityVote)
consensus, err := New(
host, shard.BeaconChainShardID, leader, bls.RandPrivateKey(), decider,
)
if err != nil {
test.Fatalf("Cannot craeate consensus: %v", err)
}
consensus.blockHash = [32]byte{}
msg := consensus.constructCommitMessage([]byte("random string"))
6 years ago
msg, err = proto.GetConsensusMessagePayload(msg)
if err != nil {
test.Errorf("Failed to get consensus message")
}
6 years ago
message := &msg_pb.Message{}
if err = protobuf.Unmarshal(msg, message); err != nil {
test.Errorf("Error when unmarshalling a message")
}
}