Skip to content

Commit 8b8ac94

Browse files
Use acp226.DelayExcess rather than uint64 (#4358)
1 parent 32806e0 commit 8b8ac94

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

vms/evm/acp226/acp226.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const (
2828
maxDelayExcess = 46_516_320 // ConversionRate * ln(MaxUint64 / MinDelayMilliseconds) + 1
2929
)
3030

31-
// DelayExcess represents the excess for delay calculation in the dynamic minimum block delay mechanism.
31+
// DelayExcess represents the excess for delay calculation in the dynamic
32+
// minimum block delay mechanism.
3233
type DelayExcess uint64
3334

3435
// Delay returns the minimum block delay in milliseconds, `m`.
@@ -44,25 +45,25 @@ func (t DelayExcess) Delay() uint64 {
4445

4546
// UpdateDelayExcess updates the DelayExcess to be as close as possible to the
4647
// desiredDelayExcess without exceeding the maximum DelayExcess change.
47-
func (t *DelayExcess) UpdateDelayExcess(desiredDelayExcess uint64) {
48-
*t = DelayExcess(calculateDelayExcess(uint64(*t), desiredDelayExcess))
48+
func (t *DelayExcess) UpdateDelayExcess(desiredDelayExcess DelayExcess) {
49+
*t = calculateDelayExcess(*t, desiredDelayExcess)
4950
}
5051

5152
// DesiredDelayExcess calculates the optimal delay excess given the desired
52-
// delay.
53-
func DesiredDelayExcess(desiredDelay uint64) uint64 {
53+
// delay in milliseconds.
54+
func DesiredDelayExcess(desiredDelay uint64) DelayExcess {
5455
// This could be solved directly by calculating D * ln(desired / M)
5556
// using floating point math. However, it introduces inaccuracies. So, we
5657
// use a binary search to find the closest integer solution.
57-
return uint64(sort.Search(maxDelayExcess, func(delayExcessGuess int) bool {
58+
return DelayExcess(sort.Search(maxDelayExcess, func(delayExcessGuess int) bool {
5859
excess := DelayExcess(delayExcessGuess)
5960
return excess.Delay() >= desiredDelay
6061
}))
6162
}
6263

63-
// calculateDelayExcess calculates the optimal new DelayExcess for a block proposer to
64-
// include given the current and desired excess values.
65-
func calculateDelayExcess(excess, desired uint64) uint64 {
64+
// calculateDelayExcess calculates the optimal new DelayExcess for a block
65+
// proposer to include given the current and desired excess values.
66+
func calculateDelayExcess(excess, desired DelayExcess) DelayExcess {
6667
change := safemath.AbsDiff(excess, desired)
6768
change = min(change, MaxDelayExcessDiff)
6869
if excess < desired {

vms/evm/acp226/acp226_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var (
9898
updateExcessTests = []struct {
9999
name string
100100
initial DelayExcess
101-
desiredExcess uint64
101+
desiredExcess DelayExcess
102102
expected DelayExcess
103103
}{
104104
{
@@ -203,7 +203,7 @@ func TestDesiredDelayExcess(t *testing.T) {
203203
continue
204204
}
205205
t.Run(test.name, func(t *testing.T) {
206-
require.Equal(t, test.excess, DelayExcess(DesiredDelayExcess(test.delay)))
206+
require.Equal(t, test.excess, DesiredDelayExcess(test.delay))
207207
})
208208
}
209209
}

0 commit comments

Comments
 (0)