Skip to content

Commit

Permalink
fix: Fix metering test assertions and bottlenose costings
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Aug 21, 2024
1 parent 58e0ef1 commit b5a1a80
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Total Cost (XRD) , 0.63457102592, 100.0%
- Execution Cost (XRD) , 0.4741398, 74.7%
- Finalization Cost (XRD) , 0.0322574, 5.1%
- Storage Cost (XRD) , 0.12817382592, 20.4%
- Storage Cost (XRD) , 0.12817382592, 20.2%
- Tipping Cost (XRD) , 0, 0.0%
- Royalty Cost (XRD) , 0, 0.0%
Execution Cost Breakdown , 9482796, 100.0%
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Total Cost (XRD) , 0.57356037273, 100.0%
- Execution Cost (XRD) , 0.3676254, 64.1%
- Finalization Cost (XRD) , 0.0427613, 7.5%
- Storage Cost (XRD) , 0.16317367273, 28.4%
Total Cost (XRD) , 0.57956227273, 100.0%
- Execution Cost (XRD) , 0.3736273, 64.5%
- Finalization Cost (XRD) , 0.0427613, 7.4%
- Storage Cost (XRD) , 0.16317367273, 28.2%
- Tipping Cost (XRD) , 0, 0.0%
- Royalty Cost (XRD) , 0, 0.0%
Execution Cost Breakdown , 7472546, 100.0%
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Total Cost (XRD) , 0.22201924184, 100.0%
- Execution Cost (XRD) , 0.145153, 65.4%
- Finalization Cost (XRD) , 0.01125345, 5.1%
- Storage Cost (XRD) , 0.06561279184, 29.6%
Total Cost (XRD) , 0.22602034184, 100.0%
- Execution Cost (XRD) , 0.1491541, 66.0%
- Finalization Cost (XRD) , 0.01125345, 5.0%
- Storage Cost (XRD) , 0.06561279184, 29.0%
- Tipping Cost (XRD) , 0, 0.0%
- Royalty Cost (XRD) , 0, 0.0%
Execution Cost Breakdown , 2983082, 100.0%
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Total Cost (XRD) , 0.42291612909, 100.0%
- Execution Cost (XRD) , 0.20327225, 48.1%
- Finalization Cost (XRD) , 0.0515111, 12.2%
- Storage Cost (XRD) , 0.16813277909, 39.9%
- Storage Cost (XRD) , 0.16813277909, 39.8%
- Tipping Cost (XRD) , 0, 0.0%
- Royalty Cost (XRD) , 0, 0.0%
Execution Cost Breakdown , 4065445, 100.0%
Expand Down
9 changes: 5 additions & 4 deletions radix-engine-tests/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ pub mod path_macros {
};
}

// Not a macro, because it needs to support a variable folder, but here
// for consistency
pub fn path_local_metering_assets(folder: &str) -> String {
format!("{}/assets/metering/{}", env!("CARGO_MANIFEST_DIR"), folder)
#[macro_export]
macro_rules! path_local_metering_receipts {
() => {
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/metering/")
};
}

pub use crate::include_local_wasm_str;
Expand Down
11 changes: 6 additions & 5 deletions radix-engine-tests/tests/application/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ fn run_cost_tests() {

#[cfg(not(feature = "alloc"))]
fn run_all(mode: CostingTaskMode) {
for protocol_version in ProtocolVersion::VARIANTS.into_iter() {
let folder = path_local_metering_assets(protocol_version.logical_name());
use radix_engine_tests::path_local_metering_receipts;

let folder = folder.as_str();
for protocol_version in ProtocolVersion::VARIANTS.into_iter() {
let base_path = path_local_metering_receipts!();
let execute = move |run: &dyn Fn(DefaultLedgerSimulator) -> TransactionReceipt,
file: &'static str| {
let ledger = LedgerSimulatorBuilder::new()
.with_custom_protocol(|builder| builder.from_bootstrap_to(protocol_version))
.without_kernel_trace()
.build();
let receipt = run(ledger);
let relative_file_path = format!("{}/{}", protocol_version.logical_name(), file);
mode.run(
folder,
file,
&base_path,
&relative_file_path,
&receipt.fee_summary,
&receipt.fee_details.unwrap(),
)
Expand Down
50 changes: 20 additions & 30 deletions radix-engine/src/utils/costing_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,40 @@ pub enum CostingTaskMode {
impl CostingTaskMode {
pub fn run(
&self,
folder: impl Into<PathBuf>,
file: &str,
base_path: impl Into<PathBuf>,
relative_file_path: &str,
fee_summary: &TransactionFeeSummary,
fee_details: &TransactionFeeDetails,
) {
match self {
CostingTaskMode::OutputCosting => {
write_cost_breakdown(fee_summary, fee_details, folder, file);
write_cost_breakdown(fee_summary, fee_details, base_path, relative_file_path);
}
CostingTaskMode::AssertCosting => {
let expected = load_cost_breakdown(folder, file);
assert_eq!(&fee_details.execution_cost_breakdown, &expected.0);
assert_eq!(&fee_details.finalization_cost_breakdown, &expected.1);
verify_cost_breakdown(fee_summary, fee_details, base_path, relative_file_path)
.unwrap();
}
}
}
}

fn load_cost_breakdown(
fn verify_cost_breakdown(
fee_summary: &TransactionFeeSummary,
fee_details: &TransactionFeeDetails,
folder: impl Into<PathBuf>,
file: &str,
) -> (BTreeMap<String, u32>, BTreeMap<String, u32>) {
let path = folder.into().join(file);
let content = std::fs::read_to_string(&path).unwrap();
let mut execution_breakdown = BTreeMap::<String, u32>::new();
let mut finalization_breakdown = BTreeMap::<String, u32>::new();
let lines: Vec<String> = content.split('\n').map(String::from).collect();
let mut is_execution = true;
for i in 7..lines.len() {
if lines[i].starts_with('-') {
let mut tokens = lines[i].split(',');
let entry = tokens.next().unwrap().trim()[2..].to_string();
let cost = tokens.next().unwrap().trim();
if is_execution {
&mut execution_breakdown
} else {
&mut finalization_breakdown
}
.insert(entry, u32::from_str(cost).unwrap());
} else {
is_execution = false;
}
relative_file_path: &str,
) -> Result<(), String> {
let path = folder.into().join(relative_file_path);
let content = std::fs::read_to_string(&path)
.map_err(|err| format!("Costing breakdown read error ({err:?}): {relative_file_path}"))?;
let expected = format_cost_breakdown(fee_summary, fee_details);
if content != expected {
// We don't use an assert_eq here so that it doesn't dump massive text on failure
return Err(format!(
"Costing breakdown needs updating: {relative_file_path}"
));
}
(execution_breakdown, finalization_breakdown)
Ok(())
}

pub fn write_cost_breakdown(
Expand Down

0 comments on commit b5a1a80

Please sign in to comment.