Skip to content

Commit

Permalink
Insert txouts for foreign inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
buffrr committed Dec 18, 2024
1 parent 1512fad commit 14158e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
34 changes: 25 additions & 9 deletions node/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ async fn it_should_allow_outbidding(rig: &TestRig) -> anyhow::Result<()> {
Ok(())
}


async fn it_should_insert_txout_for_bids(rig: &TestRig) -> anyhow::Result<()> {
rig.wait_until_synced().await?;
rig.wait_until_wallet_synced(BOB).await?;

let tx = rig.spaced.client
.wallet_list_transactions(BOB, 10, 0).await?.iter()
.filter(|tx| tx.events.iter().any(|event| event.kind == TxEventKind::Bid))
.next()
.expect("a bid").clone();

assert!(tx.fee.is_some(), "must be able to calculate fees");
Ok(())
}

/// Eve makes an invalid bid with a burn increment of 0 only refunding Bob's money
async fn it_should_only_accept_forced_zero_value_bid_increments_and_revoke(
rig: &TestRig,
Expand Down Expand Up @@ -658,7 +673,7 @@ async fn it_should_replace_mempool_bids(rig: &TestRig) -> anyhow::Result<()> {

assert!(
txs.iter().all(|tx| tx.txid != eve_replacement_txid),
"Eve's tx shouldn't be listed in Alice's wallet"
"Eve's tx shouldn't be listed in Alice's wallet"
);
Ok(())
}
Expand Down Expand Up @@ -727,14 +742,15 @@ async fn run_auction_tests() -> anyhow::Result<()> {
load_wallet(&rig, wallets_path.clone(), BOB).await?;
load_wallet(&rig, wallets_path, EVE).await?;

it_should_open_a_space_for_auction(&rig).await?;
it_should_allow_outbidding(&rig).await?;
it_should_only_accept_forced_zero_value_bid_increments_and_revoke(&rig).await?;
it_should_allow_claim_on_or_after_claim_height(&rig).await?;
it_should_allow_batch_transfers_refreshing_expire_height(&rig).await?;
it_should_allow_applying_script_in_batch(&rig).await?;
it_should_replace_mempool_bids(&rig).await?;
it_should_maintain_locktime_when_fee_bumping(&rig).await?;
it_should_open_a_space_for_auction(&rig).await.expect("should open auction");
it_should_allow_outbidding(&rig).await.expect("should allow outbidding");
it_should_insert_txout_for_bids(&rig).await.expect("should insert txout");
it_should_only_accept_forced_zero_value_bid_increments_and_revoke(&rig).await.expect("should only revoke a bid");
it_should_allow_claim_on_or_after_claim_height(&rig).await.expect("should allow claim on or above height");
it_should_allow_batch_transfers_refreshing_expire_height(&rig).await.expect("should allow batch transfers refresh expire height");
it_should_allow_applying_script_in_batch(&rig).await.expect("should allow batch applying script");
it_should_replace_mempool_bids(&rig).await.expect("should replace mempool bids");
it_should_maintain_locktime_when_fee_bumping(&rig).await.expect("should maintain locktime");

Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ impl SpacesWallet {
let txid = tx_record.tx.compute_txid();
self.apply_unconfirmed_tx(tx_record.tx, seen);

// Insert txouts for foreign inputs to be able to calculate fees
for (outpoint, txout) in tx_record.txouts {
self.internal.insert_txout(outpoint, txout);
}

let db_tx = self.connection.transaction()
.context("could not create wallet db transaction")?;
for event in tx_record.events {
Expand Down
26 changes: 17 additions & 9 deletions wallet/src/tx_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bdk_wallet::{
ToSql,
},
};
use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, Txid};
use bitcoin::{Amount, OutPoint, ScriptBuf, Transaction, TxOut, Txid};
use serde::{Deserialize, Serialize};
use protocol::{Covenant, FullSpaceOut};
use crate::rusqlite_impl::{migrate_schema, Impl};
Expand All @@ -17,6 +17,7 @@ use crate::SpacesWallet;
pub struct TxRecord {
pub tx: Transaction,
pub events: Vec<TxEvent>,
pub txouts: Vec<(OutPoint, TxOut)>
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -245,16 +246,14 @@ impl TxEvent {

impl TxRecord {
pub fn new(tx: Transaction) -> Self {
Self {
events: Vec::new(),
tx,
}
Self::new_with_events(tx, vec![])
}

pub fn new_with_events(tx: Transaction, events: Vec<TxEvent>) -> Self {
Self {
events,
tx,
txouts: vec![],
}
}

Expand Down Expand Up @@ -362,13 +361,22 @@ impl TxRecord {
Covenant::Bid { total_burned, .. } => total_burned,
_ => panic!("expected a bid"),
};
let foreign_input = match wallet.is_mine(previous.spaceout.script_pubkey.clone()) {
false => Some(previous.outpoint()),
true => None
};

if foreign_input.is_some() {
self.txouts.push((previous.outpoint(), TxOut {
value: previous.spaceout.value,
script_pubkey: previous.spaceout.script_pubkey.clone(),
}))
}

self.events.push(TxEvent {
kind: TxEventKind::Bid,
space: Some(space.name.to_string()),
foreign_input: match wallet.is_mine(previous.spaceout.script_pubkey.clone()) {
false => Some(previous.outpoint()),
true => None
},
foreign_input,
details: Some(
serde_json::to_value(BidEventDetails {
bid_current: amount,
Expand Down

0 comments on commit 14158e2

Please sign in to comment.