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.
89 lines
2.5 KiB
89 lines
2.5 KiB
6 years ago
|
package consensus
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
protobuf "github.com/golang/protobuf/proto"
|
||
|
"github.com/harmony-one/harmony/api/proto"
|
||
|
msg_pb "github.com/harmony-one/harmony/api/proto/message"
|
||
5 years ago
|
"github.com/harmony-one/harmony/consensus/quorum"
|
||
|
"github.com/harmony-one/harmony/core/values"
|
||
6 years ago
|
"github.com/harmony-one/harmony/crypto/bls"
|
||
|
"github.com/harmony-one/harmony/internal/utils"
|
||
|
"github.com/harmony-one/harmony/p2p"
|
||
|
"github.com/harmony-one/harmony/p2p/p2pimpl"
|
||
|
)
|
||
|
|
||
|
func constructAnnounceMessage(t *testing.T) []byte {
|
||
|
leader := p2p.Peer{IP: "127.0.0.1", Port: "19999"}
|
||
|
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
|
||
|
host, err := p2pimpl.NewHost(&leader, priKey)
|
||
|
if err != nil {
|
||
|
t.Fatalf("newhost failure: %v", err)
|
||
|
}
|
||
5 years ago
|
decider := quorum.NewDecider(quorum.SuperMajorityVote)
|
||
|
consensus, err := New(
|
||
|
host, values.BeaconChainShardID, leader, bls.RandPrivateKey(), decider,
|
||
|
)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Cannot create consensus: %v", err)
|
||
6 years ago
|
}
|
||
|
consensus.blockHash = [32]byte{}
|
||
|
|
||
|
msgBytes := consensus.constructAnnounceMessage()
|
||
|
msgPayload, _ := proto.GetConsensusMessagePayload(msgBytes)
|
||
|
return msgPayload
|
||
|
}
|
||
|
|
||
|
func getConsensusMessage(payload []byte) (*msg_pb.Message, error) {
|
||
|
msg := &msg_pb.Message{}
|
||
|
err := protobuf.Unmarshal(payload, msg)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return msg, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func TestParseFBFTMessage(t *testing.T) {
|
||
6 years ago
|
payload := constructAnnounceMessage(t)
|
||
|
msg, err := getConsensusMessage(payload)
|
||
|
if err != nil {
|
||
|
t.Error("create consensus message error")
|
||
|
}
|
||
5 years ago
|
_, err = ParseFBFTMessage(msg)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
t.Error("unable to parse FBFTMessage")
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func TestGetMessagesByTypeSeqViewHash(t *testing.T) {
|
||
5 years ago
|
pbftMsg := FBFTMessage{MessageType: msg_pb.MessageType_ANNOUNCE, BlockNum: 2, ViewID: 3, BlockHash: [32]byte{01, 02}}
|
||
|
log := NewFBFTLog()
|
||
6 years ago
|
log.AddMessage(&pbftMsg)
|
||
|
|
||
|
found := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, 2, 3, [32]byte{01, 02})
|
||
|
if len(found) != 1 {
|
||
|
t.Error("cannot find existing message")
|
||
|
}
|
||
|
|
||
|
notFound := log.GetMessagesByTypeSeqViewHash(msg_pb.MessageType_ANNOUNCE, 2, 3, [32]byte{01, 03})
|
||
|
if len(notFound) > 0 {
|
||
|
t.Error("find message that not exist")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestHasMatchingAnnounce(t *testing.T) {
|
||
5 years ago
|
pbftMsg := FBFTMessage{MessageType: msg_pb.MessageType_ANNOUNCE, BlockNum: 2, ViewID: 3, BlockHash: [32]byte{01, 02}}
|
||
|
log := NewFBFTLog()
|
||
6 years ago
|
log.AddMessage(&pbftMsg)
|
||
6 years ago
|
found := log.HasMatchingViewAnnounce(2, 3, [32]byte{01, 02})
|
||
6 years ago
|
if !found {
|
||
|
t.Error("found should be true")
|
||
|
}
|
||
|
|
||
6 years ago
|
notFound := log.HasMatchingViewAnnounce(2, 3, [32]byte{02, 02})
|
||
6 years ago
|
if notFound {
|
||
|
t.Error("notFound should be false")
|
||
|
}
|
||
|
}
|