Skip to content

Commit

Permalink
Remove all context.TODO usage
Browse files Browse the repository at this point in the history
Many wallet methods have gained a context parameter to plumb the
context through correctly instead of creating a new empty context.
  • Loading branch information
jrick committed Aug 20, 2019
1 parent e6b1ced commit 746bab5
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 126 deletions.
10 changes: 5 additions & 5 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (s *Syncer) Run(ctx context.Context) (err error) {
}

s.sidechainsMu.Lock()
prevChain, err := s.wallet.ChainSwitch(&s.sidechains, bestChain, nil)
prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
s.sidechainsMu.Unlock()
if err != nil {
return err
Expand Down Expand Up @@ -575,7 +575,7 @@ func (s *Syncer) winningTickets(ctx context.Context, params json.RawMessage) err
if err != nil {
return err
}
return s.wallet.VoteOnOwnedTickets(winners, block, height)
return s.wallet.VoteOnOwnedTickets(ctx, winners, block, height)
}

func (s *Syncer) blockConnected(ctx context.Context, params json.RawMessage) error {
Expand Down Expand Up @@ -603,7 +603,7 @@ func (s *Syncer) blockConnected(ctx context.Context, params json.RawMessage) err
}
if len(bestChain) != 0 {
var prevChain []*wallet.BlockNode
prevChain, err = s.wallet.ChainSwitch(&s.sidechains, bestChain, s.relevantTxs)
prevChain, err = s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, s.relevantTxs)
if err != nil {
return err
}
Expand Down Expand Up @@ -637,13 +637,13 @@ func (s *Syncer) relevantTxAccepted(ctx context.Context, params json.RawMessage)
if err != nil {
return err
}
return s.wallet.AcceptMempoolTx(tx)
return s.wallet.AcceptMempoolTx(ctx, tx)
}

func (s *Syncer) spentAndMissedTickets(ctx context.Context, params json.RawMessage) error {
missed, err := dcrd.MissedTickets(params)
if err != nil {
return err
}
return s.wallet.RevokeOwnedTickets(missed)
return s.wallet.RevokeOwnedTickets(ctx, missed)
}
30 changes: 15 additions & 15 deletions rpc/jsonrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (s *Server) accountSyncAddressIndex(ctx context.Context, icmd interface{})
// Additional addresses need to be watched. Since addresses are derived
// based on the last used address, this RPC no longer changes the child
// indexes that new addresses are derived from.
return nil, w.ExtendWatchedAddresses(account, branch, index)
return nil, w.ExtendWatchedAddresses(ctx, account, branch, index)
}

