Skip to content

Commit

Permalink
Rename BatchId -> BatchJobId to distinguish with batch hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Sep 26, 2024
1 parent 3a35877 commit 05e4e75
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
6 changes: 3 additions & 3 deletions crates/block-producer/src/batch_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{debug, info, instrument, Span};

use crate::{
block_builder::BlockBuilder,
mempool::{BatchId, Mempool},
mempool::{BatchJobId, Mempool},
ProvenTransaction, SharedRwVec, COMPONENT,
};

Expand Down Expand Up @@ -221,7 +221,7 @@ pub struct BatchProducer {
pub tx_per_batch: usize,
}

type BatchResult = Result<(BatchId, TransactionBatch), (BatchId, BuildBatchError)>;
type BatchResult = Result<(BatchJobId, TransactionBatch), (BatchJobId, BuildBatchError)>;

/// Wrapper around tokio's JoinSet that remains pending if the set is empty,
/// instead of returning None.
Expand All @@ -243,7 +243,7 @@ impl WorkerPool {

fn spawn(
&mut self,
id: BatchId,
id: BatchJobId,
transactions: Vec<Arc<ProvenTransaction>>,
note_info: NoteAuthenticationInfo,
) {
Expand Down
24 changes: 12 additions & 12 deletions crates/block-producer/src/mempool/batch_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ use std::collections::{BTreeMap, BTreeSet};
use miden_objects::transaction::TransactionId;
use miden_tx::utils::collections::KvMap;

use super::BatchId;
use super::BatchJobId;

#[derive(Default, Clone)]
pub struct BatchGraph {
nodes: BTreeMap<BatchId, Node>,
roots: BTreeSet<BatchId>,
nodes: BTreeMap<BatchJobId, Node>,
roots: BTreeSet<BatchJobId>,

/// Allows for reverse lookup of transaction -> batch.
transactions: BTreeMap<TransactionId, BatchId>,
transactions: BTreeMap<TransactionId, BatchJobId>,
}

impl BatchGraph {
pub fn insert(
&mut self,
id: BatchId,
id: BatchJobId,
transactions: Vec<TransactionId>,
parents: BTreeSet<TransactionId>,
) {
Expand Down Expand Up @@ -59,7 +59,7 @@ impl BatchGraph {
}

/// Removes the batch and all of its descendents from the graph.
pub fn purge_subgraph(&mut self, id: BatchId) -> Vec<(BatchId, Vec<TransactionId>)> {
pub fn purge_subgraph(&mut self, id: BatchJobId) -> Vec<(BatchJobId, Vec<TransactionId>)> {
let mut removed = Vec::new();

let mut to_process = vec![id];
Expand Down Expand Up @@ -100,7 +100,7 @@ impl BatchGraph {
/// Removes a set of batches from the graph without removing any descendents.
///
/// This is intended to cull completed batches from stale blocks.
pub fn remove_stale(&mut self, batches: Vec<BatchId>) -> Vec<TransactionId> {
pub fn remove_stale(&mut self, batches: Vec<BatchJobId>) -> Vec<TransactionId> {
let mut transactions = Vec::new();

for batch in batches {
Expand All @@ -124,7 +124,7 @@ impl BatchGraph {
}

/// Mark a batch as proven if it exists.
pub fn mark_proven(&mut self, id: BatchId) {
pub fn mark_proven(&mut self, id: BatchJobId) {
// Its possible for inflight batches to have been removed as part
// of another batches failure.
if let Some(node) = self.nodes.get_mut(&id) {
Expand All @@ -133,7 +133,7 @@ impl BatchGraph {
}
}

pub fn pop_for_blocking(&mut self) -> Option<(BatchId, Vec<TransactionId>)> {
pub fn pop_for_blocking(&mut self) -> Option<(BatchJobId, Vec<TransactionId>)> {
let batch_id = self.roots.pop_first()?;
let node = self.nodes.get_mut(&batch_id).expect("Root node must be in graph");
node.status = Status::InBlock;
Expand All @@ -149,7 +149,7 @@ impl BatchGraph {
Some((batch_id, transactions))
}

fn try_make_root(&mut self, id: BatchId) {
fn try_make_root(&mut self, id: BatchJobId) {
let node = self.nodes.get_mut(&id).expect("Node must be in graph");

for parent in node.parents.clone() {
Expand All @@ -167,8 +167,8 @@ impl BatchGraph {
struct Node {
status: Status,
transactions: Vec<TransactionId>,
parents: BTreeSet<BatchId>,
children: BTreeSet<BatchId>,
parents: BTreeSet<BatchJobId>,
children: BTreeSet<BatchJobId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
21 changes: 12 additions & 9 deletions crates/block-producer/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ mod batch_graph;
mod transaction_graph;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct BatchId(u64);
pub struct BatchJobId(u64);

impl Display for BatchId {
impl Display for BatchJobId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl BatchId {
impl BatchJobId {
pub fn increment(mut self) {
self.0 += 1;
}
Expand Down Expand Up @@ -71,10 +71,10 @@ pub struct Mempool {
batches: BatchGraph,

/// The next batches ID.
next_batch_id: BatchId,
next_batch_id: BatchJobId,

/// Blocks which are inflight or completed but not yet considered stale.
block_pool: BTreeMap<BlockNumber, Vec<BatchId>>,
block_pool: BTreeMap<BlockNumber, Vec<BatchJobId>>,

/// The current block height of the chain.
completed_blocks: BlockNumber,
Expand Down Expand Up @@ -140,7 +140,10 @@ impl Mempool {
/// Transactions are returned in a valid execution ordering.
///
/// Returns `None` if no transactions are available.
pub fn select_batch(&mut self, count: usize) -> Option<(BatchId, Vec<Arc<ProvenTransaction>>)> {
pub fn select_batch(
&mut self,
count: usize,
) -> Option<(BatchJobId, Vec<Arc<ProvenTransaction>>)> {
let mut parents = BTreeSet::new();
let mut batch = Vec::with_capacity(count);

Expand Down Expand Up @@ -171,7 +174,7 @@ impl Mempool {
/// Drops the failed batch and all of its descendents.
///
/// Transactions are placed back in the queue.
pub fn batch_failed(&mut self, batch: BatchId) {
pub fn batch_failed(&mut self, batch: BatchJobId) {
let removed_batches = self.batches.purge_subgraph(batch);

// Its possible to receive failures for batches which were already removed
Expand All @@ -189,14 +192,14 @@ impl Mempool {
}

/// Marks a batch as proven if it exists.
pub fn batch_proved(&mut self, batch_id: BatchId) {
pub fn batch_proved(&mut self, batch_id: BatchJobId) {
self.batches.mark_proven(batch_id);
}

/// Select at most `count` batches which are ready to be placed into the next block.
///
/// May return an empty batch set if no batches are ready.
pub fn select_block(&mut self, count: usize) -> (BlockNumber, Vec<BatchId>) {
pub fn select_block(&mut self, count: usize) -> (BlockNumber, Vec<BatchJobId>) {
// TODO: should return actual batch transaction data as well.

let mut batches = Vec::with_capacity(count);
Expand Down
2 changes: 1 addition & 1 deletion crates/block-producer/src/mempool/transaction_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use miden_objects::transaction::{ProvenTransaction, TransactionId};

use super::BatchId;
use super::BatchJobId;

#[derive(Default, Clone, Debug)]
pub struct TransactionGraph {
Expand Down

0 comments on commit 05e4e75

Please sign in to comment.