Ejemplo n.º 1
0
    def test_rerun_fail(self):
        orig = SenecaInterpreter._run_contract

        def mocked_rerun(*args, **kwargs):
            if kwargs.get('rerun'):
                return None
            return orig(*args, **kwargs)

        sender = ALICE_VK
        receiver = BOB_VK

        self.interpreter = SenecaInterpreter()
        dummy_contract = get_contract_exports(self.interpreter.ex,
                                              self.interpreter.contracts_table,
                                              contract_id='dummy')

        sender_initial_balance = dummy_contract.get_balance(sender)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=False))
        self.interpreter.interpret(contract_tx)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=True))
        with mock.patch(
                'cilantro.protocol.interpreter.SenecaInterpreter._run_contract',
                side_effect=mocked_rerun,
                autospec=True) as mock_some_method:
            with self.assertRaises(Exception) as context:
                self.interpreter.interpret(contract_tx)
Ejemplo n.º 2
0
    def test_flushes_with_update(self):
        """
        Tests that calling .flush on an self.interpreter with update_state=True after interpreting several transactions
        successfully commits the changes to the database
        """
        sender = ALICE_VK
        receiver = BOB_VK

        self.interpreter = SenecaInterpreter()
        dummy_contract = get_contract_exports(self.interpreter.ex,
                                              self.interpreter.contracts_table,
                                              contract_id='dummy')

        sender_initial_balance = dummy_contract.get_balance(sender)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=False))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(dummy_contract.get_balance(sender),
                         sender_initial_balance + 500)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=False))
        self.interpreter.interpret(contract_tx)
        self.interpreter.flush(update_state=True)
        self.assertEqual(dummy_contract.get_balance(sender),
                         sender_initial_balance + 1000)
Ejemplo n.º 3
0
    def test_run_bad_contract_reverts_to_last_successful_contract_remove_partial(
            self):
        """
        Tests that running a failing contract reverts any database changes it made before the point of failure
        """
        sender = ALICE_VK
        receiver = BOB_VK

        self.interpreter = SenecaInterpreter()
        dummy_contract = get_contract_exports(self.interpreter.ex,
                                              self.interpreter.contracts_table,
                                              contract_id='dummy')

        sender_initial_balance = dummy_contract.get_balance(sender)
        self.assertEqual(dummy_contract.get_balance(sender),
                         sender_initial_balance)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=False))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(dummy_contract.get_balance(sender),
                         sender_initial_balance + 500)
        # NOTE it attempts to update the balance to 123 and add the same user again
        #   Updating should work and adding already added user
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=True))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(dummy_contract.get_balance(sender),
                         sender_initial_balance + 500)
Ejemplo n.º 4
0
    def test_check_contract_async_shuffled_adhoc_checks(self):
        def assertCondition():
            self.assertEqual(contracts[0].transaction,
                             self.interpreter.queue[0])
            self.assertEqual(contracts[2].transaction,
                             self.interpreter.queue[1])
            self.assertEqual(contracts[1].transaction,
                             self.interpreter.queue[2])
            self.assertEqual(len(self.interpreter.queue), 3)
            self.interpreter.stop()

        sender = ALICE_VK
        receiver = BOB_VK
        now = int(time.time() * 1000)

        self.interpreter = SenecaInterpreter()
        contracts = [
            self.ordered_tx(ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK, receiver_vk=receiver, fail=False)) \
            for i in range(5)
        ]
        contracts[0]._data.utcTimeMs = now - 2000
        contracts[1]._data.utcTimeMs = now
        contracts[2]._data.utcTimeMs = now - 1000
        contracts[3]._data.utcTimeMs = now + 5000
        contracts[4]._data.utcTimeMs = now + 6000

        for c in contracts:
            self.interpreter.interpret(c, async=True)

        t = Timer(2.8, assertCondition)
        t.start()
Ejemplo n.º 5
0
    def test_restore_to_beginning(self):
        orig = SenecaInterpreter._run_contract

        sender = ALICE_VK
        receiver = BOB_VK

        self.interpreter = SenecaInterpreter()
        dummy_contract = get_contract_exports(self.interpreter.ex,
                                              self.interpreter.contracts_table,
                                              contract_id='dummy')

        sender_initial_balance = dummy_contract.get_balance(sender)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK,
                                                       receiver_vk=receiver,
                                                       fail=True))
        self.interpreter.interpret(contract_tx)
Ejemplo n.º 6
0
    def test_check_contract_correct_order(self):
        sender = ALICE_VK
        receiver = BOB_VK
        now = int(time.time() * 1000)

        self.interpreter = SenecaInterpreter()
        contracts = [
            self.ordered_tx(ContractTransactionBuilder.create_dummy_tx(sender_sk=ALICE_SK, receiver_vk=receiver, fail=False)) \
            for i in range(5)
        ]
        contracts[0]._data.utcTimeMs = now - 2000
        contracts[1]._data.utcTimeMs = now - 1000
        contracts[2]._data.utcTimeMs = now
        contracts[3]._data.utcTimeMs = now + 1000
        contracts[4]._data.utcTimeMs = now + 2000

        for c in contracts:
            self.interpreter.interpret(c, async=True)

        self.assertEqual(contracts,
                         [heappop(self.interpreter.heap)[1] for i in range(5)])
Ejemplo n.º 7
0
    def test_queue_binary(self):
        """
        Tests that queue_binary returns a list of serialized ContractTransactions
        """
        sender = ALICE_VK
        receiver = BOB_VK

        self.interpreter = SenecaInterpreter()
        dummy_contract = get_contract_exports(self.interpreter.ex,
                                              self.interpreter.contracts_table,
                                              contract_id='dummy')

        contracts = []
        for i in range(5):
            contract_tx = self.ordered_tx(
                ContractTransactionBuilder.create_dummy_tx(
                    sender_sk=ALICE_SK, receiver_vk=receiver, fail=False))
            self.interpreter.interpret(contract_tx)
            contracts.append(contract_tx.transaction)

        for actual, expected in zip([c.serialize() for c in contracts],
                                    self.interpreter.queue_binary):
            self.assertEqual(actual, expected)