diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index b5185f69..80851448 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.53.3" + run: "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2" - name: Test and Lint run: ./run_tests.sh diff --git a/.golangci.yml b/.golangci.yml index 2492e503..f79c3b87 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,19 +6,28 @@ linters: enable: - asciicheck - bidichk + - containedctx + - dupword - durationcheck - errcheck - errchkjson + - errorlint - exhaustive - exportloopref - goconst + - godot - gofmt - goimports - gosimple - govet - ineffassign + - makezero - misspell - nilerr + - nosprintfhostport + - prealloc + - predeclared + - reassign - revive - staticcheck - tparallel diff --git a/client/client.go b/client/client.go index db99274d..924a4e8b 100644 --- a/client/client.go +++ b/client/client.go @@ -220,7 +220,7 @@ func (c *Client) do(ctx context.Context, method, path string, addr stdaddr.Addre err = ValidateServerSignature(reply, respBody, c.PubKey) if err != nil { - return fmt.Errorf("authenticate server response: %v", err) + return fmt.Errorf("authenticate server response: %w", err) } err = json.Unmarshal(respBody, resp) diff --git a/database/database.go b/database/database.go index c1e2882b..2cf2a58a 100644 --- a/database/database.go +++ b/database/database.go @@ -58,7 +58,7 @@ const ( backupFileMode = 0600 ) -// backupMtx should be held when writing to the database backup file +// backupMtx should be held when writing to the database backup file. var backupMtx sync.Mutex // writeHotBackupFile writes a backup of the database file while the database diff --git a/rpc/dcrd.go b/rpc/dcrd.go index 93fa73a6..68240add 100644 --- a/rpc/dcrd.go +++ b/rpc/dcrd.go @@ -37,7 +37,6 @@ const ( // of JSON encoding. type DcrdRPC struct { Caller - ctx context.Context } type DcrdConnect struct { @@ -81,7 +80,7 @@ func (d *DcrdConnect) Client() (*DcrdRPC, string, error) { // If this is a reused connection, we don't need to validate the dcrd config // again. if !newConnection { - return &DcrdRPC{c, ctx}, d.client.addr, nil + return &DcrdRPC{c}, d.client.addr, nil } // Verify dcrd is at the required api version. @@ -139,7 +138,7 @@ func (d *DcrdConnect) Client() (*DcrdRPC, string, error) { d.log.Debugf("Connected to dcrd") - return &DcrdRPC{c, ctx}, d.client.addr, nil + return &DcrdRPC{c}, d.client.addr, nil } // GetRawTransaction uses getrawtransaction RPC to retrieve details about the @@ -147,7 +146,7 @@ func (d *DcrdConnect) Client() (*DcrdRPC, string, error) { func (c *DcrdRPC) GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, error) { verbose := 1 var resp dcrdtypes.TxRawResult - err := c.Call(c.ctx, "getrawtransaction", &resp, txHash, verbose) + err := c.Call(context.TODO(), "getrawtransaction", &resp, txHash, verbose) if err != nil { return nil, err } @@ -157,7 +156,7 @@ func (c *DcrdRPC) GetRawTransaction(txHash string) (*dcrdtypes.TxRawResult, erro // DecodeRawTransaction uses decoderawtransaction RPC to decode raw transaction bytes. func (c *DcrdRPC) DecodeRawTransaction(txHex string) (*dcrdtypes.TxRawDecodeResult, error) { var resp dcrdtypes.TxRawDecodeResult - err := c.Call(c.ctx, "decoderawtransaction", &resp, txHex) + err := c.Call(context.TODO(), "decoderawtransaction", &resp, txHex) if err != nil { return nil, err } @@ -169,7 +168,7 @@ func (c *DcrdRPC) DecodeRawTransaction(txHex string) (*dcrdtypes.TxRawDecodeResu // the network. It ignores errors caused by duplicate transactions. func (c *DcrdRPC) SendRawTransaction(txHex string) error { const allowHighFees = false - err := c.Call(c.ctx, "sendrawtransaction", nil, txHex, allowHighFees) + err := c.Call(context.TODO(), "sendrawtransaction", nil, txHex, allowHighFees) if err != nil { // Ignore errors caused by the transaction already existing in the @@ -200,7 +199,7 @@ func (c *DcrdRPC) SendRawTransaction(txHex string) error { // agenda has activated on the current network. func (c *DcrdRPC) IsDCP0010Active() (bool, error) { var info dcrdtypes.GetBlockChainInfoResult - err := c.Call(c.ctx, "getblockchaininfo", &info) + err := c.Call(context.TODO(), "getblockchaininfo", &info) if err != nil { return false, err } @@ -216,14 +215,14 @@ func (c *DcrdRPC) IsDCP0010Active() (bool, error) { // NotifyBlocks uses notifyblocks RPC to request new block notifications from dcrd. func (c *DcrdRPC) NotifyBlocks() error { - return c.Call(c.ctx, "notifyblocks", nil) + return c.Call(context.TODO(), "notifyblocks", nil) } // GetBestBlockHeader uses getbestblockhash RPC, followed by getblockheader RPC, // to retrieve the header of the best block known to the dcrd instance. func (c *DcrdRPC) GetBestBlockHeader() (*dcrdtypes.GetBlockHeaderVerboseResult, error) { var bestBlockHash string - err := c.Call(c.ctx, "getbestblockhash", &bestBlockHash) + err := c.Call(context.TODO(), "getbestblockhash", &bestBlockHash) if err != nil { return nil, err } @@ -240,7 +239,7 @@ func (c *DcrdRPC) GetBestBlockHeader() (*dcrdtypes.GetBlockHeaderVerboseResult, func (c *DcrdRPC) GetBlockHeaderVerbose(blockHash string) (*dcrdtypes.GetBlockHeaderVerboseResult, error) { const verbose = true var blockHeader dcrdtypes.GetBlockHeaderVerboseResult - err := c.Call(c.ctx, "getblockheader", &blockHeader, blockHash, verbose) + err := c.Call(context.TODO(), "getblockheader", &blockHeader, blockHash, verbose) if err != nil { return nil, err } @@ -251,7 +250,7 @@ func (c *DcrdRPC) GetBlockHeaderVerbose(blockHash string) (*dcrdtypes.GetBlockHe // hash is a live ticket known to the dcrd instance. func (c *DcrdRPC) ExistsLiveTicket(ticketHash string) (bool, error) { var exists string - err := c.Call(c.ctx, "existslivetickets", &exists, []string{ticketHash}) + err := c.Call(context.TODO(), "existslivetickets", &exists, []string{ticketHash}) if err != nil { return false, err } diff --git a/rpc/dcrwallet.go b/rpc/dcrwallet.go index 672dda94..d3201886 100644 --- a/rpc/dcrwallet.go +++ b/rpc/dcrwallet.go @@ -23,7 +23,6 @@ var ( // of JSON encoding. type WalletRPC struct { Caller - ctx context.Context } type WalletConnect struct { @@ -73,7 +72,7 @@ func (w *WalletConnect) Clients() ([]*WalletRPC, []string) { // If this is a reused connection, we don't need to validate the // dcrwallet config again. if !newConnection { - walletClients = append(walletClients, &WalletRPC{c, ctx}) + walletClients = append(walletClients, &WalletRPC{c}) continue } @@ -123,7 +122,7 @@ func (w *WalletConnect) Clients() ([]*WalletRPC, []string) { } // Verify dcrwallet is voting and unlocked. - walletRPC := &WalletRPC{c, ctx} + walletRPC := &WalletRPC{c} walletInfo, err := walletRPC.WalletInfo() if err != nil { w.log.Errorf("dcrwallet.WalletInfo error (wallet=%s): %v", c.String(), err) @@ -160,7 +159,7 @@ func (w *WalletConnect) Clients() ([]*WalletRPC, []string) { // dcrwallet instance is configured. func (c *WalletRPC) WalletInfo() (*wallettypes.WalletInfoResult, error) { var walletInfo wallettypes.WalletInfoResult - err := c.Call(c.ctx, "walletinfo", &walletInfo) + err := c.Call(context.TODO(), "walletinfo", &walletInfo) if err != nil { return nil, err } @@ -173,12 +172,12 @@ func (c *WalletRPC) AddTicketForVoting(votingWIF, blockHash, txHex string) error const label = "imported" const rescan = false const scanFrom = 0 - err := c.Call(c.ctx, "importprivkey", nil, votingWIF, label, rescan, scanFrom) + err := c.Call(context.TODO(), "importprivkey", nil, votingWIF, label, rescan, scanFrom) if err != nil { return fmt.Errorf("importprivkey failed: %w", err) } - err = c.Call(c.ctx, "addtransaction", nil, blockHash, txHex) + err = c.Call(context.TODO(), "addtransaction", nil, blockHash, txHex) if err != nil { return fmt.Errorf("addtransaction failed: %w", err) } @@ -189,14 +188,14 @@ func (c *WalletRPC) AddTicketForVoting(votingWIF, blockHash, txHex string) error // SetVoteChoice uses setvotechoice RPC to set the vote choice on the given // agenda, for the given ticket. func (c *WalletRPC) SetVoteChoice(agenda, choice, ticketHash string) error { - return c.Call(c.ctx, "setvotechoice", nil, agenda, choice, ticketHash) + return c.Call(context.TODO(), "setvotechoice", nil, agenda, choice, ticketHash) } // GetBestBlockHeight uses getblockcount RPC to query the height of the best // block known by the dcrwallet instance. func (c *WalletRPC) GetBestBlockHeight() (int64, error) { var height int64 - err := c.Call(c.ctx, "getblockcount", &height) + err := c.Call(context.TODO(), "getblockcount", &height) if err != nil { return 0, err } @@ -207,7 +206,7 @@ func (c *WalletRPC) GetBestBlockHeight() (int64, error) { // known by this dcrwallet instance. func (c *WalletRPC) TicketInfo(startHeight int64) (map[string]*wallettypes.TicketInfoResult, error) { var result []*wallettypes.TicketInfoResult - err := c.Call(c.ctx, "ticketinfo", &result, startHeight) + err := c.Call(context.TODO(), "ticketinfo", &result, startHeight) if err != nil { return nil, err } @@ -225,17 +224,17 @@ func (c *WalletRPC) TicketInfo(startHeight int64) (map[string]*wallettypes.Ticke // RescanFrom uses rescanwallet RPC to trigger the wallet to perform a rescan // from the specified block height. func (c *WalletRPC) RescanFrom(fromHeight int64) error { - return c.Call(c.ctx, "rescanwallet", nil, fromHeight) + return c.Call(context.TODO(), "rescanwallet", nil, fromHeight) } // SetTreasuryPolicy sets the specified tickets voting policy for all tspends // published by the given treasury key. func (c *WalletRPC) SetTreasuryPolicy(key, policy, ticket string) error { - return c.Call(c.ctx, "settreasurypolicy", nil, key, policy, ticket) + return c.Call(context.TODO(), "settreasurypolicy", nil, key, policy, ticket) } // SetTSpendPolicy sets the specified tickets voting policy for a single tspend // identified by its hash. func (c *WalletRPC) SetTSpendPolicy(tSpend, policy, ticket string) error { - return c.Call(c.ctx, "settspendpolicy", nil, tSpend, policy, ticket) + return c.Call(context.TODO(), "settspendpolicy", nil, tSpend, policy, ticket) }