Example #1
0
    def test_safe_repr_arbitrary_object(self):
        class Object:
            pass

        a = Object()
        b = Object()

        self.assertEqual(safe_repr(a), safe_repr(b))
Example #2
0
    def execute_tx(self, transaction, stamp_cost, environment: dict = {}, tx_number=0):
        #global PoolExecutor
        #executor = PoolExecutor
        output = self.executor.execute(
            sender=transaction['payload']['sender'],
            contract_name=transaction['payload']['contract'],
            function_name=transaction['payload']['function'],
            stamps=transaction['payload']['stamps_supplied'],
            stamp_cost=stamp_cost,
            kwargs=transaction['payload']['kwargs'],
            environment=environment,
            auto_commit=False
        )
        log.debug(output)

        tx_hash = tx_hash_from_tx(transaction)

        writes = [{'key': k, 'value': v} for k, v in output['writes'].items()]

        tx_output = {
            'hash': tx_hash,
            'transaction': transaction,
            'status': output['status_code'],
            'state': writes,
            'stamps_used': output['stamps_used'],
            'result': safe_repr(output['result']),
            'tx_number': tx_number
        }
        tx_output = format_dictionary(tx_output)
        self.executor.driver.pending_writes.clear()  # add
        return tx_output
Example #3
0
    def test_bad_tx_returns_properly(self):
        class BadTX:
            def __init__(self, tx, err):
                self.tx = tx
                self.err = err

        stu = Wallet()
        tx = transaction.build_transaction(wallet=stu,
                                           contract='testing',
                                           function='eat_stamps',
                                           kwargs={},
                                           stamps=10000,
                                           processor='0' * 64,
                                           nonce=0)

        exe = execution.SerialExecutor(executor=self.client.executor)

        result = exe.execute_tx_batch(
            batch={'transactions': [BadTX(decode(tx), Exception)]},
            stamp_cost=200,
            timestamp=int(time.time()),
            input_hash='A' * 32,
            driver=self.client.raw_driver)

        self.assertEqual(result[0]['status'], 1)
        self.assertEqual(result[0]['result'], safe_repr(str(Exception)))
        self.assertEqual(result[0]['stamps_used'], 0)
Example #4
0
    def generate_tx_error(self, transaction, error):
        tx_hash = tx_hash_from_tx(transaction)

        tx_output = {
            'hash': tx_hash,
            'transaction': transaction,
            'status': 1,
            'state': {},
            'stamps_used': 0,
            'result': safe_repr(error)
        }

        tx_output = format_dictionary(tx_output)

        return tx_output
Example #5
0
    def test_safe_repr_assertion_error_string(self):
        a = AssertionError('Hello')
        b = AssertionError('Hello')

        self.assertEqual(safe_repr(a), safe_repr(b))
Example #6
0
    def test_safe_repr_decimal_object_different_not_equal(self):
        a = Timedelta(weeks=1, days=1)
        b = Timedelta(weeks=2, days=1)

        self.assertNotEqual(safe_repr(a), safe_repr(b))
Example #7
0
    def test_safe_repr_decimal_object(self):
        a = Timedelta(weeks=1, days=1)
        b = Timedelta(weeks=1, days=1)

        self.assertEqual(safe_repr(a), safe_repr(b))
Example #8
0
    def test_safe_repr_non_object(self):
        a = str(1)
        b = safe_repr(1)

        self.assertEqual(a, b)
Example #9
0
    def execute_tx(self, transaction, stamp_cost, environment: dict = {}):
        # Deserialize Kwargs. Kwargs should be serialized JSON moving into the future for DX.

        # Add AUXILIARY_SALT for more randomness

        environment['AUXILIARY_SALT'] = transaction['metadata']['signature']

        balance = self.executor.driver.get_var(
            contract='currency',
            variable='balances',
            arguments=[transaction['payload']['sender']],
            mark=False
        )

        output = self.executor.execute(
            sender=transaction['payload']['sender'],
            contract_name=transaction['payload']['contract'],
            function_name=transaction['payload']['function'],
            stamps=transaction['payload']['stamps_supplied'],
            stamp_cost=stamp_cost,
            kwargs=transaction['payload']['kwargs'],
            environment=environment,
            auto_commit=False
        )

        self.executor.driver.pending_writes.clear()

        if output['status_code'] == 0:
            log.info(f'TX executed successfully. '
                     f'{output["stamps_used"]} stamps used. '
                     f'{len(output["writes"])} writes. '
                     f'Result = {output["result"]}')
        else:
            log.error(f'TX executed unsuccessfully. '
                      f'{output["stamps_used"]} stamps used. '
                      f'{len(output["writes"])} writes.'
                      f' Result = {output["result"]}')

        log.debug(output['writes'])

        tx_hash = tx_hash_from_tx(transaction)

        # Only apply the writes if the tx passes
        if output['status_code'] == 0:
            writes = [{'key': k, 'value': v} for k, v in output['writes'].items()]
        else:
            # Calculate only stamp deductions
            to_deduct = output['stamps_used'] / stamp_cost

            writes = [{
                'key': 'currency.balances:{}'.format(transaction['payload']['sender']),
                'value': balance - to_deduct
            }]

        tx_output = {
            'hash': tx_hash,
            'transaction': transaction,
            'status': output['status_code'],
            'state': writes,
            'stamps_used': output['stamps_used'],
            'result': safe_repr(output['result'])
        }

        tx_output = format_dictionary(tx_output)

        return tx_output