Builder: Move progress notifier configuration to a builder method

This is what builder methods are for :) and it helps to limit the growth
of alternate `build` methods.
This commit is contained in:
Jack Grigg 2021-05-26 21:58:08 +01:00
parent d9a3b98120
commit d031dabcfc
1 changed files with 13 additions and 23 deletions

View File

@ -407,6 +407,7 @@ pub struct Builder<'a, P: consensus::Parameters, R: RngCore> {
#[cfg(feature = "zfuture")]
tze_inputs: TzeInputs<'a, TransactionData>,
change_address: Option<(OutgoingViewingKey, PaymentAddress)>,
progress_notifier: Option<Sender<Progress>>,
_phantom: &'a PhantomData<P>,
}
@ -504,6 +505,7 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
#[cfg(feature = "zfuture")]
tze_inputs: TzeInputs::default(),
change_address: None,
progress_notifier: None,
_phantom: &PhantomData,
}
}
@ -610,21 +612,14 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
self.change_address = Some((ovk, to));
}
/// Builds a transaction from the configured spends and outputs.
/// Sets the notifier channel, where progress of building the transaction is sent.
///
/// Upon success, returns a tuple containing the final transaction, and the
/// [`TransactionMetadata`] generated during the build process.
///
/// `consensus_branch_id` must be valid for the block height that this transaction is
/// targeting. An invalid `consensus_branch_id` will *not* result in an error from
/// this function, and instead will generate a transaction that will be rejected by
/// the network.
pub fn build(
self,
consensus_branch_id: consensus::BranchId,
prover: &impl TxProver,
) -> Result<(Transaction, TransactionMetadata), Error> {
self.build_with_progress_notifier(consensus_branch_id, prover, None)
/// An update is sent after every Spend or Output is computed, and the `u32` sent
/// represents the total steps completed so far. It will eventually send number of
/// spends + outputs. If there's an error building the transaction, the channel is
/// closed.
pub fn with_progress_notifier(&mut self, progress_notifier: Sender<Progress>) {
self.progress_notifier = Some(progress_notifier);
}
/// Builds a transaction from the configured spends and outputs.
@ -636,16 +631,10 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
/// targeting. An invalid `consensus_branch_id` will *not* result in an error from
/// this function, and instead will generate a transaction that will be rejected by
/// the network.
///
/// `progress_notifier` sets the notifier channel, where progress of building the transaction is sent.
/// It sends an update after every Spend or Output is computed, and the `u32` sent represents
/// the total steps completed so far. It will eventually send number of spends + outputs.
/// If there's an error building the transaction, the channel is closed.
pub fn build_with_progress_notifier(
pub fn build(
mut self,
consensus_branch_id: consensus::BranchId,
prover: &impl TxProver,
progress_notifier: Option<Sender<Progress>>,
) -> Result<(Transaction, TransactionMetadata), Error> {
let mut tx_metadata = TransactionMetadata::new();
@ -775,7 +764,7 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
// Update progress and send a notification on the channel
progress += 1;
progress_notifier
self.progress_notifier
.as_ref()
.map(|tx| tx.send(Progress::new(progress, Some(total_progress))));
@ -861,7 +850,7 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> {
// Update progress and send a notification on the channel
progress += 1;
progress_notifier
self.progress_notifier
.as_ref()
.map(|tx| tx.send(Progress::new(progress, Some(total_progress))));
@ -1085,6 +1074,7 @@ mod tests {
#[cfg(feature = "zfuture")]
tze_inputs: TzeInputs::default(),
change_address: None,
progress_notifier: None,
_phantom: &PhantomData,
};