Skip to content

Commit 4cfcb42

Browse files
authored
chore(all): fix DeepSource reported issues (#2692)
- 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()`
1 parent b9449d1 commit 4cfcb42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+845
-555
lines changed

dot/build_spec.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package dot
55

66
import (
7-
"context"
87
"encoding/json"
98
"fmt"
109
"os"
@@ -95,21 +94,15 @@ func BuildFromDB(path string) (*BuildSpec, error) {
9594
tmpGen.Genesis.Raw = make(map[string]map[string]string)
9695
tmpGen.Genesis.Runtime = make(map[string]map[string]interface{})
9796

98-
// BootstrapMailer should not return an error here since there is no URLs to connect to
99-
disabledTelemetry, err := telemetry.BootstrapMailer(context.TODO(), nil, false, nil)
100-
if err != nil {
101-
panic("telemetry should not fail at BuildFromDB function: " + err.Error())
102-
}
103-
10497
config := state.Config{
10598
Path: path,
10699
LogLevel: log.Info,
107-
Telemetry: disabledTelemetry,
100+
Telemetry: telemetry.NewNoopMailer(),
108101
}
109102

110103
stateSrvc := state.NewService(config)
111104

112-
err = stateSrvc.SetupBase()
105+
err := stateSrvc.SetupBase()
113106
if err != nil {
114107
return nil, fmt.Errorf("cannot setup state database: %w", err)
115108
}

dot/build_spec_integration_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ const codeHex = "0x3a636f6465"
2222
func TestBuildFromGenesis_Integration(t *testing.T) {
2323
t.Parallel()
2424

25-
file := genesis.CreateTestGenesisJSONFile(t, false)
25+
genesisFields := genesis.Fields{
26+
Raw: map[string]map[string]string{},
27+
Runtime: map[string]map[string]interface{}{
28+
"System": {
29+
"code": "mocktestcode",
30+
},
31+
},
32+
}
33+
file := genesis.CreateTestGenesisJSONFile(t, genesisFields)
2634
bs, err := BuildFromGenesis(file, 0)
2735

2836
const expectedChainType = "TESTCHAINTYPE"
@@ -43,7 +51,7 @@ func TestBuildFromGenesis_Integration(t *testing.T) {
4351
jGen := genesis.Genesis{}
4452
err = json.Unmarshal(hr, &jGen)
4553
require.NoError(t, err)
46-
genesis.TestGenesis.Genesis = genesis.TestFieldsHR
54+
genesis.TestGenesis.Genesis = genesisFields
4755
require.Equal(t, genesis.TestGenesis.Genesis.Runtime, jGen.Genesis.Runtime)
4856
require.Equal(t, expectedChainType, jGen.ChainType)
4957
require.Equal(t, expectedProperties, jGen.Properties)

dot/build_spec_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,15 @@ func TestBuildFromDB(t *testing.T) {
167167
}
168168

169169
func TestBuildFromGenesis(t *testing.T) {
170-
testGenesisPath := genesis.CreateTestGenesisJSONFile(t, false)
170+
genesisFields := genesis.Fields{
171+
Raw: map[string]map[string]string{},
172+
Runtime: map[string]map[string]interface{}{
173+
"System": {
174+
"code": "mocktestcode",
175+
},
176+
},
177+
}
178+
testGenesisPath := genesis.CreateTestGenesisJSONFile(t, genesisFields)
171179

172180
type args struct {
173181
path string

dot/network/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ type Config struct {
9797
// telemetryInterval how often to send telemetry metrics
9898
telemetryInterval time.Duration
9999

100-
noPreAllocate bool // internal option
101-
102100
batchSize int // internal option
103101

104102
// SlotDuration is the slot duration to produce a block

dot/network/connmgr.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@ func (cm *ConnManager) ListenClose(n network.Network, addr ma.Multiaddr) {
110110
"Host %s stopped listening on address %s", n.LocalPeer(), addr)
111111
}
112112

113-
// returns a slice of peers that are unprotected and may be pruned.
114-
func (cm *ConnManager) unprotectedPeers(peers []peer.ID) []peer.ID {
115-
unprot := []peer.ID{}
116-
for _, id := range peers {
117-
if !cm.IsProtected(id, "") && !cm.isPersistent(id) {
118-
unprot = append(unprot, id)
119-
}
120-
}
121-
122-
return unprot
123-
}
124-
125113
// Connected is called when a connection opened
126114
func (cm *ConnManager) Connected(n network.Network, c network.Conn) {
127115
logger.Tracef(
@@ -141,8 +129,3 @@ func (cm *ConnManager) Disconnected(_ network.Network, c network.Conn) {
141129
cm.disconnectHandler(c.RemotePeer())
142130
}
143131
}
144-
145-
func (cm *ConnManager) isPersistent(p peer.ID) bool {
146-
_, ok := cm.persistentPeers.Load(p)
147-
return ok
148-
}

dot/network/connmgr_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ func TestProtectUnprotectPeer(t *testing.T) {
115115
require.True(t, cm.IsProtected(p1, ""))
116116
require.True(t, cm.IsProtected(p2, ""))
117117

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

121121
cm.Unprotect(p1, "")
122122
cm.Unprotect(p2, "")
123123

124-
unprot = cm.unprotectedPeers([]peer.ID{p1, p2, p3, p4})
124+
unprot = unprotectedPeers(cm, []peer.ID{p1, p2, p3, p4})
125125
require.Equal(t, unprot, []peer.ID{p1, p2, p3, p4})
126126
}
127127

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

225225
addrA := nodes[0].host.multiaddrs()[0]
226226
addrB := nodes[1].host.multiaddrs()[0]
227-
addrC := nodes[2].host.addrInfo()
227+
addrC := addrInfo(nodes[2].host)
228228

229229
config := &Config{
230230
BasePath: t.TempDir(),

dot/network/discovery_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestBeginDiscovery(t *testing.T) {
121121
nodeB := createTestService(t, configB)
122122
nodeB.noGossip = true
123123

124-
addrInfoB := nodeB.host.addrInfo()
124+
addrInfoB := addrInfo(nodeB.host)
125125
err := nodeA.host.connect(addrInfoB)
126126
if failedToDial(err) {
127127
time.Sleep(TestBackoffTimeout)
@@ -170,7 +170,7 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
170170
nodeC.noGossip = true
171171

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

181181
// connect A and C
182-
addrInfoC := nodeC.host.addrInfo()
182+
addrInfoC := addrInfo(nodeC.host)
183183
err = nodeA.host.connect(addrInfoC)
184184
if failedToDial(err) {
185185
time.Sleep(TestBackoffTimeout)

dot/network/gossip_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestGossip(t *testing.T) {
4242
handlerB := newTestStreamHandler(testBlockAnnounceMessageDecoder)
4343
nodeB.host.registerStreamHandler(nodeB.host.protocolID, handlerB.handleStream)
4444

45-
addrInfoA := nodeA.host.addrInfo()
45+
addrInfoA := addrInfo(nodeA.host)
4646
err := nodeB.host.connect(addrInfoA)
4747
// retry connect if "failed to dial" error
4848
if failedToDial(err) {
@@ -70,7 +70,7 @@ func TestGossip(t *testing.T) {
7070
}
7171
require.NoError(t, err)
7272

73-
addrInfoB := nodeB.host.addrInfo()
73+
addrInfoB := addrInfo(nodeB.host)
7474
err = nodeC.host.connect(addrInfoB)
7575
// retry connect if "failed to dial" error
7676
if failedToDial(err) {

dot/network/helpers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,28 @@ func testBlockAnnounceHandshakeDecoder(in []byte, _ peer.ID, _ bool) (Message, e
147147
err := msg.Decode(in)
148148
return msg, err
149149
}
150+
151+
// addrInfo returns the libp2p peer.AddrInfo of the host
152+
func addrInfo(h *host) peer.AddrInfo {
153+
return peer.AddrInfo{
154+
ID: h.p2pHost.ID(),
155+
Addrs: h.p2pHost.Addrs(),
156+
}
157+
}
158+
159+
// returns a slice of peers that are unprotected and may be pruned.
160+
func unprotectedPeers(cm *ConnManager, peers []peer.ID) []peer.ID {
161+
unprot := []peer.ID{}
162+
for _, id := range peers {
163+
if cm.IsProtected(id, "") {
164+
continue
165+
}
166+
167+
_, isPersistent := cm.persistentPeers.Load(id)
168+
if !isPersistent {
169+
unprot = append(unprot, id)
170+
}
171+
}
172+
173+
return unprot
174+
}

dot/network/host.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,6 @@ func (h *host) peerCount() int {
392392
return len(peers)
393393
}
394394

395-
// addrInfo returns the libp2p peer.AddrInfo of the host
396-
func (h *host) addrInfo() peer.AddrInfo {
397-
return peer.AddrInfo{
398-
ID: h.p2pHost.ID(),
399-
Addrs: h.p2pHost.Addrs(),
400-
}
401-
}
402-
403395
// multiaddrs returns the multiaddresses of the host
404396
func (h *host) multiaddrs() (multiaddrs []ma.Multiaddr) {
405397
addrs := h.p2pHost.Addrs()

0 commit comments

Comments
 (0)