Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound the time diff between blocks to max of 100 secs #2221

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading