From 203bf7d2e62f0e263740b36552c33fb4689bcd60 Mon Sep 17 00:00:00 2001 From: Jamie Holdstock Date: Mon, 5 Jun 2023 15:39:01 +0100 Subject: [PATCH] build: Update linter to 1.53.2 (#384) * build: Update linter to 1.53.2 * database: Rename helpers.go to encoding.go * database: Ignore json.Marshal errors. --- .github/workflows/go.yml | 2 +- cmd/v3tool/dcrwallet.go | 2 +- cmd/vspd/config.go | 12 ++-- database/{helpers.go => encoding.go} | 12 +++- .../{helpers_test.go => encoding_test.go} | 0 database/ticket.go | 21 +------ webapi/middleware.go | 63 ++++++++++--------- webapi/setaltsignaddr_test.go | 4 +- 8 files changed, 56 insertions(+), 60 deletions(-) rename database/{helpers.go => encoding.go} (76%) rename database/{helpers_test.go => encoding_test.go} (100%) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 44b6ec8..dd5ff7e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -17,6 +17,6 @@ jobs: - name: Build run: go build ./... - name: Install Linters - run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.51.1" + run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.2" - name: Test and Lint run: ./run_tests.sh diff --git a/cmd/v3tool/dcrwallet.go b/cmd/v3tool/dcrwallet.go index 681491a..f14cf41 100644 --- a/cmd/v3tool/dcrwallet.go +++ b/cmd/v3tool/dcrwallet.go @@ -97,7 +97,7 @@ func (w *dcrwallet) createFeeTx(feeAddress string, fee int64) (string, error) { return signedTx.Hex, nil } -func (w *dcrwallet) SignMessage(ctx context.Context, msg string, commitmentAddr stdaddr.Address) ([]byte, error) { +func (w *dcrwallet) SignMessage(_ context.Context, msg string, commitmentAddr stdaddr.Address) ([]byte, error) { var signature string err := w.Call(context.TODO(), "signmessage", &signature, commitmentAddr.String(), msg) if err != nil { diff --git a/cmd/vspd/config.go b/cmd/vspd/config.go index 0878681..d96140c 100644 --- a/cmd/vspd/config.go +++ b/cmd/vspd/config.go @@ -445,12 +445,12 @@ func loadConfig() (*config, error) { // Exit with success os.Exit(0) - } else { - // If database does not exist, return error. - if !fileExists(cfg.dbPath) { - return nil, fmt.Errorf("no database exists in %s. Run vspd with the"+ - " --feexpub option to initialize one", dataDir) - } + } + + // If database does not exist, return error. + if !fileExists(cfg.dbPath) { + return nil, fmt.Errorf("no database exists in %s. Run vspd with the"+ + " --feexpub option to initialize one", dataDir) } return &cfg, nil diff --git a/database/helpers.go b/database/encoding.go similarity index 76% rename from database/helpers.go rename to database/encoding.go index a000757..dda572f 100644 --- a/database/helpers.go +++ b/database/encoding.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 The Decred developers +// Copyright (c) 2022-2023 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -28,6 +28,16 @@ func bytesToStringMap(bytes []byte) (map[string]string, error) { return stringMap, nil } +func stringMapToBytes(stringMap map[string]string) []byte { + // json.Marshal will only return an error if passed an invalid struct. + // Structs are all known and hard-coded, so errors are never expected here. + bytes, err := json.Marshal(stringMap) + if err != nil { + panic(err) + } + return bytes +} + func int64ToBytes(i int64) []byte { bytes := make([]byte, 8) binary.LittleEndian.PutUint64(bytes, uint64(i)) diff --git a/database/helpers_test.go b/database/encoding_test.go similarity index 100% rename from database/helpers_test.go rename to database/encoding_test.go diff --git a/database/ticket.go b/database/ticket.go index 3da1f9c..a5e57d8 100644 --- a/database/ticket.go +++ b/database/ticket.go @@ -5,7 +5,6 @@ package database import ( - "encoding/json" "fmt" "time" @@ -165,28 +164,14 @@ func putTicketInBucket(bkt *bolt.Bucket, ticket Ticket) error { if err = bkt.Put(confirmedK, boolToBytes(ticket.Confirmed)); err != nil { return err } - - jsonTSpend, err := json.Marshal(ticket.TSpendPolicy) - if err != nil { + if err = bkt.Put(tSpendPolicyK, stringMapToBytes(ticket.TSpendPolicy)); err != nil { return err } - if err = bkt.Put(tSpendPolicyK, jsonTSpend); err != nil { + if err = bkt.Put(treasuryPolicyK, stringMapToBytes(ticket.TreasuryPolicy)); err != nil { return err } - jsonTreasury, err := json.Marshal(ticket.TreasuryPolicy) - if err != nil { - return err - } - if err = bkt.Put(treasuryPolicyK, jsonTreasury); err != nil { - return err - } - - jsonVoteChoices, err := json.Marshal(ticket.VoteChoices) - if err != nil { - return err - } - return bkt.Put(voteChoicesK, jsonVoteChoices) + return bkt.Put(voteChoicesK, stringMapToBytes(ticket.VoteChoices)) } func getTicketFromBkt(bkt *bolt.Bucket) (Ticket, error) { diff --git a/webapi/middleware.go b/webapi/middleware.go index dbb7b59..0e0c619 100644 --- a/webapi/middleware.go +++ b/webapi/middleware.go @@ -227,44 +227,44 @@ func (s *Server) broadcastTicket(c *gin.Context) { } _, err = dcrdClient.GetRawTransaction(parentHash.String()) - var e *wsrpc.Error - if err == nil { - // No error means dcrd already knows the parent tx, nothing to do. - } else if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo { - // ErrNoTxInfo means local dcrd is not aware of the parent. We have - // the hex, so we can broadcast it here. + if err != nil { + var e *wsrpc.Error + if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo { + // ErrNoTxInfo means local dcrd is not aware of the parent. We have + // the hex, so we can broadcast it here. - // Before broadcasting, check that the provided parent hex is - // actually the parent of the ticket. - var found bool - for _, txIn := range msgTx.TxIn { - if !txIn.PreviousOutPoint.Hash.IsEqual(&parentHash) { - continue + // Before broadcasting, check that the provided parent hex is + // actually the parent of the ticket. + var found bool + for _, txIn := range msgTx.TxIn { + if !txIn.PreviousOutPoint.Hash.IsEqual(&parentHash) { + continue + } + found = true + break } - found = true - break - } - if !found { - s.log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash) - s.sendErrorWithMsg("invalid ticket parent", types.ErrBadRequest, c) - return - } + if !found { + s.log.Errorf("%s: Invalid ticket parent (ticketHash=%s)", funcName, request.TicketHash) + s.sendErrorWithMsg("invalid ticket parent", types.ErrBadRequest, c) + return + } - s.log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash) - err = dcrdClient.SendRawTransaction(request.ParentHex) - if err != nil { - s.log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v", + s.log.Debugf("%s: Broadcasting parent tx %s (ticketHash=%s)", funcName, parentHash, request.TicketHash) + err = dcrdClient.SendRawTransaction(request.ParentHex) + if err != nil { + s.log.Errorf("%s: dcrd.SendRawTransaction for parent tx failed (ticketHash=%s): %v", + funcName, request.TicketHash, err) + s.sendError(types.ErrCannotBroadcastTicket, c) + return + } + + } else { + s.log.Errorf("%s: dcrd.GetRawTransaction for ticket parent failed (ticketHash=%s): %v", funcName, request.TicketHash, err) - s.sendError(types.ErrCannotBroadcastTicket, c) + s.sendError(types.ErrInternalError, c) return } - - } else { - s.log.Errorf("%s: dcrd.GetRawTransaction for ticket parent failed (ticketHash=%s): %v", - funcName, request.TicketHash, err) - s.sendError(types.ErrInternalError, c) - return } // Check if local dcrd already knows the ticket. @@ -276,6 +276,7 @@ func (s *Server) broadcastTicket(c *gin.Context) { // ErrNoTxInfo means local dcrd is not aware of the ticket. We have the // hex, so we can broadcast it here. + var e *wsrpc.Error if errors.As(err, &e) && e.Code == rpc.ErrNoTxInfo { s.log.Debugf("%s: Broadcasting ticket (ticketHash=%s)", funcName, request.TicketHash) err = dcrdClient.SendRawTransaction(request.TicketHex) diff --git a/webapi/setaltsignaddr_test.go b/webapi/setaltsignaddr_test.go index 005c3c9..e421b61 100644 --- a/webapi/setaltsignaddr_test.go +++ b/webapi/setaltsignaddr_test.go @@ -121,11 +121,11 @@ type testNode struct { existsLiveTicketErr error } -func (n *testNode) ExistsLiveTicket(ticketHash string) (bool, error) { +func (n *testNode) ExistsLiveTicket(_ string) (bool, error) { return n.existsLiveTicket, n.existsLiveTicketErr } -func (n *testNode) GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, error) { +func (n *testNode) GetRawTransaction(_ string) (*dcrdtypes.TxRawResult, error) { return n.getRawTransaction, n.getRawTransactionErr }