-
Notifications
You must be signed in to change notification settings - Fork 30
Introduce IntentTracker
#257
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
75bc892
a65eabe
68c0505
8a2d04f
8e1b43c
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::{str::FromStr, sync::Arc, time::Duration}; | ||
|
||
use anyhow::Context; | ||
use bdk_testenv::{bitcoincore_rpc::RpcApi, TestEnv}; | ||
use bdk_wallet::{KeychainKind, SignOptions, Wallet}; | ||
use bitcoin::{Amount, Network, Txid}; | ||
|
||
const DESCRIPTOR: &str = bdk_testenv::utils::DESCRIPTORS[3]; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let env = TestEnv::new().context("failed to start testenv")?; | ||
env.mine_blocks(101, None) | ||
.context("failed to mine blocks")?; | ||
|
||
let mut wallet = Wallet::create_single(DESCRIPTOR) | ||
.network(Network::Regtest) | ||
.create_wallet_no_persist() | ||
.context("failed to construct wallet")?; | ||
|
||
let mut emitter = bdk_bitcoind_rpc::Emitter::new( | ||
env.rpc_client(), | ||
wallet.latest_checkpoint(), | ||
0, | ||
wallet | ||
.transactions() | ||
.filter(|tx| tx.chain_position.is_unconfirmed()) | ||
.map(|tx| tx.tx_node.txid), | ||
); | ||
while let Some(block_event) = emitter.next_block()? { | ||
wallet.apply_block(&block_event.block, block_event.block_height())?; | ||
} | ||
|
||
// Receive an unconfirmed tx, spend from it, and the unconfirmed tx get's RBF'ed. | ||
// Our API should be able to recognise that the outgoing tx became evicted and allow the caller | ||
// to respond accordingly. | ||
let wallet_addr = wallet.next_unused_address(KeychainKind::External).address; | ||
let remote_addr = env | ||
.rpc_client() | ||
.get_new_address(None, None)? | ||
.assume_checked(); | ||
let incoming_txid = env.send(&wallet_addr, Amount::ONE_BTC)?; | ||
|
||
let mempool_event = emitter.mempool()?; | ||
wallet.apply_evicted_txs(mempool_event.evicted_ats()); | ||
wallet.apply_unconfirmed_txs(mempool_event.new_txs); | ||
assert_eq!(wallet.balance().total(), Amount::ONE_BTC); | ||
|
||
// Create & broadcast outgoing tx. | ||
let mut tx_builder = wallet.build_tx(); | ||
tx_builder.add_recipient(remote_addr, Amount::ONE_BTC / 2); | ||
let mut psbt = tx_builder.finish()?; | ||
assert!(wallet.sign(&mut psbt, SignOptions::default())?); | ||
let outgoing_tx = psbt.extract_tx()?; | ||
wallet.track_tx(outgoing_tx.clone()); | ||
assert_eq!(wallet.uncanonical_txs().count(), 1); | ||
|
||
// Sync. | ||
let outgoing_txid = env.rpc_client().send_raw_transaction(&outgoing_tx)?; | ||
env.wait_until_electrum_sees_txid(outgoing_txid, Duration::from_secs(5))?; | ||
let mempool_event = emitter.mempool()?; | ||
// TODO: Why is `outgoing_txid` not emitted? | ||
println!("mempool_event: {mempool_event:#?}"); | ||
wallet.apply_evicted_txs(mempool_event.evicted_ats()); | ||
wallet.apply_unconfirmed_txs(mempool_event.new_txs); | ||
let tx = wallet | ||
.canonical_txs() | ||
.find(|tx| tx.tx_node.txid == outgoing_txid) | ||
.expect("must find outgoing tx"); | ||
assert_eq!(wallet.uncanonical_txs().count(), 0); | ||
|
||
// RBF incoming tx. | ||
let res = env | ||
.rpc_client() | ||
.call::<serde_json::Value>("bumpfee", &[incoming_txid.to_string().into()])?; | ||
let incoming_replacement_txid = Txid::from_str(res.get("txid").unwrap().as_str().unwrap())?; | ||
|
||
let mempool_event = emitter.mempool()?; | ||
wallet.apply_evicted_txs(mempool_event.evicted_ats()); | ||
wallet.apply_unconfirmed_txs(mempool_event.new_txs); | ||
|
||
for uncanonical_tx in wallet.uncanonical_txs() {} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ pub struct LocalOutput { | |
pub derivation_index: u32, | ||
/// The position of the output in the blockchain. | ||
pub chain_position: ChainPosition<ConfirmationBlockTime>, | ||
/// Whether this output exists in a transaction that is yet to be broadcasted. | ||
pub needs_broadcast: bool, | ||
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. When do we expect this to be set/unset exactly? I guess it can only be unset once the transaction in question reaches threshold confirmations? 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. That is a good point, and it shows the limitations of the There should be a method such as |
||
} | ||
|
||
/// A [`Utxo`] with its `satisfaction_weight`. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Maybe an enum
state
field with something like:UNSPENT
,ON_QUEUE
,SPENT
,BROADCASTED
will avoid keep adding new boolean fields here, and provide a better path for update on future occasions, taking advantage of non exhaustive patters.is_spent
could be marked for deprecation and be used along the new field in the meantime.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 like this idea if done as a non-exhaustive enum to help reduce future API breaking changes. If we include a "LOCKED" variant could this also support #259?