Skip to content

Commit

Permalink
multi: Address some vet and linter complaints.
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh committed Aug 20, 2024
1 parent d505edd commit 5e3adcf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
7 changes: 4 additions & 3 deletions blockchain/stake/staketx.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2022 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
//
Expand All @@ -10,6 +10,7 @@ package stake
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
Expand Down Expand Up @@ -513,8 +514,8 @@ func SStxNullOutputAmounts(amounts []int64,
lengthAmounts := len(amounts)

if lengthAmounts != len(changeAmounts) {
str := "amounts was not equal in length to change amounts!"
return 0, nil, fmt.Errorf(str)
const str = "amounts was not equal in length to change amounts!"
return 0, nil, errors.New(str)
}

if amountTicket <= 0 {
Expand Down
68 changes: 31 additions & 37 deletions blockchain/stake/tickets_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015-2023 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -314,7 +314,7 @@ func TestTicketDBLongChain(t *testing.T) {
ticketsSpentInBlock(block), revokedTicketsInBlock(block),
ticketsToAdd)
if err != nil {
t.Fatalf("couldn't connect node: %v", err.Error())
t.Fatalf("couldn't connect node: %v", err)
}

// UndoTicketDataSlice tests
Expand Down Expand Up @@ -409,15 +409,15 @@ func TestTicketDBLongChain(t *testing.T) {
bestNode, err = bestNode.DisconnectNode(lotteryIV, blockUndoData,
ticketsToAdd, nil)
if err != nil {
t.Errorf(err.Error())
t.Error(err)
}
}

// Test some accessory functions.
accessoryTestNode := nodesForward[450]
exists := accessoryTestNode.ExistsLiveTicket(accessoryTestNode.nextWinners[0])
if !exists {
t.Errorf("expected winner to exist in node live tickets")
t.Error("expected winner to exist in node live tickets")
}
missedExp := make([]chainhash.Hash, 0)
accessoryTestNode.missedTickets.ForEach(func(k tickettreap.Key,
Expand Down Expand Up @@ -490,7 +490,7 @@ func TestTicketDBLongChain(t *testing.T) {
return nil
})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
// Cache all of our nodes so that we can check them when we start
Expand Down Expand Up @@ -522,38 +522,36 @@ func TestTicketDBLongChain(t *testing.T) {
ticketsSpentInBlock(block), revokedTicketsInBlock(block),
ticketsToAdd)
if err != nil {
return fmt.Errorf("couldn't connect node: %v", err.Error())
return fmt.Errorf("couldn't connect node: %v", err)
}
// Write the new node to db.
nodesForward[i] = bestNode
blockHash := block.Hash()
err := WriteConnectedBestNode(dbTx, bestNode, *blockHash)
if err != nil {
return fmt.Errorf("failure writing the best node: %v",
err.Error())
return fmt.Errorf("failure writing the best node: %v", err)
}
// Reload the node from DB and make sure it's the same.
blockHash := block.Hash()
loadedNode, err := LoadBestNode(dbTx, bestNode.Height(),
*blockHash, header, params)
if err != nil {
return fmt.Errorf("failed to load the best node: %v",
err.Error())
return fmt.Errorf("failed to load the best node: %v", err)
}
err = nodesEqual(loadedNode, bestNode)
if err != nil {
return fmt.Errorf("loaded best node was not same as "+
"in memory best node: %v", err.Error())
"in memory best node: %v", err)
}
loadedNodesForward[i] = loadedNode
}
return nil
})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}
nodesBackward := make([]*Node, testBCHeight+1)
Expand All @@ -573,12 +571,12 @@ func TestTicketDBLongChain(t *testing.T) {
bestNode, err = bestNode.DisconnectNode(header, blockUndoData,
ticketsToAdd, nil)
if err != nil {
t.Errorf(err.Error())
t.Error(err)
}
err = nodesEqual(bestNode, nodesForward[i-1])
if err != nil {
t.Errorf("non-equiv stake nodes at height %v: %v", i-1, err.Error())
t.Errorf("non-equiv stake nodes at height %v: %v", i-1, err)
}
// Try again using the database instead of the in memory
Expand All @@ -594,13 +592,12 @@ func TestTicketDBLongChain(t *testing.T) {
return nil
})
if err != nil {
t.Errorf("couldn't disconnect using the database: %v",
err.Error())
t.Errorf("couldn't disconnect using the database: %v", err)
}
err = nodesEqual(bestNode, bestNodeUsingDB)
if err != nil {
t.Errorf("non-equiv stake nodes using db when disconnecting: %v",
err.Error())
err)
}
// Write the new best node to the database.
Expand All @@ -611,14 +608,13 @@ func TestTicketDBLongChain(t *testing.T) {
err := WriteDisconnectedBestNode(dbTx, bestNode,
*parentBlockHash, formerBestNode.UndoData())
if err != nil {
return fmt.Errorf("failure writing the best node: %v",
err.Error())
return fmt.Errorf("failure writing the best node: %v", err)
}
return nil
})
if err != nil {
t.Errorf("%s", err.Error())
t.Error(err)
}
// Check the best node against the loaded best node from
Expand All @@ -628,24 +624,23 @@ func TestTicketDBLongChain(t *testing.T) {
loadedNode, err := LoadBestNode(dbTx, bestNode.Height(),
*parentBlockHash, header, params)
if err != nil {
return fmt.Errorf("failed to load the best node: %v",
err.Error())
return fmt.Errorf("failed to load the best node: %v", err)
}
err = nodesEqual(loadedNode, bestNode)
if err != nil {
return fmt.Errorf("loaded best node %v was not same as "+
"in memory best node: %v", loadedNode.Height(), err.Error())
"in memory best node: %v", loadedNode.Height(), err)
}
err = nodesEqual(loadedNode, loadedNodesForward[i-1])
if err != nil {
return fmt.Errorf("loaded best node %v was not same as "+
"cached best node: %v", loadedNode.Height(), err.Error())
"cached best node: %v", loadedNode.Height(), err)
}
return nil
})
if err != nil {
t.Errorf("%s", err.Error())
t.Error(err)
}
}
*/
Expand Down Expand Up @@ -701,7 +696,7 @@ func TestTicketDBGeneral(t *testing.T) {
return errLocal
})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

