|
| 1 | +import { Blob } from '@aztec/foundation/blob'; |
| 2 | +import { Fr } from '@aztec/foundation/fields'; |
| 3 | + |
| 4 | +import { BlobWithIndex } from '../types/index.js'; |
| 5 | +import { type BlobStore } from './interface.js'; |
| 6 | + |
| 7 | +export function describeBlobStore(getBlobStore: () => BlobStore) { |
| 8 | + let blobStore: BlobStore; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + blobStore = getBlobStore(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should store and retrieve a blob', async () => { |
| 15 | + // Create a test blob with random fields |
| 16 | + const testFields = [Fr.random(), Fr.random(), Fr.random()]; |
| 17 | + const blob = Blob.fromFields(testFields); |
| 18 | + const blockId = '0x12345'; |
| 19 | + const blobWithIndex = new BlobWithIndex(blob, 0); |
| 20 | + |
| 21 | + // Store the blob |
| 22 | + await blobStore.addBlobSidecars(blockId, [blobWithIndex]); |
| 23 | + |
| 24 | + // Retrieve the blob |
| 25 | + const retrievedBlobs = await blobStore.getBlobSidecars(blockId); |
| 26 | + const [retrievedBlob] = retrievedBlobs!; |
| 27 | + |
| 28 | + // Verify the blob was retrieved and matches |
| 29 | + expect(retrievedBlob).toBeDefined(); |
| 30 | + expect(retrievedBlob.blob.fieldsHash.toString()).toBe(blob.fieldsHash.toString()); |
| 31 | + expect(retrievedBlob.blob.commitment.toString('hex')).toBe(blob.commitment.toString('hex')); |
| 32 | + }); |
| 33 | + |
| 34 | + it('Should allow requesting a specific index of blob', async () => { |
| 35 | + const testFields = [Fr.random(), Fr.random(), Fr.random()]; |
| 36 | + const blob = Blob.fromFields(testFields); |
| 37 | + const blockId = '0x12345'; |
| 38 | + const blobWithIndex = new BlobWithIndex(blob, 0); |
| 39 | + const blobWithIndex2 = new BlobWithIndex(blob, 1); |
| 40 | + |
| 41 | + await blobStore.addBlobSidecars(blockId, [blobWithIndex, blobWithIndex2]); |
| 42 | + |
| 43 | + const retrievedBlobs = await blobStore.getBlobSidecars(blockId, [0]); |
| 44 | + const [retrievedBlob] = retrievedBlobs!; |
| 45 | + |
| 46 | + expect(retrievedBlob.blob.fieldsHash.toString()).toBe(blob.fieldsHash.toString()); |
| 47 | + expect(retrievedBlob.blob.commitment.toString('hex')).toBe(blob.commitment.toString('hex')); |
| 48 | + |
| 49 | + const retrievedBlobs2 = await blobStore.getBlobSidecars(blockId, [1]); |
| 50 | + const [retrievedBlob2] = retrievedBlobs2!; |
| 51 | + |
| 52 | + expect(retrievedBlob2.blob.fieldsHash.toString()).toBe(blob.fieldsHash.toString()); |
| 53 | + expect(retrievedBlob2.blob.commitment.toString('hex')).toBe(blob.commitment.toString('hex')); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Differentiate between blockHash and slot', async () => { |
| 57 | + const testFields = [Fr.random(), Fr.random(), Fr.random()]; |
| 58 | + const testFieldsSlot = [Fr.random(), Fr.random(), Fr.random()]; |
| 59 | + const blob = Blob.fromFields(testFields); |
| 60 | + const blobSlot = Blob.fromFields(testFieldsSlot); |
| 61 | + const blockId = '0x12345'; |
| 62 | + const slot = '12345'; |
| 63 | + const blobWithIndex = new BlobWithIndex(blob, 0); |
| 64 | + const blobWithIndexSlot = new BlobWithIndex(blobSlot, 0); |
| 65 | + |
| 66 | + await blobStore.addBlobSidecars(blockId, [blobWithIndex]); |
| 67 | + await blobStore.addBlobSidecars(slot, [blobWithIndexSlot]); |
| 68 | + |
| 69 | + const retrievedBlobs = await blobStore.getBlobSidecars(blockId, [0]); |
| 70 | + const [retrievedBlob] = retrievedBlobs!; |
| 71 | + |
| 72 | + expect(retrievedBlob.blob.fieldsHash.toString()).toBe(blob.fieldsHash.toString()); |
| 73 | + expect(retrievedBlob.blob.commitment.toString('hex')).toBe(blob.commitment.toString('hex')); |
| 74 | + |
| 75 | + const retrievedBlobs2 = await blobStore.getBlobSidecars(slot, [0]); |
| 76 | + const [retrievedBlob2] = retrievedBlobs2!; |
| 77 | + |
| 78 | + expect(retrievedBlob2.blob.fieldsHash.toString()).toBe(blobSlot.fieldsHash.toString()); |
| 79 | + expect(retrievedBlob2.blob.commitment.toString('hex')).toBe(blobSlot.commitment.toString('hex')); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return undefined for non-existent blob', async () => { |
| 83 | + const nonExistentBlob = await blobStore.getBlobSidecars('999999'); |
| 84 | + expect(nonExistentBlob).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle multiple blobs with different block IDs', async () => { |
| 88 | + // Create two different blobs |
| 89 | + const blob1 = Blob.fromFields([Fr.random(), Fr.random()]); |
| 90 | + const blob2 = Blob.fromFields([Fr.random(), Fr.random(), Fr.random()]); |
| 91 | + const blobWithIndex1 = new BlobWithIndex(blob1, 0); |
| 92 | + const blobWithIndex2 = new BlobWithIndex(blob2, 0); |
| 93 | + |
| 94 | + // Store both blobs |
| 95 | + await blobStore.addBlobSidecars('1', [blobWithIndex1]); |
| 96 | + await blobStore.addBlobSidecars('2', [blobWithIndex2]); |
| 97 | + |
| 98 | + // Retrieve and verify both blobs |
| 99 | + const retrieved1 = await blobStore.getBlobSidecars('1'); |
| 100 | + const retrieved2 = await blobStore.getBlobSidecars('2'); |
| 101 | + const [retrievedBlob1] = retrieved1!; |
| 102 | + const [retrievedBlob2] = retrieved2!; |
| 103 | + |
| 104 | + expect(retrievedBlob1.blob.commitment.toString('hex')).toBe(blob1.commitment.toString('hex')); |
| 105 | + expect(retrievedBlob2.blob.commitment.toString('hex')).toBe(blob2.commitment.toString('hex')); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should overwrite blob when using same block ID', async () => { |
| 109 | + // Create two different blobs |
| 110 | + const originalBlob = Blob.fromFields([Fr.random()]); |
| 111 | + const newBlob = Blob.fromFields([Fr.random(), Fr.random()]); |
| 112 | + const blockId = '1'; |
| 113 | + const originalBlobWithIndex = new BlobWithIndex(originalBlob, 0); |
| 114 | + const newBlobWithIndex = new BlobWithIndex(newBlob, 0); |
| 115 | + |
| 116 | + // Store original blob |
| 117 | + await blobStore.addBlobSidecars(blockId, [originalBlobWithIndex]); |
| 118 | + |
| 119 | + // Overwrite with new blob |
| 120 | + await blobStore.addBlobSidecars(blockId, [newBlobWithIndex]); |
| 121 | + |
| 122 | + // Retrieve and verify it's the new blob |
| 123 | + const retrievedBlobs = await blobStore.getBlobSidecars(blockId); |
| 124 | + const [retrievedBlob] = retrievedBlobs!; |
| 125 | + expect(retrievedBlob.blob.commitment.toString('hex')).toBe(newBlob.commitment.toString('hex')); |
| 126 | + expect(retrievedBlob.blob.commitment.toString('hex')).not.toBe(originalBlob.commitment.toString('hex')); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should handle multiple blobs with the same block ID', async () => { |
| 130 | + const blob1 = Blob.fromFields([Fr.random()]); |
| 131 | + const blob2 = Blob.fromFields([Fr.random()]); |
| 132 | + const blobWithIndex1 = new BlobWithIndex(blob1, 0); |
| 133 | + const blobWithIndex2 = new BlobWithIndex(blob2, 0); |
| 134 | + |
| 135 | + await blobStore.addBlobSidecars('1', [blobWithIndex1, blobWithIndex2]); |
| 136 | + const retrievedBlobs = await blobStore.getBlobSidecars('1'); |
| 137 | + const [retrievedBlob1, retrievedBlob2] = retrievedBlobs!; |
| 138 | + |
| 139 | + expect(retrievedBlob1.blob.commitment.toString('hex')).toBe(blob1.commitment.toString('hex')); |
| 140 | + expect(retrievedBlob2.blob.commitment.toString('hex')).toBe(blob2.commitment.toString('hex')); |
| 141 | + }); |
| 142 | +} |
0 commit comments