Skip to content

Commit

Permalink
constrain random builder to test transactions only (#2933)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron authored Apr 12, 2024
1 parent a3b61b4 commit 1f397e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
17 changes: 1 addition & 16 deletions crates/example-types/src/block_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,7 @@ impl Committable for TestTransaction {
}
}

impl Transaction for TestTransaction {
/// Create a transaction from bytes
fn from_bytes(bytes: &[u8]) -> Self {
Self(bytes.to_vec())
}

/// Get the length of the transaction in bytes
fn len(&self) -> usize {
self.0.len()
}

/// Returns whether or not the transaction is empty
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Transaction for TestTransaction {}

/// A [`BlockPayload`] that contains a list of `TestTransaction`.
#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
Expand Down
23 changes: 17 additions & 6 deletions crates/testing/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ use hotshot_builder_api::{
builder::{BuildError, Error, Options},
data_source::BuilderDataSource,
};
use hotshot_example_types::block_types::TestTransaction;
use hotshot_types::{
constants::{Version01, STATIC_VER_0_1},
traits::{
block_contents::{vid_commitment, BlockHeader, Transaction},
block_contents::{vid_commitment, BlockHeader},
election::Membership,
node_implementation::NodeType,
signature_key::BuilderSignatureKey,
Expand Down Expand Up @@ -52,6 +53,7 @@ where
for<'a> <<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType as TryFrom<
&'a TaggedBase64,
>>::Error: Display,
TYPES: NodeType<Transaction = TestTransaction>,
for<'a> <TYPES::SignatureKey as TryFrom<&'a TaggedBase64>>::Error: Display,
{
async fn start(
Expand Down Expand Up @@ -145,7 +147,10 @@ pub struct RandomBuilderSource<TYPES: NodeType> {
priv_key: <TYPES::BuilderSignatureKey as BuilderSignatureKey>::BuilderPrivateKey,
}

impl<TYPES: NodeType> RandomBuilderSource<TYPES> {
impl<TYPES: NodeType> RandomBuilderSource<TYPES>
where
TYPES: NodeType<Transaction = TestTransaction>,
{
/// Create new [`RandomBuilderSource`]
#[must_use]
#[allow(clippy::missing_panics_doc)] // ony panics if 256 == 0
Expand All @@ -170,7 +175,7 @@ impl<TYPES: NodeType> RandomBuilderSource<TYPES> {
let time_per_block = Duration::from_secs(1) / options.blocks_per_second;
loop {
let start = std::time::Instant::now();
let transactions: Vec<TYPES::Transaction> = (0..options.txn_in_block)
let transactions: Vec<TestTransaction> = (0..options.txn_in_block)
.map(|_| {
let mut bytes = vec![
0;
Expand All @@ -179,7 +184,7 @@ impl<TYPES: NodeType> RandomBuilderSource<TYPES> {
.expect("We are NOT running on a 16-bit platform")
];
rng.fill_bytes(&mut bytes);
TYPES::Transaction::from_bytes(&bytes)
TestTransaction(bytes)
})
.collect();

Expand Down Expand Up @@ -282,6 +287,7 @@ where
for<'a> <<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType as TryFrom<
&'a TaggedBase64,
>>::Error: Display,
TYPES: NodeType<Transaction = TestTransaction>,
for<'a> <TYPES::SignatureKey as TryFrom<&'a TaggedBase64>>::Error: Display,
{
let (pub_key, priv_key) = TYPES::BuilderSignatureKey::generated_from_seed_indexed([1; 32], 0);
Expand Down Expand Up @@ -496,8 +502,6 @@ fn build_block<TYPES: NodeType>(
AvailableBlockData<TYPES>,
AvailableBlockHeaderInput<TYPES>,
) {
let block_size = transactions.iter().map(|t| t.len() as u64).sum::<u64>();

let (block_payload, metadata) = TYPES::BlockPayload::from_transactions(transactions)
.expect("failed to build block payload from transactions");

Expand All @@ -508,6 +512,13 @@ fn build_block<TYPES: NodeType>(
num_storage_nodes,
);

// Get block size from the encoded payload
let block_size = block_payload
.encode()
.expect("failed to encode block")
.collect::<Vec<u8>>()
.len() as u64;

let signature_over_block_info = {
let mut block_info: Vec<u8> = Vec::new();
block_info.extend_from_slice(block_size.to_be_bytes().as_ref());
Expand Down
8 changes: 0 additions & 8 deletions crates/types/src/traits/block_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ use crate::{
pub trait Transaction:
Clone + Serialize + DeserializeOwned + Debug + PartialEq + Eq + Sync + Send + Committable + Hash
{
/// Build a transaction from bytes
fn from_bytes(bytes: &[u8]) -> Self;

/// Get the length of the transaction
fn len(&self) -> usize;

/// Whether or not the transaction is empty
fn is_empty(&self) -> bool;
}

/// Abstraction over the full contents of a block
Expand Down

0 comments on commit 1f397e7

Please sign in to comment.