Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpctest: Use context provided by the user #3012

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/rpcserver/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func TestRpcServer(t *testing.T) {
// Initialize the primary mining node with a chain of length 125,
// providing 25 mature coinbases to allow spending from for testing
// purposes.
if err := harness.SetUp(true, 25); err != nil {
ctx := context.Background()
if err := harness.SetUp(ctx, true, 25); err != nil {
// Even though the harness was not fully setup, it still needs
// to be torn down to ensure all resources such as temp
// directories are cleaned up. The error is intentionally
Expand Down Expand Up @@ -163,7 +164,6 @@ func TestRpcServer(t *testing.T) {
},
}

ctx := context.Background()
for _, test := range tests {
test.f(ctx, harness, t)
t.Logf("=== Running test: %v ===", test.name)
Expand Down
11 changes: 6 additions & 5 deletions internal/rpcserver/treasury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ func TestTreasury(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = hn.SetUp(false, 0)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = hn.SetUp(ctx, false, 0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -244,9 +248,6 @@ func TestTreasury(t *testing.T) {
t.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Create the voting wallet.
vw, err := rpctest.NewVotingWallet(ctx, hn)
if err != nil {
Expand Down Expand Up @@ -288,7 +289,7 @@ func TestTreasury(t *testing.T) {
taddPrevOuts := make([]*wire.OutPoint, nbTAddPrevOuts)
for i := 0; i < nbTAddPrevOuts; i++ {
txOut := newTxOut(int64(taddInAmt), p2pkhScriptVer, p2pkhScript)
txHash, err := hn.SendOutputs([]*wire.TxOut{txOut}, defaultFeeRate)
txHash, err := hn.SendOutputs(ctx, []*wire.TxOut{txOut}, defaultFeeRate)
if err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 11 additions & 11 deletions rpctest/memwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (m *memWallet) UnwindBlock(header []byte) {
// newAddress returns a new address from the wallet's hd key chain. It also
// loads the address into the RPC client's transaction filter to ensure any
// transactions that involve it are delivered via the notifications.
func (m *memWallet) newAddress() (stdaddr.Address, error) {
func (m *memWallet) newAddress(ctx context.Context) (stdaddr.Address, error) {
tracef(m.t, "memwallet.newAddress")
defer tracef(m.t, "memwallet.newAddress exit")

Expand All @@ -377,7 +377,7 @@ func (m *memWallet) newAddress() (stdaddr.Address, error) {
return nil, err
}

err = m.rpc.LoadTxFilter(context.Background(), false,
err = m.rpc.LoadTxFilter(ctx, false,
[]stdaddr.Address{addr}, nil)
if err != nil {
return nil, err
Expand All @@ -393,11 +393,11 @@ func (m *memWallet) newAddress() (stdaddr.Address, error) {
// NewAddress returns a fresh address spendable by the wallet.
//
// This function is safe for concurrent access.
func (m *memWallet) NewAddress() (stdaddr.Address, error) {
func (m *memWallet) NewAddress(ctx context.Context) (stdaddr.Address, error) {
m.Lock()
defer m.Unlock()

return m.newAddress()
return m.newAddress(ctx)
}

// fundTx attempts to fund a transaction sending amt coins. The coins are
Expand All @@ -406,7 +406,7 @@ func (m *memWallet) NewAddress() (stdaddr.Address, error) {
// atoms-per-byte.
//
// NOTE: The memWallet's mutex must be held when this function is called.
func (m *memWallet) fundTx(tx *wire.MsgTx, amt dcrutil.Amount, feeRate dcrutil.Amount) error {
func (m *memWallet) fundTx(ctx context.Context, tx *wire.MsgTx, amt dcrutil.Amount, feeRate dcrutil.Amount) error {
tracef(m.t, "memwallet.fundTx")
defer tracef(m.t, "memwallet.fundTx exit")

Expand Down Expand Up @@ -449,7 +449,7 @@ func (m *memWallet) fundTx(tx *wire.MsgTx, amt dcrutil.Amount, feeRate dcrutil.A
// output to the transaction reserved for change.
changeVal := amtSelected - amt - reqFee
if changeVal > 0 {
addr, err := m.newAddress()
addr, err := m.newAddress(ctx)
if err != nil {
return err
}
Expand All @@ -473,24 +473,24 @@ func (m *memWallet) fundTx(tx *wire.MsgTx, amt dcrutil.Amount, feeRate dcrutil.A
// SendOutputs creates, then sends a transaction paying to the specified output
// while observing the passed fee rate. The passed fee rate should be expressed
// in atoms-per-byte.
func (m *memWallet) SendOutputs(outputs []*wire.TxOut, feeRate dcrutil.Amount) (*chainhash.Hash, error) {
func (m *memWallet) SendOutputs(ctx context.Context, outputs []*wire.TxOut, feeRate dcrutil.Amount) (*chainhash.Hash, error) {
tracef(m.t, "memwallet.SendOutputs")
defer tracef(m.t, "memwallet.SendOutputs exit")

tx, err := m.CreateTransaction(outputs, feeRate)
tx, err := m.CreateTransaction(ctx, outputs, feeRate)
if err != nil {
return nil, err
}

return m.rpc.SendRawTransaction(context.Background(), tx, true)
return m.rpc.SendRawTransaction(ctx, tx, true)
}

// CreateTransaction returns a fully signed transaction paying to the specified
// outputs while observing the desired fee rate. The passed fee rate should be
// expressed in atoms-per-byte.
//
// This function is safe for concurrent access.
func (m *memWallet) CreateTransaction(outputs []*wire.TxOut, feeRate dcrutil.Amount) (*wire.MsgTx, error) {
func (m *memWallet) CreateTransaction(ctx context.Context, outputs []*wire.TxOut, feeRate dcrutil.Amount) (*wire.MsgTx, error) {
tracef(m.t, "memwallet.CreateTransaction")
defer tracef(m.t, "memwallet.CreateTransaction exit")

Expand All @@ -508,7 +508,7 @@ func (m *memWallet) CreateTransaction(outputs []*wire.TxOut, feeRate dcrutil.Amo
}

// Attempt to fund the transaction with spendable utxos.
if err := m.fundTx(tx, outputAmt, feeRate); err != nil {
if err := m.fundTx(ctx, tx, outputAmt, feeRate); err != nil {
return nil, err
}

Expand Down
15 changes: 7 additions & 8 deletions rpctest/rpc_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func New(t *testing.T, activeNet *chaincfg.Params, handlers *rpcclient.Notificat
//
// NOTE: This method and TearDown should always be called from the same
// goroutine as they are not concurrent safe.
func (h *Harness) SetUp(createTestChain bool, numMatureOutputs uint32) error {
func (h *Harness) SetUp(ctx context.Context, createTestChain bool, numMatureOutputs uint32) error {
// Start the dcrd node itself. This spawns a new process which will be
// managed
if err := h.node.start(); err != nil {
Expand All @@ -248,7 +248,6 @@ func (h *Harness) SetUp(createTestChain bool, numMatureOutputs uint32) error {
if err := h.connectRPCClient(); err != nil {
return err
}
ctx := context.Background()
h.wallet.Start()

// Filter transactions that pay to the coinbase associated with the
Expand Down Expand Up @@ -361,8 +360,8 @@ func (h *Harness) connectRPCClient() error {
// wallet.
//
// This function is safe for concurrent access.
func (h *Harness) NewAddress() (stdaddr.Address, error) {
return h.wallet.NewAddress()
func (h *Harness) NewAddress(ctx context.Context) (stdaddr.Address, error) {
return h.wallet.NewAddress(ctx)
}

// ConfirmedBalance returns the confirmed balance of the Harness' internal
Expand All @@ -378,8 +377,8 @@ func (h *Harness) ConfirmedBalance() dcrutil.Amount {
// according to targetOutputs.
//
// This function is safe for concurrent access.
func (h *Harness) SendOutputs(targetOutputs []*wire.TxOut, feeRate dcrutil.Amount) (*chainhash.Hash, error) {
return h.wallet.SendOutputs(targetOutputs, feeRate)
func (h *Harness) SendOutputs(ctx context.Context, targetOutputs []*wire.TxOut, feeRate dcrutil.Amount) (*chainhash.Hash, error) {
return h.wallet.SendOutputs(ctx, targetOutputs, feeRate)
}

// CreateTransaction returns a fully signed transaction paying to the specified
Expand All @@ -392,8 +391,8 @@ func (h *Harness) SendOutputs(targetOutputs []*wire.TxOut, feeRate dcrutil.Amoun
// returned to the pool of spendable outputs.
//
// This function is safe for concurrent access.
func (h *Harness) CreateTransaction(targetOutputs []*wire.TxOut, feeRate dcrutil.Amount) (*wire.MsgTx, error) {
return h.wallet.CreateTransaction(targetOutputs, feeRate)
func (h *Harness) CreateTransaction(ctx context.Context, targetOutputs []*wire.TxOut, feeRate dcrutil.Amount) (*wire.MsgTx, error) {
return h.wallet.CreateTransaction(ctx, targetOutputs, feeRate)
}

// UnlockOutputs unlocks any outputs which were previously marked as
Expand Down
Loading