예제 #1
0
def send_install_proposal(tx_context, peers):
    """Send install chaincode proposal

    Args:
        tx_context: transaction context
        peers: peers to install chaincode

    Returns: a set of proposal response
    """

    if not tx_context:
        raise ValueError("InstallProposalRequest is empty.")

    if not peers:
        raise ValueError("Please specify the peer.")

    cc_deployment_spec = chaincode_pb2.ChaincodeDeploymentSpec()
    cc_deployment_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value(
            proto_str(tx_context.tx_prop_req.cc_type))
    cc_deployment_spec.chaincode_spec.chaincode_id.name = \
        proto_str(tx_context.tx_prop_req.cc_name)
    cc_deployment_spec.chaincode_spec.chaincode_id.path = \
        proto_str(tx_context.tx_prop_req.cc_path)
    cc_deployment_spec.chaincode_spec.chaincode_id.version = \
        proto_str(tx_context.tx_prop_req.cc_version)

    if not tx_context.tx_prop_req.packaged_cc:
        cc_deployment_spec.code_package = \
            package_chaincode(
                tx_context.tx_prop_req.cc_path,
                tx_context.tx_prop_req.cc_type)
    else:
        cc_deployment_spec.code_package = \
            tx_context.tx_prop_req.packaged_cc

    channel_header_extension = proposal_pb2.ChaincodeHeaderExtension()
    channel_header_extension.chaincode_id.name = \
        proto_str("lscc")
    channel_header = build_channel_header(
        common_pb2.ENDORSER_TRANSACTION, tx_context.tx_id, '',
        current_timestamp(), tx_context.epoch,
        channel_header_extension.SerializeToString())

    header = build_header(tx_context.identity, channel_header,
                          tx_context.nonce)

    cci_spec = chaincode_pb2.ChaincodeInvocationSpec()
    cci_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value(CC_TYPE_GOLANG)
    cci_spec.chaincode_spec.chaincode_id.name = proto_str("lscc")
    cci_spec.chaincode_spec.input.args.extend(
        [proto_b(CC_INSTALL),
         cc_deployment_spec.SerializeToString()])
    proposal = build_cc_proposal(cci_spec, header,
                                 tx_context.tx_prop_req.transient_map)
    signed_proposal = sign_proposal(tx_context, proposal)

    responses = [peer.send_proposal(signed_proposal) for peer in peers]
    return responses, proposal, header
예제 #2
0
def _create_installment_proposal(tran_prop_req, signing_identity, chain):
    """Create a chaincode install proposal

    This involves assembling the proposal with the data (chaincodeID,
    chaincode invocation spec, etc.) and signing it using the private key
    corresponding to the ECert to sign.

    Args:
        chain: chain
        signing_identity: signing_identity
        tran_prop_req: see TransactionProposalRequest

    Returns: (Proposal): The created Proposal instance or None.

    """

    cc_deployment_spec = chaincode_pb2.ChaincodeDeploymentSpec()
    cc_deployment_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value('GOLANG')
    cc_deployment_spec.chaincode_spec.chaincode_id.name = \
        proto_str(tran_prop_req.chaincode_id)
    cc_deployment_spec.chaincode_spec.chaincode_id.path = \
        proto_str(tran_prop_req.chaincode_path)
    cc_deployment_spec.chaincode_spec.chaincode_id.version = \
        proto_str(tran_prop_req.chaincode_version)
    if not chain.is_dev_mode:
        cc_deployment_spec.code_package = _package_chaincode(
            tran_prop_req.chaincode_path) if not \
            tran_prop_req.chaincode_package else \
            tran_prop_req.chaincode_package
    cc_deployment_spec.effective_date.seconds = \
        tran_prop_req.effective_date.seconds
    cc_deployment_spec.effective_date.nanos = \
        tran_prop_req.effective_date.nanos

    header = build_header(signing_identity,
                          tran_prop_req.nonce,
                          common_pb2.ENDORSER_TRANSACTION,
                          chain,
                          tran_prop_req.prop_type,
                          chaincode_id="lscc")

    cci_spec = chaincode_pb2.ChaincodeInvocationSpec()
    cci_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value('GOLANG')
    cci_spec.chaincode_spec.chaincode_id.name = proto_str("lscc")
    cci_spec.chaincode_spec.input.args.extend(
        [proto_b(CC_INSTALL),
         cc_deployment_spec.SerializeToString()])
    proposal = build_proposal(cci_spec, header)

    return proposal, header
