-
Notifications
You must be signed in to change notification settings - Fork 38
/
config.rs
559 lines (503 loc) · 16.5 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::{
cmp::min,
collections::{HashMap, HashSet},
path::PathBuf,
};
use ckb_fixed_hash::{H160, H256};
pub use gw_jsonrpc_types::godwoken::GaslessTxSupportConfig;
use gw_jsonrpc_types::{
ckb_jsonrpc_types::{CellDep, JsonBytes, Script},
godwoken::ChallengeTargetType,
};
use pid::Pid;
use serde::{Deserialize, Serialize};
use crate::{consensus::Consensus, fork_config::BackendForkConfig};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Trace {
Jaeger,
TokioConsole,
}
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub node_mode: NodeMode,
pub liveness_duration_secs: Option<u64>,
#[serde(default)]
pub trace_generator_state: bool,
#[serde(default)]
pub contract_log_config: ContractLogConfig,
pub consensus: Consensus,
pub debug_backend_forks: Option<Vec<BackendForkConfig>>,
pub rpc_client: RPCClientConfig,
pub rpc_server: RPCServerConfig,
#[serde(default)]
pub debug: DebugConfig,
pub block_producer: Option<BlockProducerConfig>,
#[serde(default)]
pub offchain_validator: Option<OffChainValidatorConfig>,
#[serde(default)]
pub mem_pool: MemPoolConfig,
#[serde(default)]
pub db_block_validator: Option<DBBlockValidatorConfig>,
pub store: StoreConfig,
#[serde(default)]
pub trace: Option<Trace>,
#[serde(default)]
pub p2p_network_config: Option<P2PNetworkConfig>,
#[serde(default)]
pub sync_server: SyncServerConfig,
/// Gasless tx support is enabled when this config presents.
#[serde(default)]
pub gasless_tx_support: Option<GaslessTxSupportConfig>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "lowercase")]
pub enum RPCMethods {
PProf,
Test,
Debug,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RPCServerConfig {
pub listen: String,
#[serde(default)]
pub enable_methods: HashSet<RPCMethods>,
pub send_tx_rate_limit: Option<RPCRateLimit>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RPCClientConfig {
/// Specify standalone ckb indexer URL.
///
/// If this is None we use CKB builtin indexer RPC instead.
pub indexer_url: Option<String>,
pub ckb_url: String,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MemPoolExtraConfig {
pub allowed_sudt_proxy_creator_account_id: Vec<u32>,
pub sudt_proxy_code_hashes: Vec<H256>,
pub allowed_polyjuice_contract_creator_address: Option<HashSet<H160>>,
pub polyjuice_script_code_hash: Option<H256>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RPCRateLimit {
pub seconds: u64,
pub lru_size: usize,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WalletConfig {
pub privkey_path: PathBuf,
}
// NOTE: Rewards receiver lock must be different than lock in WalletConfig,
// since stake_capacity(minus burnt) + challenge_capacity - tx_fee will never
// bigger or equal than stake_capacity(minus burnt) + challenge_capacity.
// TODO: Support sudt stake ?
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChallengerConfig {
pub rewards_receiver_lock: Script,
}
#[derive(Clone, Debug, Default)]
pub struct ContractsCellDep {
pub rollup_config: CellDep,
pub rollup_cell_type: CellDep,
pub deposit_cell_lock: CellDep,
pub stake_cell_lock: CellDep,
pub custodian_cell_lock: CellDep,
pub withdrawal_cell_lock: CellDep,
pub challenge_cell_lock: CellDep,
pub l1_sudt_type: CellDep,
pub omni_lock: CellDep,
pub delegate_cell_lock: Option<CellDep>,
pub delegate_cell: Option<CellDep>,
pub allowed_eoa_locks: HashMap<H256, CellDep>,
pub allowed_contract_types: HashMap<H256, CellDep>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum RegistryType {
#[default]
Eth,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RegistryAddressConfig {
pub address_type: RegistryType,
pub address: JsonBytes,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct BlockProducerConfig {
pub check_mem_block_before_submit: bool,
pub fee_rate: u64,
#[serde(flatten)]
pub psc_config: PscConfig,
pub block_producer: RegistryAddressConfig,
pub challenger_config: ChallengerConfig,
pub wallet_config: Option<WalletConfig>,
pub withdrawal_unlocker_wallet_config: Option<WalletConfig>,
}
impl Default for BlockProducerConfig {
fn default() -> Self {
BlockProducerConfig {
check_mem_block_before_submit: false,
fee_rate: 1000,
psc_config: PscConfig::default(),
block_producer: RegistryAddressConfig::default(),
challenger_config: ChallengerConfig::default(),
wallet_config: None,
withdrawal_unlocker_wallet_config: None,
}
}
}
#[test]
fn test_default_block_producer_config() {
let config: BlockProducerConfig = toml::from_str("").unwrap();
assert_eq!(config, BlockProducerConfig::default());
assert!(config.fee_rate > 0);
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct PscConfig {
/// Maximum number local blocks. Local blocks are blocks that have not been
/// submitted to L1. Default is 5.
pub local_limit: u64,
/// Maximum number of submitted (but not confirmed) blocks. Default is 5.
pub submitted_limit: u64,
/// Minimum delay between blocks. Default is 8 seconds.
pub block_interval_secs: u64,
pub min_fee_rate: u64,
pub max_fee_rate: u64,
pub fee_rate_pid: Option<Pid<f64>>,
pub fee_rate_pid_interval_secs: u64,
/// Reset submission txs if a tx cannot be confirmed after the specified duration.
/// It's not very reliable. Don't use in prod.
pub confirm_timeout_secs: Option<u64>,
}
impl Default for PscConfig {
fn default() -> Self {
Self {
local_limit: 2,
submitted_limit: 10,
block_interval_secs: 8,
fee_rate_pid: None,
min_fee_rate: 1000,
max_fee_rate: 1100,
fee_rate_pid_interval_secs: 10,
confirm_timeout_secs: None,
}
}
}
#[test]
fn test_psc_config_optional() {
#[derive(Deserialize)]
struct BiggerConfig {
_x: i32,
#[serde(flatten)]
psc_config: PscConfig,
}
assert_eq!(
toml::from_str::<BiggerConfig>("_x = 3").unwrap().psc_config,
PscConfig::default()
);
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DebugConfig {
pub output_l1_tx_cycles: bool,
pub expected_l1_tx_upper_bound_cycles: u64,
/// Directory to save debugging info of l1 transactions
pub debug_tx_dump_path: PathBuf,
#[serde(default = "default_enable_debug_rpc")]
pub enable_debug_rpc: bool,
}
// Field default value for backward config file compitability
fn default_enable_debug_rpc() -> bool {
false
}
impl Default for DebugConfig {
fn default() -> Self {
const EXPECTED_TX_UPPER_BOUND_CYCLES: u64 = 350000000u64;
const DEFAULT_DEBUG_TX_DUMP_PATH: &str = "debug-tx-dump";
Self {
debug_tx_dump_path: DEFAULT_DEBUG_TX_DUMP_PATH.into(),
output_l1_tx_cycles: true,
expected_l1_tx_upper_bound_cycles: EXPECTED_TX_UPPER_BOUND_CYCLES,
enable_debug_rpc: false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OffChainValidatorConfig {
pub verify_withdrawal_signature: bool,
pub verify_tx_signature: bool,
pub verify_tx_execution: bool,
pub verify_max_cycles: u64,
pub dump_tx_on_failure: bool,
}
impl Default for OffChainValidatorConfig {
fn default() -> Self {
Self {
verify_withdrawal_signature: true,
verify_tx_signature: true,
verify_tx_execution: true,
verify_max_cycles: 70_000_000,
dump_tx_on_failure: true,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct P2PNetworkConfig {
/// Multiaddr listen address, e.g. /ip4/1.2.3.4/tcp/443
pub listen: Option<String>,
/// Multiaddr dial addresses, e.g. /ip4/1.2.3.4/tcp/443
#[serde(default)]
pub dial: Vec<String>,
pub secret_key_path: Option<PathBuf>,
pub allowed_peer_ids: Option<Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SyncServerConfig {
pub buffer_capacity: u64,
pub broadcast_channel_capacity: usize,
}
impl Default for SyncServerConfig {
fn default() -> Self {
Self {
buffer_capacity: 16,
broadcast_channel_capacity: 1024,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MemPoolConfig {
pub execute_l2tx_max_cycles: u64,
#[serde(default = "default_restore_path")]
pub restore_path: PathBuf,
#[serde(default)]
pub mem_block: MemBlockConfig,
pub fee: FeeConfig,
pub extra: MemPoolExtraConfig,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MemBlockConfig {
pub max_deposits: usize,
pub max_withdrawals: usize,
pub max_txs: usize,
#[serde(flatten)]
pub deposit_timeout_config: DepositTimeoutConfig,
#[serde(
default = "default_max_block_cycles_limit",
with = "toml_u64_serde_workaround"
)]
pub max_cycles_limit: u64,
#[serde(default = "default_syscall_cycles")]
pub syscall_cycles: SyscallCyclesConfig,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct DepositTimeoutConfig {
/// Only package deposits whose block timeout >= deposit_block_timeout.
pub deposit_block_timeout: u64,
/// Only package deposits whose timestamp timeout >= deposit_timestamp_timeout.
pub deposit_timestamp_timeout: u64,
/// Only package deposits whose epoch timeout >= deposit_epoch_timeout.
pub deposit_epoch_timeout: u64,
/// Only package deposits whose block number <= tip - deposit_minimum_blocks.
pub deposit_minimal_blocks: u64,
}
impl Default for DepositTimeoutConfig {
fn default() -> Self {
Self {
// 150 blocks, ~20 minutes.
deposit_block_timeout: 150,
// 20 minutes.
deposit_timestamp_timeout: 1_200_000,
// 1 epoch, about 4 hours, this option is supposed not actually used, so we simply set a value
deposit_epoch_timeout: 1,
deposit_minimal_blocks: 0,
}
}
}
const fn default_max_block_cycles_limit() -> u64 {
u64::MAX
}
fn default_syscall_cycles() -> SyscallCyclesConfig {
SyscallCyclesConfig::default()
}
// Workaround: https://github.com/alexcrichton/toml-rs/issues/256
// Serialize to string instead
mod toml_u64_serde_workaround {
use std::borrow::Cow;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(val: &u64, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&val.to_string())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
let s: Cow<str> = Deserialize::deserialize(deserializer)?;
s.parse::<u64>().map_err(serde::de::Error::custom)
}
}
// Field default value for backward config file compitability
fn default_restore_path() -> PathBuf {
const DEFAULT_RESTORE_PATH: &str = "mem_block";
DEFAULT_RESTORE_PATH.into()
}
impl Default for MemPoolConfig {
fn default() -> Self {
Self {
execute_l2tx_max_cycles: 100_000_000,
restore_path: default_restore_path(),
mem_block: MemBlockConfig::default(),
fee: Default::default(),
extra: Default::default(),
}
}
}
impl Default for MemBlockConfig {
fn default() -> Self {
Self {
max_deposits: 100,
max_withdrawals: 100,
max_txs: 1000,
deposit_timeout_config: Default::default(),
max_cycles_limit: default_max_block_cycles_limit(),
syscall_cycles: SyscallCyclesConfig::default(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum NodeMode {
FullNode,
Test,
#[default]
ReadOnly,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DBBlockValidatorConfig {
pub verify_max_cycles: u64,
pub parallel_verify_blocks: bool,
pub replace_scripts: Option<HashMap<H256, PathBuf>>,
pub skip_targets: Option<HashSet<(u64, ChallengeTargetType, u32)>>,
}
impl Default for DBBlockValidatorConfig {
fn default() -> Self {
Self {
verify_max_cycles: 7000_0000,
replace_scripts: None,
skip_targets: None,
parallel_verify_blocks: true,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StoreConfig {
#[serde(default = "default_store_path")]
pub path: PathBuf,
#[serde(default)]
pub cache_size: Option<usize>,
#[serde(default)]
pub options_file: Option<PathBuf>,
}
fn default_store_path() -> PathBuf {
"./gw-db".into()
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeeConfig {
// fee_rate: fee / cycles limit
pub meta_cycles_limit: u64,
// fee_rate: fee / cycles limit
pub sudt_cycles_limit: u64,
// fee_rate: fee / cycles_limit
pub eth_addr_reg_cycles_limit: u64,
// fee_rate: fee / cycles limit
pub withdraw_cycles_limit: u64,
}
impl FeeConfig {
pub fn minimal_tx_cycles_limit(&self) -> u64 {
min(
min(self.meta_cycles_limit, self.sudt_cycles_limit),
self.eth_addr_reg_cycles_limit,
)
}
}
impl Default for FeeConfig {
fn default() -> Self {
// CKB default weight is 1000 / 1000
Self {
// 20K cycles unified for simple Godwoken native contracts
meta_cycles_limit: 20000,
sudt_cycles_limit: 20000,
withdraw_cycles_limit: 20000,
eth_addr_reg_cycles_limit: 20000, // 1176198 cycles used
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GithubConfigUrl {
pub org: String,
pub repo: String,
pub branch: String,
pub path: String,
pub token: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum ContractLogConfig {
#[default]
Verbose,
Error,
}
// Cycles config for all db related syscalls
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyscallCyclesConfig {
pub sys_store_cycles: u64,
pub sys_load_cycles: u64,
pub sys_create_cycles: u64,
pub sys_load_account_script_cycles: u64,
pub sys_store_data_cycles: u64,
pub sys_load_data_cycles: u64,
pub sys_get_block_hash_cycles: u64,
pub sys_recover_account_cycles: u64,
pub sys_log_cycles: u64,
pub sys_bn_add_cycles: u64,
pub sys_bn_mul_cycles: u64,
pub sys_bn_fixed_pairing_cycles: u64,
pub sys_bn_per_pairing_cycles: u64,
pub sys_snapshot_cycles: u64,
pub sys_revert_cycles: u64,
}
impl Default for SyscallCyclesConfig {
fn default() -> Self {
SyscallCyclesConfig {
sys_store_cycles: 50000,
sys_load_cycles: 5000,
sys_create_cycles: 50000,
sys_load_account_script_cycles: 5000,
sys_store_data_cycles: 50000,
sys_load_data_cycles: 5000,
sys_get_block_hash_cycles: 50000,
sys_recover_account_cycles: 50000,
sys_log_cycles: 50000,
// default cycles of BN operations
// estimated_cycles = 3 * (Gas Cost of EIP-1108)
// see: https://eips.ethereum.org/EIPS/eip-1108
sys_bn_add_cycles: 450,
sys_bn_mul_cycles: 18_000,
sys_bn_fixed_pairing_cycles: 135_000,
sys_bn_per_pairing_cycles: 102_000,
sys_snapshot_cycles: 2000,
sys_revert_cycles: 2000,
}
}
}