def pending_transaction(): if request.method == 'GET': data = pending_pool.get_data('pool.pickle') if data != False: return str(data) else: return ("Something get wrong!\nYou have no transaction in pool")
def get_script(txid, vout): utxo_set = pp.get_data('utxo.pickle') for utxo in utxo_set: for unspent in utxo['unspent_outputs']: if unspent['tx_hash_big_endian'] == txid and hex( unspent['tx_output_n'])[2:] == vout: return unspent['script']
def utxo(): if request.method == 'GET': data = pending_pool.get_data('utxo.pickle') if data != False: return str(data) else: return ("Something get wrong!\nYou have no utxo in pool")
def get_difficulty(): if request.method == 'GET': data = pending_pool.get_data('blockchain.pickle') if data != False: diff = data.diff return str(diff) else: return ("Something get wrong!\nYou have no chain")
def get_chain(): if request.method == 'GET': data = pending_pool.get_data('blockchain.pickle') if data != False: if (type(data) == str): data = json.loads(data) return str(data) else: return ("Something get wrong!\nYou have no chain")
def last_block(): if request.method == 'GET': data = pending_pool.get_data('blockchain.pickle') if data != False: if type(data) is dict: block = data['chain'][0] else: block = data.chain[0] return str(block) else: return ("Something get wrong!\nYou have no chain")
def chain_length(): if request.method == 'GET': data = pending_pool.get_data('blockchain.pickle') if data != False: if type(data) is dict: lens = str(data['chain'][0]['height']) else: lens = str(data.chain[0].height) return lens else: return ("0")
def init_file(self, name, t): data = pp.get_data(name) if data == False: data = [] height = 0 else: data = json.loads(data) if t != 0: height = data.chain[0].height if t == 1: return data, height else: return data
def block_height(): if request.method == 'GET': height = str(request.args.get('height')) data = pending_pool.get_data('blockchain.pickle') if data != False: i = len(data.chain) - 1 while (i >= 0): if (int(data.chain[i].height) == int(height)): return str(data.chain[i]) i -= 1 return "No such block!" else: return "There no chain data!"
def __init__(self, is_premine=0): cmd.Cmd.__init__(self) self.prompt = "฿ " self.intro = "\t\tWelcome to the miner cli\nHow to use? 'help'!!" self.doc_header = "For detail information use 'help _command_')" self.blockchain = pp.get_data("blockchain.pickle") if (self.blockchain == False or is_premine == True): os.remove("utxo.pickle") self.blockchain = blockchain.Blockchain() if is_premine == True: print("PREMINE IS ON") pubkeys = premine.createKeysAndAddresses() self.blockchain.genesis_block() if is_premine == True: premine.premine_mode(pubkeys, self)
def get_utxo_set(which, address): if which == 'Testnet': resp = requests.get( 'https://testnet.blockchain.info/unspent?active=%s' % address) utxo_set = json.load(resp.text)["unspent_outputs"] else: i = 0 utxo_set = pp.get_data('utxo.pickle') if utxo_set != False: for elem in utxo_set: if (elem['address'] == address): utxo_set = elem['unspent_outputs'] i = 1 break if i != 1: utxo_set = 0 return utxo_set
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 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')