Skip to content

Commit ae2d105

Browse files
authored
Merge pull request #6475 from jferrant/chore/aac-rename-alias-to-costresult
Rename ambiguous Result alias to CostResult
2 parents 47c61e9 + 3ba766c commit ae2d105

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

clarity/src/vm/costs/mod.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ pub mod costs_3;
5555
#[allow(unused_variables)]
5656
pub mod costs_4;
5757

58-
type Result<T> = std::result::Result<T, CostErrors>;
59-
6058
pub const CLARITY_MEMORY_LIMIT: u64 = 100 * 1000 * 1000;
6159

6260
// TODO: factor out into a boot lib?
@@ -85,7 +83,7 @@ pub fn runtime_cost<T: TryInto<u64>, C: CostTracker>(
8583
cost_function: ClarityCostFunction,
8684
tracker: &mut C,
8785
input: T,
88-
) -> Result<()> {
86+
) -> Result<(), CostErrors> {
8987
let size: u64 = input.try_into().map_err(|_| CostErrors::CostOverflow)?;
9088
let cost = tracker.compute_cost(cost_function, &[size])?;
9189

@@ -104,7 +102,7 @@ pub fn analysis_typecheck_cost<T: CostTracker>(
104102
track: &mut T,
105103
t1: &TypeSignature,
106104
t2: &TypeSignature,
107-
) -> Result<()> {
105+
) -> Result<(), CostErrors> {
108106
let t1_size = t1.type_size().map_err(|_| CostErrors::CostOverflow)?;
109107
let t2_size = t2.type_size().map_err(|_| CostErrors::CostOverflow)?;
110108
let cost = track.compute_cost(
@@ -115,11 +113,11 @@ pub fn analysis_typecheck_cost<T: CostTracker>(
115113
}
116114

117115
pub trait MemoryConsumer {
118-
fn get_memory_use(&self) -> Result<u64>;
116+
fn get_memory_use(&self) -> Result<u64, CostErrors>;
119117
}
120118

121119
impl MemoryConsumer for Value {
122-
fn get_memory_use(&self) -> Result<u64> {
120+
fn get_memory_use(&self) -> Result<u64, CostErrors> {
123121
Ok(self
124122
.size()
125123
.map_err(|_| CostErrors::InterpreterFailure)?
@@ -132,10 +130,10 @@ pub trait CostTracker {
132130
&mut self,
133131
cost_function: ClarityCostFunction,
134132
input: &[u64],
135-
) -> Result<ExecutionCost>;
136-
fn add_cost(&mut self, cost: ExecutionCost) -> Result<()>;
137-
fn add_memory(&mut self, memory: u64) -> Result<()>;
138-
fn drop_memory(&mut self, memory: u64) -> Result<()>;
133+
) -> Result<ExecutionCost, CostErrors>;
134+
fn add_cost(&mut self, cost: ExecutionCost) -> Result<(), CostErrors>;
135+
fn add_memory(&mut self, memory: u64) -> Result<(), CostErrors>;
136+
fn drop_memory(&mut self, memory: u64) -> Result<(), CostErrors>;
139137
fn reset_memory(&mut self);
140138
/// Check if the given contract-call should be short-circuited.
141139
/// If so: this charges the cost to the CostTracker, and return true
@@ -145,7 +143,7 @@ pub trait CostTracker {
145143
contract: &QualifiedContractIdentifier,
146144
function: &ClarityName,
147145
input: &[u64],
148-
) -> Result<bool>;
146+
) -> Result<bool, CostErrors>;
149147
}
150148

151149
// Don't track!
@@ -154,16 +152,16 @@ impl CostTracker for () {
154152
&mut self,
155153
_cost_function: ClarityCostFunction,
156154
_input: &[u64],
157-
) -> std::result::Result<ExecutionCost, CostErrors> {
155+
) -> Result<ExecutionCost, CostErrors> {
158156
Ok(ExecutionCost::ZERO)
159157
}
160-
fn add_cost(&mut self, _cost: ExecutionCost) -> std::result::Result<(), CostErrors> {
158+
fn add_cost(&mut self, _cost: ExecutionCost) -> Result<(), CostErrors> {
161159
Ok(())
162160
}
163-
fn add_memory(&mut self, _memory: u64) -> std::result::Result<(), CostErrors> {
161+
fn add_memory(&mut self, _memory: u64) -> Result<(), CostErrors> {
164162
Ok(())
165163
}
166-
fn drop_memory(&mut self, _memory: u64) -> Result<()> {
164+
fn drop_memory(&mut self, _memory: u64) -> Result<(), CostErrors> {
167165
Ok(())
168166
}
169167
fn reset_memory(&mut self) {}
@@ -172,7 +170,7 @@ impl CostTracker for () {
172170
_contract: &QualifiedContractIdentifier,
173171
_function: &ClarityName,
174172
_input: &[u64],
175-
) -> Result<bool> {
173+
) -> Result<bool, CostErrors> {
176174
Ok(false)
177175
}
178176
}
@@ -204,7 +202,7 @@ impl DefaultVersion {
204202
cost_function_ref: &ClarityCostFunctionReference,
205203
f: &ClarityCostFunction,
206204
input: &[u64],
207-
) -> Result<ExecutionCost> {
205+
) -> Result<ExecutionCost, CostErrors> {
208206
let n = input.first().ok_or_else(|| {
209207
CostErrors::Expect("Default cost function supplied with 0 args".into())
210208
})?;
@@ -231,10 +229,7 @@ impl DefaultVersion {
231229
}
232230

233231
impl DefaultVersion {
234-
pub fn try_from(
235-
mainnet: bool,
236-
value: &QualifiedContractIdentifier,
237-
) -> std::result::Result<Self, String> {
232+
pub fn try_from(mainnet: bool, value: &QualifiedContractIdentifier) -> Result<Self, String> {
238233
if !value.is_boot() {
239234
return Err("Not a boot contract".into());
240235
}
@@ -414,7 +409,10 @@ impl PartialEq for LimitedCostTracker {
414409
}
415410
}
416411

417-
fn load_state_summary(mainnet: bool, clarity_db: &mut ClarityDatabase) -> Result<CostStateSummary> {
412+
fn load_state_summary(
413+
mainnet: bool,
414+
clarity_db: &mut ClarityDatabase,
415+
) -> Result<CostStateSummary, CostErrors> {
418416
let cost_voting_contract = boot_code_id("cost-voting", mainnet);
419417

420418
let clarity_epoch = clarity_db
@@ -455,7 +453,7 @@ fn store_state_summary(
455453
mainnet: bool,
456454
clarity_db: &mut ClarityDatabase,
457455
to_store: &CostStateSummary,
458-
) -> Result<()> {
456+
) -> Result<(), CostErrors> {
459457
let block_height = clarity_db.get_current_block_height();
460458
let cost_voting_contract = boot_code_id("cost-voting", mainnet);
461459
let epoch = clarity_db
@@ -496,7 +494,7 @@ fn load_cost_functions(
496494
mainnet: bool,
497495
clarity_db: &mut ClarityDatabase,
498496
apply_updates: bool,
499-
) -> Result<CostStateSummary> {
497+
) -> Result<CostStateSummary, CostErrors> {
500498
let clarity_epoch = clarity_db
501499
.get_clarity_epoch_version()
502500
.map_err(|e| CostErrors::CostComputationFailed(e.to_string()))?;
@@ -772,7 +770,7 @@ impl LimitedCostTracker {
772770
limit: ExecutionCost,
773771
clarity_db: &mut ClarityDatabase,
774772
epoch: StacksEpochId,
775-
) -> Result<LimitedCostTracker> {
773+
) -> Result<LimitedCostTracker, CostErrors> {
776774
let mut cost_tracker = TrackerData {
777775
cost_function_references: HashMap::new(),
778776
cost_contracts: HashMap::new(),
@@ -796,7 +794,7 @@ impl LimitedCostTracker {
796794
limit: ExecutionCost,
797795
clarity_db: &mut ClarityDatabase,
798796
epoch: StacksEpochId,
799-
) -> Result<LimitedCostTracker> {
797+
) -> Result<LimitedCostTracker, CostErrors> {
800798
let mut cost_tracker = TrackerData {
801799
cost_function_references: HashMap::new(),
802800
cost_contracts: HashMap::new(),
@@ -818,7 +816,7 @@ impl LimitedCostTracker {
818816
clarity_db: &mut ClarityDatabase,
819817
epoch: StacksEpochId,
820818
use_mainnet: bool,
821-
) -> Result<LimitedCostTracker> {
819+
) -> Result<LimitedCostTracker, CostErrors> {
822820
use crate::vm::tests::test_only_mainnet_to_chain_id;
823821
let chain_id = test_only_mainnet_to_chain_id(use_mainnet);
824822
assert!(clarity_db.is_stack_empty());
@@ -835,7 +833,7 @@ impl LimitedCostTracker {
835833
Self::Free
836834
}
837835

838-
pub fn default_cost_contract_for_epoch(epoch_id: StacksEpochId) -> Result<String> {
836+
pub fn default_cost_contract_for_epoch(epoch_id: StacksEpochId) -> Result<String, CostErrors> {
839837
let result = match epoch_id {
840838
StacksEpochId::Epoch10 => {
841839
return Err(CostErrors::Expect("Attempted to get default cost functions for Epoch 1.0 where Clarity does not exist".into()));
@@ -862,7 +860,11 @@ impl TrackerData {
862860
/// `apply_updates` - tells this function to look for any changes in the cost voting contract
863861
/// which would need to be applied. if `false`, just load the last computed cost state in this
864862
/// fork.
865-
fn load_costs(&mut self, clarity_db: &mut ClarityDatabase, apply_updates: bool) -> Result<()> {
863+
fn load_costs(
864+
&mut self,
865+
clarity_db: &mut ClarityDatabase,
866+
apply_updates: bool,
867+
) -> Result<(), CostErrors> {
866868
clarity_db.begin();
867869
let epoch_id = clarity_db
868870
.get_clarity_epoch_version()
@@ -1003,7 +1005,7 @@ impl LimitedCostTracker {
10031005
pub fn parse_cost(
10041006
cost_function_name: &str,
10051007
eval_result: InterpreterResult<Option<Value>>,
1006-
) -> Result<ExecutionCost> {
1008+
) -> Result<ExecutionCost, CostErrors> {
10071009
match eval_result {
10081010
Ok(Some(Value::Tuple(data))) => {
10091011
let results = (
@@ -1052,7 +1054,7 @@ pub fn compute_cost(
10521054
cost_function_reference: ClarityCostFunctionReference,
10531055
input_sizes: &[u64],
10541056
eval_in_epoch: StacksEpochId,
1055-
) -> Result<ExecutionCost> {
1057+
) -> Result<ExecutionCost, CostErrors> {
10561058
let mainnet = cost_tracker.mainnet;
10571059
let chain_id = cost_tracker.chain_id;
10581060
let mut null_store = NullBackingStore::new();
@@ -1103,7 +1105,7 @@ pub fn compute_cost(
11031105
parse_cost(&cost_function_reference.to_string(), eval_result)
11041106
}
11051107

1106-
fn add_cost(s: &mut TrackerData, cost: ExecutionCost) -> std::result::Result<(), CostErrors> {
1108+
fn add_cost(s: &mut TrackerData, cost: ExecutionCost) -> Result<(), CostErrors> {
11071109
s.total.add(&cost)?;
11081110
if cfg!(feature = "disable-costs") {
11091111
// Disable check for exceeding the cost limit to allow mining large blocks for profiling purposes.
@@ -1119,7 +1121,7 @@ fn add_cost(s: &mut TrackerData, cost: ExecutionCost) -> std::result::Result<(),
11191121
}
11201122
}
11211123

1122-
fn add_memory(s: &mut TrackerData, memory: u64) -> std::result::Result<(), CostErrors> {
1124+
fn add_memory(s: &mut TrackerData, memory: u64) -> Result<(), CostErrors> {
11231125
s.memory = s.memory.cost_overflow_add(memory)?;
11241126
if s.memory > s.memory_limit {
11251127
Err(CostErrors::MemoryBalanceExceeded(s.memory, s.memory_limit))
@@ -1128,7 +1130,7 @@ fn add_memory(s: &mut TrackerData, memory: u64) -> std::result::Result<(), CostE
11281130
}
11291131
}
11301132

1131-
fn drop_memory(s: &mut TrackerData, memory: u64) -> Result<()> {
1133+
fn drop_memory(s: &mut TrackerData, memory: u64) -> Result<(), CostErrors> {
11321134
s.memory = s
11331135
.memory
11341136
.checked_sub(memory)
@@ -1141,7 +1143,7 @@ impl CostTracker for LimitedCostTracker {
11411143
&mut self,
11421144
cost_function: ClarityCostFunction,
11431145
input: &[u64],
1144-
) -> std::result::Result<ExecutionCost, CostErrors> {
1146+
) -> Result<ExecutionCost, CostErrors> {
11451147
match self {
11461148
Self::Free => {
11471149
// tracker is free, return zero!
@@ -1172,19 +1174,19 @@ impl CostTracker for LimitedCostTracker {
11721174
}
11731175
}
11741176
}
1175-
fn add_cost(&mut self, cost: ExecutionCost) -> std::result::Result<(), CostErrors> {
1177+
fn add_cost(&mut self, cost: ExecutionCost) -> Result<(), CostErrors> {
11761178
match self {
11771179
Self::Free => Ok(()),
11781180
Self::Limited(ref mut data) => add_cost(data, cost),
11791181
}
11801182
}
1181-
fn add_memory(&mut self, memory: u64) -> std::result::Result<(), CostErrors> {
1183+
fn add_memory(&mut self, memory: u64) -> Result<(), CostErrors> {
11821184
match self {
11831185
Self::Free => Ok(()),
11841186
Self::Limited(ref mut data) => add_memory(data, memory),
11851187
}
11861188
}
1187-
fn drop_memory(&mut self, memory: u64) -> Result<()> {
1189+
fn drop_memory(&mut self, memory: u64) -> Result<(), CostErrors> {
11881190
match self {
11891191
Self::Free => Ok(()),
11901192
Self::Limited(ref mut data) => drop_memory(data, memory),
@@ -1203,7 +1205,7 @@ impl CostTracker for LimitedCostTracker {
12031205
contract: &QualifiedContractIdentifier,
12041206
function: &ClarityName,
12051207
input: &[u64],
1206-
) -> Result<bool> {
1208+
) -> Result<bool, CostErrors> {
12071209
match self {
12081210
Self::Free => {
12091211
// if we're already free, no need to worry about short circuiting contract-calls
@@ -1228,16 +1230,16 @@ impl CostTracker for &mut LimitedCostTracker {
12281230
&mut self,
12291231
cost_function: ClarityCostFunction,
12301232
input: &[u64],
1231-
) -> std::result::Result<ExecutionCost, CostErrors> {
1233+
) -> Result<ExecutionCost, CostErrors> {
12321234
LimitedCostTracker::compute_cost(self, cost_function, input)
12331235
}
1234-
fn add_cost(&mut self, cost: ExecutionCost) -> std::result::Result<(), CostErrors> {
1236+
fn add_cost(&mut self, cost: ExecutionCost) -> Result<(), CostErrors> {
12351237
LimitedCostTracker::add_cost(self, cost)
12361238
}
1237-
fn add_memory(&mut self, memory: u64) -> std::result::Result<(), CostErrors> {
1239+
fn add_memory(&mut self, memory: u64) -> Result<(), CostErrors> {
12381240
LimitedCostTracker::add_memory(self, memory)
12391241
}
1240-
fn drop_memory(&mut self, memory: u64) -> std::result::Result<(), CostErrors> {
1242+
fn drop_memory(&mut self, memory: u64) -> Result<(), CostErrors> {
12411243
LimitedCostTracker::drop_memory(self, memory)
12421244
}
12431245
fn reset_memory(&mut self) {
@@ -1248,7 +1250,7 @@ impl CostTracker for &mut LimitedCostTracker {
12481250
contract: &QualifiedContractIdentifier,
12491251
function: &ClarityName,
12501252
input: &[u64],
1251-
) -> Result<bool> {
1253+
) -> Result<bool, CostErrors> {
12521254
LimitedCostTracker::short_circuit_contract_call(self, contract, function, input)
12531255
}
12541256
}

0 commit comments

Comments
 (0)