Skip to content

Commit

Permalink
Make PaddingRule an argument to build rather than to Builder::new
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 13, 2023
1 parent 99d3d0a commit 89bc5cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ and this project adheres to Rust's notion of
- `orchard::builder::PaddingRule`

### Changed
- `orchard::builder::Builder::new` now takes an additional `PaddingRule` argument
that specifies how actions should be padded, instead of using hardcoded padding.
- `orchard::builder::Builder::add_recipient` has been renamed to `add_output`
in order to clarify than more than one output of a given transaction may be
sent to the same recipient.
- `orchard::builder::Builder::build` now takes an additional `PaddingRule` argument
that specifies how actions should be padded, instead of using hardcoded padding.
- `orchard::builder::Builder::build` now returns a `Result<Option<Bundle<...>>, ...>`
instead of a `Result<Bundle<...>, ...>` to support `PaddingRule::PadTo` and
to avoid a possible panic in the case of `PadTo(0)` or `Require(0)`.
Expand Down
16 changes: 6 additions & 10 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,16 @@ pub struct Builder {
outputs: Vec<OutputInfo>,
flags: Flags,
anchor: Anchor,
padding_rule: PaddingRule,
}

impl Builder {
/// Constructs a new empty builder for an Orchard bundle.
pub fn new(flags: Flags, anchor: Anchor, padding_rule: PaddingRule) -> Self {
pub fn new(flags: Flags, anchor: Anchor) -> Self {
Builder {
spends: vec![],
outputs: vec![],
flags,
anchor,
padding_rule,
}
}

Expand Down Expand Up @@ -428,12 +426,11 @@ impl Builder {
pub fn build<V: TryFrom<i64>>(
mut self,
mut rng: impl RngCore,
padding_rule: &PaddingRule,
) -> Result<Option<Bundle<InProgress<Unproven, Unauthorized>, V>>, BuildError> {
let num_real_spends = self.spends.len();
let num_real_outputs = self.outputs.len();
let num_actions = self
.padding_rule
.num_actions(num_real_spends, num_real_outputs);
let num_actions = padding_rule.num_actions(num_real_spends, num_real_outputs);

// Pair up the spends and outputs, extending with dummy values as necessary.
let pre_actions: Vec<_> = {
Expand Down Expand Up @@ -842,7 +839,7 @@ pub mod testing {
fn into_bundle<V: TryFrom<i64>>(mut self) -> Bundle<Authorized, V> {
let fvk = FullViewingKey::from(&self.sk);
let flags = Flags::from_parts(true, true);
let mut builder = Builder::new(flags, self.anchor, PaddingRule::DEFAULT);
let mut builder = Builder::new(flags, self.anchor);

for (note, path) in self.notes.into_iter() {
builder.add_spend(fvk.clone(), note, path).unwrap();
Expand All @@ -859,7 +856,7 @@ pub mod testing {

let pk = ProvingKey::build();
builder
.build(&mut self.rng)
.build(&mut self.rng, &PaddingRule::DEFAULT)
.unwrap()
.unwrap()
.create_proof(&pk, &mut self.rng)
Expand Down Expand Up @@ -962,7 +959,6 @@ mod tests {
let mut builder = Builder::new(
Flags::from_parts(true, true),
EMPTY_ROOTS[MERKLE_DEPTH_ORCHARD].into(),
PaddingRule::DEFAULT,
);

builder
Expand All @@ -972,7 +968,7 @@ mod tests {
assert_eq!(balance, -5000);

let bundle: Bundle<Authorized, i64> = builder
.build(&mut rng)
.build(&mut rng, &PaddingRule::DEFAULT)
.unwrap()
.unwrap()
.create_proof(&pk, &mut rng)
Expand Down
15 changes: 10 additions & 5 deletions tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ fn bundle_chain() {
// Use the empty tree.
let anchor = MerkleHashOrchard::empty_root(32.into()).into();

let mut builder =
Builder::new(Flags::from_parts(false, true), anchor, PaddingRule::DEFAULT);
let mut builder = Builder::new(Flags::from_parts(false, true), anchor);
assert_eq!(
builder.add_output(None, recipient, NoteValue::from_raw(5000), None),
Ok(())
);
let unauthorized = builder.build(&mut rng).unwrap().unwrap();
let unauthorized = builder
.build(&mut rng, &PaddingRule::DEFAULT)
.unwrap()
.unwrap();
let sighash = unauthorized.commitment().into();
let proven = unauthorized.create_proof(&pk, &mut rng).unwrap();
proven.apply_signatures(rng, sighash, &[]).unwrap()
Expand Down Expand Up @@ -84,13 +86,16 @@ fn bundle_chain() {
let anchor = root.into();
assert_eq!(anchor, merkle_path.root(cmx));

let mut builder = Builder::new(Flags::from_parts(true, true), anchor, PaddingRule::DEFAULT);
let mut builder = Builder::new(Flags::from_parts(true, true), anchor);
assert_eq!(builder.add_spend(fvk, note, merkle_path), Ok(()));
assert_eq!(
builder.add_output(None, recipient, NoteValue::from_raw(5000), None),
Ok(())
);
let unauthorized = builder.build(&mut rng).unwrap().unwrap();
let unauthorized = builder
.build(&mut rng, &PaddingRule::DEFAULT)
.unwrap()
.unwrap();
let sighash = unauthorized.commitment().into();
let proven = unauthorized.create_proof(&pk, &mut rng).unwrap();
proven
Expand Down

0 comments on commit 89bc5cd

Please sign in to comment.