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

View File

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