def walletServer(my_addr): global head_blocks try: head_blocks = TxBlock.loadBlocks("WalletBlocks.dat") except: print("WS:No previous blocks found. Starting fresh.") head_blocks = [None] server = SocketUtils.newServerConnection('localhost', 5006) while not break_now: newBlock = SocketUtils.recvObj(server) if isinstance(newBlock, TxBlock.TxBlock): if verbose: print("Rec'd block") for b in head_blocks: if b == None: if newBlock.previousHash == None: newBlock.previousBlock = b if not newBlock.is_valid(): print("Error! newBlock is not valid") else: head_blocks.remove(b) head_blocks.append(newBlock) if verbose: print("Added to head_blocks") elif newBlock.previousHash == b.computeHash(): newBlock.previousBlock = b if not newBlock.is_valid(): print("Error! newBlock is not valid") else: head_blocks.remove(b) head_blocks.append(newBlock) if verbose: print("Added to head_blocks") #TODO What if I add to an earlier (non-head) block? TxBlock.saveBlocks(head_blocks, "WalletBlocks.dat") server.close() return True
def nonceFinder(wallet_list, miner_public): global break_now # add Txs to new block while not break_now: newBlock = TxBlock.TxBlock(TxBlock.findLongestBlockchain(head_blocks)) for tx in tx_list: newBlock.addTx(tx) # Compute and add mining reward total_in, total_out = newBlock.count_totals() mine_reward = Transactions.Tx() mine_reward.add_output(miner_public, 25.0 + total_in - total_out) newBlock.addTx(mine_reward) # Find nonce if verbose: print("Finding Nonce...") newBlock.find_nonce(10000) if newBlock.good_nonce(): if verbose: print("Good nonce found") head_blocks.remove(newBlock.previousBlock) head_blocks.append(newBlock) # Send new block savePrev = newBlock.previousBlock newBlock.previousBlock = None for ip_addr, port in wallet_list: if verbose: print("Sending to " + ip_addr + ":" + str(port)) SocketUtils.sendObj(ip_addr, newBlock, 5006) newBlock.previousBlock = savePrev # Remove used txs from tx_list for tx in newBlock.data: if tx != mine_reward: tx_list.remove(tx) return True
def nonceFinder(wallet_list,miner_public): # Add txs to block global break_now while not break_now: newBlock = TxBlock.TxBlock([],findLongestBlockchain()) for tx in tx_list: newBlock.addTx(tx) # Compute and add mining reward total_in,total_out = newBlock.count_totals() mine_reward = Transactions.Tx() mine_reward.add_output(miner_public,25.0+total_in-total_out) newBlock.addTx(mine_reward) # Find nonce print ("Finding Nonce...") newBlock.find_nonce(10000) if newBlock.good_nonce(): print ("Good nonce found") # Send new block for ip_addr,port in wallet_list: print ("Sending to " + ip_addr + ":" + str(port)) Socketutil.sendObj(ip_addr,newBlock,5006) head_blocks.remove(newBlock.previousBlock) head_blocks.append(newBlock) break return True
def nonceFinder(walletList, minerPubl): global breakNow #collect those transactions into a block while not breakNow: newBlock = TxBlock.TxBlock(findLongestBlockChain()) for tx in txList: newBlock.addTx(tx) #compute and add mining reward totalIn, totalOut = newBlock.count_totals() mineReward = Transaction.Tx() mineReward.add_output(minerPubl, 25.0 + totalIn - totalOut) newBlock.addTx(mineReward) #Mine (i.e. find a satisfying nonce) newBlock.find_nonce(10000) if newBlock.good_nonce(): print("Good nonce found") #send that new block to each in walletList for ipAddr, port in walletList: print("Sending to "+ ipAddr + ":" + str(port)) SocketUtils.sendObj(ipAddr, newBlock, 5006) headBlocks.remove(newBlock.previousBlock) headBlocks.append(newBlock) return True
def nonceFinder(wallet_list, miner_public): global break_now try: head_blocks = TxBlock.loadBlocks("AllBlocks.dat") except: print("No previous blocks found. Starting fresh.") head_blocks = [None] # add Txs to new block while not break_now: newBlock = TxBlock.TxBlock(TxBlock.findLongestBlockchain(head_blocks)) placeholder = Transactions.Tx() placeholder.add_output(miner_public,25.0) newBlock.addTx(placeholder) #sort tx_list by tx fee per byte for tx in tx_list: newBlock.addTx(tx) if not newBlock.check_size(): newBlock.removeTx(tx) break newBlock.removeTx(placeholder) if verbose: print("new Block has " + str(len(newBlock.data)) + "txs.") # Compute and add mining reward total_in,total_out = newBlock.count_totals() mine_reward = Transactions.Tx() mine_reward.add_output(miner_public,25.0+total_in-total_out) newBlock.addTx(mine_reward) # Find nonce if verbose: print ("Finding Nonce...") newBlock.find_nonce(10000) if newBlock.good_nonce(): if verbose: print ("Good nonce found") head_blocks.remove(newBlock.previousBlock) head_blocks.append(newBlock) # Send new block savePrev = newBlock.previousBlock newBlock.previousBlock = None for ip_addr,port in wallet_list: if verbose: print ("Sending to " + ip_addr + ":" + str(port)) SocketUtils.sendObj(ip_addr,newBlock,5006) newBlock.previousBlock = savePrev # Remove used txs from tx_list for tx in newBlock.data: if tx != mine_reward: tx_list.remove(tx) TxBlock.saveBlocks(head_blocks,"AllBlocks.dat") return True
def minerServer(my_addr): global tx_list global break_now global head_blocks # Load tx_list try: tx_list = loadTxList("Txs.dat") except: print("No previous Txs. Starting fresh.") tx_list = [] my_ip, my_port = my_addr # Open Server Connection server = SocketUtils.newServerConnect(my_ip, my_port) # Receive transactions from wallet while not break_now: newObj = SocketUtils.recvObj(server) # isinstance requests if an Object is an Instance of a Class. if isinstance(newObj, Transaction.Tx): duplicate = False for addr,amt,inx in newObj.inputs: for tx in tx_list: for addr2, amt2, inx2 in tx.inputs: if addr2 == addr and inx2 == inx: duplicate = True if duplicate: break tx_list.append(newObj) elif isinstance(newObj, TxBlock.TxBlock): #Add new Blocks to BlockChain TxBlock.processNewBlock(newObj, head_blocks) # Transaction has been included need to remove. for tx in newObj: if tx in tx_list: tx_list.remove(tx) saveTxList(tx_list, "Txs.dat") return False
def minerServer(my_addr): global tx_list global break_now global head_blocks try: tx_list = loadTxList("Txs.dat") if verbose: print("Loaded tx_list has " + str(len(tx_list)) + " Txs.") except: print("No previous tx_list found. Starting fresh") tx_list = [] my_ip, my_port = my_addr server = SocketUtils.newServerConnection(my_ip, my_port) # Get Txs from wallets while not break_now: newObj = SocketUtils.recvObj(server) if isinstance(newObj, Transactions.Tx): #TODO check transactions for goodness in nonceFinder? #TODO check transactions for well-ordered indeces #TODO order tx_list to make indeces well-ordered duplicate = False for addr, amt, inx in newObj.inputs: for tx in tx_list: for addr2, amt2, inx2 in tx.inputs: if addr2 == addr and inx2 == inx: duplicate = True if duplicate: break tx_list.append(newObj) if verbose: print("Recd tx") if verbose: print("tx_list contains " + str(len(tx_list)) + " Txs.") elif isinstance(newObj, TxBlock.TxBlock): print("Rec'd new block") TxBlock.processNewBlock(newObj, head_blocks, True) for tx in newObj.data: if tx in tx_list: tx_list.remove(tx) else: print("rec'd " + str(type(newObj))) if verbose: print("Saving " + str(len(tx_list)) + " txs to Txs.dat") saveTxList(tx_list, "Txs.dat") return False
def getBalance(pu_key): long_chain = TxBlock.findLongestBlockchain(head_blocks) this_block = long_chain bal = 0.0 while this_block != None: for tx in this_block.data: for addr, amt in tx.inputs: if addr == pu_key: bal = bal - amt for addr, amt in tx.outputs: if addr == pu_key: bal = bal + amt this_block = this_block.previousBlock return bal
def walletServer(my_addr): global head_blocks global tx_index # Load head_blocks try: # Save Wallet blocks to different file to Miner: AllBlocks.dat. # Otherwise dangerous to read at the same time if you were writing to the same file. head_blocks = TxBlock.loadBlocks("AllBlocks.dat") except: print("No previous blocks found. Starting fresh.") head_blocks = [None] #TxBlock.loadBlocks("Genesis.dat") try: fp = open("tx_index", "rb") tx_index = pickle.load(fp) fp.close() except: tx_index = {} server = SocketUtils.newServerConnect('localhost', 5006) while not break_now: newBlock = SocketUtils.recvObj(server) if isinstance(newBlock, TxBlock.TxBlock): TxBlock.processNewBlock(newBlock, head_blocks) #TODO handle orphaned blocks. What is Child appears before the parent block? server.close() # Save head_block TxBlock.saveBlocks(head_blocks, "WalletBlocks.dat") fp = open("tx_index.dat", "wb") pickle.dump(tx_index, fp) fp.close() return True
def nonceFinder(wallet_list, miner_public): global break_now global head_blocks # add Txs to new block try: head_blocks = TxBlock.loadBlocks("AllBlocks.dat") except: head_blocks = TxBlock.loadBlocks("GenesisBlock.dat") while not break_now: newBlock = TxBlock.TxBlock(TxBlock.findLongestBlockchain(head_blocks)) tmp = Transactions.Tx() tmp.add_output(miner_public, 25.0) newBlock.addTx(tmp) for tx in tx_list: newBlock.addTx(tx) if not newBlock.check_size(): newBlock.removeTx(tx) break newBlock.removeTx(tmp) # Compute and add mining reward total_in, total_out = newBlock.count_totals() mine_reward = Transactions.Tx() mine_reward.add_output(miner_public, 25.0 + total_in - total_out) newBlock.addTx(mine_reward) # Find nonce if verbose: print("Finding Nonce...") newBlock.find_nonce(10000) if newBlock.good_nonce(): if verbose: print("Good nonce found") if not newBlock.previousBlock in head_blocks: break head_blocks.remove(newBlock.previousBlock) head_blocks.append(newBlock) # Send new block savePrev = newBlock.previousBlock newBlock.previousBlock = None for ip_addr, port in wallet_list: if verbose: print("Sending to " + ip_addr + ":" + str(port)) SocketUtils.sendObj(ip_addr, newBlock, port) newBlock.previousBlock = savePrev # Remove used txs from tx_list for tx in newBlock.data: if tx != mine_reward: tx_list.remove(tx) TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") return True
def walletServer(my_addr): global head_blocks global tx_index try: head_blocks = TxBlock.loadBlocks("AllBlocks.dat") except: head_blocks = TxBlock.loadBlocks("GenesisBlock.dat") try: fp = open("tx_index.dat", "rb") tx_index = pickle.load(fp) fp.close() except: tx_index = {} server = SocketUtils.newServerConnection('localhost', 5006) while not break_now: newBlock = SocketUtils.recvObj(server) if isinstance(newBlock, TxBlock.TxBlock): TxBlock.processNewBlock(newBlock, head_blocks) server.close() TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") fp = open("tx_index.dat", "wb") pickle.dump(tx_index, fp) fp.close() return True
sendCoins(pu1, 0.1, pr1, pu2, 0.1, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) sendCoins(pu1, 0.1, pr1, pu3, 0.03, miners) time.sleep(150) #Save/Load all blocks TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") head_blocks = TxBlock.loadBlocks("AllBlocks.dat") #Query balances new1 = getBalance(pu1) print(new1) new2 = getBalance(pu2) new3 = getBalance(pu3) #Verify balances if abs(new1 - bal1 + 2.0) > 0.00000001: print("Error! Wrong balance for pu1") else: print("Success. Good balance for pu1") if abs(new2 - bal2 - 1.0) > 0.00000001: print("Error! Wrong balance for pu2")
s.connect((ip_addr, TCP_PORT)) data = pickle.dumps(blk) s.send(data) s.close() return False if __name__ == '__main__': pr1, pu1 = generate_keys() pr2, pu2 = generate_keys() pr3, pu3 = generate_keys() Tx1 = Tx() Tx1.add_input(pu1, 2.3) Tx1.add_output(pu2, 1.0) Tx1.add_output(pu3, 1.1) Tx1.sign(pr1) Tx2 = Tx() Tx2.add_input(pu3, 2.3) Tx2.add_input(pu2, 1.0) Tx2.add_output(pu1, 3.1) Tx2.sign(pr3) Tx2.sign(pr2) B1 = TxBlock([], None) B1.addTx(Tx1) B1.addTx(Tx2) sendBlock('localhost', B1) sendBlock('localhost', Tx2)
sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) t2.start() time.sleep(60) #Save/Load all blocks TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") head_blocks = TxBlock.loadBlocks("AllBlocks.dat") #Query balances new1 = getBalance(pu1) print(new1) new2 = getBalance(pu2) new3 = getBalance(pu3) #Verify balances if abs(new1 - bal1 + 2.0) > 0.00000001: print("Error! Wrong balance for pu1") else: print("Success. Good balance for pu1") if abs(new2 - bal2 - 1.0) > 0.00000001: print("Error! Wrong balance for pu2")
data = pickle.dumps(blk) s.send(data) s.close() return False if __name__ == "__main__": pr1, pu1 = Signatures.generate_keys() pr2, pu2 = Signatures.generate_keys() pr3, pu3 = Signatures.generate_keys() Tx1 = Transactions.Tx() Tx1.add_input(pu1, 2.3) Tx1.add_output(pu2, 1.0) Tx1.add_output(pu3, 1.1) Tx1.sign(pr1) Tx2 = Transactions.Tx() Tx2.add_input(pu3, 2.3) Tx2.add_input(pu2, 1.0) Tx2.add_output(pu1, 3.1) Tx2.sign(pr2) Tx2.sign(pr3) B1 = TxBlock.TxBlock(None) B1.addTx(Tx1) B1.addTx(Tx2) sendBlock('localhost', B1) sendBlock('localhost', Tx2)
sendCoins(pu1, 0.1, pr1, pu2, 0.1) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) sendCoins(pu1, 0.1, pr1, pu3, 0.03) time.sleep(120) # Load (Save) all blocks TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") head_blocks = TxBlock.loadBlocks("AlBlocks.dat") # Gather Balance new1 = getbalance(pu1) new2 = getbalance(pu1) new3 = getbalance(pu1) # Verify Balances if abs(new1 - bal1 + 2.0) > 0.00000001: print("Error! Wrong balance for pu1") else: print('Success. Balance is correct for pu1') if abs(new2 - bal2 - 1.0) > 0.00000001: print("Error! Wrong balance for pu2")
def getBalance(pu_key): long_chain = TxBlock.findLongestBlockchain(head_blocks) return TxBlock.getBalance(pu_key, long_chain)
def nonceFinder(wallet_list, miner_public): global break_now global head_blocks # Load head_blocks try: head_blocks = TxBlock.loadBlocks("AllBlocks.dat") except: # MINER: No previous blocks found. Starting fresh. head_blocks = TxBlock.loadBlocks("GenesisBlock.dat") # Collect transactions and add to new block while not break_now: newBlock = TxBlock.TxBlock(TxBlock.findLongestBlockchain(head_blocks)) # Placeholder transaction, that stores an output transaction. placeholder = Transaction.Tx() placeholder.add_output(miner_public, 25.0) newBlock.addTx(placeholder) #TODO Sort tx_list by tx fee per byte. In 10000 byte want to cram in as many transactions fees we can. for tx in tx_list: newBlock.addTx(tx) # Transaction is too big remove it. if not newBlock.check_size(): newBlock.removeTx(tx) break newBlock.removeTx(placeholder) # Compute and collect Miner's reward total_in, total_out = newBlock.count_totals() miner_reward = Transaction.Tx() miner_reward.add_output(miner_public, 25.0+total_in-total_out) newBlock.addTx(miner_reward) # Find the Nonce print("Finding Nonce...") newBlock.find_nonce(5000) if newBlock.valid_nonce(): print("Success! Good nonce found.") if not newBlock.previousBlock in head_blocks: break # Replace the previously longest head in the set of blocks head_blocks.remove(newBlock.previousBlock) head_blocks.append(newBlock) # Send block to everyone in the wallet_list. Wallet will package block. savePrev = newBlock.previousBlock newBlock.previousBlock = None for ip_addr, port in wallet_list: SocketUtils.sendObj(ip_addr, newBlock, port) newBlock.previousBlock = savePrev # Remove used transactions from tx_list for tx in newBlock.data: if tx != miner_reward: tx_list.remove(tx) # Save head-_blocks TxBlock.saveBlocks(head_blocks, "AllBlocks.dat") return True