Exemplo n.º 1
0
 def test_invariant_invalid_key(self):
     """
     Test if illegal key blocks are not valid.
     """
     block = FakeBlock()
     block.public_key = b"definitelynotakey"
     assert not block.block_invariants_valid()
Exemplo n.º 2
0
 def test_invariant_negative_timestamp(self):
     """
     Test if negative sequence number blocks are not valid.
     """
     block = FakeBlock()
     block.timestamp = -1.0
     assert not block.block_invariants_valid()
Exemplo n.º 3
0
 def test_block_payload(self):
     blk = FakeBlock()
     blk_bytes = blk.pack()
     unpacked = blk.serializer.ez_unpack_serializables([BlockPayload],
                                                       blk_bytes)
     blk2 = BamiBlock.from_payload(unpacked[0])
     assert blk2 == blk
Exemplo n.º 4
0
def test_hot_start_db(tmpdir):
    tmp_val = tmpdir
    block_store = LMDBLockStore(str(tmp_val))
    chain_factory = ChainFactory()
    dbms = DBManager(chain_factory, block_store)

    test_block = FakeBlock()
    packed_block = test_block.pack()
    dbms.add_block(packed_block, test_block)
    tx_blob = test_block.transaction

    assert dbms.get_tx_blob_by_dot(test_block.com_id, test_block.com_dot) == tx_blob
    assert (
        dbms.get_block_blob_by_dot(test_block.com_id, test_block.com_dot)
        == packed_block
    )
    front = dbms.get_chain(test_block.com_id).frontier
    dbms.close()

    block_store2 = LMDBLockStore(str(tmp_val))
    chain_factory2 = ChainFactory()
    dbms2 = DBManager(chain_factory2, block_store2)

    assert dbms2.get_tx_blob_by_dot(test_block.com_id, test_block.com_dot) == tx_blob
    assert (
        dbms2.get_block_blob_by_dot(test_block.com_id, test_block.com_dot)
        == packed_block
    )

    assert dbms2.get_chain(test_block.com_id).frontier == front

    dbms2.close()
    tmp_val.remove()
Exemplo n.º 5
0
 def test_invariant_invalid_com_seq_num(self):
     """
     Test if illegal key blocks are not valid.
     """
     block = FakeBlock()
     block.com_seq_num = -1
     assert not block.block_invariants_valid()
Exemplo n.º 6
0
    def test_invalid_sign(self):
        key = default_eccrypto.generate_key(u"curve25519")

        blk = FakeBlock()
        blk.sign(key)

        assert not blk.block_invariants_valid()
Exemplo n.º 7
0
    def test_hash_function(self):
        """
        Check if the hash() function returns the Block hash.
        """
        block = FakeBlock()

        assert block.__hash__(), block.hash_number
Exemplo n.º 8
0
    def test_blocks_by_fdiff_with_holes(self, create_batches, insert_function):
        # init chain
        blks = create_batches(num_batches=2, num_blocks=100)
        com_id = blks[0][0].com_id
        self.val_dots = []

        def chain_dots_tester(chain_id, dots):
            for dot in dots:
                self.val_dots.append(dot)

        self.dbms2.add_observer(com_id, chain_dots_tester)

        wrap_iterate(insert_function(self.dbms, blks[0][:50]))
        wrap_iterate(insert_function(self.dbms2, blks[1][:20]))
        wrap_iterate(insert_function(self.dbms2, blks[1][40:60]))

        assert len(self.val_dots) == 20
        blobs = self.reconcile_round(com_id)
        assert len(blobs) == 41

        for b in blobs:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))

        assert len(self.val_dots) == 20
        blobs2 = self.reconcile_round(com_id)
        assert len(blobs2) == 8
        for b in blobs2:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))

        assert len(self.val_dots) == 20
        blobs2 = self.reconcile_round(com_id)
        assert len(blobs2) == 1
        for b in blobs2:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))
        assert len(self.val_dots) == 70
Exemplo n.º 9
0
 def test_sign(self):
     """
     Test signing a block and whether the signature is valid
     """
     crypto = default_eccrypto
     block = FakeBlock()
     assert crypto.is_valid_signature(
         block.key, block.pack(signature=False), block.signature
     )
Exemplo n.º 10
0
async def test_send_receive_raw_block(monkeypatch, mocker, set_vals):
    blk = FakeBlock(transaction=b"test")
    set_vals.nodes[0].overlay.send_block(
        blk.pack(), [set_vals.nodes[1].overlay.my_peer]
    )
    monkeypatch.setattr(MockDBManager, "add_block", lambda _, __, ___, prefix: None)
    monkeypatch.setattr(MockDBManager, "has_block", lambda _, __: False)
    spy = mocker.spy(MockDBManager, "has_block")
    await deliver_messages()
    spy.assert_called_with(ANY, blk.hash)
Exemplo n.º 11
0
    def test_get_tx_blob(self):
        self.test_block = FakeBlock()
        packed_block = self.test_block.pack()
        self.dbms.add_block(packed_block, self.test_block)
        self.tx_blob = self.test_block.transaction

        assert (self.dbms.get_tx_blob_by_dot(
            self.test_block.com_id, self.test_block.com_dot) == self.tx_blob)
        assert (self.dbms.get_block_blob_by_dot(
            self.test_block.com_id, self.test_block.com_dot) == packed_block)
Exemplo n.º 12
0
async def test_send_incorrect_block(monkeypatch, mocker, set_vals):
    blk = FakeBlock(transaction=b"test")
    set_vals.nodes[0].overlay.send_block(blk.pack(signature=False),
                                         [set_vals.nodes[1].overlay.my_peer])
    spy = mocker.spy(BamiBlock, "unpack")
    spy2 = mocker.spy(BamiBlock, "block_invariants_valid")
    await deliver_messages()
    spy.assert_called_once()
    spy2.assert_called_once()
    assert spy2.spy_return is False
Exemplo n.º 13
0
    def test_create_next_pers(self, monkeypatch):
        """
        Test creating a block that points towards a previous block
        """
        db = MockDBManager()
        prev = FakeBlock()

        monkeypatch.setattr(
            MockDBManager,
            "get_chain",
            lambda _, chain_id: MockChain(),
        )
        monkeypatch.setattr(
            MockChain,
            "consistent_terminal",
            Links((prev.pers_dot, )),
        )

        block = BamiBlock.create(b"test", encode_raw({b"id": 42}), db,
                                 prev.public_key)

        assert block.previous == Links((prev.pers_dot, ))
        assert block.sequence_number == prev.sequence_number + 1
        assert block.public_key == prev.public_key
        assert block.signature == EMPTY_SIG
        assert block.type == b"test"
        assert block.transaction == encode_raw({b"id": 42})
        assert block.com_id == EMPTY_PK
        assert block.com_seq_num == UNKNOWN_SEQ
Exemplo n.º 14
0
    def test_create_link_to_com_chain(self, monkeypatch):
        """
        Test creating a linked half that points back towards a previous block
        """
        key = default_eccrypto.generate_key(u"curve25519")
        com_key = default_eccrypto.generate_key(u"curve25519").pub().key_to_bin()
        db = MockDBManager()
        com_link = Links(((1, ShortKey("30303030")),))
        link = FakeBlock(com_id=com_key, links=com_link)

        monkeypatch.setattr(
            MockDBManager,
            "get_chain",
            lambda _, chain_id: MockChain() if chain_id == com_key else None,
        )
        monkeypatch.setattr(
            MockChain, "consistent_terminal", Links((link.com_dot,)),
        )
        block = BamiBlock.create(
            b"test", encode_raw({"id": 42}), db, key.pub().key_to_bin(), com_id=com_key
        )

        # include the personal community

        # Attach to the
        assert block.links == Links((link.com_dot,))
        assert block.previous == Links((GENESIS_DOT,))
        assert block.sequence_number == GENESIS_SEQ
        assert block.com_seq_num == link.com_seq_num + 1
        assert block.public_key == key.pub().key_to_bin()
        assert block.signature == EMPTY_SIG
        assert block.type == b"test"
        assert block.transaction == encode_raw({"id": 42})
        assert block.com_id == com_key
Exemplo n.º 15
0
 def test_apply_invalid_witness_tx(self, set_vals):
     blk = FakeBlock(com_id=set_vals.community_id)
     i = 1
     set_vals.nodes[0].overlay.witness_delta = 100
     while set_vals.nodes[0].overlay.should_witness_chain_point(
             blk.com_id, blk.public_key, i):
         i += 1
     tx = (i, {b"t": (True, True)})
     with pytest.raises(InvalidWitnessTransactionException):
         set_vals.nodes[0].overlay.apply_witness_tx(blk, tx)
Exemplo n.º 16
0
    def std_vals(self):
        self.chain_id = b"chain_id"
        self.block_dot = Dot((3, ShortKey("808080")))
        self.block_dot_encoded = encode_raw(self.block_dot)
        self.dot_id = self.chain_id + self.block_dot_encoded

        self.test_hash = b"test_hash"
        self.tx_blob = b"tx_blob"
        self.block_blob = b"block_blob"

        self.test_block = FakeBlock()
        self.pers = self.test_block.public_key
        self.com_id = self.test_block.com_id
Exemplo n.º 17
0
    def test_insert_with_merge_block(self, create_batches, insert_function,
                                     chain):
        batches = create_batches(num_batches=2, num_blocks=10)

        last_blk1 = batches[0][-1]
        last_blk2 = batches[1][-1]

        dot1 = (last_blk1.com_seq_num, last_blk1.short_hash)
        dot2 = (last_blk2.com_seq_num, last_blk2.short_hash)

        vals = wrap_return(insert_function(chain, batches[0]))
        assert len(vals) == 10
        assert vals[0][0] == 1 and vals[-1][0] == 10

        merge_block = FakeBlock(links=Links((dot1, dot2)))
        chain.add_block(merge_block.links, merge_block.com_seq_num,
                        merge_block.hash)

        vals = wrap_return(insert_function(chain, batches[1]))
        assert len(vals) == 11
        assert vals[0][0] == 1 and vals[-1][0] == 11

        assert len(list(chain.get_dots_by_seq_num(11))) == 1
Exemplo n.º 18
0
 def test_short_hash(self):
     block = FakeBlock()
     assert shorten(block.hash) == block.short_hash
Exemplo n.º 19
0
async def test_share_in_community(mocker, set_vals):
    blk = FakeBlock(com_id=set_vals.community_id)
    set_vals.nodes[0].overlay.share_in_community(blk, set_vals.community_id)
    spy = mocker.spy(set_vals.nodes[1].overlay, "validate_persist_block")
    await deliver_messages()
    spy.assert_called_once_with(blk, set_vals.nodes[0].overlay.my_peer)
Exemplo n.º 20
0
 def test_pack_unpack(self):
     blk = FakeBlock()
     blk_bytes = blk.pack()
     blk2 = BamiBlock.unpack(blk_bytes, blk.serializer)
     assert blk == blk2
Exemplo n.º 21
0
 def test_block_valid(self):
     blk = FakeBlock()
     assert blk.block_invariants_valid()
Exemplo n.º 22
0
async def test_reject_block(mocker, set_vals):
    blk = FakeBlock(com_id=set_vals.community_id)
    set_vals.nodes[0].overlay.reject(blk)
    spy = mocker.spy(set_vals.nodes[1].overlay, "validate_persist_block")
    await deliver_messages()
    spy.assert_called_with(ANY, set_vals.nodes[0].overlay.my_peer)
