Skip to content

Commit

Permalink
perf: wrap tx prover with spawn_blocking
Browse files Browse the repository at this point in the history
The prover may take a very long time (minutes) to run and should not
block the tokio executor.

As such, we wrap `create_transaction_from_data()` with
`spawn_blocking()` in order to execute this task in tokio's blocking
threadpool.
  • Loading branch information
dan-da committed Apr 24, 2024
1 parent b4a939c commit b160e1d
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/models/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ impl GlobalState {
.wallet_secret
.nth_generation_spending_key(0);

// assemble transaction object
Ok(Self::create_transaction_from_data(
// assemble transaction object (lengthy operation)
Self::create_transaction_from_data(
spending_key,
inputs,
spendable_utxos_and_mps,
Expand All @@ -575,7 +575,8 @@ impl GlobalState {
timestamp,

Check failure on line 575 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (ubuntu-latest)

mismatched types

Check failure on line 575 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (windows-latest)

mismatched types

Check failure on line 575 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (macos-latest)

mismatched types
mutator_set_accumulator,
privacy,
))
)
.await
}

/// Given a list of UTXOs with receiver data, assemble owned and synced and spendable
Expand Down Expand Up @@ -638,7 +639,47 @@ impl GlobalState {
/// Assembles a transaction kernel and supporting witness or proof(s) from
/// the given transaction data.
#[allow(clippy::too_many_arguments)]
fn create_transaction_from_data(
async fn create_transaction_from_data(
spending_key: SpendingKey,
inputs: Vec<RemovalRecord>,
spendable_utxos_and_mps: Vec<(Utxo, LockScript, MsMembershipProof)>,
outputs: Vec<AdditionRecord>,
output_utxos: Vec<Utxo>,
fee: NeptuneCoins,
public_announcements: Vec<PublicAnnouncement>,
timestamp: u64,
mutator_set_accumulator: MutatorSetAccumulator,
privacy: bool,
) -> Result<Transaction> {
// note: this executes the prover which can take a very
// long time, perhaps minutes. As such, we use
// spawn_blocking() to execute on tokio's blocking
// threadpool and avoid blocking the tokio executor
// and other async tasks.
let transaction = tokio::task::spawn_blocking(move || {
Self::create_transaction_from_data_worker(
spending_key,
inputs,
spendable_utxos_and_mps,
outputs,
output_utxos,
fee,
public_announcements,
timestamp,

Check failure on line 668 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (ubuntu-latest)

mismatched types

Check failure on line 668 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (windows-latest)

mismatched types

Check failure on line 668 in src/models/state/mod.rs

View workflow job for this annotation

GitHub Actions / format, lint, test (macos-latest)

mismatched types
mutator_set_accumulator,
privacy,
)
})
.await?;
Ok(transaction)
}

// note: this executes the prover which can take a very
// long time, perhaps minutes. It should never be
// called directly.
// Use create_transaction_from_data() instead.
#[allow(clippy::too_many_arguments)]
fn create_transaction_from_data_worker(
spending_key: SpendingKey,
inputs: Vec<RemovalRecord>,
spendable_utxos_and_mps: Vec<(Utxo, LockScript, MsMembershipProof)>,
Expand Down Expand Up @@ -1309,7 +1350,7 @@ mod global_state_tests {
.nth_generation_spending_key(0);

// assemble transaction object
Ok(GlobalState::create_transaction_from_data(
GlobalState::create_transaction_from_data(
spending_key,
inputs,
spendable_utxos_and_mps,
Expand All @@ -1320,7 +1361,8 @@ mod global_state_tests {
timestamp,
mutator_set_accumulator,
privacy,
))
)
.await
}

#[traced_test]
Expand Down

0 comments on commit b160e1d

Please sign in to comment.