mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The module already called itself github.com/luxfi/node while containing no node in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and the Go column of the matrix measured a VM plugin. A repo that fetches the thing it claims to be has not replaced it, so the Go sources move in — 1515 files, the daemon and everything under it — and the build reaches outside this module for nothing. Rust and C++ sit beside it unchanged and are still measured against it by the differential. That is the point of one repo: the reference implementation and the two ports are the same checkout, so parity is a thing you can run rather than a thing you assert. Verified: builds, and the closure carries no lux-private — checked at build time rather than believed. The daemon starts twelve chains on a local network (P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
73 lines
2.5 KiB
Go
73 lines
2.5 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
// LP-226 implements the dynamic minimum block delay mechanism specified here:
|
|
// https://github.com/luxfi/lps/blob/main/LPs/226-dynamic-minimum-block-times/README.md
|
|
package lp226
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/luxfi/node/vms/components/gas"
|
|
|
|
safemath "github.com/luxfi/math"
|
|
)
|
|
|
|
const (
|
|
// MinDelayMilliseconds (M) is the minimum block delay in milliseconds
|
|
MinDelayMilliseconds = 1 // ms
|
|
// ConversionRate (D) is the conversion factor for exponential calculations
|
|
ConversionRate = 1 << 20
|
|
// MaxDelayExcessDiff (Q) is the maximum change in excess per update
|
|
MaxDelayExcessDiff = 200
|
|
|
|
// InitialDelayExcess represents the initial (≈2000ms) delay excess.
|
|
// Formula: ConversionRate (2^20) * ln(2000) + 1
|
|
InitialDelayExcess = 7_970_124
|
|
|
|
maxDelayExcess = 46_516_320 // ConversionRate * ln(MaxUint64 / MinDelayMilliseconds) + 1
|
|
)
|
|
|
|
// DelayExcess represents the excess for delay calculation in the dynamic minimum block delay mechanism.
|
|
type DelayExcess uint64
|
|
|
|
// Delay returns the minimum block delay in milliseconds, `m`.
|
|
//
|
|
// Delay = MinDelayMilliseconds * e^(DelayExcess / ConversionRate)
|
|
func (t DelayExcess) Delay() uint64 {
|
|
return uint64(gas.CalculatePrice(
|
|
MinDelayMilliseconds,
|
|
gas.Gas(t),
|
|
ConversionRate,
|
|
))
|
|
}
|
|
|
|
// UpdateDelayExcess updates the DelayExcess to be as close as possible to the
|
|
// desiredDelayExcess without exceeding the maximum DelayExcess change.
|
|
func (t *DelayExcess) UpdateDelayExcess(desiredDelayExcess uint64) {
|
|
*t = DelayExcess(calculateDelayExcess(uint64(*t), desiredDelayExcess))
|
|
}
|
|
|
|
// DesiredDelayExcess calculates the optimal delay excess given the desired
|
|
// delay.
|
|
func DesiredDelayExcess(desiredDelay uint64) uint64 {
|
|
// This could be solved directly by calculating D * ln(desired / M)
|
|
// using floating point math. However, it introduces inaccuracies. So, we
|
|
// use a binary search to find the closest integer solution.
|
|
return uint64(sort.Search(maxDelayExcess, func(delayExcessGuess int) bool {
|
|
excess := DelayExcess(delayExcessGuess)
|
|
return excess.Delay() >= desiredDelay
|
|
}))
|
|
}
|
|
|
|
// calculateDelayExcess calculates the optimal new DelayExcess for a block proposer to
|
|
// include given the current and desired excess values.
|
|
func calculateDelayExcess(excess, desired uint64) uint64 {
|
|
change := safemath.AbsDiff(excess, desired)
|
|
change = min(change, MaxDelayExcessDiff)
|
|
if excess < desired {
|
|
return excess + change
|
|
}
|
|
return excess - change
|
|
}
|