Esempio n. 1
0
def sign_verify(tx, txs, out,
                DB):  #check the validity of a transaction of type sign.
    a = tools.addr(tx)
    B = tx[
        'B']  #verify a proof that addr(tx) actually owned that much money long*2-medium ago.
    M = custom.all_money
    address = tools.addr(tx)
    block = tools.db_get(tx['on_block'])
    num = max(0, tx['on_block'] - (custom.long_time * 2 - custom.medium_time))
    election_block = tools.db_get(num)
    if not signature_check(tx):
        out[0] += 'signature check'
        return False
    if 'root_hash' not in election_block:
        out[0] += 'no root hash'
        return False
    v = tools.db_verify(election_block['root_hash'], address, tx['proof'])
    if v == False:
        tools.log('your address did not exist that long ago.')
        return False
    if v['amount'] != tx['B']:
        tools.log('that is not how much money you had that long ago')
        return False
    if 'secret_hash' not in tx:
        tools.log('need the hash of a secret')
        return False
    for t in txs:
        if tools.addr(t) == address and t['type'] == 'sign':
            #tools.log('can only have one sign tx per block')
            return False
    if len(tx['jackpots']) < 1:
        tools.log('insufficient jackpots')
        return False
    if not signature_check(tx):
        out[0] += 'signature check'
        return False
    length = tools.local_get('length')
    if int(tx['on_block']) != int(length + 1):
        out[0] += 'this tx is for the wrong block. have ' + str(
            length + 1) + ' need: ' + str(tx['on_block'])
        return False
    if tx['on_block'] > 0:
        if not tx['prev'] == tools.db_get(length)['block_hash']:
            tools.log('must give hash of previous block')
            return False
    ran = tools.det_random(tx['on_block'])
    for j in tx['jackpots']:
        if type(j) != int or j not in range(200):
            tools.log('bad jackpot')
            return False
        if len(filter(lambda x: x == j, tx['jackpots'])) != 1:
            tools.log('no repeated jackpots')
            return False
        if not tools.winner(B, M, ran, address, j):
            tools.log('that jackpot is not valid: ' + str(j))
            return False
    if tx['amount'] < custom.minimum_deposit:
        tools.log('you have to deposit more than that')
        return False
    return True
Esempio n. 2
0
def sign_verify(tx, txs, out, DB):#check the validity of a transaction of type sign.
    a=tools.addr(tx)
    B=tx['B']#verify a proof that addr(tx) actually owned that much money long*2-medium ago.
    M=custom.all_money
    address=tools.addr(tx)
    block=tools.db_get(tx['on_block'])
    num=max(0,tx['on_block']-(custom.long_time*2-custom.medium_time))
    election_block=tools.db_get(num)
    if not signature_check(tx):
        out[0]+='signature check'
        return False
    if 'root_hash' not in election_block:
        out[0]+='no root hash'
        return False
    v=tools.db_verify(election_block['root_hash'], address, tx['proof'])
    if v==False:
        tools.log('your address did not exist that long ago.')
        return False
    if v['amount']!=tx['B']:
        tools.log('that is not how much money you had that long ago')
        return False
    if 'secret_hash' not in tx:
        tools.log('need the hash of a secret')
        return False
    for t in txs:
        if tools.addr(t)==address and t['type']=='sign':
            #tools.log('can only have one sign tx per block')
            return False
    if len(tx['jackpots'])<1: 
        tools.log('insufficient jackpots')
        return False
    if not signature_check(tx):
        out[0]+='signature check'
        return False
    length=tools.local_get('length')
    if int(tx['on_block'])!=int(length+1):
        out[0]+='this tx is for the wrong block. have '+str(length+1) +' need: ' +str(tx['on_block'])
        return False
    if tx['on_block']>0:
        if not tx['prev']==tools.db_get(length)['block_hash']:
            tools.log('must give hash of previous block')
            return False
    ran=tools.det_random(tx['on_block'])
    for j in tx['jackpots']:
        if type(j)!=int or j not in range(200):
               tools.log('bad jackpot')
               return False
        if len(filter(lambda x: x==j, tx['jackpots']))!=1:
               tools.log('no repeated jackpots')
               return False
        if not tools.winner(B, M, ran, address, j):
            tools.log('that jackpot is not valid: '+str(j))
            return False
    if tx['amount']<custom.minimum_deposit:
        tools.log('you have to deposit more than that')
        return False
    return True
