Skip to content

Commit

Permalink
Expose num_actions as a method on PaddingRule
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 12, 2023
1 parent a6766a3 commit e049bcc
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ impl PaddingRule {
/// The default padding rule, which ensures that the constructed bundle will contain at least 2
/// actions if any genuine Orchard spends or outputs are requested.
pub const DEFAULT: PaddingRule = PaddingRule::PadTo(MIN_ACTIONS);

/// Returns the number of logical actions that the builder will add to the bundle
/// as a consequence of this padding rule, given the specified number of spends
/// and recipients.
pub fn num_actions(&self, num_spends: usize, num_recipients: usize) -> usize {
let num_real_actions = core::cmp::max(num_spends, num_recipients);

match self {
PaddingRule::Require(n) => core::cmp::max(num_real_actions, *n),
PaddingRule::PadTo(n) => {
if num_real_actions == 0 {
0
} else {
core::cmp::max(num_real_actions, *n)
}
}
PaddingRule::None => num_real_actions,
}
}
}

/// An error type for the kinds of errors that can occur during bundle construction.
Expand Down Expand Up @@ -414,19 +433,9 @@ impl Builder {
) -> Result<Bundle<InProgress<Unproven, Unauthorized>, V>, BuildError> {
let num_real_spends = self.spends.len();
let num_real_recipients = self.recipients.len();
let num_real_actions = core::cmp::max(num_real_spends, num_real_recipients);

let num_actions = match self.padding_rule {
PaddingRule::Require(n) => core::cmp::max(num_real_actions, n),
PaddingRule::PadTo(n) => {
if num_real_actions == 0 {
0
} else {
core::cmp::max(num_real_actions, n)
}
}
PaddingRule::None => num_real_actions,
};
let num_actions = self
.padding_rule
.num_actions(num_real_spends, num_real_recipients);

// Pair up the spends and recipients, extending with dummy values as necessary.
let pre_actions: Vec<_> = {
Expand Down

0 comments on commit e049bcc

Please sign in to comment.