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.
82 lines
2.6 KiB
82 lines
2.6 KiB
7 years ago
|
package consensus
|
||
|
|
||
|
import (
|
||
7 years ago
|
"testing"
|
||
6 years ago
|
|
||
6 years ago
|
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/crypto/bls"
|
||
|
"github.com/harmony-one/harmony/internal/ctxerror"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/utils"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p/p2pimpl"
|
||
5 years ago
|
"github.com/harmony-one/harmony/shard"
|
||
7 years ago
|
)
|
||
|
|
||
|
func TestConstructAnnounceMessage(test *testing.T) {
|
||
6 years ago
|
leader := p2p.Peer{IP: "127.0.0.1", Port: "19999"}
|
||
6 years ago
|
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
|
||
|
host, err := p2pimpl.NewHost(&leader, priKey)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
test.Fatalf("newhost failure: %v", err)
|
||
6 years ago
|
}
|
||
5 years ago
|
decider := quorum.NewDecider(quorum.SuperMajorityVote)
|
||
|
consensus, err := New(
|
||
5 years ago
|
host, shard.BeaconChainShardID, leader, bls.RandPrivateKey(), decider,
|
||
5 years ago
|
)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
test.Fatalf("Cannot create consensus: %v", err)
|
||
6 years ago
|
}
|
||
7 years ago
|
consensus.blockHash = [32]byte{}
|
||
5 years ago
|
if _, err = consensus.construct(msg_pb.MessageType_ANNOUNCE, nil); err != nil {
|
||
|
test.Fatalf("could not construct announce: %v", err)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
func TestConstructPreparedMessage(test *testing.T) {
|
||
6 years ago
|
leaderPriKey := bls.RandPrivateKey()
|
||
|
leaderPubKey := leaderPriKey.GetPublicKey()
|
||
5 years ago
|
leader := p2p.Peer{IP: "127.0.0.1", Port: "19999", ConsensusPubKey: leaderPubKey}
|
||
6 years ago
|
|
||
6 years ago
|
validatorPriKey := bls.RandPrivateKey()
|
||
|
validatorPubKey := leaderPriKey.GetPublicKey()
|
||
6 years ago
|
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
|
||
|
host, err := p2pimpl.NewHost(&leader, priKey)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
test.Fatalf("newhost failure: %v", err)
|
||
6 years ago
|
}
|
||
5 years ago
|
decider := quorum.NewDecider(quorum.SuperMajorityVote)
|
||
|
consensus, err := New(
|
||
5 years ago
|
host, shard.BeaconChainShardID, leader, bls.RandPrivateKey(), decider,
|
||
5 years ago
|
)
|
||
6 years ago
|
if err != nil {
|
||
|
test.Fatalf("Cannot craeate consensus: %v", err)
|
||
|
}
|
||
6 years ago
|
consensus.ResetState()
|
||
7 years ago
|
consensus.blockHash = [32]byte{}
|
||
6 years ago
|
|
||
6 years ago
|
message := "test string"
|
||
5 years ago
|
consensus.Decider.SubmitVote(
|
||
|
quorum.Prepare, leaderPubKey, leaderPriKey.Sign(message), nil,
|
||
5 years ago
|
)
|
||
5 years ago
|
consensus.Decider.SubmitVote(
|
||
|
quorum.Prepare, validatorPubKey, validatorPriKey.Sign(message), nil,
|
||
5 years ago
|
)
|
||
5 years ago
|
|
||
6 years ago
|
// According to RJ these failures are benign.
|
||
|
if err := consensus.prepareBitmap.SetKey(leaderPubKey, true); err != nil {
|
||
|
test.Log(ctxerror.New("prepareBitmap.SetKey").WithCause(err))
|
||
|
}
|
||
|
if err := consensus.prepareBitmap.SetKey(validatorPubKey, true); err != nil {
|
||
|
test.Log(ctxerror.New("prepareBitmap.SetKey").WithCause(err))
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
network, err := consensus.construct(msg_pb.MessageType_PREPARED, nil)
|
||
|
if err != nil {
|
||
6 years ago
|
test.Errorf("Error when creating prepared message")
|
||
|
}
|
||
5 years ago
|
if network.Phase != msg_pb.MessageType_PREPARED {
|
||
6 years ago
|
test.Error("it did not created prepared message")
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|