nomos-specs/da/api/common.py
gusto 53b8be7a05
DA Api Tests (#83)
* Tests for da api full flow

* Fix test issues

* Da api ext tests (#85)

* 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

* Update bytes_per_chunk to 31 bytes

---------

Co-authored-by: Daniel Sanchez Quiros <sanchez.quiros.daniel@gmail.com>
2024-03-22 13:01:13 +02:00

59 lines
1.8 KiB
Python

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional, List, Sequence
from da.common import Certificate
from da.verifier import DABlob
@dataclass
class Metadata:
# app identifier
app_id: bytes
# index of VID certificate blob
index: int
@dataclass
class VID:
# da certificate id
cert_id: bytes
# application + index information
metadata: Metadata
class BlobStore(ABC):
@abstractmethod
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(self, app_id: bytes, indexes: Sequence[int]) -> List[Optional[DABlob]]:
pass
class DAApi:
def __init__(self, bs: BlobStore):
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
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)
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)