Skip to content

[wip] clarity-wasm block replay #6351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: feat/clarity-wasm-develop
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions clarity/src/vm/clarity_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8900,6 +8900,7 @@ mod error_mapping {
read_bytes_from_wasm, read_from_wasm_indirect, read_identifier_from_wasm,
signature_from_string,
};
use crate::vm::costs::CostErrors;
use crate::vm::errors::{CheckErrors, Error, RuntimeErrorType, ShortReturnType, WasmError};
use crate::vm::types::{OptionalData, ResponseData};
use crate::vm::{ClarityVersion, Value};
Expand Down Expand Up @@ -8974,6 +8975,21 @@ mod error_mapping {
/// Indicates an attempt to use a function with too many arguments
ArgumentCountAtMost = 15,

/// Indicates a runtime cost overrun
CostOverrunRuntime = 100,

/// Indicates a read count cost overrun
CostOverrunReadCount = 101,

/// Indicates a read length cost overrun
CostOverrunReadLength = 102,

/// Indicates a write count cost overrun
CostOverrunWriteCount = 103,

/// Indicates a write length cost overrun
CostOverrunWriteLength = 104,

/// A catch-all for errors that are not mapped to specific error codes.
/// This might be used for unexpected or unclassified errors.
NotMapped = 99,
Expand All @@ -8999,6 +9015,11 @@ mod error_mapping {
13 => ErrorMap::ArgumentCountMismatch,
14 => ErrorMap::ArgumentCountAtLeast,
15 => ErrorMap::ArgumentCountAtMost,
100 => ErrorMap::CostOverrunRuntime,
101 => ErrorMap::CostOverrunReadCount,
102 => ErrorMap::CostOverrunReadLength,
103 => ErrorMap::CostOverrunWriteCount,
104 => ErrorMap::CostOverrunWriteLength,
_ => ErrorMap::NotMapped,
}
}
Expand Down Expand Up @@ -9179,6 +9200,11 @@ mod error_mapping {
let (expected, got) = get_runtime_error_arg_lengths(&instance, &mut store);
Error::Unchecked(CheckErrors::RequiresAtMostArguments(expected, got))
}
ErrorMap::CostOverrunRuntime => Error::from(CostErrors::CostOverflow),
ErrorMap::CostOverrunReadCount => Error::from(CostErrors::CostOverflow),
ErrorMap::CostOverrunReadLength => Error::from(CostErrors::CostOverflow),
ErrorMap::CostOverrunWriteCount => Error::from(CostErrors::CostOverflow),
ErrorMap::CostOverrunWriteLength => Error::from(CostErrors::CostOverflow),
_ => panic!("Runtime error code {} not supported", runtime_error_code),
}
}
Expand Down
32 changes: 11 additions & 21 deletions stackslib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,14 +827,8 @@ fn replay_block(
block_am.weight(),
true,
) {
Ok((receipt, _, _)) => {
if receipt.anchored_block_cost != cost {
println!("Failed processing block! block = {block_id}. Unexpected cost. expected = {cost}, evaluated = {}",
receipt.anchored_block_cost);
process::exit(1);
}

info!("Block processed successfully! block = {block_id}");
Ok((_, _, _)) => {
info!("Cost check skipped. Block processed successfully! block = {block_id}");
}
Err(e) => {
println!("Failed processing block! block = {block_id}, error = {e:?}");
Expand Down Expand Up @@ -869,14 +863,19 @@ fn replay_naka_staging_block(db_path: &str, index_block_hash_hex: &str, conf: &C
None,
true,
)
.unwrap();
.unwrap_or_else(|err| {
eprintln!("Error: {:?}", err);
panic!("SortitionDB::connect failed");
});

let (block, block_size) = chainstate
.nakamoto_blocks_db()
.get_nakamoto_block(&block_id)
.unwrap()
.unwrap();
replay_block_nakamoto(&mut sortdb, &mut chainstate, &block, block_size).unwrap();
.unwrap_or_else(|e| panic!("DB error fetching {:?}: {:?}", block_id, e))
.unwrap_or_else(|| panic!("No block found for id {:?}", block_id));

replay_block_nakamoto(&mut sortdb, &mut chainstate, &block, block_size)
.unwrap_or_else(|e| panic!("ERROR replaying block: {:?}", e));
}

fn replay_block_nakamoto(
Expand Down Expand Up @@ -1143,15 +1142,6 @@ fn replay_block_nakamoto(
Err(e) => (None, Some(e)),
};

if let Some(receipt) = ok_opt {
// check the cost
let evaluated_cost = receipt.anchored_block_cost.clone();
if evaluated_cost != expected_cost {
println!("Failed processing block! block = {block_id}. Unexpected cost. expected = {expected_cost}, evaluated = {evaluated_cost}");
process::exit(1);
}
}

if let Some(e) = err_opt {
// force rollback
drop(chainstate_tx);
Expand Down
Loading