diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index ef9f046f0..ad64ab939 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -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 @@ -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) @@ -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) @@ -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()) @@ -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) diff --git a/consensus/cp_test.go b/consensus/cp_test.go index 673cb4f15..63e17ae46 100644 --- a/consensus/cp_test.go +++ b/consensus/cp_test.go @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ diff --git a/consensus/log/log_test.go b/consensus/log/log_test.go index ebe5cd703..5029c859b 100644 --- a/consensus/log/log_test.go +++ b/consensus/log/log_test.go @@ -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()) @@ -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) diff --git a/consensus/manager_test.go b/consensus/manager_test.go index 306c3f0f2..8bed91693 100644 --- a/consensus/manager_test.go +++ b/consensus/manager_test.go @@ -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) @@ -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) @@ -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) diff --git a/consensus/precommit_test.go b/consensus/precommit_test.go index 74ad88286..fe36e1474 100644 --- a/consensus/precommit_test.go +++ b/consensus/precommit_test.go @@ -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) diff --git a/consensus/propose.go b/consensus/propose.go index 95a5f13f6..615f2b6d5 100644 --- a/consensus/propose.go +++ b/consensus/propose.go @@ -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 diff --git a/consensus/propose_test.go b/consensus/propose_test.go index 68a89d811..cc7cd48ce 100644 --- a/consensus/propose_test.go +++ b/consensus/propose_test.go @@ -22,14 +22,14 @@ func TestSetProposalInvalidProposer(t *testing.T) { td.enterNewHeight(td.consY) assert.Nil(t, td.consY.Proposal()) - addr := td.valKeys[tIndexB].Address() + addr := td.consB.valKey.Address() blk, _ := td.GenerateTestBlockWithProposer(1, addr) invalidProp := proposal.NewProposal(1, 0, blk) td.consY.SetProposal(invalidProp) assert.Nil(t, td.consY.Proposal()) - td.HelperSignProposal(td.valKeys[tIndexB], invalidProp) + td.HelperSignProposal(td.consB.valKey, invalidProp) td.consY.SetProposal(invalidProp) assert.Nil(t, td.consY.Proposal()) } @@ -37,10 +37,10 @@ func TestSetProposalInvalidProposer(t *testing.T) { func TestSetProposalInvalidBlock(t *testing.T) { td := setup(t) - addr := td.valKeys[tIndexB].Address() + addr := td.consB.valKey.Address() blk, _ := td.GenerateTestBlockWithProposer(1, addr) invProp := proposal.NewProposal(1, 2, blk) - td.HelperSignProposal(td.valKeys[tIndexB], invProp) + td.HelperSignProposal(td.consB.valKey, invProp) td.enterNewHeight(td.consP) td.enterNextRound(td.consP) @@ -53,10 +53,10 @@ func TestSetProposalInvalidBlock(t *testing.T) { func TestSetProposalInvalidHeight(t *testing.T) { td := setup(t) - addr := td.valKeys[tIndexB].Address() + addr := td.consB.valKey.Address() blk, _ := td.GenerateTestBlockWithProposer(2, addr) invProp := proposal.NewProposal(2, 0, blk) - td.HelperSignProposal(td.valKeys[tIndexB], invProp) + td.HelperSignProposal(td.consB.valKey, invProp) td.enterNewHeight(td.consY) td.consY.SetProposal(invProp) @@ -94,10 +94,10 @@ func TestProposalNextRound(t *testing.T) { td.enterNewHeight(td.consX) // Byzantine node sends proposal for the second round (his turn) even before the first round is started - b, err := td.consB.state.ProposeBlock(td.consB.valKey, td.consB.rewardAddr, 1) + b, err := td.consB.state.ProposeBlock(td.consB.valKey, td.consB.rewardAddr) assert.NoError(t, err) p := proposal.NewProposal(2, 1, b) - td.HelperSignProposal(td.valKeys[tIndexB], p) + td.HelperSignProposal(td.consB.valKey, p) td.consX.SetProposal(p) diff --git a/execution/execution_test.go b/execution/execution_test.go index 10c6ff22c..ed36ccd1a 100644 --- a/execution/execution_test.go +++ b/execution/execution_test.go @@ -17,49 +17,52 @@ func TestLockTime(t *testing.T) { sb := sandbox.MockingSandbox(ts) sb.TestAcceptSortition = true exe := NewExecutor() - rndValKey := ts.RandValKey() - addr1 := rndValKey.Address() - acc1 := sb.MakeNewAccount(addr1) - acc1.AddToBalance(100 * 1e9) - sb.UpdateAccount(addr1, acc1) - rcvAddr := ts.RandAccAddress() + rndPubKey, rndPrvKey := ts.RandBLSKeyPair() + rndAccAddr := rndPubKey.AccountAddress() + rndAcc := sb.MakeNewAccount(rndAccAddr) + rndAcc.AddToBalance(100 * 1e9) + sb.UpdateAccount(rndAccAddr, rndAcc) + rndValAddr := rndPubKey.ValidatorAddress() + rndVal := sb.MakeNewValidator(rndPubKey) + rndVal.AddToStake(100 * 1e9) + sb.UpdateValidator(rndVal) _ = sb.TestStore.AddTestBlock(8642) t.Run("Future LockTime, Should returns error (+1)", func(t *testing.T) { lockTime := sb.CurrentHeight() + 1 - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1000, "future-lockTime") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000, "future-lockTime") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.ErrorIs(t, err, FutureLockTimeError{LockTime: lockTime}) }) t.Run("Past LockTime, Should returns error (-8641)", func(t *testing.T) { lockTime := sb.CurrentHeight() - sb.TestParams.TransactionToLiveInterval - 1 - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1000, "past-lockTime") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000, "past-lockTime") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.ErrorIs(t, err, PastLockTimeError{LockTime: lockTime}) }) t.Run("Transaction has valid LockTime (-8640)", func(t *testing.T) { lockTime := sb.CurrentHeight() - sb.TestParams.TransactionToLiveInterval - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1000, "ok") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000, "ok") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.NoError(t, err) }) t.Run("Transaction has valid LockTime (0)", func(t *testing.T) { lockTime := sb.CurrentHeight() - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1000, "ok") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1000, "ok") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.NoError(t, err) }) t.Run("Subsidy transaction has invalid LockTime (+1)", func(t *testing.T) { lockTime := sb.CurrentHeight() + 1 - trx := tx.NewSubsidyTx(lockTime, rcvAddr, 1000, + trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000, "invalid-lockTime") err := exe.Execute(trx, sb) assert.ErrorIs(t, err, FutureLockTimeError{LockTime: lockTime}) @@ -67,7 +70,7 @@ func TestLockTime(t *testing.T) { t.Run("Subsidy transaction has invalid LockTime (-1)", func(t *testing.T) { lockTime := sb.CurrentHeight() - 1 - trx := tx.NewSubsidyTx(lockTime, rcvAddr, 1000, + trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000, "invalid-lockTime") err := exe.Execute(trx, sb) assert.ErrorIs(t, err, PastLockTimeError{LockTime: lockTime}) @@ -75,7 +78,7 @@ func TestLockTime(t *testing.T) { t.Run("Subsidy transaction has valid LockTime (0)", func(t *testing.T) { lockTime := sb.CurrentHeight() - trx := tx.NewSubsidyTx(lockTime, rcvAddr, 1000, "ok") + trx := tx.NewSubsidyTx(lockTime, ts.RandAccAddress(), 1000, "ok") err := exe.Execute(trx, sb) assert.NoError(t, err) }) @@ -83,8 +86,8 @@ func TestLockTime(t *testing.T) { t.Run("Sortition transaction has invalid LockTime (+1)", func(t *testing.T) { lockTime := sb.CurrentHeight() + 1 proof := ts.RandProof() - trx := tx.NewSortitionTx(lockTime, addr1, proof) - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewSortitionTx(lockTime, rndValAddr, proof) + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.ErrorIs(t, err, FutureLockTimeError{LockTime: lockTime}) }) @@ -92,8 +95,8 @@ func TestLockTime(t *testing.T) { t.Run("Sortition transaction has invalid LockTime (-8)", func(t *testing.T) { lockTime := sb.CurrentHeight() - sb.TestParams.SortitionInterval - 1 proof := ts.RandProof() - trx := tx.NewSortitionTx(lockTime, addr1, proof) - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewSortitionTx(lockTime, rndValAddr, proof) + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.ErrorIs(t, err, PastLockTimeError{LockTime: lockTime}) }) @@ -101,12 +104,9 @@ func TestLockTime(t *testing.T) { t.Run("Sortition transaction has valid LockTime (-7)", func(t *testing.T) { lockTime := sb.CurrentHeight() - sb.TestParams.SortitionInterval proof := ts.RandProof() - val1 := sb.MakeNewValidator(rndValKey.PublicKey()) - val1.AddToStake(100 * 1e9) - sb.UpdateValidator(val1) - trx := tx.NewSortitionTx(lockTime, addr1, proof) - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewSortitionTx(lockTime, rndValAddr, proof) + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.NoError(t, err) }) @@ -118,44 +118,44 @@ func TestExecution(t *testing.T) { sb := sandbox.MockingSandbox(ts) exe := NewExecutor() - rndValKey := ts.RandValKey() - addr1 := rndValKey.Address() - acc1 := sb.MakeNewAccount(addr1) - acc1.AddToBalance(100 * 1e9) - sb.UpdateAccount(addr1, acc1) - rcvAddr := ts.RandAccAddress() + rndPubKey, rndPrvKey := ts.RandBLSKeyPair() + rndAccAddr := rndPubKey.AccountAddress() + rndValAddr := rndPubKey.ValidatorAddress() + rndAcc := sb.MakeNewAccount(rndAccAddr) + rndAcc.AddToBalance(100 * 1e9) + sb.UpdateAccount(rndAccAddr, rndAcc) _ = sb.TestStore.AddTestBlock(8642) lockTime := sb.CurrentHeight() t.Run("Invalid transaction, Should returns error", func(t *testing.T) { - trx := tx.NewTransferTx(lockTime, ts.RandAccAddress(), rcvAddr, 1000, 1000, "invalid-tx") + trx := tx.NewTransferTx(lockTime, ts.RandAccAddress(), ts.RandAccAddress(), 1000, 1000, "invalid-tx") err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidAddress) }) t.Run("Invalid fee, Should returns error", func(t *testing.T) { - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1, "invalid fee") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1, "invalid fee") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidFee) }) t.Run("Invalid fee, Should returns error", func(t *testing.T) { - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 1001, "invalid fee") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 1001, "invalid fee") + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidFee) }) t.Run("Invalid fee (subsidy tx), Should returns error", func(t *testing.T) { - trx := tx.NewTransferTx(lockTime, crypto.TreasuryAddress, rcvAddr, 1000, 1, "invalid fee") + trx := tx.NewTransferTx(lockTime, crypto.TreasuryAddress, ts.RandAccAddress(), 1000, 1, "invalid fee") err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidFee) assert.Error(t, exe.checkFee(trx, sb)) }) t.Run("Invalid fee (send tx), Should returns error", func(t *testing.T) { - trx := tx.NewTransferTx(lockTime, addr1, rcvAddr, 1000, 0, "invalid fee") + trx := tx.NewTransferTx(lockTime, rndAccAddr, ts.RandAccAddress(), 1000, 0, "invalid fee") err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidFee) assert.Error(t, exe.checkFee(trx, sb)) @@ -163,8 +163,8 @@ func TestExecution(t *testing.T) { t.Run("Execution failed", func(t *testing.T) { proof := ts.RandProof() - trx := tx.NewSortitionTx(lockTime, addr1, proof) - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + trx := tx.NewSortitionTx(lockTime, rndValAddr, proof) + ts.HelperSignTransaction(rndPrvKey, trx) err := exe.Execute(trx, sb) assert.Equal(t, errors.Code(err), errors.ErrInvalidAddress) }) @@ -175,16 +175,16 @@ func TestReplay(t *testing.T) { executor := NewExecutor() sb := sandbox.MockingSandbox(ts) - rndValKey := ts.RandValKey() - addr1 := rndValKey.Address() - acc := sb.MakeNewAccount(addr1) - acc.AddToBalance(1e9) - sb.UpdateAccount(addr1, acc) + rndPubKey, rndPrvKey := ts.RandBLSKeyPair() + rndAccAddr := rndPubKey.AccountAddress() + rndAcc := sb.MakeNewAccount(rndAccAddr) + rndAcc.AddToBalance(1e9) + sb.UpdateAccount(rndAccAddr, rndAcc) lockTime := sb.CurrentHeight() trx := tx.NewTransferTx(lockTime, - addr1, ts.RandAccAddress(), 10000, 1000, "") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + rndAccAddr, ts.RandAccAddress(), 10000, 1000, "") + ts.HelperSignTransaction(rndPrvKey, trx) err := executor.Execute(trx, sb) assert.NoError(t, err) @@ -200,16 +200,16 @@ func TestChecker(t *testing.T) { executor := NewExecutor() checker := NewChecker() sb := sandbox.MockingSandbox(ts) - rndValKey := ts.RandValKey() - addr1 := rndValKey.Address() - acc := sb.MakeNewAccount(addr1) - acc.AddToBalance(1e9) - sb.UpdateAccount(addr1, acc) + rndPubKey, rndPrvKey := ts.RandBLSKeyPair() + rndAccAddr := rndPubKey.AccountAddress() + rndAcc := sb.MakeNewAccount(rndAccAddr) + rndAcc.AddToBalance(1e9) + sb.UpdateAccount(rndAccAddr, rndAcc) lockTime := sb.CurrentHeight() + 1 trx := tx.NewTransferTx(lockTime, - addr1, ts.RandAccAddress(), 10000, 1000, "") - ts.HelperSignTransaction(rndValKey.PrivateKey(), trx) + rndAccAddr, ts.RandAccAddress(), 10000, 1000, "") + ts.HelperSignTransaction(rndPrvKey, trx) err := executor.Execute(trx, sb) assert.ErrorIs(t, err, FutureLockTimeError{LockTime: lockTime}) diff --git a/state/execution_test.go b/state/execution_test.go index 50bfa2a05..8472877c7 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -29,10 +29,10 @@ func TestProposeBlock(t *testing.T) { invSortitionTx, _ := td.GenerateTestSortitionTx() pub, _ := td.RandBLSKeyPair() - trx1 := tx.NewTransferTx(1, td.valKey1.Address(), td.valKey1.Address(), 1, 1000, "") + trx1 := tx.NewTransferTx(1, td.valKey1.PublicKey().AccountAddress(), td.RandAccAddress(), 1, 1000, "") td.HelperSignTransaction(td.valKey1.PrivateKey(), trx1) - trx2 := tx.NewBondTx(2, td.valKey1.Address(), pub.ValidatorAddress(), pub, 1000000000, 100000, "") + trx2 := tx.NewBondTx(2, td.valKey1.PublicKey().AccountAddress(), pub.ValidatorAddress(), pub, 1000000000, 100000, "") td.HelperSignTransaction(td.valKey1.PrivateKey(), trx2) assert.NoError(t, td.state1.txPool.AppendTx(invTransferTx)) @@ -64,7 +64,7 @@ func TestExecuteBlock(t *testing.T) { validSubsidyTx := td.state1.createSubsidyTx(rewardAddr, 1000) invTransferTx, _ := td.GenerateTestTransferTx() - validTx1 := tx.NewTransferTx(1, td.valKey1.Address(), td.valKey1.Address(), 1, 1000, "") + validTx1 := tx.NewTransferTx(1, td.valKey1.PublicKey().AccountAddress(), td.RandAccAddress(), 1, 1000, "") td.HelperSignTransaction(td.valKey1.PrivateKey(), validTx1) assert.NoError(t, td.state1.txPool.AppendTx(invTransferTx)) diff --git a/state/facade.go b/state/facade.go index b8bd7d5fa..ecc09f7d1 100644 --- a/state/facade.go +++ b/state/facade.go @@ -25,7 +25,7 @@ type Facade interface { LastBlockTime() time.Time LastCertificate() *certificate.Certificate UpdateLastCertificate(v *vote.Vote) error - ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Address, round int16) (*block.Block, error) + ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Address) (*block.Block, error) ValidateBlock(blk *block.Block) error CommitBlock(blk *block.Block, cert *certificate.Certificate) error CommitteeValidators() []*validator.Validator diff --git a/state/mock.go b/state/mock.go index 6ec99a239..7a49d27ef 100644 --- a/state/mock.go +++ b/state/mock.go @@ -114,7 +114,7 @@ func (m *MockState) Close() error { return nil } -func (m *MockState) ProposeBlock(valKey *bls.ValidatorKey, _ crypto.Address, _ int16) (*block.Block, error) { +func (m *MockState) ProposeBlock(valKey *bls.ValidatorKey, _ crypto.Address) (*block.Block, error) { blk, _ := m.ts.GenerateTestBlockWithProposer(m.TestStore.LastHeight, valKey.Address()) return blk, nil } diff --git a/state/state.go b/state/state.go index da8cd4635..f3ec11456 100644 --- a/state/state.go +++ b/state/state.go @@ -32,6 +32,8 @@ import ( "github.com/pactus-project/pactus/www/nanomsg/event" ) +var maxTransactionsPerBlock = 1000 + type state struct { lk sync.RWMutex @@ -304,14 +306,10 @@ func (st *state) createSubsidyTx(rewardAddr crypto.Address, fee int64) *tx.Tx { return transaction } -func (st *state) ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Address, round int16) (*block.Block, error) { +func (st *state) ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Address) (*block.Block, error) { st.lk.Lock() defer st.lk.Unlock() - if !st.committee.IsProposer(valKey.Address(), round) { - return nil, errors.Errorf(errors.ErrGeneric, "we are not proposer for this round") - } - // Create new sandbox and execute transactions sb := st.concreteSandbox() exe := execution.NewExecutor() @@ -333,9 +331,9 @@ func (st *state) ProposeBlock(valKey *bls.ValidatorKey, rewardAddr crypto.Addres txs.Remove(i) i-- } - // Maximum 1000 transactions per block - if txs.Len() >= 1000 { - break + if i >= maxTransactionsPerBlock-1 { + txs.Remove(i) + i-- } } diff --git a/state/state_test.go b/state/state_test.go index 59543e0fb..a0c8297f4 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -39,6 +39,8 @@ type testData struct { valKey2 *bls.ValidatorKey valKey3 *bls.ValidatorKey valKey4 *bls.ValidatorKey + genAccAddr crypto.Address + genAccKey *bls.PrivateKey commonTxPool *txpool.MockTxPool } @@ -51,6 +53,7 @@ func setup(t *testing.T) *testData { pub2, prv2 := ts.RandBLSKeyPair() pub3, prv3 := ts.RandBLSKeyPair() pub4, prv4 := ts.RandBLSKeyPair() + pub5, prv5 := ts.RandBLSKeyPair() valKey1 := bls.NewValidatorKey(prv1) valKey2 := bls.NewValidatorKey(prv2) @@ -77,10 +80,11 @@ func setup(t *testing.T) *testData { acc1.AddToBalance(21 * 1e15) // 21,000,000,000,000,000 acc2 := account.NewAccount(1) acc2.AddToBalance(21 * 1e15) // 21,000,000,000,000,000 + genAccAddr := pub5.AccountAddress() accs := map[crypto.Address]*account.Account{ crypto.TreasuryAddress: acc1, - ts.RandAccAddress(): acc2, + genAccAddr: acc2, } vals := []*validator.Validator{val1, val2, val3, val4} gnDoc := genesis.MakeGenesis(genTime, accs, vals, params) @@ -109,6 +113,8 @@ func setup(t *testing.T) *testData { valKey2: valKey2, valKey3: valKey3, valKey4: valKey4, + genAccKey: prv5, + genAccAddr: genAccAddr, commonTxPool: commonTxPool, } } @@ -129,8 +135,9 @@ func (td *testData) makeBlockAndCertificate(t *testing.T, round int16, st = td.state4 } - rewardAddr := st.valKeys[0].Address() - b, err := st.ProposeBlock(st.valKeys[0], rewardAddr, round) + rewardAddr := st.valKeys[0].PublicKey().AccountAddress() + require.True(t, rewardAddr.IsAccountAddress()) + b, err := st.ProposeBlock(st.valKeys[0], rewardAddr) require.NoError(t, err) c := td.makeCertificateAndSign(t, b.Hash(), round, valKeys...) @@ -195,23 +202,19 @@ func TestProposeBlockAndValidation(t *testing.T) { td.moveToNextHeightForAllStates(t) - b1, err := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) - assert.Error(t, err, "Should not propose") - assert.Nil(t, b1) - - trx := tx.NewTransferTx(td.state1.lastInfo.BlockHeight()+1, td.valKey1.Address(), - td.valKey2.Address(), 1000, 1000, "") + trx := tx.NewTransferTx(td.state1.lastInfo.BlockHeight()+1, td.valKey1.PublicKey().AccountAddress(), + td.RandAccAddress(), 1000, 1000, "") td.HelperSignTransaction(td.valKey1.PrivateKey(), trx) assert.NoError(t, td.commonTxPool.AppendTx(trx)) - b2, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 0) + b2, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress()) assert.NoError(t, err) assert.NotNil(t, b2) assert.Equal(t, b2.Transactions().Len(), 2) require.NoError(t, td.state1.ValidateBlock(b2)) // Propose and validate again - b3, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 0) + b3, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress()) assert.NoError(t, err) assert.NotNil(t, b3) assert.Equal(t, b3.Transactions().Len(), 2) @@ -386,22 +389,13 @@ func TestUpdateLastCertificate(t *testing.T) { } } -func TestInvalidProposerProposeBlock(t *testing.T) { - td := setup(t) - - _, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 0) - assert.Error(t, err, "Should not propose") - _, err = td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 1) - assert.NoError(t, err, "Should propose") -} - func TestBlockProposal(t *testing.T) { td := setup(t) td.moveToNextHeightForAllStates(t) t.Run("validity of proposed block", func(t *testing.T) { - b, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 0) + b, err := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress()) assert.NoError(t, err) assert.NoError(t, td.state1.ValidateBlock(b)) }) @@ -411,7 +405,7 @@ func TestBlockProposal(t *testing.T) { assert.NoError(t, td.state3.txPool.AppendTx(trx)) // Moving to the next round - b, err := td.state3.ProposeBlock(td.state3.valKeys[0], td.RandAccAddress(), 1) + b, err := td.state3.ProposeBlock(td.state3.valKeys[0], td.RandAccAddress()) assert.NoError(t, err) assert.NoError(t, td.state1.ValidateBlock(b)) assert.Equal(t, b.Transactions().Len(), 1) @@ -467,7 +461,7 @@ func TestSortition(t *testing.T) { height := uint32(1) for ; height <= 15; height++ { if height == 6 { - trx := tx.NewBondTx(1, td.valKey1.Address(), + trx := tx.NewBondTx(1, td.valKey1.PublicKey().AccountAddress(), pub.ValidatorAddress(), pub, 1000000000, 100000, "") td.HelperSignTransaction(td.valKey1.PrivateKey(), trx) @@ -510,7 +504,7 @@ func TestSortition(t *testing.T) { // Let's commit another block with the new committee height++ - b14, err := stNew.ProposeBlock(stNew.valKeys[0], td.RandAccAddress(), 3) + b14, err := stNew.ProposeBlock(stNew.valKeys[0], td.RandAccAddress()) require.NoError(t, err) require.NotNil(t, b14) @@ -601,7 +595,7 @@ func TestInvalidBlockVersion(t *testing.T) { td := setup(t) td.state1.params.BlockVersion = 2 - b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) + b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) assert.Error(t, td.state2.ValidateBlock(b)) } @@ -664,9 +658,9 @@ func TestLoadState(t *testing.T) { // Add a bond transactions to change total power (stake) pub, _ := td.RandBLSKeyPair() - tx2 := tx.NewBondTx(1, td.valKey1.Address(), + tx2 := tx.NewBondTx(1, td.genAccAddr, pub.ValidatorAddress(), pub, 8888000, 8888, "") - td.HelperSignTransaction(td.valKey1.PrivateKey(), tx2) + td.HelperSignTransaction(td.genAccKey, tx2) assert.NoError(t, td.commonTxPool.AppendTx(tx2)) @@ -727,7 +721,7 @@ func TestSetBlockTime(t *testing.T) { t.Run("Last block time is a bit far in past", func(t *testing.T) { td.state1.lastInfo.UpdateBlockTime(util.RoundNow(10).Add(-20 * time.Second)) - b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) + b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) fmt.Printf("last block time: %s\nproposed time : %s\n", td.state1.lastInfo.BlockTime(), b.Header().Time().UTC()) assert.True(t, b.Header().Time().After(td.state1.lastInfo.BlockTime())) assert.True(t, b.Header().Time().Before(util.Now().Add(10*time.Second))) @@ -736,7 +730,7 @@ func TestSetBlockTime(t *testing.T) { t.Run("Last block time is almost good", func(t *testing.T) { td.state1.lastInfo.UpdateBlockTime(util.RoundNow(10).Add(-10 * time.Second)) - b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) + b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) fmt.Printf("last block time: %s\nproposed time : %s\n", td.state1.lastInfo.BlockTime(), b.Header().Time().UTC()) assert.True(t, b.Header().Time().After(td.state1.lastInfo.BlockTime())) assert.True(t, b.Header().Time().Before(util.Now().Add(10*time.Second))) @@ -746,7 +740,7 @@ func TestSetBlockTime(t *testing.T) { // After our time t.Run("Last block time is in near future", func(t *testing.T) { td.state1.lastInfo.UpdateBlockTime(util.RoundNow(10).Add(+10 * time.Second)) - b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) + b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) fmt.Printf("last block time: %s\nproposed time : %s\n", td.state1.lastInfo.BlockTime(), b.Header().Time().UTC()) assert.True(t, b.Header().Time().After(td.state1.lastInfo.BlockTime())) assert.Zero(t, b.Header().Time().Second()%10) @@ -754,7 +748,7 @@ func TestSetBlockTime(t *testing.T) { t.Run("Last block time is more than a block in future", func(t *testing.T) { td.state1.lastInfo.UpdateBlockTime(util.RoundNow(10).Add(+20 * time.Second)) - b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress(), 0) + b, _ := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) fmt.Printf("last block time: %s\nproposed time : %s\n", td.state1.lastInfo.BlockTime(), b.Header().Time().UTC()) assert.True(t, b.Header().Time().After(td.state1.lastInfo.BlockTime())) assert.Zero(t, b.Header().Time().Second()%10) @@ -803,6 +797,7 @@ func TestCommittingInvalidBlock(t *testing.T) { func TestCalcFee(t *testing.T) { td := setup(t) + tests := []struct { amount int64 pldType payload.Type @@ -834,3 +829,22 @@ func TestCalcFee(t *testing.T) { assert.Error(t, err) } } + +func TestCheckMaximumTransactionPerBlock(t *testing.T) { + td := setup(t) + + td.moveToNextHeightForAllStates(t) + + maxTransactionsPerBlock = 10 + for i := 0; i < maxTransactionsPerBlock+2; i++ { + amt := td.RandInt64(1e6) + fee, _ := td.state1.CalculateFee(amt, payload.TypeTransfer) + trx := tx.NewTransferTx(1, td.genAccAddr, td.RandAccAddress(), amt, fee, "") + err := td.commonTxPool.AppendTx(trx) + assert.NoError(t, err) + } + + blk, err := td.state1.ProposeBlock(td.state1.valKeys[0], td.RandAccAddress()) + assert.NoError(t, err) + assert.Equal(t, maxTransactionsPerBlock, blk.Transactions().Len()) +} diff --git a/state/validation_test.go b/state/validation_test.go index 0b8e43698..04dfb1f22 100644 --- a/state/validation_test.go +++ b/state/validation_test.go @@ -31,7 +31,7 @@ func TestCertificateValidation(t *testing.T) { td.state1.store.UpdateValidator(val5) td.state2.store.UpdateValidator(val5) - nextBlock, _ := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress(), 0) + nextBlock, _ := td.state2.ProposeBlock(td.state2.valKeys[0], td.RandAccAddress()) nextBlockHash := nextBlock.Hash() height := uint32(6) round := int16(0) diff --git a/sync/bundle/message/block_announce_test.go b/sync/bundle/message/block_announce_test.go index f34426d7c..9c1b9ab21 100644 --- a/sync/bundle/message/block_announce_test.go +++ b/sync/bundle/message/block_announce_test.go @@ -18,7 +18,8 @@ func TestBlockAnnounceMessage(t *testing.T) { ts := testsuite.NewTestSuite(t) t.Run("Invalid certificate", func(t *testing.T) { - blk, cert := ts.GenerateTestBlock(0) + blk, _ := ts.GenerateTestBlock(ts.RandHeight()) + cert := certificate.NewCertificate(0, 0, nil, nil, nil) m := NewBlockAnnounceMessage(blk, cert) err := m.BasicCheck() diff --git a/sync/bundle/message/blocks_response_test.go b/sync/bundle/message/blocks_response_test.go index a28c17181..b8eb43e66 100644 --- a/sync/bundle/message/blocks_response_test.go +++ b/sync/bundle/message/blocks_response_test.go @@ -18,7 +18,8 @@ func TestBlocksResponseMessage(t *testing.T) { sid := 123 t.Run("Invalid certificate", func(t *testing.T) { - blk, cert := ts.GenerateTestBlock(0) + blk, _ := ts.GenerateTestBlock(ts.RandHeight()) + cert := certificate.NewCertificate(0, 0, nil, nil, nil) d, _ := blk.Bytes() m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, ResponseCodeMoreBlocks.String(), sid, ts.RandHeight(), [][]byte{d}, cert) diff --git a/sync/bundle/message/proposal_test.go b/sync/bundle/message/proposal_test.go index 676f90f3d..2a0f3e9cd 100644 --- a/sync/bundle/message/proposal_test.go +++ b/sync/bundle/message/proposal_test.go @@ -3,6 +3,7 @@ package message import ( "testing" + "github.com/pactus-project/pactus/types/proposal" "github.com/pactus-project/pactus/util/errors" "github.com/pactus-project/pactus/util/testsuite" "github.com/stretchr/testify/assert" @@ -17,10 +18,10 @@ func TestProposalMessage(t *testing.T) { ts := testsuite.NewTestSuite(t) t.Run("Invalid proposal", func(t *testing.T) { - proposal, _ := ts.GenerateTestProposal(100, -1) - m := NewProposalMessage(proposal) + prop := proposal.NewProposal(0, 0, nil) + m := NewProposalMessage(prop) - assert.Equal(t, errors.Code(m.BasicCheck()), errors.ErrInvalidRound) + assert.Equal(t, errors.Code(m.BasicCheck()), errors.ErrInvalidBlock) }) t.Run("OK", func(t *testing.T) { diff --git a/sync/bundle/message/vote_test.go b/sync/bundle/message/vote_test.go index 2a10c6738..9c5441dd1 100644 --- a/sync/bundle/message/vote_test.go +++ b/sync/bundle/message/vote_test.go @@ -17,7 +17,7 @@ func TestVoteMessage(t *testing.T) { ts := testsuite.NewTestSuite(t) t.Run("Invalid vote", func(t *testing.T) { - v, _ := ts.GenerateTestPrepareVote(100, -1) + v := vote.NewPrepareVote(ts.RandHash(), ts.RandHeight(), -1, ts.RandValAddress()) m := NewVoteMessage(v) assert.ErrorIs(t, m.BasicCheck(), vote.BasicCheckError{Reason: "invalid round"}) diff --git a/sync/handler_blocks_response_test.go b/sync/handler_blocks_response_test.go index e82fbd489..6a0bdbfe0 100644 --- a/sync/handler_blocks_response_test.go +++ b/sync/handler_blocks_response_test.go @@ -26,6 +26,8 @@ import ( func TestInvalidBlockData(t *testing.T) { td := setup(t, nil) + td.state.CommitTestBlocks(10) + lastHeight := td.state.LastBlockHeight() prevCert := td.GenerateTestCertificate(lastHeight) cert := td.GenerateTestCertificate(lastHeight + 1) @@ -82,6 +84,8 @@ func TestOneBlockShorter(t *testing.T) { func TestStrippedPublicKey(t *testing.T) { td := setup(t, nil) + td.state.CommitTestBlocks(10) + lastHeight := td.state.LastBlockHeight() // Add a new block and keep the signer key diff --git a/txpool/pool_test.go b/txpool/pool_test.go index e01727d25..f4aa5d2d6 100644 --- a/txpool/pool_test.go +++ b/txpool/pool_test.go @@ -134,50 +134,48 @@ func TestPrepareBlockTransactions(t *testing.T) { randHeight := td.RandHeight() + td.sandbox.TestParams.UnbondInterval _ = td.sandbox.TestStore.AddTestBlock(randHeight) - acc1ValKey := td.RandValKey() + acc1PubKey, acc1PrvKey := td.RandBLSKeyPair() + acc1Addr := acc1PubKey.AccountAddress() acc1 := account.NewAccount(0) acc1.AddToBalance(10000000000) - td.sandbox.UpdateAccount(acc1ValKey.Address(), acc1) + td.sandbox.UpdateAccount(acc1Addr, acc1) - valKey1 := td.RandValKey() - val1Pub := valKey1.PublicKey() - val1 := validator.NewValidator(val1Pub, 0) + val1PubKey, val1PrvKey := td.RandBLSKeyPair() + val1 := validator.NewValidator(val1PubKey, 0) val1.AddToStake(10000000000) td.sandbox.UpdateValidator(val1) - valKey2 := td.RandValKey() - val2Pub := valKey2.PublicKey() - val2 := validator.NewValidator(val2Pub, 0) + val2PubKey, val2PrvKey := td.RandBLSKeyPair() + val2 := validator.NewValidator(val2PubKey, 0) val2.AddToStake(10000000000) val2.UpdateUnbondingHeight(1) td.sandbox.UpdateValidator(val2) - valKey3 := td.RandValKey() - val3Pub := valKey3.PublicKey() - val3 := validator.NewValidator(val3Pub, 0) + val3PubKey, val3PrvKey := td.RandBLSKeyPair() + val3 := validator.NewValidator(val3PubKey, 0) val3.AddToStake(10000000000) td.sandbox.UpdateValidator(val3) - transferTx := tx.NewTransferTx(randHeight+1, acc1ValKey.Address(), + transferTx := tx.NewTransferTx(randHeight+1, acc1Addr, td.RandAccAddress(), 1000, 1000, "send-tx") - td.HelperSignTransaction(acc1ValKey.PrivateKey(), transferTx) + td.HelperSignTransaction(acc1PrvKey, transferTx) pub, _ := td.RandBLSKeyPair() - bondTx := tx.NewBondTx(randHeight+2, acc1ValKey.Address(), + bondTx := tx.NewBondTx(randHeight+2, acc1Addr, pub.ValidatorAddress(), pub, 1000000000, 100000, "bond-tx") - td.HelperSignTransaction(acc1ValKey.PrivateKey(), bondTx) + td.HelperSignTransaction(acc1PrvKey, bondTx) unbondTx := tx.NewUnbondTx(randHeight+3, val1.Address(), "unbond-tx") - td.HelperSignTransaction(valKey1.PrivateKey(), unbondTx) + td.HelperSignTransaction(val1PrvKey, unbondTx) withdrawTx := tx.NewWithdrawTx(randHeight+4, val2.Address(), td.RandAccAddress(), 1000, 1000, "withdraw-tx") - td.HelperSignTransaction(valKey2.PrivateKey(), withdrawTx) + td.HelperSignTransaction(val2PrvKey, withdrawTx) td.sandbox.TestAcceptSortition = true sortitionTx := tx.NewSortitionTx(randHeight, val3.Address(), td.RandProof()) - td.HelperSignTransaction(valKey3.PrivateKey(), sortitionTx) + td.HelperSignTransaction(val3PrvKey, sortitionTx) assert.NoError(t, td.pool.AppendTx(transferTx)) assert.NoError(t, td.pool.AppendTx(unbondTx)) diff --git a/types/proposal/proposal.go b/types/proposal/proposal.go index 5b10938c4..a730c70ca 100644 --- a/types/proposal/proposal.go +++ b/types/proposal/proposal.go @@ -50,7 +50,7 @@ func (p *Proposal) Signature() crypto.Signature { func (p *Proposal) BasicCheck() error { if p.data.Block == nil { - return errors.Errorf(errors.ErrInvalidSignature, "no block") + return errors.Errorf(errors.ErrInvalidBlock, "no block") } if p.data.Signature == nil { return errors.Errorf(errors.ErrInvalidSignature, "no signature") diff --git a/types/proposal/proposal_test.go b/types/proposal/proposal_test.go index 8c4191e50..241cd2958 100644 --- a/types/proposal/proposal_test.go +++ b/types/proposal/proposal_test.go @@ -65,12 +65,16 @@ func TestBasicCheck(t *testing.T) { }) t.Run("Invalid height", func(t *testing.T) { - p, _ := ts.GenerateTestProposal(0, 0) + blk, _ := ts.GenerateTestBlock(ts.RandHeight()) + p := proposal.NewProposal(0, 0, blk) + p.SetSignature(ts.RandBLSSignature()) assert.Error(t, p.BasicCheck()) }) t.Run("Invalid round", func(t *testing.T) { - p, _ := ts.GenerateTestProposal(1, -1) + blk, _ := ts.GenerateTestBlock(ts.RandHeight()) + p := proposal.NewProposal(ts.RandHeight(), -1, blk) + p.SetSignature(ts.RandBLSSignature()) assert.Error(t, p.BasicCheck()) }) diff --git a/types/tx/tx_test.go b/types/tx/tx_test.go index 7f6bebb53..416f2f957 100644 --- a/types/tx/tx_test.go +++ b/types/tx/tx_test.go @@ -167,7 +167,9 @@ func TestBasicCheck(t *testing.T) { valKey := ts.RandValKey() trx := tx.NewTransferTx(ts.RandHeight(), ts.RandAccAddress(), ts.RandAccAddress(), 1, 1, "invalid valKey") - ts.HelperSignTransaction(valKey.PrivateKey(), trx) + sig := valKey.PrivateKey().Sign(trx.SignBytes()) + trx.SetSignature(sig) + trx.SetPublicKey(valKey.PublicKey()) err := trx.BasicCheck() assert.ErrorIs(t, err, tx.BasicCheckError{ diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index 9fa6bcd7c..e26537d57 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -285,11 +285,13 @@ func (ts *TestSuite) generateTestBlock(height uint32, proposer crypto.Address, t txs.Append(tx4) txs.Append(tx5) - prevCert := ts.GenerateTestCertificate(height - 1) + var prevCert *certificate.Certificate prevBlockHash := ts.RandHash() if height == 1 { prevCert = nil prevBlockHash = hash.UndefHash + } else { + prevCert = ts.GenerateTestCertificate(height - 1) } blockCert := ts.GenerateTestCertificate(height) header := block.NewHeader(1, time, @@ -299,6 +301,11 @@ func (ts *TestSuite) generateTestBlock(height uint32, proposer crypto.Address, t proposer) blk := block.NewBlock(header, prevCert, txs) + + err := blk.BasicCheck() + if err != nil { + panic(err) + } return blk, blockCert } @@ -310,12 +317,18 @@ func (ts *TestSuite) GenerateTestCertificate(height uint32) *certificate.Certifi c2 := ts.RandInt32NonZero(10) + 10 c3 := ts.RandInt32NonZero(10) + 20 c4 := ts.RandInt32NonZero(10) + 30 - return certificate.NewCertificate( + cert := certificate.NewCertificate( height, ts.RandRound(), []int32{c1, c2, c3, c4}, []int32{c2}, sig) + + err := cert.BasicCheck() + if err != nil { + panic(err) + } + return cert } // GenerateTestProposal generates a proposal for testing purposes. @@ -424,15 +437,27 @@ func (ts *TestSuite) GenerateTestCommittee(num int) (committee.Committee, []*bls func (ts *TestSuite) HelperSignVote(valKey *bls.ValidatorKey, v *vote.Vote) { sig := valKey.Sign(v.SignBytes()) v.SetSignature(sig) + + if err := v.BasicCheck(); err != nil { + panic(err) + } } func (ts *TestSuite) HelperSignProposal(valKey *bls.ValidatorKey, p *proposal.Proposal) { sig := valKey.Sign(p.SignBytes()) p.SetSignature(sig) + + if err := p.BasicCheck(); err != nil { + panic(err) + } } func (ts *TestSuite) HelperSignTransaction(prv crypto.PrivateKey, trx *tx.Tx) { sig := prv.Sign(trx.SignBytes()) trx.SetSignature(sig) trx.SetPublicKey(prv.PublicKey()) + + if err := trx.BasicCheck(); err != nil { + panic(err) + } } diff --git a/www/http/http_test.go b/www/http/http_test.go index 72c01791b..f1f5cc8c8 100644 --- a/www/http/http_test.go +++ b/www/http/http_test.go @@ -42,6 +42,8 @@ func setup(t *testing.T) *testData { ts.RandValKey(), ts.RandValKey(), }) + mockConsMgr.MoveToNewHeight() + grpcConf := &grpc.Config{ Enable: true, Listen: "[::]:0",