-
Notifications
You must be signed in to change notification settings - Fork 36
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
7e0280e
58bae98
9d6ae5c
9ec1a6b
aeb6265
71bf887
5816070
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,120 @@ | ||
use std::{ops::Deref, str::FromStr}; | ||
|
||
use anyhow::Context; | ||
use bdk_bitcoind_rpc::Emitter; | ||
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 sync_to_tip<C>(wallet: &mut Wallet, emitter: &mut Emitter<C>) -> anyhow::Result<()> | ||
where | ||
C: Deref, | ||
C::Target: RpcApi, | ||
{ | ||
while let Some(block_event) = emitter.next_block()? { | ||
wallet.apply_block(&block_event.block, block_event.block_height())?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn sync_mempool<C>(wallet: &mut Wallet, emitter: &mut Emitter<C>) -> anyhow::Result<()> | ||
where | ||
C: Deref, | ||
C::Target: RpcApi, | ||
{ | ||
let event = emitter.mempool()?; | ||
wallet.apply_unconfirmed_txs(event.update); | ||
wallet.apply_evicted_txs(event.evicted); | ||
Ok(()) | ||
} | ||
|
||
/// Demonstate how we handle the following situations: | ||
/// | ||
/// ## An outgoing transaction depends on an incoming transaction that gets replaced. | ||
/// | ||
/// Given: | ||
/// * Wallet receives an incoming unconfirmed transaction. | ||
/// * Wallet creates outgoing transaction that spends from the incoming transaction. | ||
/// * The wallet tracks the outgoing transaction (with `wallet.track_tx`). | ||
/// * The incoming transaction gets replaced. | ||
/// | ||
/// When: | ||
/// * `wallet.uncanonical_txs` is called, expect: | ||
/// * 1 transaction is returned (the created outgoing transaction). | ||
/// * `UncanonicalTx::is_safe_to_untrack(0)` should return true. | ||
/// * `UncanonicalTx::is_safe_to_untrack(>0)` should return false. | ||
/// * TODO: Can replace == false, no inputs available. | ||
|
||
/// 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. | ||
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()), | ||
); | ||
|
||
let wallet_addr = wallet.next_unused_address(KeychainKind::External).address; | ||
let remote_addr = env | ||
.rpc_client() | ||
.get_new_address(None, None)? | ||
.require_network(Network::Regtest)?; | ||
|
||
sync_to_tip(&mut wallet, &mut emitter)?; | ||
|
||
// [INCOMING TX] : Create, broadcast & sync | ||
let incoming_txid = env.send(&wallet_addr, Amount::ONE_BTC)?; | ||
sync_mempool(&mut wallet, &mut emitter)?; | ||
assert_eq!(wallet.balance().total(), Amount::ONE_BTC); | ||
|
||
// [OUTGOING TX] : Create & track | ||
let 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())?); | ||
psbt.extract_tx()? | ||
}; | ||
wallet.track_tx(outgoing_tx.clone()); | ||
assert_eq!(wallet.uncanonical_txs().count(), 1); | ||
|
||
// 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()?; | ||
// println!("mempool_event: {mempool_event:?}"); | ||
// wallet.apply_unconfirmed_txs(mempool_event.update); | ||
// wallet.apply_evicted_txs(mempool_event.evicted); | ||
// 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 incoming_rbf_tx = { | ||
let res = env | ||
.rpc_client() | ||
.call::<serde_json::Value>("bumpfee", &[incoming_txid.to_string().into()])?; | ||
Txid::from_str(res.get("txid").unwrap().as_str().unwrap())? | ||
}; | ||
sync_mempool(&mut wallet, &mut emitter)?; | ||
|
||
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?