Beispiel #1
0
def new_bgp_withdraw():
    """
    Create a new BGP Withdraw transaction.
    The AS node that makes the transaction signs it using its private key.
    """
    values = request.get_json()
    # Check that required fields are in the posted data
    required = ['prefix', 'as_source']
    if not all(k in values for k in required):
        return 'Missing values', 400

    # Create a new transaction
    prefix = values['prefix']
    as_source = values['as_source']

    tran_time = time()  # Transaction creation time

    new_trans = BGP_Withdraw(prefix, as_source, tran_time)

    trans_hash = new_trans.calculate_hash()
    signature = node_key.sign(trans_hash.encode(), '')

    new_trans.sign(signature)

    # Broadcast it to the rest of the network (to be mined later)
    blockchain.broadcast_transaction(new_trans)

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    if new_trans_dict is not None and check_withdraw(prefix, as_source):
        update_bgp_txids(as_source)
        pending_transactions.append(new_trans_dict)  # to be mined later

    return 'New BGP Withdraw transaction created. It was also broadcasted to the network', 200
Beispiel #2
0
def new_assign_transaction():
    """
    Create a new transaction.
    The AS node that makes the transaction signs it using its private key.
    """
    values = request.get_json()
    # Check that required fields are in the posted data
    required = [
        'prefix', 'as_source', 'as_dest', 'source_lease', 'leaseDuration',
        'transferTag', 'last_assign'
    ]
    if not all(k in values for k in required):
        return 'Missing values', 400

    # Create a new transaction
    prefix = values['prefix']
    as_source = values['as_source']
    as_dest = values['as_dest']
    source_lease = values['source_lease']
    leaseDuration = values['leaseDuration']
    transferTag = values['transferTag']
    last_assign = values['last_assign']

    tran_time = time()  # Transaction creation time

    new_trans = AssignTransaction(prefix, as_source, as_dest, source_lease,
                                  leaseDuration, transferTag, tran_time,
                                  last_assign)

    trans_hash = new_trans.calculate_hash()
    signature = node_key.sign(trans_hash.encode(), '')

    new_trans.sign(signature)

    test_str = '{}{}{}'.format(as_source, as_dest.sort(), last_assign).encode()
    test_hash = hashlib.sha256(test_str).hexdigest()
    if test_hash in assign_txids:
        return 'Transaction was already made', 500  # don't include the same transaction multiple times in the chain

    # Broadcast it to the rest of the network (to be mined later)
    blockchain.broadcast_transaction(new_trans)

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    if new_trans_dict is not None:
        pending_transactions.append(new_trans_dict)  # to be mined later
        my_assignments.add(trans_hash)
        assigned_prefixes.add(prefix)
        assign_txids.add(test_hash)
    return 'New Assign transaction created. It was also broadcasted to the network', 200
Beispiel #3
0
def receive_incoming_assign_transaction():
    """
    Receive an incoming transaction sent by an AS.
    """
    values = request.get_json()

    # Check that the required fields are in the posted data
    required = [
        'prefix', 'as_source', 'as_dest', 'source_lease', 'leaseDuration',
        'transferTag', 'signature', 'time', 'last_assign'
    ]
    if not all(k in values for k in required):
        return 'Missing values', 400

    prefix = values['prefix']
    as_source = values['as_source']
    as_dest = values['as_dest']
    source_lease = values['source_lease']
    leaseDuration = values['leaseDuration']
    transferTag = values['transferTag']
    last_assign = values['last_assign']
    signature = values['signature']
    time = values['time']

    # Create a new transaction
    new_trans = AssignTransaction(prefix, as_source, as_dest, source_lease,
                                  leaseDuration, transferTag, time,
                                  last_assign)

    new_trans.signature = signature

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    tran_hash = new_trans.calculate_hash()

    if new_trans_dict is not None:
        pending_transactions.append(new_trans_dict)  # to be mined later
        assigned_prefixes.add(prefix)
        # for out of order transactions
        if tran_hash in invalid_transactions:
            invalid_transactions.remove(tran_hash)
        return "Incoming Assign transaction received", 200
    else:
        # store the hash of this invalid transaction to the list
        invalid_transactions.append(tran_hash)
        return "Incoming Assign transaction invalid. Transaction is not accepted", 500
