예제 #1
0
    def get_block(self, block_id, include_status=False):
        """Get the block with the specified `block_id` (and optionally its status)

        Returns the block corresponding to `block_id` or None if no match is
        found.

        Args:
            block_id (str): transaction id of the transaction to get
            include_status (bool): also return the status of the block
                       the return value is then a tuple: (block, status)
        """
        # get block from database
        block_dict = backend.query.get_block(self.connection, block_id)
        # get the asset ids from the block
        if block_dict:
            asset_ids = Block.get_asset_ids(block_dict)
            # get the assets from the database
            assets = self.get_assets(asset_ids)
            # add the assets to the block transactions
            block_dict = Block.couple_assets(block_dict, assets)

        status = None
        if include_status:
            if block_dict:
                status = self.block_election_status(block_dict)
            return block_dict, status
        else:
            return block_dict
예제 #2
0
    def test_couple_assets(self, b):
        from bigchaindb.models import Block, Transaction

        assets = [
            {'msg': '1'},
            {'msg': '2'},
            {'msg': '3'},
        ]

        txs = []
        # create 3 assets
        for asset in assets:
            tx = Transaction.create([b.me], [([b.me], 1)], asset=asset)
            txs.append(tx)

        # create a `TRANSFER` transaction.
        # the asset in `TRANSFER` transactions is not extracted
        tx = Transaction.transfer(txs[0].to_inputs(), [([b.me], 1)],
                                  asset_id=txs[0].id)
        txs.append(tx)

        # create the block
        block = Block(txs)
        # decouple assets
        assets_from_block, block_dict = block.decouple_assets()

        # reconstruct the block
        block_dict_reconstructed = Block.couple_assets(block_dict,
                                                       assets_from_block)

        # check that the reconstructed block is the same as the original block
        assert block == Block.from_dict(block_dict_reconstructed)
예제 #3
0
    def get_block(self, block_id, include_status=False):
        """Get the block with the specified `block_id` (and optionally its status)

        Returns the block corresponding to `block_id` or None if no match is
        found.

        Args:
            block_id (str): transaction id of the transaction to get
            include_status (bool): also return the status of the block
                       the return value is then a tuple: (block, status)
        """
        # get block from database
        block_dict = backend.query.get_block(self.connection, block_id)
        # get the asset ids from the block
        if block_dict:
            asset_ids = Block.get_asset_ids(block_dict)
            txn_ids = Block.get_txn_ids(block_dict)
            # get the assets from the database
            assets = self.get_assets(asset_ids)
            # get the metadata from the database
            metadata = self.get_metadata(txn_ids)
            # add the assets to the block transactions
            block_dict = Block.couple_assets(block_dict, assets)
            # add the metadata to the block transactions
            block_dict = Block.couple_metadata(block_dict, metadata)

        status = None
        if include_status:
            if block_dict:
                status = self.block_election_status(block_dict)
            return block_dict, status
        else:
            return block_dict