Skip to content

Commit

Permalink
Da api ext tests (#85)
Browse files Browse the repository at this point in the history
* Index store links blob to cert_id

* Tests for multiple indexes pointing to the same blob

* Test multiple indexes to the same blob in the full flow
  • Loading branch information
bacv authored Mar 22, 2024
1 parent c3d04e8 commit 10a5cc4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
31 changes: 28 additions & 3 deletions da/api/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def add(self, cert_id: bytes, metadata: Metadata):
if metadata.index in self.app_id_store[metadata.app_id]:
raise ValueError("index already written")

blob = self.blob_store.pop(cert_id)
self.app_id_store[metadata.app_id][metadata.index] = blob
self.app_id_store[metadata.app_id][metadata.index] = cert_id

# Implements `get_multiple` method from BlobStore abstract class.
def get_multiple(self, app_id, indexes) -> List[Optional[DABlob]]:
return [
self.app_id_store[app_id].get(i) for i in indexes
self.blob_store.get(self.app_id_store[app_id].get(i), None) if self.app_id_store[app_id].get(i) else None for i in indexes
]



class TestFlow(TestCase):
def test_api_write_read(self):
expected_blob = "hello"
Expand Down Expand Up @@ -70,3 +70,28 @@ def test_same_index(self):

self.assertEqual([expected_blob], blobs)

def test_multiple_indexes_same_data(self):
expected_blob = "hello"
cert_id = b"11"*32
app_id = 1
idx1 = 1
idx2 = 2
mock_meta1 = Metadata(app_id, idx1)
mock_meta2 = Metadata(app_id, idx2)

mock_store = MockStore()
mock_store.populate(expected_blob, cert_id)

api = DAApi(mock_store)

api.write(cert_id, mock_meta1)
mock_store.populate(expected_blob, cert_id)
api.write(cert_id, mock_meta2)

blobs_idx1 = api.read(app_id, [idx1])
blobs_idx2 = api.read(app_id, [idx2])

self.assertEqual([expected_blob], blobs_idx1)
self.assertEqual([expected_blob], blobs_idx2)
self.assertEqual(mock_store.app_id_store[app_id][idx1], mock_store.app_id_store[app_id][idx2])

42 changes: 41 additions & 1 deletion da/test_full_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,44 @@ def __send_and_await_response(node: int, blob: DABlob):
for node in sorted(self.api_nodes, key=lambda n: bls_pop.SkToPk(n.verifier.sk))
))
original_blobs = list(self.dispersal._prepare_data(encoded_data))
self.assertEqual(blobs, original_blobs)
self.assertEqual(blobs, original_blobs)

def test_same_blob_multiple_indexes(self):
app_id = int.to_bytes(1)
indexes = [1, 2, 3] # Different indexes to test with the same blob

# encoder
data = self.encoder_test.data
encoding_params = DAEncoderParams(column_count=self.n_nodes // 2, bytes_per_field_element=32)
encoded_data = DAEncoder(encoding_params).encode(data)

# mock send and await method with local verifiers
def __send_and_await_response(node: int, blob: DABlob):
node = self.api_nodes[int.from_bytes(node)]
return node.receive_blob(blob)

# inject mock send and await method
self.dispersal._send_and_await_response = __send_and_await_response
certificate = self.dispersal.disperse(encoded_data)

# Loop through each index and simulate dispersal with the same cert_id but different metadata
for index in indexes:
vid = VID(
certificate.id(),
Metadata(app_id, index)
)

# verifier
for node in self.api_nodes:
node.receive_cert(vid)

# Verify retrieval for each index
for index in indexes:
# Notice that we need to sort the api_nodes by their public key to have the blobs sorted in the same fashion
# as we do actually do dispersal.
blobs = list(chain.from_iterable(
node.read(app_id, [index])
for node in sorted(self.api_nodes, key=lambda n: bls_pop.SkToPk(n.verifier.sk))
))
original_blobs = list(self.dispersal._prepare_data(encoded_data))
self.assertEqual(blobs, original_blobs, f"Failed at index {index}")

0 comments on commit 10a5cc4

Please sign in to comment.