Пример #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
Пример #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())
Пример #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
Пример #4
0
    def generate_block(self):
        """
        임시 블럭 생성하는 메소드
        :return: 임시 블럭
        """

        block = Block()
        for x in range(10):
            tx = Transaction()
            tx.put_data("{args:[]}")
            block.put_transaction(tx)
        block.generate_block(self.chain.last_block)
        return block
Пример #5
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
Пример #6
0
    def test_put_data(self):
        """트랜잭션 생성확인
        해쉬값의 존재여부

        :return:
        """
        tx = Transaction()
        txhash = tx.put_data("{args:[]}")
        self.assertNotEqual(txhash, "")
Пример #7
0
    def test_dump_tx_size(self):
        # GIVEN
        tx = Transaction()
        tx.put_data("TEST")
        tx.transaction_type = TransactionStatus.confirmed

        tx_only_data = TransactionDataOnly()
        tx_only_data.data = "TEST"

        # WHEN
        dump_a = pickle.dumps(tx_only_data)
        dump_b = pickle.dumps(tx)

        # THEN
        logging.debug("size of tx_only_data: " + str(sys.getsizeof(dump_a)))
        logging.debug("size of tx: " + str(sys.getsizeof(dump_b)))

        self.assertLessEqual(sys.getsizeof(dump_a), sys.getsizeof(dump_b) * 1.5)
Пример #8
0
 def test_put_transaction(self):
     """
     Block 에 여러 개 transaction 들을 넣는 것을 test.
     """
     block = Block()
     tx_list = []
     tx_size = 10
     for x in range(0, tx_size):
         tx = Transaction()
         tx2 = Transaction()
         hashed_value = tx.put_data("{args:[]}")
         tx2.put_data("{args:[]}")
         tx_list.append(tx2)
         self.assertNotEqual(hashed_value, "", "트랜잭션 생성 실패")
         self.assertTrue(block.put_transaction(tx), "Block에 트랜잭션 추가 실패")
     self.assertTrue(block.put_transaction(tx_list), "Block에 여러 트랜잭션 추가 실패")
     self.assertEqual(len(block.confirmed_transaction_list), tx_size * 2,
                      "트랜잭션 사이즈 확인 실패")
Пример #9
0
    def generate_block(self):
        """
        블럭 생성기
        :return: 임의생성블럭
        """
        genesis = Block()
        genesis.generate_block()
        # Block 생성
        block = Block()
        # Transaction(s) 추가
        for x in range(0, 10):
            tx = Transaction()
            tx.put_data("{args:[]}")
            block.put_transaction(tx)

        # Hash 생성 이 작업까지 끝내고 나서 Block을 peer에 보낸다
        block.generate_block(genesis)
        return block
Пример #10
0
    def generate_test_block(self):
        """
        임시 블럭 생성하는 메소드
        :return: 임시 블럭
        """

        block = Block()
        for x in range(0, 10):
            tx = Transaction()
            hashed_value = tx.put_data("{args:[]}")
            self.assertNotEqual(hashed_value, "", "트랜잭션 생성 실패")
            self.assertTrue(block.put_transaction(tx), "Block에 트랜잭션 추가 실패")

        return block
Пример #11
0
    def test_tx_json_serialize(self):
        # GIVEN
        tx = Transaction()
        tx.put_data("TEST")
        tx.transaction_type = TransactionStatus.confirmed
        logging.debug(f"transaction for test({tx})")

        # WHEN
        wrap_up = {"__Transaction__": tx}
        serialized = json.dumps(wrap_up, sort_keys=True, cls=CustomEncoder)
        logging.debug(f"serialized tx: {serialized}")

        tx_json = json.loads(serialized, object_hook=decode_object)
        logging.debug(f"deserialized tx: {tx_json}")

        wrap_up_again = {"__Transaction__": tx_json}
        serialized_again = json.dumps(wrap_up_again,
                                      sort_keys=True,
                                      cls=CustomEncoder)
        logging.debug(f"re-serialized tx: {serialized_again}")

        # THEN
        self.assertEqual(serialized, serialized_again)