예제 #3
0
def _create_instantiation_proposal(tran_prop_req, chain):
    """Create a chaincode instantiation proposal

    This involves assembling the proposal with the data (chaincodeID,
    chaincode invocation spec, etc.) and signing it using the private key
    corresponding to the ECert to sign.

    Args:
        tran_prop_req: see TransactionProposalRequest

    Returns: (Proposal): The created Proposal instance or None.

    """
    args = ["init" if not tran_prop_req.fcn
            else tran_prop_req.fcn] + tran_prop_req.args

    cc_deployment_spec = chaincode_pb2.ChaincodeDeploymentSpec()
    cc_deployment_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value('GOLANG')
    cc_deployment_spec.chaincode_spec.chaincode_id.name = \
        proto_str(tran_prop_req.chaincode_id)
    cc_deployment_spec.chaincode_spec.chaincode_id.path = \
        proto_str(tran_prop_req.chaincode_path)
    cc_deployment_spec.chaincode_spec.chaincode_id.version = \
        proto_str(tran_prop_req.chaincode_version)
    cc_deployment_spec.chaincode_spec.input.args.extend(list(map(
        lambda x: proto_b(x), args)))

    header = build_header(tran_prop_req.signing_identity,
                          tran_prop_req.nonce,
                          common_pb2.ENDORSER_TRANSACTION,
                          chain,
                          tran_prop_req.prop_type,
                          chaincode_id=tran_prop_req.chaincode_id
                          )

    cci_spec = chaincode_pb2.ChaincodeInvocationSpec()
    cci_spec.chaincode_spec.type = \
        chaincode_pb2.ChaincodeSpec.Type.Value('GOLANG')
    cci_spec.chaincode_spec.chaincode_id.name = proto_str("lccc")
    cci_spec.chaincode_spec.input.args.extend(
        [proto_b(CC_INSTANTIATE), proto_b('default'),
         cc_deployment_spec.SerializeToString()])
    proposal = build_proposal(cci_spec, header)

    signed_proposal = sign_proposal(
        tran_prop_req.signing_identity, proposal)
    return signed_proposal
예제 #4
0
    def _send_cc_proposal(self, tx_context, command, peers):

        args = []
        request = tx_context.tx_prop_req

        args.append(proto_b(request.fcn))
        for arg in request.args:
            args.append(proto_b(arg))

        # construct the deployment spec
        cc_id = chaincode_pb2.ChaincodeID()
        cc_id.name = request.cc_name
        cc_id.version = request.cc_version

        cc_input = chaincode_pb2.ChaincodeInput()
        cc_input.args.extend(args)
        cc_spec = create_cc_spec(cc_input, cc_id, CC_TYPE_GOLANG)

        cc_dep_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_dep_spec.chaincode_spec.CopyFrom(cc_spec)
        cc_dep_spec.effective_date.seconds = \
            tx_context.tx_prop_req.effective_date.seconds
        cc_dep_spec.effective_date.nanos = \
            tx_context.tx_prop_req.effective_date.nanos

        # construct the invoke spec
        # TODO: if the policy not provided, new one should be built.
        if request.cc_endorsement_policy:
            policy = request.cc_endorsement_policy
        else:
            policy = ''

        invoke_input = chaincode_pb2.ChaincodeInput()
        invoke_input.args.extend([
            proto_b(command),
            proto_b(self.name),
            cc_dep_spec.SerializeToString(),
            proto_b(policy),
            proto_b('escc'),
            proto_b('vscc')
        ])

        invoke_cc_id = chaincode_pb2.ChaincodeID()
        invoke_cc_id.name = proto_str('lscc')

        cc_invoke_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cc_invoke_spec.chaincode_spec.CopyFrom(
            create_cc_spec(invoke_input, invoke_cc_id, CC_TYPE_GOLANG))

        extension = proposal_pb2.ChaincodeHeaderExtension()
        extension.chaincode_id.name = proto_str('lscc')
        channel_header = build_channel_header(
            common_pb2.ENDORSER_TRANSACTION,
            tx_context.tx_id,
            self.name,
            current_timestamp(),
            epoch=0,
            extension=extension.SerializeToString())

        header = build_header(tx_context.identity, channel_header,
                              tx_context.nonce)
        proposal = build_cc_proposal(cc_invoke_spec, header,
                                     request.transient_map)

        signed_proposal = utils.sign_proposal(tx_context, proposal)
        send_executions = [
            peer.send_proposal(signed_proposal) for peer in peers
        ]

        return rx.Observable.merge(send_executions).to_iterable() \
            .map(lambda responses: (responses, proposal, header))
