def connect(): click.echo("Connecting to bootstrap...") my_ip = get_my_ip() url = "http://" + my_ip + ":5000/setup_myself" r = requests.get(url) click.echo("Returned this:") click.echo(r.text)
def t(id, amount): # Send amount CC another node #url = "http://" + addresses[id] + ':5000/send_money' print("Sending request") my_ip = get_my_ip() print(my_ip) print(type(my_ip)) url = "http://" + my_ip + ":5000/send_money" r = requests.post(url, data={ 'ip': addresses.global_addresses[id], 'amount': amount }) print(r)
def incoming_transaction(): pickle_transaction = request.form.to_dict()['transaction'] pickle_wallet = request.form.to_dict()['wallet'] my_transaction = jsonpickle.decode(pickle_transaction) incoming_wallet = jsonpickle.decode(pickle_wallet) print("WALLET BEFORE") print("WHO AM I???") print(get_my_ip()) print(MY_NODE.wallet.utxos) if (node.validate_transaction(incoming_wallet, my_transaction, MY_NODE.wallet)): # Update the sender utxos # IF validated add transaction to block print(my_transaction) MY_NODE.add_transaction_to_block(my_transaction) print(MY_NODE.current_block) print("WALLET AFTER") print(MY_NODE.wallet.utxos) return ("OK")
def setup_bootstrap_node(): # Create the very first node # The node constructor also creates the Wallet() we need and binds it to the node (I hope) myNode = node.Node(0, MY_ADDRESS) myNode.ring.append({ 'ip': get_my_ip(), 'id': 1, 'public_key': myNode.wallet.get_public_key() }) # Create the genesis block with id = 0 and prev_hash = -1 genesis_block = block.Block( 0, -1, [] ) # TODO: we shouldn't have to pass emtpy list as listOfTransactions in constructor, see with peppas # Need to add the first and only transaction to the genesis block #print("The bootstrap node's wallet private key is ") #print(myNode.wallet.get_private_key()) first_transaction = transaction.genesis_transaction( myNode.wallet, NETWORK_SIZE) #first_transaction = transaction.Transaction( sender=0, recipient=MY_ADDRESS, amount=NETWORK_SIZE * 100, inputs=[]) # TODO: Use transaction.genesis_transaction genesis_block.add_transaction(first_transaction) print(genesis_block.listOfTransactions) # Add the first block to the node's blockchain if not myNode.chain.add_block(genesis_block): raise Exception('genesis not added') myNode.address = MY_ADDRESS myNode.current_block = myNode.create_new_block( myNode.chain.last_block().current_hash) print("current_block") print(myNode.current_block) print("Chain: ") print(myNode.chain.last_block()) print("Bootstrap node has: ") print(myNode.wallet.calculate_balance()) # Return the node return (myNode)
def balance(): r = requests.get("http://" + get_my_ip() + ":5000/get_balance") print("Your wallet has " + r.text + " NBCs")
for entry in MY_NODE.ring: print("TO: ", "http://" + entry['ip'] + ":5000/add_to_client_ring") # TODO: ** Maybe we have to jsonpickle? ** r = requests.post("http://" + entry['ip'] + ":5000/add_to_client_ring", data={ 'ring': ring_pickle, 'blockchain': blockchain_pickle }) app = Flask(__name__) # run it once fore every node #### IF BOOTSTRAP NODE #### if (get_my_ip() == '192.168.0.2'): print("I'm gonna be the bootstrap!!!") MY_ADDRESS = get_my_ip() # Number of nodes in network NETWORK_SIZE = 5 #IDs NODE_IDS = [5, 4, 3, 2] # IP addresses of all nodes in ring MY_NODE = setup_bootstrap_node() #ADDRESS_BOOK = [{MY_ADDRESS:MY_NODE.wallet.get_public_key()}] #### IF REGULAR NODE #### else: print("I'm gonna be a client :(") MY_ADDRESS = get_my_ip() # Bootstrap node address (we suppose it is known to everyone) SERVER_ADDRESS = '192.168.0.2'