Skip to content

Commit 3f244bc

Browse files
committed
Consolidate utxo types
Orderbook utxos are utxos that capture some metadata including: amount and owner.
1 parent 5bf78e5 commit 3f244bc

4 files changed

Lines changed: 25 additions & 29 deletions

File tree

src/actions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use log::debug;
44

55
use crate::{
66
bulletin_board::BulletinBoardId,
7-
cospend::UtxoWithAmount,
7+
cospend::UtxoWithMetadata,
88
message::MessageId,
99
transaction::Outpoint,
1010
tx_contruction::TxConstructionState,
@@ -119,7 +119,7 @@ pub(crate) struct WalletView {
119119
payment_obligations: Vec<PaymentObligationData>,
120120
active_cospends: Vec<BulletinBoardId>,
121121
cospend_proposals: Vec<(BulletinBoardId, MessageId)>,
122-
utxos: Vec<UtxoWithAmount>,
122+
utxos: Vec<UtxoWithMetadata>,
123123
registered_inputs: Vec<Outpoint>,
124124
}
125125

@@ -128,7 +128,7 @@ impl WalletView {
128128
payment_obligations: Vec<PaymentObligationData>,
129129
cospend_proposals: Vec<(BulletinBoardId, MessageId)>,
130130
active_cospends: Vec<BulletinBoardId>,
131-
utxos: Vec<UtxoWithAmount>,
131+
utxos: Vec<UtxoWithMetadata>,
132132
registered_inputs: Vec<Outpoint>,
133133
) -> Self {
134134
Self {

src/cospend.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ use crate::wallet::WalletId;
33
use bitcoin::Amount;
44

55
#[derive(Debug, Clone)]
6-
pub(crate) struct UtxoWithAmount {
6+
pub(crate) struct UtxoWithMetadata {
77
pub(crate) outpoint: Outpoint,
88
pub(crate) amount: Amount,
9-
}
10-
11-
/// A UTXO from the order book
12-
#[derive(Debug, Clone)]
13-
pub(crate) struct OrderBookEntry {
14-
pub(crate) utxo: UtxoWithAmount,
159
pub(crate) owner: WalletId,
1610
}
1711

@@ -23,15 +17,15 @@ fn amount_distance(a: Amount, b: Amount) -> u64 {
2317
/// Each entry is scored by the minimum amount distance to any taker UTXO,
2418
/// so the best-matched makers appear first.
2519
pub(crate) fn generate_candidates(
26-
order_book: &[OrderBookEntry],
27-
taker_utxos: &[UtxoWithAmount],
28-
) -> Vec<OrderBookEntry> {
29-
let mut scored: Vec<(u64, &OrderBookEntry)> = order_book
20+
order_book: &[UtxoWithMetadata],
21+
taker_utxos: &[UtxoWithMetadata],
22+
) -> Vec<UtxoWithMetadata> {
23+
let mut scored: Vec<(u64, &UtxoWithMetadata)> = order_book
3024
.iter()
3125
.map(|entry| {
3226
let min_dist = taker_utxos
3327
.iter()
34-
.map(|t| amount_distance(entry.utxo.amount, t.amount))
28+
.map(|t| amount_distance(entry.amount, t.amount))
3529
.min()
3630
.unwrap_or(u64::MAX);
3731
(min_dist, entry)
@@ -41,8 +35,8 @@ pub(crate) fn generate_candidates(
4135
scored.sort_unstable_by(|(dist_a, a), (dist_b, b)| {
4236
dist_a
4337
.cmp(dist_b)
44-
.then_with(|| a.utxo.outpoint.txid.0.cmp(&b.utxo.outpoint.txid.0))
45-
.then_with(|| a.utxo.outpoint.index.cmp(&b.utxo.outpoint.index))
38+
.then_with(|| a.outpoint.txid.0.cmp(&b.outpoint.txid.0))
39+
.then_with(|| a.outpoint.index.cmp(&b.outpoint.index))
4640
});
4741

4842
scored.into_iter().map(|(_, entry)| entry.clone()).collect()

src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use serde::Serialize;
1515
use crate::bulletin_board::BroadcastMessageType;
1616
use crate::bulletin_board::BulletinBoardData;
1717
use crate::bulletin_board::BulletinBoardId;
18-
use crate::cospend::OrderBookEntry;
19-
use crate::cospend::UtxoWithAmount;
18+
use crate::cospend::UtxoWithMetadata;
2019
use crate::message::MessageType;
2120
use crate::tx_contruction::MultiPartyPayjoinSession;
2221
use crate::{
@@ -598,19 +597,17 @@ impl<'a> Simulation {
598597
bx_id.with_mut(self).broadcast(txs)
599598
}
600599

601-
fn get_orderbook_utxos(&'a self) -> Vec<OrderBookEntry> {
600+
fn get_orderbook_utxos(&'a self) -> Vec<UtxoWithMetadata> {
602601
self.wallet_data
603602
.iter()
604603
.flat_map(|wallet| {
605604
let info = &self.wallet_info[wallet.last_wallet_info_id.0];
606605
info.registered_inputs
607606
.iter()
608607
.filter(|outpoint| info.confirmed_utxos.contains(outpoint))
609-
.map(|outpoint| OrderBookEntry {
610-
utxo: UtxoWithAmount {
611-
outpoint: *outpoint,
612-
amount: outpoint.with(self).data().amount,
613-
},
608+
.map(|outpoint| UtxoWithMetadata {
609+
outpoint: *outpoint,
610+
amount: outpoint.with(self).data().amount,
614611
owner: wallet.id,
615612
})
616613
.collect::<Vec<_>>()

src/wallet.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
actions::{Action, CompositeScorer, CompositeStrategy, WalletView},
33
blocks::BroadcastSetId,
44
bulletin_board::BulletinBoardId,
5-
cospend::{generate_candidates, UtxoWithAmount},
5+
cospend::{generate_candidates, UtxoWithMetadata},
66
message::{MessageId, MessageType},
77
script_type::ScriptType,
88
tx_contruction::{
@@ -444,9 +444,10 @@ impl<'a> WalletHandleMut<'a> {
444444
let utxos = self
445445
.handle()
446446
.unspent_coins()
447-
.map(|o| UtxoWithAmount {
447+
.map(|o| UtxoWithMetadata {
448448
outpoint: o.outpoint(),
449449
amount: o.data().amount,
450+
owner: self.id,
450451
})
451452
.collect::<Vec<_>>();
452453
let registered_inputs = self
@@ -466,22 +467,25 @@ impl<'a> WalletHandleMut<'a> {
466467
}
467468

468469
// TODO: this should take input the list of order book entries
469-
// Candidate selection should be done by the cost function
470+
// TODO: at this point we are not concerned with what po's are handled just what inputs are being spent and which order book utxos we prefer.
471+
// That should be the input to this method
470472
pub(crate) fn create_cospend_proposal(&'a mut self, po_ids: &[PaymentObligationId]) {
471473
// TODO: change should be decomposed.
472474
let change_addr = self.new_address();
473475
// TODO: should you try to construct with other utxos than the ones bnb picks out?
474476
let tx_template = self.construct_transaction_template(po_ids, &change_addr, false, None);
475477
let orderbook_utxos = self.sim.get_orderbook_utxos();
478+
// TODO: currently we pick all order book utxos,
476479
let candidates = generate_candidates(
477480
&orderbook_utxos,
478481
&tx_template
479482
.inputs
480483
.iter()
481484
.map(|input| input.outpoint.with(self.sim))
482-
.map(|input| UtxoWithAmount {
485+
.map(|input| UtxoWithMetadata {
483486
outpoint: input.outpoint,
484487
amount: input.data().amount,
488+
owner: self.id,
485489
})
486490
.collect::<Vec<_>>(),
487491
);
@@ -493,6 +497,7 @@ impl<'a> WalletHandleMut<'a> {
493497
}
494498

495499
// Collect unique maker wallet IDs from the best candidate
500+
// TODO: Why must this be unique?
496501
let maker_ids: Vec<WalletId> = candidates
497502
.iter()
498503
.map(|e| e.owner)

0 commit comments

Comments
 (0)