@@ -55,8 +55,6 @@ pub mod costs_3;
55
55
#[ allow( unused_variables) ]
56
56
pub mod costs_4;
57
57
58
- type Result < T > = std:: result:: Result < T , CostErrors > ;
59
-
60
58
pub const CLARITY_MEMORY_LIMIT : u64 = 100 * 1000 * 1000 ;
61
59
62
60
// TODO: factor out into a boot lib?
@@ -85,7 +83,7 @@ pub fn runtime_cost<T: TryInto<u64>, C: CostTracker>(
85
83
cost_function : ClarityCostFunction ,
86
84
tracker : & mut C ,
87
85
input : T ,
88
- ) -> Result < ( ) > {
86
+ ) -> Result < ( ) , CostErrors > {
89
87
let size: u64 = input. try_into ( ) . map_err ( |_| CostErrors :: CostOverflow ) ?;
90
88
let cost = tracker. compute_cost ( cost_function, & [ size] ) ?;
91
89
@@ -104,7 +102,7 @@ pub fn analysis_typecheck_cost<T: CostTracker>(
104
102
track : & mut T ,
105
103
t1 : & TypeSignature ,
106
104
t2 : & TypeSignature ,
107
- ) -> Result < ( ) > {
105
+ ) -> Result < ( ) , CostErrors > {
108
106
let t1_size = t1. type_size ( ) . map_err ( |_| CostErrors :: CostOverflow ) ?;
109
107
let t2_size = t2. type_size ( ) . map_err ( |_| CostErrors :: CostOverflow ) ?;
110
108
let cost = track. compute_cost (
@@ -115,11 +113,11 @@ pub fn analysis_typecheck_cost<T: CostTracker>(
115
113
}
116
114
117
115
pub trait MemoryConsumer {
118
- fn get_memory_use ( & self ) -> Result < u64 > ;
116
+ fn get_memory_use ( & self ) -> Result < u64 , CostErrors > ;
119
117
}
120
118
121
119
impl MemoryConsumer for Value {
122
- fn get_memory_use ( & self ) -> Result < u64 > {
120
+ fn get_memory_use ( & self ) -> Result < u64 , CostErrors > {
123
121
Ok ( self
124
122
. size ( )
125
123
. map_err ( |_| CostErrors :: InterpreterFailure ) ?
@@ -132,10 +130,10 @@ pub trait CostTracker {
132
130
& mut self ,
133
131
cost_function : ClarityCostFunction ,
134
132
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 > ;
139
137
fn reset_memory ( & mut self ) ;
140
138
/// Check if the given contract-call should be short-circuited.
141
139
/// If so: this charges the cost to the CostTracker, and return true
@@ -145,7 +143,7 @@ pub trait CostTracker {
145
143
contract : & QualifiedContractIdentifier ,
146
144
function : & ClarityName ,
147
145
input : & [ u64 ] ,
148
- ) -> Result < bool > ;
146
+ ) -> Result < bool , CostErrors > ;
149
147
}
150
148
151
149
// Don't track!
@@ -154,16 +152,16 @@ impl CostTracker for () {
154
152
& mut self ,
155
153
_cost_function : ClarityCostFunction ,
156
154
_input : & [ u64 ] ,
157
- ) -> std :: result :: Result < ExecutionCost , CostErrors > {
155
+ ) -> Result < ExecutionCost , CostErrors > {
158
156
Ok ( ExecutionCost :: ZERO )
159
157
}
160
- fn add_cost ( & mut self , _cost : ExecutionCost ) -> std :: result :: Result < ( ) , CostErrors > {
158
+ fn add_cost ( & mut self , _cost : ExecutionCost ) -> Result < ( ) , CostErrors > {
161
159
Ok ( ( ) )
162
160
}
163
- fn add_memory ( & mut self , _memory : u64 ) -> std :: result :: Result < ( ) , CostErrors > {
161
+ fn add_memory ( & mut self , _memory : u64 ) -> Result < ( ) , CostErrors > {
164
162
Ok ( ( ) )
165
163
}
166
- fn drop_memory ( & mut self , _memory : u64 ) -> Result < ( ) > {
164
+ fn drop_memory ( & mut self , _memory : u64 ) -> Result < ( ) , CostErrors > {
167
165
Ok ( ( ) )
168
166
}
169
167
fn reset_memory ( & mut self ) { }
@@ -172,7 +170,7 @@ impl CostTracker for () {
172
170
_contract : & QualifiedContractIdentifier ,
173
171
_function : & ClarityName ,
174
172
_input : & [ u64 ] ,
175
- ) -> Result < bool > {
173
+ ) -> Result < bool , CostErrors > {
176
174
Ok ( false )
177
175
}
178
176
}
@@ -204,7 +202,7 @@ impl DefaultVersion {
204
202
cost_function_ref : & ClarityCostFunctionReference ,
205
203
f : & ClarityCostFunction ,
206
204
input : & [ u64 ] ,
207
- ) -> Result < ExecutionCost > {
205
+ ) -> Result < ExecutionCost , CostErrors > {
208
206
let n = input. first ( ) . ok_or_else ( || {
209
207
CostErrors :: Expect ( "Default cost function supplied with 0 args" . into ( ) )
210
208
} ) ?;
@@ -231,10 +229,7 @@ impl DefaultVersion {
231
229
}
232
230
233
231
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 > {
238
233
if !value. is_boot ( ) {
239
234
return Err ( "Not a boot contract" . into ( ) ) ;
240
235
}
@@ -414,7 +409,10 @@ impl PartialEq for LimitedCostTracker {
414
409
}
415
410
}
416
411
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 > {
418
416
let cost_voting_contract = boot_code_id ( "cost-voting" , mainnet) ;
419
417
420
418
let clarity_epoch = clarity_db
@@ -455,7 +453,7 @@ fn store_state_summary(
455
453
mainnet : bool ,
456
454
clarity_db : & mut ClarityDatabase ,
457
455
to_store : & CostStateSummary ,
458
- ) -> Result < ( ) > {
456
+ ) -> Result < ( ) , CostErrors > {
459
457
let block_height = clarity_db. get_current_block_height ( ) ;
460
458
let cost_voting_contract = boot_code_id ( "cost-voting" , mainnet) ;
461
459
let epoch = clarity_db
@@ -496,7 +494,7 @@ fn load_cost_functions(
496
494
mainnet : bool ,
497
495
clarity_db : & mut ClarityDatabase ,
498
496
apply_updates : bool ,
499
- ) -> Result < CostStateSummary > {
497
+ ) -> Result < CostStateSummary , CostErrors > {
500
498
let clarity_epoch = clarity_db
501
499
. get_clarity_epoch_version ( )
502
500
. map_err ( |e| CostErrors :: CostComputationFailed ( e. to_string ( ) ) ) ?;
@@ -772,7 +770,7 @@ impl LimitedCostTracker {
772
770
limit : ExecutionCost ,
773
771
clarity_db : & mut ClarityDatabase ,
774
772
epoch : StacksEpochId ,
775
- ) -> Result < LimitedCostTracker > {
773
+ ) -> Result < LimitedCostTracker , CostErrors > {
776
774
let mut cost_tracker = TrackerData {
777
775
cost_function_references : HashMap :: new ( ) ,
778
776
cost_contracts : HashMap :: new ( ) ,
@@ -796,7 +794,7 @@ impl LimitedCostTracker {
796
794
limit : ExecutionCost ,
797
795
clarity_db : & mut ClarityDatabase ,
798
796
epoch : StacksEpochId ,
799
- ) -> Result < LimitedCostTracker > {
797
+ ) -> Result < LimitedCostTracker , CostErrors > {
800
798
let mut cost_tracker = TrackerData {
801
799
cost_function_references : HashMap :: new ( ) ,
802
800
cost_contracts : HashMap :: new ( ) ,
@@ -818,7 +816,7 @@ impl LimitedCostTracker {
818
816
clarity_db : & mut ClarityDatabase ,
819
817
epoch : StacksEpochId ,
820
818
use_mainnet : bool ,
821
- ) -> Result < LimitedCostTracker > {
819
+ ) -> Result < LimitedCostTracker , CostErrors > {
822
820
use crate :: vm:: tests:: test_only_mainnet_to_chain_id;
823
821
let chain_id = test_only_mainnet_to_chain_id ( use_mainnet) ;
824
822
assert ! ( clarity_db. is_stack_empty( ) ) ;
@@ -835,7 +833,7 @@ impl LimitedCostTracker {
835
833
Self :: Free
836
834
}
837
835
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 > {
839
837
let result = match epoch_id {
840
838
StacksEpochId :: Epoch10 => {
841
839
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 {
862
860
/// `apply_updates` - tells this function to look for any changes in the cost voting contract
863
861
/// which would need to be applied. if `false`, just load the last computed cost state in this
864
862
/// 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 > {
866
868
clarity_db. begin ( ) ;
867
869
let epoch_id = clarity_db
868
870
. get_clarity_epoch_version ( )
@@ -1003,7 +1005,7 @@ impl LimitedCostTracker {
1003
1005
pub fn parse_cost (
1004
1006
cost_function_name : & str ,
1005
1007
eval_result : InterpreterResult < Option < Value > > ,
1006
- ) -> Result < ExecutionCost > {
1008
+ ) -> Result < ExecutionCost , CostErrors > {
1007
1009
match eval_result {
1008
1010
Ok ( Some ( Value :: Tuple ( data) ) ) => {
1009
1011
let results = (
@@ -1052,7 +1054,7 @@ pub fn compute_cost(
1052
1054
cost_function_reference : ClarityCostFunctionReference ,
1053
1055
input_sizes : & [ u64 ] ,
1054
1056
eval_in_epoch : StacksEpochId ,
1055
- ) -> Result < ExecutionCost > {
1057
+ ) -> Result < ExecutionCost , CostErrors > {
1056
1058
let mainnet = cost_tracker. mainnet ;
1057
1059
let chain_id = cost_tracker. chain_id ;
1058
1060
let mut null_store = NullBackingStore :: new ( ) ;
@@ -1103,7 +1105,7 @@ pub fn compute_cost(
1103
1105
parse_cost ( & cost_function_reference. to_string ( ) , eval_result)
1104
1106
}
1105
1107
1106
- fn add_cost ( s : & mut TrackerData , cost : ExecutionCost ) -> std :: result :: Result < ( ) , CostErrors > {
1108
+ fn add_cost ( s : & mut TrackerData , cost : ExecutionCost ) -> Result < ( ) , CostErrors > {
1107
1109
s. total . add ( & cost) ?;
1108
1110
if cfg ! ( feature = "disable-costs" ) {
1109
1111
// 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<(),
1119
1121
}
1120
1122
}
1121
1123
1122
- fn add_memory ( s : & mut TrackerData , memory : u64 ) -> std :: result :: Result < ( ) , CostErrors > {
1124
+ fn add_memory ( s : & mut TrackerData , memory : u64 ) -> Result < ( ) , CostErrors > {
1123
1125
s. memory = s. memory . cost_overflow_add ( memory) ?;
1124
1126
if s. memory > s. memory_limit {
1125
1127
Err ( CostErrors :: MemoryBalanceExceeded ( s. memory , s. memory_limit ) )
@@ -1128,7 +1130,7 @@ fn add_memory(s: &mut TrackerData, memory: u64) -> std::result::Result<(), CostE
1128
1130
}
1129
1131
}
1130
1132
1131
- fn drop_memory ( s : & mut TrackerData , memory : u64 ) -> Result < ( ) > {
1133
+ fn drop_memory ( s : & mut TrackerData , memory : u64 ) -> Result < ( ) , CostErrors > {
1132
1134
s. memory = s
1133
1135
. memory
1134
1136
. checked_sub ( memory)
@@ -1141,7 +1143,7 @@ impl CostTracker for LimitedCostTracker {
1141
1143
& mut self ,
1142
1144
cost_function : ClarityCostFunction ,
1143
1145
input : & [ u64 ] ,
1144
- ) -> std :: result :: Result < ExecutionCost , CostErrors > {
1146
+ ) -> Result < ExecutionCost , CostErrors > {
1145
1147
match self {
1146
1148
Self :: Free => {
1147
1149
// tracker is free, return zero!
@@ -1172,19 +1174,19 @@ impl CostTracker for LimitedCostTracker {
1172
1174
}
1173
1175
}
1174
1176
}
1175
- fn add_cost ( & mut self , cost : ExecutionCost ) -> std :: result :: Result < ( ) , CostErrors > {
1177
+ fn add_cost ( & mut self , cost : ExecutionCost ) -> Result < ( ) , CostErrors > {
1176
1178
match self {
1177
1179
Self :: Free => Ok ( ( ) ) ,
1178
1180
Self :: Limited ( ref mut data) => add_cost ( data, cost) ,
1179
1181
}
1180
1182
}
1181
- fn add_memory ( & mut self , memory : u64 ) -> std :: result :: Result < ( ) , CostErrors > {
1183
+ fn add_memory ( & mut self , memory : u64 ) -> Result < ( ) , CostErrors > {
1182
1184
match self {
1183
1185
Self :: Free => Ok ( ( ) ) ,
1184
1186
Self :: Limited ( ref mut data) => add_memory ( data, memory) ,
1185
1187
}
1186
1188
}
1187
- fn drop_memory ( & mut self , memory : u64 ) -> Result < ( ) > {
1189
+ fn drop_memory ( & mut self , memory : u64 ) -> Result < ( ) , CostErrors > {
1188
1190
match self {
1189
1191
Self :: Free => Ok ( ( ) ) ,
1190
1192
Self :: Limited ( ref mut data) => drop_memory ( data, memory) ,
@@ -1203,7 +1205,7 @@ impl CostTracker for LimitedCostTracker {
1203
1205
contract : & QualifiedContractIdentifier ,
1204
1206
function : & ClarityName ,
1205
1207
input : & [ u64 ] ,
1206
- ) -> Result < bool > {
1208
+ ) -> Result < bool , CostErrors > {
1207
1209
match self {
1208
1210
Self :: Free => {
1209
1211
// if we're already free, no need to worry about short circuiting contract-calls
@@ -1228,16 +1230,16 @@ impl CostTracker for &mut LimitedCostTracker {
1228
1230
& mut self ,
1229
1231
cost_function : ClarityCostFunction ,
1230
1232
input : & [ u64 ] ,
1231
- ) -> std :: result :: Result < ExecutionCost , CostErrors > {
1233
+ ) -> Result < ExecutionCost , CostErrors > {
1232
1234
LimitedCostTracker :: compute_cost ( self , cost_function, input)
1233
1235
}
1234
- fn add_cost ( & mut self , cost : ExecutionCost ) -> std :: result :: Result < ( ) , CostErrors > {
1236
+ fn add_cost ( & mut self , cost : ExecutionCost ) -> Result < ( ) , CostErrors > {
1235
1237
LimitedCostTracker :: add_cost ( self , cost)
1236
1238
}
1237
- fn add_memory ( & mut self , memory : u64 ) -> std :: result :: Result < ( ) , CostErrors > {
1239
+ fn add_memory ( & mut self , memory : u64 ) -> Result < ( ) , CostErrors > {
1238
1240
LimitedCostTracker :: add_memory ( self , memory)
1239
1241
}
1240
- fn drop_memory ( & mut self , memory : u64 ) -> std :: result :: Result < ( ) , CostErrors > {
1242
+ fn drop_memory ( & mut self , memory : u64 ) -> Result < ( ) , CostErrors > {
1241
1243
LimitedCostTracker :: drop_memory ( self , memory)
1242
1244
}
1243
1245
fn reset_memory ( & mut self ) {
@@ -1248,7 +1250,7 @@ impl CostTracker for &mut LimitedCostTracker {
1248
1250
contract : & QualifiedContractIdentifier ,
1249
1251
function : & ClarityName ,
1250
1252
input : & [ u64 ] ,
1251
- ) -> Result < bool > {
1253
+ ) -> Result < bool , CostErrors > {
1252
1254
LimitedCostTracker :: short_circuit_contract_call ( self , contract, function, input)
1253
1255
}
1254
1256
}
0 commit comments