Skip to content

Commit

Permalink
chore(all): fix DeepSource reported issues (#2692)
Browse files Browse the repository at this point in the history
- Fix Hidden goroutine GO-E1007
- Fix Using a deprecated function, variable, constant or field SCC-SA1019
- Fix Found `x.Sub(time.Now())` instead of `time.Until(x)` SCC-S1024
- Fix Unused code SCC-U1000
- Fix Simplify slice expression to sliced value itself CRT-A0016
- Fix Control-coupled functions detected RVV-A0005
- Fix Exit inside non-main function detected RVV-A0003
- Fix Redefinition of built-in detected RVV-B0009
- Fix Unused method receiver detected RVV-B0013
- Fix `append` possibly assigns to a wrong variable CRT-D0001
- Fix Unused parameter detected in function RVV-B0012
- Fix Confusing naming of struct fields or methods RVV-B0001
- Fix Importing the same package multiple times GO-W5021
- Rename trie node method `Type()` to `Kind()`
  • Loading branch information
qdm12 committed Jul 26, 2022
1 parent b9449d1 commit 4cfcb42
Show file tree
Hide file tree
Showing 72 changed files with 845 additions and 555 deletions.
11 changes: 2 additions & 9 deletions dot/build_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package dot

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -95,21 +94,15 @@ func BuildFromDB(path string) (*BuildSpec, error) {
tmpGen.Genesis.Raw = make(map[string]map[string]string)
tmpGen.Genesis.Runtime = make(map[string]map[string]interface{})

// BootstrapMailer should not return an error here since there is no URLs to connect to
disabledTelemetry, err := telemetry.BootstrapMailer(context.TODO(), nil, false, nil)
if err != nil {
panic("telemetry should not fail at BuildFromDB function: " + err.Error())
}

config := state.Config{
Path: path,
LogLevel: log.Info,
Telemetry: disabledTelemetry,
Telemetry: telemetry.NewNoopMailer(),
}

stateSrvc := state.NewService(config)

err = stateSrvc.SetupBase()
err := stateSrvc.SetupBase()
if err != nil {
return nil, fmt.Errorf("cannot setup state database: %w", err)
}
Expand Down
12 changes: 10 additions & 2 deletions dot/build_spec_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ const codeHex = "0x3a636f6465"
func TestBuildFromGenesis_Integration(t *testing.T) {
t.Parallel()

file := genesis.CreateTestGenesisJSONFile(t, false)
genesisFields := genesis.Fields{
Raw: map[string]map[string]string{},
Runtime: map[string]map[string]interface{}{
"System": {
"code": "mocktestcode",
},
},
}
file := genesis.CreateTestGenesisJSONFile(t, genesisFields)
bs, err := BuildFromGenesis(file, 0)

const expectedChainType = "TESTCHAINTYPE"
Expand All @@ -43,7 +51,7 @@ func TestBuildFromGenesis_Integration(t *testing.T) {
jGen := genesis.Genesis{}
err = json.Unmarshal(hr, &jGen)
require.NoError(t, err)
genesis.TestGenesis.Genesis = genesis.TestFieldsHR
genesis.TestGenesis.Genesis = genesisFields
require.Equal(t, genesis.TestGenesis.Genesis.Runtime, jGen.Genesis.Runtime)
require.Equal(t, expectedChainType, jGen.ChainType)
require.Equal(t, expectedProperties, jGen.Properties)
Expand Down
10 changes: 9 additions & 1 deletion dot/build_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,15 @@ func TestBuildFromDB(t *testing.T) {
}

func TestBuildFromGenesis(t *testing.T) {
testGenesisPath := genesis.CreateTestGenesisJSONFile(t, false)
genesisFields := genesis.Fields{
Raw: map[string]map[string]string{},
Runtime: map[string]map[string]interface{}{
"System": {
"code": "mocktestcode",
},
},
}
testGenesisPath := genesis.CreateTestGenesisJSONFile(t, genesisFields)

type args struct {
path string
Expand Down
2 changes: 0 additions & 2 deletions dot/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ type Config struct {
// telemetryInterval how often to send telemetry metrics
telemetryInterval time.Duration

noPreAllocate bool // internal option

batchSize int // internal option

// SlotDuration is the slot duration to produce a block
Expand Down
17 changes: 0 additions & 17 deletions dot/network/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@ func (cm *ConnManager) ListenClose(n network.Network, addr ma.Multiaddr) {
"Host %s stopped listening on address %s", n.LocalPeer(), addr)
}

// returns a slice of peers that are unprotected and may be pruned.
func (cm *ConnManager) unprotectedPeers(peers []peer.ID) []peer.ID {
unprot := []peer.ID{}
for _, id := range peers {
if !cm.IsProtected(id, "") && !cm.isPersistent(id) {
unprot = append(unprot, id)
}
}

return unprot
}

// Connected is called when a connection opened
func (cm *ConnManager) Connected(n network.Network, c network.Conn) {
logger.Tracef(
Expand All @@ -141,8 +129,3 @@ func (cm *ConnManager) Disconnected(_ network.Network, c network.Conn) {
cm.disconnectHandler(c.RemotePeer())
}
}

func (cm *ConnManager) isPersistent(p peer.ID) bool {
_, ok := cm.persistentPeers.Load(p)
return ok
}
6 changes: 3 additions & 3 deletions dot/network/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func TestProtectUnprotectPeer(t *testing.T) {
require.True(t, cm.IsProtected(p1, ""))
require.True(t, cm.IsProtected(p2, ""))

unprot := cm.unprotectedPeers([]peer.ID{p1, p2, p3, p4})
unprot := unprotectedPeers(cm, []peer.ID{p1, p2, p3, p4})
require.Equal(t, unprot, []peer.ID{p3, p4})

cm.Unprotect(p1, "")
cm.Unprotect(p2, "")

unprot = cm.unprotectedPeers([]peer.ID{p1, p2, p3, p4})
unprot = unprotectedPeers(cm, []peer.ID{p1, p2, p3, p4})
require.Equal(t, unprot, []peer.ID{p1, p2, p3, p4})
}

Expand Down Expand Up @@ -224,7 +224,7 @@ func TestSetReservedPeer(t *testing.T) {

addrA := nodes[0].host.multiaddrs()[0]
addrB := nodes[1].host.multiaddrs()[0]
addrC := nodes[2].host.addrInfo()
addrC := addrInfo(nodes[2].host)

config := &Config{
BasePath: t.TempDir(),
Expand Down
6 changes: 3 additions & 3 deletions dot/network/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestBeginDiscovery(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
nodeC.noGossip = true

// connect A and B
addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
Expand All @@ -179,7 +179,7 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
require.NoError(t, err)

// connect A and C
addrInfoC := nodeC.host.addrInfo()
addrInfoC := addrInfo(nodeC.host)
err = nodeA.host.connect(addrInfoC)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
Expand Down
4 changes: 2 additions & 2 deletions dot/network/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGossip(t *testing.T) {
handlerB := newTestStreamHandler(testBlockAnnounceMessageDecoder)
nodeB.host.registerStreamHandler(nodeB.host.protocolID, handlerB.handleStream)

addrInfoA := nodeA.host.addrInfo()
addrInfoA := addrInfo(nodeA.host)
err := nodeB.host.connect(addrInfoA)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestGossip(t *testing.T) {
}
require.NoError(t, err)

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err = nodeC.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down
25 changes: 25 additions & 0 deletions dot/network/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,28 @@ func testBlockAnnounceHandshakeDecoder(in []byte, _ peer.ID, _ bool) (Message, e
err := msg.Decode(in)
return msg, err
}

// addrInfo returns the libp2p peer.AddrInfo of the host
func addrInfo(h *host) peer.AddrInfo {
return peer.AddrInfo{
ID: h.p2pHost.ID(),
Addrs: h.p2pHost.Addrs(),
}
}

// returns a slice of peers that are unprotected and may be pruned.
func unprotectedPeers(cm *ConnManager, peers []peer.ID) []peer.ID {
unprot := []peer.ID{}
for _, id := range peers {
if cm.IsProtected(id, "") {
continue
}

_, isPersistent := cm.persistentPeers.Load(id)
if !isPersistent {
unprot = append(unprot, id)
}
}

return unprot
}
8 changes: 0 additions & 8 deletions dot/network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,6 @@ func (h *host) peerCount() int {
return len(peers)
}

// addrInfo returns the libp2p peer.AddrInfo of the host
func (h *host) addrInfo() peer.AddrInfo {
return peer.AddrInfo{
ID: h.p2pHost.ID(),
Addrs: h.p2pHost.Addrs(),
}
}

// multiaddrs returns the multiaddresses of the host
func (h *host) multiaddrs() (multiaddrs []ma.Multiaddr) {
addrs := h.p2pHost.Addrs()
Expand Down
30 changes: 15 additions & 15 deletions dot/network/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestExternalAddrs(t *testing.T) {

node := createTestService(t, config)

addrInfo := node.host.addrInfo()
addrInfo := addrInfo(node.host)

privateIPs, err := newPrivateIPFilters()
require.NoError(t, err)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestExternalAddrsPublicIP(t *testing.T) {
}

node := createTestService(t, config)
addrInfo := node.host.addrInfo()
addrInfo := addrInfo(node.host)

privateIPs, err := newPrivateIPFilters()
require.NoError(t, err)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestExternalAddrsPublicDNS(t *testing.T) {
}

node := createTestService(t, config)
addrInfo := node.host.addrInfo()
addrInfo := addrInfo(node.host)

expected := []ma.Multiaddr{
mustNewMultiAddr("/ip4/127.0.0.1/tcp/7001"),
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestConnect(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestSend(t *testing.T) {
handler := newTestStreamHandler(testBlockRequestMessageDecoder)
nodeB.host.registerStreamHandler(nodeB.host.protocolID, handler.handleStream)

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestExistingStream(t *testing.T) {
handlerA := newTestStreamHandler(testBlockRequestMessageDecoder)
nodeA.host.registerStreamHandler(nodeA.host.protocolID, handlerA.handleStream)

addrInfoA := nodeA.host.addrInfo()
addrInfoA := addrInfo(nodeA.host)
configB := &Config{
BasePath: t.TempDir(),
Port: availablePort(t),
Expand All @@ -257,7 +257,7 @@ func TestExistingStream(t *testing.T) {
handlerB := newTestStreamHandler(testBlockRequestMessageDecoder)
nodeB.host.registerStreamHandler(nodeB.host.protocolID, handlerB.handleStream)

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestStreamCloseMetadataCleanup(t *testing.T) {
handlerB := newTestStreamHandler(testBlockAnnounceHandshakeDecoder)
nodeB.host.registerStreamHandler(blockAnnounceID, handlerB.handleStream)

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -386,7 +386,7 @@ func Test_PeerSupportsProtocol(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -490,15 +490,15 @@ func Test_RemoveReservedPeers(t *testing.T) {
time.Sleep(100 * time.Millisecond)

require.Equal(t, 1, nodeA.host.peerCount())
pID := nodeB.host.addrInfo().ID.String()
pID := addrInfo(nodeB.host).ID.String()

err = nodeA.host.removeReservedPeers(pID)
require.NoError(t, err)

time.Sleep(100 * time.Millisecond)

require.Equal(t, 1, nodeA.host.peerCount())
isProtected := nodeA.host.p2pHost.ConnManager().IsProtected(nodeB.host.addrInfo().ID, "")
isProtected := nodeA.host.p2pHost.ConnManager().IsProtected(addrInfo(nodeB.host).ID, "")
require.False(t, isProtected)

err = nodeA.host.removeReservedPeers("unknown_perr_id")
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestStreamCloseEOF(t *testing.T) {
nodeB.host.registerStreamHandler(nodeB.host.protocolID, handler.handleStream)
require.False(t, handler.exit)

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
err := nodeA.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestPeerConnect(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
nodeA.host.p2pHost.Peerstore().AddAddrs(addrInfoB.ID, addrInfoB.Addrs, peerstore.PermanentAddrTTL)
nodeA.host.cm.peerSetHandler.AddPeer(0, addrInfoB.ID)

Expand Down Expand Up @@ -620,7 +620,7 @@ func TestBannedPeer(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
nodeA.host.p2pHost.Peerstore().AddAddrs(addrInfoB.ID, addrInfoB.Addrs, peerstore.PermanentAddrTTL)
nodeA.host.cm.peerSetHandler.AddPeer(0, addrInfoB.ID)

Expand Down Expand Up @@ -673,7 +673,7 @@ func TestPeerReputation(t *testing.T) {
nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfoB := nodeB.host.addrInfo()
addrInfoB := addrInfo(nodeB.host)
nodeA.host.p2pHost.Peerstore().AddAddrs(addrInfoB.ID, addrInfoB.Addrs, peerstore.PermanentAddrTTL)
nodeA.host.cm.peerSetHandler.AddPeer(0, addrInfoB.ID)

Expand Down
2 changes: 1 addition & 1 deletion dot/network/light_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestHandleLightMessage_Response(t *testing.T) {
}
b := createTestService(t, configB)

addrInfoB := b.host.addrInfo()
addrInfoB := addrInfo(b.host)
err := s.host.connect(addrInfoB)
// retry connect if "failed to dial" error
if failedToDial(err) {
Expand Down
Loading

0 comments on commit 4cfcb42

Please sign in to comment.