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

chore: enable thelper linter #653

Merged
merged 9 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (o *OverrideStringer) String() string {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

_, signers := ts.GenerateTestCommittee(4)
Expand Down Expand Up @@ -139,6 +140,7 @@ 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 {
Expand All @@ -161,12 +163,14 @@ 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 {
Expand All @@ -188,6 +192,7 @@ 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 {
Expand All @@ -209,6 +214,7 @@ 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 {
Expand All @@ -230,16 +236,19 @@ 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 {
Expand All @@ -252,6 +261,7 @@ 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)
}

Expand Down Expand Up @@ -284,6 +294,7 @@ 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)
Expand All @@ -310,6 +321,7 @@ 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:
Expand Down
1 change: 1 addition & 0 deletions consensus/voteset/voteset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 {
Expand Down
2 changes: 2 additions & 0 deletions execution/executor/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

sandbox := sandbox.MockingSandbox(ts)
Expand All @@ -35,6 +36,7 @@ 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()
Expand Down
5 changes: 5 additions & 0 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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(),
Expand All @@ -40,6 +41,7 @@ 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)

Expand Down Expand Up @@ -67,6 +69,7 @@ func testConfig() *Config {
}

func shouldReceiveEvent(t *testing.T, net *network, eventType EventType) Event {
t.Helper()
timeout := time.NewTimer(2 * time.Second)

for {
Expand All @@ -83,6 +86,7 @@ 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 {
Expand All @@ -97,6 +101,7 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)
store := store.MockingStore(ts)
params := param.DefaultParams()
Expand Down
1 change: 1 addition & 0 deletions state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)
store := store.MockingStore(ts)
lastInfo := NewLastInfo(store)
Expand Down
5 changes: 5 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

pub1, prv1 := ts.RandomBLSKeyPair()
Expand Down Expand Up @@ -106,6 +107,7 @@ 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
Expand All @@ -128,6 +130,7 @@ 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)
Expand Down Expand Up @@ -158,13 +161,15 @@ 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))
assert.NoError(t, td.state4.CommitBlock(td.state4.lastInfo.BlockHeight()+1, b, c))
}

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)
}
Expand Down
2 changes: 2 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

conf := &Config{
Expand All @@ -36,6 +37,7 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions sync/firewall/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

logger := logger.NewSubLogger("firewall", nil)
Expand Down
9 changes: 9 additions & 0 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func testConfig() *Config {
}

func setup(t *testing.T, config *Config) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

if config == nil {
Expand Down Expand Up @@ -110,6 +111,7 @@ 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 {
Expand Down Expand Up @@ -154,10 +156,12 @@ 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 {
Expand All @@ -181,21 +185,25 @@ 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()
}
Expand All @@ -213,6 +221,7 @@ 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)
}

Expand Down
3 changes: 2 additions & 1 deletion tests/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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 {
Expand Down
5 changes: 4 additions & 1 deletion tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ 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, "")
Expand All @@ -30,6 +32,7 @@ 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, "")
Expand Down
3 changes: 2 additions & 1 deletion tests/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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 {
Expand Down
2 changes: 2 additions & 0 deletions txpool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

ch := make(chan message.Message, 10)
Expand All @@ -49,6 +50,7 @@ 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 {
Expand Down
1 change: 1 addition & 0 deletions util/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ 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{
Expand Down
1 change: 1 addition & 0 deletions wallet/vault/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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)
Expand Down
1 change: 1 addition & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func bufDialer(context.Context, string) (net.Conn, error) {
}

func setup(t *testing.T) *testData {
t.Helper()
ts := testsuite.NewTestSuite(t)

password := ""
Expand Down
Loading
Loading