From 2caa1dc35fd198375f368785ed97c76f94554fb9 Mon Sep 17 00:00:00 2001 From: gop Date: Fri, 11 Oct 2024 11:51:33 -0500 Subject: [PATCH] Bound the time diff between blocks to max of 100 secs --- consensus/blake3pow/consensus.go | 10 ++++++++-- consensus/progpow/consensus.go | 10 ++++++++-- params/protocol_params.go | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/consensus/blake3pow/consensus.go b/consensus/blake3pow/consensus.go index ad86db881..3b974e4bf 100644 --- a/consensus/blake3pow/consensus.go +++ b/consensus/blake3pow/consensus.go @@ -531,9 +531,15 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa bigTime := new(big.Int).SetUint64(time) bigParentTime := new(big.Int).SetUint64(parentOfParent.Time()) + // Bound the time diff so that the difficulty doesnt have huge discontinuity + // in the values + timeDiff := new(big.Int).Sub(bigTime, bigParentTime) + if timeDiff.Cmp(big.NewInt(params.MaxTimeDiffBetweenBlocks)) > 0 { + timeDiff = big.NewInt(params.MaxTimeDiffBetweenBlocks) + } + // holds intermediate values to make the algo easier to read & audit - x := new(big.Int) - x.Sub(bigTime, bigParentTime) + x := new(big.Int).Set(timeDiff) x.Sub(blake3pow.config.DurationLimit, x) x.Mul(x, parent.Difficulty()) k, _ := mathutil.BinaryLog(new(big.Int).Set(parent.Difficulty()), 64) diff --git a/consensus/progpow/consensus.go b/consensus/progpow/consensus.go index bacf41bdd..7310b8cf0 100644 --- a/consensus/progpow/consensus.go +++ b/consensus/progpow/consensus.go @@ -537,9 +537,15 @@ func (progpow *Progpow) CalcDifficulty(chain consensus.ChainHeaderReader, parent bigTime := new(big.Int).SetUint64(time) bigParentTime := new(big.Int).SetUint64(parentOfParent.Time()) + // Bound the time diff so that the difficulty doesnt have huge discontinuity + // in the values + timeDiff := new(big.Int).Sub(bigTime, bigParentTime) + if timeDiff.Cmp(big.NewInt(params.MaxTimeDiffBetweenBlocks)) > 0 { + timeDiff = big.NewInt(params.MaxTimeDiffBetweenBlocks) + } + // holds intermediate values to make the algo easier to read & audit - x := new(big.Int) - x.Sub(bigTime, bigParentTime) + x := new(big.Int).Set(timeDiff) x.Sub(progpow.config.DurationLimit, x) x.Mul(x, parent.Difficulty()) k, _ := mathutil.BinaryLog(new(big.Int).Set(parent.Difficulty()), 64) diff --git a/params/protocol_params.go b/params/protocol_params.go index 938aa34e2..88bb06841 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -187,6 +187,8 @@ var ( // These numbers should be "equivalent" to the initial conversion rate QuaiToQiConversionBase = big.NewInt(10000000) // Is the starting "historical conversion" in Qits for 10,000 Quai we need 10,000*1e3 QiToQuaiConversionBase = big.NewInt(10000000) // Is the starting "historical conversion" in Qits for 10,000 Qi we need 10,000*1e3 + + MaxTimeDiffBetweenBlocks int64 = 100 // Max time difference between the blocks to 100 secs ) func init() {