Exemplo n.º 1
0
def test_chunk_root_calculation():
    with pytest.raises(ValueError):
        calc_chunk_root(b"\x00" * (COLLATION_SIZE - 1))
    with pytest.raises(ValueError):
        calc_chunk_root(b"\x00" * (COLLATION_SIZE + 1))
    with pytest.raises(ValueError):
        calc_chunk_root(b"\x00" * (COLLATION_SIZE - CHUNK_SIZE))
    with pytest.raises(ValueError):
        calc_chunk_root(b"\x00" * (COLLATION_SIZE + CHUNK_SIZE))

    chunk_number = COLLATION_SIZE // CHUNK_SIZE
    chunks = [b"\x00" * CHUNK_SIZE] * chunk_number
    body = b"".join(chunks)

    assert calc_chunk_root(body) == calc_merkle_root(chunks)
Exemplo n.º 2
0
def random_collation(shard_id, period):
    body = zpad_right(int_to_big_endian(random.getrandbits(8 * 32)),
                      COLLATION_SIZE)
    header = CollationHeader(
        shard_id=shard_id,
        period=period,
        chunk_root=calc_chunk_root(body),
        proposer_address=b"\xff" * 20,
    )
    return Collation(header, body)
Exemplo n.º 3
0
    def propose(self) -> Collation:
        """Broadcast a new collation to the network, add it to the local shard, and return it."""
        # create collation for current period
        period = self.get_current_period()
        body = zpad_right(str(self).encode("utf-8"), COLLATION_SIZE)
        header = CollationHeader(self.shard.shard_id, calc_chunk_root(body), period, b"\x11" * 20)
        collation = Collation(header, body)

        self.logger.debug("Proposing collation {}".format(collation))

        # add collation to local chain
        self.shard.add_collation(collation)

        # broadcast collation
        for peer in self.peer_pool.peers:
            cast(ShardingPeer, peer).send_collations([collation])

        return collation
Exemplo n.º 4
0
def generate_collations():
    explicit_params = {}
    for period in itertools.count():
        default_params = {
            "shard_id": 0,
            "period": period,
            "body": zpad_right(b"body%d" % period, COLLATION_SIZE),
            "proposer_address": zpad_right(b"proposer%d" % period, 20),
        }
        # only calculate chunk root if it wouldn't be replaced anyway
        if "chunk_root" not in explicit_params:
            default_params["chunk_root"] = calc_chunk_root(default_params["body"])

        params = merge(default_params, explicit_params)
        header = CollationHeader(
            shard_id=params["shard_id"],
            chunk_root=params["chunk_root"],
            period=params["period"],
            proposer_address=params["proposer_address"],
        )
        collation = Collation(header, params["body"])
        explicit_params = (yield collation) or {}
Exemplo n.º 5
0
async def test_collation_requests(request, event_loop):
    # setup two peers
    sender, receiver = await get_directly_linked_sharding_peers(request, event_loop)
    receiver_peer_pool = MockPeerPoolWithConnectedPeers([receiver])

    # setup shard db for request receiving node
    receiver_db = ShardDB(MemoryDB())
    receiver_shard = Shard(receiver_db, 0)

    # create three collations and add two to the shard of the receiver
    # body is shared to avoid unnecessary chunk root calculation
    body = zpad_right(b"body", COLLATION_SIZE)
    chunk_root = calc_chunk_root(body)
    c1 = Collation(CollationHeader(0, chunk_root, 0, zpad_right(b"proposer1", 20)), body)
    c2 = Collation(CollationHeader(0, chunk_root, 1, zpad_right(b"proposer2", 20)), body)
    c3 = Collation(CollationHeader(0, chunk_root, 2, zpad_right(b"proposer3", 20)), body)
    for collation in [c1, c2]:
        receiver_shard.add_collation(collation)

    # start shard syncer
    receiver_syncer = ShardSyncer(receiver_shard, receiver_peer_pool)
    asyncio.ensure_future(receiver_syncer.run())

    def finalizer():
        event_loop.run_until_complete(receiver_syncer.cancel())
    request.addfinalizer(finalizer)

    cancel_token = CancelToken("test")

    # request single collation
    received_collations = await asyncio.wait_for(
        sender.get_collations([c1.hash], cancel_token),
        timeout=1,
    )
    assert received_collations == set([c1])

    # request multiple collations
    received_collations = await asyncio.wait_for(
        sender.get_collations([c1.hash, c2.hash], cancel_token),
        timeout=1,
    )
    assert received_collations == set([c1, c2])

    # request no collations
    received_collations = await asyncio.wait_for(
        sender.get_collations([], cancel_token),
        timeout=1,
    )
    assert received_collations == set()

    # request unknown collation
    received_collations = await asyncio.wait_for(
        sender.get_collations([c3.hash], cancel_token),
        timeout=1,
    )
    assert received_collations == set()

    # request multiple collations, including unknown one
    received_collations = await asyncio.wait_for(
        sender.get_collations([c1.hash, c2.hash, c3.hash], cancel_token),
        timeout=1,
    )
    assert received_collations == set([c1, c2])
Exemplo n.º 6
0
 def add_body(self, body: bytes) -> None:
     chunk_root = calc_chunk_root(body)
     self.db.set(chunk_root, body)
     self.set_availability(chunk_root, Availability.AVAILABLE)
Exemplo n.º 7
0
def header(body):
    return CollationHeader(shard_id=0,
                           chunk_root=calc_chunk_root(body),
                           period=2,
                           proposer_address=b"\x22" * 20)