def genesis_block(self): new_block = block.Block(reward=self.reward) if new_block.error == True: return False new_block.mine(self.target) self.chain.append(new_block) pp.add_data(self, 'blockchain.pickle') return True
def mine(self, prev_hash): self.height += 1 new_block = block.Block(reward=self.reward, previous_hash=prev_hash, height=self.height) new_block.mine(self.target) self.chain.insert(0, new_block) config.broadcast_to_friend(new_block, '/block/new') pp.add_data(self, 'blockchain.pickle') if len(self.chain) % 5 == 0: self.reCalcDiff() if (len(self.chain) % 10 == 0): self.reward /= 2
def del_from_pool(tx): #deserialiazed transaction utxo_set = pp.get_data('utxo.pickle') if utxo_set == False: print("Empty utxo pool!!") return new_pool = [] for elem in tx['inputs']: address = get_address(elem['ScriptSig']) for utxo in utxo_set: if utxo['address'] != address: new_pool.append(utxo) else: new_data = [] for unspent in utxo['unspent_outputs']: if unspent['tx_hash_big_endian'] != elem[ 'TXID'] and unspent['tx_output_n'] != elem['VOUT']: new_data.append(unspent) utxo['unspent_outputs'] = new_data new_pool.append(utxo) pp.add_data(new_pool, 'utxo.pickle')
def consensus(): nodes = pp.read_nodes_from_file("node") len_list = [] dicti = {} for x in nodes: dicti = {} dicti["ip"] = x try: dicti["length"] = int(requests.get("http://" + x + "/chain/length").text) except: dicti["length"] = 0 len_list.append(dicti) newlist = sorted(len_list, key=lambda k: k['length']) if (newlist[-1]['ip'] != nodes[0]): req = requests.get("http://" + newlist[-1]['ip'] + "/chain") chain = req.text req = requests.get("http://" + newlist[-1]['ip'] + "/utxo") utxo = req.text pp.add_data(chain, 'blockchain.pickle') pp.add_data(utxo, 'utxo.pickle')
def add_output(tx_output, n, tx_hash): ##### new_data = { 'tx_hash_big_endian': tx_hash, 'tx_output_n': n, 'script': tx_output['SPK'], 'value': tx_output['Value'] } ready_data = { 'address': tx_output['address'], 'unspent_outputs': [{ 'tx_hash_big_endian': tx_hash, 'tx_output_n': n, 'script': tx_output['SPK'], 'value': tx_output['Value'] }] } ##### i = 0 utxo_set = pp.get_data('utxo.pickle') print("Utxo in add_output ", utxo_set) if type(utxo_set) is bytes: utx = [] for utxo in utxo_set: if type(utxo) is not int: utx.append(utxo) utxo_set = utx if utxo_set == False: utxo_set = [] else: for elem in utxo_set: if (elem['address'] == tx_output['address']): i = 1 elem['unspent_outputs'].append(new_data) if i == 0: utxo_set.append(ready_data) pp.add_data(utxo_set, 'utxo.pickle')
def add_output(tx_output, n, tx_hash): ##### new_data = { 'tx_hash_big_endian': tx_hash, 'tx_output_n': n, 'script': tx_output['SPK'], 'value': tx_output['Value'] } ready_data = { 'address': tx_output['address'], 'unspent_outputs': [{ 'tx_hash_big_endian': tx_hash, 'tx_output_n': n, 'script': tx_output['SPK'], 'value': tx_output['Value'] }] } ##### i = 0 utxo_set = pp.get_data('utxo.pickle') # if type(utxo_set) is str: # utxo_set = literal_eval(utxo_set) if utxo_set == False: utxo_set = [] else: # lens = len(utxo_set) ЗАЧЕМ???? for elem in utxo_set: if (elem['address'] == tx_output['address']): i = 1 elem['unspent_outputs'].append(new_data) if i == 0: utxo_set.append(ready_data) pp.add_data(utxo_set, 'utxo.pickle')
def genesis_block(self): new_block = block.Block(reward=self.reward) new_block.mine(self.target) self.chain.append(new_block) pp.add_data(self, 'blockchain.pickle')