diff --git a/blockchain/stake/staketx.go b/blockchain/stake/staketx.go index 2ea21b012..e7a0de316 100644 --- a/blockchain/stake/staketx.go +++ b/blockchain/stake/staketx.go @@ -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. // @@ -10,6 +10,7 @@ package stake import ( "bytes" "encoding/binary" + "errors" "fmt" "math" "math/big" @@ -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 { diff --git a/blockchain/stake/tickets_test.go b/blockchain/stake/tickets_test.go index 7bcbe0a1a..1976c1d8b 100644 --- a/blockchain/stake/tickets_test.go +++ b/blockchain/stake/tickets_test.go @@ -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. @@ -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 @@ -409,7 +409,7 @@ func TestTicketDBLongChain(t *testing.T) { bestNode, err = bestNode.DisconnectNode(lotteryIV, blockUndoData, ticketsToAdd, nil) if err != nil { - t.Errorf(err.Error()) + t.Error(err) } } @@ -417,7 +417,7 @@ func TestTicketDBLongChain(t *testing.T) { 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, @@ -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 @@ -522,7 +522,7 @@ 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. @@ -530,8 +530,7 @@ func TestTicketDBLongChain(t *testing.T) { 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. @@ -539,13 +538,12 @@ func TestTicketDBLongChain(t *testing.T) { 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 } @@ -553,7 +551,7 @@ func TestTicketDBLongChain(t *testing.T) { return nil }) if err != nil { - t.Fatalf(err.Error()) + t.Fatal(err) } nodesBackward := make([]*Node, testBCHeight+1) @@ -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 @@ -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. @@ -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 @@ -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) } } */ @@ -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 @@ -760,7 +755,7 @@ 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 } @@ -768,7 +763,7 @@ func TestTicketDBGeneral(t *testing.T) { return nil }) if err != nil { - t.Fatalf(err.Error()) + t.Fatal(err) } nodesBackward := make([]*Node, testBCHeight+1) @@ -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 @@ -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. @@ -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 @@ -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) } } diff --git a/internal/rpcserver/rpcserver.go b/internal/rpcserver/rpcserver.go index 50fef4379..ad67d6dbd 100644 --- a/internal/rpcserver/rpcserver.go +++ b/internal/rpcserver/rpcserver.go @@ -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) } @@ -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 "+ @@ -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.