class TestSandbox(TestCase): def setUp(self): self.d = ContractDriver() self.d.flush() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() self.d.set_contract(name='submission', code=contract, author='sys') self.d.commit() self.recipients = [secrets.token_hex(16) for _ in range(10000)] def tearDown(self): self.d.flush() def test_transfer_performance(self): e = Executor() e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( '../integration/test_contracts/erc20_clone.s.py')) for r in self.recipients: e.execute(sender='stu', contract_name='erc20_clone', function_name='transfer', kwargs={ 'amount': 1, 'to': r })
class TestMetering(TestCase): def setUp(self): # Hard load the submission contract self.d = ContractDriver() self.d.flush() with open(contracting.__path__[0] + '/contracts/submission.s.py') as f: contract = f.read() self.d.set_contract(name='submission', code=contract) self.d.commit() # Execute the currency contract with metering disabled self.e = Executor(driver=self.d) self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/currency.s.py'), metering=False, auto_commit=True) def tearDown(self): self.d.flush() def test_simple_execution_deducts_stamps(self): prior_balance = self.d.get('currency.balances:stu') output = self.e.execute('stu', 'currency', 'transfer', kwargs={ 'amount': 100, 'to': 'colin' }, auto_commit=True) new_balance = self.d.get('currency.balances:stu') self.assertEqual(float(prior_balance - new_balance - 100), output['stamps_used'] / STAMPS_PER_TAU) def test_too_few_stamps_fails_and_deducts_properly(self): prior_balance = self.d.get('currency.balances:stu') print(prior_balance) small_amount_of_stamps = 1 * STAMPS_PER_TAU output = self.e.execute('stu', 'currency', 'transfer', kwargs={ 'amount': 100, 'to': 'colin' }, stamps=small_amount_of_stamps, auto_commit=True) print(output) new_balance = self.d.get('currency.balances:stu') self.assertEqual(float(prior_balance - new_balance), output['stamps_used'] / STAMPS_PER_TAU) def test_adding_too_many_stamps_throws_error(self): prior_balance = self.d.get('currency.balances:stu') too_many_stamps = (prior_balance + 1000) * STAMPS_PER_TAU output = self.e.execute('stu', 'currency', 'transfer', kwargs={ 'amount': 100, 'to': 'colin' }, stamps=too_many_stamps, auto_commit=True) self.assertEqual(output['status_code'], 1) def test_adding_all_stamps_with_infinate_loop_eats_all_balance(self): self.d.set('currency.balances:stu', 500) self.d.commit() prior_balance = self.d.get('currency.balances:stu') prior_balance *= STAMPS_PER_TAU self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/inf_loop.s.py'), stamps=prior_balance, metering=True, auto_commit=True) new_balance = self.d.get('currency.balances:stu') # Not all stamps will be deducted because it will blow up in the middle of execution self.assertTrue(new_balance < 0.01) def test_submitting_contract_succeeds_with_enough_stamps(self): prior_balance = self.d.get('currency.balances:stu') print(prior_balance) output = self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/erc20_clone.s.py'), auto_commit=True) print(output) new_balance = self.d.get('currency.balances:stu') print(new_balance) self.assertEqual(float(prior_balance - new_balance), output['stamps_used'] / STAMPS_PER_TAU) def test_pending_writes_has_deducted_stamp_amount_prior_to_auto_commit( self): prior_balance = self.d.get('currency.balances:stu') output = self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/erc20_clone.s.py'), auto_commit=False) self.assertNotEquals( self.e.driver.pending_writes['currency.balances:stu'], prior_balance)
class TestExecutor(TestCase): def setUp(self): self.d = ContractDriver() self.d.flush() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() self.d.set_contract(name='submission', code=contract) self.d.commit() self.compiler = ContractingCompiler() def tearDown(self): self.d.flush() def test_submission(self): e = Executor(metering=False) code = '''@export def d(): a = 1 return 1 ''' kwargs = {'name': 'stubucks', 'code': code} e.execute(**TEST_SUBMISSION_KWARGS, kwargs=kwargs, auto_commit=True) self.compiler.module_name = 'stubucks' new_code = self.compiler.parse_to_code(code) self.assertEqual(self.d.get_contract('stubucks'), new_code) def test_submission_then_function_call(self): e = Executor(metering=False) code = '''@export def d(): return 1 ''' kwargs = {'name': 'stubuckz', 'code': code} e.execute(**TEST_SUBMISSION_KWARGS, kwargs=kwargs) output = e.execute(sender='stu', contract_name='stubuckz', function_name='d', kwargs={}) self.assertEqual(output['result'], 1) self.assertEqual(output['status_code'], 0) def test_kwarg_helper(self): k = submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py') code = '''v = Variable() @export def set_v(i: int): v.set(i) @export def get_v(): return v.get() ''' self.assertEqual(k['name'], 'test_orm_variable_contract') self.assertEqual(k['code'], code) def test_orm_variable_sets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py'), auto_commit=True) e.execute('stu', 'test_orm_variable_contract', 'set_v', kwargs={'i': 1000}, auto_commit=True) i = self.d.get('test_orm_variable_contract.v') self.assertEqual(i, 1000) def test_orm_variable_gets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py')) res = e.execute('stu', 'test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(res['result'], None) def test_orm_variable_gets_and_sets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py')) e.execute('stu', 'test_orm_variable_contract', 'set_v', kwargs={'i': 1000}) res = e.execute('stu', 'test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(res['result'], 1000) def test_orm_hash_sets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_hash_contract.s.py'), auto_commit=True) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'key1', 'v': 1234 }, auto_commit=True) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'another_key', 'v': 9999 }, auto_commit=True) key1 = self.d.get('test_orm_hash_contract.h:key1') another_key = self.d.get('test_orm_hash_contract.h:another_key') self.assertEqual(key1, 1234) self.assertEqual(another_key, 9999) def test_orm_hash_gets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_hash_contract.s.py')) res = e.execute('stu', 'test_orm_hash_contract', 'get_h', kwargs={'k': 'test'}) self.assertEqual(res['result'], None) def test_orm_hash_gets_and_sets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_hash_contract.s.py')) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'key1', 'v': 1234 }) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'another_key', 'v': 9999 }) key1 = e.execute('stu', 'test_orm_hash_contract', 'get_h', kwargs={'k': 'key1'}) another_key = e.execute('stu', 'test_orm_hash_contract', 'get_h', kwargs={'k': 'another_key'}) self.assertEqual(key1['result'], 1234) self.assertEqual(another_key['result'], 9999) def test_orm_foreign_variable_sets_in_contract_doesnt_work(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py')) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_foreign_key_contract.s.py')) e.execute('stu', 'test_orm_variable_contract', 'set_v', kwargs={'i': 1000}) # this should fail status = e.execute('stu', 'test_orm_foreign_key_contract', 'set_fv', kwargs={'i': 999}) self.assertEqual(status['status_code'], 1) i = e.execute('stu', 'test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(i['result'], 1000) def test_orm_foreign_variable_gets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_variable_contract.s.py')) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_foreign_key_contract.s.py')) e.execute('stu', 'test_orm_variable_contract', 'set_v', kwargs={'i': 424242}) # this should fail i = e.execute('stu', 'test_orm_foreign_key_contract', 'get_fv', kwargs={}) self.assertEqual(i['result'], 424242) def test_orm_foreign_hash_sets_in_contract_doesnt_work(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_hash_contract.s.py'), auto_commit=True) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_foreign_hash_contract.s.py'), auto_commit=True) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'key1', 'v': 1234 }, auto_commit=True) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'another_key', 'v': 9999 }, auto_commit=True) status_1 = e.execute('stu', 'test_orm_foreign_hash_contract', 'set_fh', kwargs={ 'k': 'key1', 'v': 5555 }, auto_commit=True) status_2 = e.execute('stu', 'test_orm_foreign_hash_contract', 'set_fh', kwargs={ 'k': 'another_key', 'v': 1000 }, auto_commit=True) key1 = self.d.get('test_orm_hash_contract.h:key1') another_key = self.d.get('test_orm_hash_contract.h:another_key') self.assertEqual(key1, 1234) self.assertEqual(another_key, 9999) self.assertEqual(status_1['status_code'], 1) self.assertEqual(status_2['status_code'], 1) def test_orm_foreign_hash_gets_and_sets_in_contract(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_hash_contract.s.py')) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_foreign_hash_contract.s.py')) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'key1', 'v': 1234 }) e.execute('stu', 'test_orm_hash_contract', 'set_h', kwargs={ 'k': 'another_key', 'v': 9999 }) key1 = e.execute('stu', 'test_orm_foreign_hash_contract', 'get_fh', kwargs={'k': 'key1'}) another_key = e.execute('stu', 'test_orm_foreign_hash_contract', 'get_fh', kwargs={'k': 'another_key'}) self.assertEqual(key1['result'], 1234) self.assertEqual(another_key['result'], 9999) def test_orm_contract_not_accessible(self): e = Executor(metering=False) output = e.execute( **TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_orm_no_contract_access.s.py')) self.assertIsInstance(output['result'], Exception) def test_construct_function_sets_properly(self): e = Executor(metering=False) r = e.execute( **TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/test_construct_function_works.s.py')) output = e.execute('stu', 'test_construct_function_works', 'get', kwargs={}) self.assertEqual(output['result'], 42) def test_import_exported_function_works(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/import_this.s.py')) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/importing_that.s.py')) output = e.execute('stu', 'importing_that', 'test', kwargs={}) self.assertEqual(output['result'], 12345 - 1000) def test_arbitrary_environment_passing_works_via_executor(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/i_use_env.s.py')) this_is_a_passed_in_variable = 555 env = {'this_is_a_passed_in_variable': this_is_a_passed_in_variable} output = e.execute('stu', 'i_use_env', 'env_var', kwargs={}, environment=env) self.assertEqual(output['result'], this_is_a_passed_in_variable) def test_arbitrary_environment_passing_fails_if_not_passed_correctly(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( './test_contracts/i_use_env.s.py')) this_is_a_passed_in_variable = 555 env = { 'this_is_another_passed_in_variable': this_is_a_passed_in_variable } output = e.execute('stu', 'i_use_env', 'env_var', kwargs={}, environment=env) self.assertEqual(output['status_code'], 1)
class TestContractDriver(TestCase): def setUp(self): self.d = Driver() self.d.flush() self.c = ContractDriver(self.d) self.c.clear_pending_state() def test_values_returns_values_for_keys(self): kvs = [('899af0b15aa0f227e658c96a24fa890e', 'ece22e0f19e822908b136e391d488ba5'), ('fd556e848073c754c48f07c96b460f42', '09af42359ef2be582203259e4919ce46'), ('7cf326d5efaf6699a4910960448ea9d2', '4c591f2e61097ba70da20abbf06e6bb2'), ('c481cc9f36576211e53a1c1b46d11fb8', '040cd915a382c169a24a12d843bb4cf4'), ('1f43b3c044dae3f3e32d89ed92285bf0', '38739b29fd4ee6cd7e27c69cc7832dd8'), ('6dab790f8c8e710f18e7c37417050bef', '2e28e7ebca069c4c577fd187b2f10a4d'), ('8a60834e92a513caa8282528d353e856', '6222c85f25d1a07dd58675ebba399dfb'), ('e04fdc0f3e7fb72c15de97e8500de314', '67fcb589f5f164019f395baa41864f94'), ('0691ccd1ee4d60e32e030a51a7751695', 'a3c9e2b7e08ca31ae0d06369c966f405'), ('5bf713c0713a3a1e24e7620fe884164e', '8690981810a100d80132fd22c63b6c15'), ('e42885e2af741c1d0008435809cdef34', 'f7f404a61f615b748fa0df6727769387'), ('77276db2ade4828775d182bb07a4351e', 'dedc6d5869b85a8e6f83b7aaf62be896'), ('e433b42847cc35091467a1accae2d6da', 'd7084251eddafadf1aeaf98fdee2a67a'), ('f420ccf1f0fd1b7342346f04342e50a8', 'd7bfc0c6cd62ac769e406911cd6c4be7'), ('163317155517d347c414a6085903588e', '40af65a30356375ee64a5459c99905f9'), ('16c380229f60017ae6f96b352e01e1c4', '87f66f3113f9f75c231aacfa8c6821cd'), ('c38f9c34ac9a5823df15c89da27a3b38', '5dad274ed444fb0b8b4dcd5747b65219'), ('f48123a394da4ba71a5555bd15f152b5', '51cbdc1c76e0202c3f4cbc84039c9cce'), ('7da2edaf903f6ca547f982b3eebfc7c4', '5c8f4644b207a87c6b5a22b04acaf9ee'), ('f2e46a3d29b934c8ef4fbedfbb6a825b', '2bb489bdad3155095c4c5b873744a3d5')] for kv in kvs: self.c.set(kv[0], kv[1]) vs = self.c.values() vs.sort() expected = [kv[1] for kv in kvs] expected.sort() self.assertListEqual(vs, expected) def test_items_only_in_cache_works(self): kvs = { '899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', '7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', '1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', '6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', '8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', '0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', '5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387', '77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', '163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', '16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', '7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs.items(): self.c.set(k, v) items = self.c.items() self.assertDictEqual(items, kvs) def test_items_only_in_db_works(self): kvs = { '899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', '7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', '1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', '6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', '8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', '0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', '5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387', '77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', '163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', '16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', '7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs.items(): self.c.driver.set(k, v) items = self.c.items() self.assertDictEqual(items, kvs) def test_items_in_both_cache_and_db_works(self): kvs_1 = { '899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', '7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', '1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', '6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', '8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', '0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', '5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387' } kvs_2 = { '77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', '163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', '16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', '7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs_1.items(): self.c.set(k, v) for k, v in kvs_2.items(): self.c.driver.set(k, v) kvs = { '899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', '7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', '1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', '6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', '8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', '0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', '5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387', '77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', '163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', '16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', '7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } items = self.c.items() self.assertDictEqual(items, kvs) def test_items_prefix_in_cache(self): kvs_1 = { 'pref1_899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'pref1_fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', 'pref1_7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'pref1_c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', 'pref1_1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', 'pref1_6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', 'pref1_8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'pref1_e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', 'pref1_0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', 'pref1_5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'pref1_e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387' } kvs_2 = { 'pref2_77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'pref2_e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'pref2_f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', 'pref2_163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', 'pref2_16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'pref2_c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'pref2_f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', 'pref2_7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'pref2_f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs_1.items(): self.c.set(k, v) for k, v in kvs_2.items(): self.c.set(k, v) items = self.c.items('pref1_') self.assertDictEqual(items, kvs_1) def test_items_prefix_in_db(self): kvs_1 = { 'pref1_899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'pref1_fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', 'pref1_7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'pref1_c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', 'pref1_1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', 'pref1_6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', 'pref1_8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'pref1_e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', 'pref1_0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', 'pref1_5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'pref1_e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387' } kvs_2 = { 'pref2_77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'pref2_e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'pref2_f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', 'pref2_163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', 'pref2_16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', 'pref2_c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'pref2_f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', 'pref2_7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'pref2_f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs_1.items(): self.c.driver.set(k, v) for k, v in kvs_2.items(): self.c.driver.set(k, v) items = self.c.items('pref2_') self.assertDictEqual(items, kvs_2) def test_items_prefix_mix(self): kvs_1 = { 'pref1_899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'pref1_fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', 'pref1_7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'pref1_c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', 'pref1_1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', } kvs_2 = { 'pref2_77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'pref2_e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'pref2_f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', 'pref2_163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', 'pref2_16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', } for k, v in kvs_1.items(): self.c.driver.set(k, v) for k, v in kvs_2.items(): self.c.driver.set(k, v) kvs_3 = { 'pref1_6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', 'pref1_8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'pref1_e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', 'pref1_0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', 'pref1_5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'pref1_e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387' } kvs_4 = { 'pref2_c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'pref2_f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', 'pref2_7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'pref2_f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs_3.items(): self.c.set(k, v) for k, v in kvs_4.items(): self.c.set(k, v) kvs_2.update(kvs_4) items = self.c.items('pref2_') self.assertDictEqual(items, kvs_2) def test_make_key_no_args(self): c = 'stubucks' v = 'balances' expected = 'stubucks.balances' self.assertEqual(self.c.make_key(c, v), expected) def test_make_key_args(self): c = 'stubucks' v = 'balances' a = ['1', '2', '3'] expected = 'stubucks.balances:1:2:3' self.assertEqual(self.c.make_key(c, v, a), expected) def test_get_var(self): self.c.set('stubucks.balances', 123) self.assertEqual(self.c.get_var('stubucks', 'balances'), 123) def test_set_var(self): self.c.set_var('stubucks', 'balances', value=123) self.assertEqual(self.c.get_var('stubucks', 'balances'), 123) def test_get_multi_hash_var(self): self.c.set_var('stubucks', 'balances', ['1', '2', '3'], value=123) self.assertEqual( self.c.get_var('stubucks', 'balances', ['1', '2', '3']), 123) def test_get_keys(self): kvs_1 = { 'pref1_899af0b15aa0f227e658c96a24fa890e': 'ece22e0f19e822908b136e391d488ba5', 'pref1_fd556e848073c754c48f07c96b460f42': '09af42359ef2be582203259e4919ce46', 'pref1_7cf326d5efaf6699a4910960448ea9d2': '4c591f2e61097ba70da20abbf06e6bb2', 'pref1_c481cc9f36576211e53a1c1b46d11fb8': '040cd915a382c169a24a12d843bb4cf4', 'pref1_1f43b3c044dae3f3e32d89ed92285bf0': '38739b29fd4ee6cd7e27c69cc7832dd8', } kvs_2 = { 'pref2_77276db2ade4828775d182bb07a4351e': 'dedc6d5869b85a8e6f83b7aaf62be896', 'pref2_e433b42847cc35091467a1accae2d6da': 'd7084251eddafadf1aeaf98fdee2a67a', 'pref2_f420ccf1f0fd1b7342346f04342e50a8': 'd7bfc0c6cd62ac769e406911cd6c4be7', 'pref2_163317155517d347c414a6085903588e': '40af65a30356375ee64a5459c99905f9', 'pref2_16c380229f60017ae6f96b352e01e1c4': '87f66f3113f9f75c231aacfa8c6821cd', } for k, v in kvs_1.items(): self.c.driver.set(k, v) for k, v in kvs_2.items(): self.c.driver.set(k, v) kvs_3 = { 'pref1_6dab790f8c8e710f18e7c37417050bef': '2e28e7ebca069c4c577fd187b2f10a4d', 'pref1_8a60834e92a513caa8282528d353e856': '6222c85f25d1a07dd58675ebba399dfb', 'pref1_e04fdc0f3e7fb72c15de97e8500de314': '67fcb589f5f164019f395baa41864f94', 'pref1_0691ccd1ee4d60e32e030a51a7751695': 'a3c9e2b7e08ca31ae0d06369c966f405', 'pref1_5bf713c0713a3a1e24e7620fe884164e': '8690981810a100d80132fd22c63b6c15', 'pref1_e42885e2af741c1d0008435809cdef34': 'f7f404a61f615b748fa0df6727769387' } kvs_4 = { 'pref2_c38f9c34ac9a5823df15c89da27a3b38': '5dad274ed444fb0b8b4dcd5747b65219', 'pref2_f48123a394da4ba71a5555bd15f152b5': '51cbdc1c76e0202c3f4cbc84039c9cce', 'pref2_7da2edaf903f6ca547f982b3eebfc7c4': '5c8f4644b207a87c6b5a22b04acaf9ee', 'pref2_f2e46a3d29b934c8ef4fbedfbb6a825b': '2bb489bdad3155095c4c5b873744a3d5' } for k, v in kvs_3.items(): self.c.set(k, v) for k, v in kvs_4.items(): self.c.set(k, v) kvs_2.update(kvs_4) keys = self.c.keys('pref2_') keys.sort() expected = list(kvs_2.keys()) expected.sort() self.assertListEqual(keys, expected) def test_set_contract(self): code = ''' print('hello, world!') ''' time = Datetime._from_datetime(datetime.now()) self.c.set_contract(name='test', code=code, owner='something', timestamp=time) code_obj = compile(code, '', 'exec') code_blob = marshal.dumps(code_obj) self.assertEqual(self.c.get_contract('test'), code) self.assertEqual(self.c.get_compiled('test'), code_blob) self.assertEqual(self.c.get_owner('test'), 'something') self.assertEqual(self.c.get_time_submitted('test'), time)
class TestAtomicSwapContract(TestCase): def setUp(self): self.d = ContractDriver() self.d.flush() with open(contracting.__path__[0] + '/contracts/submission.s.py') as f: contract = f.read() self.d.set_contract(name='submission', code=contract) self.d.commit() self.e = Executor(currency_contract='erc20_clone', metering=False) environment = {'now': Datetime(2019, 1, 1)} self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py'), environment=environment) self.e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/atomic_swaps.s.py')) def tearDown(self): self.e.bypass_privates = False self.d.flush() def test_initiate_not_enough_approved(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) output = self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5000000 }) self.assertEqual(output['status_code'], 1) self.assertTrue(isinstance(output['result'], AssertionError)) def test_initiate_transfers_coins_correctly(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) atomic_swaps = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account':'atomic_swaps'}) stu = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'stu'}) stu_as = self.e.execute('stu', 'erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'atomic_swaps'}) self.assertEqual(atomic_swaps['result'], 5) self.assertEqual(stu['result'], 999995) self.assertEqual(stu_as['result'], 999995) def test_initiate_writes_to_correct_key_and_properly(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}, auto_commit=True) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }, auto_commit=True) key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' expiration, amount = self.d.get(key) self.assertEqual(expiration, Datetime(2020, 1, 1)) self.assertEqual(amount, 5) def test_redeem_on_wrong_secret_fails(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) output = self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '00'}) self.assertEqual(output['status_code'], 1) self.assertEqual(str(output['result']), 'Incorrect sender or secret passed.') def test_redeem_on_wrong_sender_fails(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) output = self.e.execute('stu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}) # status_code, result, stamps_used self.assertEqual(output['status_code'], 1) self.assertEqual(str(output['result']), 'Incorrect sender or secret passed.') def test_past_expiration_fails(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2021, 1, 1)} output = self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) self.assertEqual(output['status_code'], 1) self.assertEqual(str(output['result']), 'Swap has expired.') def test_successful_redeem_transfers_coins_correctly(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2019, 1, 1)} self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) atomic_swaps = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'atomic_swaps'}) raghu = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'raghu'}) self.assertEqual(raghu['result'], 5) self.assertEqual(atomic_swaps['result'], 0) def test_successful_redeem_deletes_entry(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2019, 1, 1)} self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' v = self.d.get(key) self.assertEqual(v, None) def test_refund_works(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2021, 1, 1)} self.e.execute('stu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) atomic_swaps = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'atomic_swaps'}) stu = self.e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'stu'}) self.assertEqual(stu['result'], 1000000) self.assertEqual(atomic_swaps['result'], 0) def test_refund_too_early_fails(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2019, 1, 1)} res = self.e.execute('stu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) self.assertEqual(str(res['result']), 'Swap has not expired.') def test_refund_participant_is_signer_fails(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2021, 1, 1)} res = self.e.execute('raghu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) self.assertEqual(str(res['result']), 'Caller and signer cannot issue a refund.') def test_refund_fails_with_wrong_secret(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2019, 1, 1)} res = self.e.execute('stu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '00'}, environment=environment) self.assertEqual(str(res['result']), 'No swap to refund found.') def test_refund_resets_swaps(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) self.e.execute('stu', 'atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }) environment = {'now': Datetime(2021, 1, 1)} self.e.execute('stu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' v = self.d.get(key) self.assertEqual(v, None) def test_trying_to_call_private_function_fails(self): with self.assertRaises(AssertionError): self.e.execute('stu', 'atomic_swaps', '__test', kwargs={}) self.e.bypass_privates = True self.e.execute('stu', 'atomic_swaps', '__test', kwargs={})
} TEST_SUBMISSION_KWARGS = { 'sender': 'stu', 'contract_name': 'submission', 'function_name': 'submit_contract' } d = ContractDriver() d.flush() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() d.set_contract(name='submission', code=contract, author='sys') d.commit() recipients = [secrets.token_hex(16) for _ in range(1000)] e = Executor() e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file( '../integration/test_contracts/erc20_clone.s.py')) import datetime for i in range(20): now = datetime.datetime.now()