Skip to content

Commit 3770c78

Browse files
committed
all: address errcheck linter errors
1 parent 85b383e commit 3770c78

File tree

47 files changed

+556
-227
lines changed

Some content is hidden

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

47 files changed

+556
-227
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ jobs:
1919
with:
2020
go-version: 1.21.x
2121

22+
- name: Lint
23+
run: make lint
24+
2225
- name: Test
2326
run: make test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ go.work
2222

2323
# Build output directories
2424
build/
25+
.build/
2526

2627
# Visual Studio Code
2728
.vscode/
2829

30+
# Jetbrains
31+
.idea/

.golangci-lint.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
run:
2+
3+
linters:
4+
disable-all: true
5+
enable:
6+
- errcheck
7+
- ineffassign
8+
9+
issues:
10+
exclude-rules:
11+
- path: '(.+)_test\.go'
12+
text: ".*GetPaymentHistory is deprecated: Payment history has migrated to chats"
13+
- path: '(.+)testutil\.go'
14+
text: ".*GetPaymentHistory is deprecated: Payment history has migrated to chats"

Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
all: test
1+
.PHONY: all
2+
all: lint test
23

4+
.PHONY: test
35
test:
46
@go test -cover ./...
57

6-
.PHONY: all test
8+
.PHONY: lint
9+
lint: tools.golangci-lint
10+
@golangci-lint --timeout=3m --config .golangci-lint.yaml run ./...
11+
12+
.PHONY: tools
13+
tools: tools.golangci-lint
14+
15+
tools.golangci-lint: .build/markers/golangci-lint_installed
16+
.build/markers/golangci-lint_installed:
17+
@command -v golangci-lint >/dev/null ; if [ $$? -ne 0 ]; then \
18+
CGO_ENABLED=0 go install github.com/golangci/golangci-lint/cmd/[email protected]; \
19+
fi
20+
@mkdir -p $(shell dirname $@) && touch "$@"

pkg/cache/cache.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cache
22

33
import (
4-
"errors"
54
"log"
65
"sync"
76
)
@@ -11,7 +10,7 @@ type Cache interface {
1110
SetVerbose(verbose bool)
1211
GetWeight() int
1312
GetBudget() int
14-
Insert(key string, value interface{}, weight int) error
13+
Insert(key string, value interface{}, weight int) (inserted bool)
1514
Retrieve(key string) (interface{}, bool)
1615
Clear()
1716
}
@@ -61,12 +60,12 @@ func (c *cache) GetBudget() int {
6160
}
6261

