-
Notifications
You must be signed in to change notification settings - Fork 636
wallet+wtxmgr: check credit in FetchOutpointInfo
#1011
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcutil/hdkeychain" | ||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/btcsuite/btcd/wire" | ||
"github.com/btcsuite/btcwallet/waddrmgr" | ||
|
@@ -129,6 +130,84 @@ func TestFetchOutpointInfo(t *testing.T) { | |
require.Equal(t, int64(0-testBlockHeight), confirmations) | ||
} | ||
|
||
// TestFetchOutpointInfoErr checks when the wallet cannot find an output, a | ||
// proper error is returned. | ||
func TestFetchOutpointInfoErr(t *testing.T) { | ||
t.Parallel() | ||
|
||
w, cleanup := testWallet(t) | ||
defer cleanup() | ||
|
||
// Create an address we can use to send some coins to. | ||
addr, err := w.CurrentAddress(0, waddrmgr.KeyScopeBIP0084) | ||
require.NoError(t, err) | ||
p2shAddr, err := txscript.PayToAddrScript(addr) | ||
require.NoError(t, err) | ||
|
||
// Create a tx that has two outputs - output1 belongs to the wallet, | ||
// output2 is external. | ||
output1 := wire.NewTxOut(100000, p2shAddr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm why do they they pay to the same addr thought therefore we created addr above ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't matter but still better to have a proper logic in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they are dummy values - techinically they should be the same so we know the result doesn't depend on the pkscript, but again I don't think it matters. |
||
output2 := wire.NewTxOut(100000, p2shAddr) | ||
tx := &wire.MsgTx{ | ||
TxIn: []*wire.TxIn{{}}, | ||
TxOut: []*wire.TxOut{ | ||
output1, | ||
output2, | ||
}, | ||
} | ||
|
||
// Add the tx and its first output as the credit. | ||
addTxAndCredit(t, w, tx, 0) | ||
|
||
testCases := []struct { | ||
name string | ||
prevOut *wire.OutPoint | ||
|
||
// TODO(yy): refator `FetchOutpointInfo` to return wrapped | ||
// errors. | ||
errExpected string | ||
}{ | ||
{ | ||
name: "no tx details", | ||
prevOut: &wire.OutPoint{ | ||
Hash: chainhash.Hash{1, 2, 3}, | ||
Index: 0, | ||
}, | ||
errExpected: "does not belong to the wallet", | ||
}, | ||
{ | ||
name: "invalid output index", | ||
prevOut: &wire.OutPoint{ | ||
Hash: tx.TxHash(), | ||
Index: 1000, | ||
}, | ||
errExpected: "invalid output index", | ||
}, | ||
{ | ||
name: "no credit found", | ||
prevOut: &wire.OutPoint{ | ||
Hash: tx.TxHash(), | ||
Index: 1, | ||
}, | ||
errExpected: "does not belong to the wallet", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Look up the UTXO for the outpoint now and compare it | ||
// to the expected error. | ||
tx, out, conf, err := w.FetchOutpointInfo(tc.prevOut) | ||
require.ErrorContains(t, err, tc.errExpected) | ||
require.Nil(t, tx) | ||
require.Nil(t, out) | ||
require.Zero(t, conf) | ||
}) | ||
} | ||
} | ||
|
||
// TestFetchDerivationInfo checks that the wallet can gather the derivation | ||
// info about an output based on the pkScript. | ||
func TestFetchDerivationInfo(t *testing.T) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.