|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package simplex |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/ava-labs/simplex" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/ava-labs/avalanchego/snow/consensus/snowman" |
| 16 | + "github.com/ava-labs/avalanchego/snow/engine/common" |
| 17 | + "github.com/ava-labs/avalanchego/utils/logging" |
| 18 | +) |
| 19 | + |
| 20 | +func TestBlockBuilder(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + genesis := newTestBlock(t, newBlockConfig{}) |
| 23 | + child := newTestBlock(t, newBlockConfig{ |
| 24 | + prev: genesis, |
| 25 | + }) |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + block snowman.Block |
| 30 | + shouldBuild bool |
| 31 | + expectedBlock simplex.VerifiedBlock |
| 32 | + vmBlockBuildF func(ctx context.Context) (snowman.Block, error) |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "build block successfully", |
| 36 | + block: child.vmBlock, |
| 37 | + shouldBuild: true, |
| 38 | + expectedBlock: child, |
| 39 | + vmBlockBuildF: func(_ context.Context) (snowman.Block, error) { |
| 40 | + return child.vmBlock, nil |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "fail to build block", |
| 45 | + block: nil, |
| 46 | + shouldBuild: false, |
| 47 | + vmBlockBuildF: func(_ context.Context) (snowman.Block, error) { |
| 48 | + return nil, errors.New("failed to build block") |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "fail to verify block", |
| 53 | + block: nil, |
| 54 | + vmBlockBuildF: func(_ context.Context) (snowman.Block, error) { |
| 55 | + b := newTestBlock(t, newBlockConfig{ |
| 56 | + prev: genesis, |
| 57 | + }) |
| 58 | + b.vmBlock.(*wrappedBlock).VerifyV = errors.New("verification failed") |
| 59 | + return b.vmBlock, nil |
| 60 | + }, |
| 61 | + shouldBuild: false, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + count := 0 |
| 68 | + vm := newTestVM() |
| 69 | + |
| 70 | + vm.WaitForEventF = func(_ context.Context) (common.Message, error) { |
| 71 | + count++ |
| 72 | + return common.PendingTxs, nil |
| 73 | + } |
| 74 | + vm.BuildBlockF = tt.vmBlockBuildF |
| 75 | + |
| 76 | + bb := &BlockBuilder{ |
| 77 | + log: logging.NoLog{}, |
| 78 | + vm: vm, |
| 79 | + blockTracker: genesis.blockTracker, |
| 80 | + } |
| 81 | + |
| 82 | + timeoutCtx, cancelCtx := context.WithTimeout(ctx, 100*time.Millisecond) |
| 83 | + defer cancelCtx() |
| 84 | + |
| 85 | + block, built := bb.BuildBlock(timeoutCtx, child.BlockHeader().ProtocolMetadata) |
| 86 | + require.Equal(t, tt.shouldBuild, built) |
| 87 | + require.Equal(t, tt.expectedBlock, block) |
| 88 | + if tt.expectedBlock == nil { |
| 89 | + require.Greater(t, count, 1) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestBlockBuilderCancelContext(t *testing.T) { |
| 96 | + ctx := context.Background() |
| 97 | + vm := newTestVM() |
| 98 | + genesis := newTestBlock(t, newBlockConfig{}) |
| 99 | + child := newTestBlock(t, newBlockConfig{ |
| 100 | + prev: genesis, |
| 101 | + }) |
| 102 | + vm.WaitForEventF = func(ctx context.Context) (common.Message, error) { |
| 103 | + <-ctx.Done() |
| 104 | + return 0, ctx.Err() |
| 105 | + } |
| 106 | + |
| 107 | + bb := &BlockBuilder{ |
| 108 | + log: logging.NoLog{}, |
| 109 | + vm: vm, |
| 110 | + blockTracker: genesis.blockTracker, |
| 111 | + } |
| 112 | + |
| 113 | + timeoutCtx, cancelCtx := context.WithTimeout(ctx, 100*time.Millisecond) |
| 114 | + defer cancelCtx() |
| 115 | + |
| 116 | + _, built := bb.BuildBlock(timeoutCtx, child.BlockHeader().ProtocolMetadata) |
| 117 | + require.False(t, built, "Block should not be built when context is cancelled") |
| 118 | +} |
| 119 | + |
| 120 | +func TestWaitForPendingBlock(t *testing.T) { |
| 121 | + ctx := context.Background() |
| 122 | + vm := newTestVM() |
| 123 | + genesis := newTestBlock(t, newBlockConfig{}) |
| 124 | + count := 0 |
| 125 | + vm.WaitForEventF = func(_ context.Context) (common.Message, error) { |
| 126 | + if count == 0 { |
| 127 | + count++ |
| 128 | + return common.StateSyncDone, nil |
| 129 | + } |
| 130 | + return common.PendingTxs, nil |
| 131 | + } |
| 132 | + |
| 133 | + bb := &BlockBuilder{ |
| 134 | + log: logging.NoLog{}, |
| 135 | + vm: vm, |
| 136 | + blockTracker: genesis.blockTracker, |
| 137 | + } |
| 138 | + |
| 139 | + bb.WaitForPendingBlock(ctx) |
| 140 | + require.Equal(t, 1, count) |
| 141 | +} |
| 142 | + |
| 143 | +func TestBlockBuildingExponentialBackoff(t *testing.T) { |
| 144 | + ctx := context.Background() |
| 145 | + vm := newTestVM() |
| 146 | + genesis := newTestBlock(t, newBlockConfig{}) |
| 147 | + child := newTestBlock(t, newBlockConfig{ |
| 148 | + prev: genesis, |
| 149 | + }) |
| 150 | + const ( |
| 151 | + failedAttempts = 7 |
| 152 | + minimumExpectedDelay = initBackoff * (1<<failedAttempts - 1) |
| 153 | + ) |
| 154 | + |
| 155 | + vm.WaitForEventF = func(_ context.Context) (common.Message, error) { |
| 156 | + return common.PendingTxs, nil |
| 157 | + } |
| 158 | + |
| 159 | + count := 0 |
| 160 | + vm.BuildBlockF = func(_ context.Context) (snowman.Block, error) { |
| 161 | + if count < failedAttempts { |
| 162 | + count++ |
| 163 | + return nil, errors.New("failed to build block") |
| 164 | + } |
| 165 | + |
| 166 | + // on the 8th try, return the block successfully |
| 167 | + return child.vmBlock, nil |
| 168 | + } |
| 169 | + |
| 170 | + bb := &BlockBuilder{ |
| 171 | + log: logging.NoLog{}, |
| 172 | + vm: vm, |
| 173 | + blockTracker: genesis.blockTracker, |
| 174 | + } |
| 175 | + |
| 176 | + start := time.Now() |
| 177 | + block, built := bb.BuildBlock(ctx, child.BlockHeader().ProtocolMetadata) |
| 178 | + endTime := time.Since(start) |
| 179 | + |
| 180 | + require.True(t, built) |
| 181 | + require.Equal(t, child.BlockHeader(), block.BlockHeader()) |
| 182 | + |
| 183 | + // 10, 20, 40, 80, 160, 320, 640 = 1270ms |
| 184 | + require.GreaterOrEqual(t, endTime, minimumExpectedDelay) |
| 185 | +} |
| 186 | + |
| 187 | +func TestWaitForPendingBlockBackoff(t *testing.T) { |
| 188 | + ctx := context.Background() |
| 189 | + vm := newTestVM() |
| 190 | + const ( |
| 191 | + failedAttempts = 7 |
| 192 | + minimumExpectedDelay = initBackoff * (1<<failedAttempts - 1) |
| 193 | + ) |
| 194 | + |
| 195 | + count := 0 |
| 196 | + vm.WaitForEventF = func(_ context.Context) (common.Message, error) { |
| 197 | + if count < failedAttempts { |
| 198 | + count++ |
| 199 | + return common.StateSyncDone, nil |
| 200 | + } |
| 201 | + |
| 202 | + return common.PendingTxs, nil |
| 203 | + } |
| 204 | + |
| 205 | + bb := &BlockBuilder{ |
| 206 | + log: logging.NoLog{}, |
| 207 | + vm: vm, |
| 208 | + blockTracker: nil, |
| 209 | + } |
| 210 | + |
| 211 | + start := time.Now() |
| 212 | + bb.WaitForPendingBlock(ctx) |
| 213 | + endTime := time.Since(start) |
| 214 | + |
| 215 | + // 10, 20, 40, 80, 160, 320, 640 = 1270ms |
| 216 | + require.GreaterOrEqual(t, endTime, minimumExpectedDelay) |
| 217 | +} |
0 commit comments