6362
// Insert inserts an object into the cache
64-
func (c *cache) Insert(key string, value interface{}, weight int) error {
63+
func (c *cache) Insert(key string, value interface{}, weight int) (inserted bool) {
6564
c.mutex.Lock()
6665
defer c.mutex.Unlock()
6766

6867
if _, found := c.lookup[key]; found {
69-
return errors.New("key already exists in cache")
68+
return false
7069
}
7170

7271
node := &cacheNode{
@@ -106,7 +105,7 @@ func (c *cache) Insert(key string, value interface{}, weight int) error {
106105
delete(c.lookup, c.tail.key)
107106
}
108107

109-
return nil
108+
return true
110109
}
111110

112111
// Retrieve gets an object out of the cache

pkg/cache/cache_test.go

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@ package cache
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/require"
57
)
68

79
func TestCacheInsert(t *testing.T) {
810
cache := NewCache(1)
9-
insertError := cache.Insert("A", "", 1)
10-
11-
if insertError != nil {
12-
t.Fatalf("Cache insert resulted in unexpected error: %s", insertError)
13-
}
11+
require.True(t, cache.Insert("A", "", 1))
1412
}
1513

1614
func TestCacheInsertWithinBudget(t *testing.T) {
1715
cache := NewCache(1)
18-
insertError := cache.Insert("A", "", 2)
19-
20-
if insertError != nil {
21-
t.Fatalf("Cache insert resulted in unexpected error: %s", insertError)
22-
}
16+
require.True(t, cache.Insert("A", "", 2))
2317
}
2418

2519
func TestCacheInsertUpdatesWeight(t *testing.T) {
@@ -28,19 +22,13 @@ func TestCacheInsertUpdatesWeight(t *testing.T) {
2822
_ = cache.Insert("B", "", 1)
2923
_ = cache.Insert("budget_exceeded", "", 1)
3024

31-
if cache.GetWeight() != 2 {
32-
t.Fatal("Cache with budget 2 did not correctly set weight after evicting one of three nodes")
33-
}
25+
require.Equal(t, 2, cache.GetWeight())
3426
}
3527

3628
func TestCacheInsertDuplicateRejected(t *testing.T) {
3729
cache := NewCache(2)
38-
_ = cache.Insert("dupe", "", 1)
39-
dupeError := cache.Insert("dupe", "", 1)
40-
41-
if dupeError == nil {
42-
t.Fatal("Cache insert of duplicate key did not result in any err")
43-
}
30+
require.True(t, cache.Insert("dupe", "", 1))
31+
require.False(t, cache.Insert("dupe", "", 1))
4432
}
4533

4634
func TestCacheInsertEvictsLeastRecentlyUsed(t *testing.T) {
@@ -51,43 +39,34 @@ func TestCacheInsertEvictsLeastRecentlyUsed(t *testing.T) {
5139
_ = cache.Insert("B", "", 1)
5240

5341
_, foundEvicted := cache.Retrieve("evicted")
54-
if foundEvicted {
55-
t.Fatal("Cache insert did not trigger eviction after weight exceedance")
56-
}
42+
require.False(t, foundEvicted)
5743

5844
// double check that only 1 one was evicted and not any extra
5945
_, foundA := cache.Retrieve("A")
46+
require.True(t, foundA)
6047
_, foundB := cache.Retrieve("B")
61-
62-
if !foundA || !foundB {
63-
t.Fatal("Cache insert evicted more than necessary")
64-
}
48+
require.True(t, foundB)
6549
}
6650

6751
func TestCacheInsertEvictsLeastRecentlyRetrieved(t *testing.T) {
6852
cache := NewCache(2)
6953
_ = cache.Insert("A", "", 1)
7054
_ = cache.Insert("evicted", "", 1)
7155

72-
// retrieve the oldest node, promoting it head so it is not evicted
56+
// retrieve the oldest node, promoting it head, so it is not evicted
7357
cache.Retrieve("A")
7458

7559
// insert once more, exceeding weight capacity
7660
_ = cache.Insert("B", "", 1)
7761
// now the least recently used key should be evicted
7862
_, foundEvicted := cache.Retrieve("evicted")
79-
if foundEvicted {
80-
t.Fatal("Cache insert did not evict least recently used after weight exceedance")
81-
}
63+
require.False(t, foundEvicted)
8264
}
8365

8466
func TestClear(t *testing.T) {
8567
cache := NewCache(1)
8668
_ = cache.Insert("cleared", "", 1)
8769
cache.Clear()
8870
_, found := cache.Retrieve("cleared")
89-
90-
if found {
91-
t.Fatal("Still able to retrieve nodes after cache was cleared")
92-
}
71+
require.False(t, found)
9372
}

pkg/code/async/account/gift_card.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,18 @@ func (p *service) initiateProcessToAutoReturnGiftCard(ctx context.Context, giftC
198198
}
199199

200200
// Finally, update the user by best-effort sending them a push
201-
go push.SendGiftCardReturnedPushNotification(
202-
ctx,
203-
p.data,
204-
p.pusher,
205-
giftCardVaultAccount,
206-
)
201+
go func() {
202+
err := push.SendGiftCardReturnedPushNotification(
203+
ctx,
204+
p.data,
205+
p.pusher,
206+
giftCardVaultAccount,
207+
)
208+
if err != nil {
209+
p.log.WithError(err).Warn("failed to send gift card return push notification (best effort)")
210+
}
211+
}()
212+
207213
return nil
208214
}
209215

pkg/code/async/commitment/worker.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ import (
44
"context"
55
"database/sql"
66
"errors"
7+
"fmt"
78
"math"
89
"sync"
910
"time"
1011

1112
"github.com/mr-tron/base58"
1213
"github.com/newrelic/go-agent/v3/newrelic"
14+
"github.com/sirupsen/logrus"
1315

14-
"github.com/code-payments/code-server/pkg/database/query"
15-
"github.com/code-payments/code-server/pkg/metrics"
16-
"github.com/code-payments/code-server/pkg/pointer"
17-
"github.com/code-payments/code-server/pkg/retry"
18-
"github.com/code-payments/code-server/pkg/solana"
1916
"github.com/code-payments/code-server/pkg/code/common"
2017
"github.com/code-payments/code-server/pkg/code/data/action"
2118
"github.com/code-payments/code-server/pkg/code/data/commitment"
@@ -24,6 +21,11 @@ import (
2421
"github.com/code-payments/code-server/pkg/code/data/nonce"
2522
"github.com/code-payments/code-server/pkg/code/data/treasury"
2623
"github.com/code-payments/code-server/pkg/code/transaction"
24+
"github.com/code-payments/code-server/pkg/database/query"
25+
"github.com/code-payments/code-server/pkg/metrics"
26+
"github.com/code-payments/code-server/pkg/pointer"
27+
"github.com/code-payments/code-server/pkg/retry"
28+
"github.com/code-payments/code-server/pkg/solana"
2729
)
2830

2931
//
@@ -424,16 +426,39 @@ func (p *service) injectCommitmentVaultManagementFulfillments(ctx context.Contex
424426
if err != nil {
425427
return err
426428
}
429+
430+
// note: defer() will only run when the outer function returns, and therefore
431+
// all of the defer()'s in this loop will be run all at once at the end, rather
432+
// than at the end of each iteration.
433+
//
434+
// Since we are not committing (and therefore consuming) the nonce's until the
435+
// end of the function, this is desirable. If we released at the end of each
436+
// iteration, we could potentially acquire the same nonce multiple times for
437+
// different transactions, which would fail.
427438
defer func() {
428-
selectedNonce.ReleaseIfNotReserved()
439+
if err := selectedNonce.ReleaseIfNotReserved(); err != nil {
440+
p.log.
441+
WithFields(logrus.Fields{
442+
"method": "injectCommitmentVaultManagementFulfillments",
443+
"nonce_account": selectedNonce.Account.PublicKey().ToBase58(),
444+
"blockhash": selectedNonce.Blockhash.ToBase58(),
445+
}).
446+
WithError(err).
447+
Warn("failed to release nonce")
448+
}
449+
450+
// This is idempotent regardless of whether the above
429451
selectedNonce.Unlock()
430452
}()
431453

432454
txn, err := transaction.MakeNoncedTransaction(selectedNonce.Account, selectedNonce.Blockhash, txnToMake.ixns...)
433455
if err != nil {
434456
return err
435457
}
436-
txn.Sign(common.GetSubsidizer().PrivateKey().ToBytes())
458+
459+
if err := txn.Sign(common.GetSubsidizer().PrivateKey().ToBytes()); err != nil {
460+
return fmt.Errorf("failed to sign transaction: %w", err)
461+
}
437462

438463
intentOrderingIndex := uint64(0)
439464
fulfillmentOrderingIndex := uint32(i)

0 commit comments

Comments
 (0)