예제 #5
0
    def send_install_proposal(self, tx_context, peers=None, scheduler=None):
        """ Send install chaincode proposal

        Args:
            schedule: Rx schedule
            install_proposal_req: install proposal request
            targets: a set of peer to send

        Returns: a set of proposal response

        """
        if peers is None:
            targets = self._peers.values()
        else:
            targets = peers
        # self._validate_state() # TODO: enable this later
        # self._validate_peers(targets)  # TODO: enable this later

        if not tx_context:
            raise ValueError("InstallProposalRequest is null.")

        cc_deployment_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_deployment_spec.chaincode_spec.type = \
            chaincode_pb2.ChaincodeSpec.Type.Value(
                proto_str(tx_context.tx_prop_req.cc_type))
        cc_deployment_spec.chaincode_spec.chaincode_id.name = \
            proto_str(tx_context.tx_prop_req.cc_name)
        cc_deployment_spec.chaincode_spec.chaincode_id.path = \
            proto_str(tx_context.tx_prop_req.cc_path)
        cc_deployment_spec.chaincode_spec.chaincode_id.version = \
            proto_str(tx_context.tx_prop_req.cc_version)
        if not self._is_dev_mode:
            if not tx_context.tx_prop_req.packaged_cc:
                cc_deployment_spec.code_package = \
                    self._package_chaincode(
                        tx_context.tx_prop_req.cc_path,
                        tx_context.tx_prop_req.cc_type)
            else:
                cc_deployment_spec.code_package = \
                    tx_context.tx_prop_req.packaged_cc

        cc_deployment_spec.effective_date.seconds = \
            tx_context.tx_prop_req.effective_date.seconds
        cc_deployment_spec.effective_date.nanos = \
            tx_context.tx_prop_req.effective_date.nanos

        channel_header_extension = proposal_pb2.ChaincodeHeaderExtension()
        channel_header_extension.chaincode_id.name = \
            proto_str("lscc")
        channel_header = utils.build_channel_header(
            common_pb2.ENDORSER_TRANSACTION, tx_context.tx_id, '',
            utils.current_timestamp(), tx_context.epoch,
            channel_header_extension.SerializeToString())

        header = utils.build_header(tx_context.identity, channel_header,
                                    tx_context.nonce)

        cci_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cci_spec.chaincode_spec.type = \
            chaincode_pb2.ChaincodeSpec.Type.Value(CC_TYPE_GOLANG)
        cci_spec.chaincode_spec.chaincode_id.name = proto_str("lscc")
        cci_spec.chaincode_spec.input.args.extend(
            [proto_b(CC_INSTALL),
             cc_deployment_spec.SerializeToString()])
        proposal = utils.build_cc_proposal(
            cci_spec, header, tx_context.tx_prop_req.transient_map)
        signed_proposal = utils.sign_proposal(tx_context, proposal)

        send_executions = [
            peer.send_proposal(signed_proposal, scheduler) for peer in targets
        ]

        return rx.Observable.merge(send_executions).to_iterable() \
            .map(lambda responses: (responses, proposal, header))
    def _send_cc_proposal(self, tx_context, command, peers):

        args = []
        request = tx_context.tx_prop_req

        args.append(proto_b(request.fcn))
        for arg in request.args:
            args.append(proto_b(arg))

        # construct the deployment spec
        cc_id = chaincode_pb2.ChaincodeID()
        cc_id.name = request.cc_name
        cc_id.version = request.cc_version

        cc_input = chaincode_pb2.ChaincodeInput()
        cc_input.args.extend(args)
        cc_spec = create_cc_spec(cc_input, cc_id, CC_TYPE_GOLANG)

        cc_dep_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_dep_spec.chaincode_spec.CopyFrom(cc_spec)

        # Pass msps, TODO create an MSPManager as done in fabric-sdk-node
        policy = self._build_policy(request.cc_endorsement_policy)

        args = [
            proto_b(command),
            proto_b(self.name),
            cc_dep_spec.SerializeToString(),
            policy,
            proto_b('escc'),
            proto_b('vscc'),
        ]

        # collections_configs need V1_2 or later capability enabled,
        # otherwise private channel collections and data are not available
        collections_configs = []
        if request.collections_config:
            for config in request.collections_config:
                static_config = collection_pb2.StaticCollectionConfig()
                static_config.name = config['name']
                static_config.member_orgs_policy.signature_policy. \
                    CopyFrom(self._build_policy(config['policy'],
                             returnProto=True))
                static_config.maximum_peer_count = config['maxPeerCount']
                static_config. \
                    required_peer_count = config.get('requiredPeerCount', 0)
                static_config.block_to_live = config.get('blockToLive', 0)
                static_config.member_only_read = config.get(
                    'memberOnlyRead', False)

                collections_config = collection_pb2.CollectionConfig()
                collections_config.static_collection_config.CopyFrom(
                    static_config)

                collections_configs.append(collections_config)

            cc_coll_cfg = collection_pb2.CollectionConfigPackage()
            cc_coll_cfg.config.extend(collections_configs)
            args.append(cc_coll_cfg.SerializeToString())

        # construct the invoke spec
        invoke_input = chaincode_pb2.ChaincodeInput()
        invoke_input.args.extend(args)

        invoke_cc_id = chaincode_pb2.ChaincodeID()
        invoke_cc_id.name = proto_str('lscc')

        cc_invoke_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cc_invoke_spec.chaincode_spec.CopyFrom(
            create_cc_spec(invoke_input, invoke_cc_id, CC_TYPE_GOLANG))

        extension = proposal_pb2.ChaincodeHeaderExtension()
        extension.chaincode_id.name = proto_str('lscc')
        channel_header = build_channel_header(
            common_pb2.ENDORSER_TRANSACTION,
            tx_context.tx_id,
            self.name,
            current_timestamp(),
            epoch=0,
            extension=extension.SerializeToString())

        header = build_header(tx_context.identity, channel_header,
                              tx_context.nonce)
        proposal = build_cc_proposal(cc_invoke_spec, header,
                                     request.transient_map)

        signed_proposal = utils.sign_proposal(tx_context, proposal)
        response = [peer.send_proposal(signed_proposal) for peer in peers]
        return response, proposal, header
