Skip to content

Commit

Permalink
Fix Deepsource error.
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Apr 12, 2021
1 parent 271f92e commit 5be799b
Show file tree
Hide file tree
Showing 45 changed files with 106 additions and 137 deletions.
2 changes: 1 addition & 1 deletion cmd/gossamer/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func getKeystorePassword(ctx *cli.Context) []byte {
// unlockKeystore compares the length of passwords to the length of accounts,
// prompts the user for a password if no password is provided, and then unlocks
// the accounts within the provided keystore
func unlockKeystore(ks keystore.Keystore, basepath string, unlock string, password string) error {
func unlockKeystore(ks keystore.Keystore, basepath, unlock, password string) error {
var passwords []string

if password != "" {
Expand Down
3 changes: 2 additions & 1 deletion cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ func buildSpecAction(ctx *cli.Context) error {
if bs == nil {
return fmt.Errorf("error building genesis")
}
res := []byte{} //nolint

var res []byte
if ctx.Bool(RawFlag.Name) {
res, err = bs.ToJSONRaw()
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func confirmMessage(msg string) bool {
fmt.Print("> ")
for {
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
text = strings.ReplaceAll(text, "\n", "")
return strings.Compare("Y", text) == 0
}
}
Expand Down
29 changes: 28 additions & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package core

import (
"io"
"math/big"
"os"
"sort"
Expand All @@ -36,6 +37,32 @@ import (
"github.com/stretchr/testify/require"
)

type mockDigestItem struct {
i int
}

func newMockDigestItem(i int) *mockDigestItem {
return &mockDigestItem{
i: i,
}
}

func (d *mockDigestItem) String() string {
return ""
}

func (d *mockDigestItem) Type() byte {
return byte(d.i)
}

func (d *mockDigestItem) Encode() ([]byte, error) {
return []byte{byte(d.i)}, nil
}

func (d *mockDigestItem) Decode(_ io.Reader) error {
return nil
}

func addTestBlocksToState(t *testing.T, depth int, blockState BlockState) []*types.Header {
return addTestBlocksToStateWithParent(t, blockState.BestBlockHash(), depth, blockState)
}
Expand Down Expand Up @@ -374,7 +401,7 @@ func TestService_HandleSubmittedExtrinsic(t *testing.T) {
s := NewTestService(t, nil)

parentHash := common.MustHexToHash("0x35a28a7dbaf0ba07d1485b0f3da7757e3880509edc8c31d0850cb6dd6219361d")
header, err := types.NewHeader(parentHash, big.NewInt(1), common.Hash{}, common.Hash{}, types.NewEmptyDigest())
header, err := types.NewHeader(parentHash, common.Hash{}, common.Hash{}, big.NewInt(1), types.NewEmptyDigest())
require.NoError(t, err)

//initialize block header
Expand Down
29 changes: 1 addition & 28 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package core

import (
"io"
"io/ioutil"
"math/big"
"testing"
Expand Down Expand Up @@ -51,7 +50,7 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{})
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)
return gen, genTrie, genesisHeader
}
Expand Down Expand Up @@ -263,32 +262,6 @@ func (s *mockSyncer) IsSynced() bool {

func (s *mockSyncer) SetSyncing(bool) {}

type mockDigestItem struct { //nolint
i int
}

func newMockDigestItem(i int) *mockDigestItem { //nolint
return &mockDigestItem{
i: i,
}
}

func (d *mockDigestItem) String() string { //nolint
return ""
}

func (d *mockDigestItem) Type() byte { //nolint
return byte(d.i)
}

func (d *mockDigestItem) Encode() ([]byte, error) { //nolint
return []byte{byte(d.i)}, nil
}

func (d *mockDigestItem) Decode(_ io.Reader) error { //nolint
return nil
}

type mockTransactionHandler struct{}

func (h *mockTransactionHandler) HandleTransactionMessage(_ *network.TransactionMessage) error {
Expand Down
8 changes: 1 addition & 7 deletions dot/network/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,7 @@ func (q *syncQueue) handleBlockAnnounceHandshake(blockNum uint32, from peer.ID)
func (q *syncQueue) handleBlockAnnounce(msg *BlockAnnounceMessage, from peer.ID) {
q.updatePeerScore(from, 1)

header, err := types.NewHeader(
msg.ParentHash,
msg.Number,
msg.StateRoot,
msg.ExtrinsicsRoot,
msg.Digest,
)
header, err := types.NewHeader(msg.ParentHash, msg.StateRoot, msg.ExtrinsicsRoot, msg.Number, msg.Digest)
if err != nil {
logger.Error("failed to create header from BlockAnnounce", "error", err)
return
Expand Down
4 changes: 2 additions & 2 deletions dot/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestInitNode_LoadGenesisData(t *testing.T) {
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{})
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)

err = stateSrvc.Initialize(gen, genesisHeader, genTrie)
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestInitNode_LoadGenesisData(t *testing.T) {
require.NoError(t, err)

stateRoot := genesisHeader.StateRoot
expectedHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), stateRoot, trie.EmptyHash, types.NewEmptyDigest())
expectedHeader, err := types.NewHeader(common.NewHash([]byte{0}), stateRoot, trie.EmptyHash, big.NewInt(0), types.NewEmptyDigest())
require.NoError(t, err)
require.Equal(t, expectedHeader.Hash(), genesisHeader.Hash())
}
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func LocalhostFilter() *ipfilter.IPFilter {

// LocalRequestOnly HTTP handler to restrict to only local connections
func LocalRequestOnly(r *rpc.RequestInfo, i interface{}) error {
ip, _, error := net.SplitHostPort(r.Request.RemoteAddr)
if error != nil {
ip, _, err := net.SplitHostPort(r.Request.RemoteAddr)
if err != nil {
return errors.New("unable to parse IP")
}
f := LocalhostFilter()
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (cm *ChainModule) unwindRequest(req interface{}) ([]string, error) {
if err != nil {
return nil, err
}
res = append(res, u[:]...)
res = append(res, u...)
}
case interface{}:
h, err := cm.lookupHashByInterface(x)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func newTestGenesisWithTrieAndHeader() (*genesis.Genesis, *trie.Trie, *types.Hea
panic(err)
}

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{}) //nolint
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{}) //nolint
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (sm *StateModule) GetChildStorageSize(r *http.Request, req *StateChildStora

// GetKeysPaged Returns the keys with prefix with pagination support.
func (sm *StateModule) GetKeysPaged(r *http.Request, req *StateStorageKeyRequest, res *StateStorageKeysResponse) error {
if len(req.Prefix) == 0 {
if req.Prefix == "" {
req.Prefix = "0x"
}
hPrefix, err := common.HexToBytes(req.Prefix)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (sm *SystemModule) NodeRoles(r *http.Request, req *EmptyRequest, res *[]int

// AccountNextIndex Returns the next valid index (aka. nonce) for given account.
func (sm *SystemModule) AccountNextIndex(r *http.Request, req *StringRequest, res *U64Response) error {
if req == nil || len(req.String) == 0 {
if req == nil || req.String == "" {
return errors.New("account address must be valid")
}
addressPubKey := crypto.PublicAddressToByteArray(common.Address(req.String))
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/subscription/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *WSConn) HandleComm() {
logger.Warn("websocket failed to read message", "error", err)
return
}
logger.Debug("websocket received", "message", fmt.Sprintf("%s", mbytes))
logger.Debug("websocket received", "message", mbytes)

// determine if request is for subscribe method type
var msg map[string]interface{}
Expand Down
7 changes: 2 additions & 5 deletions dot/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,8 @@ func (s *Service) Initialize(gen *genesis.Genesis, header *types.Header, t *trie
s.Storage = storageState
s.Block = blockState
s.Epoch = epochState
} else {
// close database
if err = db.Close(); err != nil {
return fmt.Errorf("failed to close database: %s", err)
}
} else if err = db.Close(); err != nil {
return fmt.Errorf("failed to close database: %s", err)
}

logger.Info("state", "genesis hash", blockState.genesisHash)
Expand Down
4 changes: 2 additions & 2 deletions dot/state/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{})
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)
return gen, genTrie, genesisHeader
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestService_Initialize(t *testing.T) {
err := state.Initialize(genData, genesisHeader, genTrie)
require.NoError(t, err)

genesisHeader, err = types.NewHeader(common.NewHash([]byte{77}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{})
genesisHeader, err = types.NewHeader(common.NewHash([]byte{77}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)

err = state.Initialize(genData, genesisHeader, genTrie)
Expand Down
8 changes: 1 addition & 7 deletions dot/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ func (s *Service) HandleBlockAnnounce(msg *network.BlockAnnounceMessage) error {
logger.Debug("received BlockAnnounceMessage")

// create header from message
header, err := types.NewHeader(
msg.ParentHash,
msg.Number,
msg.StateRoot,
msg.ExtrinsicsRoot,
msg.Digest,
)
header, err := types.NewHeader(msg.ParentHash, msg.StateRoot, msg.ExtrinsicsRoot, msg.Number, msg.Digest)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{})
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)
return gen, genTrie, genesisHeader
}
Expand Down
4 changes: 2 additions & 2 deletions dot/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func TestDeepCopyBlock(t *testing.T) {
}

func TestMustEncodeBlock(t *testing.T) {
h1, err := NewHeader(common.Hash{}, big.NewInt(0), common.Hash{}, common.Hash{}, Digest{})
h1, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), Digest{})
require.NoError(t, err)
b1 := NewBlock(h1, NewBody([]byte{}))
enc, err := b1.Encode()
require.NoError(t, err)

h2, err := NewHeader(common.Hash{0x1, 0x2}, big.NewInt(0), common.Hash{}, common.Hash{}, Digest{})
h2, err := NewHeader(common.Hash{0x1, 0x2}, common.Hash{}, common.Hash{}, big.NewInt(0), Digest{})
require.NoError(t, err)
b2 := NewBlock(h2, NewBody([]byte{0xa, 0xb}))
enc2, err := b2.Encode()
Expand Down
2 changes: 1 addition & 1 deletion dot/types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Header struct {
}

// NewHeader creates a new block header and sets its hash field
func NewHeader(parentHash common.Hash, number *big.Int, stateRoot common.Hash, extrinsicsRoot common.Hash, digest []DigestItem) (*Header, error) {
func NewHeader(parentHash, stateRoot, extrinsicsRoot common.Hash, number *big.Int, digest []DigestItem) (*Header, error) {
if number == nil {
// Hash() will panic if number is nil
return nil, errors.New("cannot have nil block number")
Expand Down
6 changes: 3 additions & 3 deletions dot/types/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func TestDecodeHeader(t *testing.T) {
header, err := NewHeader(common.Hash{}, big.NewInt(0), common.Hash{}, common.Hash{}, Digest{})
header, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), Digest{})
require.NoError(t, err)

enc, err := header.Encode()
Expand All @@ -42,7 +42,7 @@ func TestDecodeHeader(t *testing.T) {
}

func TestMustEncodeHeader(t *testing.T) {
bh1, err := NewHeader(common.Hash{}, big.NewInt(0), common.Hash{}, common.Hash{}, Digest{})
bh1, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), Digest{})
require.NoError(t, err)
enc, err := bh1.Encode()
require.NoError(t, err)
Expand All @@ -58,7 +58,7 @@ func TestMustEncodeHeader(t *testing.T) {
},
}

bh2, err := NewHeader(common.Hash{}, big.NewInt(0), common.Hash{}, common.Hash{}, testDigest)
bh2, err := NewHeader(common.Hash{}, common.Hash{}, common.Hash{}, big.NewInt(0), testDigest)
require.NoError(t, err)
enc2, err := bh2.Encode()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/babe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (*genesis.Genesis, *trie.Trie
genTrie, err := genesis.NewTrieFromGenesis(gen)
require.NoError(t, err)

genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), big.NewInt(0), genTrie.MustHash(), trie.EmptyHash, types.Digest{}) //nolint
genesisHeader, err := types.NewHeader(common.NewHash([]byte{0}), genTrie.MustHash(), trie.EmptyHash, big.NewInt(0), types.Digest{}) //nolint
require.NoError(t, err)
return gen, genTrie, genesisHeader
}
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *Service) buildBlock(parent *types.Header, slot Slot) (*types.Block, err

// create new block header
number := big.NewInt(0).Add(parent.Number, big.NewInt(1))
header, err := types.NewHeader(parent.Hash(), number, common.Hash{}, common.Hash{}, types.NewEmptyDigest())
header, err := types.NewHeader(parent.Hash(), common.Hash{}, common.Hash{}, number, types.NewEmptyDigest())
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions lib/babe/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSeal(t *testing.T) {
zeroHash, err := common.HexToHash("0x00")
require.NoError(t, err)

header, err := types.NewHeader(zeroHash, big.NewInt(0), zeroHash, zeroHash, types.Digest{})
header, err := types.NewHeader(zeroHash, zeroHash, zeroHash, big.NewInt(0), types.Digest{})
require.NoError(t, err)

encHeader, err := header.Encode()
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestApplyExtrinsic(t *testing.T) {
babeService.epochData.threshold = maxThreshold

parentHash := common.MustHexToHash("0x35a28a7dbaf0ba07d1485b0f3da7757e3880509edc8c31d0850cb6dd6219361d")
header, err := types.NewHeader(parentHash, big.NewInt(1), common.Hash{}, common.Hash{}, types.NewEmptyDigest())
header, err := types.NewHeader(parentHash, common.Hash{}, common.Hash{}, big.NewInt(1), types.NewEmptyDigest())
require.NoError(t, err)

//initialize block header
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestBuildAndApplyExtrinsic(t *testing.T) {
babeService.epochData.threshold = maxThreshold

parentHash := common.MustHexToHash("0x35a28a7dbaf0ba07d1485b0f3da7757e3880509edc8c31d0850cb6dd6219361d")
header, err := types.NewHeader(parentHash, big.NewInt(1), common.Hash{}, common.Hash{}, types.NewEmptyDigest())
header, err := types.NewHeader(parentHash, common.Hash{}, common.Hash{}, big.NewInt(1), types.NewEmptyDigest())
require.NoError(t, err)

//initialize block header
Expand Down
4 changes: 2 additions & 2 deletions lib/babe/median.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (b *Service) getCurrentSlot() (uint64, error) {

// slotTime calculates the slot time in the form of seconds since the unix epoch
// for a given slot in seconds, returns 0 and an error if it can't be calculated
func (b *Service) slotTime(slot uint64, slotTail uint64) (uint64, error) {
func (b *Service) slotTime(slot, slotTail uint64) (uint64, error) {
var at []uint64

head := b.blockState.BestBlockHash()
Expand Down Expand Up @@ -162,7 +162,7 @@ func median(l []uint64) (uint64, error) {
}

// slotOffset returns the number of slots between slot
func slotOffset(start uint64, end uint64) (uint64, error) {
func slotOffset(start, end uint64) (uint64, error) {
os := end - start
if end < start {
return 0, errors.New("cannot have negative Slot Offset")
Expand Down
Loading

0 comments on commit 5be799b

Please sign in to comment.