use super::config::DEFAULT_TEST_BLOCK_SIZE;
use crate::BlockId;
use crate::blocks::{
Block, BlockMetadata,
state::{Registered, Reset, Staged},
};
use crate::pools::SequenceHash;
use crate::registry::BlockRegistry;
use super::token_blocks::create_test_token_block;
pub(crate) fn create_staged_block<T: BlockMetadata + std::fmt::Debug>(
id: BlockId,
tokens: &[u32],
) -> Block<T, Staged> {
let token_block = create_test_token_block(tokens, tokens.len() as u32);
let block: Block<T, Reset> = Block::new(id, tokens.len());
block.complete(&token_block).expect("Should complete")
}
pub(crate) fn create_registered_block<T: BlockMetadata + std::fmt::Debug>(
id: BlockId,
tokens: &[u32],
) -> (Block<T, Registered>, SequenceHash) {
let staged = create_staged_block::<T>(id, tokens);
let seq_hash = staged.sequence_hash();
let registry = BlockRegistry::new();
let handle = registry.register_sequence_hash(seq_hash);
(staged.register_with_handle(handle), seq_hash)
}
pub(crate) fn create_reset_block<T: BlockMetadata>(
id: BlockId,
block_size: usize,
) -> Block<T, Reset> {
Block::new(id, block_size)
}
pub(crate) fn create_reset_blocks<T: BlockMetadata>(
count: usize,
block_size: usize,
) -> Vec<Block<T, Reset>> {
(0..count as BlockId)
.map(|id| Block::new(id, block_size))
.collect()
}
pub(crate) struct TestBlockBuilder<T: BlockMetadata> {
id: BlockId,
block_size: usize,
tokens: Option<Vec<u32>>,
_phantom: std::marker::PhantomData<T>,
}
impl<T: BlockMetadata> TestBlockBuilder<T> {
pub(crate) fn new(id: BlockId) -> Self {
Self {
id,
block_size: DEFAULT_TEST_BLOCK_SIZE,
tokens: None,
_phantom: std::marker::PhantomData,
}
}
pub(crate) fn with_block_size(mut self, size: usize) -> Self {
use super::config::validate_test_block_size;
assert!(
validate_test_block_size(size),
"Invalid test block size: {}. Must be power of 2 between 1 and 1024",
size
);
self.block_size = size;
self
}
pub(crate) fn with_tokens(mut self, tokens: Vec<u32>) -> Self {
self.tokens = Some(tokens);
self
}
pub(crate) fn fill_iota(mut self, start: u32) -> Self {
let tokens: Vec<u32> = (start..start + self.block_size as u32).collect();
self.tokens = Some(tokens);
self
}
pub(crate) fn build_staged(self) -> Block<T, Staged>
where
T: std::fmt::Debug,
{
use super::config::generate_test_tokens;
let tokens = self
.tokens
.unwrap_or_else(|| generate_test_tokens(self.id as u32 * 100, self.block_size));
assert_eq!(
tokens.len(),
self.block_size,
"Token count {} doesn't match block size {}",
tokens.len(),
self.block_size
);
let token_block = create_test_token_block(&tokens, self.block_size as u32);
Block::new(self.id, self.block_size)
.complete(&token_block)
.expect("Block size should match token block size")
}
pub(crate) fn build_registered_with_registry(
self,
registry: &BlockRegistry,
) -> (Block<T, Registered>, SequenceHash)
where
T: std::fmt::Debug,
{
let staged = self.build_staged();
let seq_hash = staged.sequence_hash();
let handle = registry.register_sequence_hash(seq_hash);
(staged.register_with_handle(handle), seq_hash)
}
}