casper_execution_engine/engine_state/
engine_config.rs1use std::collections::BTreeSet;
5
6use num_rational::Ratio;
7use num_traits::One;
8
9use casper_types::{
10 account::AccountHash, FeeHandling, ProtocolVersion, PublicKey, RefundHandling, StorageCosts,
11 SystemConfig, TimeDiff, WasmConfig, DEFAULT_FEE_HANDLING, DEFAULT_MINIMUM_BID_AMOUNT,
12 DEFAULT_REFUND_HANDLING,
13};
14
15pub const DEFAULT_MAX_QUERY_DEPTH: u64 = 5;
17pub const DEFAULT_MAX_ASSOCIATED_KEYS: u32 = 100;
19pub const DEFAULT_MAX_RUNTIME_CALL_STACK_HEIGHT: u32 = 12;
21#[deprecated(
23 since = "3.2.0",
24 note = "not used in `casper-execution-engine` config anymore"
25)]
26pub const DEFAULT_MAX_STORED_VALUE_SIZE: u32 = 8 * 1024 * 1024;
27pub const DEFAULT_MINIMUM_DELEGATION_AMOUNT: u64 = 500 * 1_000_000_000;
29pub const DEFAULT_MAXIMUM_DELEGATION_AMOUNT: u64 = 1_000_000_000 * 1_000_000_000;
31pub const DEFAULT_STRICT_ARGUMENT_CHECKING: bool = false;
33const VESTING_SCHEDULE_LENGTH_DAYS: usize = 91;
36const DAY_MILLIS: usize = 24 * 60 * 60 * 1000;
37pub const DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS: u64 =
39 VESTING_SCHEDULE_LENGTH_DAYS as u64 * DAY_MILLIS as u64;
40pub const DEFAULT_MAX_DELEGATORS_PER_VALIDATOR: u32 = 1200;
42pub const DEFAULT_ALLOW_AUCTION_BIDS: bool = true;
44pub const DEFAULT_ALLOW_UNRESTRICTED_TRANSFERS: bool = true;
46pub const DEFAULT_COMPUTE_REWARDS: bool = true;
48pub const DEFAULT_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::V2_0_0;
50pub const DEFAULT_BALANCE_HOLD_INTERVAL: TimeDiff = TimeDiff::from_seconds(24 * 60 * 60);
52
53pub const DEFAULT_ENABLE_ENTITY: bool = false;
55
56pub(crate) const DEFAULT_TRAP_ON_AMBIGUOUS_ENTITY_VERSION: bool = false;
57
58#[derive(Debug, Clone)]
60pub struct EngineConfig {
61 max_associated_keys: u32,
65 max_runtime_call_stack_height: u32,
66 minimum_delegation_amount: u64,
67 maximum_delegation_amount: u64,
68 minimum_bid_amount: u64,
69 strict_argument_checking: bool,
71 vesting_schedule_period_millis: u64,
73 max_delegators_per_validator: u32,
74 wasm_config: WasmConfig,
75 system_config: SystemConfig,
76 protocol_version: ProtocolVersion,
77 pub(crate) administrative_accounts: BTreeSet<AccountHash>,
79 pub(crate) allow_auction_bids: bool,
82 pub(crate) allow_unrestricted_transfers: bool,
88 pub(crate) refund_handling: RefundHandling,
90 pub(crate) fee_handling: FeeHandling,
92 pub(crate) compute_rewards: bool,
94 pub(crate) enable_entity: bool,
95 pub(crate) trap_on_ambiguous_entity_version: bool,
96 storage_costs: StorageCosts,
97}
98
99impl Default for EngineConfig {
100 fn default() -> Self {
101 EngineConfig {
102 max_associated_keys: DEFAULT_MAX_ASSOCIATED_KEYS,
103 max_runtime_call_stack_height: DEFAULT_MAX_RUNTIME_CALL_STACK_HEIGHT,
104 minimum_delegation_amount: DEFAULT_MINIMUM_DELEGATION_AMOUNT,
105 maximum_delegation_amount: DEFAULT_MAXIMUM_DELEGATION_AMOUNT,
106 minimum_bid_amount: DEFAULT_MINIMUM_BID_AMOUNT,
107 strict_argument_checking: DEFAULT_STRICT_ARGUMENT_CHECKING,
108 vesting_schedule_period_millis: DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS,
109 max_delegators_per_validator: DEFAULT_MAX_DELEGATORS_PER_VALIDATOR,
110 wasm_config: WasmConfig::default(),
111 system_config: SystemConfig::default(),
112 administrative_accounts: Default::default(),
113 allow_auction_bids: DEFAULT_ALLOW_AUCTION_BIDS,
114 allow_unrestricted_transfers: DEFAULT_ALLOW_UNRESTRICTED_TRANSFERS,
115 refund_handling: DEFAULT_REFUND_HANDLING,
116 fee_handling: DEFAULT_FEE_HANDLING,
117 compute_rewards: DEFAULT_COMPUTE_REWARDS,
118 protocol_version: DEFAULT_PROTOCOL_VERSION,
119 enable_entity: DEFAULT_ENABLE_ENTITY,
120 trap_on_ambiguous_entity_version: DEFAULT_TRAP_ON_AMBIGUOUS_ENTITY_VERSION,
121 storage_costs: Default::default(),
122 }
123 }
124}
125
126impl EngineConfig {
127 pub fn max_associated_keys(&self) -> u32 {
129 self.max_associated_keys
130 }
131
132 pub fn max_runtime_call_stack_height(&self) -> u32 {
134 self.max_runtime_call_stack_height
135 }
136
137 pub fn wasm_config(&self) -> &WasmConfig {
139 &self.wasm_config
140 }
141
142 pub fn system_config(&self) -> &SystemConfig {
144 &self.system_config
145 }
146
147 pub fn protocol_version(&self) -> ProtocolVersion {
149 self.protocol_version
150 }
151
152 pub fn minimum_delegation_amount(&self) -> u64 {
154 self.minimum_delegation_amount
155 }
156
157 pub fn maximum_delegation_amount(&self) -> u64 {
159 self.maximum_delegation_amount
160 }
161
162 pub fn minimum_bid_amount(&self) -> u64 {
164 self.minimum_bid_amount
165 }
166
167 pub fn strict_argument_checking(&self) -> bool {
169 self.strict_argument_checking
170 }
171
172 pub fn vesting_schedule_period_millis(&self) -> u64 {
174 self.vesting_schedule_period_millis
175 }
176
177 pub fn max_delegators_per_validator(&self) -> u32 {
179 self.max_delegators_per_validator
180 }
181
182 pub fn administrative_accounts(&self) -> &BTreeSet<AccountHash> {
184 &self.administrative_accounts
185 }
186
187 pub fn allow_auction_bids(&self) -> bool {
189 self.allow_auction_bids
190 }
191
192 pub fn allow_unrestricted_transfers(&self) -> bool {
194 self.allow_unrestricted_transfers
195 }
196
197 pub(crate) fn is_administrator(&self, account_hash: &AccountHash) -> bool {
199 self.administrative_accounts.contains(account_hash)
200 }
201
202 pub fn refund_handling(&self) -> RefundHandling {
204 self.refund_handling
205 }
206
207 pub fn fee_handling(&self) -> FeeHandling {
209 self.fee_handling
210 }
211
212 pub fn storage_costs(&self) -> &StorageCosts {
214 &self.storage_costs
215 }
216
217 pub fn compute_rewards(&self) -> bool {
219 self.compute_rewards
220 }
221
222 pub fn trap_on_ambiguous_entity_version(&self) -> bool {
224 self.trap_on_ambiguous_entity_version
225 }
226
227 #[doc(hidden)]
232 pub fn set_protocol_version(&mut self, protocol_version: ProtocolVersion) {
233 self.protocol_version = protocol_version;
234 }
235
236 #[cfg(feature = "test-support")]
238 pub fn set_max_memory(&mut self, new_value: u32) {
239 *self.wasm_config.v1_mut().max_memory_mut() = new_value;
240 }
241}
242
243#[derive(Default, Debug)]
248pub struct EngineConfigBuilder {
249 max_query_depth: Option<u64>,
250 max_associated_keys: Option<u32>,
251 max_runtime_call_stack_height: Option<u32>,
252 minimum_delegation_amount: Option<u64>,
253 maximum_delegation_amount: Option<u64>,
254 minimum_bid_amount: Option<u64>,
255 strict_argument_checking: Option<bool>,
256 vesting_schedule_period_millis: Option<u64>,
257 max_delegators_per_validator: Option<u32>,
258 wasm_config: Option<WasmConfig>,
259 system_config: Option<SystemConfig>,
260 protocol_version: Option<ProtocolVersion>,
261 administrative_accounts: Option<BTreeSet<PublicKey>>,
262 allow_auction_bids: Option<bool>,
263 allow_unrestricted_transfers: Option<bool>,
264 refund_handling: Option<RefundHandling>,
265 fee_handling: Option<FeeHandling>,
266 compute_rewards: Option<bool>,
267 balance_hold_interval: Option<TimeDiff>,
268 enable_entity: Option<bool>,
269 trap_on_ambiguous_entity_version: Option<bool>,
270 storage_costs: Option<StorageCosts>,
271}
272
273impl EngineConfigBuilder {
274 pub fn new() -> Self {
276 EngineConfigBuilder::default()
277 }
278
279 pub fn with_max_query_depth(mut self, max_query_depth: u64) -> Self {
281 self.max_query_depth = Some(max_query_depth);
282 self
283 }
284
285 pub fn with_max_associated_keys(mut self, max_associated_keys: u32) -> Self {
287 self.max_associated_keys = Some(max_associated_keys);
288 self
289 }
290
291 pub fn with_max_runtime_call_stack_height(
293 mut self,
294 max_runtime_call_stack_height: u32,
295 ) -> Self {
296 self.max_runtime_call_stack_height = Some(max_runtime_call_stack_height);
297 self
298 }
299
300 pub fn with_strict_argument_checking(mut self, value: bool) -> Self {
302 self.strict_argument_checking = Some(value);
303 self
304 }
305
306 pub fn with_vesting_schedule_period_millis(mut self, value: u64) -> Self {
308 self.vesting_schedule_period_millis = Some(value);
309 self
310 }
311
312 pub fn with_max_delegators_per_validator(mut self, value: u32) -> Self {
314 self.max_delegators_per_validator = Some(value);
315 self
316 }
317
318 pub fn with_wasm_config(mut self, wasm_config: WasmConfig) -> Self {
320 self.wasm_config = Some(wasm_config);
321 self
322 }
323
324 pub fn with_system_config(mut self, system_config: SystemConfig) -> Self {
326 self.system_config = Some(system_config);
327 self
328 }
329
330 pub fn with_protocol_version(mut self, protocol_version: ProtocolVersion) -> Self {
332 self.protocol_version = Some(protocol_version);
333 self
334 }
335
336 pub fn with_wasm_max_stack_height(mut self, wasm_stack_height: u32) -> Self {
338 let wasm_config = self.wasm_config.get_or_insert_with(WasmConfig::default);
339 *wasm_config.v1_mut().max_stack_height_mut() = wasm_stack_height;
340 self
341 }
342
343 pub fn with_minimum_delegation_amount(mut self, minimum_delegation_amount: u64) -> Self {
345 self.minimum_delegation_amount = Some(minimum_delegation_amount);
346 self
347 }
348
349 pub fn with_maximum_delegation_amount(mut self, maximum_delegation_amount: u64) -> Self {
351 self.maximum_delegation_amount = Some(maximum_delegation_amount);
352 self
353 }
354
355 pub fn with_minimum_bid_amount(mut self, minimum_bid_amount: u64) -> Self {
357 self.minimum_bid_amount = Some(minimum_bid_amount);
358 self
359 }
360
361 pub fn with_administrative_accounts(
363 mut self,
364 administrator_accounts: BTreeSet<PublicKey>,
365 ) -> Self {
366 self.administrative_accounts = Some(administrator_accounts);
367 self
368 }
369
370 pub fn with_allow_auction_bids(mut self, allow_auction_bids: bool) -> Self {
372 self.allow_auction_bids = Some(allow_auction_bids);
373 self
374 }
375
376 pub fn with_allow_unrestricted_transfers(mut self, allow_unrestricted_transfers: bool) -> Self {
378 self.allow_unrestricted_transfers = Some(allow_unrestricted_transfers);
379 self
380 }
381
382 pub fn with_refund_handling(mut self, refund_handling: RefundHandling) -> Self {
384 match refund_handling {
385 RefundHandling::Refund { refund_ratio } | RefundHandling::Burn { refund_ratio } => {
386 debug_assert!(
387 refund_ratio <= Ratio::one(),
388 "refund ratio should be in the range of [0, 1]"
389 );
390 }
391 RefundHandling::NoRefund => {
392 }
394 }
395
396 self.refund_handling = Some(refund_handling);
397 self
398 }
399
400 pub fn with_fee_handling(mut self, fee_handling: FeeHandling) -> Self {
402 self.fee_handling = Some(fee_handling);
403 self
404 }
405
406 pub fn with_compute_rewards(mut self, compute_rewards: bool) -> Self {
408 self.compute_rewards = Some(compute_rewards);
409 self
410 }
411
412 pub fn balance_hold_interval(mut self, balance_hold_interval: TimeDiff) -> Self {
414 self.balance_hold_interval = Some(balance_hold_interval);
415 self
416 }
417
418 pub fn with_enable_entity(mut self, enable_entity: bool) -> Self {
420 self.enable_entity = Some(enable_entity);
421 self
422 }
423
424 pub fn with_trap_on_ambiguous_entity_version(
426 mut self,
427 trap_on_ambiguous_entity_version: bool,
428 ) -> Self {
429 self.trap_on_ambiguous_entity_version = Some(trap_on_ambiguous_entity_version);
430 self
431 }
432
433 pub fn with_storage_costs(mut self, storage_costs: StorageCosts) -> Self {
435 self.storage_costs = Some(storage_costs);
436 self
437 }
438
439 pub fn build(self) -> EngineConfig {
441 let max_associated_keys = self
442 .max_associated_keys
443 .unwrap_or(DEFAULT_MAX_ASSOCIATED_KEYS);
444 let max_runtime_call_stack_height = self
445 .max_runtime_call_stack_height
446 .unwrap_or(DEFAULT_MAX_RUNTIME_CALL_STACK_HEIGHT);
447 let minimum_delegation_amount = self
448 .minimum_delegation_amount
449 .unwrap_or(DEFAULT_MINIMUM_DELEGATION_AMOUNT);
450 let maximum_delegation_amount = self
451 .maximum_delegation_amount
452 .unwrap_or(DEFAULT_MAXIMUM_DELEGATION_AMOUNT);
453 let minimum_bid_amount = self
454 .minimum_bid_amount
455 .unwrap_or(DEFAULT_MINIMUM_BID_AMOUNT);
456 let wasm_config = self.wasm_config.unwrap_or_default();
457 let system_config = self.system_config.unwrap_or_default();
458 let protocol_version = self.protocol_version.unwrap_or(DEFAULT_PROTOCOL_VERSION);
459 let administrative_accounts = {
460 self.administrative_accounts
461 .unwrap_or_default()
462 .iter()
463 .map(PublicKey::to_account_hash)
464 .collect()
465 };
466 let allow_auction_bids = self
467 .allow_auction_bids
468 .unwrap_or(DEFAULT_ALLOW_AUCTION_BIDS);
469 let allow_unrestricted_transfers = self
470 .allow_unrestricted_transfers
471 .unwrap_or(DEFAULT_ALLOW_UNRESTRICTED_TRANSFERS);
472 let refund_handling = self.refund_handling.unwrap_or(DEFAULT_REFUND_HANDLING);
473 let fee_handling = self.fee_handling.unwrap_or(DEFAULT_FEE_HANDLING);
474
475 let strict_argument_checking = self
476 .strict_argument_checking
477 .unwrap_or(DEFAULT_STRICT_ARGUMENT_CHECKING);
478 let vesting_schedule_period_millis = self
479 .vesting_schedule_period_millis
480 .unwrap_or(DEFAULT_VESTING_SCHEDULE_LENGTH_MILLIS);
481 let max_delegators_per_validator = self
482 .max_delegators_per_validator
483 .unwrap_or(DEFAULT_MAX_DELEGATORS_PER_VALIDATOR);
484 let compute_rewards = self.compute_rewards.unwrap_or(DEFAULT_COMPUTE_REWARDS);
485 let enable_entity = self.enable_entity.unwrap_or(DEFAULT_ENABLE_ENTITY);
486 let trap_on_ambiguous_entity_version = self
487 .trap_on_ambiguous_entity_version
488 .unwrap_or(DEFAULT_TRAP_ON_AMBIGUOUS_ENTITY_VERSION);
489 let storage_costs = self.storage_costs.unwrap_or_default();
490
491 EngineConfig {
492 max_associated_keys,
493 max_runtime_call_stack_height,
494 minimum_delegation_amount,
495 maximum_delegation_amount,
496 minimum_bid_amount,
497 wasm_config,
498 system_config,
499 protocol_version,
500 administrative_accounts,
501 allow_auction_bids,
502 allow_unrestricted_transfers,
503 refund_handling,
504 fee_handling,
505 strict_argument_checking,
506 vesting_schedule_period_millis,
507 max_delegators_per_validator,
508 compute_rewards,
509 enable_entity,
510 trap_on_ambiguous_entity_version,
511 storage_costs,
512 }
513 }
514}