Пример #1
0
def send_transaction():
    log_ip(request, inspect.stack()[0][3])
    data = request.json
    transaction = Transaction.from_json(data["transaction"]).object()
    sig = data["signature"]
    transaction.add_sign(sig)

    logger.debug(transaction)
    logger.info("Wallet: Attempting to Send Transaction")
    try:
        r = requests.post(
            "http://0.0.0.0:" + str(consts.MINER_SERVER_PORT) +
            "/newtransaction",
            data=compress(transaction.to_json()),
            timeout=(5, 1),
        )
        if r.status_code == 400:
            response.status = 400
            logger.error(
                "Wallet: Could not Send Transaction. Invalid transaction: " +
                r.text)
            return "Invalid Transaction: " + r.text
    except Exception as e:
        response.status = 400
        logger.error("Wallet: Could not Send Transaction. Try Again." + str(e))
        return "Internal error, try again"
    else:
        logger.info("Wallet: Transaction Sent, Wait for it to be Mined")
    return "Done"
Пример #2
0
def process_new_transaction(request_data: bytes) -> str:
    global BLOCKCHAIN
    transaction_json = decompress(request_data)
    if transaction_json:
        try:
            tx = Transaction.from_json(transaction_json).object()
            # Add transaction to Mempool
            if tx not in BLOCKCHAIN.mempool:
                if BLOCKCHAIN.active_chain.is_transaction_valid(tx):
                    logger.debug(
                        "Valid Transaction received, Adding to Mempool")
                    BLOCKCHAIN.mempool.add(tx)
                    # Broadcast block to other peers
                    send_to_all_peers("/newtransaction", request_data)
                else:
                    return "Transaction Already received"
            else:
                logger.debug(
                    "The transation is not valid, not added to Mempool")
                return "Not Valid Transaction"
        except Exception as e:
            logger.error("Server: New Transaction: Invalid tx received: " +
                         str(e))
            raise e
            return "Not Valid Transaction"
    return "Done"
    def send_amount(self, receiver_address: str, amount: int, message: Optional[str]) -> bool:
        contract_private_key = int(self.current_contract_priv_key)
        contract_wallet = Wallet(pub_key=None, priv_key=contract_private_key)
        data = {
            'bounty': amount,
            'sender_public_key': contract_wallet.public_key,
            'receiver_public_key': receiver_address,
            'message': message
        }
        r = requests.post("http://0.0.0.0:" + str(consts.MINER_SERVER_PORT) + "/makeTransaction", json=data)
        tx_data = r.json()
        logger.debug(f"BlockchainVmInterface: send_amount - Make Transaction returned: {tx_data}")

        transaction = Transaction.from_json(tx_data['send_this']).object()
        signed_string = contract_wallet.sign(tx_data['sign_this'])
        transaction.add_sign(signed_string)

        return self.add_contract_tx_to_mempool(transaction)
    def update_contract_output(self, output: str) -> bool:
        contract_private_key = int(self.current_contract_priv_key)
        contract_wallet = Wallet(pub_key=None, priv_key=contract_private_key)
        contract_address = contract_wallet.public_key
        cc, _, cp = get_cc_co_cp_by_contract_address(contract_address)
        if cp != str(contract_private_key):
            # We should panic
            logger.error(f"Contract private keys do not match for address {contract_address}")
            return False
        if cc == '':
            logger.error(f"Contract code is empty for address {contract_address}")
            return False
        cpub = contract_address

        data = {
            'bounty': 1,
            'sender_public_key': contract_wallet.public_key,
            'receiver_public_key': 'AAAAA' + cpub[5:], # Some random address
            'message': "Updated output of the tx", # Possibly add the prev tx_hash here
            'contract_code': cc
        }
        r = requests.post("http://0.0.0.0:" + str(consts.MINER_SERVER_PORT) + "/makeTransaction", json=data)
        logger.debug(f"BlockchainVmInterface: update_contract_output - Make Transaction returned: {r.text}")
        res = r.json()['send_this']
        tx_data = json.loads(res)

        tx_data['contract_output'] = output
        tx_data['contract_priv_key'] = cp
        tx_data['data'] = "" # Leaving it empty
        tx_data['timestamp'] = int(time.time())
        for num in tx_data['vout']:
            tx_data['vout'][num]["address"] = cpub

        transaction = Transaction.from_json(json.dumps(tx_data)).object()
        tx_vin = transaction.vin
        transaction.vin = {}
        transaction.contract_output = None
        tx_json_to_sign = transaction.to_json()
        signed_string = contract_wallet.sign(tx_json_to_sign)
        transaction.vin = tx_vin
        transaction.contract_output = output
        transaction.add_sign(signed_string)

        return self.add_contract_tx_to_mempool(transaction)
Пример #5
0
def process_new_transaction(request_data: bytes) -> Tuple[bool, str]:
    global BLOCKCHAIN
    transaction_json = decompress(request_data)
    if transaction_json:
        try:
            tx = Transaction.from_json(transaction_json).object()
            # Validations
            for txIn in tx.vin.values():
                if is_valid_contract_address(txIn.pub_key):
                    return False, "Cannot send funds from a contract address"
            if tx.contract_output is not None:
                return False, "Contract Output should be None"
            if tx.contract_code != "":  # This is a contract
                contract_address = tx.get_contract_address()
                # Ensure that there is no other contract on this contract address
                cc, _, _ = get_cc_co_cp_by_contract_address(contract_address)
                if cc != "":
                    return False, "There is already some other contract at this contract address"
            # Add transaction to Mempool
            if tx not in BLOCKCHAIN.mempool:
                ok, msg = BLOCKCHAIN.active_chain.is_transaction_valid(tx)
                if ok:
                    logger.debug(
                        "Valid Transaction received, Adding to Mempool")
                    BLOCKCHAIN.mempool.add(tx)
                    logger.debug(
                        f"Mempool now contains {len(BLOCKCHAIN.mempool)} transaction(s)"
                    )
                    # Broadcast block to other peers
                    send_to_all_peers("/newtransaction", request_data)
                else:
                    logger.debug(
                        "The transation is not valid, not added to Mempool - "
                        + msg)
                    return False, "Not Valid Transaction: " + msg
            else:
                return True, "Transaction Already received"
        except Exception as e:
            logger.error("Server: New Transaction: Invalid tx received: " +
                         str(e))
            return False, "Not Valid Transaction"
    return True, "Done"