Exemplo n.º 23
0
class TestIntegrationDBManager:
    @pytest.fixture(autouse=True)
    def setUp(self, tmpdir) -> None:
        tmp_val = tmpdir
        self.block_store = LMDBLockStore(str(tmp_val))
        self.chain_factory = ChainFactory()
        self.dbms = DBManager(self.chain_factory, self.block_store)
        yield
        self.dbms.close()

    @pytest.fixture(autouse=True)
    def setUp2(self, tmpdir) -> None:
        tmp_val = tmpdir
        self.block_store2 = LMDBLockStore(str(tmp_val))
        self.chain_factory2 = ChainFactory()
        self.dbms2 = DBManager(self.chain_factory2, self.block_store2)
        yield
        try:
            self.dbms2.close()
            tmp_val.remove()
        except FileNotFoundError:
            pass

    def test_get_tx_blob(self):
        self.test_block = FakeBlock()
        packed_block = self.test_block.pack()
        self.dbms.add_block(packed_block, self.test_block)
        self.tx_blob = self.test_block.transaction

        assert (
            self.dbms.get_tx_blob_by_dot(
                self.test_block.com_id, self.test_block.com_dot
            )
            == self.tx_blob
        )
        assert (
            self.dbms.get_block_blob_by_dot(
                self.test_block.com_id, self.test_block.com_dot
            )
            == packed_block
        )

    def test_add_notify_block_one_chain(self, create_batches, insert_function):
        self.val_dots = []

        def chain_dots_tester(chain_id, dots):
            for dot in dots:
                assert (len(self.val_dots) == 0 and dot[0] == 1) or dot[
                    0
                ] == self.val_dots[-1][0] + 1
                self.val_dots.append(dot)

        blks = create_batches(num_batches=1, num_blocks=100)
        com_id = blks[0][0].com_id
        self.dbms.add_observer(com_id, chain_dots_tester)

        wrap_iterate(insert_function(self.dbms, blks[0]))
        assert len(self.val_dots) == 100

    def test_add_notify_block_with_conflicts(self, create_batches, insert_function):
        self.val_dots = []

        def chain_dots_tester(chain_id, dots):
            for dot in dots:
                self.val_dots.append(dot)

        blks = create_batches(num_batches=2, num_blocks=100)
        com_id = blks[0][0].com_id
        self.dbms.add_observer(com_id, chain_dots_tester)

        wrap_iterate(insert_function(self.dbms, blks[0][:20]))
        wrap_iterate(insert_function(self.dbms, blks[1][:40]))
        wrap_iterate(insert_function(self.dbms, blks[0][20:60]))
        wrap_iterate(insert_function(self.dbms, blks[1][40:]))
        wrap_iterate(insert_function(self.dbms, blks[0][60:]))

        assert len(self.val_dots) == 200

    def test_blocks_by_frontier_diff(self, create_batches, insert_function):
        # init chain
        blks = create_batches(num_batches=2, num_blocks=100)
        com_id = blks[0][0].com_id

        wrap_iterate(insert_function(self.dbms, blks[0][:50]))
        wrap_iterate(insert_function(self.dbms2, blks[1][:50]))

        front = self.dbms.get_chain(com_id).frontier
        front_diff = self.dbms2.get_chain(com_id).reconcile(front)
        vals_request = set()

        blobs = self.dbms.get_block_blobs_by_frontier_diff(
            com_id, front_diff, vals_request
        )
        assert len(blobs) == 41

    def reconcile_round(self, com_id):
        front = self.dbms.get_chain(com_id).frontier
        front_diff = self.dbms2.get_chain(com_id).reconcile(front)
        vals_request = set()
        blobs = self.dbms.get_block_blobs_by_frontier_diff(
            com_id, front_diff, vals_request
        )
        return blobs

    def test_blocks_by_fdiff_with_holes(self, create_batches, insert_function):
        # init chain
        blks = create_batches(num_batches=2, num_blocks=100)
        com_id = blks[0][0].com_id
        self.val_dots = []

        def chain_dots_tester(chain_id, dots):
            for dot in dots:
                self.val_dots.append(dot)

        self.dbms2.add_observer(com_id, chain_dots_tester)

        wrap_iterate(insert_function(self.dbms, blks[0][:50]))
        wrap_iterate(insert_function(self.dbms2, blks[1][:20]))
        wrap_iterate(insert_function(self.dbms2, blks[1][40:60]))

        assert len(self.val_dots) == 20
        blobs = self.reconcile_round(com_id)
        assert len(blobs) == 41

        for b in blobs:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))

        assert len(self.val_dots) == 20
        blobs2 = self.reconcile_round(com_id)
        assert len(blobs2) == 8
        for b in blobs2:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))

        assert len(self.val_dots) == 20
        blobs2 = self.reconcile_round(com_id)
        assert len(blobs2) == 1
        for b in blobs2:
            self.dbms2.add_block(b, FakeBlock.unpack(b, blks[0][0].serializer))
        assert len(self.val_dots) == 70