예제 #1
0
    def test_from_db(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()

        # write the assets and block separately
        b.write_assets(assets_from_block)
        b.write_block(block)

        # check the reconstructed block is the same as the original block
        block_from_db = Block.from_db(b, block_dict)
        assert block == block_from_db
예제 #2
0
    def test_get_last_voted_block_returns_genesis_if_no_votes_has_been_casted(
            self, b):
        from bigchaindb.models import Block
        from bigchaindb.backend import query

        genesis = query.get_genesis_block(b.connection)
        genesis = Block.from_db(b, genesis)
        gb = b.get_last_voted_block()
        assert gb == genesis
        assert b.validate_block(gb) == gb
예제 #3
0
파일: vote.py 프로젝트: cgwyx/bigchaindb
 def validate_block(self, block_dict):
     if not self.bigchain.has_previous_vote(block_dict['id']):
         try:
             block = Block.from_db(self.bigchain, block_dict, from_dict_kwargs={
                 'tx_construct': FastTransaction
             })
         except (exceptions.InvalidHash):
             # XXX: if a block is invalid we should skip the `validate_tx`
             # step, but since we are in a pipeline we cannot just jump to
             # another function. Hackish solution: generate an invalid
             # transaction and propagate it to the next steps of the
             # pipeline.
             return block_dict['id'], [self.invalid_dummy_tx]
         try:
             block._validate_block(self.bigchain)
         except exceptions.ValidationError:
             # XXX: if a block is invalid we should skip the `validate_tx`
             # step, but since we are in a pipeline we cannot just jump to
             # another function. Hackish solution: generate an invalid
             # transaction and propagate it to the next steps of the
             # pipeline.
             return block.id, [self.invalid_dummy_tx]
         return block.id, block_dict['block']['transactions']
예제 #4
0
 def validate_block(self, block_dict):
     if not self.bigchain.has_previous_vote(block_dict['id']):
         try:
             block = Block.from_db(self.bigchain, block_dict, from_dict_kwargs={
                 'tx_construct': FastTransaction
             })
         except (exceptions.InvalidHash):
             # XXX: if a block is invalid we should skip the `validate_tx`
             # step, but since we are in a pipeline we cannot just jump to
             # another function. Hackish solution: generate an invalid
             # transaction and propagate it to the next steps of the
             # pipeline.
             return block_dict['id'], [self.invalid_dummy_tx]
         try:
             block._validate_block(self.bigchain)
         except exceptions.ValidationError:
             # XXX: if a block is invalid we should skip the `validate_tx`
             # step, but since we are in a pipeline we cannot just jump to
             # another function. Hackish solution: generate an invalid
             # transaction and propagate it to the next steps of the
             # pipeline.
             return block.id, [self.invalid_dummy_tx]
         return block.id, block_dict['block']['transactions']