Skip to content

Commit

Permalink
test: add transaction and block makers to testsuite (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre authored Jul 1, 2024
1 parent 22b04e7 commit 5727751
Show file tree
Hide file tree
Showing 30 changed files with 316 additions and 323 deletions.
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (cs *consensus) announceNewBlock(blk *block.Block, cert *certificate.BlockC

func (cs *consensus) makeBlockCertificate(votes map[crypto.Address]*vote.Vote,
) *certificate.BlockCertificate {
cert := certificate.NewBlockCertificate(cs.height, cs.round, false)
cert := certificate.NewBlockCertificate(cs.height, cs.round)
cert.SetSignature(cs.signersInfo(votes))

return cert
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (td *testData) commitBlockForAllStates(t *testing.T) (*block.Block, *certif
var err error
prop := td.makeProposal(t, height+1, 0)

cert := certificate.NewBlockCertificate(height+1, 0, false)
cert := certificate.NewBlockCertificate(height+1, 0)
sb := cert.SignBytes(prop.Block().Hash())
sig1 := td.consX.valKey.Sign(sb)
sig2 := td.consY.valKey.Sign(sb)
Expand Down
7 changes: 4 additions & 3 deletions consensus/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/pactus-project/pactus/types/proposal"
"github.com/pactus-project/pactus/types/vote"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,7 +24,7 @@ func TestSetProposalInvalidProposer(t *testing.T) {
assert.Nil(t, td.consY.Proposal())

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(1, addr)
blk, _ := td.GenerateTestBlock(1, testsuite.BlockWithProposer(addr))
invalidProp := proposal.NewProposal(1, 0, blk)

td.consY.SetProposal(invalidProp)
Expand All @@ -38,7 +39,7 @@ func TestSetProposalInvalidBlock(t *testing.T) {
td := setup(t)

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(1, addr)
blk, _ := td.GenerateTestBlock(1, testsuite.BlockWithProposer(addr))
invProp := proposal.NewProposal(1, 2, blk)
td.HelperSignProposal(td.consB.valKey, invProp)

Expand All @@ -54,7 +55,7 @@ func TestSetProposalInvalidHeight(t *testing.T) {
td := setup(t)

addr := td.consB.valKey.Address()
blk, _ := td.GenerateTestBlockWithProposer(2, addr)
blk, _ := td.GenerateTestBlock(2, testsuite.BlockWithProposer(addr))
invProp := proposal.NewProposal(2, 0, blk)
td.HelperSignProposal(td.consB.valKey, invProp)

Expand Down
4 changes: 2 additions & 2 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func TestAccountChange(t *testing.T) {
func TestAnyRecentTransaction(t *testing.T) {
td := setup(t)

randTx1, _ := td.GenerateTestTransferTx()
randTx2, _ := td.GenerateTestTransferTx()
randTx1 := td.GenerateTestTransferTx()
randTx2 := td.GenerateTestTransferTx()
td.sandbox.CommitTransaction(randTx1)
td.sandbox.CommitTransaction(randTx2)

Expand Down
8 changes: 4 additions & 4 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func TestProposeBlock(t *testing.T) {
lockTime := td.state.LastBlockHeight()
dupSubsidyTx := tx.NewSubsidyTx(lockTime, proposer.Address(),
td.state.params.BlockReward, "duplicated subsidy transaction")
invTransferTx, _ := td.GenerateTestTransferTx()
invBondTx, _ := td.GenerateTestBondTx()
invSortitionTx, _ := td.GenerateTestSortitionTx()
invTransferTx := td.GenerateTestTransferTx()
invBondTx := td.GenerateTestBondTx()
invSortitionTx := td.GenerateTestSortitionTx()

pub, _ := td.RandBLSKeyPair()
validTrx1 := tx.NewTransferTx(lockTime, td.genAccKey.PublicKeyNative().AccountAddress(),
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestExecuteBlock(t *testing.T) {
rewardAddr := td.RandAccAddress()
invSubsidyTx := td.state.createSubsidyTx(rewardAddr, 1001)
validSubsidyTx := td.state.createSubsidyTx(rewardAddr, 1000)
invTransferTx, _ := td.GenerateTestTransferTx()
invTransferTx := td.GenerateTestTransferTx()

validTx1 := tx.NewTransferTx(1, td.genAccKey.PublicKeyNative().AccountAddress(),
td.RandAccAddress(), 1, 1000, "")
Expand Down
2 changes: 1 addition & 1 deletion state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func setup(t *testing.T) *testData {
prevCert, lastSeed, val2.Address())

sig := valKey.Sign([]byte("fatdog"))
lastCert := certificate.NewBlockCertificate(lastHeight, 0, true)
lastCert := certificate.NewBlockCertificate(lastHeight, 0)
lastCert.SetSignature(committers, []int32{}, sig)
mockStore.SaveBlock(lastBlock, lastCert)
assert.Equal(t, mockStore.LastHeight, lastHeight)
Expand Down
2 changes: 1 addition & 1 deletion state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (m *MockState) CommitBlock(b *block.Block, cert *certificate.BlockCertifica
func (*MockState) Close() {}

func (m *MockState) ProposeBlock(valKey *bls.ValidatorKey, _ crypto.Address) (*block.Block, error) {
blk, _ := m.ts.GenerateTestBlockWithProposer(m.TestStore.LastHeight, valKey.Address())
blk, _ := m.ts.GenerateTestBlock(m.TestStore.LastHeight, testsuite.BlockWithProposer(valKey.Address()))

return blk, nil
}
Expand Down
10 changes: 5 additions & 5 deletions state/score/score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ func TestScoreManager(t *testing.T) {
maxCert := uint32(3)
sm := NewScoreManager(maxCert)

cert1 := certificate.NewBlockCertificate(1, 0, false)
cert1 := certificate.NewBlockCertificate(1, 0)
cert1.SetSignature([]int32{0, 1, 2, 3}, []int32{0}, nil)

cert2 := certificate.NewBlockCertificate(2, 0, false)
cert2 := certificate.NewBlockCertificate(2, 0)
cert2.SetSignature([]int32{0, 1, 2, 3}, []int32{3}, nil)

cert3 := certificate.NewBlockCertificate(3, 0, false)
cert3 := certificate.NewBlockCertificate(3, 0)
cert3.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

cert4 := certificate.NewBlockCertificate(4, 0, false)
cert4 := certificate.NewBlockCertificate(4, 0)
cert4.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

cert5 := certificate.NewBlockCertificate(5, 0, false)
cert5 := certificate.NewBlockCertificate(5, 0)
cert5.SetSignature([]int32{1, 2, 3, 4}, []int32{2}, nil)

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (td *testData) makeCertificateAndSign(t *testing.T, blockHash hash.Hash,

sigs := make([]*bls.Signature, 0, len(td.genValKeys))
height := td.state.LastBlockHeight()
cert := certificate.NewBlockCertificate(height+1, round, true)
cert := certificate.NewBlockCertificate(height+1, round)
signBytes := cert.SignBytes(blockHash)
committers := []int32{0, 1, 2, 3}
absentees := []int32{3}
Expand Down
1 change: 0 additions & 1 deletion state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func TestBlockValidation(t *testing.T) {
invPrevCert := certificate.NewBlockCertificate(
blk0.PrevCertificate().Height(),
blk0.PrevCertificate().Round(),
blk0.PrevCertificate().FastPath(),
)
invPrevCert.SetSignature(
blk0.PrevCertificate().Committers(),
Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBlockAnnounceMessage(t *testing.T) {

t.Run("Invalid certificate", func(t *testing.T) {
blk, _ := ts.GenerateTestBlock(ts.RandHeight())
cert := certificate.NewBlockCertificate(0, 0, false)
cert := certificate.NewBlockCertificate(0, 0)
m := NewBlockAnnounceMessage(blk, cert)
err := m.BasicCheck()

Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBlocksResponseMessage(t *testing.T) {
sid := 123
t.Run("Invalid certificate", func(t *testing.T) {
blk, _ := ts.GenerateTestBlock(ts.RandHeight())
cert := certificate.NewBlockCertificate(0, 0, false)
cert := certificate.NewBlockCertificate(0, 0)
d, _ := blk.Bytes()
m := NewBlocksResponseMessage(ResponseCodeMoreBlocks, ResponseCodeMoreBlocks.String(),
sid, ts.RandHeight(), [][]byte{d}, cert)
Expand Down
2 changes: 1 addition & 1 deletion sync/bundle/message/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestTransactionsMessage(t *testing.T) {
})

t.Run("OK", func(t *testing.T) {
trx, _ := ts.GenerateTestTransferTx()
trx := ts.GenerateTestTransferTx()
m := NewTransactionsMessage([]*tx.Tx{trx})

assert.NoError(t, m.BasicCheck())
Expand Down
4 changes: 3 additions & 1 deletion sync/handler_block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
func TestParsingBlockAnnounceMessages(t *testing.T) {
td := setup(t, nil)

td.state.CommitTestBlocks(10)

pid := td.RandPeerID()
lastHeight := td.state.LastBlockHeight()
blk1, cert1 := td.GenerateTestBlock(lastHeight + 1)
Expand Down Expand Up @@ -38,7 +40,7 @@ func TestInvalidBlockAnnounce(t *testing.T) {
pid := td.RandPeerID()
height := td.state.LastBlockHeight() + 1
blk, _ := td.GenerateTestBlock(height)
invCert := certificate.NewBlockCertificate(height, 0, false)
invCert := certificate.NewBlockCertificate(height, 0)
msg := message.NewBlockAnnounceMessage(blk, invCert)

err := td.receivingNewMessage(td.sync, msg, pid)
Expand Down
6 changes: 4 additions & 2 deletions sync/handler_blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func TestInvalidBlockData(t *testing.T) {
func TestOneBlockShorter(t *testing.T) {
td := setup(t, nil)

td.state.CommitTestBlocks(10)

lastHeight := td.state.LastBlockHeight()
blk1, cert1 := td.GenerateTestBlock(lastHeight + 1)
d1, _ := blk1.Bytes()
Expand Down Expand Up @@ -282,7 +284,7 @@ func TestSyncing(t *testing.T) {
blockInterval := td.syncBob.state.Genesis().Params().BlockInterval()
blockTime := td.syncBob.state.Genesis().GenesisTime()
for i := uint32(0); i < 100; i++ {
blk, cert := td.GenerateTestBlockWithTime(i+1, blockTime)
blk, cert := td.GenerateTestBlock(i+1, testsuite.BlockWithTime(blockTime))
assert.NoError(t, td.syncBob.state.CommitBlock(blk, cert))

blockTime = blockTime.Add(blockInterval)
Expand Down Expand Up @@ -343,7 +345,7 @@ func TestSyncingHasBlockInCache(t *testing.T) {
blockInterval := td.syncBob.state.Genesis().Params().BlockInterval()
blockTime := td.syncBob.state.Genesis().GenesisTime()
for i := uint32(0); i < 23; i++ {
blk, cert := td.GenerateTestBlockWithTime(i+1, blockTime)
blk, cert := td.GenerateTestBlock(i+1, testsuite.BlockWithTime(blockTime))
assert.NoError(t, td.syncBob.state.CommitBlock(blk, cert))

blockTime = blockTime.Add(blockInterval)
Expand Down
2 changes: 1 addition & 1 deletion sync/handler_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestParsingTransactionsMessages(t *testing.T) {
td := setup(t, nil)

t.Run("Parsing transactions message", func(t *testing.T) {
trx1, _ := td.GenerateTestBondTx()
trx1 := td.GenerateTestBondTx()
msg := message.NewTransactionsMessage([]*tx.Tx{trx1})
pid := td.RandPeerID()

Expand Down
13 changes: 10 additions & 3 deletions txpool/mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package txpool

import (
"slices"

"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/types/amount"
Expand Down Expand Up @@ -66,9 +68,14 @@ func (m *MockTxPool) AppendTxAndBroadcast(trx *tx.Tx) error {
return nil
}

func (*MockTxPool) RemoveTx(_ hash.Hash) {
// This test pools is shared between different test objects
// delete(m.Txs, id)
func (m *MockTxPool) RemoveTx(id hash.Hash) {
for i, trx := range m.Txs {
if trx.ID() == id {
m.Txs = slices.Delete(m.Txs, i, i+1)

return
}
}
}

func (m *MockTxPool) PrepareBlockTransactions() block.Txs {
Expand Down
6 changes: 3 additions & 3 deletions txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func TestAppendAndRemove(t *testing.T) {
func TestAppendInvalidTransaction(t *testing.T) {
td := setup(t)

invalidTx, _ := td.GenerateTestTransferTx()
assert.Error(t, td.pool.AppendTx(invalidTx))
invTrx := td.GenerateTestTransferTx()
assert.Error(t, td.pool.AppendTx(invTrx))
}

// TestFullPool tests if the pool prunes the old transactions when it is full.
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestAppendAndBroadcast(t *testing.T) {
assert.NoError(t, td.pool.AppendTxAndBroadcast(testTrx))
td.shouldPublishTransaction(t, testTrx.ID())

invTrx, _ := td.GenerateTestBondTx()
invTrx := td.GenerateTestBondTx()
assert.Error(t, td.pool.AppendTxAndBroadcast(invTrx))
}

Expand Down
4 changes: 2 additions & 2 deletions types/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func TestBasicCheck(t *testing.T) {
})

t.Run("Without the previous certificate", func(t *testing.T) {
b0, _ := ts.GenerateTestBlock(ts.RandHeight())
b := block.NewBlock(b0.Header(), nil, b0.Transactions())
b, _ := ts.GenerateTestBlock(ts.RandHeight(), testsuite.BlockWithPrevCert(nil))

err := b.BasicCheck()
assert.ErrorIs(t, err, block.BasicCheckError{
Expand Down Expand Up @@ -269,6 +268,7 @@ func TestTxFromBytes(t *testing.T) {
_, err = blk.Bytes()
assert.NoError(t, err)

// Invalid data
_, err = block.FromBytes([]byte{1})
assert.Error(t, err)
}
Expand Down
20 changes: 10 additions & 10 deletions types/block/txs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func TestTxsMerkle(t *testing.T) {
ts := testsuite.NewTestSuite(t)

txs := block.NewTxs()
trx1, _ := ts.GenerateTestTransferTx()
trx2, _ := ts.GenerateTestTransferTx()
trx1 := ts.GenerateTestTransferTx()
trx2 := ts.GenerateTestTransferTx()
txs.Append(trx1)
merkle := txs.Root()
assert.Equal(t, merkle, trx1.ID())
Expand All @@ -31,11 +31,11 @@ func TestAppendPrependRemove(t *testing.T) {
ts := testsuite.NewTestSuite(t)

txs := block.NewTxs()
trx1, _ := ts.GenerateTestTransferTx()
trx2, _ := ts.GenerateTestTransferTx()
trx3, _ := ts.GenerateTestTransferTx()
trx4, _ := ts.GenerateTestTransferTx()
trx5, _ := ts.GenerateTestTransferTx()
trx1 := ts.GenerateTestTransferTx()
trx2 := ts.GenerateTestTransferTx()
trx3 := ts.GenerateTestTransferTx()
trx4 := ts.GenerateTestTransferTx()
trx5 := ts.GenerateTestTransferTx()
txs.Append(trx2)
txs.Append(trx3)
txs.Prepend(trx1)
Expand All @@ -52,7 +52,7 @@ func TestIsEmpty(t *testing.T) {
txs := block.NewTxs()
assert.True(t, txs.IsEmpty())

trx, _ := ts.GenerateTestTransferTx()
trx := ts.GenerateTestTransferTx()
txs.Append(trx)
assert.False(t, txs.IsEmpty())
}
Expand All @@ -61,8 +61,8 @@ func TestGetTransaction(t *testing.T) {
ts := testsuite.NewTestSuite(t)

txs := block.NewTxs()
trx1, _ := ts.GenerateTestTransferTx()
trx2, _ := ts.GenerateTestTransferTx()
trx1 := ts.GenerateTestTransferTx()
trx2 := ts.GenerateTestTransferTx()
txs.Append(trx1)
txs.Append(trx2)
assert.Equal(t, trx1, txs.Get(0))
Expand Down
23 changes: 4 additions & 19 deletions types/certificate/block_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ type BlockCertificate struct {
}

// NewBlockCertificate creates a new BlockCertificate.
func NewBlockCertificate(height uint32, round int16, fastPath bool) *BlockCertificate {
func NewBlockCertificate(height uint32, round int16) *BlockCertificate {
return &BlockCertificate{
baseCertificate: baseCertificate{
height: height,
round: round,
fastPath: fastPath,
height: height,
round: round,
},
}
}
Expand All @@ -29,27 +28,13 @@ func (cert *BlockCertificate) SignBytes(blockHash hash.Hash) []byte {
sb = append(sb, util.Uint32ToSlice(cert.height)...)
sb = append(sb, util.Int16ToSlice(cert.round)...)

if cert.fastPath {
sb = append(sb, util.StringToBytes("PREPARE")...)
}

return sb
}

func (cert *BlockCertificate) Validate(validators []*validator.Validator, blockHash hash.Hash) error {
calcRequiredPowerFn := func(committeePower int64) int64 {
t := (committeePower - 1) / 5
p := (3 * t) + 1
if cert.fastPath {
p = (4 * t) + 1
}

return p
}

signBytes := cert.SignBytes(blockHash)

return cert.baseCertificate.validate(validators, signBytes, calcRequiredPowerFn)
return cert.baseCertificate.validate(validators, signBytes, require2Fp1Power)
}

func (cert *BlockCertificate) Clone() *BlockCertificate {
Expand Down
Loading

0 comments on commit 5727751

Please sign in to comment.