Example #1
0
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)
Example #2
0
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)
Example #3
0
 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 trigger_pos_miners(the_miners_list, the_type_of_consensus,
                       expected_chain_length, numOfTXperBlock,
                       blockchainFunction):
    for counter in range(expected_chain_length):
        randomly_chosen_miners = []
        x = int(round((len(the_miners_list) / 2), 0))
        for i in range(x):
            randomly_chosen_miners.append(random.choice(the_miners_list))
        biggest_stake = 0
        final_chosen_miner = the_miners_list[0]
        temp_file_py = modification.read_file(
            'temporary/miners_stake_amounts.json')
        for chosen_miner in randomly_chosen_miners:
            stake = temp_file_py[chosen_miner.address]
            if stake > biggest_stake:
                biggest_stake = stake
                final_chosen_miner = chosen_miner
        for entity in the_miners_list:
            entity.next_pos_block_from = final_chosen_miner.address
        if mempool.MemPool.qsize() != 0:
            final_chosen_miner.build_block(numOfTXperBlock, mempool.MemPool,
                                           the_miners_list,
                                           the_type_of_consensus,
                                           blockchainFunction,
                                           expected_chain_length)
        output.simulation_progress(counter, expected_chain_length)
Example #5
0
 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)
Example #6
0
 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)
def dpos_voting(the_miners_list):
    temp_file_py = modification.read_file('temporary/miner_wallets_log.json')
    votes_and_stakes = {}
    for miner in the_miners_list:
        votes_and_stakes[miner.address] = {}
    for miner in the_miners_list:
        chosen_miner = miner
        while chosen_miner == miner:
            chosen_miner = random.choice(the_miners_list)
        miner.dpos_vote_for = chosen_miner.address
        max_amount_to_be_staked = temp_file_py[miner.address]
        miner.amount_to_be_staked = random.randint(0, max_amount_to_be_staked)
        votes_and_stakes[chosen_miner.address][
            miner.address] = miner.amount_to_be_staked
    return votes_and_stakes
Example #8
0
def fork_analysis(list_of_miners):
    chain_versions = []
    for entity in list_of_miners:
        temp_path = "temporary/" + entity.address + "_local_chain.json"
        chain = modification.read_file(temp_path)
        h = hashlib.sha256()
        h.update(str(chain).encode(encoding='UTF-8'))
        hashed_chain = h.hexdigest()
        if hashed_chain in chain_versions:
            pass
        else:
            chain_versions.append(hashed_chain)
    output.fork_analysis(len(chain_versions))
    modification.write_file(
        'temporary/forking_log.json',
        {"Number of times a fork appeared": len(chain_versions) - 1})
Example #9
0
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)
Example #10
0
 def global_chain_is_confirmed_by_majority(self, global_chain,
                                           no_of_miners):
     chain_is_confirmed = True
     temporary_confirmations_log = modification.read_file(
         'temporary/confirmation_log.json')
     for block in global_chain:
         condition_0 = block != '0'
         if condition_0:
             condition_1 = not (global_chain[block]['Header']['hash']
                                in temporary_confirmations_log)
             if condition_1:
                 chain_is_confirmed = False
                 break
             else:
                 condition_2 = temporary_confirmations_log[
                     global_chain[block]['Header']['hash']]['votes'] <= (
                         no_of_miners / 2)
                 if condition_2:
                     chain_is_confirmed = False
                     break
     return chain_is_confirmed
Example #11
0
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
Example #12
0
 def receive_new_block(self, new_block, type_of_consensus, miner_list,
                       blockchain_function, discarded_txs,
                       expected_chain_length):
     local_chain_temporary_file = modification.read_file(
         str("temporary/" + self.address + "_local_chain.json"))
     # print("a new block is received from " + str(new_block['generator_id']))
     condition_1 = (len(local_chain_temporary_file)
                    == 0) and (new_block['generator_id'] == 'The Network')
     if condition_1:
         self.add(new_block, blockchain_function, expected_chain_length,
                  miner_list)
     else:
         if self.gossiping:
             self.gossip(blockchain_function, miner_list)
         list_of_hashes_in_local_chain = []
         for key in local_chain_temporary_file:
             list_of_hashes_in_local_chain.append(
                 local_chain_temporary_file[key]['hash'])
         if new_block['hash'] not in list_of_hashes_in_local_chain:
             condition_2 = type_of_consensus == 1 and consensus.pow_block_is_valid(
                 new_block, self.top_block['hash'])
             condition_3 = type_of_consensus == 2 and new_block[
                 'generator_id'] == self.next_pos_block_from
             condition_4 = type_of_consensus == 3 and consensus.poa_block_is_valid(
                 new_block, self.top_block['hash'], miner_list)
             if condition_2 or condition_3 or condition_4:
                 self.add(new_block, blockchain_function,
                          expected_chain_length, miner_list)
                 time.sleep(self.trans_delay)
                 for elem in miner_list:
                     if elem.address in self.neighbours:
                         elem.receive_new_block(new_block,
                                                type_of_consensus,
                                                miner_list,
                                                blockchain_function,
                                                discarded_txs,
                                                expected_chain_length)
Example #13
0
 def receive_new_block(self, new_block, type_of_consensus, miner_list,
                       blockchain_function, expected_chain_length):
     block_already_received = False
     local_chain_temporary_file = modification.read_file(
         str("temporary/" + self.address + "_local_chain.json"))
     # print("a new block is received from " + str(new_block['generator_id']))
     condition_1 = (len(local_chain_temporary_file)
                    == 0) and (new_block['Header']['generator_id']
                               == 'The Network')
     if condition_1:
         self.add(new_block, blockchain_function, expected_chain_length,
                  miner_list)
     else:
         if self.gossiping:
             self.gossip(blockchain_function, miner_list)
         list_of_hashes_in_local_chain = []
         for key in local_chain_temporary_file:
             read_hash = local_chain_temporary_file[key]['Header']['hash']
             list_of_hashes_in_local_chain.append(read_hash)
             if new_block['Header']['hash'] == read_hash:
                 block_already_received = True
                 break
         if not block_already_received:
             if new_consensus_module.block_is_valid(
                     type_of_consensus, new_block, self.top_block,
                     self.next_pos_block_from, miner_list, self.delegates):
                 self.add(new_block, blockchain_function,
                          expected_chain_length, miner_list)
                 time.sleep(self.trans_delay)
                 for elem in miner_list:
                     if elem.address in self.neighbours:
                         elem.receive_new_block(new_block,
                                                type_of_consensus,
                                                miner_list,
                                                blockchain_function,
                                                expected_chain_length)
Example #14
0
import random
import output
import mempool
import modification

data = modification.read_file("Sim_parameters.json")


class Fog:
    def __init__(self, address):
        self.address = address
        self.tasks = []
        self.list_of_connected_users = set()
        self.STOR_PLC = data["STOR_PLC(0=in the Fog,1=in the BC)"]
        self.local_storage = []

    def receive_tasks(self, tasks, sender):
        self.tasks.extend(tasks)
        self.list_of_connected_users.add(sender)

    def send_tasks_to_BC(self, user_informed):
        temporary_task = random.choice(self.tasks)
        if not user_informed:
            output.inform_of_fog_procedure(temporary_task[-1], self.STOR_PLC)
        if temporary_task[-1] == 1:
            for task in self.tasks:
                if self.STOR_PLC == 1:
                    mempool.MemPool.put(task)
                else:
                    self.local_storage.append(task)
        if temporary_task[-1] == 2: