Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing issue when a block has max transactions #735

Merged
merged 1 commit into from Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 22 additions & 19 deletions consensus/consensus_test.go
Expand Up @@ -128,14 +128,14 @@ func setupWithSeed(t *testing.T, seed int64) *testData {
message: msg,
})
}
td.consX = newConsensus(testConfig(), stX, valKeys[tIndexX], valKeys[tIndexX].Address(),
broadcaster, newConcreteMediator())
td.consY = newConsensus(testConfig(), stY, valKeys[tIndexY], valKeys[tIndexY].Address(),
broadcaster, newConcreteMediator())
td.consB = newConsensus(testConfig(), stB, valKeys[tIndexB], valKeys[tIndexB].Address(),
broadcaster, newConcreteMediator())
td.consP = newConsensus(testConfig(), stP, valKeys[tIndexP], valKeys[tIndexP].Address(),
broadcaster, newConcreteMediator())
td.consX = newConsensus(testConfig(), stX, valKeys[tIndexX],
valKeys[tIndexX].PublicKey().AccountAddress(), broadcaster, newConcreteMediator())
td.consY = newConsensus(testConfig(), stY, valKeys[tIndexY],
valKeys[tIndexY].PublicKey().AccountAddress(), broadcaster, newConcreteMediator())
td.consB = newConsensus(testConfig(), stB, valKeys[tIndexB],
valKeys[tIndexB].PublicKey().AccountAddress(), broadcaster, newConcreteMediator())
td.consP = newConsensus(testConfig(), stP, valKeys[tIndexP],
valKeys[tIndexP].PublicKey().AccountAddress(), broadcaster, newConcreteMediator())

