diff --git a/dot/rpc/modules/childstate_test.go b/dot/rpc/modules/childstate_test.go index 8bcb7aba455..1fb7c90f53e 100644 --- a/dot/rpc/modules/childstate_test.go +++ b/dot/rpc/modules/childstate_test.go @@ -124,7 +124,6 @@ func TestChildStateModule_GetKeys(t *testing.T) { Hash: &common.Hash{}, }, }, - exp: []string{}, expErr: errors.New("GetStorageChild error"), }, { @@ -138,7 +137,6 @@ func TestChildStateModule_GetKeys(t *testing.T) { Key: []byte(":child_storage_key"), }, }, - exp: []string{}, expErr: errors.New("GetStateRootFromBlock error"), }, } @@ -148,7 +146,7 @@ func TestChildStateModule_GetKeys(t *testing.T) { storageAPI: tt.fields.storageAPI, blockAPI: tt.fields.blockAPI, } - res := []string{} + var res []string err := cs.GetKeys(tt.args.in0, tt.args.req, &res) if tt.expErr != nil { assert.EqualError(t, err, tt.expErr.Error()) diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index a950e1362b7..0788c9d3809 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -446,14 +446,13 @@ func TestSystemModule_LocalListenAddresses(t *testing.T) { args: args{ req: &EmptyRequest{}, }, - exp: []string{}, expErr: errors.New("multiaddress list is empty"), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { sm := tt.sysModule - res := []string{} + var res []string err := sm.LocalListenAddresses(tt.args.r, tt.args.req, &res) if tt.expErr != nil { assert.EqualError(t, err, tt.expErr.Error()) diff --git a/dot/types/body.go b/dot/types/body.go index 95c0dc477da..134ac8dd3e2 100644 --- a/dot/types/body.go +++ b/dot/types/body.go @@ -24,12 +24,11 @@ func NewBody(e []Extrinsic) *Body { // NewBodyFromBytes returns a Body from a SCALE encoded byte array. func NewBodyFromBytes(b []byte) (*Body, error) { - exts := [][]byte{} - if len(b) == 0 { return NewBody([]Extrinsic{}), nil } + var exts [][]byte err := scale.Unmarshal(b, &exts) if err != nil { return nil, err @@ -59,16 +58,15 @@ func NewBodyFromEncodedBytes(exts [][]byte) (*Body, error) { // NewBodyFromExtrinsicStrings creates a block body given an array of hex-encoded // 0x-prefixed strings. -func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) { - exts := []Extrinsic{} - for _, s := range ss { - b, err := common.HexToBytes(s) +func NewBodyFromExtrinsicStrings(ss []string) (body *Body, err error) { + exts := make([]Extrinsic, len(ss)) + for i, s := range ss { + exts[i], err = common.HexToBytes(s) if errors.Is(err, common.ErrNoPrefix) { - b = []byte(s) + exts[i] = []byte(s) } else if err != nil { return nil, err } - exts = append(exts, b) } return NewBody(exts), nil diff --git a/dot/types/body_test.go b/dot/types/body_test.go index d27f9f8048d..42bc894fed3 100644 --- a/dot/types/body_test.go +++ b/dot/types/body_test.go @@ -4,7 +4,6 @@ package types import ( - "fmt" "testing" "github.com/ChainSafe/gossamer/lib/common" @@ -48,14 +47,11 @@ func TestBodyFromEncodedBytes(t *testing.T) { } func TestBodyFromExtrinsicStrings(t *testing.T) { - extStrings := []string{} - - for _, ext := range exts { - extStrings = append(extStrings, common.BytesToHex(ext)) + extStrings := make([]string, len(exts)) + for i := range exts { + extStrings[i] = common.BytesToHex(exts[i]) } - fmt.Println(extStrings) - bodyFromByteExtrinsics := NewBody(exts) bodyFromStringExtrinsics, err := NewBodyFromExtrinsicStrings(extStrings) require.NoError(t, err) diff --git a/dot/types/grandpa.go b/dot/types/grandpa.go index bcec20b49ee..0dd6067b4c3 100644 --- a/dot/types/grandpa.go +++ b/dot/types/grandpa.go @@ -149,7 +149,7 @@ func EncodeGrandpaVoters(voters GrandpaVoters) ([]byte, error) { // DecodeGrandpaVoters returns a decoded GrandpaVoters func DecodeGrandpaVoters(in []byte) (GrandpaVoters, error) { - dec := []voter{} + var dec []voter err := scale.Unmarshal(in, &dec) if err != nil { return nil, err diff --git a/dot/types/grandpa_test.go b/dot/types/grandpa_test.go index 16f87cb6692..1c3184ec6f1 100644 --- a/dot/types/grandpa_test.go +++ b/dot/types/grandpa_test.go @@ -68,7 +68,7 @@ func TestGrandpaAuthoritiesRawToAuthorities(t *testing.T) { require.NoError(t, err) require.Equal(t, exp, enc) - dec := []GrandpaAuthoritiesRaw{} + var dec []GrandpaAuthoritiesRaw err = scale.Unmarshal(enc, &dec) require.NoError(t, err) require.Equal(t, auths, dec) diff --git a/dot/types/inherents.go b/dot/types/inherents.go index ab13b8e07e2..36f3655e3c1 100644 --- a/dot/types/inherents.go +++ b/dot/types/inherents.go @@ -98,7 +98,7 @@ func (d *InherentData) Encode() ([]byte, error) { return nil, err } - keys := [][8]byte{} + keys := make([][8]byte, 0, len(d.Data)) for key := range d.Data { keys = append(keys, key) } diff --git a/lib/blocktree/leaves.go b/lib/blocktree/leaves.go index 3ef6afee955..549bb07cb8f 100644 --- a/lib/blocktree/leaves.go +++ b/lib/blocktree/leaves.go @@ -111,7 +111,7 @@ func (lm *leafMap) nodes() []*node { lm.RLock() defer lm.RUnlock() - nodes := []*node{} + var nodes []*node lm.smap.Range(func(h, n interface{}) bool { node := n.(*node) diff --git a/lib/blocktree/node_test.go b/lib/blocktree/node_test.go index 4cfbf4596b5..48678c8269a 100644 --- a/lib/blocktree/node_test.go +++ b/lib/blocktree/node_test.go @@ -23,7 +23,7 @@ func TestNode_GetLeaves(t *testing.T) { testNode := bt.getNode(branches[0].hash).children[0] leaves := testNode.getLeaves(nil) - expected := []*node{} + var expected []*node for _, lf := range bt.leaves.toMap() { if lf.isDescendantOf(testNode) { expected = append(expected, lf) diff --git a/lib/common/common.go b/lib/common/common.go index 0cce0a405ce..4500046545b 100644 --- a/lib/common/common.go +++ b/lib/common/common.go @@ -17,9 +17,8 @@ import ( var ErrNoPrefix = errors.New("could not byteify non 0x prefixed string") // StringToInts turns a string consisting of ints separated by commas into an int array -func StringToInts(in string) ([]int, error) { +func StringToInts(in string) (res []int, err error) { intstrs := strings.Split(in, ",") - res := []int{} for _, intstr := range intstrs { i, err := strconv.Atoi(intstr) if err != nil { @@ -32,18 +31,18 @@ func StringToInts(in string) ([]int, error) { // StringArrayToBytes turns an array of strings into an array of byte arrays func StringArrayToBytes(in []string) [][]byte { - b := [][]byte{} - for _, str := range in { - b = append(b, []byte(str)) + b := make([][]byte, len(in)) + for i := range in { + b[i] = []byte(in[i]) } return b } // BytesToStringArray turns an array of byte arrays into an array strings func BytesToStringArray(in [][]byte) []string { - strs := []string{} - for _, b := range in { - strs = append(strs, string(b)) + strs := make([]string, len(in)) + for i := range in { + strs[i] = string(in[i]) } return strs } diff --git a/lib/common/hasher_test.go b/lib/common/hasher_test.go index b1ae2123278..32c44c73eb4 100644 --- a/lib/common/hasher_test.go +++ b/lib/common/hasher_test.go @@ -14,7 +14,7 @@ import ( func TestBlake2b218_EmptyHash(t *testing.T) { // test case from https://github.com/noot/blake2b_test which uses the blake2-rfp rust crate // also see https://github.com/paritytech/substrate/blob/master/core/primitives/src/hashing.rs - in := []byte{} + var in []byte h, err := common.Blake2b128(in) require.NoError(t, err) @@ -42,7 +42,7 @@ func Test_MustBlake2b8(t *testing.T) { func TestBlake2bHash_EmptyHash(t *testing.T) { // test case from https://github.com/noot/blake2b_test which uses the blake2-rfp rust crate // also see https://github.com/paritytech/substrate/blob/master/core/primitives/src/hashing.rs - in := []byte{} + var in []byte h, err := common.Blake2bHash(in) require.NoError(t, err) @@ -53,7 +53,7 @@ func TestBlake2bHash_EmptyHash(t *testing.T) { func TestKeccak256_EmptyHash(t *testing.T) { // test case from https://github.com/debris/tiny-keccak/blob/master/tests/keccak.rs#L4 - in := []byte{} + var in []byte h, err := common.Keccak256(in) require.NoError(t, err) diff --git a/lib/keystore/basic_keystore.go b/lib/keystore/basic_keystore.go index fc670b6f767..a5ed943ef76 100644 --- a/lib/keystore/basic_keystore.go +++ b/lib/keystore/basic_keystore.go @@ -82,8 +82,7 @@ func (ks *BasicKeystore) GetKeypairFromAddress(pub common.Address) KeyPair { } // PublicKeys returns all public keys in the keystore -func (ks *BasicKeystore) PublicKeys() []crypto.PublicKey { - srkeys := []crypto.PublicKey{} +func (ks *BasicKeystore) PublicKeys() (srkeys []crypto.PublicKey) { if ks.keys == nil { return srkeys } diff --git a/lib/keystore/basic_keystore_test.go b/lib/keystore/basic_keystore_test.go index 6013826eeb7..73bd2bb98b8 100644 --- a/lib/keystore/basic_keystore_test.go +++ b/lib/keystore/basic_keystore_test.go @@ -35,7 +35,7 @@ func TestBasicKeystore(t *testing.T) { func TestBasicKeystore_PublicKeys(t *testing.T) { ks := NewBasicKeystore("test", crypto.Sr25519Type) - expectedPubkeys := []crypto.PublicKey{} + var expectedPubkeys []crypto.PublicKey numKps := 12 for i := 0; i < numKps; i++ { diff --git a/lib/keystore/generic_keystore.go b/lib/keystore/generic_keystore.go index 3f4f12e08a6..8548b1ab5b0 100644 --- a/lib/keystore/generic_keystore.go +++ b/lib/keystore/generic_keystore.go @@ -73,8 +73,7 @@ func (ks *GenericKeystore) GetKeypairFromAddress(pub common.Address) KeyPair { } // PublicKeys returns all public keys in the keystore -func (ks *GenericKeystore) PublicKeys() []crypto.PublicKey { - srkeys := []crypto.PublicKey{} +func (ks *GenericKeystore) PublicKeys() (srkeys []crypto.PublicKey) { if ks.keys == nil { return srkeys } @@ -109,8 +108,7 @@ func (ks *GenericKeystore) NumEd25519Keys() int { } // Ed25519PublicKeys keys -func (ks *GenericKeystore) Ed25519PublicKeys() []crypto.PublicKey { - edkeys := []crypto.PublicKey{} +func (ks *GenericKeystore) Ed25519PublicKeys() (edkeys []crypto.PublicKey) { if ks.keys == nil { return edkeys } @@ -138,8 +136,7 @@ func (ks *GenericKeystore) Ed25519Keypairs() (edkeys []KeyPair) { } // Sr25519PublicKeys PublicKey -func (ks *GenericKeystore) Sr25519PublicKeys() []crypto.PublicKey { - srkeys := []crypto.PublicKey{} +func (ks *GenericKeystore) Sr25519PublicKeys() (srkeys []crypto.PublicKey) { if ks.keys == nil { return srkeys } @@ -167,8 +164,7 @@ func (ks *GenericKeystore) Sr25519Keypairs() (srkeys []KeyPair) { } // Secp256k1PublicKeys PublicKey -func (ks *GenericKeystore) Secp256k1PublicKeys() []crypto.PublicKey { - sckeys := []crypto.PublicKey{} +func (ks *GenericKeystore) Secp256k1PublicKeys() (sckeys []crypto.PublicKey) { if ks.keys == nil { return sckeys } diff --git a/lib/keystore/generic_keystore_test.go b/lib/keystore/generic_keystore_test.go index 33029d3e42b..a55f72b97a8 100644 --- a/lib/keystore/generic_keystore_test.go +++ b/lib/keystore/generic_keystore_test.go @@ -18,7 +18,7 @@ import ( func TestGetSr25519PublicKeys(t *testing.T) { ks := NewGenericKeystore("test") - expectedPubkeys := []crypto.PublicKey{} + var expectedPubkeys []crypto.PublicKey numKps := 12 for i := 0; i < numKps; i++ { @@ -54,7 +54,7 @@ func TestGetSr25519PublicKeys(t *testing.T) { func TestGetEd25519PublicKeys(t *testing.T) { ks := NewGenericKeystore("test") - expectedPubkeys := []crypto.PublicKey{} + var expectedPubkeys []crypto.PublicKey numKps := 10 for i := 0; i < numKps; i++ { @@ -90,7 +90,7 @@ func TestGetEd25519PublicKeys(t *testing.T) { func TestGetSecp256k1PublicKeys(t *testing.T) { ks := NewGenericKeystore("test") - expectedPubkeys := []crypto.PublicKey{} + var expectedPubkeys []crypto.PublicKey numKps := 10 for i := 0; i < numKps; i++ { diff --git a/lib/keystore/helpers_test.go b/lib/keystore/helpers_test.go index 877cfe1ae5e..9efc0cbcb9e 100644 --- a/lib/keystore/helpers_test.go +++ b/lib/keystore/helpers_test.go @@ -217,7 +217,7 @@ func TestImportKey(t *testing.T) { func TestListKeys(t *testing.T) { testdir := t.TempDir() - expected := []string{} + var expected []string for i := 0; i < 5; i++ { var err error diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index 0c90002d165..0d1419c26f5 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -1865,8 +1865,7 @@ func Test_ext_trie_blake2_256_verify_proof_version_1(t *testing.T) { hashEnc, err := scale.Marshal(testcase.root) require.NoError(t, err) - args := []byte{} - args = append(args, hashEnc...) + args := hashEnc encProof, err := scale.Marshal(testcase.proof) require.NoError(t, err)