|
| 1 | +package blobindexer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 8 | + "github.com/gobitfly/beaconchain/pkg/commons/types" |
| 9 | + "github.com/gobitfly/beaconchain/pkg/commons/utils" |
| 10 | + constypes "github.com/gobitfly/beaconchain/pkg/consapi/types" |
| 11 | +) |
| 12 | + |
| 13 | +func TestShouldSkipBlobIndexing(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + status *BlobIndexerStatus |
| 17 | + expected bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "CurrentBlobIndexerId and ID are the same", |
| 21 | + status: &BlobIndexerStatus{ |
| 22 | + CurrentBlobIndexerId: "1", |
| 23 | + LastUpdate: time.Now(), |
| 24 | + }, |
| 25 | + expected: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "different CurrentBlobIndexerId and ID", |
| 29 | + status: &BlobIndexerStatus{ |
| 30 | + CurrentBlobIndexerId: "2", |
| 31 | + LastUpdate: time.Now(), |
| 32 | + }, |
| 33 | + expected: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "valid LastUpdate time", |
| 37 | + status: &BlobIndexerStatus{ |
| 38 | + CurrentBlobIndexerId: "1", |
| 39 | + LastUpdate: time.Now().Add(-2 * waitForOtherBlobIndexerDuration), |
| 40 | + }, |
| 41 | + expected: false, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + bi := &BlobIndexer{id: "1"} |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + result := bi.shouldSkipBlobIndexing(tt.status) |
| 50 | + if result != tt.expected { |
| 51 | + t.Errorf("got %v, want %v", result, tt.expected) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestCalculateMinBlobSlot(t *testing.T) { |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + spec *constypes.StandardSpecResponse |
| 61 | + headHeader *constypes.StandardBeaconHeaderResponse |
| 62 | + pruneMarginEpochs uint64 |
| 63 | + expectedMinBlobSlot uint64 |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "normal case: head header slot greater than min blob slot range", |
| 67 | + spec: &constypes.StandardSpecResponse{ |
| 68 | + Data: constypes.StandardSpec{ |
| 69 | + MinEpochsForBlobSidecarsRequests: uint64Ptr(2), |
| 70 | + SlotsPerEpoch: 32, |
| 71 | + }, |
| 72 | + }, |
| 73 | + headHeader: &constypes.StandardBeaconHeaderResponse{ |
| 74 | + Data: struct { |
| 75 | + Root hexutil.Bytes `json:"root"` |
| 76 | + Header struct { |
| 77 | + Message struct { |
| 78 | + Slot uint64 `json:"slot,string"` |
| 79 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 80 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 81 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 82 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 83 | + } `json:"message"` |
| 84 | + Signature hexutil.Bytes `json:"signature"` |
| 85 | + } `json:"header"` |
| 86 | + }{ |
| 87 | + Header: struct { |
| 88 | + Message struct { |
| 89 | + Slot uint64 `json:"slot,string"` |
| 90 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 91 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 92 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 93 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 94 | + } `json:"message"` |
| 95 | + Signature hexutil.Bytes `json:"signature"` |
| 96 | + }{ |
| 97 | + Message: struct { |
| 98 | + Slot uint64 `json:"slot,string"` |
| 99 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 100 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 101 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 102 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 103 | + }{ |
| 104 | + Slot: 100, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + pruneMarginEpochs: 1, |
| 110 | + expectedMinBlobSlot: 4, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "head header slot equal to min blob slot range", |
| 114 | + spec: &constypes.StandardSpecResponse{ |
| 115 | + Data: constypes.StandardSpec{ |
| 116 | + MinEpochsForBlobSidecarsRequests: uint64Ptr(2), |
| 117 | + SlotsPerEpoch: 32, |
| 118 | + }, |
| 119 | + }, |
| 120 | + headHeader: &constypes.StandardBeaconHeaderResponse{ |
| 121 | + Data: struct { |
| 122 | + Root hexutil.Bytes `json:"root"` |
| 123 | + Header struct { |
| 124 | + Message struct { |
| 125 | + Slot uint64 `json:"slot,string"` |
| 126 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 127 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 128 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 129 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 130 | + } `json:"message"` |
| 131 | + Signature hexutil.Bytes `json:"signature"` |
| 132 | + } `json:"header"` |
| 133 | + }{ |
| 134 | + Header: struct { |
| 135 | + Message struct { |
| 136 | + Slot uint64 `json:"slot,string"` |
| 137 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 138 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 139 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 140 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 141 | + } `json:"message"` |
| 142 | + Signature hexutil.Bytes `json:"signature"` |
| 143 | + }{ |
| 144 | + Message: struct { |
| 145 | + Slot uint64 `json:"slot,string"` |
| 146 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 147 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 148 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 149 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 150 | + }{ |
| 151 | + Slot: 64, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + pruneMarginEpochs: 1, |
| 157 | + expectedMinBlobSlot: 0, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "head header slot less than min blob slot range", |
| 161 | + spec: &constypes.StandardSpecResponse{ |
| 162 | + Data: constypes.StandardSpec{ |
| 163 | + MinEpochsForBlobSidecarsRequests: uint64Ptr(2), |
| 164 | + SlotsPerEpoch: 32, |
| 165 | + }, |
| 166 | + }, |
| 167 | + headHeader: &constypes.StandardBeaconHeaderResponse{ |
| 168 | + Data: struct { |
| 169 | + Root hexutil.Bytes `json:"root"` |
| 170 | + Header struct { |
| 171 | + Message struct { |
| 172 | + Slot uint64 `json:"slot,string"` |
| 173 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 174 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 175 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 176 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 177 | + } `json:"message"` |
| 178 | + Signature hexutil.Bytes `json:"signature"` |
| 179 | + } `json:"header"` |
| 180 | + }{ |
| 181 | + Header: struct { |
| 182 | + Message struct { |
| 183 | + Slot uint64 `json:"slot,string"` |
| 184 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 185 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 186 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 187 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 188 | + } `json:"message"` |
| 189 | + Signature hexutil.Bytes `json:"signature"` |
| 190 | + }{ |
| 191 | + Message: struct { |
| 192 | + Slot uint64 `json:"slot,string"` |
| 193 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 194 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 195 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 196 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 197 | + }{ |
| 198 | + Slot: 50, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + pruneMarginEpochs: 1, |
| 204 | + expectedMinBlobSlot: 0, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "prune margin slot range greater than calculated min blob slot", |
| 208 | + spec: &constypes.StandardSpecResponse{ |
| 209 | + Data: constypes.StandardSpec{ |
| 210 | + MinEpochsForBlobSidecarsRequests: uint64Ptr(2), |
| 211 | + SlotsPerEpoch: 32, |
| 212 | + }, |
| 213 | + }, |
| 214 | + headHeader: &constypes.StandardBeaconHeaderResponse{ |
| 215 | + Data: struct { |
| 216 | + Root hexutil.Bytes `json:"root"` |
| 217 | + Header struct { |
| 218 | + Message struct { |
| 219 | + Slot uint64 `json:"slot,string"` |
| 220 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 221 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 222 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 223 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 224 | + } `json:"message"` |
| 225 | + Signature hexutil.Bytes `json:"signature"` |
| 226 | + } `json:"header"` |
| 227 | + }{ |
| 228 | + Header: struct { |
| 229 | + Message struct { |
| 230 | + Slot uint64 `json:"slot,string"` |
| 231 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 232 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 233 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 234 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 235 | + } `json:"message"` |
| 236 | + Signature hexutil.Bytes `json:"signature"` |
| 237 | + }{ |
| 238 | + Message: struct { |
| 239 | + Slot uint64 `json:"slot,string"` |
| 240 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 241 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 242 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 243 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 244 | + }{ |
| 245 | + Slot: 100, |
| 246 | + }, |
| 247 | + }, |
| 248 | + }, |
| 249 | + }, |
| 250 | + pruneMarginEpochs: 4, |
| 251 | + expectedMinBlobSlot: 36, |
| 252 | + }, |
| 253 | + { |
| 254 | + name: "prune margin slot range equal to calculated min blob slot", |
| 255 | + spec: &constypes.StandardSpecResponse{ |
| 256 | + Data: constypes.StandardSpec{ |
| 257 | + MinEpochsForBlobSidecarsRequests: uint64Ptr(2), |
| 258 | + SlotsPerEpoch: 32, |
| 259 | + }, |
| 260 | + }, |
| 261 | + headHeader: &constypes.StandardBeaconHeaderResponse{ |
| 262 | + Data: struct { |
| 263 | + Root hexutil.Bytes `json:"root"` |
| 264 | + Header struct { |
| 265 | + Message struct { |
| 266 | + Slot uint64 `json:"slot,string"` |
| 267 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 268 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 269 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 270 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 271 | + } `json:"message"` |
| 272 | + Signature hexutil.Bytes `json:"signature"` |
| 273 | + } `json:"header"` |
| 274 | + }{ |
| 275 | + Header: struct { |
| 276 | + Message struct { |
| 277 | + Slot uint64 `json:"slot,string"` |
| 278 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 279 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 280 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 281 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 282 | + } `json:"message"` |
| 283 | + Signature hexutil.Bytes `json:"signature"` |
| 284 | + }{ |
| 285 | + Message: struct { |
| 286 | + Slot uint64 `json:"slot,string"` |
| 287 | + ProposerIndex uint64 `json:"proposer_index,string"` |
| 288 | + ParentRoot hexutil.Bytes `json:"parent_root"` |
| 289 | + StateRoot hexutil.Bytes `json:"state_root"` |
| 290 | + BodyRoot hexutil.Bytes `json:"body_root"` |
| 291 | + }{ |
| 292 | + Slot: 128, |
| 293 | + }, |
| 294 | + }, |
| 295 | + }, |
| 296 | + }, |
| 297 | + pruneMarginEpochs: 2, |
| 298 | + expectedMinBlobSlot: 64, |
| 299 | + }, |
| 300 | + } |
| 301 | + |
| 302 | + for _, tt := range tests { |
| 303 | + t.Run(tt.name, func(t *testing.T) { |
| 304 | + utils.Config = &types.Config{ |
| 305 | + BlobIndexer: types.BlobIndexerConfig{ |
| 306 | + PruneMarginEpochs: tt.pruneMarginEpochs, |
| 307 | + }, |
| 308 | + } |
| 309 | + |
| 310 | + minBlobSlot := calculateMinBlobSlot(tt.spec, tt.headHeader) |
| 311 | + if minBlobSlot != tt.expectedMinBlobSlot { |
| 312 | + t.Errorf("got minBlobSlot %v, want %v", minBlobSlot, tt.expectedMinBlobSlot) |
| 313 | + } |
| 314 | + }) |
| 315 | + } |
| 316 | +} |
| 317 | + |
| 318 | +// unit64Ptr returns a pointer to a uint64 |
| 319 | +func uint64Ptr(i uint64) *uint64 { |
| 320 | + return &i |
| 321 | +} |
0 commit comments