Esempio n. 1
0
    async def submit_transaction(self, request):
        log.debug(f'New request: {request}')
        # Reject TX if the queue is too large
        if len(self.queue) >= self.max_queue_len:
            return response.json({'error': "Queue full. Resubmit shortly."},
                                 status=503,
                                 headers={'Access-Control-Allow-Origin': '*'})

        tx_raw = _json.loads(request.body)
        log.error(tx_raw)
        # Check that the payload is valid JSON
        tx = decode(request.body)
        if tx is None:
            return response.json({'error': 'Malformed request body.'},
                                 headers={'Access-Control-Allow-Origin': '*'})

        # Check that the TX is correctly formatted
        try:
            transaction.check_tx_formatting(tx, self.wallet.verifying_key)

            transaction.transaction_is_valid(
                transaction=tx,
                expected_processor=self.wallet.verifying_key,
                client=self.client,
                nonces=self.nonces)

            nonce, pending_nonce = transaction.get_nonces(
                sender=tx['payload']['sender'],
                processor=tx['payload']['processor'],
                driver=self.nonces)

            pending_nonce = transaction.get_new_pending_nonce(
                tx_nonce=tx['payload']['nonce'],
                nonce=nonce,
                pending_nonce=pending_nonce)

            self.nonces.set_pending_nonce(sender=tx['payload']['sender'],
                                          processor=tx['payload']['processor'],
                                          value=pending_nonce)
        except TransactionException as e:
            log.error(f'Tx has error: {type(e)}')
            log.error(tx)
            return response.json(transaction.EXCEPTION_MAP[type(e)],
                                 headers={'Access-Control-Allow-Origin': '*'})

        # Add TX to the processing queue
        self.queue.append(request.body)
        log.error('Added to q')

        # Return the TX hash to the user so they can track it
        tx_hash = tx_hash_from_tx(tx)

        return response.json(
            {
                'success':
                'Transaction successfully submitted to the network.',
                'hash': tx_hash
            },
            headers={'Access-Control-Allow-Origin': '*'})
Esempio n. 2
0
    def test_check_tx_formatting_incorrect_processor_fails(self):
        w = Wallet()

        tx = build_transaction(wallet=w,
                               processor='b' * 64,
                               stamps=123,
                               nonce=0,
                               contract='currency',
                               function='transfer',
                               kwargs={
                                   'amount': 123.0,
                                   'to': 'jeff'
                               })

        decoded = decode(tx)

        with self.assertRaises(transaction.TransactionProcessorInvalid):
            transaction.check_tx_formatting(decoded, 'c' * 64)
Esempio n. 3
0
    def test_check_tx_formatting_signature_fails(self):
        w = Wallet()

        tx = build_transaction(wallet=w,
                               processor='b' * 64,
                               stamps=123,
                               nonce=0,
                               contract='currency',
                               function='transfer',
                               kwargs={
                                   'amount': 123,
                                   'to': 'jeff'
                               })

        decoded = decode(tx)
        decoded['payload']['sender'] = 'a' * 64

        with self.assertRaises(transaction.TransactionSignatureInvalid):
            transaction.check_tx_formatting(decoded, 'b' * 64)
Esempio n. 4
0
    def test_check_tx_formatting_not_formatted_fails(self):
        w = Wallet()

        tx = build_transaction(wallet=w,
                               processor='b' * 64,
                               stamps=123,
                               nonce=0,
                               contract='currency',
                               function='transfer',
                               kwargs={
                                   'amount': 123,
                                   'to': 'jeff'
                               })

        decoded = decode(tx)
        decoded['payload']['nonce'] = -123

        with self.assertRaises(transaction.TransactionFormattingError):
            transaction.check_tx_formatting(decoded, 'b' * 64)
Esempio n. 5
0
    def test_check_tx_formatting_succeeds(self):
        w = Wallet()

        tx = build_transaction(
            wallet=w,
            processor='b' * 64,
            stamps=123,
            nonce=0,
            contract='currency',
            function='transfer',
            kwargs={
                'amount': decimal.Decimal('123.872345873452873459873459870'),
                'to': 'jeff'
            })

        decoded = decode(tx)

        error = transaction.check_tx_formatting(decoded, 'b' * 64)
        self.assertIsNone(error)