예제 #7
0
    def send_install_proposal(self, tx_context, peers=None):
        """Send install chaincode proposal

        :param install_proposal_req: install proposal request
        :param targets: a set of peer to send
        :param tx_context: a tx_context instance
        :param peers: peers (Default value = None)
        :return: a set of proposal response
        """
        if peers is None:
            targets = self._peers.values()
        else:
            targets = peers
        # self._validate_state() # TODO: enable this later
        # self._validate_peers(targets)  # TODO: enable this later

        if not tx_context:
            raise ValueError("InstallProposalRequest is null.")

        cc_deployment_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_deployment_spec.chaincode_spec.type = \
            chaincode_pb2.ChaincodeSpec.Type.Value(
                utils.proto_str(tx_context.tx_prop_req.cc_type))
        cc_deployment_spec.chaincode_spec.chaincode_id.name = \
            proto_str(tx_context.tx_prop_req.cc_name)
        cc_deployment_spec.chaincode_spec.chaincode_id.path = \
            proto_str(tx_context.tx_prop_req.cc_path)
        cc_deployment_spec.chaincode_spec.chaincode_id.version = \
            proto_str(tx_context.tx_prop_req.cc_version)
        if not self._is_dev_mode:
            if not tx_context.tx_prop_req.packaged_cc:
                cc_deployment_spec.code_package = \
                    package_chaincode(
                        tx_context.tx_prop_req.cc_path,
                        tx_context.tx_prop_req.cc_type)
            else:
                cc_deployment_spec.code_package = \
                    tx_context.tx_prop_req.packaged_cc

        channel_header_extension = proposal_pb2.ChaincodeHeaderExtension()
        channel_header_extension.chaincode_id.name = \
            proto_str("lscc")
        channel_header = utils.build_channel_header(
            common_pb2.ENDORSER_TRANSACTION,
            tx_context.tx_id,
            '',
            utils.current_timestamp(),
            tx_context.epoch,
            channel_header_extension.SerializeToString()
        )

        header = utils.build_header(tx_context.identity,
                                    channel_header,
                                    tx_context.nonce)

        cci_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cci_spec.chaincode_spec.type = \
            chaincode_pb2.ChaincodeSpec.Type.Value(CC_TYPE_GOLANG)
        cci_spec.chaincode_spec.chaincode_id.name = proto_str("lscc")
        cci_spec.chaincode_spec.input.args.extend(
            [proto_b(CC_INSTALL), cc_deployment_spec.SerializeToString()])
        proposal = utils.build_cc_proposal(
            cci_spec, header,
            tx_context.tx_prop_req.transient_map)
        signed_proposal = utils.sign_proposal(tx_context, proposal)

        responses = [peer.send_proposal(signed_proposal)
                     for peer in targets]

        return responses, proposal, header