// -------------------------------
// Better logging during testing
Expand Down Expand Up @@ -342,12 +342,13 @@ func (td *testData) commitBlockForAllStates(t *testing.T) (*block.Block, *certif
p := td.makeProposal(t, height+1, 0)

sb := certificate.BlockCertificateSignBytes(p.Block().Hash(), height+1, 0)
sig1 := td.valKeys[0].Sign(sb)
sig2 := td.valKeys[1].Sign(sb)
sig4 := td.valKeys[3].Sign(sb)
sig1 := td.consX.valKey.Sign(sb)
sig2 := td.consY.valKey.Sign(sb)
sig4 := td.consP.valKey.Sign(sb)

sig := bls.SignatureAggregate(sig1, sig2, sig4)
cert := certificate.NewCertificate(height+1, 0, []int32{0, 1, 2, 3}, []int32{2}, sig)
cert := certificate.NewCertificate(height+1, 0,
[]int32{tIndexX, tIndexY, tIndexB, tIndexP}, []int32{tIndexB}, sig)
blk := p.Block()

err = td.consX.state.CommitBlock(blk, cert)
Expand All @@ -368,22 +369,22 @@ func (td *testData) makeProposal(t *testing.T, height uint32, round int16) *prop
var p *proposal.Proposal
switch (height % 4) + uint32(round%4) {
case 1:
blk, err := td.consX.state.ProposeBlock(td.consX.valKey, td.consX.rewardAddr, round)
blk, err := td.consX.state.ProposeBlock(td.consX.valKey, td.consX.rewardAddr)
require.NoError(t, err)
p = proposal.NewProposal(height, round, blk)
td.HelperSignProposal(td.consX.valKey, p)
case 2:
blk, err := td.consY.state.ProposeBlock(td.consY.valKey, td.consY.rewardAddr, round)
blk, err := td.consY.state.ProposeBlock(td.consY.valKey, td.consY.rewardAddr)
require.NoError(t, err)
p = proposal.NewProposal(height, round, blk)
td.HelperSignProposal(td.consY.valKey, p)
case 3:
blk, err := td.consB.state.ProposeBlock(td.consB.valKey, td.consB.rewardAddr, round)
blk, err := td.consB.state.ProposeBlock(td.consB.valKey, td.consB.rewardAddr)
require.NoError(t, err)
p = proposal.NewProposal(height, round, blk)
td.HelperSignProposal(td.consB.valKey, p)
case 0, 4:
blk, err := td.consP.state.ProposeBlock(td.consP.valKey, td.consP.rewardAddr, round)
blk, err := td.consP.state.ProposeBlock(td.consP.valKey, td.consP.rewardAddr)
require.NoError(t, err)
p = proposal.NewProposal(height, round, blk)
td.HelperSignProposal(td.consP.valKey, p)
Expand Down Expand Up @@ -602,9 +603,10 @@ func TestDuplicateProposal(t *testing.T) {
h := uint32(4)
r := int16(0)
p1 := td.makeProposal(t, h, r)
trx := tx.NewTransferTx(h, td.valKeys[0].Address(),
td.valKeys[1].Address(), 1000, 1000, "proposal changer")
td.HelperSignTransaction(td.valKeys[0].PrivateKey(), trx)
trx := tx.NewTransferTx(h, td.consX.rewardAddr,
td.RandAccAddress(), 1000, 1000, "proposal changer")
td.HelperSignTransaction(td.consX.valKey.PrivateKey(), trx)

assert.NoError(t, td.txPool.AppendTx(trx))
p2 := td.makeProposal(t, h, r)
assert.NotEqual(t, p1.Hash(), p2.Hash())
Expand Down Expand Up @@ -794,6 +796,7 @@ func TestByzantine(t *testing.T) {
// Byzantine node create the second proposal and send it to the partitioned node P
byzTrx := tx.NewTransferTx(h,
td.consB.rewardAddr, td.RandAccAddress(), 1000, 1000, "")
td.HelperSignTransaction(td.consB.valKey.PrivateKey(), byzTrx)
assert.NoError(t, td.txPool.AppendTx(byzTrx))
p2 := td.makeProposal(t, h, r)

Expand Down
42 changes: 21 additions & 21 deletions consensus/cp_test.go
Expand Up @@ -153,7 +153,7 @@ func TestInvalidJustInitOne(t *testing.T) {
just := &vote.JustInitOne{}

t.Run("invalid value: zero", func(t *testing.T) {
v := vote.NewCPPreVote(hash.UndefHash, h, r, 0, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(hash.UndefHash, h, r, 0, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -163,7 +163,7 @@ func TestInvalidJustInitOne(t *testing.T) {
})

t.Run("invalid block hash", func(t *testing.T) {
v := vote.NewCPPreVote(hash.UndefHash, h, r, 1, vote.CPValueOne, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(hash.UndefHash, h, r, 1, vote.CPValueOne, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -173,7 +173,7 @@ func TestInvalidJustInitOne(t *testing.T) {
})

t.Run("cp-round should be zero", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -184,7 +184,7 @@ func TestInvalidJustInitOne(t *testing.T) {

t.Run("with main-vote justification", func(t *testing.T) {
invJust := &vote.JustMainVoteNoConflict{}
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, invJust, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, invJust, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -205,7 +205,7 @@ func TestInvalidJustInitZero(t *testing.T) {
}

t.Run("invalid value: one", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -215,7 +215,7 @@ func TestInvalidJustInitZero(t *testing.T) {
})

t.Run("cp-round should be zero", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -225,7 +225,7 @@ func TestInvalidJustInitZero(t *testing.T) {
})

t.Run("invalid certificate", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -246,7 +246,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
}

t.Run("invalid value: abstain", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -256,7 +256,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
})

t.Run("cp-round should not be zero", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -266,7 +266,7 @@ func TestInvalidJustPreVoteHard(t *testing.T) {
})

t.Run("invalid certificate", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -287,7 +287,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
}

t.Run("invalid value: abstain", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -297,7 +297,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
})

t.Run("cp-round should not be zero", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -307,7 +307,7 @@ func TestInvalidJustPreVoteSoft(t *testing.T) {
})

t.Run("invalid certificate", func(t *testing.T) {
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPPreVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -328,7 +328,7 @@ func TestInvalidJustMainVoteNoConflict(t *testing.T) {
}

t.Run("invalid value: abstain", func(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -338,7 +338,7 @@ func TestInvalidJustMainVoteNoConflict(t *testing.T) {
})

t.Run("invalid certificate", func(t *testing.T) {
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -362,7 +362,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
},
Just1: &vote.JustInitOne{},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueZero, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -378,7 +378,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
},
Just1: &vote.JustInitOne{},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueOne, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -394,7 +394,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
},
Just1: &vote.JustInitOne{},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -412,7 +412,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
QCert: td.GenerateTestCertificate(h),
},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -429,7 +429,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
Just0: just0,
Just1: &vote.JustInitOne{},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 0, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand All @@ -448,7 +448,7 @@ func TestInvalidJustMainVoteConflict(t *testing.T) {
QCert: td.GenerateTestCertificate(h),
},
}
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.valKeys[tIndexB].Address())
v := vote.NewCPMainVote(td.RandHash(), h, r, 1, vote.CPValueAbstain, just, td.consB.valKey.Address())

err := td.consX.checkJust(v)
assert.ErrorIs(t, err, invalidJustificationError{
Expand Down
3 changes: 1 addition & 2 deletions consensus/log/log_test.go
Expand Up @@ -60,7 +60,7 @@ func TestAddValidVote(t *testing.T) {
func TestAddInvalidVoteType(t *testing.T) {
ts := testsuite.NewTestSuite(t)

committee, valKeys := ts.GenerateTestCommittee(4)
committee, _ := ts.GenerateTestCommittee(4)
log := NewLog()
log.MoveToNewHeight(committee.Validators())

Expand All @@ -69,7 +69,6 @@ func TestAddInvalidVoteType(t *testing.T) {
invVote := new(vote.Vote)
err := invVote.UnmarshalCBOR(data)
assert.NoError(t, err)
ts.HelperSignVote(valKeys[0], invVote)

added, err := log.AddVote(invVote)
assert.Error(t, err)
Expand Down
10 changes: 5 additions & 5 deletions consensus/manager_test.go
Expand Up @@ -61,7 +61,7 @@ func TestManager(t *testing.T) {
})

t.Run("Testing set proposal", func(t *testing.T) {
b, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address(), 1)
b, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address())
p := proposal.NewProposal(stateHeight+1, 0, b)
ts.HelperSignProposal(valKeys[0], p)

Expand All @@ -81,7 +81,7 @@ func TestManager(t *testing.T) {
})

t.Run("Check discarding old proposals", func(t *testing.T) {
b, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address(), 1)
b, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address())
p := proposal.NewProposal(stateHeight-1, 1, b)
ts.HelperSignProposal(valKeys[0], p)

Expand Down Expand Up @@ -120,13 +120,13 @@ func TestManager(t *testing.T) {
})

t.Run("Processing upcoming proposal", func(t *testing.T) {
b1, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address(), 1)
b1, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address())
p1 := proposal.NewProposal(stateHeight+2, 0, b1)

b2, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address(), 1)
b2, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address())
p2 := proposal.NewProposal(stateHeight+3, 0, b2)

b3, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address(), 1)
b3, _ := state.ProposeBlock(valKeys[0], valKeys[0].Address())
p3 := proposal.NewProposal(stateHeight+4, 0, b3)

ts.HelperSignProposal(valKeys[0], p1)
Expand Down
6 changes: 3 additions & 3 deletions consensus/precommit_test.go
Expand Up @@ -39,9 +39,9 @@ func TestPrecommitDuplicatedProposal(t *testing.T) {
r := int16(0)

p1 := td.makeProposal(t, h, r)
trx := tx.NewTransferTx(h, td.valKeys[0].Address(),
td.valKeys[1].Address(), 1000, 1000, "invalid proposal")
td.HelperSignTransaction(td.valKeys[0].PrivateKey(), trx)
trx := tx.NewTransferTx(h, td.consX.rewardAddr,
td.RandAccAddress(), 1000, 1000, "invalid proposal")
td.HelperSignTransaction(td.consX.valKey.PrivateKey(), trx)

assert.NoError(t, td.txPool.AppendTx(trx))
p2 := td.makeProposal(t, h, r)
Expand Down
2 changes: 1 addition & 1 deletion consensus/propose.go
Expand Up @@ -29,7 +29,7 @@ func (s *proposeState) decide() {
}

func (s *proposeState) createProposal(height uint32, round int16) {
block, err := s.state.ProposeBlock(s.valKey, s.rewardAddr, round)
block, err := s.state.ProposeBlock(s.valKey, s.rewardAddr)
if err != nil {
s.logger.Error("unable to propose a block!", "error", err)
return
Expand Down