Skip to content

Feat/sync specific vtxo #66

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 0 additions & 1 deletion ark-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub use error::Error;
/// # &self,
/// # server_pk: XOnlyPublicKey,
/// # exit_delay: bitcoin::Sequence,
/// # descriptor_template: &str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be running doc tests in CI?

/// # network: Network,
/// # ) -> Result<BoardingOutput, Error> {
/// # unimplemented!()
Expand Down
57 changes: 47 additions & 10 deletions ark-client/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,34 @@ where
{
/// Lift all pending VTXOs and boarding outputs into the Ark, converting them into new,
/// confirmed VTXOs. We do this by "joining the next round".
pub async fn board<R>(&self, rng: &mut R) -> Result<(), Error>
pub async fn settle_all<R>(&self, rng: &mut R) -> Result<(), Error>
where
R: Rng + CryptoRng + Clone,
{
// Get off-chain address and send all funds to this address, no change output 🦄
let (to_address, _) = self.get_offchain_address()?;

let (boarding_inputs, vtxo_inputs, total_amount) =
self.fetch_round_transaction_inputs().await?;

self.settle(rng, boarding_inputs, vtxo_inputs, total_amount)
.await?;

Ok(())
}

/// Lift all pending VTXOs and boarding outputs into the Ark, converting them into new,
/// confirmed VTXOs. We do this by "joining the next round".
Comment on lines +59 to +60
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should document this function differently.

pub async fn settle<R>(
&self,
rng: &mut R,
boarding_inputs: Vec<round::OnChainInput>,
vtxo_inputs: Vec<round::VtxoInput>,
total_amount: Amount,
) -> Result<(), Error>
where
R: Rng + CryptoRng + Clone,
{
// Get off-chain address and send all funds to this address, no change output 🦄
let (to_address, _) = self.get_offchain_address()?;

tracing::debug!(
offchain_adress = %to_address.encode(),
?boarding_inputs,
Expand Down Expand Up @@ -153,11 +171,8 @@ where
Ok(txid)
}

/// Get all the [`round::OnChainInput`]s and [`round::VtxoInput`]s that can be used to join an
/// upcoming round.
async fn fetch_round_transaction_inputs(
&self,
) -> Result<(Vec<round::OnChainInput>, Vec<round::VtxoInput>, Amount), Error> {
/// Get all the [`round::OnChainInput`]s that can be used to join an upcoming round.
pub async fn fetch_boarding_inputs(&self) -> Result<(Vec<round::OnChainInput>, Amount), Error> {
// Get all known boarding outputs.
let boarding_outputs = self.inner.wallet.get_boarding_outputs()?;

Expand Down Expand Up @@ -194,6 +209,13 @@ where
}
}

Ok((boarding_inputs, total_amount))
}

/// Get all [`round::VtxoInput`]s that can be used to join an upcoming round.
pub async fn fetch_pending_vtxos(&self) -> Result<(Vec<round::VtxoInput>, Amount), Error> {
let mut total_amount = Amount::ZERO;

let spendable_vtxos = self.spendable_vtxos().await?;

for (vtxo_outpoints, _) in spendable_vtxos.iter() {
Expand All @@ -218,7 +240,22 @@ where
})
.collect::<Vec<_>>();

Ok((boarding_inputs, vtxo_inputs, total_amount))
Ok((vtxo_inputs, total_amount))
}

/// Get all the [`round::OnChainInput`]s and [`round::VtxoInput`]s that can be used to join an
/// upcoming round.
async fn fetch_round_transaction_inputs(
&self,
) -> Result<(Vec<round::OnChainInput>, Vec<round::VtxoInput>, Amount), Error> {
let (boarding_inputs, boarding_input_amount) = self.fetch_boarding_inputs().await?;
let (vtxo_inputs, vtxo_input_amount) = self.fetch_pending_vtxos().await?;

Ok((
boarding_inputs,
vtxo_inputs,
boarding_input_amount + vtxo_input_amount,
))
}

