-
Notifications
You must be signed in to change notification settings - Fork 77
fix(wallet): categorize self-transfers to external addresses as trusted_pending #431
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -1115,12 +1115,54 @@ impl Wallet { | |
| /// Return the balance, separated into available, trusted-pending, untrusted-pending, and | ||
| /// immature values. | ||
| pub fn balance(&self) -> Balance { | ||
| self.tx_graph.graph().balance( | ||
| let graph = self.tx_graph.graph(); | ||
| let index = &self.tx_graph.index; | ||
|
|
||
| let owned_outpoints: HashSet<OutPoint> = | ||
| index.outpoints().iter().map(|(_, op)| *op).collect(); | ||
|
|
||
| let mut untrusted_outpoints: HashSet<OutPoint> = HashSet::new(); | ||
|
|
||
| let mut changed = true; | ||
| while changed { | ||
| changed = false; | ||
| for tx_node in graph.full_txs() { | ||
| if tx_node.tx.is_coinbase() { | ||
| continue; | ||
| } | ||
| let is_untrusted = tx_node.tx.input.iter().any(|txin| { | ||
| !owned_outpoints.contains(&txin.previous_output) | ||
| || untrusted_outpoints.contains(&txin.previous_output) | ||
|
Contributor
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. I don't know if I'm missing an assumption over graph here, but if This case: #[test]
fn test_pay_to_internal_from_not_trusted_but_confirmed() {
let (mut wallet, _) = get_funded_wallet_wpkh();
// Build a tx whose input comes from an unknown (external) outpoint,
// but whose output goes to our change (internal) keychain address.
let external_txid = Txid::from_raw_hash(Hash::all_zeros());
let tx = Transaction {
input: vec![TxIn {
previous_output: OutPoint {
txid: external_txid,
vout: 0,
},
..Default::default()
}],
output: vec![TxOut {
value: Amount::from_sat(500),
script_pubkey: wallet
.next_unused_address(KeychainKind::Internal)
.address
.script_pubkey(),
}],
version: transaction::Version::ONE,
lock_time: absolute::LockTime::ZERO,
};
insert_tx(&mut wallet, tx);
let anchor = ConfirmationBlockTime {
block_id: wallet.latest_checkpoint().get(2000).unwrap().block_id(),
confirmation_time: 0,
};
// The transaction is confirmed
insert_anchor(&mut wallet, external_txid, anchor);
let balance = wallet.balance();
// The output is ours but the input is not owned, so it must be untrusted_pending.
assert!(balance.trusted_pending > Amount::ZERO);
assert_eq!(balance.untrusted_pending, Amount::ZERO);
}
Contributor
Author
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. Yup, I think there is another bug in there. Maybe another hashset of confirmed outpoints and checking if the hashet contains it on: Would solve that?
Contributor
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. Sorry, did you intend to show something in the code? I'm not getting it.
Contributor
Author
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. Yes, I think I lost context just with this snippet. The point was to add another condition there to check if it's confirmed or not, maybe by storing all the confirmed transactions in a separate hash set. Agreeing with benchmarking it first.
Contributor
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. After discussing with @ValuedMammal, we conclude the definition of trust is: |
||
| }); | ||
| if is_untrusted { | ||
| for (vout, _) in tx_node.tx.output.iter().enumerate() { | ||
| let op = OutPoint { | ||
| txid: tx_node.txid, | ||
| vout: vout as u32, | ||
| }; | ||
| if owned_outpoints.contains(&op) && untrusted_outpoints.insert(op) { | ||
| changed = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let trusted_outpoints: HashSet<&OutPoint> = | ||
| owned_outpoints.difference(&untrusted_outpoints).collect(); | ||
|
|
||
| let keyed_outpoints = index | ||
| .outpoints() | ||
| .iter() | ||
| .cloned() | ||
| .map(|((k, i), op)| ((k, i, op), op)); | ||
|
|
||
| graph.balance( | ||
| &self.chain, | ||
| self.chain.tip().block_id(), | ||
| CanonicalizationParams::default(), | ||
| self.tx_graph.index.outpoints().iter().cloned(), | ||
| |&(k, _), _| k == KeychainKind::Internal, | ||
| keyed_outpoints, | ||
| |&(_, _, op), _| trusted_outpoints.contains(&op), | ||
| ) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change this for
loop { ... }and break if conditions are met.changeddoesn't hold any meaning by itself outside of this particular context.