Example #1
0
    def start_node():
        import downloads
        import nodes
        import tor
        import bandwidth
        state.bandwidth = bandwidth.Bandwidth()
        state.tor = tor.Tor()
        state.node = node.server.start()
        state.downloads = downloads.Downloads()
        #state.scraping = downloads.ScrapeThread()
        state.nodes = nodes.Nodes()

        def publish():
            if not state.tor.is_online():
                state.main.call_later(1, publish)
            else:
                nodes.publish_node()

        state.main.add_callback(publish)
x_value = [[] for i in range(num_Algo)]
y_value = [[] for i in range(num_Algo)]

for algo in range(num_Algo):

    # Iterate over the different configuration (Number of nodes)
    for x in range(10, max_node, pace_nodes):

        all_visit = []
        for sim in range(number_Simulations):
            # Create a new world for this configuration
            new_world = world.World(size_x, size_y)
            # Create the nodes
            nodes_list = []
            for i in range(x):
                newN = nodes.Nodes(i, False, new_world)
                nodes_list.append(newN)
            for n in nodes_list:
                n.sense_In_Range_Nodes()

            if algo == 0:
                v = nodes_list[0].complete_graph_coloring1()
            elif algo == 1:
                v = nodes_list[0].complete_graph_coloring2()
            else:
                v = nodes_list[0].complete_graph_coloring3()

            all_visit.append(v)

        average = sum(all_visit) / number_Simulations
        f.write(f'{algo} {x} {average}\n')
Example #3
0
Phase 1 : Initialisation and making of the conflict graph
'''
### Generate a world
import world

new_world = world.World(4, 4)

### Generation of all the nodes and master nodes inside nodes_list
import nodes

nodes_list = []
num_master = cst.NUM_MASTER_NODES

for x in range(cst.NUM_NODES):
    if (num_master > 0):
        newN = nodes.Nodes(x, True, new_world)
        nodes_list.append(newN)
        num_master = num_master - 1
        continue
    newN = nodes.Nodes(x, False, new_world)
    nodes_list.append(newN)

### Scouting for neighbor
# for n in nodes_list:
#     n.sense_In_Range_Nodes()

import gui
import time

### Creating a GUI to display the node and the annimations
new_GUI = gui.GUI()
Example #4
0
#   ############

node_list = []

new_world = world.World(5,5)

# for i in range(9):
#     newN = nodes.Nodes(i,False, new_world, False)
#     node_list.append(newN)


SU_list = []
PU_list = [] 
### SU
for i in range(8):
    newN = nodes.Nodes(i,False,new_world,False)
    node_list.append(newN)
    SU_list.append(newN)
### PU
for i in range(8, 9):
    newN = nodes.Nodes(i,False,new_world, True)
    node_list.append(newN)
    PU_list.append(newN)

for pu in PU_list:
    pu.set_PU_Color()

new_world.reset()
node_list[0].set_Positions_man(1,1)
node_list[1].set_Positions_man(2,0)
node_list[2].set_Positions_man(3,0)
Example #5
0
class Block():
    tx = transactions.Tx()
    blk = blockchain.Blockchain()
    node = nodes.Nodes()

    def mine(self, current_wallet):
        self.node.add_node(current_wallet)
        while True:
            last_block = get_last_block()
            index = last_block['index'] + 1
            timestamp = int(round(time.time() * 1000))
            transactions = get_transactions()
            clear_open_transactions()
            difficulty = calculate_difficulty(last_block, timestamp)
            proof = self.proof_of_work(timestamp, transactions, difficulty,
                                       current_wallet)
            block = {
                "index": index,
                "timestamp": timestamp,
                "transactions": transactions,
                "difficulty": difficulty,
                "hash": proof['hash'],
                "proof_of_work": proof['pof'],
                "last_timestamp": last_block['timestamp'],
                "last_difficulty": last_block['difficulty'],
                "previous_hash": last_block['hash']
            }
            self.blk.add_block(block)
            self.transfer_funds(transactions)
            nodes = get_nodes()
            mining_transactions = [{
                "sender": "MINING",
                "receiver": current_wallet,
                "amount": MINING_REWARD / 2
            }]
            individual_reward = (MINING_REWARD / 2) / len(nodes)
            for el in nodes:
                mining_transactions.append({
                    "sender": "MINING",
                    "receiver": el,
                    "amount": individual_reward
                })
            self.tx.add_mining_transactions(mining_transactions)

    def proof_of_work(self, timestamp, transactions, difficulty,
                      current_wallet):
        pof = {"success": False, "proof": -1, "difficulty": difficulty}
        while pof['success'] == False:
            pof['proof'] = random.randint(1, 2**31)
            pof = proof(timestamp, transactions, pof)
        return {"hash": pof['hash'], "pof": pof['proof']}

    def transfer_funds(self, transactions):
        if len(transactions) > 0:
            db = connect_to_db_accounts()
            for el in transactions:
                if Wallet.verify_transaction(el):
                    db.find_one_and_update({"username": el['receiver']},
                                           {'$inc': {
                                               "balance": el['amount']
                                           }})
                    print('{} received {}'.format(el['receiver'],
                                                  el['amount']))
                else:
                    print('Transaction {} is invalid'.format(el))
                    self.tx.remove_transaction(el)


# print(Block().tx.add_transaction('MINING', 'markgagnon', 100))
# Block().mine('markgagnon')
# print(get_transactions())
# clear_open_transactions()
# print(get_last_block())