Esempio n. 1
0
def mine_1(reward_pubkey, peers, times, db_extention):
#    bitcoin_count=getblockcount()
    state=state_library.current_state(db_extention)
    diff=difficulty(state['length']+1)
    sha={'hash':'f'*64}
    print('start mining ' +str(times)+ ' times')
#    bitcoin_hash=getblockhash(bitcoin_count)
    transactions=load_transactions(db_extention)
    extra=0
    for tx in transactions:
        if tx['id']==reward_pubkey:
            extra+=1
    if reward_pubkey not in state:
        state[reward_pubkey]={'count':1, 'amount':0}
    if 'count' not in state[reward_pubkey]:
        state[reward_pubkey]['count']=1
    count=state[reward_pubkey]['count']+extra
    transactions.append({'type':'mint', 'amount':10**5, 'id':reward_pubkey, 'count':count})
    length=state['length']
    nonce=0
    hash_count=0
#    blocks=easy_threading(mine_2, (length, diff, nonce, state, transactions))
    blocks=[mine_2(length, diff, nonce, state, transactions)]
    print('blocks:' + str(blocks))
    blocks=filter(lambda x: x!=False, blocks)
    if len(blocks)>0:
        print('blocks:' + str(blocks))
        for block in blocks:
            chain_push(block)
Esempio n. 2
0
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'])
Esempio n. 3
0
def mine_1(reward_pubkey, peers, times):
    sha={'hash':100}
    diff=0
    hash_count=0
    print('start mining ' +str(times)+ ' times')
    while sha['hash']>diff:
#        print(str(hash_count))
        if hash_count>=times:
#            time.sleep(2)#otherwise you send requests WAY TOO FAST and the networking miners shutdown.
            print('was unable to find blocks')
            return False
        state=state_library.current_state(reward_pubkey)
        bitcoin_count=getblockcount()
        bitcoin_hash=getblockhash(bitcoin_count)
        diff=difficulty(bitcoin_count, state['length']+1)
        nonce=random.randint(0,10000000000000000)
#        time.sleep(0.01)
        transactions=load_transactions()
        extra=0
        for tx in transactions:
            if tx['id']==reward_pubkey:
                extra+=1
        if 'count' not in state[reward_pubkey]:
            state[reward_pubkey]['count']=1
        count=state[reward_pubkey]['count']+extra
        transactions.append({'type':'mint', 'amount':10**5, 'id':reward_pubkey, 'count':count})
        length=state['length']
        sha=blockhash(length, nonce, state, transactions, bitcoin_hash)
        hash_count+=1
#    block={'nonce':nonce, 'length':length, 'sha':sha['hash'], 'transactions':transactions, 'bitcoin_hash':bitcoin_hash, 'bitcoin_count':bitcoin_count, 'exact':sha['exact'], 'prev_sha':state['recent_hash']}
    block={'nonce':nonce, 'length':length, 'sha':sha['hash'], 'transactions':transactions, 'bitcoin_hash':bitcoin_hash, 'bitcoin_count':bitcoin_count, 'prev_sha':state['recent_hash']}
    print('new link: ' +str(block))
    chain_push(block)
Esempio n. 4
0
def mine_1(reward_pubkey, peers):
    sha={'hash':100}
    diff=0
    hashes_limit=1+config.hashes_till_check
    hash_count=0
    print('start mining ' +str(hashes_limit)+ ' times')
    while sha['hash']>diff:
#        print(str(hash_count))
        if hash_count>=config.hashes_till_check:
            print('was unable to find blocks')
            return False
        state=state_library.current_state(reward_pubkey)
        bitcoin_count=getblockcount()
        bitcoin_hash=getblockhash(bitcoin_count)
        diff=difficulty(bitcoin_count, state['length']+1)
        nonce=random.randint(0,10000000000000000)
        time.sleep(0.01)
        transactions=load_transactions()
        extra=0
        for tx in transactions:
            if tx['id']==reward_pubkey:
                extra+=1
        count=state[reward_pubkey]['count']+extra
        transactions.append({'type':'mint', 'amount':10**5, 'id':reward_pubkey, 'count':count})
        length=state['length']
        sha=blockhash(length, nonce, state, transactions, bitcoin_hash)
        hash_count+=1
#    block={'nonce':nonce, 'length':length, 'sha':sha['hash'], 'transactions':transactions, 'bitcoin_hash':bitcoin_hash, 'bitcoin_count':bitcoin_count, 'exact':sha['exact'], 'prev_sha':state['recent_hash']}
    block={'nonce':nonce, 'length':length, 'sha':sha['hash'], 'transactions':transactions, 'bitcoin_hash':bitcoin_hash, 'bitcoin_count':bitcoin_count, 'prev_sha':state['recent_hash']}
    print('new link: ' +str(block))
    chain_push(block)
