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

fix(ci): fix all Deepsource issues #3046

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion dot/rpc/json2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *CodecRequest) WriteError(w http.ResponseWriter, _ int, err error) {
c.writeServerResponse(w, res)
}

func (c CodecRequest) tryToMapIfNotAnErrorAlready(err error) error {
func (c *CodecRequest) tryToMapIfNotAnErrorAlready(err error) error {
if _, ok := err.(*json2.Error); ok || c.errorMapper == nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
Hash: &common.Hash{},
},
},
exp: []string{},
expErr: errors.New("GetStorageChild error"),
},
{
Expand All @@ -138,7 +137,6 @@ func TestChildStateModule_GetKeys(t *testing.T) {
Key: []byte(":child_storage_key"),
},
},
exp: []string{},
expErr: errors.New("GetStateRootFromBlock error"),
},
}
Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions dot/rpc/modules/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions dot/types/babe_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func NewBabePrimaryPreDigest(authorityIndex uint32,
}

// ToPreRuntimeDigest returns the BabePrimaryPreDigest as a PreRuntimeDigest
func (d *BabePrimaryPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(*d)
func (d BabePrimaryPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(d)
}

// Index returns VDT index
Expand All @@ -85,8 +85,8 @@ func NewBabeSecondaryPlainPreDigest(authorityIndex uint32, slotNumber uint64) *B
}

