Skip to content

Commit acacbf3

Browse files
chore: refactor ntx-builder code org
1 parent 580f9cb commit acacbf3

File tree

16 files changed

+212
-207
lines changed

16 files changed

+212
-207
lines changed

crates/ntx-builder/src/actor/account_state.rs renamed to crates/ntx-builder/src/actor/candidate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use miden_protocol::account::Account;
44
use miden_protocol::block::BlockHeader;
55
use miden_protocol::transaction::PartialBlockchain;
66

7-
use crate::actor::inflight_note::InflightNetworkNote;
7+
use crate::inflight_note::InflightNetworkNote;
88

99
// TRANSACTION CANDIDATE
1010
// ================================================================================================

crates/ntx-builder/src/actor/execute.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ use tokio::task::JoinError;
5353
use tracing::{Instrument, instrument};
5454

5555
use crate::COMPONENT;
56-
use crate::actor::account_state::TransactionCandidate;
57-
use crate::block_producer::BlockProducerClient;
56+
use crate::actor::candidate::TransactionCandidate;
57+
use crate::clients::{BlockProducerClient, StoreClient};
5858
use crate::db::Db;
59-
use crate::store::StoreClient;
6059

6160
#[derive(Debug, thiserror::Error)]
6261
pub enum NtxError {

crates/ntx-builder/src/actor/inflight_note.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

crates/ntx-builder/src/actor/mod.rs

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
pub(crate) mod account_effect;
2-
pub mod account_state;
1+
pub mod candidate;
32
mod execute;
4-
pub(crate) mod inflight_note;
53

64
use std::num::NonZeroUsize;
75
use std::sync::Arc;
86
use std::time::Duration;
97

10-
use account_state::TransactionCandidate;
8+
use candidate::TransactionCandidate;
119
use futures::FutureExt;
1210
use miden_node_proto::clients::{Builder, ValidatorClient};
1311
use miden_node_proto::domain::account::NetworkAccountId;
@@ -23,10 +21,9 @@ use tokio::sync::{AcquireError, Notify, RwLock, Semaphore, mpsc};
2321
use tokio_util::sync::CancellationToken;
2422
use url::Url;
2523

26-
use crate::block_producer::BlockProducerClient;
27-
use crate::builder::ChainState;
24+
use crate::chain_state::ChainState;
25+
use crate::clients::{BlockProducerClient, StoreClient};
2826
use crate::db::Db;
29-
use crate::store::StoreClient;
3027

3128
/// Converts a database result into an `ActorShutdownReason` error, logging the error on failure.
3229
fn db_query<T>(
@@ -446,69 +443,3 @@ impl AccountActor {
446443
let _ = ack_rx.await;
447444
}
448445
}
449-
450-
// HELPERS
451-
// ================================================================================================
452-
453-
/// Checks if the backoff block period has passed.
454-
///
455-
/// The number of blocks passed since the last attempt must be greater than or equal to
456-
/// e^(0.25 * `attempt_count`) rounded to the nearest integer.
457-
///
458-
/// This evaluates to the following:
459-
/// - After 1 attempt, the backoff period is 1 block.
460-
/// - After 3 attempts, the backoff period is 2 blocks.
461-
/// - After 10 attempts, the backoff period is 12 blocks.
462-
/// - After 20 attempts, the backoff period is 148 blocks.
463-
/// - etc...
464-
#[expect(clippy::cast_precision_loss, clippy::cast_sign_loss)]
465-
fn has_backoff_passed(
466-
chain_tip: BlockNumber,
467-
last_attempt: Option<BlockNumber>,
468-
attempts: usize,
469-
) -> bool {
470-
if attempts == 0 {
471-
return true;
472-
}
473-
// Compute the number of blocks passed since the last attempt.
474-
let blocks_passed = last_attempt
475-
.and_then(|last| chain_tip.checked_sub(last.as_u32()))
476-
.unwrap_or_default();
477-
478-
// Compute the exponential backoff threshold: Δ = e^(0.25 * n).
479-
let backoff_threshold = (0.25 * attempts as f64).exp().round() as usize;
480-
481-
// Check if the backoff period has passed.
482-
blocks_passed.as_usize() > backoff_threshold
483-
}
484-
485-
#[cfg(test)]
486-
mod tests {
487-
use miden_protocol::block::BlockNumber;
488-
489-
use super::has_backoff_passed;
490-
491-
#[rstest::rstest]
492-
#[test]
493-
#[case::all_zero(Some(BlockNumber::GENESIS), BlockNumber::GENESIS, 0, true)]
494-
#[case::no_attempts(None, BlockNumber::GENESIS, 0, true)]
495-
#[case::one_attempt(Some(BlockNumber::GENESIS), BlockNumber::from(2), 1, true)]
496-
#[case::three_attempts(Some(BlockNumber::GENESIS), BlockNumber::from(3), 3, true)]
497-
#[case::ten_attempts(Some(BlockNumber::GENESIS), BlockNumber::from(13), 10, true)]
498-
#[case::twenty_attempts(Some(BlockNumber::GENESIS), BlockNumber::from(149), 20, true)]
499-
#[case::one_attempt_false(Some(BlockNumber::GENESIS), BlockNumber::from(1), 1, false)]
500-
#[case::three_attempts_false(Some(BlockNumber::GENESIS), BlockNumber::from(2), 3, false)]
501-
#[case::ten_attempts_false(Some(BlockNumber::GENESIS), BlockNumber::from(12), 10, false)]
502-
#[case::twenty_attempts_false(Some(BlockNumber::GENESIS), BlockNumber::from(148), 20, false)]
503-
fn backoff_has_passed(
504-
#[case] last_attempt_block_num: Option<BlockNumber>,
505-
#[case] current_block_num: BlockNumber,
506-
#[case] attempt_count: usize,
507-
#[case] backoff_should_have_passed: bool,
508-
) {
509-
assert_eq!(
510-
backoff_should_have_passed,
511-
has_backoff_passed(current_block_num, last_attempt_block_num, attempt_count)
512-
);
513-
}
514-
}

crates/ntx-builder/src/builder.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,16 @@ use miden_node_proto::domain::account::NetworkAccountId;
77
use miden_node_proto::domain::mempool::MempoolEvent;
88
use miden_protocol::account::delta::AccountUpdateDetails;
99
use miden_protocol::block::BlockHeader;
10-
use miden_protocol::crypto::merkle::mmr::PartialMmr;
11-
use miden_protocol::transaction::PartialBlockchain;
1210
use tokio::sync::{RwLock, mpsc};
1311
use tokio_stream::StreamExt;
1412
use tonic::Status;
1513

1614
use crate::NtxBuilderConfig;
1715
use crate::actor::{AccountActorContext, AccountOrigin, ActorRequest};
16+
use crate::chain_state::ChainState;
17+
use crate::clients::StoreClient;
1818
use crate::coordinator::Coordinator;
1919
use crate::db::Db;
20-
use crate::store::StoreClient;
21-
22-
// CHAIN STATE
23-
// ================================================================================================
24-
25-
/// Contains information about the chain that is relevant to the [`NetworkTransactionBuilder`] and
26-
/// all account actors managed by the [`Coordinator`].
27-
///
28-
/// The chain MMR stored here contains:
29-
/// - The MMR peaks.
30-
/// - Block headers and authentication paths for the last [`NtxBuilderConfig::max_block_count`]
31-
/// blocks.
32-
///
33-
/// Authentication paths for older blocks are pruned because the NTX builder executes all notes as
34-
/// "unauthenticated" (see [`InputNotes::from_unauthenticated_notes`]) and therefore does not need
35-
/// to prove that input notes were created in specific past blocks.
36-
#[derive(Debug, Clone)]
37-
pub struct ChainState {
38-
/// The current tip of the chain.
39-
pub chain_tip_header: BlockHeader,
40-
/// A partial representation of the chain MMR.
41-
///
42-
/// Contains block headers and authentication paths for the last
43-
/// [`NtxBuilderConfig::max_block_count`] blocks only, since all notes are executed as
44-
/// unauthenticated.
45-
pub chain_mmr: Arc<PartialBlockchain>,
46-
}
47-
48-
impl ChainState {
49-
/// Constructs a new instance of [`ChainState`].
50-
pub(crate) fn new(chain_tip_header: BlockHeader, chain_mmr: PartialMmr) -> Self {
51-
let chain_mmr = PartialBlockchain::new(chain_mmr, [])
52-
.expect("partial blockchain should build from partial mmr");
53-
Self {
54-
chain_tip_header,
55-
chain_mmr: Arc::new(chain_mmr),
56-
}
57-
}
58-
59-
/// Consumes the chain state and returns the chain tip header and the partial blockchain as a
60-
/// tuple.
61-
pub fn into_parts(self) -> (BlockHeader, Arc<PartialBlockchain>) {
62-
(self.chain_tip_header, self.chain_mmr)
63-
}
64-
}
6520

6621
// NETWORK TRANSACTION BUILDER
6722
// ================================================================================================
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::sync::Arc;
2+
3+
use miden_protocol::block::BlockHeader;
4+
use miden_protocol::crypto::merkle::mmr::PartialMmr;
5+
use miden_protocol::transaction::PartialBlockchain;
6+
7+
// CHAIN STATE
8+
// ================================================================================================
9+
10+
/// Contains information about the chain that is relevant to the [`NetworkTransactionBuilder`] and
11+
/// all account actors managed by the [`Coordinator`].
12+
///
13+
/// The chain MMR stored here contains:
14+
/// - The MMR peaks.
15+
/// - Block headers and authentication paths for the last
16+
/// [`NtxBuilderConfig::max_block_count`](crate::NtxBuilderConfig::max_block_count) blocks.
17+
///
18+
/// Authentication paths for older blocks are pruned because the NTX builder executes all notes as
19+
/// "unauthenticated" (see [`InputNotes::from_unauthenticated_notes`]) and therefore does not need
20+
/// to prove that input notes were created in specific past blocks.
21+
#[derive(Debug, Clone)]
22+
pub struct ChainState {
23+
/// The current tip of the chain.
24+
pub chain_tip_header: BlockHeader,
25+
/// A partial representation of the chain MMR.
26+
///
27+
/// Contains block headers and authentication paths for the last
28+
/// [`NtxBuilderConfig::max_block_count`](crate::NtxBuilderConfig::max_block_count) blocks
29+
/// only, since all notes are executed as unauthenticated.
30+
pub chain_mmr: Arc<PartialBlockchain>,
31+
}
32+
33+
impl ChainState {
34+
/// Constructs a new instance of [`ChainState`].
35+
pub(crate) fn new(chain_tip_header: BlockHeader, chain_mmr: PartialMmr) -> Self {
36+
let chain_mmr = PartialBlockchain::new(chain_mmr, [])
37+
.expect("partial blockchain should build from partial mmr");
38+
Self {
39+
chain_tip_header,
40+
chain_mmr: Arc::new(chain_mmr),
41+
}
42+
}
43+
44+
/// Consumes the chain state and returns the chain tip header and the partial blockchain as a
45+
/// tuple.
46+
pub fn into_parts(self) -> (BlockHeader, Arc<PartialBlockchain>) {
47+
(self.chain_tip_header, self.chain_mmr)
48+
}
49+
}
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod block_producer;
2+
mod store;
3+
4+
pub use block_producer::BlockProducerClient;
5+
pub use store::StoreClient;

crates/ntx-builder/src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use miden_protocol::transaction::TransactionId;
1313
use tracing::{info, instrument};
1414

1515
use crate::COMPONENT;
16-
use crate::actor::inflight_note::InflightNetworkNote;
1716
use crate::db::migrations::apply_migrations;
1817
use crate::db::models::queries;
18+
use crate::inflight_note::InflightNetworkNote;
1919

2020
pub(crate) mod models;
2121

0 commit comments

Comments
 (0)