RunAction* directly on ConsensusState.

This commit is contained in:
Jae Kwon
2014-10-21 01:18:46 -07:00
parent f5d0794756
commit 5ffe406f16
6 changed files with 235 additions and 148 deletions

View File

@ -58,7 +58,7 @@ func TestSetupRound(t *testing.T) {
// Add a vote, precommit, and commit by val0.
voteTypes := []byte{VoteTypePrevote, VoteTypePrecommit, VoteTypeCommit}
for _, voteType := range voteTypes {
vote := &Vote{Height: 0, Round: 0, Type: voteType} // nil vote
vote := &Vote{Height: 1, Round: 0, Type: voteType} // nil vote
privAccounts[0].Sign(vote)
cs.AddVote(vote)
}
@ -66,13 +66,13 @@ func TestSetupRound(t *testing.T) {
// Ensure that vote appears in RoundState.
rs0 := cs.GetRoundState()
if vote := rs0.Prevotes.Get(0); vote == nil || vote.Type != VoteTypePrevote {
t.Errorf("Expected to find prevote %v, not there", vote)
t.Errorf("Expected to find prevote but got %v", vote)
}
if vote := rs0.Precommits.Get(0); vote == nil || vote.Type != VoteTypePrecommit {
t.Errorf("Expected to find precommit %v, not there", vote)
t.Errorf("Expected to find precommit but got %v", vote)
}
if vote := rs0.Commits.Get(0); vote == nil || vote.Type != VoteTypeCommit {
t.Errorf("Expected to find commit %v, not there", vote)
t.Errorf("Expected to find commit but got %v", vote)
}
// Setup round 1 (next round)
@ -81,13 +81,13 @@ func TestSetupRound(t *testing.T) {
// Now the commit should be copied over to prevotes and precommits.
rs1 := cs.GetRoundState()
if vote := rs1.Prevotes.Get(0); vote == nil || vote.Type != VoteTypeCommit {
t.Errorf("Expected to find commit %v, not there", vote)
t.Errorf("Expected to find commit but got %v", vote)
}
if vote := rs1.Precommits.Get(0); vote == nil || vote.Type != VoteTypeCommit {
t.Errorf("Expected to find commit %v, not there", vote)
t.Errorf("Expected to find commit but got %v", vote)
}
if vote := rs1.Commits.Get(0); vote == nil || vote.Type != VoteTypeCommit {
t.Errorf("Expected to find commit %v, not there", vote)
t.Errorf("Expected to find commit but got %v", vote)
}
// Setup round 1 (should fail)
@ -102,22 +102,125 @@ func TestSetupRound(t *testing.T) {
}
func TestMakeProposalNoPrivValidator(t *testing.T) {
func TestRunActionProposeNoPrivValidator(t *testing.T) {
cs, _ := makeConsensusState()
cs.MakeProposal()
cs.RunActionPropose(1, 0)
rs := cs.GetRoundState()
if rs.Proposal != nil {
t.Error("Expected to make no proposal, since no privValidator")
}
}
func TestMakeProposalEmptyMempool(t *testing.T) {
func TestRunActionPropose(t *testing.T) {
cs, privAccounts := makeConsensusState()
priv := NewPrivValidator(privAccounts[0], db_.NewMemDB())
cs.SetPrivValidator(priv)
cs.MakeProposal()
cs.RunActionPropose(1, 0)
rs := cs.GetRoundState()
t.Log(rs.Proposal)
// Check that Proposal, ProposalBlock, ProposalBlockPartSet are set.
if rs.Proposal == nil {
t.Error("rs.Proposal should be set")
}
if rs.ProposalBlock == nil {
t.Error("rs.ProposalBlock should be set")
}
if rs.ProposalBlockPartSet.Total() == 0 {
t.Error("rs.ProposalBlockPartSet should be set")
}
}
func checkRoundState(t *testing.T, cs *ConsensusState,
height uint32, round uint16, step RoundStep) {
rs := cs.GetRoundState()
if rs.Height != height {
t.Errorf("cs.RoundState.Height should be %v, got %v", height, rs.Height)
}
if rs.Round != round {
t.Errorf("cs.RoundState.Round should be %v, got %v", round, rs.Round)
}
if rs.Step != step {
t.Errorf("cs.RoundState.Step should be %v, got %v", step, rs.Step)
}
}
func TestRunActionPrecommit(t *testing.T) {
cs, privAccounts := makeConsensusState()
priv := NewPrivValidator(privAccounts[0], db_.NewMemDB())
cs.SetPrivValidator(priv)
blockHash := cs.RunActionPrecommit(1, 0)
if blockHash != nil {
t.Errorf("RunActionPrecommit should fail without a proposal")
}
cs.RunActionPropose(1, 0)
// Test RunActionPrecommit failures:
blockHash = cs.RunActionPrecommit(1, 1)
if blockHash != nil {
t.Errorf("RunActionPrecommit should fail for wrong round")
}
blockHash = cs.RunActionPrecommit(2, 0)
if blockHash != nil {
t.Errorf("RunActionPrecommit should fail for wrong height")
}
blockHash = cs.RunActionPrecommit(1, 0)
if blockHash != nil {
t.Errorf("RunActionPrecommit should fail, not enough prevotes")
}
// Add at least +2/3 prevotes.
for i := 0; i < 7; i++ {
vote := &Vote{
Height: 1,
Round: uint16(i),
Type: VoteTypePrevote,
BlockHash: cs.ProposalBlock.Hash(),
}
privAccounts[i].Sign(vote)
cs.AddVote(vote)
}
// Test RunActionPrecommit success:
blockHash = cs.RunActionPrecommit(1, 0)
if len(blockHash) == 0 {
t.Errorf("RunActionPrecommit should have succeeded")
}
checkRoundState(t, cs, 1, 0, RoundStepPrecommit)
// Test RunActionCommit failures:
blockHash = cs.RunActionCommit(1, 1)
if blockHash != nil {
t.Errorf("RunActionCommit should fail for wrong round")
}
blockHash = cs.RunActionCommit(2, 0)
if blockHash != nil {
t.Errorf("RunActionCommit should fail for wrong height")
}
blockHash = cs.RunActionCommit(1, 0)
if blockHash != nil {
t.Errorf("RunActionCommit should fail, not enough commits")
}
// Add at least +2/3 precommits.
for i := 0; i < 7; i++ {
vote := &Vote{
Height: 1,
Round: uint16(i),
Type: VoteTypePrecommit,
BlockHash: cs.ProposalBlock.Hash(),
}
privAccounts[i].Sign(vote)
cs.AddVote(vote)
}
// Test RunActionCommit success:
blockHash = cs.RunActionCommit(1, 0)
if len(blockHash) == 0 {
t.Errorf("RunActionCommit should have succeeded")
}
checkRoundState(t, cs, 1, 0, RoundStepCommit)
}