Beispiel #1
0
    def create_tx(self, data):
        tx = Transaction()
        score_id = ""
        score_version = ""

        try:
            score_info = self._channel_service.score_info
            score_id = score_info[message_code.MetaParams.ScoreInfo.score_id]
            score_version = score_info[message_code.MetaParams.ScoreInfo.score_version]
        except KeyError as e:
            logging.debug(f"CreateTX : load score info fail\n"
                          f"cause : {e}")

        send_tx_type = self._channel_service.get_channel_option()["send_tx_type"]
        tx.init_meta(ChannelProperty().peer_id, score_id, score_version, ChannelProperty().name, send_tx_type)
        tx.put_data(data)
        tx.sign_hash(self._channel_service.peer_auth)

        self._channel_service.broadcast_scheduler.schedule_job(BroadcastCommand.CREATE_TX, tx)

        try:
            data_log = json.loads(data)
        except Exception as e:
            data_log = {'tx_hash': tx.tx_hash}

        util.apm_event(ChannelProperty().peer_id, {
            'event_type': 'CreateTx',
            'peer_id': ChannelProperty().peer_id,
            'peer_name': conf.PEER_NAME,
            'channel_name': ChannelProperty().name,
            'tx_hash': tx.tx_hash,
            'data': data_log})

        return tx.tx_hash
Beispiel #2
0
    def test_cert_signature(self):
        """GIVEN conf.TX_CERT_AUTH = True, PeerAuthorization create using cert
        WHEN create new tx and create signature
        THEN tx.public_key must be x.509 der cert
        """
        channel_name = "cert_channel"
        conf.CHANNEL_OPTION = {
            channel_name: {
                "load_cert": True,
                "consensus_cert_use": True,
                "tx_cert_use": True,
                "key_load_type": conf.KeyLoadType.FILE_LOAD,
                "public_path": os.path.join(conf.LOOPCHAIN_ROOT_PATH, 'resources/default_certs/cert.pem'),
                "private_path": os.path.join(conf.LOOPCHAIN_ROOT_PATH, 'resources/default_certs/key.pem'),
                "private_password": None
            }
        }
        peer_auth = PeerAuthorization(channel_name)

        # WHEN
        tx = Transaction()
        tx.put_data('{"a":"b"}')
        tx.sign_hash(peer_auth)

        # THEN
        logging.debug(f"tx public key : {tx.public_key}")
        self.assertEqual(tx.public_key, peer_auth.tx_cert)
        # tx.publickey using for load certificate and not raise any error
        x509.load_der_x509_certificate(tx.public_key, default_backend())
Beispiel #3
0
def create_basic_tx(peer_id: str, peer_auth: PeerAuthorization) -> Transaction:
    """ create basic tx data is "{args:[]}"

    :param peer_id: peer_id
    :param peer_auth:
    :return: transaction
    """
    tx = Transaction()
    tx.put_meta('peer_id', peer_id)
    tx.put_data("{args:[]}")
    tx.sign_hash(peer_auth)
    return tx
Beispiel #4
0
def create_tx(peer_id: str, data: str, peer_auth: PeerAuthorization) -> Transaction:
    """ create basic tx data is "{args:[]}"

    :param peer_id: peer_id
    :param data: tx_data
    :param peer_auth:
    :return: transaction
    """
    tx = Transaction()
    tx.put_meta('peer_id', peer_id)
    tx.put_meta(Transaction.CHANNEL_KEY, conf.LOOPCHAIN_DEFAULT_CHANNEL)
    tx.put_data(data)
    tx.sign_hash(peer_auth)
    return tx
Beispiel #5
0
 def test_block_rebuild(self):
     """ GIVEN 1Block with 3tx, and conf remove failed tx when in block
     WHEN Block call verify_through_score_invoke
     THEN all order 3tx must removed in block
     """
     block = Block(conf.LOOPCHAIN_DEFAULT_CHANNEL)
     fail_tx_hash = None
     for i in range(3):
         tx = Transaction()
         tx.put_meta(Transaction.CHANNEL_KEY,
                     conf.LOOPCHAIN_DEFAULT_CHANNEL)
         tx.put_data("aaaaa")
         tx.sign_hash(self.peer_auth)
         block.put_transaction(tx)
         if i == 2:
             fail_tx_hash = tx.tx_hash
     verify, need_rebuild, invoke_results = block.verify_through_score_invoke(
         True)
     self.assertTrue(need_rebuild)
     logging.debug(f"fail tx hash : {fail_tx_hash}")
     self.assertEqual(block.confirmed_tx_len, 2)
     for i, tx in enumerate(block.confirmed_transaction_list):
         self.assertNotEqual(i, 2, "index 2 must be deleted")
         self.assertNotEqual(tx.tx_hash, fail_tx_hash)