rpc: Do not store contexts in structs.

Documentation for the context package suggests that Contexts should
never be stored in structs.
This commit is contained in:
jholdstock 2023-08-23 13:56:11 +01:00 committed by Jamie Holdstock
parent 49b9db1a64
commit e80ebfea13
2 changed files with 21 additions and 23 deletions

View File

@ -37,7 +37,6 @@ const (
// of JSON encoding.
type DcrdRPC struct {
Caller
ctx context.Context
}
type DcrdConnect struct {
@ -79,7 +78,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.
@ -137,7 +136,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
@ -145,7 +144,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
}
@ -155,7 +154,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
}
@ -167,7 +166,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
@ -198,7 +197,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
}
@ -214,14 +213,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
}
@ -238,7 +237,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
}
@ -249,7 +248,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
}

View File

@ -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)
}