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 sendCoins(pu_send, amt_send, pr_send, pu_recv, amt_recv, miner_list): newTx = Transactions.Tx() newTx.add_input(pu_send, amt_send) newTx.add_output(pu_recv, amt_recv) newTx.sign(pr_send) SocketUtils.sendObj('localhost', newTx) 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 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 sendCoins(pu_send, amt_send, pr_send, pu_recv, amt_recv): global tx_index newTx = Transactions.Tx() if not pu_send in tx_index: tx_index[pu_send] = 0 newTx.add_input(pu_send, amt_send, tx_index[pu_send]) newTx.add_output(pu_recv, amt_recv) newTx.sign(pr_send) for ip, port in miners: SocketUtils.sendObj(ip, newTx, port) tx_index[pu_send] = tx_index[pu_send] + 1 return True
if __name__ == "__main__": import Signatures import threading import time my_pr, my_pu = Signatures.loadKeys("private.key", "public.key") t1 = threading.Thread(target=minerServer, args=(('localhost', 5005), )) t2 = threading.Thread(target=nonceFinder, args=(wallets, my_pu)) server = SocketUtils.newServerConnection('localhost', 5006) t1.start() t2.start() pr1, pu1 = Signatures.generate_keys() pr2, pu2 = Signatures.generate_keys() pr3, pu3 = Signatures.generate_keys() Tx1 = Transactions.Tx() Tx2 = Transactions.Tx() Tx1.add_input(pu1, 4.0) Tx1.add_input(pu2, 1.0) Tx1.add_output(pu3, 4.8) Tx2.add_input(pu3, 4.0) Tx2.add_output(pu2, 4.0) Tx2.add_reqd(pu1) Tx1.sign(pr1) Tx1.sign(pr2) Tx2.sign(pr3) Tx2.sign(pr1) new_tx_list = [Tx1, Tx2]