Skip to content

Commit

Permalink
chore: enable thelper linter (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Aug 21, 2023
1 parent 80a6e6d commit 3a9c164
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 4 deletions.
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
24 changes: 24 additions & 0 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}

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

func setup(t *testing.T) *testData {
t.Helper()

ts := testsuite.NewTestSuite(t)

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

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ 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 +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
Expand All @@ -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)
Expand Down Expand Up @@ -158,13 +164,17 @@ 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
4 changes: 4 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type testData struct {
}

func setup(t *testing.T) *testData {
t.Helper()

ts := testsuite.NewTestSuite(t)

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

func setup(t *testing.T) *testData {
t.Helper()

ts := testsuite.NewTestSuite(t)

logger := logger.NewSubLogger("firewall", nil)
Expand Down
18 changes: 18 additions & 0 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ 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 +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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
Expand All @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion tests/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion tests/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand All @@ -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, "")
Expand Down
4 changes: 3 additions & 1 deletion tests/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 3a9c164

Please sign in to comment.