|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package evm |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanchego/utils/timer/mockable" |
| 11 | + "github.com/ava-labs/avalanchego/vms/evm/acp226" |
| 12 | + "github.com/ava-labs/libevm/common" |
| 13 | + "github.com/ava-labs/libevm/core/types" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/ava-labs/coreth/params/extras" |
| 17 | + "github.com/ava-labs/coreth/plugin/evm/customtypes" |
| 18 | +) |
| 19 | + |
| 20 | +func TestCalculateBlockBuildingDelay(t *testing.T) { |
| 21 | + now := time.UnixMilli(10000) |
| 22 | + nowSecUint64 := uint64(now.Unix()) |
| 23 | + nowMilliUint64 := uint64(now.UnixMilli()) |
| 24 | + clock := &mockable.Clock{} |
| 25 | + clock.Set(now) |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + config *extras.ChainConfig |
| 29 | + currentHeader *types.Header |
| 30 | + lastBuildTime time.Time |
| 31 | + lastBuildParentHash common.Hash |
| 32 | + expectedTimeToWait time.Duration |
| 33 | + expectedShouldBuildNow bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "pre_granite_returns_build_immediately_zero_time", |
| 37 | + config: extras.TestFortunaChainConfig, // Pre-Granite config |
| 38 | + currentHeader: &types.Header{ |
| 39 | + ParentHash: common.Hash{1}, |
| 40 | + Time: nowSecUint64, |
| 41 | + }, |
| 42 | + lastBuildTime: time.Time{}, // Zero time means not a retry |
| 43 | + lastBuildParentHash: common.Hash{1}, |
| 44 | + expectedShouldBuildNow: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "pre_granite_returns_build_immediately_different_parent_hash", |
| 48 | + config: extras.TestFortunaChainConfig, // Pre-Granite config |
| 49 | + currentHeader: &types.Header{ |
| 50 | + ParentHash: common.Hash{2}, |
| 51 | + Time: nowSecUint64, |
| 52 | + }, |
| 53 | + lastBuildTime: now, |
| 54 | + lastBuildParentHash: common.Hash{1}, |
| 55 | + expectedShouldBuildNow: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "pre_granite_returns_build_delays_with_same_parent_hash", |
| 59 | + config: extras.TestFortunaChainConfig, // Pre-Granite config |
| 60 | + currentHeader: &types.Header{ |
| 61 | + ParentHash: common.Hash{1}, |
| 62 | + Time: nowSecUint64, |
| 63 | + }, |
| 64 | + lastBuildTime: now, |
| 65 | + lastBuildParentHash: common.Hash{1}, |
| 66 | + expectedTimeToWait: PreGraniteMinBlockBuildingRetryDelay, |
| 67 | + expectedShouldBuildNow: false, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "pre_granite_returns_build_returns_immediately_if_enough_time_passed", |
| 71 | + config: extras.TestFortunaChainConfig, // Pre-Granite config |
| 72 | + currentHeader: &types.Header{ |
| 73 | + ParentHash: common.Hash{1}, |
| 74 | + Time: nowSecUint64, |
| 75 | + }, |
| 76 | + lastBuildTime: now.Add(-PreGraniteMinBlockBuildingRetryDelay), // Less than retry delay ago |
| 77 | + lastBuildParentHash: common.Hash{1}, // Same as current parent |
| 78 | + expectedTimeToWait: 0, |
| 79 | + expectedShouldBuildNow: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "pre_granite_returns_build_delays_only_remaining_min_delay", |
| 83 | + config: extras.TestFortunaChainConfig, // Pre-Granite config |
| 84 | + currentHeader: &types.Header{ |
| 85 | + ParentHash: common.Hash{1}, |
| 86 | + Time: nowSecUint64, |
| 87 | + }, |
| 88 | + lastBuildTime: now.Add(-PreGraniteMinBlockBuildingRetryDelay / 2), // Less than retry delay ago |
| 89 | + lastBuildParentHash: common.Hash{1}, |
| 90 | + expectedTimeToWait: PreGraniteMinBlockBuildingRetryDelay / 2, |
| 91 | + expectedShouldBuildNow: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "granite_block_with_now_time", |
| 95 | + config: extras.TestGraniteChainConfig, |
| 96 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64, acp226.InitialDelayExcess), |
| 97 | + lastBuildTime: time.Time{}, |
| 98 | + lastBuildParentHash: common.Hash{1}, |
| 99 | + expectedTimeToWait: 2000 * time.Millisecond, // should wait for initial delay |
| 100 | + expectedShouldBuildNow: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "granite_block_with_2_seconds_before_clock_no_retry", |
| 104 | + config: extras.TestGraniteChainConfig, |
| 105 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64-2000, acp226.InitialDelayExcess), |
| 106 | + lastBuildTime: time.Time{}, // Zero time means not a retry |
| 107 | + lastBuildParentHash: common.Hash{1}, |
| 108 | + expectedTimeToWait: 0, // should not wait for initial delay |
| 109 | + expectedShouldBuildNow: true, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "granite_block_with_2_seconds_before_clock_with_retry", |
| 113 | + config: extras.TestGraniteChainConfig, |
| 114 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64-2000, acp226.InitialDelayExcess), |
| 115 | + lastBuildTime: now, |
| 116 | + lastBuildParentHash: common.Hash{1}, |
| 117 | + expectedTimeToWait: PostGraniteMinBlockBuildingRetryDelay, |
| 118 | + expectedShouldBuildNow: false, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "granite_with_2_seconds_before_clock_only_waits_for_retry_delay", |
| 122 | + config: extras.TestGraniteChainConfig, |
| 123 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64-2000, 0), // 0 means min delay excess which is 1 |
| 124 | + lastBuildTime: now, |
| 125 | + lastBuildParentHash: common.Hash{1}, |
| 126 | + expectedTimeToWait: PostGraniteMinBlockBuildingRetryDelay, |
| 127 | + expectedShouldBuildNow: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "granite_with_2_seconds_before_clock_only_waits_for_remaining_retry_delay", |
| 131 | + config: extras.TestGraniteChainConfig, |
| 132 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64-2000, 0), // 0 means min delay excess which is 1 |
| 133 | + lastBuildTime: now.Add(-PostGraniteMinBlockBuildingRetryDelay / 2), // Less than retry delay ago |
| 134 | + lastBuildParentHash: common.Hash{1}, |
| 135 | + expectedTimeToWait: PostGraniteMinBlockBuildingRetryDelay / 2, |
| 136 | + expectedShouldBuildNow: false, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "granite_with_2_seconds_after_clock", |
| 140 | + config: extras.TestGraniteChainConfig, |
| 141 | + currentHeader: createGraniteTestHeader(common.Hash{1}, nowMilliUint64+2000, acp226.InitialDelayExcess), |
| 142 | + lastBuildTime: time.Time{}, // Zero time means not a retry |
| 143 | + lastBuildParentHash: common.Hash{1}, |
| 144 | + expectedTimeToWait: 4000 * time.Millisecond, |
| 145 | + expectedShouldBuildNow: false, |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for _, tt := range tests { |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + b := &blockBuilder{ |
| 152 | + clock: clock, |
| 153 | + chainConfig: tt.config, |
| 154 | + } |
| 155 | + |
| 156 | + timeToWait, shouldBuildNow, err := b.calculateBlockBuildingDelay( |
| 157 | + tt.lastBuildTime, |
| 158 | + tt.lastBuildParentHash, |
| 159 | + tt.currentHeader, |
| 160 | + ) |
| 161 | + |
| 162 | + require.NoError(t, err) |
| 163 | + require.Equal(t, tt.expectedTimeToWait, timeToWait) |
| 164 | + require.Equal(t, tt.expectedShouldBuildNow, shouldBuildNow) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func createGraniteTestHeader(parentHash common.Hash, timeMilliseconds uint64, minDelayExcess acp226.DelayExcess) *types.Header { |
| 170 | + header := &types.Header{ |
| 171 | + Time: timeMilliseconds / 1000, |
| 172 | + } |
| 173 | + header.ParentHash = parentHash |
| 174 | + |
| 175 | + extra := &customtypes.HeaderExtra{ |
| 176 | + TimeMilliseconds: &timeMilliseconds, |
| 177 | + MinDelayExcess: &minDelayExcess, |
| 178 | + } |
| 179 | + customtypes.SetHeaderExtra(header, extra) |
| 180 | + |
| 181 | + return header |
| 182 | +} |
0 commit comments