2024-03-13 13:59:27 +00:00
|
|
|
from hashlib import sha3_256
|
2024-02-12 14:35:23 +00:00
|
|
|
from unittest import TestCase
|
2024-03-13 13:59:27 +00:00
|
|
|
|
2024-03-15 10:34:43 +00:00
|
|
|
from da.encoder import DAEncoderParams, DAEncoder
|
|
|
|
from da.test_encoder import TestEncoder
|
|
|
|
from da.verifier import DAVerifier, DABlob
|
2024-03-14 11:20:46 +00:00
|
|
|
from da.common import NodeId, Attestation, Bitfield
|
2024-03-13 13:59:27 +00:00
|
|
|
from da.dispersal import Dispersal, EncodedData, DispersalSettings
|
|
|
|
|
2024-03-15 10:34:43 +00:00
|
|
|
from py_ecc.bls import G2ProofOfPossession as bls_pop
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDispersal(TestCase):
|
2024-03-13 13:59:27 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.n_nodes = 16
|
|
|
|
self.nodes_ids = [NodeId(x.to_bytes(length=32, byteorder='big')) for x in range(self.n_nodes)]
|
|
|
|
self.secret_keys = list(range(1, self.n_nodes+1))
|
|
|
|
self.public_keys = [bls_pop.SkToPk(sk) for sk in self.secret_keys]
|
|
|
|
dispersal_settings = DispersalSettings(
|
|
|
|
self.nodes_ids,
|
|
|
|
self.public_keys,
|
|
|
|
self.n_nodes // 2 + 1
|
|
|
|
)
|
|
|
|
self.dispersal = Dispersal(dispersal_settings)
|
|
|
|
self.encoder_test = TestEncoder()
|
|
|
|
self.encoder_test.setUp()
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def test_build_certificate_insufficient_attestations(self):
|
2024-03-13 13:59:27 +00:00
|
|
|
with self.assertRaises(AssertionError):
|
2024-03-14 11:20:46 +00:00
|
|
|
self.dispersal._build_certificate(None, [], [])
|
2024-02-12 14:35:23 +00:00
|
|
|
|
|
|
|
def test_build_certificate_enough_attestations(self):
|
2024-03-13 13:59:27 +00:00
|
|
|
mock_encoded_data = EncodedData(
|
|
|
|
None, None, None, [], [], [], bytes(b"f"*48), []
|
|
|
|
)
|
|
|
|
mock_message = sha3_256(mock_encoded_data.aggregated_column_commitment).digest()
|
|
|
|
mock_attestations = [Attestation(bls_pop.Sign(sk, mock_message)) for sk in self.secret_keys]
|
2024-03-14 11:20:46 +00:00
|
|
|
certificate = self.dispersal._build_certificate(
|
|
|
|
mock_encoded_data,
|
|
|
|
mock_attestations,
|
|
|
|
Bitfield([True for _ in range(len(self.secret_keys))])
|
|
|
|
)
|
2024-03-13 13:59:27 +00:00
|
|
|
self.assertIsNotNone(certificate)
|
|
|
|
self.assertEqual(certificate.aggregated_column_commitment, mock_encoded_data.aggregated_column_commitment)
|
|
|
|
self.assertEqual(certificate.row_commitments, [])
|
|
|
|
self.assertIsNotNone(certificate.aggregated_signatures)
|
|
|
|
self.assertTrue(
|
2024-03-15 10:34:43 +00:00
|
|
|
certificate.verify(self.public_keys)
|
2024-03-13 13:59:27 +00:00
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
def test_disperse(self):
|
|
|
|
data = self.encoder_test.data
|
2024-03-20 10:03:39 +00:00
|
|
|
encoding_params = DAEncoderParams(column_count=self.n_nodes // 2, bytes_per_chunk=31)
|
2024-03-13 13:59:27 +00:00
|
|
|
encoded_data = DAEncoder(encoding_params).encode(data)
|
2024-02-12 14:35:23 +00:00
|
|
|
|
2024-03-13 13:59:27 +00:00
|
|
|
# mock send and await method with local verifiers
|
|
|
|
def __send_and_await_response(node: NodeId, blob: DABlob):
|
|
|
|
sk = self.secret_keys[int.from_bytes(node)]
|
|
|
|
verifier = DAVerifier(sk)
|
|
|
|
return verifier.verify(blob)
|
|
|
|
# inject mock send and await method
|
|
|
|
self.dispersal._send_and_await_response = __send_and_await_response
|
|
|
|
|
|
|
|
certificate = self.dispersal.disperse(encoded_data)
|
|
|
|
self.assertIsNotNone(certificate)
|
2024-03-15 10:34:43 +00:00
|
|
|
self.assertTrue(certificate.verify(self.public_keys)
|
2024-03-13 13:59:27 +00:00
|
|
|
)
|
2024-03-14 11:20:46 +00:00
|
|
|
self.assertEqual(
|
|
|
|
certificate.signers,
|
|
|
|
[True if i < self.dispersal.settings.threshold else False for i in range(self.n_nodes)]
|
|
|
|
)
|
2024-02-12 14:35:23 +00:00
|
|
|
|