예제 #8
0
    def _send_cc_proposal(self, tx_context, command, peers):

        args = []
        request = tx_context.tx_prop_req

        args.append(proto_b(request.fcn))
        for arg in request.args:
            args.append(proto_b(arg))

        # construct the deployment spec
        cc_id = chaincode_pb2.ChaincodeID()
        cc_id.name = request.cc_name
        cc_id.version = request.cc_version

        cc_input = chaincode_pb2.ChaincodeInput()
        cc_input.args.extend(args)
        cc_spec = create_cc_spec(cc_input, cc_id, CC_TYPE_GOLANG)

        cc_dep_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_dep_spec.chaincode_spec.CopyFrom(cc_spec)

        # Pass msps, TODO create an MSPManager as done in fabric-sdk-node
        policy = self._build_policy(request.cc_endorsement_policy)

        # construct the invoke spec
        invoke_input = chaincode_pb2.ChaincodeInput()
        invoke_input.args.extend(
            [proto_b(command),
             proto_b(self.name),
             cc_dep_spec.SerializeToString(),
             policy,
             proto_b('escc'),
             proto_b('vscc')])

        invoke_cc_id = chaincode_pb2.ChaincodeID()
        invoke_cc_id.name = proto_str('lscc')

        cc_invoke_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cc_invoke_spec.chaincode_spec.CopyFrom(create_cc_spec(invoke_input,
                                                              invoke_cc_id,
                                                              CC_TYPE_GOLANG)
                                               )

        extension = proposal_pb2.ChaincodeHeaderExtension()
        extension.chaincode_id.name = proto_str('lscc')
        channel_header = build_channel_header(
            common_pb2.ENDORSER_TRANSACTION,
            tx_context.tx_id,
            self.name,
            current_timestamp(),
            epoch=0,
            extension=extension.SerializeToString()
        )

        header = build_header(tx_context.identity,
                              channel_header,
                              tx_context.nonce)
        proposal = build_cc_proposal(
            cc_invoke_spec,
            header,
            request.transient_map)

        signed_proposal = utils.sign_proposal(tx_context, proposal)
        response = [peer.send_proposal(signed_proposal)
                    for peer in peers]
        return response, proposal, header
예제 #9
0
    def _send_cc_proposal(self, tx_context, command, peers):

        args = []
        request = tx_context.tx_prop_req

        args.append(proto_b(request.fcn))
        for arg in request.args:
            args.append(proto_b(arg))

        # construct the deployment spec
        cc_id = chaincode_pb2.ChaincodeID()
        cc_id.name = request.cc_name
        cc_id.version = request.cc_version

        cc_input = chaincode_pb2.ChaincodeInput()
        cc_input.args.extend(args)
        cc_spec = create_cc_spec(cc_input, cc_id, CC_TYPE_GOLANG)

        cc_dep_spec = chaincode_pb2.ChaincodeDeploymentSpec()
        cc_dep_spec.chaincode_spec.CopyFrom(cc_spec)
        cc_dep_spec.effective_date.seconds = \
            tx_context.tx_prop_req.effective_date.seconds
        cc_dep_spec.effective_date.nanos = \
            tx_context.tx_prop_req.effective_date.nanos

        # construct the invoke spec
        # TODO: if the policy not provided, new one should be built.
        if request.cc_endorsement_policy:
            policy = request.cc_endorsement_policy
        else:
            policy = ''

        invoke_input = chaincode_pb2.ChaincodeInput()
        invoke_input.args.extend([
            proto_b(command),
            proto_b(self.name),
            cc_dep_spec.SerializeToString(),
            proto_b(policy),
            proto_b('escc'),
            proto_b('vscc')
        ])

        invoke_cc_id = chaincode_pb2.ChaincodeID()
        invoke_cc_id.name = proto_str('lscc')

        cc_invoke_spec = chaincode_pb2.ChaincodeInvocationSpec()
        cc_invoke_spec.chaincode_spec.CopyFrom(
            create_cc_spec(invoke_input, invoke_cc_id, CC_TYPE_GOLANG))

        extension = proposal_pb2.ChaincodeHeaderExtension()
        extension.chaincode_id.name = proto_str('lscc')
        channel_header = build_channel_header(
            common_pb2.ENDORSER_TRANSACTION,
            tx_context.tx_id,
            self.name,
            current_timestamp(),
            epoch=0,
            extension=extension.SerializeToString())

        header = build_header(tx_context.identity, channel_header,
                              tx_context.nonce)
        proposal = build_cc_proposal(cc_invoke_spec, header,
                                     request.transient_map)

        try:
            responses = send_transaction_proposal(proposal, header, tx_context,
                                                  peers)
        except Exception as e:
            raise IOError("fail to send transanction proposal", e)

        q = Queue(1)
        result = True
        for r in responses:
            r.subscribe(on_next=lambda x: q.put(x),
                        on_error=lambda x: q.put(x))
            res, _ = q.get()
            result = result and (res.response.status == 200)

        return result