// Cache all of our nodes so that we can check them when we start
Expand Down Expand Up @@ -760,15 +755,15 @@ func TestTicketDBGeneral(t *testing.T) {
err = nodesEqual(loadedNode, bestNode)
if err != nil {
return fmt.Errorf("loaded best node was not same as "+
"in memory best node: %v", err.Error())
"in memory best node: %v", err)
}
loadedNodesForward[i] = loadedNode
}

return nil
})
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

nodesBackward := make([]*Node, testBCHeight+1)
Expand All @@ -792,12 +787,12 @@ func TestTicketDBGeneral(t *testing.T) {
bestNode, err = bestNode.DisconnectNode(lotteryIV, blockUndoData,
ticketsToAdd, nil)
if err != nil {
t.Fatalf(err.Error())
t.Fatal(err)
}

err = nodesEqual(bestNode, nodesForward[i-1])
if err != nil {
t.Errorf("non-equiv stake nodes at height %v: %v", i-1, err.Error())
t.Errorf("non-equiv stake nodes at height %v: %v", i-1, err)
}

// Try again using the database instead of the in memory
Expand All @@ -821,13 +816,12 @@ func TestTicketDBGeneral(t *testing.T) {
return err
})
if err != nil {
t.Errorf("couldn't disconnect using the database: %v",
err.Error())
t.Errorf("couldn't disconnect using the database: %v", err)
}
err = nodesEqual(bestNode, bestNodeUsingDB)
if err != nil {
t.Errorf("non-equiv stake nodes using db when disconnecting: %v",
err.Error())
err)
}

// Write the new best node to the database.
Expand All @@ -845,7 +839,7 @@ func TestTicketDBGeneral(t *testing.T) {
return nil
})
if err != nil {
t.Errorf("%s", err.Error())
t.Error(err)
}

// Check the best node against the loaded best node from
Expand All @@ -861,18 +855,18 @@ func TestTicketDBGeneral(t *testing.T) {
err = nodesEqual(loadedNode, bestNode)
if err != nil {
return fmt.Errorf("loaded best node was not same as "+
"in memory best node: %v", err.Error())
"in memory best node: %v", err)
}
err = nodesEqual(loadedNode, loadedNodesForward[i-1])
if err != nil {
return fmt.Errorf("loaded best node was not same as "+
"previously cached node: %v", err.Error())
"previously cached node: %v", err)
}

return nil
})
if err != nil {
t.Errorf("%s", err.Error())
t.Error(err)
}
}

Expand Down
9 changes: 4 additions & 5 deletions internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,15 +757,14 @@ func handleCreateRawTransaction(_ context.Context, s *Server, cmd interface{}) (

if !(input.Tree == wire.TxTreeRegular ||
input.Tree == wire.TxTreeStake) {
return nil, rpcInvalidError("Tx tree must be regular " +
"or stake")
return nil, rpcInvalidError("Tx tree must be regular or stake")
}

prevOutV := wire.NullValueIn
if input.Amount > 0 {
amt, err := dcrutil.NewAmount(input.Amount)
if err != nil {
return nil, rpcInvalidError(err.Error())
return nil, rpcInvalidError("%v", err)
}
prevOutV = int64(amt)
}
Expand Down Expand Up @@ -1043,7 +1042,7 @@ func handleCreateRawSSRtx(_ context.Context, s *Server, cmd interface{}) (interf
ticketSubmissionAmount := dcrutil.Amount(ticketSubmission.Value)
inputAmount, err := dcrutil.NewAmount(input.Amount)
if err != nil {
return nil, rpcInvalidError(err.Error())
return nil, rpcInvalidError("%v", err)
}
if inputAmount != ticketSubmissionAmount {
return nil, rpcInvalidError("Input amount %v is not equal to ticket "+
Expand Down Expand Up @@ -1093,7 +1092,7 @@ func handleCreateRawSSRtx(_ context.Context, s *Server, cmd interface{}) (interf
revocationTxVersion, s.cfg.ChainParams, prevHeaderBytes,
isAutoRevocationsEnabled)
if err != nil {
return nil, rpcInvalidError(err.Error(), "Invalid SSRtx")
return nil, rpcInvalidError("Invalid SSRtx: %v", err)
}

// Check to make sure our SSRtx was created correctly.
Expand Down

0 comments on commit 5e3adcf

Please sign in to comment.