Skip to content

Commit

Permalink
chore: minor MAST module cleanup (#1407)
Browse files Browse the repository at this point in the history
* refactor: remove MerkleTreeNode trait
* chore: added comments and section separators
* fix: remove implicit serialization of MastNodeId
  • Loading branch information
bobbinth authored Jul 22, 2024
1 parent 2c22ac1 commit b10567b
Show file tree
Hide file tree
Showing 23 changed files with 481 additions and 365 deletions.
2 changes: 1 addition & 1 deletion assembly/src/assembler/mast_forest_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ops::Index;
use alloc::{collections::BTreeMap, vec::Vec};
use vm_core::{
crypto::hash::RpoDigest,
mast::{MastForest, MastForestError, MastNode, MastNodeId, MerkleTreeNode},
mast::{MastForest, MastForestError, MastNode, MastNodeId},
DecoratorList, Operation,
};

Expand Down
2 changes: 1 addition & 1 deletion assembly/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use mast_forest_builder::MastForestBuilder;
use vm_core::{
mast::{MastForest, MastNodeId, MerkleTreeNode},
mast::{MastForest, MastNodeId},
Decorator, DecoratorList, Kernel, Operation, Program,
};

Expand Down
2 changes: 1 addition & 1 deletion assembly/src/assembler/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
diagnostics::SourceFile,
LibraryPath, RpoDigest, SourceSpan, Spanned,
};
use vm_core::mast::{MastForest, MastNodeId, MerkleTreeNode};
use vm_core::mast::{MastForest, MastNodeId};

pub type CallSet = BTreeSet<RpoDigest>;

Expand Down
70 changes: 31 additions & 39 deletions core/src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,30 @@ use miden_crypto::hash::rpo::RpoDigest;

mod node;
pub use node::{
get_span_op_group_count, BasicBlockNode, CallNode, DynNode, ExternalNode, JoinNode, LoopNode,
MastNode, OpBatch, OperationOrDecorator, SplitNode, OP_BATCH_SIZE, OP_GROUP_SIZE,
BasicBlockNode, CallNode, DynNode, ExternalNode, JoinNode, LoopNode, MastNode, OpBatch,
OperationOrDecorator, SplitNode, OP_BATCH_SIZE, OP_GROUP_SIZE,
};
use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use winter_utils::DeserializationError;

mod serialization;

#[cfg(test)]
mod tests;

/// Encapsulates the behavior that a [`MastNode`] (and all its variants) is expected to have.
pub trait MerkleTreeNode {
fn digest(&self) -> RpoDigest;
fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a;
}

// MAST FOREST
// ================================================================================================

/// Represents the types of errors that can occur when dealing with MAST forest.
#[derive(Debug, thiserror::Error)]
pub enum MastForestError {
#[error(
"invalid node count: MAST forest exceeds the maximum of {} nodes",
MastForest::MAX_NODES
)]
TooManyNodes,
/// Represents one or more procedures, represented as a collection of [`MastNode`]s.
///
/// A [`MastForest`] does not have an entrypoint, and hence is not executable. A [`crate::Program`]
/// can be built from a [`MastForest`] to specify an entrypoint.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct MastForest {
/// All of the nodes local to the trees comprising the MAST forest.
nodes: Vec<MastNode>,

/// Roots of procedures defined within this MAST forest.
roots: Vec<MastNodeId>,
}

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -155,38 +152,33 @@ impl MastNodeId {
}
}

impl fmt::Display for MastNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MastNodeId({})", self.0)
impl From<MastNodeId> for u32 {
fn from(value: MastNodeId) -> Self {
value.0
}
}

impl Serializable for MastNodeId {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target)
impl From<&MastNodeId> for u32 {
fn from(value: &MastNodeId) -> Self {
value.0
}
}

impl Deserializable for MastNodeId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let inner = source.read_u32()?;

Ok(Self(inner))
impl fmt::Display for MastNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MastNodeId({})", self.0)
}
}

// MAST FOREST ERROR
// ================================================================================================

/// Represents one or more procedures, represented as a collection of [`MastNode`]s.
///
/// A [`MastForest`] does not have an entrypoint, and hence is not executable. A [`crate::Program`]
/// can be built from a [`MastForest`] to specify an entrypoint.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct MastForest {
/// All of the nodes local to the trees comprising the MAST forest.
nodes: Vec<MastNode>,

/// Roots of procedures defined within this MAST forest.
roots: Vec<MastNodeId>,
/// Represents the types of errors that can occur when dealing with MAST forest.
#[derive(Debug, thiserror::Error)]
pub enum MastForestError {
#[error(
"invalid node count: MAST forest exceeds the maximum of {} nodes",
MastForest::MAX_NODES
)]
TooManyNodes,
}
Loading

0 comments on commit b10567b

Please sign in to comment.