@@ -31,7 +31,6 @@ The module provide epoch functionality for starcoin.
31
31
32
32
<pre ><code ><b >use</b > <a href =" ConsensusConfig.md#0x1_ConsensusConfig " >0x1::ConsensusConfig</a >;
33
33
<b >use</b > <a href =" CoreAddresses.md#0x1_CoreAddresses " >0x1::CoreAddresses</a >;
34
- <b >use</b > <a href =" Errors.md#0x1_Errors " >0x1::Errors</a >;
35
34
<b >use</b > <a href =" Event.md#0x1_Event " >0x1::Event</a >;
36
35
<b >use</b > <a href =" Math.md#0x1_Math " >0x1::Math</a >;
37
36
<b >use</b > <a href =" Option.md#0x1_Option " >0x1::Option</a >;
@@ -369,13 +368,21 @@ compute next block time_target.
369
368
<summary >Implementation</summary >
370
369
371
370
372
- <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_compute_next_block_time_target " >compute_next_block_time_target</a >(config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >, last_epoch_time_target: u64, epoch_start_time: u64, now_milli_second: u64, start_block_number: u64, end_block_number: u64, total_uncles: u64): u64 {
371
+ <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_compute_next_block_time_target " >compute_next_block_time_target</a >(
372
+ config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >,
373
+ last_epoch_time_target: u64,
374
+ epoch_start_time: u64,
375
+ now_milli_second: u64,
376
+ start_block_number: u64,
377
+ end_block_number: u64,
378
+ total_uncles: u64
379
+ ): u64 {
373
380
<b >let</b > total_time = now_milli_second - epoch_start_time;
374
381
<b >let</b > blocks = end_block_number - start_block_number;
375
382
<b >let</b > avg_block_time = total_time / blocks;
376
383
<b >let</b > uncles_rate = total_uncles * <a href =" Epoch.md#0x1_Epoch_THOUSAND " >THOUSAND</a > / blocks;
377
384
<b >let</b > new_epoch_block_time_target = (<a href =" Epoch.md#0x1_Epoch_THOUSAND " >THOUSAND</a > + uncles_rate) * avg_block_time /
378
- (<a href =" ConsensusConfig.md#0x1_ConsensusConfig_uncle_rate_target " >ConsensusConfig::uncle_rate_target</a >(config) + <a href =" Epoch.md#0x1_Epoch_THOUSAND " >THOUSAND</a >);
385
+ (<a href =" ConsensusConfig.md#0x1_ConsensusConfig_uncle_rate_target " >ConsensusConfig::uncle_rate_target</a >(config) + <a href =" Epoch.md#0x1_Epoch_THOUSAND " >THOUSAND</a >);
379
386
<b >if</b > (new_epoch_block_time_target > ; last_epoch_time_target * 2) {
380
387
new_epoch_block_time_target = last_epoch_time_target * 2;
381
388
};
@@ -426,26 +433,43 @@ adjust_epoch try to advance to next epoch if current epoch ends.
426
433
<summary >Implementation</summary >
427
434
428
435
429
- <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_adjust_epoch " >adjust_epoch</a >(account: &signer, block_number: u64, timestamp: u64, uncles: u64, parent_gas_used:u64): u128
436
+ <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_adjust_epoch " >adjust_epoch</a >(
437
+ account: &signer,
438
+ block_number: u64,
439
+ timestamp: u64,
440
+ uncles: u64,
441
+ parent_gas_used: u64
442
+ ): u128
430
443
<b >acquires</b > <a href =" Epoch.md#0x1_Epoch " >Epoch</a >, <a href =" Epoch.md#0x1_Epoch_EpochData " >EpochData</a > {
431
444
<a href =" CoreAddresses.md#0x1_CoreAddresses_assert_genesis_address " >CoreAddresses::assert_genesis_address</a >(account);
432
445
433
446
<b >let</b > epoch_ref = <b >borrow_global_mut</b >< ; <a href =" Epoch.md#0x1_Epoch " >Epoch</a >> ; (<a href =" CoreAddresses.md#0x1_CoreAddresses_GENESIS_ADDRESS " >CoreAddresses::GENESIS_ADDRESS</a >());
434
- <b >assert</b >!(epoch_ref.max_uncles_per_block > ; = uncles, <a href =" Errors.md#0x1_Errors_invalid_argument " >Errors::invalid_argument</a >(<a href =" Epoch.md#0x1_Epoch_EINVALID_UNCLES_COUNT " >EINVALID_UNCLES_COUNT</a >));
447
+ // <b >assert</b >!(epoch_ref.max_uncles_per_block > ; = uncles, <a href =" Errors.md#0x1_Errors_invalid_argument " >Errors::invalid_argument</a >(<a href =" Epoch.md#0x1_Epoch_EINVALID_UNCLES_COUNT " >EINVALID_UNCLES_COUNT</a >));
435
448
436
449
<b >let</b > epoch_data = <b >borrow_global_mut</b >< ; <a href =" Epoch.md#0x1_Epoch_EpochData " >EpochData</a >> ; (<a href =" CoreAddresses.md#0x1_CoreAddresses_GENESIS_ADDRESS " >CoreAddresses::GENESIS_ADDRESS</a >());
437
450
<b >let</b > (new_epoch, reward_per_block) = <b >if</b > (block_number < ; epoch_ref.end_block_number) {
438
451
(<b >false</b >, epoch_ref.reward_per_block)
439
452
} <b >else</b > <b >if</b > (block_number == epoch_ref.end_block_number) {
440
453
//start a new epoch
441
- <b >assert</b >!(uncles == 0, <a href =" Errors.md#0x1_Errors_invalid_argument " >Errors::invalid_argument</a >(<a href =" Epoch.md#0x1_Epoch_EINVALID_UNCLES_COUNT " >EINVALID_UNCLES_COUNT</a >));
454
+ // <b >assert</b >!(uncles == 0, <a href =" Errors.md#0x1_Errors_invalid_argument " >Errors::invalid_argument</a >(<a href =" Epoch.md#0x1_Epoch_EINVALID_UNCLES_COUNT " >EINVALID_UNCLES_COUNT</a >));
442
455
// block time target unit is milli_seconds.
443
456
<b >let</b > now_milli_seconds = timestamp;
444
457
445
458
<b >let</b > config = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_get_config " >ConsensusConfig::get_config</a >();
446
459
<b >let</b > last_epoch_time_target = epoch_ref.block_time_target;
447
- <b >let</b > new_epoch_block_time_target = <a href =" Epoch.md#0x1_Epoch_compute_next_block_time_target " >compute_next_block_time_target</a >(&config, last_epoch_time_target, epoch_ref.start_time, now_milli_seconds, epoch_ref.start_block_number, epoch_ref.end_block_number, epoch_data.uncles);
448
- <b >let</b > new_reward_per_block = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_do_compute_reward_per_block " >ConsensusConfig::do_compute_reward_per_block</a >(&config, new_epoch_block_time_target);
460
+ <b >let</b > new_epoch_block_time_target = <a href =" Epoch.md#0x1_Epoch_compute_next_block_time_target " >compute_next_block_time_target</a >(
461
+ &config,
462
+ last_epoch_time_target,
463
+ epoch_ref.start_time,
464
+ now_milli_seconds,
465
+ epoch_ref.start_block_number,
466
+ epoch_ref.end_block_number,
467
+ epoch_data.uncles
468
+ );
469
+ <b >let</b > new_reward_per_block = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_do_compute_reward_per_block " >ConsensusConfig::do_compute_reward_per_block</a >(
470
+ &config,
471
+ new_epoch_block_time_target
472
+ );
449
473
450
474
//<b >update</b > epoch by adjust result or config, because <a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a > may be updated.
451
475
epoch_ref.number = epoch_ref.number + 1;
@@ -461,15 +485,21 @@ adjust_epoch try to advance to next epoch if current epoch ends.
461
485
462
486
epoch_data.uncles = 0;
463
487
<b >let</b > last_epoch_total_gas = epoch_data.total_gas + (parent_gas_used <b >as</b > u128);
464
- <a href =" Epoch.md#0x1_Epoch_adjust_gas_limit " >adjust_gas_limit</a >(&config, epoch_ref, last_epoch_time_target, new_epoch_block_time_target, last_epoch_total_gas);
488
+ <a href =" Epoch.md#0x1_Epoch_adjust_gas_limit " >adjust_gas_limit</a >(
489
+ &config,
490
+ epoch_ref,
491
+ last_epoch_time_target,
492
+ new_epoch_block_time_target,
493
+ last_epoch_total_gas
494
+ );
465
495
<a href =" Epoch.md#0x1_Epoch_emit_epoch_event " >emit_epoch_event</a >(epoch_ref, epoch_data.total_reward);
466
496
(<b >true</b >, new_reward_per_block)
467
497
} <b >else</b > {
468
498
//This should never happened.
469
499
<b >abort</b > <a href =" Epoch.md#0x1_Epoch_EUNREACHABLE " >EUNREACHABLE</a >
470
500
};
471
501
<b >let</b > reward = reward_per_block +
472
- reward_per_block * (epoch_ref.reward_per_uncle_percent <b >as</b > u128) * (uncles <b >as</b > u128) / (<a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > <b >as</b > u128);
502
+ reward_per_block * (epoch_ref.reward_per_uncle_percent <b >as</b > u128) * (uncles <b >as</b > u128) / (<a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > <b >as</b > u128);
473
503
<a href =" Epoch.md#0x1_Epoch_update_epoch_data " >update_epoch_data</a >(epoch_data, new_epoch, reward, uncles, parent_gas_used);
474
504
reward
475
505
}
@@ -511,8 +541,20 @@ adjust_epoch try to advance to next epoch if current epoch ends.
511
541
<summary >Implementation</summary >
512
542
513
543
514
- <pre ><code ><b >fun</b > <a href =" Epoch.md#0x1_Epoch_adjust_gas_limit " >adjust_gas_limit</a >(config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >, epoch_ref: &<b >mut</b > <a href =" Epoch.md#0x1_Epoch " >Epoch</a >, last_epoch_time_target: u64, new_epoch_time_target: u64, last_epoch_total_gas:u128) {
515
- <b >let</b > new_gas_limit = <a href =" Epoch.md#0x1_Epoch_compute_gas_limit " >compute_gas_limit</a >(config, last_epoch_time_target, new_epoch_time_target, epoch_ref.block_gas_limit, last_epoch_total_gas);
544
+ <pre ><code ><b >fun</b > <a href =" Epoch.md#0x1_Epoch_adjust_gas_limit " >adjust_gas_limit</a >(
545
+ config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >,
546
+ epoch_ref: &<b >mut</b > <a href =" Epoch.md#0x1_Epoch " >Epoch</a >,
547
+ last_epoch_time_target: u64,
548
+ new_epoch_time_target: u64,
549
+ last_epoch_total_gas: u128
550
+ ) {
551
+ <b >let</b > new_gas_limit = <a href =" Epoch.md#0x1_Epoch_compute_gas_limit " >compute_gas_limit</a >(
552
+ config,
553
+ last_epoch_time_target,
554
+ new_epoch_time_target,
555
+ epoch_ref.block_gas_limit,
556
+ last_epoch_total_gas
557
+ );
516
558
<b >if</b > (<a href =" Option.md#0x1_Option_is_some " >Option::is_some</a >(&new_gas_limit)) {
517
559
epoch_ref.block_gas_limit = <a href =" Option.md#0x1_Option_destroy_some " >Option::destroy_some</a >(new_gas_limit);
518
560
}
@@ -551,17 +593,31 @@ Compute block's gas limit of next epoch.
551
593
<summary >Implementation</summary >
552
594
553
595
554
- <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_compute_gas_limit " >compute_gas_limit</a >(config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >, last_epoch_time_target: u64, new_epoch_time_target: u64, last_epoch_block_gas_limit: u64, last_epoch_total_gas: u128) : <a href =" Option.md#0x1_Option_Option " >Option::Option</a >< ; u64> ; {
596
+ <pre ><code ><b >public</b > <b >fun</b > <a href =" Epoch.md#0x1_Epoch_compute_gas_limit " >compute_gas_limit</a >(
597
+ config: &<a href =" ConsensusConfig.md#0x1_ConsensusConfig " >ConsensusConfig</a >,
598
+ last_epoch_time_target: u64,
599
+ new_epoch_time_target: u64,
600
+ last_epoch_block_gas_limit: u64,
601
+ last_epoch_total_gas: u128
602
+ ): <a href =" Option.md#0x1_Option_Option " >Option::Option</a >< ; u64> ; {
555
603
<b >let</b > epoch_block_count = (<a href =" ConsensusConfig.md#0x1_ConsensusConfig_epoch_block_count " >ConsensusConfig::epoch_block_count</a >(config) <b >as</b > u128);
556
- <b >let</b > gas_limit_threshold = (last_epoch_total_gas > ; = <a href =" Math.md#0x1_Math_mul_div " >Math::mul_div</a >((last_epoch_block_gas_limit <b >as</b > u128) * epoch_block_count, (80 <b >as</b > u128), (<a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > <b >as</b > u128)));
604
+ <b >let</b > gas_limit_threshold = (last_epoch_total_gas > ; = <a href =" Math.md#0x1_Math_mul_div " >Math::mul_div</a >(
605
+ (last_epoch_block_gas_limit <b >as</b > u128) * epoch_block_count,
606
+ (80 <b >as</b > u128),
607
+ (<a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > <b >as</b > u128)
608
+ ));
557
609
<b >let</b > new_gas_limit = <a href =" Option.md#0x1_Option_none " >Option::none</a >< ; u64> ; ();
558
610
559
611
<b >let</b > min_block_time_target = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_min_block_time_target " >ConsensusConfig::min_block_time_target</a >(config);
560
612
<b >let</b > max_block_time_target = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_max_block_time_target " >ConsensusConfig::max_block_time_target</a >(config);
561
- <b >let</b > base_block_gas_limit = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_base_block_gas_limit " >ConsensusConfig::base_block_gas_limit</a >(config);
613
+ <b >let</b > base_block_gas_limit = <a href =" ConsensusConfig.md#0x1_ConsensusConfig_base_block_gas_limit " >ConsensusConfig::base_block_gas_limit</a >(config);
562
614
<b >if</b > (last_epoch_time_target == new_epoch_time_target) {
563
615
<b >if</b > (new_epoch_time_target == min_block_time_target && gas_limit_threshold) {
564
- <b >let</b > increase_gas_limit = <a href =" Epoch.md#0x1_Epoch_in_or_decrease_gas_limit " >in_or_decrease_gas_limit</a >(last_epoch_block_gas_limit, 110, base_block_gas_limit);
616
+ <b >let</b > increase_gas_limit = <a href =" Epoch.md#0x1_Epoch_in_or_decrease_gas_limit " >in_or_decrease_gas_limit</a >(
617
+ last_epoch_block_gas_limit,
618
+ 110,
619
+ base_block_gas_limit
620
+ );
565
621
new_gas_limit = <a href =" Option.md#0x1_Option_some " >Option::some</a >(increase_gas_limit);
566
622
} <b >else</b > <b >if</b > (new_epoch_time_target == max_block_time_target && !gas_limit_threshold) {
567
623
<b >let</b > decrease_gas_limit = <a href =" Epoch.md#0x1_Epoch_in_or_decrease_gas_limit " >in_or_decrease_gas_limit</a >(last_epoch_block_gas_limit, 90, base_block_gas_limit);
@@ -606,7 +662,7 @@ Compute block's gas limit of next epoch.
606
662
607
663
<pre ><code ><b >fun</b > <a href =" Epoch.md#0x1_Epoch_in_or_decrease_gas_limit " >in_or_decrease_gas_limit</a >(last_epoch_block_gas_limit: u64, percent: u64, min_block_gas_limit: u64): u64 {
608
664
<b >let</b > tmp_gas_limit = <a href =" Math.md#0x1_Math_mul_div " >Math::mul_div</a >((last_epoch_block_gas_limit <b >as</b > u128), (percent <b >as</b > u128), (<a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > <b >as</b > u128));
609
- <b >let</b > new_gas_limit = <b >if</b > (tmp_gas_limit > ; (min_block_gas_limit <b >as</b > u128)) {
665
+ <b >let</b > new_gas_limit = <b >if</b > (tmp_gas_limit > ; (min_block_gas_limit <b >as</b > u128)) {
610
666
(tmp_gas_limit <b >as</b > u64)
611
667
} <b >else</b > {
612
668
min_block_gas_limit
@@ -625,7 +681,7 @@ Compute block's gas limit of next epoch.
625
681
626
682
627
683
628
- <pre ><code ><b >include</b > <a href =" Math.md#0x1_Math_MulDivAbortsIf " >Math::MulDivAbortsIf</a >{ x: last_epoch_block_gas_limit, y: percent, z: <a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a >};
684
+ <pre ><code ><b >include</b > <a href =" Math.md#0x1_Math_MulDivAbortsIf " >Math::MulDivAbortsIf</a > { x: last_epoch_block_gas_limit, y: percent, z: <a href =" Epoch.md#0x1_Epoch_HUNDRED " >HUNDRED</a > };
629
685
<b >aborts_if</b > <a href =" Math.md#0x1_Math_spec_mul_div " >Math::spec_mul_div</a >() > ; MAX_U64;
630
686
</code ></pre >
631
687
@@ -648,7 +704,13 @@ Compute block's gas limit of next epoch.
648
704
<summary >Implementation</summary >
649
705
650
706
651
- <pre ><code ><b >fun</b > <a href =" Epoch.md#0x1_Epoch_update_epoch_data " >update_epoch_data</a >(epoch_data: &<b >mut</b > <a href =" Epoch.md#0x1_Epoch_EpochData " >EpochData</a >, new_epoch: bool, reward: u128, uncles: u64, parent_gas_used:u64) {
707
+ <pre ><code ><b >fun</b > <a href =" Epoch.md#0x1_Epoch_update_epoch_data " >update_epoch_data</a >(
708
+ epoch_data: &<b >mut</b > <a href =" Epoch.md#0x1_Epoch_EpochData " >EpochData</a >,
709
+ new_epoch: bool,
710
+ reward: u128,
711
+ uncles: u64,
712
+ parent_gas_used: u64
713
+ ) {
652
714
<b >if</b > (new_epoch) {
653
715
epoch_data.total_reward = reward;
654
716
epoch_data.uncles = uncles;
0 commit comments