示例#1
0
    def vote(self, block_id, previous_block_id, decision, invalid_reason=None):
        """Create a signed vote for a block given the
        :attr:`previous_block_id` and the :attr:`decision` (valid/invalid).

        Args:
            block_id (str): The id of the block to vote on.
            previous_block_id (str): The id of the previous block.
            decision (bool): Whether the block is valid or invalid.
            invalid_reason (Optional[str]): Reason the block is invalid
        """

        if block_id == previous_block_id:
            raise exceptions.CyclicBlockchainError()

        vote = {
            'voting_for_block': block_id,
            'previous_block': previous_block_id,
            'is_block_valid': decision,
            'invalid_reason': invalid_reason,
            'timestamp': gen_timestamp()
        }

        vote_data = serialize(vote)
        signature = crypto.PrivateKey(self.me_private).sign(vote_data.encode())

        vote_signed = {
            'node_pubkey': self.me,
            'signature': signature.decode(),
            'vote': vote
        }

        return vote_signed
示例#2
0
def fulfill_escrow_asset(bigchain,
                         source,
                         to,
                         asset_id,
                         sk,
                         execution_fulfillment=None):
    asset = bigchain.get_transaction(asset_id['txid'])
    asset_owners = asset['transaction']['conditions'][
        asset_id['cid']]['new_owners']

    other_owner = [owner for owner in asset_owners if not owner == source][0]

    # Create a base template for fulfill transaction
    asset_escrow_fulfill = bigchain.create_transaction(
        asset_owners,
        to,
        asset_id,
        'TRANSFER',
        payload=asset['transaction']['data']['payload'])

    # Parse the threshold cryptocondition
    escrow_fulfillment = cc.Fulfillment.from_dict(
        asset['transaction']['conditions'][0]['condition']['details'])

    # Get the fulfillment message to sign
    tx_escrow_execute_fulfillment_message = \
        bigchaindb.util.get_fulfillment_message(asset_escrow_fulfill,
                                                asset_escrow_fulfill['transaction']['fulfillments'][0],
                                                serialized=True)

    # get the indices path for the source that wants to fulfill
    _, indices = get_subcondition_indices_from_vk(escrow_fulfillment, source)
    subfulfillment_source = escrow_fulfillment
    for index in indices:
        subfulfillment_source = subfulfillment_source.subconditions[index][
            'body']

    # sign the fulfillment
    subfulfillment_source.sign(tx_escrow_execute_fulfillment_message,
                               crypto.PrivateKey(sk))

    # get the indices path for the other source that delivers the condition
    _, indices = get_subcondition_indices_from_vk(escrow_fulfillment,
                                                  other_owner)
    subfulfillment_other = escrow_fulfillment.subconditions[indices[0]]['body']

    # update the condition
    del escrow_fulfillment.subconditions[indices[0]]
    escrow_fulfillment.add_subcondition(subfulfillment_other.condition)

    if execution_fulfillment:
        _, indices = get_subcondition_indices_from_type(
            escrow_fulfillment, cc.PreimageSha256Fulfillment.TYPE_ID)
        del escrow_fulfillment.subconditions[indices[0]]['body'].subconditions[
            indices[1]]
        escrow_fulfillment.subconditions[
            indices[0]]['body'].add_subfulfillment_uri(execution_fulfillment)

    asset_escrow_fulfill['transaction']['fulfillments'][0][
        'fulfillment'] = escrow_fulfillment.serialize_uri()

    bigchain.write_transaction(asset_escrow_fulfill)
    return asset_escrow_fulfill