Esempio n. 5
0
def add_transaction(tx):#to local pool
    transactions=load_transactions()
    state=state_library.current_state()
    if verify_transactions(transactions+[tx], state)['bool']:
        push_appendDB(transactions_database, tx)
        return True
    return False
Esempio n. 6
0
File: gui.py Progetto: Aeium/go-coin
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)
Esempio n. 7
0
def add_transaction(tx, db_ex=''):#to local pool
    if tx['type']=='mint':
        return False
    transactions=load_transactions(db_ex)
    state=state_library.current_state(db_ex)
    if verify_transactions(transactions+[tx], state)['bool']:
        push_appendDB(ex(db_ex, transactions_database), tx)
        return True
    return False
Esempio n. 8
0
 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)
Esempio n. 9
0
 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()
Esempio n. 10
0
def new_game(tx):
    print('tx: ' + str(tx))
    state = state_library.current_state()
    tx['last_move_time'] = state['length']
    tx.pop('signature')
    tx.pop('id')
    tx.pop('count')
    tx.pop('type')
    tx['move_number'] = 1
    return tx
Esempio n. 11
0
File: go.py Progetto: Aeium/go-coin
def new_game(tx):
    print('tx: ' +str(tx))
    state=state_library.current_state()
    tx['last_move_time']=state['length']
    tx.pop('signature')
    tx.pop('id')
    tx.pop('count')
    tx.pop('type')
    tx['move_number']=1
    return tx
Esempio n. 12
0
 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()
Esempio n. 13
0
def peer_check(peer):
    print('checking peer')
    state=state_library.current_state()
    cmd=(lambda x: send_command(peer, x))
    block_count=cmd({'type':'blockCount'})
    print('block count: ' +str(block_count))
    if type(block_count)!=type({'a':1}):
        return []
    if 'error' in block_count.keys():
        return []        
    print('state: ' +str(state))
    ahead=int(block_count['length'])-int(state['length'])
    if ahead < 0:
        print('WE ARE AHEAD OF THEM')
        chain=copy.deepcopy(load_chain())
        pushblock(chain[int(block_count['length'])+1],[peer])
        return []
    if ahead == 0:#if we are on the same block, ask for any new txs
        print('ON SAME BLOCK')
        if state['recent_hash']!=block_count['recent_hash']:
            chain_unpush()
            print('WE WERE ON A FORK. time to back up.')
            return []
        my_txs=load_transactions()
        txs=cmd({'type':'transactions'})
        add_transactions(txs)
        pushers=set_minus(my_txs, txs, ['count', 'id'])
        for push in pushers:
            pushtx(push, [peer])
        return []
    if ahead>1300:
        try_state=cmd({'type':'backup_states',
                   'start': block_count['length']-1000})
        if type(try_state)==type({'a':'1'}):
            print('state: ' +str(state))
            state=try_state
            state_library.save_state(state)
        return []
    print("############################## ahead: "+str(ahead))
    start=int(state['length'])-20
    if start<0:
        start=0
    if ahead>500:
        end=int(state['length'])+499
    else:
        end=block_count['length']
    blocks= cmd({'type':'rangeRequest', 
                 'range':[start, end]})
    if type(blocks)!=type([1,2]):
        return []
    times=1
    while fork_check(blocks, state) and times>0:
        times-=1
        chain_unpush()
    return blocks
Esempio n. 14
0
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)
Esempio n. 15
0
def chain_unpush(db_ex=''):
    chain=load_chain(db_ex)
    orphaned_txs=[]
    txs=load_transactions(db_ex)
    state=state_library.current_state(db_ex)
    length=state['length']
    state=state_library.recent_backup(db_ex)
    for i in range(length-state['length']):
        try:
            orphaned_txs+=chain[-1-i]['transactions']
        except:
            pass
    shorten_chain_db(state['length'], db_ex)
    state_library.save_state(state, db_ex)
    reset_transactions(db_ex)
    add_transactions(orphaned_txs, db_ex)
    add_transactions(txs, db_ex)
Esempio n. 16
0
def chain_unpush():
    chain=load_chain()
    orphaned_txs=[]
    txs=load_transactions()
    state=state_library.current_state()
    length=state['length']
    state=state_library.recent_backup()
    for i in range(length-state['length']):
        orphaned_txs+=chain[-1-i]['transactions']
#    chain=chain[:-1]
    #reset_chain() instead, just back up to the nearest save.
    shorten_chain_db(state['length'])
    state_library.save_state(state)
    reset_transactions()
#    for i in chain[-100:]:
#        chain_push(i)
    add_transactions(orphaned_txs)
    add_transactions(txs)
