Skip to content

Commit c439281

Browse files
Resolve staticcheck lint warnings. (#654)
1 parent 4ce4278 commit c439281

16 files changed

+43
-57
lines changed

accounting/rewind.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func AccountAtRound(account models.Account, round uint64, db idb.IndexerDb) (acc
185185
fmt.Sprintf("queried round r: %d < requested round round: %d", r, round)}
186186
return
187187
}
188-
for txnrow := range txns {
188+
txnrow, ok := <-txns
189+
if ok {
189190
if txnrow.Error != nil {
190191
err = txnrow.Error
191192
return
@@ -217,12 +218,11 @@ func AccountAtRound(account models.Account, round uint64, db idb.IndexerDb) (acc
217218
acct.PendingRewards = rewardsDelta * rewardsUnits
218219
acct.Amount = acct.PendingRewards + acct.AmountWithoutPendingRewards
219220
acct.Round = round
220-
return
221+
} else {
222+
// There were no prior transactions, it must have been empty before, zero out things
223+
acct.PendingRewards = 0
224+
acct.Amount = acct.AmountWithoutPendingRewards
221225
}
222-
223-
// There were no prior transactions, it must have been empty before, zero out things
224-
acct.PendingRewards = 0
225-
acct.Amount = acct.AmountWithoutPendingRewards
226226
}
227227

228228
acct.Round = round

api/handlers.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,15 @@ func (si *ServerImplementation) LookupApplicationByID(ctx echo.Context, applicat
258258
out := generated.ApplicationResponse{
259259
CurrentRound: round,
260260
}
261-
for result := range results {
262-
if result.Error != nil {
263-
return indexerError(ctx, result.Error.Error())
264-
}
265-
out.Application = &result.Application
266-
return ctx.JSON(http.StatusOK, out)
261+
result, ok := <-results
262+
if !ok {
263+
return ctx.JSON(http.StatusNotFound, out)
264+
}
265+
if result.Error != nil {
266+
return indexerError(ctx, result.Error.Error())
267267
}
268-
return ctx.JSON(http.StatusNotFound, out)
268+
out.Application = &result.Application
269+
return ctx.JSON(http.StatusOK, out)
269270
}
270271

271272
// LookupAssetByID looks up a particular asset

api/pointer_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func boolPtr(x bool) *bool {
9696
}
9797

9898
func strArrayPtr(x []string) *[]string {
99-
if x == nil || len(x) == 0 {
99+
if len(x) == 0 {
100100
return nil
101101
}
102102
return &x

cmd/block-generator/generator/generate.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"github.com/algorand/go-algorand/rpcs"
2121
)
2222

23-
var errOutOfRange = fmt.Errorf("selection is out of weighted range")
24-
2523
// TxTypeID is the transaction type.
2624
type TxTypeID string
2725

@@ -229,8 +227,8 @@ type Report map[TxTypeID]TxData
229227

230228
// TxData is the generator report data.
231229
type TxData struct {
232-
GenerationTimeMilli time.Duration `json:"generation_time_milli"`
233-
GenerationCount uint64 `json:"num_generated"`
230+
GenerationTime time.Duration `json:"generation_time_milli"`
231+
GenerationCount uint64 `json:"num_generated"`
234232
}
235233

236234
func track(id TxTypeID) (TxTypeID, time.Time) {
@@ -239,7 +237,7 @@ func track(id TxTypeID) (TxTypeID, time.Time) {
239237
func (g *generator) recordData(id TxTypeID, start time.Time) {
240238
data := g.reportData[id]
241239
data.GenerationCount++
242-
data.GenerationTimeMilli += time.Since(start)
240+
data.GenerationTime += time.Since(start)
243241
g.reportData[id] = data
244242
}
245243

@@ -460,7 +458,7 @@ func (g *generator) generatePaymentTxnInternal(selection TxTypeID, round uint64,
460458
fee := uint64(1000)
461459
total := amount + fee
462460
if g.balances[sendIndex] < total {
463-
fmt.Printf(fmt.Sprintf("\n\nthe sender account does not have enough algos for the transfer. idx %d, payment number %d\n\n", sendIndex, g.numPayments))
461+
fmt.Printf("\n\nthe sender account does not have enough algos for the transfer. idx %d, payment number %d\n\n", sendIndex, g.numPayments)
464462
os.Exit(1)
465463
}
466464

@@ -581,11 +579,11 @@ func (g *generator) generateAssetTxnInternalHint(txType TxTypeID, round uint64,
581579
txn = g.makeAssetTransferTxn(g.makeTxnHeader(sender, round, intra), receiver, amount, basics.Address{}, asset.assetID)
582580

583581
if asset.holdings[0].balance < amount {
584-
fmt.Printf(fmt.Sprintf("\n\ncreator doesn't have enough funds for asset %d\n\n", asset.assetID))
582+
fmt.Printf("\n\ncreator doesn't have enough funds for asset %d\n\n", asset.assetID)
585583
os.Exit(1)
586584
}
587585
if g.balances[asset.holdings[0].acctIndex] < fee {
588-
fmt.Printf(fmt.Sprintf("\n\ncreator doesn't have enough funds for transaction %d\n\n", asset.assetID))
586+
fmt.Printf("\n\ncreator doesn't have enough funds for transaction %d\n\n", asset.assetID)
589587
os.Exit(1)
590588
}
591589

@@ -626,7 +624,7 @@ func (g *generator) generateAssetTxnInternalHint(txType TxTypeID, round uint64,
626624
}
627625

628626
if g.balances[senderIndex] < txn.Fee.ToUint64() {
629-
fmt.Printf(fmt.Sprintf("\n\nthe sender account does not have enough algos for the transfer. idx %d, asset transaction type %v, num %d\n\n", senderIndex, actual, g.reportData[actual].GenerationCount))
627+
fmt.Printf("\n\nthe sender account does not have enough algos for the transfer. idx %d, asset transaction type %v, num %d\n\n", senderIndex, actual, g.reportData[actual].GenerationCount)
630628
os.Exit(1)
631629
}
632630
g.balances[senderIndex] -= txn.Fee.ToUint64()

cmd/block-generator/generator/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestParseRound(t *testing.T) {
4747
},
4848
{
4949
name: "invalid prefix",
50-
url: fmt.Sprintf("/v2/wrong/prefix/1"),
50+
url: "/v2/wrong/prefix/1",
5151
expectedRound: 0,
5252
err: "not a blocks query",
5353
},

cmd/e2equeries/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func main() {
7171
printAccountQuery(db, idb.AccountQueryOptions{HasAssetID: bestid, Limit: bestcount})
7272
}
7373

74-
dt := time.Now().Sub(start)
74+
dt := time.Since(start)
7575
exitValue := testutil.ExitValue()
7676
if exitValue == 0 {
7777
fmt.Printf("wat OK %s\n", dt.String())

cmd/idbtest/idbtest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func main() {
190190
testTxnPaging(db, idb.TransactionFilter{AlgosGT: uint64Ptr(1)})
191191
}
192192

193-
dt := time.Now().Sub(start)
193+
dt := time.Since(start)
194194
exitValue += testutil.ExitValue()
195195
if exitValue == 0 {
196196
fmt.Printf("wat OK %s\n", dt.String())

cmd/validator/core/dynamic_processor.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func (gp DynamicProcessor) ProcessAddress(algodData, indexerData []byte) (Result
5959

6060
// isDeleted is a simple helper to check for boolean values
6161
func isDeleted(val interface{}) (bool, error) {
62-
switch val.(type) {
62+
switch val := val.(type) {
6363
case bool:
64-
return val.(bool), nil
64+
return val, nil
6565
default:
6666
return false, fmt.Errorf("unable to parse value as boolean (%v)", val)
6767
}
@@ -143,10 +143,10 @@ func normalizeRecurse(key string, data interface{}) (interface{}, error) {
143143
result := data
144144

145145
// If the data is a complex type, recursively normalize result
146-
switch data.(type) {
146+
switch data := data.(type) {
147147
case map[string]interface{}:
148148
// Handle objects.
149-
object := data.(map[string]interface{})
149+
object := data
150150

151151
if deletedVal, ok := object["deleted"]; ok {
152152
deleted, err := isDeleted(deletedVal)
@@ -209,7 +209,7 @@ func normalizeRecurse(key string, data interface{}) (interface{}, error) {
209209
case []interface{}:
210210
// Normalize each element of array
211211
resultArray := make([]interface{}, 0)
212-
for _, arrVal := range data.([]interface{}) {
212+
for _, arrVal := range data {
213213
normalized, err := normalizeRecurse("", arrVal)
214214
if err != nil {
215215
return nil, err

cmd/validator/core/struct_processor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ func equals(indexer, algod generated.Account) (differences []string) {
242242
if !stringPtrEqual(algodCreatedApp.Params.Creator, indexerCreatedApp.Params.Creator) {
243243
differences = append(differences, fmt.Sprintf("created-app creator %d", algodCreatedApp.Id))
244244
}
245-
if bytes.Compare(algodCreatedApp.Params.ApprovalProgram, indexerCreatedApp.Params.ApprovalProgram) != 0 {
245+
if !bytes.Equal(algodCreatedApp.Params.ApprovalProgram, indexerCreatedApp.Params.ApprovalProgram) {
246246
differences = append(differences, fmt.Sprintf("created-app approval-program %d", algodCreatedApp.Id))
247247
}
248-
if bytes.Compare(algodCreatedApp.Params.ClearStateProgram, indexerCreatedApp.Params.ClearStateProgram) != 0 {
248+
if !bytes.Equal(algodCreatedApp.Params.ClearStateProgram, indexerCreatedApp.Params.ClearStateProgram) {
249249
differences = append(differences, fmt.Sprintf("created-app clear-state-program %d", algodCreatedApp.Id))
250250
}
251251
if !tealKeyValueStoreEqual(algodCreatedApp.Params.GlobalState, indexerCreatedApp.Params.GlobalState) {
@@ -321,7 +321,7 @@ func bytesPtrEqual(val1, val2 *[]uint8) bool {
321321
return false
322322
}
323323

324-
return bytes.Compare(*val1, *val2) == 0
324+
return bytes.Equal(*val1, *val2)
325325
}
326326

327327
func boolPtrEqual(val1, val2 *bool) bool {

fetcher/fetcher.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (bot *fetcherImpl) catchupLoop() {
9999
var err error
100100
var blockbytes []byte
101101
aclient := bot.Algod()
102-
for true {
102+
for {
103103
if bot.isDone() {
104104
return
105105
}
@@ -127,7 +127,7 @@ func (bot *fetcherImpl) followLoop() {
127127
var err error
128128
var blockbytes []byte
129129
aclient := bot.Algod()
130-
for true {
130+
for {
131131
for retries := 0; retries < 3; retries++ {
132132
if bot.isDone() {
133133
return
@@ -162,7 +162,7 @@ func (bot *fetcherImpl) followLoop() {
162162

163163
// Run is part of the Fetcher interface
164164
func (bot *fetcherImpl) Run() {
165-
for true {
165+
for {
166166
if bot.isDone() {
167167
return
168168
}

idb/migration/migration.go

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ func (m *Migration) runMigrations(ch chan struct{}) {
216216
close(ch)
217217
}
218218
m.log.Println("Migration finished successfully.")
219-
return
220219
}
221220

222221
// RunMigrations runs all tasks which have been loaded into the migration.

idb/migration/migration_test.go

-16
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,13 @@ var fastSuccessTask2 = makeTask(0*time.Second, nil, 2, false, "fast success")
2020
var fastSuccessTask3 = makeTask(0*time.Second, nil, 3, false, "fast success")
2121

2222
var fastErrorTask1 = makeTask(0*time.Second, errMigration, 1, false, "fast error")
23-
var fastErrorTask2 = makeTask(0*time.Second, errMigration, 2, false, "fast error")
24-
var fastErrorTask3 = makeTask(0*time.Second, errMigration, 3, false, "fast error")
2523

26-
var slowErrorTask1 = makeTask(1*time.Second, errMigration, 1, false, "slow error")
27-
var slowErrorTask2 = makeTask(1*time.Second, errMigration, 2, false, "slow error")
2824
var slowErrorTask3 = makeTask(1*time.Second, errMigration, 3, false, "slow error")
2925

3026
var slowSuccessBlockingTask1 = makeTask(1*time.Second, nil, 1, true, "blocking slow success")
3127
var slowSuccessBlockingTask2 = makeTask(1*time.Second, nil, 2, true, "blocking slow success")
3228
var slowSuccessBlockingTask3 = makeTask(1*time.Second, nil, 3, true, "blocking slow success")
3329

34-
var fastSuccessBlockingTask1 = makeTask(0*time.Second, nil, 1, true, "blocking fast success")
35-
var fastSuccessBlockingTask2 = makeTask(0*time.Second, nil, 2, true, "blocking fast success")
36-
var fastSuccessBlockingTask3 = makeTask(0*time.Second, nil, 3, true, "blocking fast success")
37-
38-
var fastErrorBlockingTask1 = makeTask(0*time.Second, errMigration, 1, true, "blocking fast error")
39-
var fastErrorBlockingTask2 = makeTask(0*time.Second, errMigration, 2, true, "blocking fast error")
40-
var fastErrorBlockingTask3 = makeTask(0*time.Second, errMigration, 3, true, "blocking fast error")
41-
42-
var slowErrorBlockingTask1 = makeTask(1*time.Second, errMigration, 1, true, "blocking slow error")
43-
var slowErrorBlockingTask2 = makeTask(1*time.Second, errMigration, 2, true, "blocking slow error")
44-
var slowErrorBlockingTask3 = makeTask(1*time.Second, errMigration, 3, true, "blocking slow error")
45-
4630
type testTask struct {
4731
id int
4832
description string

idb/postgres/postgres.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func buildTransactionQuery(tf idb.TransactionFilter) (query string, whereArgs []
598598
partNumber++
599599
}
600600
if tf.RekeyTo != nil && (*tf.RekeyTo) {
601-
whereParts = append(whereParts, fmt.Sprintf("(t.txn -> 'txn' -> 'rekey') IS NOT NULL"))
601+
whereParts = append(whereParts, "(t.txn -> 'txn' -> 'rekey') IS NOT NULL")
602602
}
603603
query = "SELECT t.round, t.intra, t.txnbytes, t.extra, t.asset, h.realtime FROM txn t JOIN block_header h ON t.round = h.round"
604604
if joinParticipation {
@@ -1375,7 +1375,7 @@ func stringPtr(x string) *string {
13751375
}
13761376

13771377
func baPtr(x []byte) *[]byte {
1378-
if x == nil || len(x) == 0 {
1378+
if len(x) == 0 {
13791379
return nil
13801380
}
13811381
allzero := true

idb/postgres/postgres_integration_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ func TestAppExtraPages(t *testing.T) {
782782

783783
var ap basics.AppParams
784784
err = encoding.DecodeJSON(paramsStr, &ap)
785+
require.NoError(t, err)
785786
require.Equal(t, uint32(1), ap.ExtraProgramPages)
786787

787788
var filter generated.SearchForApplicationsParams
@@ -864,6 +865,7 @@ func TestLargeAssetAmount(t *testing.T) {
864865
txn := test.MakeConfigAssetTxn(
865866
0, math.MaxUint64, 0, false, "mc", "mycoin", "", test.AccountA)
866867
block, err := test.MakeBlockForTxns(test.MakeGenesisBlock().BlockHeader, &txn)
868+
require.NoError(t, err)
867869

868870
err = db.AddBlock(&block)
869871
require.NoError(t, err)

idb/postgres/postgres_migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func needsMigration(state MigrationState) bool {
100100
// upsertMigrationState updates the migration state, and optionally increments
101101
// the next counter with an existing transaction.
102102
// If `tx` is nil, use a normal query.
103+
//lint:ignore U1000 this function might be used in a future migration
103104
func upsertMigrationState(db *IndexerDb, tx pgx.Tx, state *MigrationState) error {
104105
migrationStateJSON := encoding.EncodeJSON(state)
105106
return db.setMetastate(tx, schema.MigrationMetastateKey, string(migrationStateJSON))
@@ -176,6 +177,7 @@ func (db *IndexerDb) getMigrationState() (MigrationState, error) {
176177
}
177178

178179
// sqlMigration executes a sql statements as the entire migration.
180+
//lint:ignore U1000 this function might be used in a future migration
179181
func sqlMigration(db *IndexerDb, state *MigrationState, sqlLines []string) error {
180182
db.accountingLock.Lock()
181183
defer db.accountingLock.Unlock()

version/strings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Version() string {
4141
if err != nil {
4242
return fmt.Sprintf("compiled with bad GitDecorateBase64, %s", err.Error())
4343
}
44-
tre := regexp.MustCompile("tag:\\s+([^,]+)")
44+
tre := regexp.MustCompile(`tag:\s+([^,]+)`)
4545
m := tre.FindAllStringSubmatch(string(b), -1)
4646
if m == nil {
4747
return UnknownVersion

0 commit comments

Comments
 (0)