def nonce_finder(wallet_list, miner_public): global break_now #add txs to new block while not break_now: NewBlock = txblock.TxBlock(txblock.find_longest_chain(head_blocks)) for tx in tx_list: NewBlock.add_tx(tx) #mining reward total_in, total_out = NewBlock.count_totals() mine_reward = transaction.Tx() mine_reward.add_output(miner_public, 25.0 + total_in - total_out) NewBlock.add_tx(mine_reward) #find nonce print('finding nonce...') NewBlock.find_nonce(10000) if NewBlock.good_nonce(): print('good nonce found') head_blocks.remove(NewBlock.previous_block) head_blocks.append(NewBlock) #send new block save_prev = NewBlock.previous_block NewBlock.previous_block = None print(type(wallet_list)) for addr, port in wallet_list: print('sending to ' + addr + ':' + str(port)) socketutils.send_obj(addr, NewBlock, 5006) NewBlock.previous_block = save_prev #remove used txs from tx list for tx in NewBlock.data: if tx != mine_reward: tx_list.remove(tx) txblock.save_blocks(head_blocks, 'all_blocks.dat') return True
def attachMiners(self): """Creates Miner objects (including calculating power) and attaches them to graph nodes, then populates miner adjacencies. """ top_powers = [] if self.settings.top_miner_power: num_other_miners = len(self.graph.nodes) - len( self.settings.top_miner_power) other_miners_power = 100.0 - sum(self.settings.top_miner_power) assert num_other_miners > 0 and other_miners_power > 0 power_to_percent_ratio = float( num_other_miners) / other_miners_power top_powers = [ percent * power_to_percent_ratio for percent in self.settings.top_miner_power ] genesis_tx = transaction.Tx(-1, None, 0, []) self.all_tx.append(genesis_tx) for node_index in self.graph.nodes: if node_index > len(top_powers) - 1: power = self.settings.miner_power_distribution.sample() else: power = top_powers[node_index] new_miner = self.protocol.getMinerClass()(node_index, genesis_tx, self.graph, self, power) self.graph.nodes[node_index]['miner'] = new_miner for node_index in self.graph.nodes: edges = self.graph[node_index] self.graph.nodes[node_index]['miner'].adjacencies = { edge_index: (self.graph.nodes[edge_index]['miner'], edges[edge_index]['network_delay']) for edge_index in edges }
def makeTx(self): """Makes a new transaction, connects it to the chain, and returns it. Returns: Tx -- Newly created transaction. """ new_tx = transaction.Tx(self.simulation.tick, self.id, self.id_bag.getNextId(), []) self.simulation.all_tx.append(new_tx) return new_tx
def makeTx(self): """Makes a new transaction, connects it to the chain, and returns it. Returns: Tx -- Newly created transaction. """ sorted_fronts = sorted(self.frontier_nodes, reverse=True, key=lambda n: n.depth) parent_choices = [n for n in sorted_fronts if n.depth == sorted_fronts[0].depth] # Only consider the deepest frontier nodes. parent = random.choice(parent_choices) new_tx = transaction.Tx(self.simulation.tick, self.id, self.id_bag.getNextId(), [parent.tx.hash]) self.sheep_tx.add(new_tx) self.simulation.all_tx.append(new_tx) return new_tx
def makeTx(self): """Makes a new transaction, connects it to the chain, and returns it. Returns: Tx -- Newly created transaction. """ pointers = [] parents = self.getNewParents() assert parents # Should always have at least one (genesis tx). for parent in parents: parent_hash = parent.hash assert parent_hash in self.chain_pointers pointers.append(parent_hash) new_tx = transaction.Tx(self.simulation.tick, self.id, self.id_bag.getNextId(), pointers) self.sheep_tx.add(new_tx) self.simulation.all_tx.append(new_tx) return new_tx
import time my_pr, my_pu = signatures.generate_keys() t1 = threading.Thread(target=miner_server, args=(('localhost', 5005), )) t2 = threading.Thread(target=nonce_finder, args=(wallet_list, my_pu)) server = socketutils.new_server_connection('localhost', 5006) t1.start() t2.start() pr1, pu1 = signatures.generate_keys() pr2, pu2 = signatures.generate_keys() pr3, pu3 = signatures.generate_keys() Tx1 = transaction.Tx() Tx2 = transaction.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_required(pu1) Tx1.sign(pr1) Tx1.sign(pr2) Tx2.sign(pr3) Tx2.sign(pr1) new_tx_list = [Tx1, Tx2]