Ejemplo n.º 1
0
    def test_read(self):
        b1 = Block(1, GENESIS.block_id, [Transaction(0, 'c', 0)], 1)
        b2 = Block(2, GENESIS.block_id, [Transaction(0, 'c', 1)], 2)
        b3 = Block(3, b2.block_id, [Transaction(0, 'c', 2)], 3)
        b4 = Block(4, b3.block_id, [Transaction(0, 'c', 3)], 4)
        b5 = Block(5, b2.block_id, [Transaction(0, 'c', 4)], 5)

        self.bt.add_block(b1)
        self.bt.add_block(b2)
        self.bt.add_block(b3)
        self.bt.add_block(b4)
        self.bt.add_block(b5)

        self.bt.db.close()

        # create another BLocktree wich should load the blocks stored by self.bt
        bt2 = Blocktree(0)

        assert bt2.db.get(str(b1.block_id).encode()) == b1.serialize()
        assert bt2.db.get(str(b2.block_id).encode()) == b2.serialize()
        assert bt2.db.get(str(b3.block_id).encode()) == b3.serialize()
        assert bt2.db.get(str(b4.block_id).encode()) == b4.serialize()
        assert bt2.db.get(str(b5.block_id).encode()) == b5.serialize()

        bt2.db.close()
Ejemplo n.º 2
0
    def test_write(self):

        b1 = Block(1, GENESIS.block_id, [Transaction(0, 'c', 0)], 1)
        b2 = Block(2, GENESIS.block_id, [Transaction(0, 'c', 1)], 2)
        b3 = Block(3, b2.block_id, [Transaction(0, 'c', 2)], 3)
        b4 = Block(4, b3.block_id, [Transaction(0, 'c', 3)], 4)
        b5 = Block(5, b2.block_id, [Transaction(0, 'c', 4)], 5)

        self.bt.add_block(b1)
        self.bt.add_block(b2)
        self.bt.add_block(b3)
        self.bt.add_block(b4)
        self.bt.add_block(b5)

        assert self.bt.db.get(str(b1.block_id).encode()) == b1.serialize()
        assert self.bt.db.get(str(b2.block_id).encode()) == b2.serialize()
        assert self.bt.db.get(str(b3.block_id).encode()) == b3.serialize()
        assert self.bt.db.get(str(b4.block_id).encode()) == b4.serialize()
        assert self.bt.db.get(str(b5.block_id).encode()) == b5.serialize()
Ejemplo n.º 3
0
    def test_blk(self):
        """Test receipt of a Block.
        """
        self.node.receive_block = MagicMock()

        txn1 = Transaction(0, 'command1', 1)
        txn2 = Transaction(0, 'command2', 2)
        block = Block(0, 0, [txn1, txn2], 1)
        s = block.serialize()
        self.proto.stringReceived(s)

        self.assertTrue(self.node.receive_block.called)
        obj = self.node.receive_block.call_args[0][0]
        self.assertEqual(type(obj), Block)
        self.assertEqual(obj.txs[0], txn1)