async fn join_next_ark_round<R>(
Expand Down
12 changes: 6 additions & 6 deletions e2e-tests/tests/e2e_concurrent_boarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ pub async fn concurrent_boarding() {
let alice_task = tokio::spawn({
async move {
let mut rng = StdRng::from_entropy();
alice.board(&mut rng).await.unwrap();
alice.settle_all(&mut rng).await.unwrap();
alice
}
});

let bob_task = tokio::spawn(async move {
let mut rng = StdRng::from_entropy();
bob.board(&mut rng).await.unwrap();
bob.settle_all(&mut rng).await.unwrap();
bob
});

let claire_task = tokio::spawn(async move {
let mut rng = StdRng::from_entropy();
claire.board(&mut rng).await.unwrap();
claire.settle_all(&mut rng).await.unwrap();
claire
});

Expand Down Expand Up @@ -144,20 +144,20 @@ pub async fn concurrent_boarding() {
let alice_task = tokio::spawn({
async move {
let mut rng = StdRng::from_entropy();
alice.board(&mut rng).await.unwrap();
alice.settle_all(&mut rng).await.unwrap();
alice
}
});

let bob_task = tokio::spawn(async move {
let mut rng = StdRng::from_entropy();
bob.board(&mut rng).await.unwrap();
bob.settle_all(&mut rng).await.unwrap();
bob
});

let claire_task = tokio::spawn(async move {
let mut rng = StdRng::from_entropy();
claire.board(&mut rng).await.unwrap();
claire.settle_all(&mut rng).await.unwrap();
claire
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn send_onchain_vtxo_and_boarding_output() {

assert_eq!(offchain_balance.total(), Amount::ZERO);

alice.board(&mut rng).await.unwrap();
alice.settle_all(&mut rng).await.unwrap();
wait_until_balance(&alice, fund_amount, Amount::ZERO).await;

alice.commit_vtxos_on_chain().await.unwrap();
Expand Down
21 changes: 18 additions & 3 deletions e2e-tests/tests/e2e_two_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ pub async fn e2e() {
assert_eq!(alice_offchain_balance.total(), Amount::ZERO);
assert_eq!(bob_offchain_balance.total(), Amount::ZERO);

alice.board(&mut rng).await.unwrap();
{
let (boarding_inputs, amount) = alice.fetch_boarding_inputs().await.unwrap();
tracing::debug!(?boarding_inputs, ?amount, "Settling boarding inputs");
alice
.settle(&mut rng, boarding_inputs, vec![], amount)
.await
.unwrap();
}

tokio::time::sleep(std::time::Duration::from_secs(2)).await;

let alice_offchain_balance = alice.offchain_balance().await.unwrap();
Expand Down Expand Up @@ -107,7 +115,14 @@ pub async fn e2e() {
assert_eq!(bob_offchain_balance.confirmed(), Amount::ZERO);
assert_eq!(bob_offchain_balance.pending(), send_to_bob_vtxo_amount);

bob.board(&mut rng).await.unwrap();
{
let (pending_vtxos, amount) = bob.fetch_pending_vtxos().await.unwrap();
tracing::debug!(?pending_vtxos, ?amount, "Settling bob's vtxo");
bob.settle(&mut rng, vec![], pending_vtxos, amount)
.await
.unwrap();
}

tokio::time::sleep(std::time::Duration::from_secs(2)).await;

let alice_offchain_balance = alice.offchain_balance().await.unwrap();
Expand All @@ -127,7 +142,7 @@ pub async fn e2e() {
assert_eq!(bob_offchain_balance.confirmed(), send_to_bob_vtxo_amount);
assert_eq!(bob_offchain_balance.pending(), Amount::ZERO);

alice.board(&mut rng).await.unwrap();
alice.settle_all(&mut rng).await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;

let alice_offchain_balance = alice.offchain_balance().await.unwrap();
Expand Down
Loading