From 66d9cb7558717e256bd2b6f41cefaa905aea49ba Mon Sep 17 00:00:00 2001 From: cfal Date: Wed, 27 Nov 2024 16:35:14 +0700 Subject: [PATCH] core: fix lint errors --- core/cmd/shell_test.go | 2 +- core/cmd/tron_keys_commands_test.go | 9 +++-- core/cmd/tron_node_commands.go | 6 ++-- .../relayer_chain_interoperators_test.go | 6 ++-- .../services/keystore/keys/tronkey/account.go | 7 ++-- .../keystore/keys/tronkey/account_test.go | 33 ++++++++++--------- core/services/keystore/models.go | 2 +- core/services/keystore/models_test.go | 2 +- core/services/keystore/tron_test.go | 27 ++++++++------- core/web/tron_keys_controller_test.go | 24 +++++++------- 10 files changed, 57 insertions(+), 61 deletions(-) diff --git a/core/cmd/shell_test.go b/core/cmd/shell_test.go index c5502528f70..d9741549302 100644 --- a/core/cmd/shell_test.go +++ b/core/cmd/shell_test.go @@ -642,7 +642,7 @@ func TestSetupTronRelayer(t *testing.T) { require.NotNil(t, relayers) require.Len(t, relayers, nEnabledChains) // no using plugin, so registry should be empty - require.Len(t, reg.List(), 0) + require.Empty(t, reg.List()) }) t.Run("plugin", func(t *testing.T) { diff --git a/core/cmd/tron_keys_commands_test.go b/core/cmd/tron_keys_commands_test.go index b76edbf7c2e..29480600d74 100644 --- a/core/cmd/tron_keys_commands_test.go +++ b/core/cmd/tron_keys_commands_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "flag" - "fmt" "os" "testing" @@ -76,10 +75,10 @@ func TestShell_TronKeys(t *testing.T) { key, err := app.GetKeyStore().Tron().Create(ctx) require.NoError(t, err) requireTronKeyCount(t, app, 1) - assert.Nil(t, cmd.NewTronKeysClient(client).ListKeys(cltest.EmptyCLIContext())) - require.Equal(t, 1, len(r.Renders)) + require.NoError(t, cmd.NewTronKeysClient(client).ListKeys(cltest.EmptyCLIContext())) + require.Len(t, r.Renders, 1) keys := *r.Renders[0].(*cmd.TronKeyPresenters) - assert.True(t, key.PublicKeyStr() == keys[0].PubKey) + assert.Equal(t, key.PublicKeyStr(), keys[0].PubKey) }) t.Run("CreateTronKey", func(tt *testing.T) { @@ -142,7 +141,7 @@ func TestShell_TronKeys(t *testing.T) { set = flag.NewFlagSet("test Tron export", 0) flagSetApplyFromAction(cmd.NewTronKeysClient(client).ExportKey, set, "tron") - require.NoError(tt, set.Parse([]string{fmt.Sprint(key.ID())})) + require.NoError(tt, set.Parse([]string{key.ID()})) require.NoError(tt, set.Set("new-password", "../internal/fixtures/incorrect_password.txt")) require.NoError(tt, set.Set("output", keyName)) diff --git a/core/cmd/tron_node_commands.go b/core/cmd/tron_node_commands.go index bc174d21cb1..6a59167dfcf 100644 --- a/core/cmd/tron_node_commands.go +++ b/core/cmd/tron_node_commands.go @@ -16,8 +16,7 @@ func (p *TronNodePresenter) ToRow() []string { // RenderTable implements TableRenderer func (p TronNodePresenter) RenderTable(rt RendererTable) error { - var rows [][]string - rows = append(rows, p.ToRow()) + rows := [][]string{p.ToRow()} renderList(nodeHeaders, rows, rt.Writer) return nil @@ -28,8 +27,7 @@ type TronNodePresenters []TronNodePresenter // RenderTable implements TableRenderer func (ps TronNodePresenters) RenderTable(rt RendererTable) error { - var rows [][]string - + rows := [][]string{} for _, p := range ps { rows = append(rows, p.ToRow()) } diff --git a/core/services/chainlink/relayer_chain_interoperators_test.go b/core/services/chainlink/relayer_chain_interoperators_test.go index 034fe799b64..d9c76613ee1 100644 --- a/core/services/chainlink/relayer_chain_interoperators_test.go +++ b/core/services/chainlink/relayer_chain_interoperators_test.go @@ -221,7 +221,7 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { expectedTronChainCnt int expectedTronNodeCnt int - expectedTronRelayerIds []types.RelayID + expectedTronRelayerIDs []types.RelayID }{ {name: "2 evm chains with 3 nodes", @@ -304,7 +304,7 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { }, expectedTronChainCnt: 2, expectedTronNodeCnt: 2, - expectedTronRelayerIds: []types.RelayID{ + expectedTronRelayerIDs: []types.RelayID{ {Network: relay.NetworkTron, ChainID: tronChainID1}, {Network: relay.NetworkTron, ChainID: tronChainID2}, }, @@ -364,7 +364,7 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { expectedTronChainCnt: 2, expectedTronNodeCnt: 2, - expectedTronRelayerIds: []types.RelayID{ + expectedTronRelayerIDs: []types.RelayID{ {Network: relay.NetworkTron, ChainID: tronChainID1}, {Network: relay.NetworkTron, ChainID: tronChainID2}, }, diff --git a/core/services/keystore/keys/tronkey/account.go b/core/services/keystore/keys/tronkey/account.go index a48cdf5f81c..3a93ba82e9f 100644 --- a/core/services/keystore/keys/tronkey/account.go +++ b/core/services/keystore/keys/tronkey/account.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "crypto/sha256" "encoding/hex" + "errors" "fmt" "math/big" @@ -136,7 +137,7 @@ func DecodeCheck(input string) ([]byte, error) { } if len(decodeCheck) < 4 { - return nil, fmt.Errorf("b58 check error") + return nil, errors.New("base58 check error") } // tron address should should have 20 bytes + 4 checksum + 1 Prefix @@ -146,7 +147,7 @@ func DecodeCheck(input string) ([]byte, error) { // check prefix if decodeCheck[0] != prefixMainnet { - return nil, fmt.Errorf("invalid prefix") + return nil, errors.New("invalid prefix") } decodeData := decodeCheck[:len(decodeCheck)-4] @@ -166,7 +167,7 @@ func DecodeCheck(input string) ([]byte, error) { return decodeData, nil } - return nil, fmt.Errorf("b58 check error") + return nil, errors.New("base58 check error") } func Decode(input string) ([]byte, error) { diff --git a/core/services/keystore/keys/tronkey/account_test.go b/core/services/keystore/keys/tronkey/account_test.go index aa05d92d217..08ac567859d 100644 --- a/core/services/keystore/keys/tronkey/account_test.go +++ b/core/services/keystore/keys/tronkey/account_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func Test_DecodeBase58(t *testing.T) { @@ -24,12 +24,12 @@ func Test_DecodeBase58(t *testing.T) { for _, addr := range invalidAddresses { _, err := DecodeCheck(addr) - assert.NotNil(t, err) + require.Error(t, err) } for _, addr := range validAddresses { _, err := DecodeCheck(addr) - assert.Nil(t, err) + require.NoError(t, err) } } @@ -44,12 +44,12 @@ func TestAddress(t *testing.T) { for _, addrStr := range validAddresses { t.Run(addrStr, func(t *testing.T) { addr, err := Base58ToAddress(addrStr) - assert.Nil(t, err) - assert.Equal(t, addrStr, addr.String()) + require.NoError(t, err) + require.Equal(t, addrStr, addr.String()) decoded, err := DecodeCheck(addrStr) - assert.Nil(t, err) - assert.True(t, bytes.Equal(decoded, addr.Bytes())) + require.NoError(t, err) + require.True(t, bytes.Equal(decoded, addr.Bytes())) }) } }) @@ -64,10 +64,10 @@ func TestAddress(t *testing.T) { for _, addrStr := range invalidAddresses { t.Run(addrStr, func(t *testing.T) { _, err := Base58ToAddress(addrStr) - assert.NotNil(t, err) + require.Error(t, err) _, err = DecodeCheck(addrStr) - assert.NotNil(t, err) + require.Error(t, err) }) } }) @@ -75,29 +75,30 @@ func TestAddress(t *testing.T) { t.Run("Address Conversion", func(t *testing.T) { addrStr := "TSvT6Bg3siokv3dbdtt9o4oM1CTXmymGn1" addr, err := Base58ToAddress(addrStr) - assert.Nil(t, err) + require.NoError(t, err) t.Run("To Bytes", func(t *testing.T) { bytes := addr.Bytes() - assert.Equal(t, 21, len(bytes)) + require.Len(t, bytes, 21) }) t.Run("To Hex", func(t *testing.T) { hex := addr.Hex() - assert.True(t, hex[:2] == "0x") - assert.Equal(t, 44, len(hex)) // first 2 bytes are 0x + require.Equal(t, "0x", hex[:2]) + require.Len(t, hex, 44) }) }) t.Run("Address Validity", func(t *testing.T) { t.Run("Valid Address", func(t *testing.T) { - addr, _ := Base58ToAddress("TSvT6Bg3siokv3dbdtt9o4oM1CTXmymGn1") - assert.True(t, isValid(addr)) + addr, err := Base58ToAddress("TSvT6Bg3siokv3dbdtt9o4oM1CTXmymGn1") + require.NoError(t, err) + require.True(t, isValid(addr)) }) t.Run("Zero Address", func(t *testing.T) { addr := Address{} - assert.False(t, isValid(addr)) + require.False(t, isValid(addr)) }) }) } diff --git a/core/services/keystore/models.go b/core/services/keystore/models.go index 31b5a37c96f..1ebc7480997 100644 --- a/core/services/keystore/models.go +++ b/core/services/keystore/models.go @@ -289,7 +289,7 @@ func (kr *keyRing) logPubKeys(lggr logger.Logger) { for _, aptosKey := range kr.Aptos { aptosIDs = append(aptosIDs, aptosKey.ID()) } - var tronIDs []string + tronIDs := []string{} for _, tronKey := range kr.Tron { tronIDs = append(tronIDs, tronKey.ID()) } diff --git a/core/services/keystore/models_test.go b/core/services/keystore/models_test.go index 6e217ea379f..05ec1f60bc0 100644 --- a/core/services/keystore/models_test.go +++ b/core/services/keystore/models_test.go @@ -66,7 +66,7 @@ func TestKeyRing_Encrypt_Decrypt(t *testing.T) { require.Equal(t, originalKeyRing.Cosmos[tk1.ID()].PublicKey(), decryptedKeyRing.Cosmos[tk1.ID()].PublicKey()) require.Equal(t, originalKeyRing.Cosmos[tk2.ID()].PublicKey(), decryptedKeyRing.Cosmos[tk2.ID()].PublicKey()) // compare tron keys - require.Equal(t, 2, len(decryptedKeyRing.Tron)) + require.Len(t, decryptedKeyRing.Tron, 2) require.Equal(t, originalKeyRing.Tron[tk1.ID()].Base58Address(), decryptedKeyRing.Tron[tk1.ID()].Base58Address()) require.Equal(t, originalKeyRing.Tron[tk2.ID()].Base58Address(), decryptedKeyRing.Tron[tk2.ID()].Base58Address()) // compare csa keys diff --git a/core/services/keystore/tron_test.go b/core/services/keystore/tron_test.go index f98da43001d..2450e573898 100644 --- a/core/services/keystore/tron_test.go +++ b/core/services/keystore/tron_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/utils" @@ -35,7 +34,7 @@ func Test_TronKeyStore_E2E(t *testing.T) { defer reset() keys, err := ks.GetAll() require.NoError(t, err) - require.Equal(t, 0, len(keys)) + require.Empty(t, keys) }) t.Run("errors when getting non-existent ID", func(t *testing.T) { @@ -62,7 +61,7 @@ func Test_TronKeyStore_E2E(t *testing.T) { exportJSON, err := ks.Export(key.ID(), cltest.Password) require.NoError(t, err) _, err = ks.Export("non-existent", cltest.Password) - assert.Error(t, err) + require.Error(t, err) _, err = ks.Delete(ctx, key.ID()) require.NoError(t, err) _, err = ks.Get(key.ID()) @@ -70,9 +69,9 @@ func Test_TronKeyStore_E2E(t *testing.T) { importedKey, err := ks.Import(ctx, exportJSON, cltest.Password) require.NoError(t, err) _, err = ks.Import(ctx, exportJSON, cltest.Password) - assert.Error(t, err) + require.Error(t, err) _, err = ks.Import(ctx, []byte(""), cltest.Password) - assert.Error(t, err) + require.Error(t, err) require.Equal(t, key.ID(), importedKey.ID()) retrievedKey, err := ks.Get(key.ID()) require.NoError(t, err) @@ -87,17 +86,17 @@ func Test_TronKeyStore_E2E(t *testing.T) { err = ks.Add(ctx, newKey) require.NoError(t, err) err = ks.Add(ctx, newKey) - assert.Error(t, err) + require.Error(t, err) keys, err := ks.GetAll() require.NoError(t, err) - require.Equal(t, 1, len(keys)) + require.Len(t, keys, 1) _, err = ks.Delete(ctx, newKey.ID()) require.NoError(t, err) _, err = ks.Delete(ctx, newKey.ID()) - assert.Error(t, err) + require.Error(t, err) keys, err = ks.GetAll() require.NoError(t, err) - require.Equal(t, 0, len(keys)) + require.Empty(t, keys) _, err = ks.Get(newKey.ID()) require.Error(t, err) }) @@ -106,14 +105,14 @@ func Test_TronKeyStore_E2E(t *testing.T) { defer reset() ctx := testutils.Context(t) err := ks.EnsureKey(ctx) - assert.NoError(t, err) + require.NoError(t, err) err = ks.EnsureKey(ctx) - assert.NoError(t, err) + require.NoError(t, err) keys, err := ks.GetAll() require.NoError(t, err) - require.Equal(t, 1, len(keys)) + require.Len(t, keys, 1) }) t.Run("sign tx", func(t *testing.T) { @@ -125,7 +124,7 @@ func Test_TronKeyStore_E2E(t *testing.T) { // sign unknown ID _, err = ks.Sign(testutils.Context(t), "not-real", nil) - assert.Error(t, err) + require.Error(t, err) // sign known key @@ -143,7 +142,7 @@ func Test_TronKeyStore_E2E(t *testing.T) { require.NoError(t, err) // signatures should match using keystore sign or key sign - assert.Equal(t, directSig, sig) + require.Equal(t, directSig, sig) }) } diff --git a/core/web/tron_keys_controller_test.go b/core/web/tron_keys_controller_test.go index b4839a88207..5628d7ac2dc 100644 --- a/core/web/tron_keys_controller_test.go +++ b/core/web/tron_keys_controller_test.go @@ -1,7 +1,6 @@ package web_test import ( - "fmt" "net/http" "testing" @@ -12,7 +11,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/web" "github.com/smartcontractkit/chainlink/v2/core/web/presenters" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -28,12 +26,12 @@ func TestTronKeysController_Index_HappyPath(t *testing.T) { resources := []presenters.TronKeyResource{} err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, response), &resources) - assert.NoError(t, err) + require.NoError(t, err) require.Len(t, resources, len(keys)) - assert.Equal(t, keys[0].ID(), resources[0].ID) - assert.Equal(t, keys[0].PublicKeyStr(), resources[0].PubKey) + require.Equal(t, keys[0].ID(), resources[0].ID) + require.Equal(t, keys[0].PublicKeyStr(), resources[0].PubKey) } func TestTronKeysController_Create_HappyPath(t *testing.T) { @@ -53,10 +51,10 @@ func TestTronKeysController_Create_HappyPath(t *testing.T) { resource := presenters.TronKeyResource{} err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, response), &resource) - assert.NoError(t, err) + require.NoError(t, err) - assert.Equal(t, keys[0].ID(), resource.ID) - assert.Equal(t, keys[0].PublicKeyStr(), resource.PubKey) + require.Equal(t, keys[0].ID(), resource.ID) + require.Equal(t, keys[0].PublicKeyStr(), resource.PubKey) _, err = keyStore.Tron().Get(resource.ID) require.NoError(t, err) @@ -70,7 +68,7 @@ func TestTronKeysController_Delete_NonExistentTronKeyID(t *testing.T) { nonExistentTronKeyID := "foobar" response, cleanup := client.Delete("/v2/keys/tron/" + nonExistentTronKeyID) t.Cleanup(cleanup) - assert.Equal(t, http.StatusNotFound, response.StatusCode) + require.Equal(t, http.StatusNotFound, response.StatusCode) } func TestTronKeysController_Delete_HappyPath(t *testing.T) { @@ -83,13 +81,13 @@ func TestTronKeysController_Delete_HappyPath(t *testing.T) { initialLength := len(keys) key, _ := keyStore.Tron().Create(ctx) - response, cleanup := client.Delete(fmt.Sprintf("/v2/keys/tron/%s", key.ID())) + response, cleanup := client.Delete("/v2/keys/tron/" + key.ID()) t.Cleanup(cleanup) - assert.Equal(t, http.StatusOK, response.StatusCode) - assert.Error(t, utils.JustError(keyStore.Tron().Get(key.ID()))) + require.Equal(t, http.StatusOK, response.StatusCode) + require.Error(t, utils.JustError(keyStore.Tron().Get(key.ID()))) keys, _ = keyStore.Tron().GetAll() - assert.Equal(t, initialLength, len(keys)) + require.Len(t, keys, initialLength) } func setupTronKeysControllerTests(t *testing.T) (cltest.HTTPClientCleaner, keystore.Master) {