Пример #12
0
    def test_generate_and_validate_hash(self):
        """트랜잭션 생성시 만들어진 hash 와 검증시 비교하는 hash 가 동일한지 확인하는 테스트

        :return:
        """
        # GIVEN
        tx = Transaction()
        tx.init_meta("AAAAA", "BBBBB", "CCCCC", conf.LOOPCHAIN_DEFAULT_CHANNEL)
        tx.put_meta("1234", "5678")
        tx.put_meta("1", "5")
        tx.put_meta("2", "5")
        tx.put_meta("3", "5")
        tx.put_meta("4", "5")
        txhash1 = tx.put_data("TEST DATA DATA")
        txtime = tx.get_timestamp()

        tx2 = Transaction()
        tx2.init_meta("AAAAA", "BBBBB", "CCCCC",
                      conf.LOOPCHAIN_DEFAULT_CHANNEL)
        tx2.put_meta("1234", "5678")
        tx2.put_meta("1", "5")
        tx2.put_meta("2", "5")
        tx2.put_meta("3", "5")
        tx2.put_meta("4", "5")
        txhash2 = tx2.put_data("TEST DATA DATA", txtime)

        # WHEN
        txhash1_1 = Transaction.generate_transaction_hash(tx)

        # THEN
        logging.debug("txhash1: " + str(txhash1))
        logging.debug("txhash1_1: " + str(txhash1_1))
        logging.debug("txhash2: " + str(txhash2))

        self.assertEqual(txhash1, txhash2)
        self.assertEqual(txhash1, txhash1_1)
        self.assertEqual(txhash2, txhash1_1)
Пример #13
0
    def test_diff_hash(self):
        """트랜잭션을 생성하여, 같은 값을 입력 하여도 트랜잭션 HASH는 달라야 함
        1000건 생성하여 트랜잭션 비교

        :return:
        """
        sttime = time.time()
        tx_list = []
        test_size = 1000
        for x in range(test_size):
            tx1 = Transaction()
            hashed_value = tx1.put_data("{args:[]}")
            tx_list.append(hashed_value)
        self.assertTrue(len(set(tx_list)) == len(tx_list), "중복된 트랜잭션이 있습니다.")
        logging.debug("test_diff_hash %i times : %f", test_size, time.time() - sttime)
Пример #14
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)
Пример #15
0
    def __add_single_tx_to_block_return_tx_with_test(self):
        last_block = self.chain.last_block
        block = Block()
        tx = Transaction()
        hashed_value = tx.put_data("1234")
        self.assertNotEqual(hashed_value, "", "트랜잭션 생성 실패")
        self.assertTrue(block.put_transaction(tx), "Block에 트랜잭션 추가 실패")

        logging.debug("tx_hash: " + tx.get_tx_hash())

        block.generate_block(last_block)
        block.block_status = BlockStatus.confirmed
        # add_block include __add_tx_to_block_db what we want to test
        self.assertTrue(self.chain.add_block(block),
                        "Fail Add Block to BlockChain in test_add_tx_to_block_db")
        return tx
Пример #16
0
    def test_transaction_performace(self):
        """트랜잭션의 생성 퍼포먼스 1초에 몇개까지 만들 수 있는지 확인
        1초에 5000개 이상

        :return:
        """
        _sttime = time.time()
        tx_list = []

        dummy_data = "TEST Transaction Data"
        put_data = 0
        while time.time() - _sttime < 1.0:
            tx1 = Transaction()
            hashed_value = tx1.put_data("{args:[]}" + (dummy_data + str(put_data)))
            tx_list.append(hashed_value)
            put_data += 1

        self.assertTrue(len(set(tx_list)) == len(tx_list), "중복된 트랜잭션이 있습니다.")
        logging.debug("TX generate %i in a second", len(tx_list))

        self.assertTrue(len(tx_list) > 5000, len(tx_list))