Beispiel #1
0
def create_transaction():
    global clients, client, pending_tx
    receiver = request.form['receiver']
    amount = int(request.form['amount'])

    try:
        pub_key = get_public_key(clients[receiver])
        tx = client.send_transaction(pub_key, amount)
        print("Creating transaction...")
        print(f"Balance {client.balance}")

        pending_tx[tx.tid] = 3
        serialized_tx = tx.serialize()
        broadcast(miners, serialized_tx, '/recv_tx')
        print("Broadcasting Transaction")

        while (pending_tx[tx.tid] != 0):
            time.sleep(1)
        print("Received enough transaction.")
        proof = request_proof(miners[0], serialized_tx)
        print("Sending proof...")
        status = send_proof(clients[receiver], serialized_tx, proof)

        del (pending_tx[tx.tid])
        if(status == 200):
            print("Proof validated")
            return Response(status=200)
        else:
            print("Proof BAAAAAAAAAAAAAAD")
            return Response(status=406)
    except ValueError:
        return Response(status=500)
Beispiel #2
0
def send_transaction():
    global miner, clients, pending_tx
    receiver = request.form['receiver']
    amount = request.form['amount']

    try:
        pub_key = get_public_key(clients[receiver])
        tx = miner.send_transaction(pub_key, amount)
        pending_tx[tx.tid] = 3
        serialized_tx = tx.serialize()
        broadcast(miners, serialized_tx, '/recv_tx')
        print("Broadcasting Transaction")

        while (pending_tx[tx.tid] != 0):
            time.sleep(1)
        print("Received enough transaction.")
        proof = miner.get_transaction_proof(tx)
        print("Sending proof...")
        status = send_proof(clients[receiver], serialized_tx, proof)

        del (pending_tx[tx.tid])
        if (status == 200):
            print("Proof validated")
            return Response(status=200)
        else:
            print("Proof BAAAAAAAAAAAAAAD")
            return Response(status=406)

    except KeyError:
        print("Not enough coins")
        return Response(status=500)
def start_mine():
    global miners, miner, malicious

    # start the second chain
    print("Mining second chain")
    block = miner.mine_malicious(bc_idx=0,
                                 b_idx=1,
                                 need_transaction=False,
                                 continuous=False)
    json_data = block.serialize()
    broadcast(miners, json_data, '/recv_block')
    broadcast_malicious(malicious, json_data, '/recv_block')
    for m in malicious.values():
        job = Process(target=start_malicious, args=(m, ))
        job.start()

    while True:
        print("Mining")
        block = miner.mine_malicious(bc_idx=1, need_transaction=False)
        if (block):
            json_data = block.serialize()
            broadcast(miners, json_data, '/recv_block')
            broadcast_malicious(malicious, json_data, '/recv_block')

    return Response(status=200)
def start_mine():
    global miners, miner, selfish_pool

    while True:
        print("Mining")
        block = miner.mine_selfish(need_transaction=False)
        if (block):
            json_data = block.serialize()
            broadcast(selfish_pool, json_data, '/recv_block_selfish')
            recv_block_selfish(block, others=False)

    return Response(status=200)
def start_mine():
    global miners, miner, pending_tx, malicious

    while True:
        print("Mining")
        block = miner.mine_malicious(bc_idx=1, need_transaction=False)
        if (block):
            json_data = block.serialize()
            broadcast(miners, json_data, '/recv_block')
            broadcast_malicious(malicious, json_data, '/recv_block')
            for tid in pending_tx.keys():
                pending_tx[tid] = pending_tx[tid] - 1

    return Response(status=200)
Beispiel #6
0
def resend_duplicate():
    global miner

    serialized_tx = request.form["tx"]
    res = broadcast(miners, serialized_tx, '/recv_tx')
    if res.status_code == 500:
        print("Can't add duplicate transaction as it exist in the blockchain")
        return Response(status=500)

    return Response(status=200)
