Fix nitpicks (formatting and typing)

This commit is contained in:
Daniel Sanchez Quiros 2024-03-14 17:58:24 +01:00
parent 484f4daa2d
commit 5fdb7e2408
2 changed files with 32 additions and 21 deletions

View File

@ -1,41 +1,50 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional, List
from typing import Optional, List, Sequence
from da.common import Certificate
from da.verifier import DABlob
@dataclass
class Metadata:
# index of VID certificate blob
index: int
app_id: int
# app identifier
app_id: bytes
class BlobStore(ABC):
@abstractmethod
def add(certificate, metadata):
def add(self, certificate: Certificate, metadata: Metadata):
"""
Raises: ValueError if there is already a registered certificate fot the given metadata
"""
pass
@abstractmethod
def get_multiple(app_id, indexes) -> List[DABlob]:
def get_multiple(self, app_id: bytes, indexes: Sequence[int]) -> List[Optional[DABlob]]:
pass
class Api:
class DAApi:
def __init__(self, bs: BlobStore):
self.store = bs
"""
Write method should be used by a service that is able to retrieve verified certificates
from the latest Block. Once a certificate is retrieved, api creates a relation between
the blob of an original data, certificate and index for the app_id of the certificate.
"""
def write(self, certificate, metadata):
def write(self, certificate: Certificate, metadata: Metadata):
"""
Write method should be used by a service that is able to retrieve verified certificates
from the latest Block. Once a certificate is retrieved, api creates a relation between
the blob of an original data, certificate and index for the app_id of the certificate.
Raises: ValueError if there is already a registered certificate for a given metadata
"""
self.store.add(certificate, metadata)
return
"""
Read method should accept only `app_id` and a list of indexes. The returned list of
blobs should be ordered in the same sequence as `indexes` in a request.
If node does not have the blob for some indexes, then it should add None object as an
item.
"""
def read(self, app_id, indexes) -> Optional[List[Optional[DABlob]]]:
def read(self, app_id, indexes) -> List[Optional[DABlob]]:
"""
Read method should accept only `app_id` and a list of indexes. The returned list of
blobs should be ordered in the same sequence as `indexes` in a request.
If node does not have the blob for some indexes, then it should add None object as an
item.
"""
return self.store.get_multiple(app_id, indexes)

View File

@ -3,10 +3,12 @@ from collections import defaultdict
from da.api.common import *
@dataclass
class MockCertificate:
cert_id: int
class MockStore(BlobStore):
def __init__(self):
self.blob_store = {}
@ -41,7 +43,7 @@ class TestFlow(TestCase):
mock_store = MockStore()
mock_store.populate(expected_blob, cert_id)
api = Api(mock_store)
api = DAApi(mock_store)
api.write(cert_id, mock_meta)
blobs = api.read(app_id, [idx])
@ -58,7 +60,7 @@ class TestFlow(TestCase):
mock_store = MockStore()
mock_store.populate(expected_blob, cert_id)
api = Api(mock_store)
api = DAApi(mock_store)
api.write(cert_id, mock_meta)
with self.assertRaises(ValueError):