Skip to content

Commit

Permalink
make limits a member of StacksBlockBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Coppola committed Jan 28, 2022
1 parent ad8e229 commit 4f93de2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/chainstate/stacks/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ use crate::types::chainstate::{BlockHeaderHash, StacksAddress, StacksWorkScore};
use crate::types::chainstate::{StacksBlockHeader, StacksBlockId, StacksMicroblockHeader};
use crate::types::proof::TrieHash;
use chainstate::stacks::db::blocks::SetupBlockResult;
use clarity_vm::clarity::MAX_EPOCH_SIZE;
use clarity_vm::clarity::ProductionBlockLimitFns;

#[derive(Debug, Clone)]
pub struct BlockBuilderSettings {
Expand Down Expand Up @@ -606,6 +608,7 @@ impl<'a> StacksMicroblockBuilder<'a> {
/// # Error Handling
/// - If the error when processing a tx is `CostOverflowError`, reset the cost of the block.
fn mine_next_transaction(
&self,
clarity_tx: &mut ClarityTx<'a>,
tx: StacksTransaction,
tx_len: u64,
Expand All @@ -631,7 +634,9 @@ impl<'a> StacksMicroblockBuilder<'a> {
} else {
considered.insert(tx.txid());
}
if bytes_so_far + tx_len >= MAX_EPOCH_SIZE.into() {

let max_epoch_size = self.header_reader.clarity_state.block_limits_fns.output_length_limit;
if bytes_so_far + tx_len >= max_epoch_size.into() {
info!(
"Adding microblock tx {} would exceed epoch data size",
&tx.txid()
Expand Down Expand Up @@ -707,7 +712,7 @@ impl<'a> StacksMicroblockBuilder<'a> {

let mut result = Ok(());
for (tx, tx_len) in txs_and_lens.into_iter() {
match StacksMicroblockBuilder::mine_next_transaction(
match self.mine_next_transaction(
&mut clarity_tx,
tx.clone(),
tx_len,
Expand Down Expand Up @@ -819,7 +824,7 @@ impl<'a> StacksMicroblockBuilder<'a> {
return Ok(false);
}

match StacksMicroblockBuilder::mine_next_transaction(
match self.mine_next_transaction(
clarity_tx,
mempool_tx.tx.clone(),
mempool_tx.metadata.len,
Expand Down Expand Up @@ -988,6 +993,7 @@ impl StacksBlockBuilder {
miner_privkey: StacksPrivateKey::new(), // caller should overwrite this, or refrain from mining microblocks
miner_payouts: None,
miner_id: miner_id,
block_limits_fns: ProductionBlockLimitFns(),
}
}

Expand Down Expand Up @@ -1128,7 +1134,7 @@ impl StacksBlockBuilder {
tx_len: u64,
limit_behavior: &BlockLimitFunction,
) -> TransactionResult {
if self.bytes_so_far + tx_len >= MAX_EPOCH_SIZE.into() {
if self.bytes_so_far + tx_len >= self.block_limits_fns.output_length_limit.into() {
return TransactionResult::skipped_due_to_error(&tx, Error::BlockTooBigError);
}

Expand Down
5 changes: 2 additions & 3 deletions src/chainstate/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use crate::types::chainstate::{
};
use crate::types::chainstate::{StacksBlockHeader, StacksBlockId, StacksMicroblockHeader};
use crate::types::proof::{TrieHash, TRIEHASH_ENCODED_SIZE};
use clarity_vm::clarity::BlockLimitsFunctions;

pub mod address;
pub mod auth;
Expand Down Expand Up @@ -877,11 +878,9 @@ pub struct StacksBlockBuilder {
parent_header_hash: BlockHeaderHash,
parent_microblock_hash: Option<BlockHeaderHash>,
miner_id: usize,
block_limits_fns:BlockLimitsFunctions,
}

// maximum amount of data a leader can send during its epoch (2MB)
pub const MAX_EPOCH_SIZE: u32 = 2 * 1024 * 1024;

// maximum microblock size is 64KB, but note that the current leader has a space budget of
// $MAX_EPOCH_SIZE bytes (so the average microblock size needs to be 4kb if there are 256 of them)
pub const MAX_MICROBLOCK_SIZE: u32 = 65536;
Expand Down
5 changes: 4 additions & 1 deletion src/clarity_vm/clarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ use crate::types::proof::TrieHash;
use crate::util::boot::{boot_code_acc, boot_code_addr, boot_code_id, boot_code_tx_auth};
use crate::util::db::Error as db_error;
use crate::util::secp256k1::MessageSignature;
use chainstate::stacks::MAX_EPOCH_SIZE;
use types::chainstate::BurnchainHeaderHash;

/// This struct is used to map the context at a point in time to concrete limits on block
Expand All @@ -79,6 +78,7 @@ use types::chainstate::BurnchainHeaderHash;
/// 2) override defaults with special settings to make a tool, e.g., the mempool analyzer
/// 3) override defaults with special settings for test.
/// Note: We use a simple interface that can be expanded over time if necessary.
#[derive(Clone)]
pub struct BlockLimitsFunctions {
/// The length limit of a block.
pub output_length_limit: u32,
Expand Down Expand Up @@ -286,6 +286,9 @@ impl ClarityBlockConnection<'_> {
}
}

// Maximum amount of data a leader can send during its epoch (2MB).
pub const MAX_EPOCH_SIZE: u32 = 2 * 1024 * 1024;

/// `BlockLimitsFunctions` for production. Use `MAX_EPOCH_SIZE` for length limit and get block
/// limits from `StacksEpoch`.
pub fn ProductionBlockLimitFns() -> BlockLimitsFunctions {
Expand Down

0 comments on commit 4f93de2

Please sign in to comment.