Beispiel #7
0
def start_mine():
    global miners, miner, pending_tx, clients, malicious, selfish_pool

    wait = request.form['wait']
    selfish = False
    if (wait == 'selfish'):
        selfish = True
        wait = 'no'
    if (wait == 'no'):
        wait = False
    else:
        wait = True
    while True:
        print("Mining")
        block = miner.mine(wait)

        if (block):
            json_data = block.serialize()
            broadcast(miners, json_data, '/recv_block')
            if(wait):
                broadcast_client(
                    clients, block.serialize(True), '/recv_header')
            else:
                if(selfish):
                    broadcast_malicious(selfish_pool, json_data, '/recv_block')
                    if(len(miner.blockchain.blockchains[0]) == 1):
                        job = Process(target=start_malicious,
                                    args=(malicious['malicious1'], ))
                        job.start()
                else:
                    broadcast_malicious(malicious, json_data, '/recv_block')
                    if(len(miner.blockchain.blockchains[0]) == 3):
                        job = Process(target=start_malicious,
                                    args=(malicious['malicious1'], ))
                        job.start()
            for tid in pending_tx.keys():
                pending_tx[tid] = pending_tx[tid] - 1

    # response = Response(response=json_data, status=201)
    return Response(status=200)
def recv_block_selfish(block, others=False):
    global miners, miner, public_blockchain, private_branch_len, unpublished_block

    # if others:
    # private_chain.add_block(block, print_idx=True)
    # miner.private_chain.append(block)

    delta_prev = len(miner.private_chain) - \
        len(public_blockchain.blockchains[public_blockchain.true_blockchain])

    miner.add_block_to_private(block)

    # public_blockchain.add_block(block)
    private_branch_len += 1
    unpublished_block.append(block)

    print("delta_prev00: " + str(delta_prev))
    print("private_branch_len: " + str(private_branch_len))
    print()
    if (delta_prev == 0 and private_branch_len == 2):
        print("Sending block!")
        for i in range(len(unpublished_block)):
            try:
                block = unpublished_block.pop(0)
                # block = private_chain.blockchains[0][i]
                json_data = block.serialize()
                print("Broadcasting...")
                broadcast(miners, json_data, '/recv_block')
                print()
                public_blockchain.add_block(block)
            except IndexError:
                print("Private chain emptied")
        private_branch_len = 0
        unpublished_block = []

    return Response(status=200)
def receive_block():
    global miners, miner, public_blockchain, private_branch_len, unpublished_block

    json_block = request.form['block']
    print("Received block from normal miner.")
    print()
    block = Block.deserialize(json_block)

    delta_prev = len(miner.private_chain) - len(
        public_blockchain.blockchains[public_blockchain.true_blockchain])

    public_blockchain.add_block(block)
    # temp_public_chain = public_blockchain.blockchains[public_blockchain.true_blockchain]

    # if len(temp_public_chain) != len(public_chain):
    #     public_chain = temp_public_chain.copy()
    #     print(len(public_chain))

    print("delta_prev: " + str(delta_prev))
    print("private_branch_len: " + str(private_branch_len))
    print()

    if (delta_prev == 0):
        # private_chain.add_block(block)
        print("Resetting private chain")
        print()
        miner.reset_private_chain()
        private_branch_len = 0
        unpublished_block = []
        # temp_private_chain = Blockchain()
        # for block in public_chain:
        #     temp_private_chain.add_block(block)
        # private_chain = copy(temp_private_chain, deep=True)

    elif (delta_prev == 1):
        print("delta_prev = 1")
        # public last block
        # json_data = private_chain.blockchains[0][-1].serialize()
        try:
            block = unpublished_block.pop()
            json_data = block.serialize()
            print("Broadcasting...")
            broadcast(miners, json_data, '/recv_block')
            print()
            public_blockchain.add_block(block)
        except IndexError:
            print("Private chain emptied")
            print()

    elif (delta_prev == 2):
        # publish all chain
        for i in range(len(unpublished_block)):
            # json_data = private_chain.blockchains[0][i].serialize()
            try:
                block = unpublished_block.pop(0)
                json_data = block.serialize()
                print("Broadcasting...")
                broadcast(miners, json_data, '/recv_block')
                print()
                public_blockchain.add_block(block)
                time.sleep(1)
            except IndexError:
                print("Private chain emptied")
                print()
        private_branch_len = 0
        unpublished_block = []

    else:
        # publish first unpublished block
        # json_data = private_chain.blockchains[0][last_unpublished_block].serialize()
        try:
            block = unpublished_block.pop(0)
            json_data = block.serialize()
            print("Broadcasting...")
            broadcast(miners, json_data, '/recv_block')
            print()
            public_blockchain.add_block(block)
        except IndexError:
            print("Private chain emptied")

    return Response(status=200)