Skip to content

Commit

Permalink
Increased the generality of Builder struct implementations. Made Sapl…
Browse files Browse the repository at this point in the history
…ing Builder views stricter.
  • Loading branch information
Murisi Tarusenga committed Apr 18, 2023
1 parent dc76395 commit 0f9ec18
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion masp_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub struct Builder<P, R, Key = ExtendedSpendingKey, Notifier = Sender<Progress>>
progress_notifier: Option<Notifier>,
}

impl<P, R> Builder<P, R> {
impl<P, R, K, N> Builder<P, R, K, N> {
/// Returns the network parameters that the builder has been configured for.
pub fn params(&self) -> &P {
&self.params
Expand Down
23 changes: 14 additions & 9 deletions masp_primitives/src/transaction/components/sapling/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,18 @@ impl<Key: BorshDeserialize> BorshDeserialize for SpendDescriptionInfo<Key> {
}
}

impl fees::InputView<()> for SpendDescriptionInfo {
impl<K> fees::InputView<()> for SpendDescriptionInfo<K> {
fn note_id(&self) -> &() {
// The builder does not make use of note identifiers, so we can just return the unit value.
&()
}

fn value(&self) -> Amount {
// An existing note to be spent must have a valid amount value.
Amount::from_pair(self.note.asset_type, self.note.value)
.expect("Existing note value with invalid asset type or value.")
fn value(&self) -> u64 {
self.note.value
}

fn asset_type(&self) -> AssetType {
self.note.asset_type
}
}

Expand Down Expand Up @@ -198,9 +200,12 @@ impl SaplingOutputInfo {
}

impl fees::OutputView for SaplingOutputInfo {
fn value(&self) -> Amount {
Amount::from_pair(self.note.asset_type, self.note.value)
.expect("Note values should be checked at construction.")
fn value(&self) -> u64 {
self.note.value
}

fn asset_type(&self) -> AssetType {
self.note.asset_type
}
}

Expand Down Expand Up @@ -328,7 +333,7 @@ impl Authorization for Unauthorized {
type AuthSig = SpendDescriptionInfo;
}

impl<P> SaplingBuilder<P> {
impl<P, K> SaplingBuilder<P, K> {
pub fn new(params: P, target_height: BlockHeight) -> Self {
SaplingBuilder {
params,
Expand Down
10 changes: 7 additions & 3 deletions masp_primitives/src/transaction/components/sapling/fees.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
//! Types related to computation of fees and change related to the Sapling components
//! of a transaction.
use crate::transaction::components::amount::Amount;
use crate::asset_type::AssetType;

/// A trait that provides a minimized view of a Sapling input suitable for use in
/// fee and change calculation.
pub trait InputView<NoteRef> {
/// An identifier for the input being spent.
fn note_id(&self) -> &NoteRef;
/// The value of the input being spent.
fn value(&self) -> Amount;
fn value(&self) -> u64;
/// The asset type of the input being spent.
fn asset_type(&self) -> AssetType;
}

/// A trait that provides a minimized view of a Sapling output suitable for use in
/// fee and change calculation.
pub trait OutputView {
/// The value of the output being produced.
fn value(&self) -> Amount;
fn value(&self) -> u64;
/// The asset type of the output being produced.
fn asset_type(&self) -> AssetType;
}
15 changes: 11 additions & 4 deletions masp_primitives/src/transaction/components/transparent/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//! of a transaction.
use super::TxOut;
use crate::transaction::{components::amount::Amount, TransparentAddress};
use crate::transaction::TransparentAddress;
use crate::asset_type::AssetType;

/// This trait provides a minimized view of a transparent input suitable for use in
/// fee and change computation.
Expand All @@ -15,14 +16,20 @@ pub trait InputView {
/// fee and change computation.
pub trait OutputView {
/// Returns the value of the output being created.
fn value(&self) -> Amount;
fn value(&self) -> i64;
/// Returns the asset type of the output being created.
fn asset_type(&self) -> AssetType;
/// Returns the script corresponding to the newly created output.
fn transparent_address(&self) -> &TransparentAddress;
}

impl OutputView for TxOut {
fn value(&self) -> Amount {
Amount::from_pair(self.asset_type, self.value).unwrap()
fn value(&self) -> i64 {
self.value
}

fn asset_type(&self) -> AssetType {
self.asset_type
}

fn transparent_address(&self) -> &TransparentAddress {
Expand Down

0 comments on commit 0f9ec18

Please sign in to comment.