Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions console/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ pub trait Network:
fn INCLUSION_UPGRADE_HEIGHT() -> Result<u32>;

/// Returns the genesis block bytes.
///
/// Note, that this always returns the bytes of the production genesis block.
/// As a result, this may not match the bytes of the genesis block with the `test_targets` feature enabled.
fn genesis_bytes() -> &'static [u8];

/// Returns the restrictions list as a JSON-compatible string.
Expand Down
1 change: 1 addition & 0 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ test-helpers = [
]
test_targets = [
"snarkvm-console/test_targets",
"snarkvm-ledger-block/test_targets",
]
timer = [ "aleo-std/timer" ]

Expand Down
1 change: 1 addition & 0 deletions ledger/block/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ wasm = [
]
test = [ "snarkvm-synthesizer-process/test" ]
test-helpers = [ ]
test_targets = [ "snarkvm-console/test_targets" ]

[dependencies.snarkvm-console]
workspace = true
Expand Down
50 changes: 39 additions & 11 deletions ledger/block/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ impl<N: Network> ToBytes for Block<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::MainnetV0;

type CurrentNetwork = MainnetV0;
use console::network::{MainnetV0, TestnetV0};

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -170,28 +168,58 @@ mod tests {
}

#[test]
fn test_genesis_bytes() -> Result<()> {
fn test_genesis_bytes_mainnet_unchecked() -> Result<()> {
// Load the genesis block.
let genesis_block = Block::<CurrentNetwork>::read_le(CurrentNetwork::genesis_bytes()).unwrap();
let genesis_block = Block::<MainnetV0>::read_le_unchecked(MainnetV0::genesis_bytes()).unwrap();

// Check the byte representation.
let expected_bytes = genesis_block.to_bytes_le()?;
assert_eq!(genesis_block, Block::read_le(&expected_bytes[..])?);
assert_eq!(genesis_block, Block::read_le_unchecked(&expected_bytes[..])?);

Ok(())
}

#[test]
fn test_bincode() -> Result<()> {
fn test_genesis_bytes_testnet_unchecked() -> Result<()> {
// Load the genesis block.
let genesis_block = Block::<CurrentNetwork>::read_le(CurrentNetwork::genesis_bytes()).unwrap();
let genesis_block = Block::<TestnetV0>::read_le_unchecked(TestnetV0::genesis_bytes()).unwrap();

let bincode_data = bincode::serialize(&genesis_block)?;
let block = bincode::deserialize(&bincode_data)?;
// Check the byte representation.
let expected_bytes = genesis_block.to_bytes_le()?;
assert_eq!(genesis_block, Block::read_le_unchecked(&expected_bytes[..])?);

assert_eq!(genesis_block, block);
Ok(())
}

#[test]
fn test_genesis_bytes_mainnet() -> Result<()> {
// Load the genesis block.
let genesis_block = Block::<MainnetV0>::read_le(MainnetV0::genesis_bytes()).unwrap();

// Check the byte representation.
let expected_bytes = genesis_block.to_bytes_le()?;
assert_eq!(genesis_block, Block::read_le(&expected_bytes[..])?);
assert_eq!(genesis_block, Block::read_le_unchecked(&expected_bytes[..])?);

Ok(())
}

// When `test_targets` is enabled, the gensis bytes do not match the expected coinbase target.
// (except for Mainnet which ignores this feature)
#[cfg(not(feature = "test_targets"))]
mod production {
use super::*;
#[test]
fn test_genesis_bytes_testnet() -> Result<()> {
// Load the genesis block.
let genesis_block = Block::<TestnetV0>::read_le(TestnetV0::genesis_bytes()).unwrap();

// Check the byte representation.
let expected_bytes = genesis_block.to_bytes_le()?;
assert_eq!(genesis_block, Block::read_le(&expected_bytes[..])?);
assert_eq!(genesis_block, Block::read_le_unchecked(&expected_bytes[..])?);

Ok(())
}
}
}
60 changes: 42 additions & 18 deletions ledger/block/src/header/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::*;
impl<N: Network> FromBytes for Header<N> {
/// Reads the block header from the buffer.
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
fn read_le_with_unchecked<R: Read>(mut reader: R, unchecked: bool) -> IoResult<Self> {
// Read the version.
let version = u8::read_le(&mut reader)?;
// Ensure the version is valid.
Expand All @@ -27,25 +27,49 @@ impl<N: Network> FromBytes for Header<N> {
}

// Read from the buffer.
let previous_state_root = N::StateRoot::read_le(&mut reader)?;
let transactions_root = Field::<N>::read_le(&mut reader)?;
let finalize_root = Field::<N>::read_le(&mut reader)?;
let ratifications_root = Field::<N>::read_le(&mut reader)?;
let solutions_root = Field::<N>::read_le(&mut reader)?;
let subdag_root = Field::<N>::read_le(&mut reader)?;
let metadata = Metadata::read_le(&mut reader)?;
let previous_state_root = N::StateRoot::read_le_with_unchecked(&mut reader, unchecked)?;
let transactions_root = Field::<N>::read_le_with_unchecked(&mut reader, unchecked)?;
let finalize_root = Field::<N>::read_le_with_unchecked(&mut reader, unchecked)?;
let ratifications_root = Field::<N>::read_le_with_unchecked(&mut reader, unchecked)?;
let solutions_root = Field::<N>::read_le_with_unchecked(&mut reader, unchecked)?;
let subdag_root = Field::<N>::read_le_with_unchecked(&mut reader, unchecked)?;
let metadata = Metadata::read_le_with_unchecked(&mut reader, unchecked)?;

// Construct the block header.
Self::from(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
)
.map_err(|e| error(format!("{e:#?}")))
if unchecked {
Self::from_unchecked(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
)
} else {
Self::from(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
)
}
.map_err(into_io_error)
}

/// Reads the block header from the buffer.
#[inline]
fn read_le<R: Read>(reader: R) -> IoResult<Self> {
Self::read_le_with_unchecked(reader, false)
}

/// Reads the block header from the buffer without performing any validity checks.
#[inline]
fn read_le_unchecked<R: Read>(reader: R) -> IoResult<Self> {
Self::read_le_with_unchecked(reader, true)
}
}

Expand Down
51 changes: 37 additions & 14 deletions ledger/block/src/header/metadata/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use super::*;
impl<N: Network> FromBytes for Metadata<N> {
/// Reads the metadata from the buffer.
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
fn read_le_with_unchecked<R: Read>(mut reader: R, unchecked: bool) -> IoResult<Self> {
// Read the version.
let version = u8::read_le(&mut reader)?;
// Ensure the version is valid.
if version != 1 {
return Err(error("Invalid metadata version"));
return Err(io_error("Invalid metadata version"));
}

// Read from the buffer.
Expand All @@ -39,20 +39,43 @@ impl<N: Network> FromBytes for Metadata<N> {
let timestamp = i64::read_le(&mut reader)?;

// Construct the metadata.
Self::new(
network,
round,
height,
cumulative_weight,
cumulative_proof_target,
coinbase_target,
proof_target,
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
)
if unchecked {
Self::from_unchecked(
network,
round,
height,
cumulative_weight,
cumulative_proof_target,
coinbase_target,
proof_target,
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
)
} else {
Self::from(
network,
round,
height,
cumulative_weight,
cumulative_proof_target,
coinbase_target,
proof_target,
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
)
}
.map_err(into_io_error)
}

fn read_le<R: Read>(reader: R) -> IoResult<Self> {
Self::read_le_with_unchecked(reader, false)
}

fn read_le_unchecked<R: Read>(reader: R) -> IoResult<Self> {
Self::read_le_with_unchecked(reader, true)
}
}

impl<N: Network> ToBytes for Metadata<N> {
Expand Down
64 changes: 61 additions & 3 deletions ledger/block/src/header/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct Metadata<N: Network> {

impl<N: Network> Metadata<N> {
/// Initializes a new metadata with the given inputs.
///
/// This is identitcal to calling [`Self::from`].
#[allow(clippy::too_many_arguments)]
pub fn new(
network: u16,
Expand All @@ -66,9 +68,37 @@ impl<N: Network> Metadata<N> {
last_coinbase_target: u64,
last_coinbase_timestamp: i64,
timestamp: i64,
) -> Result<Self> {
Self::from(
network,
round,
height,
cumulative_weight,
cumulative_proof_target,
coinbase_target,
proof_target,
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
)
}

/// Initializes a new metadata with the given inputs.
#[allow(clippy::too_many_arguments)]
pub fn from(
network: u16,
round: u64,
height: u32,
cumulative_weight: u128,
cumulative_proof_target: u128,
coinbase_target: u64,
proof_target: u64,
last_coinbase_target: u64,
last_coinbase_timestamp: i64,
timestamp: i64,
) -> Result<Self> {
// Construct a new metadata.
let metadata = Self {
let metadata = Self::from_unchecked(
network,
round,
height,
Expand All @@ -79,15 +109,43 @@ impl<N: Network> Metadata<N> {
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
_phantom: PhantomData,
};
)?;

// Ensure the header is valid.
metadata.check_validity().with_context(|| "Invalid block metadata")?;

Ok(metadata)
}

/// Initializes a new metadata with the given inputs, and without performing any validity checks.
#[allow(clippy::too_many_arguments)]
pub fn from_unchecked(
network: u16,
round: u64,
height: u32,
cumulative_weight: u128,
cumulative_proof_target: u128,
coinbase_target: u64,
proof_target: u64,
last_coinbase_target: u64,
last_coinbase_timestamp: i64,
timestamp: i64,
) -> Result<Self> {
Ok(Self {
network,
round,
height,
cumulative_weight,
cumulative_proof_target,
coinbase_target,
proof_target,
last_coinbase_target,
last_coinbase_timestamp,
timestamp,
_phantom: PhantomData,
})
}

/// Returns `true` if the block metadata is well-formed.
///
/// Consider using [`Self::check_validity`] to get more information on invalid block metadata.
Expand Down
28 changes: 25 additions & 3 deletions ledger/block/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,43 @@ impl<N: Network> Header<N> {
subdag_root: Field<N>,
metadata: Metadata<N>,
) -> Result<Self> {
// Construct a new block header.
let header = Self {
let header = Self::from_unchecked(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
};
)?;

// Ensure the header is valid.
header.check_validity().with_context(|| "Invalid block header")?;
Ok(header)
}

/// Initializes a new block header with the given inputs,
/// and without performing any validity checks.
pub fn from_unchecked(
previous_state_root: N::StateRoot,
transactions_root: Field<N>,
finalize_root: Field<N>,
ratifications_root: Field<N>,
solutions_root: Field<N>,
subdag_root: Field<N>,
metadata: Metadata<N>,
) -> Result<Self> {
Ok(Self {
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
})
}

/// Returns `true` if the block header is well-formed.
///
/// Consider using [`Self::check_validity`] to get more information on invalid block headers.
Expand Down
Loading