// ToPreRuntimeDigest returns the BabeSecondaryPlainPreDigest as a PreRuntimeDigest
func (d *BabeSecondaryPlainPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(*d)
func (d BabeSecondaryPlainPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(d)
}

// Index returns VDT index
Expand Down Expand Up @@ -118,8 +118,8 @@ func NewBabeSecondaryVRFPreDigest(authorityIndex uint32,
}

// ToPreRuntimeDigest returns the BabeSecondaryVRFPreDigest as a PreRuntimeDigest
func (d *BabeSecondaryVRFPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(*d)
func (d BabeSecondaryVRFPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
return toPreRuntimeDigest(d)
}

// Index returns VDT index
Expand Down
14 changes: 6 additions & 8 deletions dot/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions dot/types/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package types

import (
"fmt"
"testing"

"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions dot/types/consensus_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ type NextEpochData struct {
}

// Index returns VDT index
func (NextEpochData) Index() uint { return 1 }
func (NextEpochData) Index() uint { return 1 } //skipcq: GO-W1029

func (d NextEpochData) String() string {
func (d NextEpochData) String() string { //skipcq: GO-W1029
return fmt.Sprintf("NextEpochData Authorities=%v Randomness=%v", d.Authorities, d.Randomness)
}

// ToEpochData returns the NextEpochData as EpochData
func (d *NextEpochData) ToEpochData() (*EpochData, error) {
func (d *NextEpochData) ToEpochData() (*EpochData, error) { //skipcq: GO-W1029
auths, err := BABEAuthorityRawToAuthority(d.Authorities)
if err != nil {
return nil, err
Expand Down Expand Up @@ -135,15 +135,15 @@ type NextConfigData struct {
}

// Index returns VDT index
func (NextConfigData) Index() uint { return 3 }
func (NextConfigData) Index() uint { return 3 } //skipcq: GO-W1029

func (d NextConfigData) String() string {
func (d NextConfigData) String() string { //skipcq: GO-W1029
return fmt.Sprintf("NextConfigData{C1=%d, C2=%d, SecondarySlots=%d}",
d.C1, d.C2, d.SecondarySlots)
}

// ToConfigData returns the NextConfigData as ConfigData
func (d *NextConfigData) ToConfigData() *ConfigData {
func (d *NextConfigData) ToConfigData() *ConfigData { //skipcq: GO-W1029
return &ConfigData{
C1: d.C1,
C2: d.C2,
Expand Down
2 changes: 1 addition & 1 deletion dot/types/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dot/types/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dot/types/inherents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pprof/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *Settings) setDefaults() {
}
}

func (s Settings) String() string {
func (s *Settings) String() string {
return fmt.Sprintf(
"listening on %s and setting block profile rate to %d, mutex profile rate to %d",
s.ListeningAddress, s.BlockProfileRate, s.MutexProfileRate)
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type CopySettings struct {
// Copy deep copies the node.
// Setting copyChildren to true will deep copy
// children as well.
func (n *Node) Copy(settings CopySettings) *Node { //skipcq: GO-W1029
func (n *Node) Copy(settings CopySettings) *Node {
cpy := &Node{
Dirty: n.Dirty,
Generation: n.Generation,
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (n *Node) String() string {
}

// StringNode returns a gotree compatible node for String methods.
func (n Node) StringNode() (stringNode *gotree.Node) {
func (n *Node) StringNode() (stringNode *gotree.Node) {
caser := cases.Title(language.BritishEnglish)
stringNode = gotree.New(caser.String(n.Kind().String()))
stringNode.Appendf("Generation: %d", n.Generation)
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/subvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "bytes"
// StorageValueEqual returns true if the node storage value is equal to the
// storage value given as argument. In particular, it returns false
// if one storage value is nil and the other storage value is the empty slice.
func (n Node) StorageValueEqual(stoageValue []byte) (equal bool) {
func (n *Node) StorageValueEqual(stoageValue []byte) (equal bool) {
if len(stoageValue) == 0 && len(n.StorageValue) == 0 {
return (stoageValue == nil && n.StorageValue == nil) ||
(stoageValue != nil && n.StorageValue != nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/blocktree/leaves.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/blocktree/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions lib/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions lib/common/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewHash(in []byte) (res Hash) {
}

// ToBytes turns a hash to a byte array
func (h Hash) ToBytes() []byte {
func (h Hash) ToBytes() []byte { //skipcq: GO-W1029
b := [32]byte(h)
return b[:]
}
Expand All @@ -49,24 +49,24 @@ func HashValidator(field reflect.Value) interface{} {
}

// IsEmpty returns true if the hash is empty, false otherwise.
func (h Hash) IsEmpty() bool {
func (h Hash) IsEmpty() bool { //skipcq: GO-W1029
return h == Hash{}
}

// String returns the hex string for the hash
func (h Hash) String() string {
func (h Hash) String() string { //skipcq: GO-W1029
return fmt.Sprintf("0x%x", h[:])
}

// Short returns the first 4 bytes and the last 4 bytes of the hex string for the hash
func (h Hash) Short() string {
func (h Hash) Short() string { //skipcq: GO-W1029
const nBytes = 4
return fmt.Sprintf("0x%x...%x", h[:nBytes], h[len(h)-nBytes:])
}

// SetBytes sets the hash to the value of b.
// If b is larger than len(h), b will be cropped from the left.
func (h *Hash) SetBytes(b []byte) {
func (h *Hash) SetBytes(b []byte) { //skipcq: GO-W1029
if len(b) > len(h) {
b = b[len(b)-HashLength:]
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func BytesToHash(b []byte) Hash {
}

// UnmarshalJSON converts hex data to hash
func (h *Hash) UnmarshalJSON(data []byte) error {
func (h *Hash) UnmarshalJSON(data []byte) error { //skipcq: GO-W1029
trimmedData := strings.Trim(string(data), "\"")
if len(trimmedData) < 2 {
return errors.New("invalid hash format")
Expand All @@ -109,7 +109,7 @@ func (h *Hash) UnmarshalJSON(data []byte) error {
}

// MarshalJSON converts hash to hex data
func (h Hash) MarshalJSON() ([]byte, error) {
func (h Hash) MarshalJSON() ([]byte, error) { //skipcq: GO-W1029
return json.Marshal(h.String())
}

Expand Down
6 changes: 3 additions & 3 deletions lib/common/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions lib/keystore/basic_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading