Skip to content

Commit 53b4ea4

Browse files
committed
fix(network-monitor): move local proving off async runtime
1 parent d96a547 commit 53b4ea4

3 files changed

Lines changed: 66 additions & 40 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [BREAKING] Removed `CheckNullifiers` endpoint ([#2049](https://github.com/0xMiden/node/pull/2049)).
2121
- Replaced blocking-in-async operations in the validator, remote prover, and ntx-builder with `spawn_blocking` to avoid starving the Tokio runtime ([#2041](https://github.com/0xMiden/node/pull/2041)).
2222
- Replaced local store block proving with `spawn_blocking` to avoid starving the Tokio runtime ([#1976](https://github.com/0xMiden/node/issues/1976)).
23+
- Replaced local network monitor transaction execution and proving with `spawn_blocking` to avoid starving the Tokio runtime ([#1976](https://github.com/0xMiden/node/issues/1976)).
2324
- Implemented persistent RocksDB backend for `AccountStateForest`, improving startup time ([#2020](https://github.com/0xMiden/node/pull/2020)).
2425
- [BREAKING] Replaced binding URL env vars and CLI flags with listen socket addresses ([#2054](https://github.com/0xMiden/node/pull/2054)).
2526
- [BREAKING] `BlockRange.block_to` is now required for all RPC endpoints ([#2056](https://github.com/0xMiden/node/pull/2056)).

bin/network-monitor/src/counter.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use anyhow::{Context, Result};
1111
use miden_node_proto::clients::RpcClient;
1212
use miden_node_proto::generated::rpc::BlockHeaderByNumberRequest;
1313
use miden_node_proto::generated::transaction::ProvenTransaction;
14+
use miden_node_utils::spawn::spawn_blocking_in_current_span;
1415
use miden_protocol::account::auth::AuthSecretKey;
1516
use miden_protocol::account::{Account, AccountCode, AccountHeader, AccountId};
1617
use miden_protocol::asset::AssetVault;
@@ -271,10 +272,6 @@ impl IncrementService {
271272
err
272273
)]
273274
async fn submit_increment(&mut self) -> Result<(String, AccountHeader, BlockNumber)> {
274-
let authenticator = BasicAuthenticator::new(&[AuthSecretKey::Falcon512Poseidon2(
275-
self.tx.secret_key.clone(),
276-
)]);
277-
278275
let account_interface = AccountInterface::from_account(&self.tx.wallet_account);
279276

280277
let (network_note, note_recipient) = create_network_note(
@@ -285,26 +282,38 @@ impl IncrementService {
285282
)?;
286283
let script = account_interface.build_send_notes_script(&[network_note.into()], None)?;
287284

288-
let executor =
289-
TransactionExecutor::new(&self.tx.data_store).with_authenticator(&authenticator);
290-
291285
let mut tx_args = TransactionArgs::default().with_tx_script(script);
292286
tx_args.add_output_note_recipient(Box::new(note_recipient));
293287

294-
let executed_tx = Box::pin(executor.execute_transaction(
295-
self.tx.wallet_account.id(),
296-
self.tx.block_header.block_num(),
297-
InputNotes::default(),
298-
tx_args,
299-
))
288+
let data_store = self.tx.data_store.clone();
289+
let secret_key = self.tx.secret_key.clone();
290+
let account_id = self.tx.wallet_account.id();
291+
let block_num = self.tx.block_header.block_num();
292+
let handle = tokio::runtime::Handle::current();
293+
let (proven_tx, tx_inputs, final_account) = spawn_blocking_in_current_span(move || {
294+
let authenticator =
295+
BasicAuthenticator::new(&[AuthSecretKey::Falcon512Poseidon2(secret_key)]);
296+
let executor = TransactionExecutor::new(&data_store).with_authenticator(&authenticator);
297+
298+
let executed_tx = handle
299+
.block_on(executor.execute_transaction(
300+
account_id,
301+
block_num,
302+
InputNotes::default(),
303+
tx_args,
304+
))
305+
.context("Failed to execute transaction")?;
306+
307+
let tx_inputs = executed_tx.tx_inputs().to_bytes();
308+
let final_account = executed_tx.final_account().clone();
309+
let proven_tx = handle
310+
.block_on(LocalTransactionProver::default().prove(executed_tx))
311+
.context("Failed to prove transaction")?;
312+
313+
Ok::<_, anyhow::Error>((proven_tx, tx_inputs, final_account))
314+
})
300315
.await
301-
.context("Failed to execute transaction")?;
302-
303-
let tx_inputs = executed_tx.tx_inputs().to_bytes();
304-
let final_account = executed_tx.final_account().clone();
305-
306-
let prover = LocalTransactionProver::default();
307-
let proven_tx = prover.prove(executed_tx).await.context("Failed to prove transaction")?;
316+
.context("counter increment task failed")??;
308317

309318
let request = ProvenTransaction {
310319
transaction: proven_tx.to_bytes(),

bin/network-monitor/src/deploy/mod.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use anyhow::{Context, Result};
1010
use miden_node_proto::clients::{Builder, RpcClient};
1111
use miden_node_proto::generated::rpc::BlockHeaderByNumberRequest;
1212
use miden_node_proto::generated::transaction::ProvenTransaction;
13+
use miden_node_utils::spawn::spawn_blocking_in_current_span;
1314
use miden_protocol::account::{Account, AccountId, PartialAccount, StorageMapKey};
1415
use miden_protocol::asset::{AssetVaultKey, AssetWitness};
1516
use miden_protocol::block::{BlockHeader, BlockNumber};
@@ -141,26 +142,31 @@ pub async fn deploy_counter_account(counter_account: &Account, rpc_url: &Url) ->
141142
let mut data_store = MonitorDataStore::new(genesis_header, genesis_chain_mmr);
142143
data_store.add_account(counter_account.clone());
143144

144-
let executor: TransactionExecutor<'_, '_, _, BasicAuthenticator> =
145-
TransactionExecutor::new(&data_store).with_debug_mode();
146-
147-
let tx_args = TransactionArgs::default();
148-
149-
let executed_tx = executor
150-
.execute_transaction(
151-
counter_account.id(),
152-
BlockNumber::GENESIS,
153-
InputNotes::default(),
154-
tx_args,
155-
)
156-
.await
157-
.context("Failed to execute transaction")?;
158-
159-
let transaction_inputs = executed_tx.tx_inputs().to_bytes();
160-
161-
let prover = LocalTransactionProver::default();
162-
163-
let proven_tx = prover.prove(executed_tx).await.context("Failed to prove transaction")?;
145+
let account_id = counter_account.id();
146+
let handle = tokio::runtime::Handle::current();
147+
let (proven_tx, transaction_inputs) = spawn_blocking_in_current_span(move || {
148+
let executor: TransactionExecutor<'_, '_, _, BasicAuthenticator> =
149+
TransactionExecutor::new(&data_store).with_debug_mode();
150+
151+
let tx_args = TransactionArgs::default();
152+
let executed_tx = handle
153+
.block_on(executor.execute_transaction(
154+
account_id,
155+
BlockNumber::GENESIS,
156+
InputNotes::default(),
157+
tx_args,
158+
))
159+
.context("Failed to execute transaction")?;
160+
161+
let transaction_inputs = executed_tx.tx_inputs().to_bytes();
162+
let proven_tx = handle
163+
.block_on(LocalTransactionProver::default().prove(executed_tx))
164+
.context("Failed to prove transaction")?;
165+
166+
Ok::<_, anyhow::Error>((proven_tx, transaction_inputs))
167+
})
168+
.await
169+
.context("counter account deployment task failed")??;
164170

165171
let request = ProvenTransaction {
166172
transaction: proven_tx.to_bytes(),
@@ -186,6 +192,16 @@ pub struct MonitorDataStore {
186192
mast_store: TransactionMastStore,
187193
}
188194

195+
impl Clone for MonitorDataStore {
196+
fn clone(&self) -> Self {
197+
let mut cloned = Self::new(self.block_header.clone(), self.partial_block_chain.clone());
198+
for account in self.accounts.values() {
199+
cloned.add_account(account.clone());
200+
}
201+
cloned
202+
}
203+
}
204+
189205
impl MonitorDataStore {
190206
pub fn new(block_header: BlockHeader, partial_block_chain: PartialBlockchain) -> Self {
191207
Self {

0 commit comments

Comments
 (0)