Exemple #1
0
    def confirm_block(self, confirmed_block_hash):
        """인증완료후 Block을 Confirm해 줍니다.
        :param confirmed_block_hash: 인증된 블럭의 hash
        :return: Block 에 포함된 tx 의 갯수를 리턴한다.
        """
        logging.debug("BlockChain::confirm_block")

        try:
            unconfirmed_block_byte = self.__confirmed_block_db.Get(BlockChain.UNCONFIRM_BLOCK_KEY)
        except KeyError:
            except_msg = f"there is no unconfirmed block in this peer block_hash({confirmed_block_hash})"
            logging.warning(except_msg)
            raise BlockchainError(except_msg)

        unconfirmed_block = Block()
        unconfirmed_block.deserialize_block(unconfirmed_block_byte)

        if unconfirmed_block.block_hash != confirmed_block_hash:
            logging.warning("It's not possible to add block while check block hash is fail-")
            raise BlockchainError('확인하는 블럭 해쉬 값이 다릅니다.')

        logging.debug("unconfirmed_block.block_hash: " + unconfirmed_block.block_hash)
        logging.debug("confirmed_block_hash: " + confirmed_block_hash)
        logging.debug("unconfirmed_block.prev_block_hash: " + unconfirmed_block.prev_block_hash)

        unconfirmed_block.block_status = BlockStatus.confirmed
        # Block Validate and save Block
        self.add_block(unconfirmed_block)
        self.__confirmed_block_db.Delete(BlockChain.UNCONFIRM_BLOCK_KEY)

        return unconfirmed_block.confirmed_transaction_list.__len__()
Exemple #2
0
    def __add_genesisblock(self, tx_info: dict = None):
        """제네시스 블럭을 추가 합니다.

        :param tx_info: Transaction data for making genesis block from an initial file
        :return:
        """
        logging.info("Make Genesis Block....")
        block = Block(channel_name=self.__channel_name)
        block.block_status = BlockStatus.confirmed
        if tx_info:
            keys = tx_info.keys()
            if "accounts" in keys and "message" in keys:
                genesis_validator = get_genesis_tx_validator(
                    self.__channel_name)
                is_valid, tx = genesis_validator.init_genesis_tx(tx_info)
                if is_valid:
                    block.put_genesis_transaction(tx)
                else:
                    raise TransactionInvalidError(
                        message=f"The Genesis block data is invalid."
                        f"That's why Genesis block couldn't be generated.")

        block.generate_block()
        # 제네시스 블럭을 추가 합니다.
        self.add_block(block)
Exemple #3
0
    def __add_genesisblock(self):
        """제네시스 블럭을 추가 합니다.

        :return:
        """
        logging.info("Make Genesis Block....")
        block = Block(channel_name=self.__channel_name)
        block.block_status = BlockStatus.confirmed
        block.generate_block()
        # 제네시스 블럭을 추가 합니다.
        self.add_block(block)
    def __add_single_tx_block_blockchain_return_tx(self):
        last_block = self.chain.last_block
        block = Block(channel_name=conf.LOOPCHAIN_DEFAULT_CHANNEL)
        tx = test_util.create_basic_tx(self.__peer_id, self.peer_auth)
        block.put_transaction(tx)

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

        block.generate_block(last_block)
        block.block_status = BlockStatus.confirmed

        # add_block to blockchain
        self.assertTrue(self.chain.add_block(block),
                        "Fail Add Block to BlockChain")
        return tx
Exemple #5
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