Skip to content

Commit ac6575c

Browse files
author
Aaron Blankstein
authored
Merge pull request #2940 from blockstack/feat/test-mainnet
testing: Unit tests for cost contracts on mainnet
2 parents d5ddfdf + b8430f9 commit ac6575c

File tree

8 files changed

+485
-200
lines changed

8 files changed

+485
-200
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/clarity_vm/clarity.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,18 @@ impl ClarityInstance {
385385
header_db,
386386
burn_state_db,
387387
cost_track,
388-
mainnet: false,
388+
mainnet: self.mainnet,
389389
epoch,
390390
};
391391

392+
let use_mainnet = self.mainnet;
392393
conn.as_transaction(|clarity_db| {
393394
let (ast, _) = clarity_db
394-
.analyze_smart_contract(&boot_code_id("costs", false), BOOT_CODE_COSTS)
395+
.analyze_smart_contract(&boot_code_id("costs", use_mainnet), BOOT_CODE_COSTS)
395396
.unwrap();
396397
clarity_db
397398
.initialize_smart_contract(
398-
&boot_code_id("costs", false),
399+
&boot_code_id("costs", use_mainnet),
399400
&ast,
400401
BOOT_CODE_COSTS,
401402
|_, _| false,
@@ -406,31 +407,31 @@ impl ClarityInstance {
406407
conn.as_transaction(|clarity_db| {
407408
let (ast, analysis) = clarity_db
408409
.analyze_smart_contract(
409-
&boot_code_id("cost-voting", false),
410+
&boot_code_id("cost-voting", use_mainnet),
410411
&*BOOT_CODE_COST_VOTING,
411412
)
412413
.unwrap();
413414
clarity_db
414415
.initialize_smart_contract(
415-
&boot_code_id("cost-voting", false),
416+
&boot_code_id("cost-voting", use_mainnet),
416417
&ast,
417418
&*BOOT_CODE_COST_VOTING,
418419
|_, _| false,
419420
)
420421
.unwrap();
421422

422423
clarity_db
423-
.save_analysis(&boot_code_id("cost-voting", false), &analysis)
424+
.save_analysis(&boot_code_id("cost-voting", use_mainnet), &analysis)
424425
.unwrap();
425426
});
426427

427428
conn.as_transaction(|clarity_db| {
428429
let (ast, _) = clarity_db
429-
.analyze_smart_contract(&boot_code_id("pox", false), &*BOOT_CODE_POX_TESTNET)
430+
.analyze_smart_contract(&boot_code_id("pox", use_mainnet), &*BOOT_CODE_POX_TESTNET)
430431
.unwrap();
431432
clarity_db
432433
.initialize_smart_contract(
433-
&boot_code_id("pox", false),
434+
&boot_code_id("pox", use_mainnet),
434435
&ast,
435436
&*BOOT_CODE_POX_TESTNET,
436437
|_, _| false,

src/vm/analysis/tests/costs.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ use crate::core::StacksEpochId;
3939
use crate::types::chainstate::{BlockHeaderHash, StacksBlockId};
4040
use crate::types::proof::ClarityMarfTrieId;
4141

42-
pub fn test_tracked_costs(prog: &str, epoch: StacksEpochId) -> ExecutionCost {
42+
pub fn test_tracked_costs(prog: &str, use_mainnet: bool, epoch: StacksEpochId) -> ExecutionCost {
4343
let marf = MarfedKV::temporary();
44-
let mut clarity_instance = ClarityInstance::new(false, marf);
44+
let mut clarity_instance = ClarityInstance::new(use_mainnet, marf);
4545

4646
let p1 = execute("'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR");
4747

@@ -173,24 +173,42 @@ pub fn test_tracked_costs(prog: &str, epoch: StacksEpochId) -> ExecutionCost {
173173
}
174174
}
175175

176-
#[test]
177-
fn test_all() {
178-
let baseline = test_tracked_costs("1", StacksEpochId::Epoch20);
176+
fn test_all(use_mainnet: bool) {
177+
let baseline = test_tracked_costs("1", use_mainnet, StacksEpochId::Epoch20);
179178

180179
for f in NativeFunctions::ALL.iter() {
181180
let test = get_simple_test(f);
182-
let cost = test_tracked_costs(test, StacksEpochId::Epoch20);
181+
let cost = test_tracked_costs(test, use_mainnet, StacksEpochId::Epoch20);
183182
assert!(cost.exceeds(&baseline));
184183
}
185184
}
186185

187186
#[test]
188-
fn epoch_205_test_all() {
189-
let baseline = test_tracked_costs("1", StacksEpochId::Epoch2_05);
187+
fn test_all_mainnet() {
188+
test_all(true)
189+
}
190+
191+
#[test]
192+
fn test_all_testnet() {
193+
test_all(false)
194+
}
195+
196+
fn epoch_205_test_all(use_mainnet: bool) {
197+
let baseline = test_tracked_costs("1", use_mainnet, StacksEpochId::Epoch2_05);
190198

191199
for f in NativeFunctions::ALL.iter() {
192200
let test = get_simple_test(f);
193-
let cost = test_tracked_costs(test, StacksEpochId::Epoch2_05);
201+
let cost = test_tracked_costs(test, use_mainnet, StacksEpochId::Epoch2_05);
194202
assert!(cost.exceeds(&baseline));
195203
}
196204
}
205+
206+
#[test]
207+
fn epoch_205_test_all_mainnet() {
208+
epoch_205_test_all(true)
209+
}
210+
211+
#[test]
212+
fn epoch_205_test_all_testnet() {
213+
epoch_205_test_all(false)
214+
}

src/vm/contexts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,12 @@ impl<'a> OwnedEnvironment<'a> {
544544
pub fn new_max_limit(
545545
mut database: ClarityDatabase<'a>,
546546
epoch: StacksEpochId,
547+
use_mainnet: bool,
547548
) -> OwnedEnvironment<'a> {
548-
let cost_track = LimitedCostTracker::new_max_limit(&mut database, epoch)
549+
let cost_track = LimitedCostTracker::new_max_limit(&mut database, epoch, use_mainnet)
549550
.expect("FAIL: problem instantiating cost tracking");
550551
OwnedEnvironment {
551-
context: GlobalContext::new(false, database, cost_track, epoch),
552+
context: GlobalContext::new(use_mainnet, database, cost_track, epoch),
552553
default_contract: ContractContext::new(QualifiedContractIdentifier::transient()),
553554
call_stack: CallStack::new(),
554555
}

0 commit comments

Comments
 (0)