diff --git a/.golangci.yml b/.golangci.yml index 184ce97b7..eaf58d8bb 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -40,7 +40,6 @@ linters: - goerr113 # Consider to enable it - errorlint # Consider to enable it - errname # Consider to enable it - - thelper # Consider to enable it - wastedassign # Consider to enable it - nonamedreturns # Consider to enable it - nlreturn # Consider to enable it diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index 822df5c6d..7eb054aee 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -63,6 +63,8 @@ func (o *OverrideStringer) String() string { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) _, signers := ts.GenerateTestCommittee(4) @@ -139,6 +141,8 @@ func setup(t *testing.T) *testData { } func (td *testData) shouldPublishBlockAnnounce(t *testing.T, cons *consensus, hash hash.Hash) { + t.Helper() + timeout := time.NewTimer(1 * time.Second) for { @@ -161,12 +165,16 @@ func (td *testData) shouldPublishBlockAnnounce(t *testing.T, cons *consensus, ha func (td *testData) shouldPublishProposal(t *testing.T, cons *consensus, height uint32, round int16, ) *proposal.Proposal { + t.Helper() + return shouldPublishProposal(t, cons, height, round) } func shouldPublishProposal(t *testing.T, cons *consensus, height uint32, round int16, ) *proposal.Proposal { + t.Helper() + timeout := time.NewTimer(1 * time.Second) for { @@ -188,6 +196,8 @@ func shouldPublishProposal(t *testing.T, cons *consensus, } func (td *testData) shouldPublishQueryProposal(t *testing.T, cons *consensus, height uint32, round int16) { + t.Helper() + timeout := time.NewTimer(2 * time.Second) for { @@ -209,6 +219,8 @@ func (td *testData) shouldPublishQueryProposal(t *testing.T, cons *consensus, he } func (td *testData) shouldPublishVote(t *testing.T, cons *consensus, voteType vote.Type, hash hash.Hash) { + t.Helper() + timeout := time.NewTimer(2 * time.Second) for { @@ -230,16 +242,22 @@ func (td *testData) shouldPublishVote(t *testing.T, cons *consensus, voteType vo } func checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) { + t.Helper() + h, r := cons.HeightRound() assert.Equal(t, h, height) assert.Equal(t, r, round) } func (td *testData) checkHeightRound(t *testing.T, cons *consensus, height uint32, round int16) { + t.Helper() + checkHeightRound(t, cons, height, round) } func checkHeightRoundWait(t *testing.T, cons *consensus, height uint32, round int16) { + t.Helper() + for i := 0; i < 20; i++ { h, r := cons.HeightRound() if h == height && r == round { @@ -252,6 +270,8 @@ func checkHeightRoundWait(t *testing.T, cons *consensus, height uint32, round in } func (td *testData) checkHeightRoundWait(t *testing.T, cons *consensus, height uint32, round int16) { + t.Helper() + checkHeightRoundWait(t, cons, height, round) } @@ -284,6 +304,8 @@ func (td *testData) enterNextRound(cons *consensus) { } func (td *testData) commitBlockForAllStates(t *testing.T) (*block.Block, *block.Certificate) { + t.Helper() + height := td.consX.state.LastBlockHeight() var err error p := td.makeProposal(t, height+1, 0) @@ -310,6 +332,8 @@ func (td *testData) commitBlockForAllStates(t *testing.T) (*block.Block, *block. } func (td *testData) makeProposal(t *testing.T, height uint32, round int16) *proposal.Proposal { + t.Helper() + var p *proposal.Proposal switch (height % 4) + uint32(round) { case 1: diff --git a/consensus/voteset/voteset_test.go b/consensus/voteset/voteset_test.go index 39e650efd..e23bb0c19 100644 --- a/consensus/voteset/voteset_test.go +++ b/consensus/voteset/voteset_test.go @@ -14,6 +14,8 @@ import ( ) func setupCommittee(t *testing.T, ts *testsuite.TestSuite, stakes ...int64) (committee.Committee, []crypto.Signer) { + t.Helper() + signers := []crypto.Signer{} vals := []*validator.Validator{} for i, s := range stakes { diff --git a/execution/executor/transfer_test.go b/execution/executor/transfer_test.go index 8279cd6e0..e353c42b0 100644 --- a/execution/executor/transfer_test.go +++ b/execution/executor/transfer_test.go @@ -20,6 +20,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) sandbox := sandbox.MockingSandbox(ts) @@ -35,6 +37,8 @@ func setup(t *testing.T) *testData { } func (td *testData) checkTotalCoin(t *testing.T, fee int64) { + t.Helper() + total := int64(0) for _, acc := range td.sandbox.TestStore.Accounts { total += acc.Balance() diff --git a/network/network_test.go b/network/network_test.go index e648db69d..a5e91e825 100644 --- a/network/network_test.go +++ b/network/network_test.go @@ -18,6 +18,8 @@ import ( // Original code from: // https://github.com/libp2p/go-libp2p/blob/master/p2p/host/autorelay/autorelay_test.go func makeTestRelay(t *testing.T) host.Host { + t.Helper() + h, err := lp2p.New( lp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"), lp2p.DisableRelay(), @@ -40,6 +42,8 @@ func makeTestRelay(t *testing.T) host.Host { } func makeTestNetwork(t *testing.T, conf *Config, opts []lp2p.Option) *network { + t.Helper() + net, err := newNetwork(conf, opts) assert.NoError(t, err) @@ -67,6 +71,8 @@ func testConfig() *Config { } func shouldReceiveEvent(t *testing.T, net *network, eventType EventType) Event { + t.Helper() + timeout := time.NewTimer(2 * time.Second) for { @@ -83,6 +89,8 @@ func shouldReceiveEvent(t *testing.T, net *network, eventType EventType) Event { } func shouldNotReceiveEvent(t *testing.T, net *network) { + t.Helper() + timeout := time.NewTimer(100 * time.Millisecond) for { @@ -97,6 +105,8 @@ func shouldNotReceiveEvent(t *testing.T, net *network) { } func readData(t *testing.T, r io.ReadCloser, len int) []byte { + t.Helper() + buf := make([]byte, len) _, err := r.Read(buf) assert.NoError(t, err) diff --git a/sandbox/sandbox_test.go b/sandbox/sandbox_test.go index afb802a97..828fe5548 100644 --- a/sandbox/sandbox_test.go +++ b/sandbox/sandbox_test.go @@ -24,6 +24,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) store := store.MockingStore(ts) params := param.DefaultParams() diff --git a/state/lastinfo/last_info_test.go b/state/lastinfo/last_info_test.go index e495c271f..fd60b5fc7 100644 --- a/state/lastinfo/last_info_test.go +++ b/state/lastinfo/last_info_test.go @@ -27,6 +27,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) store := store.MockingStore(ts) lastInfo := NewLastInfo(store) diff --git a/state/state_test.go b/state/state_test.go index b1f8fb9b6..970dcb7af 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -41,6 +41,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) pub1, prv1 := ts.RandomBLSKeyPair() @@ -106,6 +108,8 @@ func setup(t *testing.T) *testData { func (td *testData) makeBlockAndCertificate(t *testing.T, round int16, signers ...crypto.Signer, ) (*block.Block, *block.Certificate) { + t.Helper() + var st *state if td.state1.committee.IsProposer(td.state1.signers[0].Address(), round) { st = td.state1 @@ -128,6 +132,8 @@ func (td *testData) makeBlockAndCertificate(t *testing.T, round int16, func (td *testData) makeCertificateAndSign(t *testing.T, blockHash hash.Hash, round int16, signers ...crypto.Signer, ) *block.Certificate { + t.Helper() + assert.NotZero(t, len(signers)) sigs := make([]*bls.Signature, len(signers)) sb := block.CertificateSignBytes(blockHash, round) @@ -158,6 +164,8 @@ func (td *testData) makeCertificateAndSign(t *testing.T, blockHash hash.Hash, ro } func (td *testData) commitBlockForAllStates(t *testing.T, b *block.Block, c *block.Certificate) { + t.Helper() + assert.NoError(t, td.state1.CommitBlock(td.state1.lastInfo.BlockHeight()+1, b, c)) assert.NoError(t, td.state2.CommitBlock(td.state2.lastInfo.BlockHeight()+1, b, c)) assert.NoError(t, td.state3.CommitBlock(td.state3.lastInfo.BlockHeight()+1, b, c)) @@ -165,6 +173,8 @@ func (td *testData) commitBlockForAllStates(t *testing.T, b *block.Block, c *blo } func (td *testData) moveToNextHeightForAllStates(t *testing.T) { + t.Helper() + b, c := td.makeBlockAndCertificate(t, 0, td.valSigner1, td.valSigner2, td.valSigner3, td.valSigner4) td.commitBlockForAllStates(t, b, c) } diff --git a/store/store_test.go b/store/store_test.go index bda0a818d..ed821d6d6 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -17,6 +17,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) conf := &Config{ @@ -36,6 +38,8 @@ func setup(t *testing.T) *testData { } func (td *testData) saveTestBlocks(t *testing.T, num int) { + t.Helper() + lastHeight, _ := td.store.LastCertificate() for i := 0; i < num; i++ { b := td.GenerateTestBlock(nil, nil) diff --git a/sync/firewall/firewall_test.go b/sync/firewall/firewall_test.go index d414c0e57..7f70340f3 100644 --- a/sync/firewall/firewall_test.go +++ b/sync/firewall/firewall_test.go @@ -29,6 +29,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) logger := logger.NewSubLogger("firewall", nil) diff --git a/sync/sync_test.go b/sync/sync_test.go index 4ded25f84..ce635d7fb 100644 --- a/sync/sync_test.go +++ b/sync/sync_test.go @@ -64,6 +64,8 @@ func testConfig() *Config { } func setup(t *testing.T, config *Config) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) if config == nil { @@ -110,6 +112,8 @@ func setup(t *testing.T, config *Config) *testData { } func shouldPublishMessageWithThisType(t *testing.T, net *network.MockNetwork, msgType message.Type) *bundle.Bundle { + t.Helper() + timeout := time.NewTimer(3 * time.Second) for { @@ -154,10 +158,14 @@ func shouldPublishMessageWithThisType(t *testing.T, net *network.MockNetwork, ms func (td *testData) shouldPublishMessageWithThisType(t *testing.T, net *network.MockNetwork, msgType message.Type, ) *bundle.Bundle { + t.Helper() + return shouldPublishMessageWithThisType(t, net, msgType) } func (td *testData) shouldNotPublishMessageWithThisType(t *testing.T, net *network.MockNetwork, msgType message.Type) { + t.Helper() + timeout := time.NewTimer(300 * time.Millisecond) for { @@ -181,21 +189,29 @@ func (td *testData) receivingNewMessage(sync *synchronizer, msg message.Message, } func addBlocks(t *testing.T, state *state.MockState, count int) { + t.Helper() + h := state.LastBlockHeight() state.CommitTestBlocks(count) assert.Equal(t, h+uint32(count), state.LastBlockHeight()) } func (td *testData) addBlocks(t *testing.T, state *state.MockState, count int) { + t.Helper() + addBlocks(t, state, count) } func (td *testData) addPeer(t *testing.T, pub crypto.PublicKey, pid peer.ID, nodeNetwork bool) { + t.Helper() + td.sync.peerSet.UpdatePeerInfo(pid, peerset.StatusCodeKnown, t.Name(), version.Agent(), []*bls.PublicKey{pub.(*bls.PublicKey)}, nodeNetwork) } func (td *testData) addPeerToCommittee(t *testing.T, pid peer.ID, pub crypto.PublicKey) { + t.Helper() + if pub == nil { pub, _ = td.RandomBLSKeyPair() } @@ -213,6 +229,8 @@ func (td *testData) addPeerToCommittee(t *testing.T, pid peer.ID, pub crypto.Pub } func (td *testData) checkPeerStatus(t *testing.T, pid peer.ID, code peerset.StatusCode) { + t.Helper() + require.Equal(t, td.sync.peerSet.GetPeer(pid).Status, code) } diff --git a/tests/account_test.go b/tests/account_test.go index e509e689d..bd650da16 100644 --- a/tests/account_test.go +++ b/tests/account_test.go @@ -9,7 +9,9 @@ import ( "github.com/stretchr/testify/require" ) -func getAccount(_ *testing.T, addr crypto.Address) *pactus.AccountInfo { +func getAccount(t *testing.T, addr crypto.Address) *pactus.AccountInfo { + t.Helper() + res, err := tBlockchain.GetAccount(tCtx, &pactus.GetAccountRequest{Address: addr.String()}) if err != nil { diff --git a/tests/transaction_test.go b/tests/transaction_test.go index ca20c7b7d..f7ddda1d3 100644 --- a/tests/transaction_test.go +++ b/tests/transaction_test.go @@ -13,13 +13,17 @@ import ( "github.com/stretchr/testify/require" ) -func sendRawTx(_ *testing.T, raw []byte) error { +func sendRawTx(t *testing.T, raw []byte) error { + t.Helper() + _, err := tTransaction.SendRawTransaction(tCtx, &pactus.SendRawTransactionRequest{Data: raw}) return err } func broadcastSendTransaction(t *testing.T, sender crypto.Signer, receiver crypto.Address, amt, fee int64) error { + t.Helper() + stamp := lastHash().Stamp() seq := getSequence(sender.Address()) trx := tx.NewTransferTx(stamp, seq+1, sender.Address(), receiver, amt, fee, "") @@ -30,6 +34,8 @@ func broadcastSendTransaction(t *testing.T, sender crypto.Signer, receiver crypt } func broadcastBondTransaction(t *testing.T, sender crypto.Signer, pub crypto.PublicKey, stake, fee int64) error { + t.Helper() + stamp := lastHash().Stamp() seq := getSequence(sender.Address()) trx := tx.NewBondTx(stamp, seq+1, sender.Address(), pub.Address(), pub.(*bls.PublicKey), stake, fee, "") diff --git a/tests/validator_test.go b/tests/validator_test.go index 4d73fb4ba..45facb6ba 100644 --- a/tests/validator_test.go +++ b/tests/validator_test.go @@ -9,7 +9,9 @@ import ( "github.com/stretchr/testify/require" ) -func getValidator(_ *testing.T, addr crypto.Address) *pactus.ValidatorInfo { +func getValidator(t *testing.T, addr crypto.Address) *pactus.ValidatorInfo { + t.Helper() + res, err := tBlockchain.GetValidator(tCtx, &pactus.GetValidatorRequest{Address: addr.String()}) if err != nil { diff --git a/txpool/pool_test.go b/txpool/pool_test.go index c03d3177d..5accfb684 100644 --- a/txpool/pool_test.go +++ b/txpool/pool_test.go @@ -27,6 +27,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) ch := make(chan message.Message, 10) @@ -49,6 +51,8 @@ func setup(t *testing.T) *testData { } func (td *testData) shouldPublishTransaction(t *testing.T, id tx.ID) { + t.Helper() + timeout := time.NewTimer(1 * time.Second) for { diff --git a/util/testsuite/testsuite.go b/util/testsuite/testsuite.go index 2f7557058..e9a9ad4b8 100644 --- a/util/testsuite/testsuite.go +++ b/util/testsuite/testsuite.go @@ -42,6 +42,8 @@ func NewTestSuiteForSeed(seed int64) *TestSuite { // NewTestSuite creates a new TestSuite by generating new seed. func NewTestSuite(t *testing.T) *TestSuite { + t.Helper() + seed := time.Now().UTC().UnixNano() t.Logf("%v seed is %v", t.Name(), seed) return &TestSuite{ diff --git a/wallet/vault/vault_test.go b/wallet/vault/vault_test.go index 26ce2f62e..cc3995d3f 100644 --- a/wallet/vault/vault_test.go +++ b/wallet/vault/vault_test.go @@ -24,6 +24,8 @@ type testData struct { // setup return an instances of vault fo testing. func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) mnemonic := GenerateMnemonic(128) diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index c3c6fc6e3..2482dfe1d 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -53,6 +53,8 @@ func bufDialer(context.Context, string) (net.Conn, error) { } func setup(t *testing.T) *testData { + t.Helper() + ts := testsuite.NewTestSuite(t) password := "" diff --git a/www/grpc/server_test.go b/www/grpc/server_test.go index 1484c1fe2..756642cfd 100644 --- a/www/grpc/server_test.go +++ b/www/grpc/server_test.go @@ -87,6 +87,8 @@ func bufDialer(context.Context, string) (net.Conn, error) { } func testBlockchainClient(t *testing.T) (*grpc.ClientConn, pactus.BlockchainClient) { + t.Helper() + conn, err := grpc.DialContext(tCtx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { @@ -96,6 +98,8 @@ func testBlockchainClient(t *testing.T) (*grpc.ClientConn, pactus.BlockchainClie } func testNetworkClient(t *testing.T) (*grpc.ClientConn, pactus.NetworkClient) { + t.Helper() + conn, err := grpc.DialContext(tCtx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { @@ -105,6 +109,8 @@ func testNetworkClient(t *testing.T) (*grpc.ClientConn, pactus.NetworkClient) { } func testTransactionClient(t *testing.T) (*grpc.ClientConn, pactus.TransactionClient) { + t.Helper() + conn, err := grpc.DialContext(tCtx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { @@ -114,6 +120,8 @@ func testTransactionClient(t *testing.T) (*grpc.ClientConn, pactus.TransactionCl } func testWalletClient(t *testing.T) (*grpc.ClientConn, pactus.WalletClient) { + t.Helper() + conn, err := grpc.DialContext(tCtx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { diff --git a/www/http/http_test.go b/www/http/http_test.go index e8efc4a5d..a3a8a06a9 100644 --- a/www/http/http_test.go +++ b/www/http/http_test.go @@ -28,6 +28,8 @@ type testData struct { } func setup(t *testing.T) *testData { + t.Helper() + if tTestData != nil { return tTestData }