-
Notifications
You must be signed in to change notification settings - Fork 637
/
config.rs
1037 lines (935 loc) · 42 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::convert::TryInto;
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use actix;
use actix_web;
use chrono::Utc;
use log::info;
use num_rational::Rational;
use serde::{Deserialize, Serialize};
use lazy_static::lazy_static;
use near_chain_configs::{ClientConfig, Genesis, GenesisConfig};
use near_crypto::{InMemorySigner, KeyFile, KeyType, PublicKey, Signer};
use near_jsonrpc::RpcConfig;
use near_network::test_utils::open_port;
use near_network::types::ROUTED_MESSAGE_TTL;
use near_network::utils::blacklist_from_iter;
use near_network::NetworkConfig;
use near_primitives::account::{AccessKey, Account};
use near_primitives::hash::CryptoHash;
use near_primitives::state_record::StateRecord;
use near_primitives::types::{
AccountId, AccountInfo, Balance, BlockHeightDelta, EpochHeight, Gas, NumBlocks, NumSeats,
NumShards, ShardId,
};
use near_primitives::utils::{generate_random_string, get_num_seats_per_shard};
use near_primitives::validator_signer::{InMemoryValidatorSigner, ValidatorSigner};
use near_primitives::version::PROTOCOL_VERSION;
use near_runtime_configs::RuntimeConfig;
use near_telemetry::TelemetryConfig;
/// Initial balance used in tests.
pub const TESTING_INIT_BALANCE: Balance = 1_000_000_000 * NEAR_BASE;
/// Validator's stake used in tests.
pub const TESTING_INIT_STAKE: Balance = 50_000_000 * NEAR_BASE;
/// One NEAR, divisible by 10^24.
pub const NEAR_BASE: Balance = 1_000_000_000_000_000_000_000_000;
/// Millinear, 1/1000 of NEAR.
pub const MILLI_NEAR: Balance = NEAR_BASE / 1000;
/// Attonear, 1/10^18 of NEAR.
pub const ATTO_NEAR: Balance = 1;
/// Block production tracking delay.
pub const BLOCK_PRODUCTION_TRACKING_DELAY: u64 = 100;
/// Expected block production time in ms.
pub const MIN_BLOCK_PRODUCTION_DELAY: u64 = 600;
/// Maximum time to delay block production without approvals is ms.
pub const MAX_BLOCK_PRODUCTION_DELAY: u64 = 2_000;
/// Maximum time until skipping the previous block is ms.
pub const MAX_BLOCK_WAIT_DELAY: u64 = 6_000;
/// Reduce wait time for every missing block in ms.
const REDUCE_DELAY_FOR_MISSING_BLOCKS: u64 = 100;
/// Horizon at which instead of fetching block, fetch full state.
const BLOCK_FETCH_HORIZON: BlockHeightDelta = 50;
/// Horizon to step from the latest block when fetching state.
const STATE_FETCH_HORIZON: NumBlocks = 5;
/// Behind this horizon header fetch kicks in.
const BLOCK_HEADER_FETCH_HORIZON: BlockHeightDelta = 50;
/// Time between check to perform catchup.
const CATCHUP_STEP_PERIOD: u64 = 100;
/// Time between checking to re-request chunks.
const CHUNK_REQUEST_RETRY_PERIOD: u64 = 400;
/// Expected epoch length.
pub const EXPECTED_EPOCH_LENGTH: BlockHeightDelta = (5 * 60 * 1000) / MIN_BLOCK_PRODUCTION_DELAY;
/// Criterion for kicking out block producers.
pub const BLOCK_PRODUCER_KICKOUT_THRESHOLD: u8 = 90;
/// Criterion for kicking out chunk producers.
pub const CHUNK_PRODUCER_KICKOUT_THRESHOLD: u8 = 90;
/// Fast mode constants for testing/developing.
pub const FAST_MIN_BLOCK_PRODUCTION_DELAY: u64 = 120;
pub const FAST_MAX_BLOCK_PRODUCTION_DELAY: u64 = 500;
pub const FAST_EPOCH_LENGTH: BlockHeightDelta = 60;
/// Time to persist Accounts Id in the router without removing them in seconds.
pub const TTL_ACCOUNT_ID_ROUTER: u64 = 60 * 60;
/// Maximum amount of routes to store for each account id.
pub const MAX_ROUTES_TO_STORE: usize = 5;
/// Expected number of blocks per year
pub const NUM_BLOCKS_PER_YEAR: u64 = 365 * 24 * 60 * 60;
/// Initial gas limit.
pub const INITIAL_GAS_LIMIT: Gas = 1_000_000_000_000_000;
/// Initial gas price.
pub const MIN_GAS_PRICE: Balance = 1_000_000_000;
/// Protocol treasury account
pub const PROTOCOL_TREASURY_ACCOUNT: &str = "near";
/// Fishermen stake threshold.
pub const FISHERMEN_THRESHOLD: Balance = 10 * NEAR_BASE;
/// Number of blocks for which a given transaction is valid
pub const TRANSACTION_VALIDITY_PERIOD: NumBlocks = 100;
/// Number of seats for block producers
pub const NUM_BLOCK_PRODUCER_SEATS: NumSeats = 50;
/// How much height horizon to give to consider peer up to date.
pub const HIGHEST_PEER_HORIZON: u64 = 5;
/// The minimum stake required for staking is last seat price divided by this number.
pub const MINIMUM_STAKE_DIVISOR: u64 = 10;
/// Number of epochs before protocol upgrade.
pub const PROTOCOL_UPGRADE_NUM_EPOCHS: EpochHeight = 2;
pub const CONFIG_FILENAME: &str = "config.json";
pub const GENESIS_CONFIG_FILENAME: &str = "genesis.json";
pub const NODE_KEY_FILE: &str = "node_key.json";
pub const VALIDATOR_KEY_FILE: &str = "validator_key.json";
pub const MAINNET_TELEMETRY_URL: &str = "https://explorer.mainnet.near.org/api/nodes";
pub const NETWORK_TELEMETRY_URL: &str = "https://explorer.{}.near.org/api/nodes";
lazy_static! {
/// The rate at which the gas price can be adjusted (alpha in the formula).
/// The formula is
/// gas_price_t = gas_price_{t-1} * (1 + (gas_used/gas_limit - 1/2) * alpha))
pub static ref GAS_PRICE_ADJUSTMENT_RATE: Rational = Rational::new(1, 100);
/// Protocol treasury reward
pub static ref PROTOCOL_REWARD_RATE: Rational = Rational::new(1, 10);
/// Maximum inflation rate per year
pub static ref MAX_INFLATION_RATE: Rational = Rational::new(5, 100);
/// Protocol upgrade stake threshold.
pub static ref PROTOCOL_UPGRADE_STAKE_THRESHOLD: Rational = Rational::new(8, 10);
}
/// Maximum number of active peers. Hard limit.
fn default_max_num_peers() -> u32 {
40
}
/// Minimum outbound connections a peer should have to avoid eclipse attacks.
fn default_minimum_outbound_connections() -> u32 {
5
}
/// Lower bound of the ideal number of connections.
fn default_ideal_connections_lo() -> u32 {
30
}
/// Upper bound of the ideal number of connections.
fn default_ideal_connections_hi() -> u32 {
35
}
/// Peers which last message is was within this period of time are considered active recent peers.
fn default_peer_recent_time_window() -> Duration {
Duration::from_secs(600)
}
/// Number of peers to keep while removing a connection.
/// Used to avoid disconnecting from peers we have been connected since long time.
fn default_safe_set_size() -> u32 {
20
}
/// Time to persist Accounts Id in the router without removing them in seconds.
fn default_ttl_account_id_router() -> Duration {
Duration::from_secs(TTL_ACCOUNT_ID_ROUTER)
}
/// Period to check on peer status
fn default_peer_stats_period() -> Duration {
Duration::from_secs(5)
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Network {
/// Address to listen for incoming connections.
pub addr: String,
/// Address to advertise to peers for them to connect.
/// If empty, will use the same port as the addr, and will introspect on the listener.
pub external_address: String,
/// Comma separated list of nodes to connect to.
pub boot_nodes: String,
/// Maximum number of active peers. Hard limit.
#[serde(default = "default_max_num_peers")]
pub max_num_peers: u32,
/// Minimum outbound connections a peer should have to avoid eclipse attacks.
#[serde(default = "default_minimum_outbound_connections")]
pub minimum_outbound_peers: u32,
/// Lower bound of the ideal number of connections.
#[serde(default = "default_ideal_connections_lo")]
pub ideal_connections_lo: u32,
/// Upper bound of the ideal number of connections.
#[serde(default = "default_ideal_connections_hi")]
pub ideal_connections_hi: u32,
/// Peers which last message is was within this period of time are considered active recent peers (in seconds).
#[serde(default = "default_peer_recent_time_window")]
pub peer_recent_time_window: Duration,
/// Number of peers to keep while removing a connection.
/// Used to avoid disconnecting from peers we have been connected since long time.
#[serde(default = "default_safe_set_size")]
pub safe_set_size: u32,
/// Handshake timeout.
pub handshake_timeout: Duration,
/// Duration before trying to reconnect to a peer.
pub reconnect_delay: Duration,
/// Skip waiting for peers before starting node.
pub skip_sync_wait: bool,
/// Ban window for peers who misbehave.
pub ban_window: Duration,
/// List of addresses that will not be accepted as valid neighbors.
/// It can be IP:Port or IP (to blacklist all connections coming from this address).
#[serde(default)]
pub blacklist: Vec<String>,
/// Time to persist Accounts Id in the router without removing them in seconds.
#[serde(default = "default_ttl_account_id_router")]
pub ttl_account_id_router: Duration,
/// Period to check on peer status
#[serde(default = "default_peer_stats_period")]
pub peer_stats_period: Duration,
}
impl Default for Network {
fn default() -> Self {
Network {
addr: "0.0.0.0:24567".to_string(),
external_address: "".to_string(),
boot_nodes: "".to_string(),
max_num_peers: default_max_num_peers(),
minimum_outbound_peers: default_minimum_outbound_connections(),
ideal_connections_lo: default_ideal_connections_lo(),
ideal_connections_hi: default_ideal_connections_hi(),
peer_recent_time_window: default_peer_recent_time_window(),
safe_set_size: default_safe_set_size(),
handshake_timeout: Duration::from_secs(20),
reconnect_delay: Duration::from_secs(60),
skip_sync_wait: false,
ban_window: Duration::from_secs(3 * 60 * 60),
blacklist: vec![],
ttl_account_id_router: default_ttl_account_id_router(),
peer_stats_period: default_peer_stats_period(),
}
}
}
/// Serde default only supports functions without parameters.
fn default_reduce_wait_for_missing_block() -> Duration {
Duration::from_millis(REDUCE_DELAY_FOR_MISSING_BLOCKS)
}
fn default_header_sync_initial_timeout() -> Duration {
Duration::from_secs(10)
}
fn default_header_sync_progress_timeout() -> Duration {
Duration::from_secs(2)
}
fn default_header_sync_stall_ban_timeout() -> Duration {
Duration::from_secs(120)
}
fn default_header_sync_expected_height_per_second() -> u64 {
10
}
fn default_sync_check_period() -> Duration {
Duration::from_secs(10)
}
fn default_sync_step_period() -> Duration {
Duration::from_millis(10)
}
fn default_gc_blocks_limit() -> NumBlocks {
2
}
fn default_view_client_threads() -> usize {
4
}
fn default_doomslug_step_period() -> Duration {
Duration::from_millis(100)
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Consensus {
/// Minimum number of peers to start syncing.
pub min_num_peers: usize,
/// Duration to check for producing / skipping block.
pub block_production_tracking_delay: Duration,
/// Minimum duration before producing block.
pub min_block_production_delay: Duration,
/// Maximum wait for approvals before producing block.
pub max_block_production_delay: Duration,
/// Maximum duration before skipping given height.
pub max_block_wait_delay: Duration,
/// Duration to reduce the wait for each missed block by validator.
#[serde(default = "default_reduce_wait_for_missing_block")]
pub reduce_wait_for_missing_block: Duration,
/// Produce empty blocks, use `false` for testing.
pub produce_empty_blocks: bool,
/// Horizon at which instead of fetching block, fetch full state.
pub block_fetch_horizon: BlockHeightDelta,
/// Horizon to step from the latest block when fetching state.
pub state_fetch_horizon: NumBlocks,
/// Behind this horizon header fetch kicks in.
pub block_header_fetch_horizon: BlockHeightDelta,
/// Time between check to perform catchup.
pub catchup_step_period: Duration,
/// Time between checking to re-request chunks.
pub chunk_request_retry_period: Duration,
/// How much time to wait after initial header sync
#[serde(default = "default_header_sync_initial_timeout")]
pub header_sync_initial_timeout: Duration,
/// How much time to wait after some progress is made in header sync
#[serde(default = "default_header_sync_progress_timeout")]
pub header_sync_progress_timeout: Duration,
/// How much time to wait before banning a peer in header sync if sync is too slow
#[serde(default = "default_header_sync_stall_ban_timeout")]
pub header_sync_stall_ban_timeout: Duration,
/// Expected increase of header head weight per second during header sync
#[serde(default = "default_header_sync_expected_height_per_second")]
pub header_sync_expected_height_per_second: u64,
/// How frequently we check whether we need to sync
#[serde(default = "default_sync_check_period")]
pub sync_check_period: Duration,
/// During sync the time we wait before reentering the sync loop
#[serde(default = "default_sync_step_period")]
pub sync_step_period: Duration,
/// Time between running doomslug timer.
#[serde(default = "default_doomslug_step_period")]
pub doomslug_step_period: Duration,
}
impl Default for Consensus {
fn default() -> Self {
Consensus {
min_num_peers: 3,
block_production_tracking_delay: Duration::from_millis(BLOCK_PRODUCTION_TRACKING_DELAY),
min_block_production_delay: Duration::from_millis(MIN_BLOCK_PRODUCTION_DELAY),
max_block_production_delay: Duration::from_millis(MAX_BLOCK_PRODUCTION_DELAY),
max_block_wait_delay: Duration::from_millis(MAX_BLOCK_WAIT_DELAY),
reduce_wait_for_missing_block: default_reduce_wait_for_missing_block(),
produce_empty_blocks: true,
block_fetch_horizon: BLOCK_FETCH_HORIZON,
state_fetch_horizon: STATE_FETCH_HORIZON,
block_header_fetch_horizon: BLOCK_HEADER_FETCH_HORIZON,
catchup_step_period: Duration::from_millis(CATCHUP_STEP_PERIOD),
chunk_request_retry_period: Duration::from_millis(CHUNK_REQUEST_RETRY_PERIOD),
header_sync_initial_timeout: default_header_sync_initial_timeout(),
header_sync_progress_timeout: default_header_sync_progress_timeout(),
header_sync_stall_ban_timeout: default_header_sync_stall_ban_timeout(),
header_sync_expected_height_per_second: default_header_sync_expected_height_per_second(
),
sync_check_period: default_sync_check_period(),
sync_step_period: default_sync_step_period(),
doomslug_step_period: default_doomslug_step_period(),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Config {
pub genesis_file: String,
pub genesis_records_file: Option<String>,
pub validator_key_file: String,
pub node_key_file: String,
pub rpc: RpcConfig,
pub telemetry: TelemetryConfig,
pub network: Network,
pub consensus: Consensus,
pub tracked_accounts: Vec<AccountId>,
pub tracked_shards: Vec<ShardId>,
pub archive: bool,
#[serde(default = "default_gc_blocks_limit")]
pub gc_blocks_limit: NumBlocks,
#[serde(default = "default_view_client_threads")]
pub view_client_threads: usize,
}
impl Default for Config {
fn default() -> Self {
Config {
genesis_file: GENESIS_CONFIG_FILENAME.to_string(),
genesis_records_file: None,
validator_key_file: VALIDATOR_KEY_FILE.to_string(),
node_key_file: NODE_KEY_FILE.to_string(),
rpc: RpcConfig::default(),
telemetry: TelemetryConfig::default(),
network: Network::default(),
consensus: Consensus::default(),
tracked_accounts: vec![],
tracked_shards: vec![],
archive: false,
gc_blocks_limit: default_gc_blocks_limit(),
view_client_threads: 4,
}
}
}
impl Config {
pub fn from_file(path: &PathBuf) -> Self {
let mut file = File::open(path).expect("Could not open config file.");
let mut content = String::new();
file.read_to_string(&mut content).expect("Could not read from config file.");
Config::from(content.as_str())
}
pub fn write_to_file(&self, path: &PathBuf) {
let mut file = File::create(path).expect("Failed to create / write a config file.");
let str = serde_json::to_string_pretty(self).expect("Error serializing the config.");
if let Err(err) = file.write_all(str.as_bytes()) {
panic!("Failed to write a config file {}", err);
}
}
}
impl From<&str> for Config {
fn from(content: &str) -> Self {
serde_json::from_str(content).expect("Failed to deserialize config")
}
}
#[easy_ext::ext(GenesisExt)]
impl Genesis {
pub fn test_with_seeds(
seeds: Vec<&str>,
num_validator_seats: NumSeats,
num_validator_seats_per_shard: Vec<NumSeats>,
) -> Self {
let mut validators = vec![];
let mut records = vec![];
for (i, &account) in seeds.iter().enumerate() {
let signer = InMemorySigner::from_seed(account, KeyType::ED25519, account);
let i = i as u64;
if i < num_validator_seats {
validators.push(AccountInfo {
account_id: account.to_string(),
public_key: signer.public_key.clone(),
amount: TESTING_INIT_STAKE,
});
}
records.extend(
state_records_account_with_key(
account,
&signer.public_key.clone(),
TESTING_INIT_BALANCE
- if i < num_validator_seats { TESTING_INIT_STAKE } else { 0 },
if i < num_validator_seats { TESTING_INIT_STAKE } else { 0 },
CryptoHash::default(),
)
.into_iter(),
);
}
add_protocol_account(&mut records);
let config = GenesisConfig {
protocol_version: PROTOCOL_VERSION,
genesis_time: Utc::now(),
chain_id: random_chain_id(),
num_block_producer_seats: num_validator_seats,
num_block_producer_seats_per_shard: num_validator_seats_per_shard.clone(),
avg_hidden_validator_seats_per_shard: vec![0; num_validator_seats_per_shard.len()],
dynamic_resharding: false,
protocol_upgrade_stake_threshold: *PROTOCOL_UPGRADE_STAKE_THRESHOLD,
protocol_upgrade_num_epochs: PROTOCOL_UPGRADE_NUM_EPOCHS,
epoch_length: FAST_EPOCH_LENGTH,
gas_limit: INITIAL_GAS_LIMIT,
gas_price_adjustment_rate: *GAS_PRICE_ADJUSTMENT_RATE,
block_producer_kickout_threshold: BLOCK_PRODUCER_KICKOUT_THRESHOLD,
validators,
protocol_reward_rate: *PROTOCOL_REWARD_RATE,
max_inflation_rate: *MAX_INFLATION_RATE,
num_blocks_per_year: NUM_BLOCKS_PER_YEAR,
protocol_treasury_account: PROTOCOL_TREASURY_ACCOUNT.to_string(),
transaction_validity_period: TRANSACTION_VALIDITY_PERIOD,
chunk_producer_kickout_threshold: CHUNK_PRODUCER_KICKOUT_THRESHOLD,
fishermen_threshold: FISHERMEN_THRESHOLD,
min_gas_price: MIN_GAS_PRICE,
..Default::default()
};
Genesis::new(config, records.into())
}
pub fn test(seeds: Vec<&str>, num_validator_seats: NumSeats) -> Self {
Self::test_with_seeds(seeds, num_validator_seats, vec![num_validator_seats])
}
pub fn test_free(seeds: Vec<&str>, num_validator_seats: NumSeats) -> Self {
let mut genesis =
Self::test_with_seeds(seeds, num_validator_seats, vec![num_validator_seats]);
genesis.config.runtime_config = RuntimeConfig::free();
genesis
}
pub fn test_sharded(
seeds: Vec<&str>,
num_validator_seats: NumSeats,
num_validator_seats_per_shard: Vec<NumSeats>,
) -> Self {
Self::test_with_seeds(seeds, num_validator_seats, num_validator_seats_per_shard)
}
}
#[derive(Clone)]
pub struct NearConfig {
config: Config,
pub client_config: ClientConfig,
pub network_config: NetworkConfig,
pub rpc_config: RpcConfig,
pub telemetry_config: TelemetryConfig,
pub genesis: Arc<Genesis>,
pub validator_signer: Option<Arc<dyn ValidatorSigner>>,
}
impl NearConfig {
pub fn new(
config: Config,
genesis: Arc<Genesis>,
network_key_pair: KeyFile,
validator_signer: Option<Arc<dyn ValidatorSigner>>,
) -> Self {
NearConfig {
config: config.clone(),
client_config: ClientConfig {
version: Default::default(),
chain_id: genesis.config.chain_id.clone(),
rpc_addr: config.rpc.addr.clone(),
block_production_tracking_delay: config.consensus.block_production_tracking_delay,
min_block_production_delay: config.consensus.min_block_production_delay,
max_block_production_delay: config.consensus.max_block_production_delay,
max_block_wait_delay: config.consensus.max_block_wait_delay,
reduce_wait_for_missing_block: config.consensus.reduce_wait_for_missing_block,
skip_sync_wait: config.network.skip_sync_wait,
sync_check_period: config.consensus.sync_check_period,
sync_step_period: config.consensus.sync_step_period,
sync_height_threshold: 1,
header_sync_initial_timeout: config.consensus.header_sync_initial_timeout,
header_sync_progress_timeout: config.consensus.header_sync_progress_timeout,
header_sync_stall_ban_timeout: config.consensus.header_sync_stall_ban_timeout,
header_sync_expected_height_per_second: config
.consensus
.header_sync_expected_height_per_second,
min_num_peers: config.consensus.min_num_peers,
log_summary_period: Duration::from_secs(10),
produce_empty_blocks: config.consensus.produce_empty_blocks,
epoch_length: genesis.config.epoch_length,
num_block_producer_seats: genesis.config.num_block_producer_seats,
announce_account_horizon: genesis.config.epoch_length / 2,
ttl_account_id_router: config.network.ttl_account_id_router,
// TODO(1047): this should be adjusted depending on the speed of sync of state.
block_fetch_horizon: config.consensus.block_fetch_horizon,
state_fetch_horizon: config.consensus.state_fetch_horizon,
block_header_fetch_horizon: config.consensus.block_header_fetch_horizon,
catchup_step_period: config.consensus.catchup_step_period,
chunk_request_retry_period: config.consensus.chunk_request_retry_period,
doosmslug_step_period: config.consensus.doomslug_step_period,
tracked_accounts: config.tracked_accounts,
tracked_shards: config.tracked_shards,
archive: config.archive,
gc_blocks_limit: config.gc_blocks_limit,
view_client_threads: config.view_client_threads,
},
network_config: NetworkConfig {
public_key: network_key_pair.public_key,
secret_key: network_key_pair.secret_key,
account_id: validator_signer.as_ref().map(|vs| vs.validator_id().clone()),
addr: if config.network.addr.is_empty() {
None
} else {
Some(config.network.addr.parse().unwrap())
},
boot_nodes: if config.network.boot_nodes.is_empty() {
vec![]
} else {
config
.network
.boot_nodes
.split(',')
.map(|chunk| chunk.try_into().expect("Failed to parse PeerInfo"))
.collect()
},
handshake_timeout: config.network.handshake_timeout,
reconnect_delay: config.network.reconnect_delay,
bootstrap_peers_period: Duration::from_secs(60),
max_num_peers: config.network.max_num_peers,
minimum_outbound_peers: config.network.minimum_outbound_peers,
ideal_connections_lo: config.network.ideal_connections_lo,
ideal_connections_hi: config.network.ideal_connections_hi,
peer_recent_time_window: config.network.peer_recent_time_window,
safe_set_size: config.network.safe_set_size,
ban_window: config.network.ban_window,
max_send_peers: 512,
peer_expiration_duration: Duration::from_secs(7 * 24 * 60 * 60),
peer_stats_period: Duration::from_secs(5),
ttl_account_id_router: config.network.ttl_account_id_router,
routed_message_ttl: ROUTED_MESSAGE_TTL,
max_routes_to_store: MAX_ROUTES_TO_STORE,
highest_peer_horizon: HIGHEST_PEER_HORIZON,
push_info_period: Duration::from_millis(100),
blacklist: blacklist_from_iter(config.network.blacklist),
outbound_disabled: false,
},
telemetry_config: config.telemetry,
rpc_config: config.rpc,
genesis,
validator_signer,
}
}
}
impl NearConfig {
/// Test tool to save configs back to the folder.
/// Useful for dynamic creating testnet configs and then saving them in different folders.
pub fn save_to_dir(&self, dir: &Path) {
fs::create_dir_all(dir).expect("Failed to create directory");
self.config.write_to_file(&dir.join(CONFIG_FILENAME));
if let Some(validator_signer) = &self.validator_signer {
validator_signer.write_to_file(&dir.join(&self.config.validator_key_file));
}
let network_signer =
InMemorySigner::from_secret_key("".to_string(), self.network_config.secret_key.clone());
network_signer.write_to_file(&dir.join(&self.config.node_key_file));
self.genesis.to_file(&dir.join(&self.config.genesis_file));
}
}
fn add_protocol_account(records: &mut Vec<StateRecord>) {
let signer = InMemorySigner::from_seed(
PROTOCOL_TREASURY_ACCOUNT,
KeyType::ED25519,
PROTOCOL_TREASURY_ACCOUNT,
);
records.extend(state_records_account_with_key(
PROTOCOL_TREASURY_ACCOUNT,
&signer.public_key,
TESTING_INIT_BALANCE,
0,
CryptoHash::default(),
));
}
fn random_chain_id() -> String {
format!("test-chain-{}", generate_random_string(5))
}
fn state_records_account_with_key(
account_id: &str,
public_key: &PublicKey,
amount: u128,
staked: u128,
code_hash: CryptoHash,
) -> Vec<StateRecord> {
vec![
StateRecord::Account {
account_id: account_id.to_string(),
account: Account { amount, locked: staked, code_hash, storage_usage: 0 },
},
StateRecord::AccessKey {
account_id: account_id.to_string(),
public_key: public_key.clone(),
access_key: AccessKey::full_access(),
},
]
}
/// Initializes genesis and client configs and stores in the given folder
pub fn init_configs(
dir: &Path,
chain_id: Option<&str>,
account_id: Option<&str>,
test_seed: Option<&str>,
num_shards: NumShards,
fast: bool,
genesis: Option<&str>,
download: bool,
download_genesis_url: Option<&str>,
) {
fs::create_dir_all(dir).expect("Failed to create directory");
// Check if config already exists in home dir.
if dir.join(CONFIG_FILENAME).exists() {
let config = Config::from_file(&dir.join(CONFIG_FILENAME));
let genesis_config = GenesisConfig::from_file(&dir.join(config.genesis_file));
panic!("Found existing config in {} with chain-id = {}. Use unsafe_reset_all to clear the folder.", dir.to_str().unwrap(), genesis_config.chain_id);
}
let chain_id = chain_id
.and_then(|c| if c.is_empty() { None } else { Some(c.to_string()) })
.unwrap_or_else(random_chain_id);
match chain_id.as_ref() {
"mainnet" => {
if test_seed.is_some() {
panic!("Test seed is not supported for MainNet");
}
let mut config = Config::default();
config.telemetry.endpoints.push(MAINNET_TELEMETRY_URL.to_string());
config.write_to_file(&dir.join(CONFIG_FILENAME));
// TODO: add download genesis for mainnet
let genesis: Genesis = serde_json::from_str(
&std::str::from_utf8(include_bytes!("../res/mainnet_genesis.json"))
.expect("Failed to convert genesis file into string"),
)
.expect("Failed to deserialize MainNet genesis");
let network_signer = InMemorySigner::from_random("".to_string(), KeyType::ED25519);
network_signer.write_to_file(&dir.join(config.node_key_file));
genesis.to_file(&dir.join(config.genesis_file));
info!(target: "near", "Generated MainNet genesis file in {}", dir.to_str().unwrap());
}
"testnet" | "betanet" => {
if test_seed.is_some() {
panic!("Test seed is not supported for official TestNet");
}
let mut config = Config::default();
config.telemetry.endpoints.push(NETWORK_TELEMETRY_URL.replace("{}", &chain_id));
config.write_to_file(&dir.join(CONFIG_FILENAME));
// If account id was given, create new key pair for this validator.
if let Some(account_id) =
account_id.and_then(|x| if x.is_empty() { None } else { Some(x.to_string()) })
{
let signer =
InMemoryValidatorSigner::from_random(account_id.clone(), KeyType::ED25519);
info!(target: "near", "Use key {} for {} to stake.", signer.public_key(), account_id);
signer.write_to_file(&dir.join(config.validator_key_file));
}
let network_signer = InMemorySigner::from_random("".to_string(), KeyType::ED25519);
network_signer.write_to_file(&dir.join(config.node_key_file));
// download genesis from s3
let genesis_path = dir.join("genesis.json");
let mut genesis_path_str =
genesis_path.to_str().expect("Genesis path must be initialized");
if let Some(url) = download_genesis_url {
download_genesis(&url.to_string(), &genesis_path);
} else if download {
let url = get_genesis_url(&chain_id);
download_genesis(&url, &genesis_path);
} else {
genesis_path_str =
genesis.unwrap_or_else(|| panic!("Genesis file is required for {}.", &chain_id))
}
let mut genesis = Genesis::from_file(&genesis_path_str);
genesis.config.chain_id = chain_id.clone();
genesis.to_file(&dir.join(config.genesis_file));
info!(target: "near", "Generated for {} network node key and genesis file in {}", chain_id, dir.to_str().unwrap());
}
_ => {
// Create new configuration, key files and genesis for one validator.
let mut config = Config::default();
config.network.skip_sync_wait = true;
if fast {
config.consensus.min_block_production_delay =
Duration::from_millis(FAST_MIN_BLOCK_PRODUCTION_DELAY);
config.consensus.max_block_production_delay =
Duration::from_millis(FAST_MAX_BLOCK_PRODUCTION_DELAY);
}
config.write_to_file(&dir.join(CONFIG_FILENAME));
let account_id = account_id
.and_then(|x| if x.is_empty() { None } else { Some(x) })
.unwrap_or("test.near")
.to_string();
let signer = if let Some(test_seed) = test_seed {
InMemoryValidatorSigner::from_seed(&account_id, KeyType::ED25519, test_seed)
} else {
InMemoryValidatorSigner::from_random(account_id.clone(), KeyType::ED25519)
};
signer.write_to_file(&dir.join(config.validator_key_file));
let network_signer = InMemorySigner::from_random("".to_string(), KeyType::ED25519);
network_signer.write_to_file(&dir.join(config.node_key_file));
let mut records = state_records_account_with_key(
&account_id,
&signer.public_key(),
TESTING_INIT_BALANCE,
TESTING_INIT_STAKE,
CryptoHash::default(),
);
add_protocol_account(&mut records);
let genesis_config = GenesisConfig {
protocol_version: PROTOCOL_VERSION,
genesis_time: Utc::now(),
chain_id,
genesis_height: 0,
num_block_producer_seats: NUM_BLOCK_PRODUCER_SEATS,
num_block_producer_seats_per_shard: get_num_seats_per_shard(
num_shards,
NUM_BLOCK_PRODUCER_SEATS,
),
avg_hidden_validator_seats_per_shard: (0..num_shards).map(|_| 0).collect(),
dynamic_resharding: false,
protocol_upgrade_stake_threshold: *PROTOCOL_UPGRADE_STAKE_THRESHOLD,
protocol_upgrade_num_epochs: PROTOCOL_UPGRADE_NUM_EPOCHS,
epoch_length: if fast { FAST_EPOCH_LENGTH } else { EXPECTED_EPOCH_LENGTH },
gas_limit: INITIAL_GAS_LIMIT,
gas_price_adjustment_rate: *GAS_PRICE_ADJUSTMENT_RATE,
block_producer_kickout_threshold: BLOCK_PRODUCER_KICKOUT_THRESHOLD,
chunk_producer_kickout_threshold: CHUNK_PRODUCER_KICKOUT_THRESHOLD,
online_max_threshold: Rational::new(99, 100),
online_min_threshold: Rational::new(BLOCK_PRODUCER_KICKOUT_THRESHOLD as isize, 100),
runtime_config: Default::default(),
validators: vec![AccountInfo {
account_id: account_id.clone(),
public_key: signer.public_key(),
amount: TESTING_INIT_STAKE,
}],
transaction_validity_period: TRANSACTION_VALIDITY_PERIOD,
protocol_reward_rate: *PROTOCOL_REWARD_RATE,
max_inflation_rate: *MAX_INFLATION_RATE,
total_supply: 0,
num_blocks_per_year: NUM_BLOCKS_PER_YEAR,
protocol_treasury_account: account_id,
fishermen_threshold: FISHERMEN_THRESHOLD,
min_gas_price: MIN_GAS_PRICE,
..Default::default()
};
let genesis = Genesis::new(genesis_config, records.into());
genesis.to_file(&dir.join(config.genesis_file));
info!(target: "near", "Generated node key, validator key, genesis file in {}", dir.to_str().unwrap());
}
}
}
pub fn create_testnet_configs_from_seeds(
seeds: Vec<String>,
num_shards: NumShards,
num_non_validator_seats: NumSeats,
local_ports: bool,
archive: bool,
) -> (Vec<Config>, Vec<InMemoryValidatorSigner>, Vec<InMemorySigner>, Genesis) {
let num_validator_seats = (seeds.len() - num_non_validator_seats as usize) as NumSeats;
let validator_signers = seeds
.iter()
.map(|seed| InMemoryValidatorSigner::from_seed(seed, KeyType::ED25519, seed))
.collect::<Vec<_>>();
let network_signers = seeds
.iter()
.map(|seed| InMemorySigner::from_seed("", KeyType::ED25519, seed))
.collect::<Vec<_>>();
let genesis = Genesis::test_sharded(
seeds.iter().map(|s| s.as_str()).collect(),
num_validator_seats,
get_num_seats_per_shard(num_shards, num_validator_seats),
);
let mut configs = vec![];
let first_node_port = open_port();
for i in 0..seeds.len() {
let mut config = Config::default();
config.consensus.min_block_production_delay = Duration::from_millis(600);
config.consensus.max_block_production_delay = Duration::from_millis(2000);
if local_ports {
config.network.addr =
format!("127.0.0.1:{}", if i == 0 { first_node_port } else { open_port() });
config.rpc.addr = format!("127.0.0.1:{}", open_port());
config.network.boot_nodes = if i == 0 {
"".to_string()
} else {
format!("{}@127.0.0.1:{}", network_signers[0].public_key, first_node_port)
};
config.network.skip_sync_wait = num_validator_seats == 1;
}
config.archive = archive;
config.consensus.min_num_peers =
std::cmp::min(num_validator_seats as usize - 1, config.consensus.min_num_peers);
configs.push(config);
}
(configs, validator_signers, network_signers, genesis)
}
/// Create testnet configuration. If `local_ports` is true,
/// sets up new ports for all nodes except the first one and sets boot node to it.
pub fn create_testnet_configs(
num_shards: NumShards,
num_validator_seats: NumSeats,
num_non_validator_seats: NumSeats,
prefix: &str,
local_ports: bool,
archive: bool,
) -> (Vec<Config>, Vec<InMemoryValidatorSigner>, Vec<InMemorySigner>, Genesis) {
create_testnet_configs_from_seeds(
(0..(num_validator_seats + num_non_validator_seats))
.map(|i| format!("{}{}", prefix, i))
.collect::<Vec<_>>(),
num_shards,
num_non_validator_seats,
local_ports,
archive,
)
}
pub fn init_testnet_configs(
dir: &Path,
num_shards: NumShards,
num_validator_seats: NumSeats,
num_non_validator_seats: NumSeats,
prefix: &str,
archive: bool,
) {
let (configs, validator_signers, network_signers, genesis) = create_testnet_configs(
num_shards,
num_validator_seats,
num_non_validator_seats,
prefix,
false,
archive,
);
for i in 0..(num_validator_seats + num_non_validator_seats) as usize {
let node_dir = dir.join(format!("{}{}", prefix, i));
fs::create_dir_all(node_dir.clone()).expect("Failed to create directory");
validator_signers[i].write_to_file(&node_dir.join(&configs[i].validator_key_file));
network_signers[i].write_to_file(&node_dir.join(&configs[i].node_key_file));
genesis.to_file(&node_dir.join(&configs[i].genesis_file));
configs[i].write_to_file(&node_dir.join(CONFIG_FILENAME));
info!(target: "near", "Generated node key, validator key, genesis file in {}", node_dir.display());
}
}
pub fn get_genesis_url(chain_id: &String) -> String {
format!(
"https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/{}/genesis.json",
chain_id,
)
}
pub fn download_genesis(url: &String, path: &PathBuf) {
info!(target: "near", "Downloading config file from: {} ...", url);
let url = url.clone();
let path = path.clone();
actix::System::builder().build().block_on(async move {
let client = actix_web::client::Client::new();
let mut response =
client.get(url).send().await.expect("Unable to download the genesis file");
// IMPORTANT: limit specifies the maximum size of the genesis
// In case where the genesis is bigger than the specified limit Overflow Error is thrown
let body = response
.body()
.limit(2_500_000_000)
.await
.expect("Genesis file is bigger than 2.5GB. Please make the limit higher.");
std::fs::write(&path, &body).expect("Failed to create / write a config file.");
info!(target: "near", "Saved the config file to: {} ...", path.as_path().display());
});
}
pub fn load_config(dir: &Path) -> NearConfig {
let config = Config::from_file(&dir.join(CONFIG_FILENAME));
let genesis = if let Some(ref genesis_records_file) = config.genesis_records_file {
Genesis::from_files(&dir.join(&config.genesis_file), &dir.join(genesis_records_file))
} else {
Genesis::from_file(&dir.join(&config.genesis_file))
};
let validator_signer = if dir.join(&config.validator_key_file).exists() {
let signer =
Arc::new(InMemoryValidatorSigner::from_file(&dir.join(&config.validator_key_file)))
as Arc<dyn ValidatorSigner>;
Some(signer)
} else {
None
};
let network_signer = InMemorySigner::from_file(&dir.join(&config.node_key_file));
NearConfig::new(config, Arc::new(genesis), (&network_signer).into(), validator_signer)
}
pub fn load_test_config(seed: &str, port: u16, genesis: Arc<Genesis>) -> NearConfig {
let mut config = Config::default();
config.network.addr = format!("0.0.0.0:{}", port);