Skip to content

Commit

Permalink
feat: update tx estimator when tx yields too big tx error
Browse files Browse the repository at this point in the history
  • Loading branch information
kantai committed Oct 22, 2024
1 parent 50c723a commit a7432bb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
11 changes: 10 additions & 1 deletion stackslib/src/chainstate/nakamoto/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,19 @@ impl BlockBuilder for NakamotoBlockBuilder {
tx.txid(),
100 - TX_BLOCK_LIMIT_PROPORTION_HEURISTIC,
&total_budget
);
let mut measured_cost = cost_after;
let measured_cost = if measured_cost.sub(&cost_before).is_ok() {
Some(measured_cost)
} else {
warn!(
"Failed to compute measured cost of a too big transaction"
);
None
};
return TransactionResult::error(
&tx,
Error::TransactionTooBigError,
Error::TransactionTooBigError(measured_cost),
);
} else {
warn!(
Expand Down
68 changes: 63 additions & 5 deletions stackslib/src/chainstate/stacks/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,18 @@ impl<'a> StacksMicroblockBuilder<'a> {
100 - TX_BLOCK_LIMIT_PROPORTION_HEURISTIC,
&total_budget
);
let mut measured_cost = cost_after.clone();
let measured_cost = if measured_cost.sub(cost_before).is_ok() {
Some(measured_cost)
} else {
warn!(
"Failed to compute measured cost of a too big transaction"
);
None
};
return Ok(TransactionResult::error(
&tx,
Error::TransactionTooBigError,
Error::TransactionTooBigError(measured_cost),
));
} else {
warn!(
Expand Down Expand Up @@ -1323,7 +1332,22 @@ impl<'a> StacksMicroblockBuilder<'a> {
return Ok(None);
}
}
Error::TransactionTooBigError => {
Error::TransactionTooBigError(measured_cost) => {
if update_estimator {
if let Some(measured_cost) = measured_cost {
if let Err(e) = estimator.notify_event(
&mempool_tx.tx.payload,
&measured_cost,
&block_limit,
&stacks_epoch_id,
) {
warn!("Error updating estimator";
"txid" => %mempool_tx.metadata.txid,
"error" => ?e);
}
}
}

invalidated_txs.push(mempool_tx.metadata.txid);
}
_ => {}
Expand Down Expand Up @@ -2405,7 +2429,22 @@ impl StacksBlockBuilder {
return Ok(None);
}
}
Error::TransactionTooBigError => {
Error::TransactionTooBigError(measured_cost) => {
if update_estimator {
if let Some(measured_cost) = measured_cost {
if let Err(e) = estimator.notify_event(
&txinfo.tx.payload,
&measured_cost,
&block_limit,
&stacks_epoch_id,
) {
warn!("Error updating estimator";
"txid" => %txinfo.metadata.txid,
"error" => ?e);
}
}
}

invalidated_txs.push(txinfo.metadata.txid);
}
Error::InvalidStacksTransaction(_, true) => {
Expand Down Expand Up @@ -2714,9 +2753,18 @@ impl BlockBuilder for StacksBlockBuilder {
100 - TX_BLOCK_LIMIT_PROPORTION_HEURISTIC,
&total_budget
);
let mut measured_cost = cost_after;
let measured_cost = if measured_cost.sub(&cost_before).is_ok() {
Some(measured_cost)
} else {
warn!(
"Failed to compute measured cost of a too big transaction"
);
None
};
return TransactionResult::error(
&tx,
Error::TransactionTooBigError,
Error::TransactionTooBigError(measured_cost),
);
} else {
warn!(
Expand Down Expand Up @@ -2795,9 +2843,19 @@ impl BlockBuilder for StacksBlockBuilder {
100 - TX_BLOCK_LIMIT_PROPORTION_HEURISTIC,
&total_budget
);
let mut measured_cost = cost_after;
let measured_cost = if measured_cost.sub(&cost_before).is_ok() {
Some(measured_cost)
} else {
warn!(
"Failed to compute measured cost of a too big transaction"
);
None
};

return TransactionResult::error(
&tx,
Error::TransactionTooBigError,
Error::TransactionTooBigError(measured_cost),
);
} else {
warn!(
Expand Down
10 changes: 6 additions & 4 deletions stackslib/src/chainstate/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum Error {
NotInSameFork,
InvalidChainstateDB,
BlockTooBigError,
TransactionTooBigError,
TransactionTooBigError(Option<ExecutionCost>),
BlockCostExceeded,
NoTransactionsToMine,
MicroblockStreamTooLongError,
Expand Down Expand Up @@ -168,7 +168,9 @@ impl fmt::Display for Error {
Error::NoSuchBlockError => write!(f, "No such Stacks block"),
Error::InvalidChainstateDB => write!(f, "Invalid chainstate database"),
Error::BlockTooBigError => write!(f, "Too much data in block"),
Error::TransactionTooBigError => write!(f, "Too much data in transaction"),
Error::TransactionTooBigError(ref c) => {
write!(f, "Too much data in transaction: measured_cost={c:?}")
}
Error::BlockCostExceeded => write!(f, "Block execution budget exceeded"),
Error::MicroblockStreamTooLongError => write!(f, "Too many microblocks in stream"),
Error::IncompatibleSpendingConditionError => {
Expand Down Expand Up @@ -246,7 +248,7 @@ impl error::Error for Error {
Error::NoSuchBlockError => None,
Error::InvalidChainstateDB => None,
Error::BlockTooBigError => None,
Error::TransactionTooBigError => None,
Error::TransactionTooBigError(..) => None,
Error::BlockCostExceeded => None,
Error::MicroblockStreamTooLongError => None,
Error::IncompatibleSpendingConditionError => None,
Expand Down Expand Up @@ -291,7 +293,7 @@ impl Error {
Error::NoSuchBlockError => "NoSuchBlockError",
Error::InvalidChainstateDB => "InvalidChainstateDB",
Error::BlockTooBigError => "BlockTooBigError",
Error::TransactionTooBigError => "TransactionTooBigError",
Error::TransactionTooBigError(..) => "TransactionTooBigError",
Error::BlockCostExceeded => "BlockCostExceeded",
Error::MicroblockStreamTooLongError => "MicroblockStreamTooLongError",
Error::IncompatibleSpendingConditionError => "IncompatibleSpendingConditionError",
Expand Down

0 comments on commit a7432bb

Please sign in to comment.