def validate_transactions(self, list_of_new_transactions, miner_role): user_wallets_temporary_file = modification.read_file( str("temporary/" + self.address + "_users_wallets.json")) if list_of_new_transactions: for key in user_wallets_temporary_file: for transaction in list_of_new_transactions: if miner_role == "receiver": if key == (str(transaction[1]) + "." + str(transaction[2])): if user_wallets_temporary_file[key][ 'wallet_value'] >= transaction[0]: user_wallets_temporary_file[key][ 'wallet_value'] -= transaction[0] else: return False if key == (str(transaction[3]) + "." + str(transaction[4])): user_wallets_temporary_file[key][ 'wallet_value'] += transaction[0] if miner_role == "generator" and key == ( str(transaction[1]) + "." + str(transaction[2])): if user_wallets_temporary_file[key][ 'wallet_value'] < transaction[0]: output.illegal_tx( transaction, user_wallets_temporary_file[key] ['wallet_value']) del transaction if miner_role == "generator": return list_of_new_transactions if miner_role == "receiver": modification.rewrite_file( str("temporary/" + self.address + "_users_wallets.json"), user_wallets_temporary_file) return True
def award_winning_miners(num_of_miners): final_confirmation_log = modification.read_file( "temporary/confirmation_log.json") miner_final_wallets_log_py = modification.read_file( "temporary/miner_wallets_log.json") for key in final_confirmation_log: if final_confirmation_log[key]['votes'] > int(num_of_miners / 2): for key1 in miner_final_wallets_log_py: if key1 == final_confirmation_log[key]['winning_miner']: miner_final_wallets_log_py[key1] += mining_award modification.rewrite_file("temporary/miner_wallets_log.json", miner_final_wallets_log_py)
def update_global_longest_chain(self, local_chain_temporary_file, blockchain_function, list_of_miners): temporary_global_longest_chain = modification.read_file( 'temporary/longest_chain.json') if len(temporary_global_longest_chain['chain']) < len( local_chain_temporary_file): temporary_global_longest_chain[ 'chain'] = local_chain_temporary_file temporary_global_longest_chain['from'] = self.address modification.rewrite_file('temporary/longest_chain.json', temporary_global_longest_chain) else: if len(temporary_global_longest_chain['chain']) > len( local_chain_temporary_file) and self.gossiping: self.gossip(blockchain_function, list_of_miners)
def inform_miners_of_users_wallets(): if blockchainFunction == 3: user_wallets = {} for user in list_of_end_users: wallet_info = { 'parent': user.addressParent, 'self': user.addressSelf, 'wallet_value': user.wallet } user_wallets[str(user.addressParent) + '.' + str(user.addressSelf)] = wallet_info for i in range(len(miner_list)): modification.rewrite_file( str("temporary/" + miner_list[i].address + "_users_wallets.json"), user_wallets)
def stake(list_of_miners, num_of_consensus): if num_of_consensus == 2: for miner in list_of_miners: temp_miner_wallets_log_py = modification.read_file( 'temporary/miner_wallets_log.json') temp_miners_stake_amounts_py = modification.read_file( 'temporary/miners_stake_amounts.json') temp_miners_stake_amounts_py[miner.address] = random.randint( 0, temp_miner_wallets_log_py[miner.address]) temp_miner_wallets_log_py[ miner.address] -= temp_miners_stake_amounts_py[miner.address] modification.rewrite_file('temporary/miner_wallets_log.json', temp_miner_wallets_log_py) modification.rewrite_file('temporary/miners_stake_amounts.json', temp_miners_stake_amounts_py)
def report_a_successful_block_addition(winning_miner, hash_of_added_block): record_exist = False temporary_confirmation_log = modification.read_file( "temporary/confirmation_log.json") for key in temporary_confirmation_log: if key == hash_of_added_block and winning_miner == temporary_confirmation_log[ key]['winning_miner']: temporary_confirmation_log[key]['votes'] += 1 record_exist = True break if not record_exist: temporary_confirmation_log[str(hash_of_added_block)] = { 'winning_miner': winning_miner, 'votes': 1 } modification.rewrite_file("temporary/confirmation_log.json", temporary_confirmation_log)
def add(self, block, blockchain_function, expected_chain_length, list_of_miners): ready = False while True: try: local_chain_external_file = open( str("temporary/" + self.address + "_local_chain.json")) local_chain_temporary_file = json.load( local_chain_external_file) local_chain_external_file.close() break except: time.sleep(0.2) if len(local_chain_temporary_file) == 0: ready = True else: condition = blockchain_function == 3 and self.validate_transactions( block['Body']['transactions'], "receiver") if blockchain_function != 3 or condition: if block['Body']['previous_hash'] == self.top_block['Header'][ 'hash']: blockchain.report_a_successful_block_addition( block['Header']['generator_id'], block['Header']['hash']) # output.block_success_addition(self.address, block['generator_id']) ready = True if ready: block['Header']['blockNo'] = len(local_chain_temporary_file) self.top_block = block local_chain_temporary_file[str( len(local_chain_temporary_file))] = block modification.rewrite_file( str("temporary/" + self.address + "_local_chain.json"), local_chain_temporary_file) if self.gossiping: self.update_global_longest_chain(local_chain_temporary_file, blockchain_function, list_of_miners)
def initiate_miners(): the_miners_list = [] if blockchainPlacement == 1: for i in range(NumOfFogNodes): the_miners_list.append( miner.Miner(i + 1, trans_delay, gossip_activated)) if blockchainPlacement == 2: for i in range(NumOfMiners): the_miners_list.append( miner.Miner(i + 1, trans_delay, gossip_activated)) for entity in the_miners_list: modification.write_file( "temporary/" + entity.address + "_local_chain.json", {}) miner_wallets_log_py = modification.read_file( "temporary/miner_wallets_log.json") miner_wallets_log_py[str( entity.address)] = data['miners_initial_wallet_value'] modification.rewrite_file("temporary/miner_wallets_log.json", miner_wallets_log_py) connect_miners(the_miners_list) output.miners_are_up() return the_miners_list
def gossip(self, blockchain_function, list_of_miners): local_chain_temporary_file = modification.read_file( str("temporary/" + self.address + "_local_chain.json")) temporary_global_longest_chain = modification.read_file( 'temporary/longest_chain.json') condition_1 = len(temporary_global_longest_chain['chain']) > len( local_chain_temporary_file) condition_2 = self.global_chain_is_confirmed_by_majority( temporary_global_longest_chain['chain'], len(list_of_miners)) if condition_1 and condition_2: confirmed_chain = temporary_global_longest_chain['chain'] confirmed_chain_from = temporary_global_longest_chain['from'] modification.rewrite_file( str("temporary/" + self.address + "_local_chain.json"), confirmed_chain) self.top_block = confirmed_chain[str(len(confirmed_chain) - 1)] output.local_chain_is_updated(self.address, len(confirmed_chain)) if blockchain_function == 3: user_wallets_temp_file = modification.read_file( str("temporary/" + confirmed_chain_from + "_users_wallets.json")) modification.rewrite_file( str("temporary/" + self.address + "_users_wallets.json"), user_wallets_temp_file)