From 37fcaac4ae5e745be0e02863215e66723c236045 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Thu, 6 Jan 2022 08:06:21 -0600 Subject: [PATCH] feat: update pessimistic cost estimator to differentiate by contract sender --- CHANGELOG.md | 7 ++ src/cost_estimates/pessimistic.rs | 4 +- src/cost_estimates/tests/cost_estimators.rs | 98 +++++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed63bec8c9..41925503b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to the versioning scheme outlined in the [README.md](README.md). +## [Unreleased] + +### Changed + +- The pessimistic execution cost estimator differentiates between + contracts with different origin addresses. + ## [2.05.0.0.0] This software update is a consensus changing release and the diff --git a/src/cost_estimates/pessimistic.rs b/src/cost_estimates/pessimistic.rs index 3467ceef3d..dd94e5a34f 100644 --- a/src/cost_estimates/pessimistic.rs +++ b/src/cost_estimates/pessimistic.rs @@ -232,8 +232,8 @@ impl PessimisticEstimator { StacksEpochId::Epoch2_05 => ":2.05", }; format!( - "cc{}:{}.{}", - epoch_marker, cc.contract_name, cc.function_name + "cc{}:{}:{}.{}", + epoch_marker, cc.address, cc.contract_name, cc.function_name ) } TransactionPayload::SmartContract(_sc) => "contract-publish".to_string(), diff --git a/src/cost_estimates/tests/cost_estimators.rs b/src/cost_estimates/tests/cost_estimators.rs index d6e7203cd3..3bebce117e 100644 --- a/src/cost_estimates/tests/cost_estimators.rs +++ b/src/cost_estimates/tests/cost_estimators.rs @@ -282,6 +282,104 @@ fn test_pessimistic_cost_estimator_declining_average() { ); } +#[test] +/// This tests the PessimisticEstimator as a unit (i.e., separate +/// from the trait auto-impl method) by providing payload inputs +/// to produce the expected pessimistic result (i.e., mean over a 10-sample +/// window, where the window only updates if the new entry would make a dimension +/// worse). +fn pessimistic_estimator_contract_owner_separation() { + let mut estimator = instantiate_test_db(); + let cc_payload_0 = TransactionPayload::ContractCall(TransactionContractCall { + address: StacksAddress::new(0, Hash160([0; 20])), + contract_name: "contract-1".into(), + function_name: "func1".into(), + function_args: vec![], + }); + let cc_payload_1 = TransactionPayload::ContractCall(TransactionContractCall { + address: StacksAddress::new(0, Hash160([1; 20])), + contract_name: "contract-1".into(), + function_name: "func1".into(), + function_args: vec![], + }); + + estimator + .notify_event( + &cc_payload_0, + &ExecutionCost { + write_length: 1, + write_count: 1, + read_length: 1, + read_count: 1, + runtime: 1, + }, + &BLOCK_LIMIT_MAINNET_20, + &StacksEpochId::Epoch20, + ) + .expect("Should be able to process event"); + + assert_eq!( + estimator.estimate_cost(&cc_payload_1, &StacksEpochId::Epoch20,), + Err(EstimatorError::NoEstimateAvailable) + ); + + assert_eq!( + estimator + .estimate_cost(&cc_payload_0, &StacksEpochId::Epoch20,) + .expect("Should be able to provide cost estimate now"), + ExecutionCost { + write_length: 1, + write_count: 1, + read_length: 1, + read_count: 1, + runtime: 1, + } + ); + + estimator + .notify_event( + &cc_payload_1, + &ExecutionCost { + write_length: 5, + write_count: 5, + read_length: 5, + read_count: 5, + runtime: 5, + }, + &BLOCK_LIMIT_MAINNET_20, + &StacksEpochId::Epoch20, + ) + .expect("Should be able to process event"); + + // cc_payload_0 should not be affected + assert_eq!( + estimator + .estimate_cost(&cc_payload_0, &StacksEpochId::Epoch20,) + .expect("Should be able to provide cost estimate now"), + ExecutionCost { + write_length: 1, + write_count: 1, + read_length: 1, + read_count: 1, + runtime: 1, + } + ); + + // cc_payload_1 should be updated + assert_eq!( + estimator + .estimate_cost(&cc_payload_1, &StacksEpochId::Epoch20,) + .expect("Should be able to provide cost estimate now"), + ExecutionCost { + write_length: 5, + write_count: 5, + read_length: 5, + read_count: 5, + runtime: 5, + } + ); +} + #[test] /// This tests the PessimisticEstimator as a unit (i.e., separate /// from the trait auto-impl method) by providing payload inputs