Esempio n. 3
0
def sign_verify(tx, txs, out, DB):  # check the validity of a transaction of type sign.
    a = tools.addr(tx)
    acc = tools.db_get(a)
    if a["amount"] < tx["B"]:
        tools.log("you do not have that much money")
    B = tx["B"]
    M = custom.all_money
    address = tools.addr(tx)
    block = tools.db_get(tx["on_block"])
    num = max(0, tx["on_block"] - (custom.long_time * 2 - custom.medium_time))
    election_block = tools.db_get(num)
    if "root_hash" not in election_block:
        out[0] += "no root hash"
        return False
    v = tools.db_verify(election_block["root_hash"], address, tx["proof"])
    if v == False:
        tools.log("your address did not exist that long ago.")
        return False
    if v["amount"] != tx["B"]:
        tools.log("that is not how much money you had that long ago")
        return False
    if "secret_hash" not in tx:
        tools.log("need the hash of a secret")
        return False
    for t in txs:
        if tools.addr(t) == address and t["type"] == "sign":
            # tools.log('can only have one sign tx per block')
            return False
    if len(tx["jackpots"]) < 1:
        tools.log("insufficient jackpots")
        return False
    length = tools.local_get("length")
    if int(tx["on_block"]) != int(length + 1):
        out[0] += "this tx is for the wrong block. have " + str(length + 1) + " need: " + str(tx["on_block"])
        return False
    if tx["on_block"] > 0:
        if not tx["prev"] == tools.db_get(length)["block_hash"]:
            tools.log("must give hash of previous block")
            return False
    ran = tools.det_random(tx["on_block"])
    for j in tx["jackpots"]:
        if type(j) != int or j not in range(200):
            tools.log("bad jackpot")
            return False
        if len(filter(lambda x: x == j, tx["jackpots"])) != 1:
            tools.log("no repeated jackpots")
            return False
        if not tools.winner(B, M, ran, address, j):
            tools.log("that jackpot is not valid: " + str(j))
            return False
    if tx["amount"] < custom.minimum_deposit:
        tools.log("you have to deposit more than that")
        return False
    return True
Esempio n. 4
0
def create_sign_tx():
    on_block=tools.local_get('length')+1
    if on_block==0:
        time.sleep(1)
        return{'error':'not ready'}
    r=tools.det_random(on_block)
    jackpots=[]
    address=tools.local_get('address')
    l=max(-1, on_block-1-(custom.long_time*2-custom.medium_time))
    election_block=tools.db_get(l+1)
    proof=tools.local_get('balance_proofs'+str(l))
    if 'root_hash' not in election_block:
        return({'error':'database changed'})
    a=tools.db_verify(election_block['root_hash'], address, proof)
    if a==False:
        #tools.log('election block: ' +str(election_block))
        #tools.log('proof: ' +str(proof))
        return({'error':'not valid proof'})
    old_balance=a['amount']
    M=custom.all_money
    for j in range(custom.jackpot_nonces):
        if tools.winner(old_balance, M, r, address, j):
            jackpots.append(j)
    if len(jackpots)>0:
        tx={'on_block':on_block, 'jackpots':jackpots, 'type':'sign', 'amount':M/3000/3}
        tx['B']=old_balance
        tx['proof']=proof
        if proof=='empty':
            time.sleep(1)
            return {'error':'not ready'}
        secrets=tools.local_get('secrets')
        if str(on_block) in secrets:
            secret=secrets[str(on_block)]
        else:
            secret=tools.unpackage(tools.package({'salt':str(random.random())+str(random.random()), 'entropy':random.randint(0,1)}))
            secrets[str(on_block)]=secret
            tools.local_put('secrets', secrets)
        tx['secret_hash']=tools.det_hash(secret)
        if on_block>0:
            block=tools.db_get(on_block-1)
            if 'amount' in block and block['amount']==0:
                return({'error':'database changed'})
            #tools.log('on_block: ' +str(a))
            tx['prev']=block['block_hash']
    else:
        tx= {'error':'no jackpots'}
    return tx
