@@ -48,8 +48,8 @@ const (
4848 extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
4949
5050 validatorBytesLength = common .AddressLength
51- wiggleTime = 500 * time . Millisecond // Random delay (per signer) to allow concurrent signers
52- fixedBackOffTime = 200 * time . Millisecond
51+ wiggleTime = uint64 ( 1 ) // second, Random delay (per signer) to allow concurrent signers
52+ initialBackOffTime = uint64 ( 1 ) // second
5353
5454 systemRewardPercent = 4 // it means 1/2^4 = 1/16 percentage of gas fee incoming will be distributed to system
5555
8181 common .HexToAddress (GovHubContract ): true ,
8282 common .HexToAddress (TokenHubContract ): true ,
8383 common .HexToAddress (RelayerIncentivizeContract ): true ,
84- common .HexToAddress (CrossChainContract ): true ,
84+ common .HexToAddress (CrossChainContract ): true ,
8585 }
8686)
8787
@@ -395,6 +395,16 @@ func (p *Parlia) verifyCascadingFields(chain consensus.ChainReader, header *type
395395 return consensus .ErrUnknownAncestor
396396 }
397397
398+ snap , err := p .snapshot (chain , number - 1 , header .ParentHash , parents )
399+ if err != nil {
400+ return err
401+ }
402+
403+ err = p .blockTimeVerifyForRamanujanFork (snap , header , parent )
404+ if err != nil {
405+ return nil
406+ }
407+
398408 // Verify that the gas limit is <= 2^63-1
399409 capacity := uint64 (0x7fffffffffffffff )
400410 if header .GasLimit > capacity {
@@ -570,7 +580,7 @@ func (p *Parlia) verifySeal(chain consensus.ChainReader, header *types.Header, p
570580
571581 // Ensure that the difficulty corresponds to the turn-ness of the signer
572582 if ! p .fakeDiff {
573- inturn := snap .inturn (header . Number . Uint64 (), signer )
583+ inturn := snap .inturn (signer )
574584 if inturn && header .Difficulty .Cmp (diffInTurn ) != 0 {
575585 return errWrongDifficulty
576586 }
@@ -626,8 +636,7 @@ func (p *Parlia) Prepare(chain consensus.ChainReader, header *types.Header) erro
626636 if parent == nil {
627637 return consensus .ErrUnknownAncestor
628638 }
629-
630- header .Time = parent .Time + p .config .Period
639+ header .Time = p .blockTimeForRamanujanFork (snap , header , parent )
631640 if header .Time < uint64 (time .Now ().Unix ()) {
632641 header .Time = uint64 (time .Now ().Unix ())
633642 }
@@ -809,14 +818,7 @@ func (p *Parlia) Seal(chain consensus.ChainReader, block *types.Block, results c
809818 }
810819
811820 // Sweet, the protocol permits us to sign the block, wait for our time
812- delay := time .Until (time .Unix (int64 (header .Time ), 0 )) // nolint: gosimple
813- if header .Difficulty .Cmp (diffNoTurn ) == 0 {
814- // It's not our turn explicitly to sign, delay it a bit
815- wiggle := time .Duration (len (snap .Validators )/ 2 + 1 ) * wiggleTime
816- delay += time .Duration (fixedBackOffTime ) + time .Duration (rand .Int63n (int64 (wiggle )))
817-
818- log .Trace ("Out-of-turn signing requested" , "wiggle" , common .PrettyDuration (wiggle ))
819- }
821+ delay := p .delayForRamanujanFork (snap , header )
820822
821823 log .Info ("Sealing block with" , "number" , number , "delay" , delay , "headerDifficulty" , header .Difficulty , "val" , val .Hex ())
822824
@@ -861,7 +863,7 @@ func (p *Parlia) CalcDifficulty(chain consensus.ChainReader, time uint64, parent
861863// that a new block should have based on the previous blocks in the chain and the
862864// current signer.
863865func CalcDifficulty (snap * Snapshot , signer common.Address ) * big.Int {
864- if snap .inturn (snap . Number + 1 , signer ) {
866+ if snap .inturn (signer ) {
865867 return new (big.Int ).Set (diffInTurn )
866868 }
867869 return new (big.Int ).Set (diffNoTurn )
@@ -1140,6 +1142,26 @@ func encodeSigHeader(w io.Writer, header *types.Header, chainId *big.Int) {
11401142 }
11411143}
11421144
1145+ func backOffTime (snap * Snapshot , val common.Address ) uint64 {
1146+ if snap .inturn (val ) {
1147+ return 0
1148+ } else {
1149+ dis := snap .distanceToInTurn (val )
1150+ s := rand .NewSource (int64 (snap .Number ))
1151+ r := rand .New (s )
1152+ n := len (snap .Validators )
1153+ backOffSteps := make ([]uint64 , 0 , n )
1154+ for idx := uint64 (0 ); idx < uint64 (n ); idx ++ {
1155+ backOffSteps = append (backOffSteps , idx )
1156+ }
1157+ r .Shuffle (n , func (i , j int ) {
1158+ backOffSteps [i ], backOffSteps [j ] = backOffSteps [j ], backOffSteps [i ]
1159+ })
1160+ delay := initialBackOffTime + backOffSteps [dis ]* wiggleTime
1161+ return delay
1162+ }
1163+ }
1164+
11431165// chain context
11441166type chainContext struct {
11451167 Chain consensus.ChainReader
@@ -1194,4 +1216,3 @@ func applyMessage(
11941216 }
11951217 return msg .Gas () - returnGas , err
11961218}
1197-
0 commit comments