Esempio n. 17
0
def chain_push(block):
    statee=state_library.current_state()    
    print('CHAIN PUSH')
    if new_block_check(block, statee):
        print('PASSED TESTS')
        state=verify_transactions(block['transactions'], statee)
        state=state['newstate']
        state['length']+=1
        state['recent_hash']=block['sha']
        state_library.save_state(state)
        if block['length']%10==0 and block['length']>11:
            state_library.backup_state(state)
        txs=load_transactions()
        reset_transactions()
        add_transactions(txs)
        return push_appendDB(chain_db, block)
    else:
        print('FAILED TESTS')
        return 'bad'
Esempio n. 18
0
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'])
Esempio n. 19
0
File: gui.py Progetto: Aeium/go-coin
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)
Esempio n. 20
0
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)
Esempio n. 21
0
def chain_push(block, db_extention=''):
    statee=state_library.current_state(db_extention)    
    print('CHAIN PUSH')
    if new_block_check(block, statee):
#        print('PASSED TESTS')
        state=verify_transactions(block['transactions'], statee)
        state=state['newstate']
        state['length']+=1
        state['recent_hash']=block['sha']
        state_library.save_state(state, db_extention)
        if block['length']%10==0 and block['length']>11:
            state_library.backup_state(state, db_extention)
        txs=load_transactions(db_extention)
        reset_transactions(db_extention)
        add_transactions(txs, db_extention)
#        print('exiting Chain Push')
        return push_appendDB(ex(db_extention, chain_db), block)
    else:
        print('FAILED TESTS')
        return 'bad'
Esempio n. 22
0
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())
Esempio n. 23
0
def new_game(tx):
    print('tx: ' +str(tx))
    state=state_library.current_state()
    tx['last_move_time']=state['length']
    tx.pop('signature')
    tx.pop('id')
    tx.pop('count')
    tx.pop('type')
    if 'amount' not in tx:
        tx['amount']=0
    if 'time' not in tx:
        tx['time']=5
    if 'size' not in tx:
        tx['size']=19
    if 'white' not in tx:
        tx['white']=[]
    if 'black' not in tx:
        tx['black']=[]
    tx['move_number']=1
    return tx
Esempio n. 24
0
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'])
Esempio n. 25
0
 def clean_state():
     transactions = blockchain.load_transactions()
     state = state_library.current_state()
     return blockchain.verify_transactions(transactions, state)['newstate']
Esempio n. 26
0
 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']
Esempio n. 27
0
def peer_check(peer, db_ex):
    print('checking peer')
    state=state_library.current_state(db_ex)
    cmd=(lambda x: send_command(peer, x))
    block_count=cmd({'type':'blockCount'})
    print('block count: ' +str(block_count))
    if type(block_count)!=type({'a':1}):
        return []
    if 'error' in block_count.keys():
        return []        
#    print('state: ' +str(state))
    ahead=int(block_count['length'])-int(state['length'])
    if ahead < 0:
        chain=copy.deepcopy(load_chain(db_ex))
        print('len chain: ' +str(len(chain)))
        print('length: ' +str(int(block_count['length'])+1))
        print('state len: ' +str(state['length']))
        try:
            pushblock(chain[int(block_count['length'])+1],[peer])
        except:
            pass
        if db_ex=='_miner':
            probability(0.2, chain_unpush(db_ex))
        return []
    if ahead == 0:#if we are on the same block, ask for any new txs
        print('ON SAME BLOCK')
        if state['recent_hash']!=block_count['recent_hash']:
            chain_unpush(db_ex)
            print('WE WERE ON A FORK. time to back up.')
            return []
        my_txs=load_transactions(db_ex)
        txs=cmd({'type':'transactions'})
        add_transactions(txs, db_ex)
        pushers=set_minus(my_txs, txs, ['count', 'id'])
        for push in pushers:
            pushtx(push, [peer])
        return []
#    if ahead>1001:
#        try_state=cmd({'type':'backup_states',
#                   'start': block_count['length']-1000})
#        if type(try_state)==type({'a':'1'}) and 'error' not in state:
#            print('state: ' +str(state))
#            state=try_state
#            state_library.save_state(state)
#        return []
#    print("############################## ahead: "+str(ahead))
#    def f():
#        for i in range(5):
#            chain_unpush(db_ex)
#    probability(0.03, chain_unpush(db_ex))
    start=int(state['length'])-30
    if start<0:
        start=0
    if ahead>500:
        end=int(state['length'])+499
    else:
        end=block_count['length']
    blocks= cmd({'type':'rangeRequest', 
                 'range':[start, end]})
#    print('@@@@@@@@@@@@downloaded blocks')
    if type(blocks)!=type([1,2]):
        return []
    times=1
    while fork_check(blocks, state) and times>0:
        times-=1
        chain_unpush(db_ex)
    return blocks
Esempio n. 28
0
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
Esempio n. 29
0
 def clean_state():
     transactions=blockchain.load_transactions()
     state=state_library.current_state()
     return blockchain.verify_transactions(transactions, state)['newstate']
Esempio n. 30
0
File: gui.py Progetto: Aeium/go-coin
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