Esempio n. 5
0
def main():
    board.Board.size = 10
    board.Board.index = 1
    board.Board.logs.clear()
    p1 = board.Board("P1")
    p1.init_board()
    p2 = board.Board("P2")
    p2.init_board()
    is_running = True
    is_wrong_enter_mode = False
    while is_running:
        if game_sts == 0:
            tools.clear_console()
            if is_wrong_enter_mode:
                print(
                    '\033[31m',
                    "The game mod you have selected is invalid, please select 1 or 2!",
                    '\033[0m')
                is_wrong_enter_mode = False
            print(
                "Available game modes: 1 - Multiplayer, 2 - Single player, or write \"exit\" to terminate."
            )
            mode = input("Select the game mode: ")
            mode = mode.upper()
            if mode == "EXIT":
                break
            elif tools.is_mode_correct(mode):
                set_game_sts(int(mode))
            else:
                is_wrong_enter_mode = True
                continue
        elif game_sts == 1:
            tools.clear_console()
            if tools.is_ship_available(fleet_p1)[0] or tools.is_ship_available(
                    fleet_p2)[0]:
                if player == "P1" and tools.is_ship_available(fleet_p1)[0]:
                    print(p1.ship_input_board_print())
                    p1.print_message()
                    p1.message = None
                    ship = tools.is_ship_available(fleet_p1)
                    print(
                        f"\033[36mPlayer {player[1]} placing ships phase.\033[0m"
                    )
                else:
                    set_player("P2")
                    print(p2.ship_input_board_print())
                    p2.print_message()
                    p2.message = None
                    ship = tools.is_ship_available(fleet_p2)
                    print(
                        f"\033[34mPlayer {player[1]} placing ships phase.\033[0m"
                    )
                coord = input(
                    f"Write coordinates of the {ship[1]} (size: {ship[2]}) (e.g. A1, D5): "
                )
                coord = coord.upper()
                if coord == "EXIT":
                    break
                else:
                    if player == "P1":
                        if p1.is_coordinates_correct(coord):
                            coord_xy = tools.get_coordinates(coord)
                            coord_xy.append(ship[2])
                            if ship[2] > 1:
                                position = input(
                                    f"Set location of the {ship[1]}, \"H\" - horizontal, \"V\" - vertical: "
                                )
                                position = position.upper()
                            else:
                                position = "H"
                            if p1.is_location_correct(position):
                                coord_xy.append(position)
                                is_added = p1.add_ship(*coord_xy)
                                if is_added:
                                    fleet_p1[(ship[1], ship[2])] -= 1
                    else:
                        if p2.is_coordinates_correct(coord):
                            coord_xy = tools.get_coordinates(coord)
                            coord_xy.append(ship[2])
                            if ship[2] > 1:
                                position = input(
                                    f"Set position of the {ship[1]}, \"H\" - horizontal, \"V\" - vertical: "
                                )
                                position = position.upper()
                            else:
                                position = "H"
                            if p2.is_location_correct(position):
                                coord_xy.append(position)
                                is_added = p2.add_ship(*coord_xy)
                                if is_added:
                                    fleet_p2[(ship[1], ship[2])] -= 1
                                    if not tools.is_ship_available(
                                            fleet_p2)[0]:
                                        set_player("P1")
                                        board.Board.logs.clear()
                                        board.Board.index = 1
            else:
                tools.clear_console()
                if player == "P1":
                    tools.print_both_boards(p1, p2, game_sts)
                    p2.print_message()
                    p2.message = None
                    print(f"\033[36mPlayer {player[1]}\'s turn.\033[0m")
                else:
                    tools.print_both_boards(p1, p2, game_sts)
                    p1.print_message()
                    p1.message = None
                    print(f"\033[34mPlayer {player[1]}\'s turn.\033[0m")
                shot = input(f"Player {player[1]} shot: ")
                shot = shot.upper()
                if shot == "EXIT":
                    break
                else:
                    if player == "P1":
                        if p2.is_coordinates_correct(shot):
                            coord_xy = tools.get_coordinates(shot)
                            is_shot = p2.shot(*coord_xy)
                            tools.clear_console()
                            tools.print_both_boards(p1, p2, game_sts)
                            p2.print_message()
                            p2.message = None
                            if is_shot:
                                set_player("P2")
                            time.sleep(2)
                            if p2.has_lost():
                                p2.message = "Player 1 has won!!!"
                                p2.print_message()
                                p2.message = None
                                del p1
                                del p2
                                reset_game()
                                board.Board.size = 10
                                board.Board.index = 1
                                board.Board.logs.clear()
                                p1 = board.Board("P1")
                                p1.init_board()
                                p2 = board.Board("P2")
                                p2.init_board()
                                time.sleep(4)
                                tools.winner("p1.txt")
                    elif player == "P2":
                        if p1.is_coordinates_correct(shot):
                            coord_xy = tools.get_coordinates(shot)
                            is_shot = p1.shot(*coord_xy)
                            tools.clear_console()
                            tools.print_both_boards(p1, p2, game_sts)
                            p1.print_message()
                            p1.message = None
                            if is_shot:
                                set_player("P1")
                            time.sleep(2)
                            if p1.has_lost():
                                p1.message = "Player 2 has won!!!"
                                p1.print_message()
                                p1.message = None
                                del p1
                                del p2
                                reset_game()
                                board.Board.size = 10
                                board.Board.index = 1
                                board.Board.logs.clear()
                                p1 = board.Board("P1")
                                p1.init_board()
                                p2 = board.Board("P2")
                                p2.init_board()
                                time.sleep(4)
                                tools.winner("p2.txt")

        elif game_sts == 2:
            tools.clear_console()
            if tools.is_ship_available(fleet_p1)[0] or tools.is_ship_available(
                    fleet_p2)[0]:
                if player == "P1" and tools.is_ship_available(fleet_p1)[0]:
                    print(p1.ship_input_board_print())
                    p1.print_message()
                    p1.message = None
                    print(
                        f"\033[36mPlayer {player[1]} placing ships phase.\033[0m"
                    )
                    ship = tools.is_ship_available(fleet_p1)
                    coord = input(
                        f"Write coordinates of the {ship[1]} (size: {ship[2]}) (e.g. A1, D5): "
                    )
                    coord = coord.upper()
                    if coord == "EXIT":
                        break
                    else:
                        if player == "P1":
                            if p1.is_coordinates_correct(coord):
                                coord_xy = tools.get_coordinates(coord)
                                coord_xy.append(ship[2])
                                if ship[2] > 1:
                                    position = input(
                                        f"Set position of the {ship[1]}, \"H\" - horizontal, \"V\" - vertical: "
                                    )
                                    position = position.upper()
                                else:
                                    position = "H"
                                if p1.is_location_correct(position):
                                    coord_xy.append(position)
                                    is_added = p1.add_ship(*coord_xy)
                                    if is_added:
                                        fleet_p1[(ship[1], ship[2])] -= 1
                                        if not tools.is_ship_available(
                                                fleet_p1)[0]:
                                            set_player("AI")
                                            board.Board.logs.clear()
                                            board.Board.index = 1
                else:
                    p2.ai_place_ships(fleet_p2)
                    p2.message = None
                    set_player("P1")
                    board.Board.logs.clear()
                    board.Board.index = 1
            else:
                tools.clear_console()
                if player == "P1":
                    tools.print_both_boards(p1, p2, game_sts)
                    print(f"\033[36mPlayer {player[1]}\'s turn.\033[0m")
                    p2.print_message()
                    p2.message = None
                    shot = input(f"Player {player[1]} shot: ")
                    shot = shot.upper()
                    if shot == "EXIT":
                        break
                    else:
                        if player == "P1":
                            if p2.is_coordinates_correct(shot):
                                coord_xy = tools.get_coordinates(shot)
                                is_shot = p2.shot(*coord_xy)
                                tools.clear_console()
                                tools.print_both_boards(p1, p2, game_sts)
                                p2.print_message()
                                p2.message = None
                                if is_shot:
                                    set_player("AI")
                                time.sleep(2)
                                if p2.has_lost():
                                    p2.message = "Player 1 has won!!!"
                                    p2.print_message()
                                    p2.message = None
                                    del p1
                                    del p2
                                    board.Board.size = 10
                                    board.Board.index = 1
                                    board.Board.logs.clear()
                                    p1 = board.Board("P1")
                                    p1.init_board()
                                    p2 = board.Board("P2")
                                    p2.init_board()
                                    reset_game()
                                    time.sleep(4)
                                    tools.winner("p1.txt")
                        else:
                            tools.clear_console()
                            tools.print_both_boards(p1, p2, game_sts)
                            p2.print_message()
                            p2.message = None
                elif player == "AI":
                    p1.ai_shot()
                    tools.clear_console()
                    tools.print_both_boards(p1, p2, game_sts)
                    print(f"\033[34mPlayer AI\'s turn.\033[0m")
                    p1.print_message()
                    p1.message = None
                    set_player("P1")
                    time.sleep(3)
                    if p1.has_lost():
                        p1.message = "Player AI has won!!!"
                        p1.print_message()
                        p1.message = None
                        del p1
                        del p2
                        board.Board.size = 10
                        board.Board.index = 1
                        board.Board.logs.clear()
                        p1 = board.Board("P1")
                        p1.init_board()
                        p2 = board.Board("P2")
                        p2.init_board()
                        reset_game()
                        time.sleep(4)
                        tools.winner("ai.txt")