def generate_block(difficulty, merkle_root, transactions): try: 'set block header info' monitoring.log("log.=================================") monitoring.log("log.Generate Block.") start_time = time.time() prev_block_height, prev_hash = file_controller.get_last_block() # TODO: type error 발생 must be str , not int # if type(prev_block_height) is not int: # prev_block_height = int(prev_block_height) # if type(prev_hash) is not str: # prev_hash = str(prev_hash) block_info = merkle_root + prev_hash vote_result = difficulty monitoring.log("log.Mining Block.") block_hash, nonce, tryanderror = proof_of_work.proof_of_work( block_info, difficulty) timestamp = int(round(time.time()*1000.0)) # UTC timestamp rule monitoring.log("log.Create Block Header.") block_header = block.BlockHeader( prev_hash, nonce, merkle_root, vote_result, str(timestamp)) block_header.block_id = "test_block_id" block_header.block_number = int(prev_block_height) + 1 block_header.block_hash = block_hash block_header.block_info = block_info block_header.miner = nodeproperty.Total_peer_num block_header.num_tx = len(transactions) monitoring.log("log.Create Block.") new_block = block.Block(block_header, transactions) json_new_block = json.dumps( new_block, indent=4, default=lambda o: o.__dict__, sort_keys=True) monitoring.log("log.Generate block complete") monitoring.log("log. ") # end_time = time.time() # elapsed_time = end_time - start_time # monitoring.log("log.Block Generate Time Time: "+str(elapsed_time)+" seconds") # tps = voting.TransactionCountForConsensus / elapsed_time # print("log.Transaction per second : "+ str(tps)) file_controller.remove_all_transactions() file_controller.remove_all_voting() sender.send_to_all_peers(json_new_block,nodeproperty.My_receiver_port) # sender.send_to_all(json_new_block) monitoring.log("log.Send block complete") except TypeError as te: print("@generate block", te)
def genesisblock_generate(): transaction = "Base lab logchain project" prev_hash = "N/A" nonce = "N/A" merkle_root = hashlib.sha256(str(transaction).encode('utf-8')).hexdigest() vote_result = "N/A" timestamp = "20170904" block_header = block.BlockHeader( prev_hash, nonce, merkle_root, vote_result, timestamp) block_header.block_id = 'genesis_block' block_header.block_number = "1" block_header.block_info = block_header.prev_hash + block_header.merkle_root block_header.block_hash = hashlib.sha256( str(block_header.block_info).encode('utf-8')).hexdigest() block_header.miner = "Base Lab" block_header.num_tx = "1" genesisblock = block.Block(block_header, transaction) json_genesisblock = json.dumps( genesisblock, indent=4, default=lambda o: o.__dict__, sort_keys=True) file_controller.create_new_block( str(block_header.block_number), json_genesisblock) logging.debug('Genesis block created now.') logging.debug(" - block hash: "+ block_header.block_hash) logging.debug(" - block ID: " + block_header.block_id) logging.debug(" - block info: " + block_header.block_info) logging.debug(" - block transaction: " + transaction)
def generate_block(difficulty, merkle_root, transactions): try: 'set block header info' print("=================================") print("Generate Block") start_time = time.time() prev_block_height, prev_hash = file_controller.get_last_block() block_info = merkle_root + prev_hash vote_result = difficulty 'mining block' block_hash, nonce, tryanderror = proof_of_work.proof_of_work( block_info, difficulty) timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime()) 'create block header' block_header = block.BlockHeader( prev_hash, nonce, merkle_root, vote_result, timestamp) block_header.block_id = "test_block_id" block_header.block_number = int(prev_block_height) + 1 block_header.block_hash = block_hash block_header.block_info = block_info block_header.miner = nodeproperty.Total_peer_num block_header.num_tx = len(transactions) 'create block' new_block = block.Block(block_header, transactions) json_new_block = json.dumps( new_block, indent=4, default=lambda o: o.__dict__, sort_keys=True) print("Generate block complete") print(" ") end_time = time.time() elapsed_time = end_time - start_time print("Block Generate Time Time: %.8f seconds" % elapsed_time) print(" ") print("Transaction per second : ", 30 / elapsed_time) print("") file_controller.remove_all_transactions() file_controller.remove_all_voting() sender.send_to_all(json_new_block) print("send block complete") except TypeError as te: print("@generate block", te)