Skip to content

Commit

Permalink
feat(wallet): allow UTXO selection by specific outputs and by token (#…
Browse files Browse the repository at this point in the history
…4227)

Description
---
- adds UtxoSelectionCriteria param to select_utxos
- removes unique_id and parent_public_key utxo fetching
- adds `UtxoSelectionCriteria::TokenOutputs` to allow db-level filtering for unique_id 
- adds `UtxoSelectionCriteria::SpecificOutputs` to allow spendin specific utxos
- always sort (secondary to first sort) from most to least mature if tip_height is not known 
- remove some commented out and deprecated logic
- remove MaturityThenSmallest ordering  

Motivation and Context
---
The previous logic of UTXO selection has been kept equivalent (no utxo selection tests needed to be changed), but extended to allow Tokens and specific UTXOs to be selected at the db-level

- Aurora wallet will need to spend specific utxos.
- MaturityThenSmallest is redundant because it is only applicable if the tip height is not known, which is now handled independently of the ordering.  i.e. if you dont know the tip height (pretty rare) you always want to select the most mature utxos first to reduce chances of it not being spendable regardless of selected value ordering
- `UtxoSelectionCriteria::TokenOutputs` does db-level querying which is more performant, and will be chaned on development branch to ContractOutputs (so was worth doing)

How Has This Been Tested?
---
Existing tests for coin split and utxo selection
Manually, running soin split and make it rain
  • Loading branch information
sdbondi authored Jun 23, 2022
1 parent 28a8f8b commit f2a7e18
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 172 deletions.
126 changes: 126 additions & 0 deletions base_layer/wallet/src/output_manager_service/input_selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
fmt,
fmt::{Display, Formatter},
};

use tari_common_types::types::PublicKey;

use crate::output_manager_service::storage::models::DbUnblindedOutput;

#[derive(Debug, Clone, Default)]
pub struct UtxoSelectionCriteria {
pub filter: UtxoSelectionFilter,
pub ordering: UtxoSelectionOrdering,
}

impl UtxoSelectionCriteria {
pub fn largest_first() -> Self {
Self {
filter: UtxoSelectionFilter::Standard,
ordering: UtxoSelectionOrdering::LargestFirst,
}
}

pub fn for_token(unique_id: Vec<u8>, parent_public_key: Option<PublicKey>) -> Self {
Self {
filter: UtxoSelectionFilter::TokenOutput {
unique_id,
parent_public_key,
},
ordering: UtxoSelectionOrdering::Default,
}
}
}

impl Display for UtxoSelectionCriteria {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "filter: {}, ordering: {}", self.filter, self.ordering)
}
}

/// UTXO selection ordering
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UtxoSelectionOrdering {
/// The Default ordering is heuristic and depends on the requested value and the value of the available UTXOs.
/// If the requested value is larger than the largest available UTXO, we select LargerFirst as inputs, otherwise
/// SmallestFirst.
Default,
/// Start from the smallest UTXOs and work your way up until the amount is covered. Main benefit
/// is removing small UTXOs from the blockchain, con is that it costs more in fees
SmallestFirst,
/// A strategy that selects the largest UTXOs first. Preferred when the amount is large
LargestFirst,
}

impl Default for UtxoSelectionOrdering {
fn default() -> Self {
UtxoSelectionOrdering::Default
}
}

impl Display for UtxoSelectionOrdering {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UtxoSelectionOrdering::SmallestFirst => write!(f, "Smallest"),
UtxoSelectionOrdering::LargestFirst => write!(f, "Largest"),
UtxoSelectionOrdering::Default => write!(f, "Default"),
}
}
}

#[derive(Debug, Clone)]
pub enum UtxoSelectionFilter {
/// Select OutputType::Standard or OutputType::Coinbase outputs only
Standard,
/// Select matching token outputs. This will be deprecated in future.
TokenOutput {
unique_id: Vec<u8>,
parent_public_key: Option<PublicKey>,
},
/// Selects specific outputs. All outputs must be exist and be spendable.
SpecificOutputs { outputs: Vec<DbUnblindedOutput> },
}

impl Default for UtxoSelectionFilter {
fn default() -> Self {
UtxoSelectionFilter::Standard
}
}

impl Display for UtxoSelectionFilter {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
UtxoSelectionFilter::Standard => {
write!(f, "Standard")
},
UtxoSelectionFilter::TokenOutput { .. } => {
write!(f, "TokenOutput{{..}}")
},
UtxoSelectionFilter::SpecificOutputs { outputs } => {
write!(f, "Specific({} output(s))", outputs.len())
},
}
}
}
25 changes: 14 additions & 11 deletions base_layer/wallet/src/output_manager_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::sync::Arc;
pub mod config;
pub mod error;
pub mod handle;

mod input_selection;
pub use input_selection::{UtxoSelectionCriteria, UtxoSelectionFilter, UtxoSelectionOrdering};

mod recovery;
pub mod resources;
pub mod service;
pub mod storage;
mod tasks;

use std::{marker::PhantomData, sync::Arc};

use futures::future;
use log::*;
Expand All @@ -47,16 +60,6 @@ use crate::{
},
};

pub mod config;
pub mod error;
pub mod handle;
mod recovery;
pub mod resources;
pub mod service;
pub mod storage;
mod tasks;
use std::marker::PhantomData;

const LOG_TARGET: &str = "wallet::output_manager_service::initializer";

pub struct OutputManagerServiceInitializer<T, TKeyManagerInterface>
Expand Down
Loading

0 comments on commit f2a7e18

Please sign in to comment.