func makeMultiSigScript(w *wallet.Wallet, keys []string, nRequired int) ([]byte, error) {
Expand Down Expand Up @@ -483,7 +483,7 @@ func (s *Server) consolidate(ctx context.Context, icmd interface{}) (interface{}

// TODO In the future this should take the optional account and
// only consolidate UTXOs found within that account.
txHash, err := w.Consolidate(cmd.Inputs, account, changeAddr)
txHash, err := w.Consolidate(ctx, cmd.Inputs, account, changeAddr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1040,7 +1040,7 @@ func (s *Server) importPrivKey(ctx context.Context, icmd interface{}) (interface
}

// Import the private key, handling any errors.
_, err = w.ImportPrivateKey(wif)
_, err = w.ImportPrivateKey(ctx, wif)
if err != nil {
switch {
case errors.Is(errors.Exist, err):
Expand Down Expand Up @@ -1091,7 +1091,7 @@ func (s *Server) importScript(ctx context.Context, icmd interface{}) (interface{
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "empty script")
}

err = w.ImportScript(rs)
err = w.ImportScript(ctx, rs)
if err != nil {
switch {
case errors.Is(errors.Exist, err):
Expand Down Expand Up @@ -1129,7 +1129,7 @@ func (s *Server) createNewAccount(ctx context.Context, icmd interface{}) (interf
return nil, errReservedAccountName
}

_, err := w.NextAccount(cmd.Account)
_, err := w.NextAccount(ctx, cmd.Account)
if err != nil {
if errors.Is(errors.Locked, err) {
return nil, rpcErrorf(dcrjson.ErrRPCWalletUnlockNeeded, "creating new accounts requires an unlocked wallet")
Expand Down Expand Up @@ -1262,7 +1262,7 @@ func (s *Server) getNewAddress(ctx context.Context, icmd interface{}) (interface
return nil, err
}

addr, err := w.NewExternalAddress(account, callOpts...)
addr, err := w.NewExternalAddress(ctx, account, callOpts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1293,7 +1293,7 @@ func (s *Server) getRawChangeAddress(ctx context.Context, icmd interface{}) (int
return nil, err
}

addr, err := w.NewChangeAddress(account)
addr, err := w.NewChangeAddress(ctx, account)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2188,7 +2188,7 @@ func (s *Server) purchaseTicket(ctx context.Context, icmd interface{}) (interfac
}
}

hashes, err := w.PurchaseTickets(0, spendLimit, minConf, ticketAddr,
hashes, err := w.PurchaseTickets(ctx, 0, spendLimit, minConf, ticketAddr,
account, numTickets, poolAddr, poolFee, expiry, w.RelayFee(),
ticketFee)
if err != nil {
Expand Down Expand Up @@ -2245,12 +2245,12 @@ func makeOutputs(pairs map[string]dcrutil.Amount, chainParams *chaincfg.Params)
// sendPairs creates and sends payment transactions.
// It returns the transaction hash in string format upon success
// All errors are returned in dcrjson.RPCError format
func sendPairs(w *wallet.Wallet, amounts map[string]dcrutil.Amount, account uint32, minconf int32) (string, error) {
func sendPairs(ctx context.Context, w *wallet.Wallet, amounts map[string]dcrutil.Amount, account uint32, minconf int32) (string, error) {
outputs, err := makeOutputs(amounts, w.ChainParams())
if err != nil {
return "", err
}
txSha, err := w.SendOutputs(outputs, account, minconf)
txSha, err := w.SendOutputs(ctx, outputs, account, minconf)
if err != nil {
if errors.Is(errors.Locked, err) {
return "", errWalletUnlockNeeded
Expand Down Expand Up @@ -2288,7 +2288,7 @@ func (s *Server) redeemMultiSigOut(ctx context.Context, icmd interface{}) (inter
}
} else {
account := uint32(udb.DefaultAccountNum)
addr, err = w.NewInternalAddress(account, wallet.WithGapPolicyWrap())
addr, err = w.NewInternalAddress(ctx, account, wallet.WithGapPolicyWrap())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2602,7 +2602,7 @@ func (s *Server) sendFrom(ctx context.Context, icmd interface{}) (interface{}, e
cmd.ToAddress: amt,
}

return sendPairs(w, pairs, account, minConf)
return sendPairs(ctx, w, pairs, account, minConf)
}

// sendMany handles a sendmany RPC request by creating a new transaction
Expand Down Expand Up @@ -2644,7 +2644,7 @@ func (s *Server) sendMany(ctx context.Context, icmd interface{}) (interface{}, e
pairs[k] = amt
}

return sendPairs(w, pairs, account, minConf)
return sendPairs(ctx, w, pairs, account, minConf)
}

// sendToAddress handles a sendtoaddress RPC request by creating a new
Expand Down Expand Up @@ -2681,7 +2681,7 @@ func (s *Server) sendToAddress(ctx context.Context, icmd interface{}) (interface
}

// sendtoaddress always spends from the default account, this matches bitcoind
return sendPairs(w, pairs, udb.DefaultAccountNum, 1)
return sendPairs(ctx, w, pairs, udb.DefaultAccountNum, 1)
}

// sendToMultiSig handles a sendtomultisig RPC request by creating a new
Expand Down Expand Up @@ -2746,7 +2746,7 @@ func (s *Server) sendToMultiSig(ctx context.Context, icmd interface{}) (interfac
}

tx, addr, script, err :=
w.CreateMultisigTx(account, amount, pubkeys, nrequired, minconf)
w.CreateMultisigTx(ctx, account, amount, pubkeys, nrequired, minconf)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (s *walletServer) NextAccount(ctx context.Context, req *pb.NextAccountReque
return nil, translateError(err)
}

account, err := s.wallet.NextAccount(req.AccountName)
account, err := s.wallet.NextAccount(ctx, req.AccountName)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -467,12 +467,12 @@ func (s *walletServer) NextAddress(ctx context.Context, req *pb.NextAddressReque
)
switch req.Kind {
case pb.NextAddressRequest_BIP0044_EXTERNAL:
addr, err = s.wallet.NewExternalAddress(req.Account, callOpts...)
addr, err = s.wallet.NewExternalAddress(ctx, req.Account, callOpts...)
if err != nil {
return nil, translateError(err)
}
case pb.NextAddressRequest_BIP0044_INTERNAL:
addr, err = s.wallet.NewInternalAddress(req.Account, callOpts...)
addr, err = s.wallet.NewInternalAddress(ctx, req.Account, callOpts...)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -541,7 +541,7 @@ func (s *walletServer) ImportPrivateKey(ctx context.Context, req *pb.ImportPriva
return nil, err
}

_, err = s.wallet.ImportPrivateKey(wif)
_, err = s.wallet.ImportPrivateKey(ctx, wif)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -608,7 +608,7 @@ func (s *walletServer) ImportScript(ctx context.Context,
return nil, err
}

err = s.wallet.ImportScript(req.Script)
err = s.wallet.ImportScript(ctx, req.Script)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -915,7 +915,7 @@ func (s *walletServer) FundTransaction(ctx context.Context, req *pb.FundTransact

var changeScript []byte
if req.IncludeChangeScript && inputDetail.Amount > dcrutil.Amount(req.TargetAmount) {
changeAddr, err := s.wallet.NewChangeAddress(req.Account)
changeAddr, err := s.wallet.NewChangeAddress(ctx, req.Account)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -1524,7 +1524,7 @@ func (s *walletServer) PublishTransaction(ctx context.Context, req *pb.PublishTr
"Bytes do not represent a valid raw transaction: %v", err)
}

txHash, err := s.wallet.PublishTransaction(&msgTx, req.SignedTransaction, n)
txHash, err := s.wallet.PublishTransaction(ctx, &msgTx, req.SignedTransaction, n)
if err != nil {
return nil, translateError(err)
}
Expand Down Expand Up @@ -1608,7 +1608,7 @@ func (s *walletServer) PurchaseTickets(ctx context.Context,
return nil, translateError(err)
}

resp, err := s.wallet.PurchaseTickets(0, spendLimit, minConf,
resp, err := s.wallet.PurchaseTickets(ctx, 0, spendLimit, minConf,
ticketAddr, req.Account, numTickets, poolAddr, req.PoolFees,
expiry, txFee, ticketFee)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (s *Syncer) handleTxInvs(ctx context.Context, rp *p2p.RemotePeer, hashes []
// Save any relevant transaction.
relevant := s.filterRelevant(txs)
for _, tx := range relevant {
err := s.wallet.AcceptMempoolTx(tx)
err := s.wallet.AcceptMempoolTx(ctx, tx)
if err != nil {
op := errors.Opf(opf, rp.RemoteAddr())
log.Warn(errors.E(op, err))
Expand Down Expand Up @@ -946,7 +946,7 @@ func (s *Syncer) handleBlockAnnouncements(ctx context.Context, rp *p2p.RemotePee
}
}

prevChain, err := s.wallet.ChainSwitch(&s.sidechains, bestChain, matchingTxs)
prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, matchingTxs)
if err != nil {
return err
}
Expand Down Expand Up @@ -1116,7 +1116,7 @@ func (s *Syncer) getHeaders(ctx context.Context, rp *p2p.RemotePeer) error {
return err
}

prevChain, err := s.wallet.ChainSwitch(&s.sidechains, bestChain, nil)
prevChain, err := s.wallet.ChainSwitch(ctx, &s.sidechains, bestChain, nil)
if err != nil {
s.sidechainMu.Unlock()
return err
Expand Down
2 changes: 1 addition & 1 deletion ticketbuyer/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/decred/dcrwallet/ticketbuyer/v4

require (
github.com/decred/dcrd/chaincfg/chainhash v1.0.1
github.com/decred/dcrd/chaincfg/chainhash v1.0.2
github.com/decred/dcrd/dcrutil/v2 v2.0.0
github.com/decred/dcrwallet/errors v1.1.0
github.com/decred/dcrwallet/wallet/v3 v3.0.0-00010101000000-000000000000
Expand Down
Loading

0 comments on commit 746bab5

Please sign in to comment.