|
| 1 | +import { type BlockBlobData, type CheckpointBlobData, makeBlockEndBlobData } from '@aztec/blob-lib/encoding'; |
| 2 | +import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types'; |
| 3 | +import { Fr } from '@aztec/foundation/curves/bn254'; |
| 4 | +import { Body, CommitteeAttestation } from '@aztec/stdlib/block'; |
| 5 | +import { L1PublishedData } from '@aztec/stdlib/checkpoint'; |
| 6 | +import { CheckpointHeader } from '@aztec/stdlib/rollup'; |
| 7 | + |
| 8 | +import { type RetrievedCheckpoint, retrievedToPublishedCheckpoint } from './data_retrieval.js'; |
| 9 | + |
| 10 | +describe('data_retrieval', () => { |
| 11 | + describe('retrievedToPublishedCheckpoint', () => { |
| 12 | + it('handles multi-block checkpoint', async () => { |
| 13 | + // Create 3 different bodies with distinct transaction effects |
| 14 | + const body1 = await Body.random({ txsPerBlock: 2 }); |
| 15 | + const body2 = await Body.random({ txsPerBlock: 2 }); |
| 16 | + const body3 = await Body.random({ txsPerBlock: 2 }); |
| 17 | + |
| 18 | + // Convert to BlockBlobData |
| 19 | + const block1BlobData = makeBlockBlobDataFromBody(body1, BlockNumber(1), true, 1000); |
| 20 | + const block2BlobData = makeBlockBlobDataFromBody(body2, BlockNumber(2), false, 2000); |
| 21 | + const block3BlobData = makeBlockBlobDataFromBody(body3, BlockNumber(3), false, 3000); |
| 22 | + |
| 23 | + // Calculate total blob fields for checkpoint end marker |
| 24 | + const numBlobFields = 100; // Approximate, doesn't need to be exact for this test |
| 25 | + |
| 26 | + const checkpointBlobData: CheckpointBlobData = { |
| 27 | + blocks: [block1BlobData, block2BlobData, block3BlobData], |
| 28 | + checkpointEndMarker: { numBlobFields }, |
| 29 | + }; |
| 30 | + |
| 31 | + const archiveRoot = Fr.random(); |
| 32 | + |
| 33 | + const retrievedCheckpoint: RetrievedCheckpoint = { |
| 34 | + checkpointNumber: CheckpointNumber(1), |
| 35 | + archiveRoot, |
| 36 | + header: CheckpointHeader.random(), |
| 37 | + checkpointBlobData, |
| 38 | + l1: new L1PublishedData(1n, 1000n, '0x1234'), |
| 39 | + chainId: new Fr(1), |
| 40 | + version: new Fr(1), |
| 41 | + attestations: [CommitteeAttestation.empty()], |
| 42 | + }; |
| 43 | + |
| 44 | + const publishedCheckpoint = await retrievedToPublishedCheckpoint(retrievedCheckpoint); |
| 45 | + |
| 46 | + // Verify we got 3 blocks |
| 47 | + expect(publishedCheckpoint.checkpoint.blocks).toHaveLength(3); |
| 48 | + |
| 49 | + // Verify each block has the correct number of txs |
| 50 | + for (const block of publishedCheckpoint.checkpoint.blocks) { |
| 51 | + expect(block.body.txEffects).toHaveLength(2); |
| 52 | + } |
| 53 | + |
| 54 | + // The critical assertion: each block should have the tx hashes from its corresponding body |
| 55 | + const reconstructedBlock1 = publishedCheckpoint.checkpoint.blocks[0]; |
| 56 | + const reconstructedBlock2 = publishedCheckpoint.checkpoint.blocks[1]; |
| 57 | + const reconstructedBlock3 = publishedCheckpoint.checkpoint.blocks[2]; |
| 58 | + |
| 59 | + // Block 1 should have body1's tx hashes |
| 60 | + expect(reconstructedBlock1.body.txEffects.map(tx => tx.txHash.toString())).toEqual( |
| 61 | + body1.txEffects.map(tx => tx.txHash.toString()), |
| 62 | + ); |
| 63 | + |
| 64 | + // Block 2 should have body2's tx hashes |
| 65 | + expect(reconstructedBlock2.body.txEffects.map(tx => tx.txHash.toString())).toEqual( |
| 66 | + body2.txEffects.map(tx => tx.txHash.toString()), |
| 67 | + ); |
| 68 | + |
| 69 | + // Block 3 should have body3's tx hashes |
| 70 | + expect(reconstructedBlock3.body.txEffects.map(tx => tx.txHash.toString())).toEqual( |
| 71 | + body3.txEffects.map(tx => tx.txHash.toString()), |
| 72 | + ); |
| 73 | + |
| 74 | + // Also verify blocks are distinct from each other |
| 75 | + expect(reconstructedBlock1.body.txEffects.map(tx => tx.txHash.toString())).not.toEqual( |
| 76 | + reconstructedBlock2.body.txEffects.map(tx => tx.txHash.toString()), |
| 77 | + ); |
| 78 | + expect(reconstructedBlock1.body.txEffects.map(tx => tx.txHash.toString())).not.toEqual( |
| 79 | + reconstructedBlock3.body.txEffects.map(tx => tx.txHash.toString()), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('handles single-block checkpoint', async () => { |
| 84 | + const body1 = await Body.random({ txsPerBlock: 3 }); |
| 85 | + const block1BlobData = makeBlockBlobDataFromBody(body1, BlockNumber(1), true, 5000); |
| 86 | + |
| 87 | + const checkpointBlobData: CheckpointBlobData = { |
| 88 | + blocks: [block1BlobData], |
| 89 | + checkpointEndMarker: { numBlobFields: 50 }, |
| 90 | + }; |
| 91 | + |
| 92 | + const archiveRoot = Fr.random(); |
| 93 | + |
| 94 | + const retrievedCheckpoint: RetrievedCheckpoint = { |
| 95 | + checkpointNumber: CheckpointNumber(1), |
| 96 | + archiveRoot, |
| 97 | + header: CheckpointHeader.random(), |
| 98 | + checkpointBlobData, |
| 99 | + l1: new L1PublishedData(1n, 1000n, '0x1234'), |
| 100 | + chainId: new Fr(1), |
| 101 | + version: new Fr(1), |
| 102 | + attestations: [], |
| 103 | + }; |
| 104 | + |
| 105 | + const publishedCheckpoint = await retrievedToPublishedCheckpoint(retrievedCheckpoint); |
| 106 | + |
| 107 | + expect(publishedCheckpoint.checkpoint.blocks).toHaveLength(1); |
| 108 | + expect(publishedCheckpoint.checkpoint.blocks[0].body.txEffects).toHaveLength(3); |
| 109 | + |
| 110 | + // Verify tx hashes match the original body |
| 111 | + expect(publishedCheckpoint.checkpoint.blocks[0].body.txEffects.map(tx => tx.txHash.toString())).toEqual( |
| 112 | + body1.txEffects.map(tx => tx.txHash.toString()), |
| 113 | + ); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +/** |
| 119 | + * Helper to create a BlockBlobData from a Body. This ensures the blob data is compatible |
| 120 | + * with Body.fromTxBlobData. |
| 121 | + */ |
| 122 | +function makeBlockBlobDataFromBody( |
| 123 | + body: Body, |
| 124 | + blockNumber: BlockNumber, |
| 125 | + isFirstBlock: boolean, |
| 126 | + seed: number, |
| 127 | +): BlockBlobData { |
| 128 | + const blockEndBlobData = makeBlockEndBlobData({ |
| 129 | + seed, |
| 130 | + isFirstBlock, |
| 131 | + blockEndMarker: { |
| 132 | + numTxs: body.txEffects.length, |
| 133 | + blockNumber, |
| 134 | + timestamp: BigInt(1000 + blockNumber), |
| 135 | + }, |
| 136 | + }); |
| 137 | + |
| 138 | + return { |
| 139 | + txs: body.toTxBlobData(), |
| 140 | + ...blockEndBlobData, |
| 141 | + }; |
| 142 | +} |
0 commit comments