def info(dic): state=state_library.current_state() chain=blockchain.load_chain() if dic['type']=='blockCount': if len(chain)>0: return package({'length':state['length'], 'recent_hash':state['recent_hash']}) else: return package({'length':0, 'recent_hash':0}) if dic['type']=='rangeRequest': ran=dic['range'] if ran[0]==0: ran[0]=1 print('ran: ' +str(ran)) if len(chain)>=int(ran[1]): print('$$$$$$$$$$dic: ' +str(dic)) return package(chain[ran[0]:ran[1]+1]) else: return package({'error':'oops'}) if dic['type']=='transactions': return package(blockchain.load_transactions()) if dic['type']=='backup_states': backups=state_library.fs_load(state_library.backup_db,{}) out={} for i in backups.keys(): find_biggest=0 if int(i)<int(dic['start']) and int(i)>int(find_biggest): find_biggest=int(i) return package(backups[str(find_biggest)]) if dic['type']=='pushtx': blockchain.add_transaction(dic['tx']) if dic['type']=='pushblock': blockchain.chain_push(dic['block'])
def move(game_name, location, pubkey, privkey): state=state_library.current_state() board=state[game_name] print('location: ' +str(location)) txs=blockchain.load_transactions() game_txs=filter(lambda x: x['game_name']==game_name, txs) tx_orig={'type':'nextTurn', 'id':pubkey, 'game_name':game_name, 'where':location, 'move_number':board['move_number']+len(game_txs)} easy_add_transaction(tx_orig, go.nextturn_sig_list, privkey)
def clean_state(): transactions=blockchain.load_transactions() state=state_library.current_state() a=blockchain.verify_transactions(transactions, state) if a['bool']: return a['newstate'] else: print('EEROR###33####3') time.sleep(1)
def clean_state(): transactions = blockchain.load_transactions() state = state_library.current_state() a = blockchain.verify_transactions(transactions, state) print('a: ' + str(a)) print('transactions: ' + str(transactions)) try: return a['newstate'] except: blockchain.reset_transactions()
def clean_state(): transactions=blockchain.load_transactions() state=state_library.current_state() a=blockchain.verify_transactions(transactions, state) print('a: ' +str(a)) print('transactions: ' +str(transactions)) try: return a['newstate'] except: blockchain.reset_transactions()
def move(game_name, location, pubkey, privkey): state = state_library.current_state() board = state[game_name] print ("location: " + str(location)) txs = blockchain.load_transactions() game_txs = filter(lambda x: x["game_name"] == game_name, txs) tx_orig = { "type": "nextTurn", "id": pubkey, "game_name": game_name, "where": location, "move_number": board["move_number"] + len(game_txs), } easy_add_transaction(tx_orig, go.nextturn_sig_list, privkey)
def info(dic): state = state_library.current_state() chain = blockchain.load_chain() if 'version' not in dic or dic['version'] != 4: return package({'error': 'wrong version'}) else: dic.pop('version') if dic['type'] == 'blockCount': if len(chain) > 0: return package({ 'length': state['length'], 'recent_hash': state['recent_hash'] }) else: return package({'length': 0, 'recent_hash': 0}) if dic['type'] == 'rangeRequest': ran = dic['range'] if ran[0] == 0: ran[0] = 1 print('ran: ' + str(ran)) if len(chain) >= int(ran[1]): print('$$$$$$$$$$dic: ' + str(dic)) return package(chain[ran[0]:ran[1] + 1]) else: return package({'error': 'oops'}) if dic['type'] == 'transactions': return package(blockchain.load_transactions()) if dic['type'] == 'backup_states': backups = state_library.fs_load(state_library.backup_db, []) for i in range(len(backups)): find_biggest = 0 if int(backups[i]['length']) < int(dic['start']) and int( backups[i]['length']) > int(find_biggest): find_biggest = int(i) return package(backups[find_biggest]) if dic['type'] == 'pushtx': #blockchain.add_transaction(dic['tx']) #append this transaction to the list of suggested transactions. blockchain.push_appendDB('suggested_transactions.db', dic['tx']) if dic['type'] == 'pushblock': #blockchain.chain_push(dic['block']) #append this block to the list of suggested blocks. blockchain.push_appendDB('suggested_blocks.db', dic['block'])
def easy_add_transaction(tx_orig, sign_over, privkey): state = state_library.current_state() pubkey = pt.privtopub(privkey) if pubkey not in state or "count" not in state[pubkey]: my_count = 1 else: my_count = state[pubkey]["count"] txs = blockchain.load_transactions() my_txs = filter(lambda x: x["id"] == pubkey, txs) tx = copy.deepcopy(tx_orig) tx["count"] = len(my_txs) + my_count tx["signature"] = pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) print (blockchain.add_transaction(tx)) if "move_number" in tx: for i in range(10): tx["move_number"] += 1 tx["signature"] = pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) print (blockchain.add_transaction(tx)) print ("tx: " + str(tx)) blockchain.pushtx(tx, quick_mine.peers_list)
def easy_add_transaction(tx_orig, sign_over, privkey): state=state_library.current_state() pubkey=pt.privtopub(privkey) if pubkey not in state or 'count' not in state[pubkey]: my_count=1 else: my_count=state[pubkey]['count'] txs=blockchain.load_transactions() my_txs=filter(lambda x: x['id']==pubkey, txs) tx=copy.deepcopy(tx_orig) tx['count']=len(my_txs)+my_count tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) print(blockchain.add_transaction(tx)) if 'move_number' in tx: for i in range(10): tx['move_number']+=1 tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) print(blockchain.add_transaction(tx)) print('tx: ' +str(tx)) blockchain.pushtx(tx, config.peers_list)
def info(dic): state=state_library.current_state() chain=blockchain.load_chain() if dic['type']=='blockCount': if len(chain)>0: return package({'length':state['length'], 'recent_hash':state['recent_hash']}) else: return package({'length':0, 'recent_hash':0}) if dic['type']=='rangeRequest': ran=dic['range'] if ran[0]==0: ran[0]=1 print('ran: ' +str(ran)) if len(chain)>=int(ran[1]): print('$$$$$$$$$$dic: ' +str(dic)) return package(chain[ran[0]:ran[1]+1]) else: return package({'error':'oops'}) if dic['type']=='transactions': return package(blockchain.load_transactions())
def info(dic): state=state_library.current_state() chain=blockchain.load_chain() if 'version' not in dic or dic['version']!=4: return package({'error':'wrong version'}) else: dic.pop('version') if dic['type']=='blockCount': if len(chain)>0: return package({'length':state['length'], 'recent_hash':state['recent_hash']}) else: return package({'length':0, 'recent_hash':0}) if dic['type']=='rangeRequest': ran=dic['range'] if ran[0]==0: ran[0]=1 print('ran: ' +str(ran)) if len(chain)>=int(ran[1]): print('$$$$$$$$$$dic: ' +str(dic)) return package(chain[ran[0]:ran[1]+1]) else: return package({'error':'oops'}) if dic['type']=='transactions': return package(blockchain.load_transactions()) if dic['type']=='backup_states': backups=state_library.fs_load(state_library.backup_db,[]) for i in range(len(backups)): find_biggest=0 if int(backups[i]['length'])<int(dic['start']) and int(backups[i]['length'])>int(find_biggest): find_biggest=int(i) return package(backups[find_biggest]) if dic['type']=='pushtx': #blockchain.add_transaction(dic['tx']) #append this transaction to the list of suggested transactions. blockchain.push_appendDB('suggested_transactions.db', dic['tx']) if dic['type']=='pushblock': #blockchain.chain_push(dic['block']) #append this block to the list of suggested blocks. blockchain.push_appendDB('suggested_blocks.db', dic['block'])
def easy_add_transaction(tx_orig, sign_over, privkey, state): tx=copy.deepcopy(tx_orig) pubkey=pt.privtopub(privkey) if pubkey not in state or 'count' not in state[pubkey]: tx['count']=1 else: tx['count']=state[pubkey]['count'] txs=blockchain.load_transactions() my_txs=filter(lambda x: x['id']==pubkey, txs) tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) if blockchain.add_transaction(tx): blockchain.pushtx(tx, config.peers_list) return True if 'move_number' in tx: for i in range(10): tx['move_number']+=1 tx['signature']=pt.ecdsa_sign(blockchain.message2signObject(tx, sign_over), privkey) if blockchain.add_transaction(tx): blockchain.pushtx(tx, config.peers_list) return True print('SOMETHING IS BROKEN') return False
def easy_add_transaction(tx_orig, sign_over, privkey, state): tx = copy.deepcopy(tx_orig) pubkey = pt.privtopub(privkey) if pubkey not in state or 'count' not in state[pubkey]: tx['count'] = 1 else: tx['count'] = state[pubkey]['count'] txs = blockchain.load_transactions() my_txs = filter(lambda x: x['id'] == pubkey, txs) tx['signature'] = pt.ecdsa_sign(go.message2signObject(tx, sign_over), privkey) if blockchain.add_transaction(tx): blockchain.pushtx(tx, config.peers_list) return True if 'move_number' in tx: for i in range(10): tx['move_number'] += 1 tx['signature'] = pt.ecdsa_sign( go.message2signObject(tx, sign_over), privkey) if blockchain.add_transaction(tx): blockchain.pushtx(tx, config.peers_list) return True print('SOMETHING IS BROKEN') return False
def home(dic): print(dic) if 'BrainWallet' in dic: dic['privkey']=pt.sha256(dic['BrainWallet']) privkey=dic['privkey'] print('priv: ' +str(dic['privkey'])) pubkey=pt.privtopub(dic['privkey']) if 'do' in dic.keys(): if dic['do']=='newGame': try: a=newgame(dic['partner'], dic['game'], pubkey, privkey, int(dic['size']), dic['amount']) except: a=newgame(dic['partner'], dic['game'], pubkey, privkey, 19, dic['amount']) active_games.append(dic['game']) if dic['do']=='winGame': wingame(dic['game'], pubkey, privkey) if dic['do']=='joinGame': active_games.append(dic['game']) if dic['do']=='deleteGame': active_games.remove(dic['game']) if 'move' in dic.keys(): string=dic['move'].split(',') i=int(string[0]) j=int(string[1]) move(dic['game'], [i, j], pubkey, privkey) fs=fs_load() out=empty_page out=out.format('<p>your address is: ' +str(pubkey)+'</p>{}') state=state_library.current_state() out=out.format('<p>current block is: ' +str(state['length'])+'</p>{}') transactions=blockchain.load_transactions() a=blockchain.verify_transactions(transactions, state) if a['bool']: state=a['newstate'] else: pass print(a) print(transactions) print('ERROR') if pubkey not in state: state[pubkey]={'amount':0} if 'amount' not in state[pubkey]: state[pubkey]['amount']=0 out=out.format('<p>current balance is: ' +str(state[pubkey]['amount']/100000.0)+'</p>{}') for game in active_games: out=out.format("<h1>"+str(game)+"</h1>{}") if game in state: out=out.format('<h1>Timer: ' + str(state[game]['last_move_time']+state[game]['time']-state['length'])+' </h1>{}') if game in state.keys(): in_last_block=state[game] out=board(out, state, game, privkey) out=out.format(easyForm('/home', 'win this game', ''' <input type="hidden" name="do" value="winGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">'''.format(privkey, game))) out=out.format(easyForm('/home', 'leave this game', ''' <input type="hidden" name="do" value="deleteGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">'''.format(privkey, game))) else: out=out.format("<p>this game does not yet exist</p>{}") out=out.format(easyForm('/home', 'delete this game', ''' <input type="hidden" name="do" value="deleteGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">'''.format(privkey,game))) out=out.format(easyForm('/home', 'Refresh boards', ''' <input type="hidden" name="privkey" value="{}"> '''.format(privkey))) out=out.format(easyForm('/home', 'Join Game', ''' <input type="hidden" name="do" value="joinGame"> <input type="hidden" name="privkey" value="{}"> <input type="text" name="game" value="unique game name"> '''.format(privkey))) out=out.format(easyForm('/home', 'New Game', ''' <input type="hidden" name="do" value="newGame"> <input type="hidden" name="privkey" value="{}"> <input type="text" name="game" value="unique game name"> <input type="text" name="partner" value="put your partners address here."> <input type="text" name="size" value="board size (9, 13, 19 are popular)"> <input type="text" name="amount" value="0"> '''.format(privkey))) return out
def clean_state(): transactions=blockchain.load_transactions() state=state_library.current_state() return blockchain.verify_transactions(transactions, state)['newstate']
def clean_state(): transactions=blockchain.load_transactions() state=state_library.current_state() a=blockchain.verify_transactions(transactions, state) print('a: ' +str(a)) return a['newstate']
def clean_state(): transactions = blockchain.load_transactions() state = state_library.current_state() return blockchain.verify_transactions(transactions, state)['newstate']
def home(dic): print (dic) if "BrainWallet" in dic: dic["privkey"] = pt.sha256(dic["BrainWallet"]) privkey = dic["privkey"] print ("priv: " + str(dic["privkey"])) pubkey = pt.privtopub(dic["privkey"]) if "do" in dic.keys(): if dic["do"] == "newGame": try: a = newgame(dic["partner"], dic["game"], pubkey, privkey, int(dic["size"]), dic["amount"]) except: a = newgame(dic["partner"], dic["game"], pubkey, privkey, 19, dic["amount"]) active_games.append(dic["game"]) if dic["do"] == "winGame": wingame(dic["game"], pubkey, privkey) if dic["do"] == "joinGame": active_games.append(dic["game"]) if dic["do"] == "deleteGame": active_games.remove(dic["game"]) if "move" in dic.keys(): string = dic["move"].split(",") i = int(string[0]) j = int(string[1]) move(dic["game"], [i, j], pubkey, privkey) fs = fs_load() out = empty_page out = out.format("<p>your address is: " + str(pubkey) + "</p>{}") state = state_library.current_state() out = out.format("<p>current block is: " + str(state["length"]) + "</p>{}") transactions = blockchain.load_transactions() a = blockchain.verify_transactions(transactions, state) if a["bool"]: state = a["newstate"] else: pass print (a) print (transactions) print ("ERROR") if pubkey not in state: state[pubkey] = {"amount": 0} if "amount" not in state[pubkey]: state[pubkey]["amount"] = 0 out = out.format("<p>current balance is: " + str(state[pubkey]["amount"] / 100000.0) + "</p>{}") for game in active_games: out = out.format("<h1>" + str(game) + "</h1>{}") if game in state: out = out.format( "<h1>Timer: " + str(state[game]["last_move_time"] + state[game]["time"] - state["length"]) + " </h1>{}" ) if game in state.keys(): in_last_block = state[game] out = board(out, state, game, privkey) out = out.format( easyForm( "/home", "win this game", """ <input type="hidden" name="do" value="winGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">""".format( privkey, game ), ) ) out = out.format( easyForm( "/home", "leave this game", """ <input type="hidden" name="do" value="deleteGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">""".format( privkey, game ), ) ) else: out = out.format("<p>this game does not yet exist</p>{}") out = out.format( easyForm( "/home", "delete this game", """ <input type="hidden" name="do" value="deleteGame"> <input type="hidden" name="privkey" value="{}"> <input type="hidden" name="game" value="{}">""".format( privkey, game ), ) ) out = out.format( easyForm( "/home", "Refresh boards", """ <input type="hidden" name="privkey" value="{}"> """.format( privkey ), ) ) out = out.format( easyForm( "/home", "Join Game", """ <input type="hidden" name="do" value="joinGame"> <input type="hidden" name="privkey" value="{}"> <input type="text" name="game" value="unique game name"> """.format( privkey ), ) ) out = out.format( easyForm( "/home", "New Game", """ <input type="hidden" name="do" value="newGame"> <input type="hidden" name="privkey" value="{}"> <input type="text" name="game" value="unique game name"> <input type="text" name="partner" value="put your partners address here."> <input type="text" name="size" value="board size (9, 13, 19 are popular)"> <input type="text" name="amount" value="0"> """.format( privkey ), ) ) return out