1use core::fmt;
5use core::str::FromStr;
6use core::time::Duration;
7#[cfg(feature = "std")]
8use std::time::{SystemTime, UNIX_EPOCH};
9
10use crate::hash::Hash256;
11use crate::hex::{self, ParseHexError};
12#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
14pub struct NetworkIdentifier(pub u8);
15
16impl NetworkIdentifier {
17 pub const MAINNET: Self = Self(0x68);
19 pub const TESTNET: Self = Self(0x98);
21}
22
23#[derive(Clone, Copy, PartialEq, Eq, Hash)]
26pub struct GenerationHashSeed(pub [u8; 32]);
27
28impl GenerationHashSeed {
29 pub const MAINNET: Self = Self(hex::decode_const(
31 b"57F7DA205008026C776CB6AED843393F04CD458E0AA2D9F1D5F31A402072B2D6",
32 ));
33 pub const TESTNET: Self = Self(hex::decode_const(
35 b"49D6E1CE276A85B70EAFE52349AACCA389302E7A9754BCF1221E79494FC665A4",
36 ));
37}
38
39impl fmt::Display for GenerationHashSeed {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 f.write_str(&hex::encode(&self.0))
42 }
43}
44
45impl fmt::Debug for GenerationHashSeed {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(f, "GenerationHashSeed({self})")
48 }
49}
50
51impl FromStr for GenerationHashSeed {
52 type Err = ParseHexError;
53
54 fn from_str(text: &str) -> Result<Self, Self::Err> {
55 hex::decode(text).map(Self)
56 }
57}
58
59#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
62pub struct NetworkTimestamp(pub u64);
63
64impl NetworkTimestamp {
65 pub fn checked_add(self, duration: Duration) -> Option<Self> {
67 let milliseconds = u64::try_from(duration.as_millis()).ok()?;
68 self.0.checked_add(milliseconds).map(Self)
69 }
70
71 pub fn checked_sub(self, duration: Duration) -> Option<Self> {
73 let milliseconds = u64::try_from(duration.as_nanos().div_ceil(1_000_000)).ok()?;
74 self.0.checked_sub(milliseconds).map(Self)
75 }
76
77 pub fn duration_since(self, earlier: Self) -> Option<Duration> {
79 self.0.checked_sub(earlier.0).map(Duration::from_millis)
80 }
81}
82
83#[derive(Clone, Copy, PartialEq, Eq, Debug)]
86pub struct NetworkParameters {
87 pub identifier: NetworkIdentifier,
89 pub generation_hash_seed: GenerationHashSeed,
91 pub epoch_adjustment: Duration,
93 pub currency_mosaic_id: u64,
95 pub block_generation_target_time: Duration,
97 pub block_time_smoothing_factor: u64,
99 pub max_difficulty_blocks: usize,
101 pub total_chain_importance: u64,
103 pub importance_grouping: u64,
105 pub max_transaction_lifetime: Duration,
107 pub max_cosignatures_per_aggregate: usize,
109 pub max_transactions_per_aggregate: usize,
111 pub max_message_size: usize,
113 pub max_hash_lock_duration: Duration,
115 pub max_secret_lock_duration: Duration,
117 pub min_proof_size: usize,
119 pub max_proof_size: usize,
121 pub max_metadata_value_size: usize,
123 pub max_namespace_name_size: usize,
125 pub min_namespace_duration: Duration,
127 pub max_namespace_duration: Duration,
129 pub voting_set_grouping: u64,
131 pub min_voting_key_lifetime: u32,
133 pub max_voting_key_lifetime: u32,
135 pub max_voting_keys_per_account: usize,
137 pub fork_heights: ForkHeights,
139 pub corrupt_aggregate_transaction_hashes: &'static [(Hash256, Hash256)],
142}
143
144#[derive(Clone, Copy, PartialEq, Eq, Debug)]
146pub struct ForkHeights {
147 pub strict_aggregate_transaction_hash: u64,
149 pub unique_aggregate_transaction_hash: u64,
151 pub strict_vrf_public_key_check: u64,
153 pub vrf_gamma_subgroup_check: u64,
156}
157
158const DAY: u64 = 24 * 60 * 60;
160
161impl NetworkParameters {
162 pub const MAINNET: Self = Self {
163 identifier: NetworkIdentifier::MAINNET,
164 generation_hash_seed: GenerationHashSeed::MAINNET,
165 epoch_adjustment: Duration::from_secs(1_615_853_185),
167 currency_mosaic_id: 0x6BED_913F_A202_23F8,
168 block_generation_target_time: Duration::from_secs(30),
169 block_time_smoothing_factor: 3000,
170 max_difficulty_blocks: 60,
171 total_chain_importance: 7_842_928_625_000_000,
172 importance_grouping: 720,
173 max_transaction_lifetime: Duration::from_secs(6 * 60 * 60),
174 max_cosignatures_per_aggregate: 25,
175 max_transactions_per_aggregate: 100,
176 max_message_size: 1024,
177 max_hash_lock_duration: Duration::from_secs(2 * DAY),
178 max_secret_lock_duration: Duration::from_secs(365 * DAY),
179 min_proof_size: 0,
180 max_proof_size: 1024,
181 max_metadata_value_size: 1024,
182 max_namespace_name_size: 64,
183 min_namespace_duration: Duration::from_secs(30 * DAY),
184 max_namespace_duration: Duration::from_secs(1825 * DAY),
185 voting_set_grouping: 1440,
186 min_voting_key_lifetime: 112,
187 max_voting_key_lifetime: 360,
188 max_voting_keys_per_account: 3,
189 fork_heights: ForkHeights {
190 strict_aggregate_transaction_hash: 1_690_500,
191 unique_aggregate_transaction_hash: 4_759_100,
192 strict_vrf_public_key_check: 5_952_255,
193 vrf_gamma_subgroup_check: 5_967_570,
194 },
195 corrupt_aggregate_transaction_hashes: &MAINNET_CORRUPT_AGGREGATE_TRANSACTION_HASHES,
196 };
197
198 pub const TESTNET: Self = Self {
199 identifier: NetworkIdentifier::TESTNET,
200 generation_hash_seed: GenerationHashSeed::TESTNET,
201 epoch_adjustment: Duration::from_secs(1_667_250_467),
203 currency_mosaic_id: 0x72C0_212E_67A0_8BCE,
204 block_generation_target_time: Duration::from_secs(30),
205 block_time_smoothing_factor: 3000,
206 max_difficulty_blocks: 60,
207 total_chain_importance: 7_842_928_625_000_000,
208 importance_grouping: 180,
209 max_transaction_lifetime: Duration::from_secs(6 * 60 * 60),
210 max_cosignatures_per_aggregate: 25,
211 max_transactions_per_aggregate: 100,
212 max_message_size: 1024,
213 max_hash_lock_duration: Duration::from_secs(2 * DAY),
214 max_secret_lock_duration: Duration::from_secs(365 * DAY),
215 min_proof_size: 0,
216 max_proof_size: 1024,
217 max_metadata_value_size: 1024,
218 max_namespace_name_size: 64,
219 min_namespace_duration: Duration::from_secs(30 * DAY),
220 max_namespace_duration: Duration::from_secs(1825 * DAY),
221 voting_set_grouping: 720,
222 min_voting_key_lifetime: 28,
223 max_voting_key_lifetime: 720,
224 max_voting_keys_per_account: 3,
225 fork_heights: ForkHeights {
226 strict_aggregate_transaction_hash: 0,
227 unique_aggregate_transaction_hash: 2_742_000,
228 strict_vrf_public_key_check: 3_888_648,
229 vrf_gamma_subgroup_check: 3_904_683,
230 },
231 corrupt_aggregate_transaction_hashes: &[],
232 };
233
234 pub fn from_identifier(identifier: NetworkIdentifier) -> Option<&'static Self> {
236 [&Self::MAINNET, &Self::TESTNET]
237 .into_iter()
238 .find(|parameters| parameters.identifier == identifier)
239 }
240
241 #[cfg(feature = "std")]
243 pub fn epoch(&self) -> SystemTime {
244 UNIX_EPOCH + self.epoch_adjustment
245 }
246
247 pub fn blocks(&self, duration: Duration) -> u64 {
249 let blocks = duration.as_millis() / self.block_generation_target_time.as_millis();
250 u64::try_from(blocks).unwrap_or(u64::MAX)
251 }
252
253 #[cfg(feature = "std")]
256 pub fn timestamp_at(&self, time: SystemTime) -> Option<NetworkTimestamp> {
257 let elapsed = time.duration_since(self.epoch()).ok()?;
258 u64::try_from(elapsed.as_millis())
259 .ok()
260 .map(NetworkTimestamp)
261 }
262
263 #[cfg(feature = "std")]
265 pub fn time_of(&self, timestamp: NetworkTimestamp) -> Option<SystemTime> {
266 self.epoch().checked_add(Duration::from_millis(timestamp.0))
267 }
268
269 pub fn finalization_epoch(&self, height: u64) -> u64 {
272 if height <= 1 {
273 return 1;
274 }
275 let adjustment = u64::from(!height.is_multiple_of(self.voting_set_grouping));
276 1 + adjustment + height / self.voting_set_grouping
277 }
278
279 pub fn vrf_checks(&self, height: u64) -> crate::vrf::Checks {
281 crate::vrf::Checks {
282 public_key: height >= self.fork_heights.strict_vrf_public_key_check,
283 gamma: height >= self.fork_heights.vrf_gamma_subgroup_check,
284 }
285 }
286
287 pub fn aggregate_version(&self, height: u64) -> u8 {
290 if height >= self.fork_heights.unique_aggregate_transaction_hash {
291 3
292 } else if height >= self.fork_heights.strict_aggregate_transaction_hash {
293 2
294 } else {
295 1
296 }
297 }
298
299 pub fn is_known_corrupt_aggregate(
301 &self,
302 aggregate_hash: &Hash256,
303 transactions_hash: &Hash256,
304 ) -> bool {
305 self.corrupt_aggregate_transaction_hashes
306 .contains(&(*aggregate_hash, *transactions_hash))
307 }
308}
309
310const MAINNET_CORRUPT_AGGREGATE_TRANSACTION_HASHES: [(Hash256, Hash256); 20] = [
313 (
314 Hash256(hex::decode_const(
315 b"26FF5E7174DEEF3147DB25C37C7AE9905157ACBA2D233D40D1F77A65B60D59BC",
316 )),
317 Hash256(hex::decode_const(
318 b"3A2F78C2E7B10FF84EB33BF1DD0FD61951F8E3EA3303614D1B20060CD88F6E14",
319 )),
320 ),
321 (
322 Hash256(hex::decode_const(
323 b"2DF1D19B5E68102665D11B3CF3069AB5DFED2E60A7690C8788172E0E5E30B72D",
324 )),
325 Hash256(hex::decode_const(
326 b"3B64321593D06FF37B191F52FE31E107A8FDB0116AB5374157F8F415B8C73BE4",
327 )),
328 ),
329 (
330 Hash256(hex::decode_const(
331 b"30FA71E6D1E34DF1E430A07E1B0817BED9A4ED6B0245B7471B0557380A700E1B",
332 )),
333 Hash256(hex::decode_const(
334 b"AE377EAB7F1970DEC21FF305C39EBDD3ECE413B911EF3E4E11F7DDDC9C9C9748",
335 )),
336 ),
337 (
338 Hash256(hex::decode_const(
339 b"33937869216924161235BA8E10DC075592022C22F6593042A1064ADD24B64FE8",
340 )),
341 Hash256(hex::decode_const(
342 b"9C00D552960DEDC38A009B541681B2C7A68902D94A9D6C089604D2E7EFCCD88E",
343 )),
344 ),
345 (
346 Hash256(hex::decode_const(
347 b"55EB9659C81600F1760C4C0A4F8A7A5C90A39FCEE36E3165143B8E72BBC709E8",
348 )),
349 Hash256(hex::decode_const(
350 b"8980CF4407A48410C4E910FE766C135F8C3C84EA9917BFDB7A71434C2CDC625A",
351 )),
352 ),
353 (
354 Hash256(hex::decode_const(
355 b"8090FFF457D88D98ACFDA7872DAAD1A901B4CB081387B1A45AFDA8EEF686EB54",
356 )),
357 Hash256(hex::decode_const(
358 b"05718908AFBA83B420AE19BE0A11EE36119AC960353BA6C3F180078C8C4B428D",
359 )),
360 ),
361 (
362 Hash256(hex::decode_const(
363 b"852A41F9452A34054DE1C21E46F925EA7B50D26AF97FBA8D745F01CC6DD5097F",
364 )),
365 Hash256(hex::decode_const(
366 b"8626BB52F6457D95B01A5C178E6A6977F8BD418CD9A7D5B0A814210E0A4FBBA3",
367 )),
368 ),
369 (
370 Hash256(hex::decode_const(
371 b"8BAA5EC2DDF55EE583ABB7EA4A4FE6EB8A4C16C41ED30CB1E68F6D1F40C3FC5A",
372 )),
373 Hash256(hex::decode_const(
374 b"59850BF728A08555D10DAD5745EE2A349FECC0B16810F5D32CF3DFC376DEFFE1",
375 )),
376 ),
377 (
378 Hash256(hex::decode_const(
379 b"8CE147B1B64E200FA48B17FE5B6F4FA2FFEC83FF03A1EC0D4725AB8122AF5B05",
380 )),
381 Hash256(hex::decode_const(
382 b"1F3D4045C3482A9EED536F61CAA58284323037B72A9A774A0C75932F85999B0F",
383 )),
384 ),
385 (
386 Hash256(hex::decode_const(
387 b"91C27BE7E5AC8EBCE6A5D576DE250054A3CDC2B674BAD5FA29B554C0A50E2973",
388 )),
389 Hash256(hex::decode_const(
390 b"05718908AFBA83B420AE19BE0A11EE36119AC960353BA6C3F180078C8C4B428D",
391 )),
392 ),
393 (
394 Hash256(hex::decode_const(
395 b"97CA49FCE55644FF28248C6EA4DCB6E87E53909811DEE2513F0218DAA394679F",
396 )),
397 Hash256(hex::decode_const(
398 b"657BE411624B617F51CD8D151735F50FE50A91A5B7D0658192EF3EC4DD0C91C3",
399 )),
400 ),
401 (
402 Hash256(hex::decode_const(
403 b"B2813813F709C3011468E23E61AE102BCB55A28A87D0CF74968B8BD2413615AA",
404 )),
405 Hash256(hex::decode_const(
406 b"3FEF96C5496E60BC1D33815139BDCD4FA707CE1C2A9A25FF4F7D3B1BE698116C",
407 )),
408 ),
409 (
410 Hash256(hex::decode_const(
411 b"B35D60C826470EB38B6963DCEF5A9FE406EDE405676EDB027956999D7618998D",
412 )),
413 Hash256(hex::decode_const(
414 b"D8915C7EFFDB62B3CB5872597522C0920AF5D55EC44A4548DF8679C08A0C0541",
415 )),
416 ),
417 (
418 Hash256(hex::decode_const(
419 b"BC312549DB027287836AD52B6EA30171AA1958B20BEB3D659F843D699DE45FD3",
420 )),
421 Hash256(hex::decode_const(
422 b"4AC4B12DE7C5B0D02BFFEA3ED98F4247FAFF701C4CE0A2CFD75E7F633DB0ED60",
423 )),
424 ),
425 (
426 Hash256(hex::decode_const(
427 b"BDF7755E44E1405C3353AB97651DD82785C6A3A8EAA5EA1CEA0E30DADB0D12F8",
428 )),
429 Hash256(hex::decode_const(
430 b"B6D02DA3F69C20F69A9209B13192174091544512CD7D2CE53C366909B354B7B1",
431 )),
432 ),
433 (
434 Hash256(hex::decode_const(
435 b"C0FAAD2439E9D8017F9A2E5F67A46850195209402C1EE55CF99880426D0F454F",
436 )),
437 Hash256(hex::decode_const(
438 b"3B74892D50665E4A989C89EDCE88905937B81BF260A679653E86886407812F83",
439 )),
440 ),
441 (
442 Hash256(hex::decode_const(
443 b"C1E7404CDAE41ACE07E8649FA2D5E3E2FF5DA502E43E5D1C6073192823F90BE6",
444 )),
445 Hash256(hex::decode_const(
446 b"A7321E82547506079C0692DC0DFF8C9CB0D56DD2C9E3208F479DB8970B985E6C",
447 )),
448 ),
449 (
450 Hash256(hex::decode_const(
451 b"D24D000FABBD1AC4BC21006A5CAE76AE9C32792A4920F66D6A6C6F76B438BDF1",
452 )),
453 Hash256(hex::decode_const(
454 b"5FDEF85E8B3669FF155B5F3DA98288C6ED8A293CC699338ADB743C0F5E8614ED",
455 )),
456 ),
457 (
458 Hash256(hex::decode_const(
459 b"D91689D6A3C9FE7AABB45C3FC44373DA23EF363D71360CB4B7ECD74F0BDE893B",
460 )),
461 Hash256(hex::decode_const(
462 b"D8915C7EFFDB62B3CB5872597522C0920AF5D55EC44A4548DF8679C08A0C0541",
463 )),
464 ),
465 (
466 Hash256(hex::decode_const(
467 b"E14FB95DA63CDA9899474D7EFD0DCB6A6C9E8931DF12ECA01F22DF2B59B6624C",
468 )),
469 Hash256(hex::decode_const(
470 b"342E7C1E6318F7DCE5CE5A09B8FA1820453C4437AEF1B872BD06531E3ADB41C0",
471 )),
472 ),
473];
474
475#[cfg(test)]
476mod tests {
477 use super::*;
478 use crate::vrf::Checks;
479
480 #[cfg(feature = "std")]
481 #[test]
482 fn converts_between_system_time_and_network_time() {
483 let network = NetworkParameters::MAINNET;
484 let epoch = network.epoch();
485 assert_eq!(network.timestamp_at(epoch), Some(NetworkTimestamp(0)));
486 let later = epoch + Duration::from_micros(1_500_999);
488 assert_eq!(network.timestamp_at(later), Some(NetworkTimestamp(1_500)));
489 assert_eq!(
490 network.time_of(NetworkTimestamp(1_500)),
491 Some(epoch + Duration::from_millis(1_500))
492 );
493 assert_eq!(network.timestamp_at(epoch - Duration::from_millis(1)), None);
495 assert_ne!(
497 NetworkParameters::TESTNET.timestamp_at(later),
498 network.timestamp_at(later)
499 );
500 }
501
502 #[test]
503 fn converts_durations_to_blocks() {
504 let network = NetworkParameters::MAINNET;
506 assert_eq!(network.blocks(network.max_hash_lock_duration), 5_760);
507 assert_eq!(network.blocks(network.max_secret_lock_duration), 1_051_200);
508 assert_eq!(network.blocks(network.min_namespace_duration), 86_400);
509 assert_eq!(network.blocks(Duration::from_secs(59)), 1);
511 }
512
513 #[test]
514 fn adds_and_subtracts_durations() {
515 let deadline = NetworkTimestamp(1_000);
516 assert_eq!(
517 deadline.checked_add(Duration::from_secs(2 * 60 * 60)),
518 Some(NetworkTimestamp(1_000 + 7_200_000))
519 );
520 assert_eq!(
521 deadline.checked_add(Duration::from_micros(1_999)),
522 Some(NetworkTimestamp(1_001))
523 );
524 assert_eq!(
525 deadline.checked_sub(Duration::from_micros(1_001)),
526 Some(NetworkTimestamp(998))
527 );
528 assert_eq!(deadline.checked_sub(Duration::from_millis(1_001)), None);
529 assert_eq!(
530 NetworkTimestamp(u64::MAX).checked_add(Duration::from_millis(1)),
531 None
532 );
533 assert_eq!(
534 NetworkTimestamp(1_500).duration_since(deadline),
535 Some(Duration::from_millis(500))
536 );
537 assert_eq!(deadline.duration_since(NetworkTimestamp(1_500)), None);
538 }
539
540 #[test]
541 fn finds_parameters_by_identifier() {
542 assert_eq!(
543 NetworkParameters::from_identifier(NetworkIdentifier::MAINNET),
544 Some(&NetworkParameters::MAINNET)
545 );
546 assert_eq!(
547 NetworkParameters::from_identifier(NetworkIdentifier::TESTNET),
548 Some(&NetworkParameters::TESTNET)
549 );
550 assert_eq!(
551 NetworkParameters::from_identifier(NetworkIdentifier(0)),
552 None
553 );
554 }
555
556 #[test]
557 fn groups_heights_into_finalization_epochs() {
558 let mainnet = NetworkParameters::MAINNET;
559 assert_eq!(mainnet.finalization_epoch(1), 1);
560 assert_eq!(mainnet.finalization_epoch(2), 2);
561 assert_eq!(mainnet.finalization_epoch(1440), 2);
562 assert_eq!(mainnet.finalization_epoch(1441), 3);
563 assert_eq!(mainnet.finalization_epoch(2880), 3);
564 assert_eq!(NetworkParameters::TESTNET.finalization_epoch(721), 3);
565 }
566
567 #[test]
568 fn allows_one_aggregate_version_per_height() {
569 let mainnet = NetworkParameters::MAINNET;
570 assert_eq!(mainnet.aggregate_version(1), 1);
571 assert_eq!(mainnet.aggregate_version(1_690_499), 1);
572 assert_eq!(mainnet.aggregate_version(1_690_500), 2);
573 assert_eq!(mainnet.aggregate_version(4_759_099), 2);
574 assert_eq!(mainnet.aggregate_version(4_759_100), 3);
575 assert_eq!(NetworkParameters::TESTNET.aggregate_version(1), 2);
577 }
578
579 #[test]
580 fn checks_vrf_proofs_from_the_forks() {
581 let only_key = Checks {
582 public_key: true,
583 gamma: false,
584 };
585 let mainnet = NetworkParameters::MAINNET;
586 assert_eq!(mainnet.vrf_checks(5_952_254), Checks::NONE);
587 assert_eq!(mainnet.vrf_checks(5_952_255), only_key);
588 assert_eq!(mainnet.vrf_checks(5_967_569), only_key);
589 assert_eq!(mainnet.vrf_checks(5_967_570), Checks::ALL);
590 let testnet = NetworkParameters::TESTNET;
591 assert_eq!(testnet.vrf_checks(3_888_647), Checks::NONE);
592 assert_eq!(testnet.vrf_checks(3_888_648), only_key);
593 assert_eq!(testnet.vrf_checks(3_904_682), only_key);
594 assert_eq!(testnet.vrf_checks(3_904_683), Checks::ALL);
595 }
596
597 #[test]
598 fn looks_up_corrupt_aggregates_by_both_hashes() {
599 let mainnet = NetworkParameters::MAINNET;
600 let (aggregate, transactions) = mainnet.corrupt_aggregate_transaction_hashes[0];
601 assert!(mainnet.is_known_corrupt_aggregate(&aggregate, &transactions));
602 assert!(!mainnet.is_known_corrupt_aggregate(&aggregate, &aggregate));
603 assert!(!NetworkParameters::TESTNET.is_known_corrupt_aggregate(&aggregate, &transactions));
604 }
605}