def test_fn(self): contracts = [] for contract_id in contract_ids: if type(contract_id) in (tuple, list): user_id, contract_id = contract_id contracts.append(get_contract_exports(self.ex, self.tables.contracts, contract_id=contract_id, user_id=user_id)) else: contracts.append(get_contract_exports(self.ex, self.tables.contracts, contract_id=contract_id)) return fn(self, *contracts)
def test_interpret_currency(self): amount = 1260 receiver = BOB_VK sender = ALICE_VK contract_tx = self.ordered_tx( ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK, receiver_vk=receiver, amount=amount)) self.interpreter = SenecaInterpreter() currency_contract = get_contract_exports( self.interpreter.ex, self.interpreter.contracts_table, contract_id='currency') sender_initial_balance = currency_contract.get_balance(sender) receiver_initial_balance = currency_contract.get_balance(receiver) self.interpreter.interpret(contract_tx) # Assert the contract ran and updated the expected rows self.assertEqual(currency_contract.get_balance(sender), sender_initial_balance - amount) self.assertEqual(currency_contract.get_balance(receiver), receiver_initial_balance + amount) # Assert the contract was added to the queue self.assertEqual(self.interpreter.queue_size, 1) self.assertEqual(self.interpreter.queue[0], contract_tx.transaction)
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)
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)
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)
def test_fn(self): assert hasattr(self, 'contracts_table'), "Use of this decorator expects 'self' to have a property " \ "'contracts_table', which references the contracts table built by " \ "build_contracts_table in storage/contracts.py" contracts = [] for contract_id in contract_ids: if type(contract_id) in (tuple, list): user_id, contract_id = contract_id contracts.append( get_contract_exports(self.ex, self.contracts_table, contract_id=contract_id, user_id=user_id)) else: contracts.append( get_contract_exports(self.ex, self.contracts_table, contract_id=contract_id)) return fn(self, *contracts)
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)
def test_run_bad_contract_reverts_to_last_successful_contract(self): """ Tests that running a failing contract reverts any database changes it made before the point of failure """ receiver = BOB_VK sender = ALICE_VK self.interpreter = SenecaInterpreter() currency_contract = get_contract_exports( self.interpreter.ex, self.interpreter.contracts_table, contract_id='currency') sender_initial_balance = currency_contract.get_balance(sender) contract_tx = self.ordered_tx( ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK, receiver_vk=receiver, amount=1000)) self.interpreter.interpret(contract_tx) self.assertEqual(currency_contract.get_balance(sender), sender_initial_balance - 1000) contract_tx = self.ordered_tx( ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK, receiver_vk=receiver, amount=200)) self.interpreter.interpret(contract_tx) self.assertEqual(currency_contract.get_balance(sender), sender_initial_balance - 1200) contract_tx = self.ordered_tx( ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK, receiver_vk=receiver, amount=60)) self.interpreter.interpret(contract_tx) self.assertEqual(currency_contract.get_balance(sender), sender_initial_balance - 1260) contract_tx = self.ordered_tx( ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK, receiver_vk=receiver, amount=3696947)) self.interpreter.interpret(contract_tx) self.assertEqual(currency_contract.get_balance(sender), sender_initial_balance - 1260)
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)