Skip to content

Commit 70036df

Browse files
committed
fix(golangci.yml): Exclude generated file from linter
chore: Refactor for linter Signed-off-by: Sophia Koehler <sophia@perun.network>
1 parent 5e5bfb3 commit 70036df

33 files changed

+70
-87
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ linters-settings:
4848
template-path: ".scripts/copyright-notice"
4949
forbidigo:
5050
forbid:
51+
- exclude-rules:
52+
- path: ".*\\.pb\\.go$" # Exclude protobuf generated files.
5153
# Forbid functions to start with "get" or "Get".
5254
- ^[Gg]et.*$
5355
- ^[Ee]quals$

backend/sim/channel/asset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewRandomAsset(rng *rand.Rand) *Asset {
4444
// MarshalBinary marshals the address into its binary representation.
4545
func (a Asset) MarshalBinary() ([]byte, error) {
4646
data := make([]byte, assetLen)
47-
byteOrder.PutUint64(data, uint64(a.ID))
47+
byteOrder.PutUint64(data, uint64(a.ID)) //nolint:gosec
4848
return data, nil
4949
}
5050

backend/sim/channel/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (*backend) CalcID(p *channel.Params) (id channel.ID, err error) {
3737
w := sha256.New()
3838

3939
// Write Parts
40-
if err := perunio.Encode(w, wallet.AddressMapArray{p.Parts}); err != nil {
40+
if err := perunio.Encode(w, wallet.AddressMapArray{Addr: p.Parts}); err != nil {
4141
log.Panic("Could not write to sha256 hasher")
4242
}
4343

channel/allocation.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func NewAllocation(numParts int, backends []wallet.BackendID, assets ...Asset) *
127127
func (a *Allocation) AssetIndex(asset Asset) (Index, bool) {
128128
for idx, _asset := range a.Assets {
129129
if asset.Equal(_asset) {
130-
return Index(idx), true
130+
return Index(idx), true //nolint:gosec
131131
}
132132
}
133133
return 0, false
@@ -194,15 +194,11 @@ func (a *Allocation) NumParts() int {
194194
func (a Allocation) Clone() (clone Allocation) {
195195
if a.Backends != nil {
196196
clone.Backends = make([]wallet.BackendID, len(a.Backends))
197-
for i, bID := range a.Backends {
198-
clone.Backends[i] = bID
199-
}
197+
copy(clone.Backends, a.Backends)
200198
}
201199
if a.Assets != nil {
202200
clone.Assets = make([]Asset, len(a.Assets))
203-
for i, asset := range a.Assets {
204-
clone.Assets[i] = asset
205-
}
201+
copy(clone.Assets, a.Assets)
206202
}
207203

208204
clone.Balances = a.Balances.Clone()
@@ -334,7 +330,7 @@ func (a Allocation) Encode(w io.Writer) error {
334330
}
335331
// encode assets
336332
for i, asset := range a.Assets {
337-
if err := perunio.Encode(w, uint32(a.Backends[i])); err != nil {
333+
if err := perunio.Encode(w, uint32(a.Backends[i])); err != nil { //nolint:gosec
338334
return errors.WithMessagef(err, "encoding backends %d", i)
339335
}
340336
if err := perunio.Encode(w, asset); err != nil {
@@ -634,7 +630,7 @@ func AssertAssetsEqual(a []Asset, b []Asset) error {
634630
return nil
635631
}
636632

637-
// AssertAssetsEqual returns an error if the given assets are not equal.
633+
// AssertBackendsEqual returns an error if the given assets are not equal.
638634
func AssertBackendsEqual(a []wallet.BackendID, b []wallet.BackendID) error {
639635
if len(a) != len(b) {
640636
return errors.New("length mismatch")

channel/allocation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestAllocationValidLimits(t *testing.T) {
278278
}
279279

280280
allocation.Assets = test.NewRandomAssets(rng, test.WithNumAssets(x.numAssets))
281-
for i, _ := range allocation.Assets {
281+
for i := range allocation.Assets {
282282
allocation.Backends[i] = 0
283283
}
284284

channel/machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (m *machine) Sig() (sig wallet.Sig, err error) {
228228
sig, err = Sign(acc, m.stagingTX.State, b)
229229
if err == nil {
230230
m.stagingTX.Sigs[m.idx] = sig
231-
return
231+
return sig, nil
232232
}
233233
}
234234
} else {

channel/multi/asset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type (
2727
AssetID() AssetID
2828
}
2929

30+
// AssetID represents an asset identifier.
3031
AssetID interface {
3132
BackendID() uint32
3233
LedgerId() LedgerID

channel/params.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const IDLen = 32
3535
// ID represents a channelID.
3636
type ID = [IDLen]byte
3737

38-
// IDMap is a map of IDs with keys corresponding to backendIDs
38+
// IDMap is a map of IDs with keys corresponding to backendIDs.
3939
type IDMap map[wallet.BackendID]ID
4040

4141
// MaxNonceLen is the maximum byte count of a nonce.
@@ -75,12 +75,12 @@ func EqualIDs(a, b map[wallet.BackendID]ID) bool {
7575
}
7676

7777
func (ids IDMap) Encode(w stdio.Writer) error {
78-
length := int32(len(ids))
78+
length := int32(len(ids)) //nolint:gosec
7979
if err := perunio.Encode(w, length); err != nil {
8080
return errors.WithMessage(err, "encoding map length")
8181
}
8282
for i, id := range ids {
83-
if err := perunio.Encode(w, int32(i)); err != nil {
83+
if err := perunio.Encode(w, int32(i)); err != nil { //nolint:gosec
8484
return errors.WithMessage(err, "encoding map index")
8585
}
8686
if err := perunio.Encode(w, id); err != nil {
@@ -112,14 +112,11 @@ func (ids *IDMap) Decode(r stdio.Reader) error {
112112

113113
func IDKey(ids IDMap) string {
114114
var buff strings.Builder
115-
// Encode the number of elements in the map first.
116-
length := int32(len(ids)) // Using int32 to encode the length
117-
err := binary.Write(&buff, binary.BigEndian, length)
115+
length := int32(len(ids)) //nolint:gosec
116+
err := binary.Write(&buff, binary.BigEndian, length) //nolint:gosec
118117
if err != nil {
119118
log.Panic("could not encode map length in Key: ", err)
120-
121119
}
122-
// Iterate over the map and encode each key-value pair.
123120
sortedKeys, sortedIDs := sortIDMap(ids)
124121
for i, id := range sortedIDs {
125122
if err := binary.Write(&buff, binary.BigEndian, int32(sortedKeys[i])); err != nil {
@@ -133,9 +130,9 @@ func IDKey(ids IDMap) string {
133130
}
134131

135132
func sortIDMap(ids IDMap) ([]wallet.BackendID, []ID) {
136-
var indexes []int
133+
indexes := make([]int, len(ids))
137134
for i := range ids {
138-
indexes = append(indexes, int(i))
135+
indexes[i] = int(i)
139136
}
140137
sort.Ints(indexes)
141138
sortedIndexes := make([]wallet.BackendID, len(indexes))
@@ -279,12 +276,12 @@ func NewParamsUnsafe(challengeDuration uint64, parts []map[wallet.BackendID]wall
279276
return p
280277
}
281278

282-
// CloneAddress returns a clone of an Address using its binary marshaling
279+
// CloneAddresses returns a clone of an Address using its binary marshaling
283280
// implementation. It panics if an error occurs during binary (un)marshaling.
284281
func CloneAddresses(as []map[wallet.BackendID]wallet.Address) []map[wallet.BackendID]wallet.Address {
285-
var cloneMap []map[wallet.BackendID]wallet.Address
286-
for _, a := range as {
287-
cloneMap = append(cloneMap, wallet.CloneAddressesMap(a))
282+
cloneMap := make([]map[wallet.BackendID]wallet.Address, len(as))
283+
for i, a := range as {
284+
cloneMap[i] = wallet.CloneAddressesMap(a)
288285
}
289286
return cloneMap
290287
}
@@ -304,7 +301,6 @@ func (p *Params) Clone() *Params {
304301

305302
// Encode uses the pkg/io module to serialize a params instance.
306303
func (p *Params) Encode(w stdio.Writer) error {
307-
308304
return perunio.Encode(w,
309305
p.ChallengeDuration,
310306
wallet.AddressMapArray{Addr: p.Parts},

channel/params_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func TestParams_Clone(t *testing.T) {
3535
require.Equalf(t, params.App, clone.App, "Clone() = %v, want %v", clone, params)
3636
require.Equalf(t, params.ChallengeDuration, clone.ChallengeDuration, "Clone() = %v, want %v", clone, params)
3737
require.Equalf(t, params.Nonce, clone.Nonce, "Clone() = %v, want %v", clone, params)
38-
3938
}
4039

4140
func TestParams_Serializer(t *testing.T) {

channel/persistence/nonpersister.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type nonPersistRestorer struct{}
3737
func (nonPersistRestorer) ChannelCreated(context.Context, channel.Source, []map[wallet.BackendID]wire.Address, *map[wallet.BackendID]channel.ID) error {
3838
return nil
3939
}
40+
4041
func (nonPersistRestorer) ChannelRemoved(context.Context, map[wallet.BackendID]channel.ID) error {
4142
return nil
4243
}

0 commit comments

Comments
 (0)