|
| 1 | +import {describe, expect, it, vi} from "vitest"; |
| 2 | +import {ForkName} from "@lodestar/params"; |
| 3 | +import {ssz} from "@lodestar/types"; |
| 4 | +import {toRootHex} from "@lodestar/utils"; |
| 5 | +import {onExecutionPayloadEnvelopesByRange} from "../../../../src/network/reqresp/handlers/executionPayloadEnvelopesByRange.js"; |
| 6 | + |
| 7 | +function rootWithByte(n: number): Uint8Array { |
| 8 | + const root = new Uint8Array(32); |
| 9 | + root[31] = n; |
| 10 | + return root; |
| 11 | +} |
| 12 | + |
| 13 | +describe("beacon-node / network / reqresp / handlers / executionPayloadEnvelopesByRange", () => { |
| 14 | + it("serves envelopes from finalized archive and non-finalized cache", async () => { |
| 15 | + const finalizedEnvelope = ssz.gloas.SignedExecutionPayloadEnvelope.defaultValue(); |
| 16 | + finalizedEnvelope.message.slot = 64; |
| 17 | + finalizedEnvelope.message.beaconBlockRoot = rootWithByte(64); |
| 18 | + |
| 19 | + const hotEnvelope = ssz.gloas.SignedExecutionPayloadEnvelope.defaultValue(); |
| 20 | + hotEnvelope.message.slot = 65; |
| 21 | + hotEnvelope.message.beaconBlockRoot = rootWithByte(65); |
| 22 | + |
| 23 | + const archivedBySlot = new Map<number, typeof finalizedEnvelope>([[64, finalizedEnvelope]]); |
| 24 | + const hotByRoot = new Map<string, typeof hotEnvelope>([ |
| 25 | + [toRootHex(hotEnvelope.message.beaconBlockRoot), hotEnvelope], |
| 26 | + ]); |
| 27 | + |
| 28 | + const chain = { |
| 29 | + earliestAvailableSlot: 1, |
| 30 | + logger: {verbose: vi.fn()}, |
| 31 | + config: { |
| 32 | + SLOTS_PER_EPOCH: 32, |
| 33 | + MAX_REQUEST_BLOCKS: 1024, |
| 34 | + MAX_REQUEST_BLOCKS_DENEB: 128, |
| 35 | + getForkName: () => ForkName.gloas, |
| 36 | + getForkBoundaryAtEpoch: (epoch: number) => ({fork: ForkName.gloas, epoch}), |
| 37 | + }, |
| 38 | + forkChoice: { |
| 39 | + getFinalizedCheckpointSlot: () => 64, |
| 40 | + getHeadRoot: () => "0xhead", |
| 41 | + getBlockHexDefaultStatus: () => ({slot: 66, blockRoot: toRootHex(rootWithByte(66))}), |
| 42 | + getAllAncestorBlocks: () => [ |
| 43 | + {slot: 66, blockRoot: toRootHex(rootWithByte(66))}, |
| 44 | + {slot: 65, blockRoot: toRootHex(rootWithByte(65))}, |
| 45 | + {slot: 64, blockRoot: toRootHex(rootWithByte(64))}, |
| 46 | + ], |
| 47 | + }, |
| 48 | + } as any; |
| 49 | + |
| 50 | + const db = { |
| 51 | + executionPayloadEnvelopeArchive: { |
| 52 | + get: vi.fn(async (slot: number) => archivedBySlot.get(slot) ?? null), |
| 53 | + }, |
| 54 | + executionPayloadEnvelope: { |
| 55 | + get: vi.fn(async (root: Uint8Array) => hotByRoot.get(toRootHex(root)) ?? null), |
| 56 | + }, |
| 57 | + } as any; |
| 58 | + |
| 59 | + const request = {startSlot: 64, count: 2}; |
| 60 | + |
| 61 | + const responses = []; |
| 62 | + for await (const response of onExecutionPayloadEnvelopesByRange(request, chain, db)) { |
| 63 | + responses.push(ssz.gloas.SignedExecutionPayloadEnvelope.deserialize(response.data)); |
| 64 | + } |
| 65 | + |
| 66 | + expect(responses).toHaveLength(2); |
| 67 | + expect(responses.map((e) => e.message.slot)).toEqual([64, 65]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns nothing for requests below earliestAvailableSlot", async () => { |
| 71 | + const chain = { |
| 72 | + earliestAvailableSlot: 10, |
| 73 | + logger: {verbose: vi.fn()}, |
| 74 | + config: { |
| 75 | + SLOTS_PER_EPOCH: 32, |
| 76 | + MAX_REQUEST_BLOCKS: 1024, |
| 77 | + MAX_REQUEST_BLOCKS_DENEB: 128, |
| 78 | + getForkName: () => ForkName.gloas, |
| 79 | + getForkBoundaryAtEpoch: (epoch: number) => ({fork: ForkName.gloas, epoch}), |
| 80 | + }, |
| 81 | + forkChoice: { |
| 82 | + getFinalizedCheckpointSlot: () => 0, |
| 83 | + getHeadRoot: () => "0xhead", |
| 84 | + getBlockHexDefaultStatus: () => undefined, |
| 85 | + getAllAncestorBlocks: () => [], |
| 86 | + }, |
| 87 | + } as any; |
| 88 | + |
| 89 | + const db = { |
| 90 | + executionPayloadEnvelopeArchive: {get: vi.fn()}, |
| 91 | + executionPayloadEnvelope: {get: vi.fn()}, |
| 92 | + } as any; |
| 93 | + |
| 94 | + const request = {startSlot: 1, count: 1}; |
| 95 | + |
| 96 | + const responses = []; |
| 97 | + for await (const response of onExecutionPayloadEnvelopesByRange(request, chain, db)) { |
| 98 | + responses.push(response); |
| 99 | + } |
| 100 | + |
| 101 | + expect(responses).toHaveLength(0); |
| 102 | + expect(chain.logger.verbose).toHaveBeenCalledTimes(1); |
| 103 | + expect(db.executionPayloadEnvelopeArchive.get).not.toHaveBeenCalled(); |
| 104 | + expect(db.executionPayloadEnvelope.get).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("includes head envelope when ancestor list excludes head", async () => { |
| 108 | + const finalizedEnvelope = ssz.gloas.SignedExecutionPayloadEnvelope.defaultValue(); |
| 109 | + finalizedEnvelope.message.slot = 64; |
| 110 | + finalizedEnvelope.message.beaconBlockRoot = rootWithByte(64); |
| 111 | + |
| 112 | + const headEnvelope = ssz.gloas.SignedExecutionPayloadEnvelope.defaultValue(); |
| 113 | + headEnvelope.message.slot = 66; |
| 114 | + headEnvelope.message.beaconBlockRoot = rootWithByte(66); |
| 115 | + |
| 116 | + const slot65Envelope = ssz.gloas.SignedExecutionPayloadEnvelope.defaultValue(); |
| 117 | + slot65Envelope.message.slot = 65; |
| 118 | + slot65Envelope.message.beaconBlockRoot = rootWithByte(65); |
| 119 | + |
| 120 | + const archivedBySlot = new Map<number, typeof finalizedEnvelope>([[64, finalizedEnvelope]]); |
| 121 | + const hotByRoot = new Map<string, typeof headEnvelope>([ |
| 122 | + [toRootHex(headEnvelope.message.beaconBlockRoot), headEnvelope], |
| 123 | + [toRootHex(slot65Envelope.message.beaconBlockRoot), slot65Envelope], |
| 124 | + ]); |
| 125 | + |
| 126 | + const chain = { |
| 127 | + earliestAvailableSlot: 1, |
| 128 | + logger: {verbose: vi.fn()}, |
| 129 | + config: { |
| 130 | + SLOTS_PER_EPOCH: 32, |
| 131 | + MAX_REQUEST_BLOCKS: 1024, |
| 132 | + MAX_REQUEST_BLOCKS_DENEB: 128, |
| 133 | + getForkName: () => ForkName.gloas, |
| 134 | + getForkBoundaryAtEpoch: (epoch: number) => ({fork: ForkName.gloas, epoch}), |
| 135 | + }, |
| 136 | + forkChoice: { |
| 137 | + getFinalizedCheckpointSlot: () => 64, |
| 138 | + getHeadRoot: () => "0xhead", |
| 139 | + getBlockHexDefaultStatus: () => ({slot: 66, blockRoot: toRootHex(rootWithByte(66))}), |
| 140 | + getAllAncestorBlocks: () => [ |
| 141 | + {slot: 65, blockRoot: toRootHex(rootWithByte(65))}, |
| 142 | + {slot: 64, blockRoot: toRootHex(rootWithByte(64))}, |
| 143 | + ], |
| 144 | + }, |
| 145 | + } as any; |
| 146 | + |
| 147 | + const db = { |
| 148 | + executionPayloadEnvelopeArchive: { |
| 149 | + get: vi.fn(async (slot: number) => archivedBySlot.get(slot) ?? null), |
| 150 | + }, |
| 151 | + executionPayloadEnvelope: { |
| 152 | + get: vi.fn(async (root: Uint8Array) => hotByRoot.get(toRootHex(root)) ?? null), |
| 153 | + }, |
| 154 | + } as any; |
| 155 | + |
| 156 | + const request = {startSlot: 64, count: 3}; |
| 157 | + |
| 158 | + const responses = []; |
| 159 | + for await (const response of onExecutionPayloadEnvelopesByRange(request, chain, db)) { |
| 160 | + responses.push(ssz.gloas.SignedExecutionPayloadEnvelope.deserialize(response.data)); |
| 161 | + } |
| 162 | + |
| 163 | + expect(responses.map((e) => e.message.slot)).toEqual([64, 65, 66]); |
| 164 | + }); |
| 165 | +}); |
0 commit comments