Beispiel #4
0
def bgp_announce_incoming():
    """
    Receive an incoming BGP Announce transaction sent by an AS.
    """
    values = request.get_json()

    # Check that the required fields are in the posted data
    required = [
        'prefix', 'bgp_timestamp', 'as_source', 'as_source_list',
        'as_dest_list', 'signature', 'time'
    ]
    if not all(k in values for k in required):
        return 'Missing values', 400

    prefix = values['prefix']
    bgp_timestamp = values['bgp_timestamp']
    as_source = values['as_source']
    as_source_list = values['as_source_list']
    as_dest_list = values['as_dest_list']
    signature = values['signature']
    time = values['time']

    # Create a new transaction
    new_trans = BGP_Announce(prefix, bgp_timestamp, as_source, as_source_list,
                             as_dest_list, time)
    new_trans.sign(signature)

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    tran_hash = new_trans.calculate_hash()

    if new_trans_dict is not None and not check_announce(
            as_source, prefix, as_source_list, as_dest_list, bgp_timestamp):
        pending_transactions.append(new_trans_dict)  # to be mined later
        blockchain.update_bgp_announce(new_trans_dict['trans'])
        # for out of order transactions
        if tran_hash in invalid_transactions:
            invalid_transactions.remove(tran_hash)
        return "Incoming BGP Announce transaction received", 200
    else:
        # store the hash of this invalid transaction to the list
        invalid_transactions.append(tran_hash)
        return "Incoming BGP Announce transaction invalid. Transaction is not accepted", 500
Beispiel #5
0
def receive_incoming_update_transaction():
    """
    Receive an incoming Update transaction sent by an AS.
    """
    values = request.get_json()

    # Check that the required fields are in the posted data
    required = [
        'as_source', 'assign_tran_id', 'time', 'signature', 'new_lease'
    ]
    if not all(k in values for k in required):
        return 'Missing values', 400

    as_source = values['as_source']
    assign_tran_id = values['assign_tran_id']
    signature = values['signature']
    time = values['time']
    new_lease = values['new_lease']

    # Create a new transaction
    new_trans = UpdateTransaction(as_source, assign_tran_id, time, new_lease)

    new_trans.signature = signature

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    tran_hash = new_trans.calculate_hash()

    if new_trans_dict is not None:
        pending_transactions.append(new_trans_dict)  # to be mined later
        # for out of order transactions
        if tran_hash in invalid_transactions:
            invalid_transactions.remove(tran_hash)
        return "Incoming Update transaction received", 200
    else:
        # store the hash of this invalid transaction to the list
        invalid_transactions.append(tran_hash)
        return "Incoming Update transaction invalid. Transaction is not accepted", 500
Beispiel #6
0
    def check_revoke(self, transaction):
        """
        Checks occasionally if a revocation of a prefix needs to happen
        It creates and broadcasts a new Revoke transaction for this prefix if true.
        """
        from Transaction import RevokeTransaction

        as_source = transaction['input'][1]
        txid = transaction['txid']

        new_revoke = RevokeTransaction(as_source, txid, time())

        trans_hash = new_revoke.calculate_hash()
        signature = node_key.sign(trans_hash.encode(), '')

        new_revoke.sign(signature)

        new_revoke_dict = new_revoke.return_transaction()

        if new_revoke_dict is not None:
            pending_transactions.append(new_revoke_dict)
            self.broadcast_transaction(new_revoke)
            my_assignments.remove(txid)
Beispiel #7
0
def bgp_withdraw_incoming():
    """
    Receive an incoming BGP Withdraw transaction sent by an AS.
    """
    values = request.get_json()

    # Check that the required fields are in the posted data
    required = ['prefix', 'as_source', 'signature', 'time']
    if not all(k in values for k in required):
        return 'Missing values', 400

    prefix = values['prefix']
    as_source = values['as_source']
    signature = values['signature']
    time = values['time']

    # Create a new transaction
    new_trans = BGP_Withdraw(prefix, as_source, time)
    new_trans.sign(signature)

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    tran_hash = new_trans.calculate_hash()

    if new_trans_dict is not None and check_withdraw(prefix, as_source):
        update_bgp_txids(as_source)
        pending_transactions.append(new_trans_dict)  # to be mined
        # for out of order transactions
        if tran_hash in invalid_transactions:
            invalid_transactions.remove(tran_hash)
        return "Incoming BGP Withdraw transaction received", 200
    else:
        # store the hash of this invalid transaction to the list
        invalid_transactions.append(tran_hash)
        return "Incoming BGP Withdraw transaction invalid. Transaction is not accepted", 500
Beispiel #8
0
def new_update_transaction():
    """
    Create a new Update transaction.
    The AS node that makes the transaction signs it using its private key.
    """
    values = request.get_json()
    # Check that required fields are in the posted data
    required = ['as_source', 'assign_tran', 'new_lease']
    if not all(k in values for k in required):
        return 'Missing values', 400

    # Create a new transaction
    as_source = values['as_source']
    assign_tran = values['assign_tran']
    new_lease = values['new_lease']

    tran_time = time()  # Transaction creation time

    new_trans = UpdateTransaction(as_source, assign_tran, tran_time, new_lease)

    trans_hash = new_trans.calculate_hash()
    signature = node_key.sign(trans_hash.encode(), '')

    new_trans.sign(signature)

    # Broadcast it to the rest of the network (to be mined later)

    blockchain.broadcast_transaction(new_trans)

    new_trans_dict = new_trans.return_transaction(
    )  # also validates the transaction

    if new_trans_dict is not None:
        pending_transactions.append(new_trans_dict)  # to be mined later

    return 'New Update transaction created. It was also broadcasted to the network', 200