From 93af88075c69f3cb5fb211a71ec49b21035ad829 Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Wed, 15 May 2024 19:57:08 +0100 Subject: [PATCH 1/8] update tests --- setup.py | 3 +- tests/coverage.md | 25 +++++ tests/integration/test_atomic_swap.py | 8 +- tests/integration/test_builtins_locked_off.py | 28 +++--- tests/integration/test_complex_contracts.py | 99 ++++++++++--------- .../test_complex_object_setting.py | 4 +- tests/integration/test_constructor_args.py | 10 +- .../test_contracts/foreign_thing.s.py | 4 +- .../test_contracts/importing_that.s.py | 4 +- .../test_orm_foreign_hash_contract.s.py | 2 +- .../test_orm_foreign_key_contract.s.py | 2 +- tests/integration/test_datetime_contracts.py | 4 +- tests/integration/test_dynamic_imports.py | 74 +++++++------- .../test_executor_submission_process.py | 88 ++++++++--------- .../test_memory_clean_up_after_execution.py | 17 ++-- tests/integration/test_misc_contracts.py | 42 ++++---- 16 files changed, 220 insertions(+), 194 deletions(-) create mode 100644 tests/coverage.md diff --git a/setup.py b/setup.py index 9610d51f..ad2fa840 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,8 @@ "motor==2.5.1", "iso8601", "h5py", - "cachetools" + "cachetools", + "psutil" ] ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError) diff --git a/tests/coverage.md b/tests/coverage.md new file mode 100644 index 00000000..cc7363a8 --- /dev/null +++ b/tests/coverage.md @@ -0,0 +1,25 @@ +# Legacy Test Coverage + +- [ ] final_gov +- [ ] governance +- [ ] integration + - [ ] test_atomic_swap + - [x] test_builtins_locked_off + - [x] test_complex_contracts + - [x] test_complex_object_setting + - [x] test_constructor_args + - [x] test_datetime_contracts + - [x] test_dynamic_imports + - [x] test_executor_submission_process + - [x] test_memory_cleanup_after_execution # This might shed some light on the StampEstimator sync bug + - [ ] test_misc_contracts + - [ ] test_pixel_game + - [ ] test_rich_ctx_calling + - [ ] test_run_private_function + - [ ] test_seneca_client_randoms + - [ ] test_seneca_client_replaces_executor + - [ ] test_senecaCompiler_integration + - [ ] test_stamp_deduction + + + diff --git a/tests/integration/test_atomic_swap.py b/tests/integration/test_atomic_swap.py index cb77ec7f..c754c350 100644 --- a/tests/integration/test_atomic_swap.py +++ b/tests/integration/test_atomic_swap.py @@ -1,5 +1,5 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor from contracting.stdlib.bridge.time import Datetime import contracting @@ -31,8 +31,8 @@ def submission_kwargs_for_file(f): class TestAtomicSwapContract(TestCase): def setUp(self): - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() with open(contracting.__path__[0] + '/contracts/submission.s.py') as f: contract = f.read() @@ -54,7 +54,7 @@ def setUp(self): def tearDown(self): self.e.bypass_privates = False - self.d.flush() + self.d.flush_full() def test_initiate_not_enough_approved(self): self.e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'atomic_swaps'}) diff --git a/tests/integration/test_builtins_locked_off.py b/tests/integration/test_builtins_locked_off.py index e7432999..8bb52b43 100644 --- a/tests/integration/test_builtins_locked_off.py +++ b/tests/integration/test_builtins_locked_off.py @@ -7,14 +7,14 @@ def setUp(self): self.c = ContractingClient(signer='stu') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_if_builtin_can_be_submitted(self): with open('./test_contracts/builtin_lib.s.py') as f: contract = f.read() with self.assertRaises(Exception): - self.c.submit(contract, name='builtin') + self.c.submit(contract, name='con_builtin') def test_if_non_builtin_can_be_submitted(self): pass @@ -25,14 +25,14 @@ def setUp(self): self.c = ContractingClient(signer='stu') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_if_builtin_can_be_submitted(self): with open('./test_contracts/mathtime.s.py') as f: contract = f.read() with self.assertRaises(Exception): - self.c.submit(contract, name='mathtime') + self.c.submit(contract, name='con_mathtime') class TestDatabaseLoaderLoadsFirst(TestCase): @@ -40,17 +40,17 @@ def setUp(self): self.c = ContractingClient(signer='stu') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_if_builtin_can_be_submitted(self): with open('./test_contracts/contracting.s.py') as f: contract = f.read() - self.c.submit(contract, name='contracting') + self.c.submit(contract, name='con_contracting') with open('./test_contracts/import_test.s.py') as f: contract = f.read() with self.assertRaises(ImportError): - self.c.submit(contract, name='import_test') + self.c.submit(contract, name='con_import_test') # import_test = self.c.get_contract('import_test') # @@ -63,17 +63,17 @@ def setUp(self): self.c = ContractingClient(signer='stu') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_if_builtin_can_be_submitted(self): with open('./test_contracts/dynamic_import.s.py') as f: contract = f.read() - self.c.submit(contract, name='dynamic_import') + self.c.submit(contract, name='con_dynamic_import') - dynamic_import = self.c.get_contract('dynamic_import') + dynamic_import = self.c.get_contract('con_dynamic_import') with self.assertRaises(ImportError): - dynamic_import.import_thing(name='math') + dynamic_import.import_thing(name='con_math') class TestFloatIssue(TestCase): @@ -81,13 +81,13 @@ def setUp(self): self.c = ContractingClient(signer='stu') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_if_builtin_can_be_submitted(self): with open('./test_contracts/float_issue.s.py') as f: contract = f.read() - self.c.submit(contract, name='float_issue') + self.c.submit(contract, name='con_float_issue') - float_issue = self.c.get_contract('float_issue') + float_issue = self.c.get_contract('con_float_issue') float_issue.get(x=0.1, y=0.1) diff --git a/tests/integration/test_complex_contracts.py b/tests/integration/test_complex_contracts.py index aa130264..7c699263 100644 --- a/tests/integration/test_complex_contracts.py +++ b/tests/integration/test_complex_contracts.py @@ -1,10 +1,11 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor from datetime import datetime from contracting.stdlib.env import gather from hashlib import sha256, sha3_256 + def submission_kwargs_for_file(f): # Get the file name only by splitting off directories split = f.split('/') @@ -18,7 +19,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f'con_{contract_name}', 'code': contract_code, } @@ -32,34 +33,34 @@ def submission_kwargs_for_file(f): class TestComplexContracts(TestCase): def setUp(self): - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() + with open('../../contracting/contracts/submission.s.py') as f: - contract = f.read() + submission_contract = f.read() + self.d.set_contract(name='submission', + code=submission_contract) + self.d.commit() - self.d.set_contract(name='submission', - code=contract) - self.d.commit() def tearDown(self): - self.d.flush() + self.d.flush_full() - def test_token_constuction_works(self): + def test_token_construction_works(self): e = Executor(metering=False) e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/currency.s.py')) - res = e.execute('stu', 'currency', 'balance', kwargs={'account': 'colin'}) - self.assertEqual(res['result'], 100) + res = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'colin'}) - print(res) + self.assertEqual(res['result'], 100) - res = e.execute('stu', 'currency', 'balance', kwargs={'account': 'stu'}) + res = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'stu'}) self.assertEqual(res['result'], 1000000) - res = e.execute('stu', 'currency', 'balance', kwargs={'account': 'raghu'}) + res = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'raghu'}) self.assertEqual(res['result'], None) def test_token_transfer_works(self): @@ -68,10 +69,10 @@ def test_token_transfer_works(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/currency.s.py')) - e.execute('stu', 'currency', 'transfer', kwargs={'amount': 1000, 'to': 'colin'}) + e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 1000, 'to': 'colin'}) - stu_balance = e.execute('stu', 'currency', 'balance', kwargs={'account': 'stu'}) - colin_balance = e.execute('stu', 'currency', 'balance', kwargs={'account': 'colin'}) + stu_balance = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'stu'}) + colin_balance = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'colin'}) self.assertEqual(stu_balance['result'], 1000000 - 1000) self.assertEqual(colin_balance['result'], 100 + 1000) @@ -92,9 +93,9 @@ def test_token_transfer_to_new_account(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/currency.s.py')) - e.execute('stu', 'currency', 'transfer', kwargs={'amount': 1000, 'to': 'raghu'}) + e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 1000, 'to': 'raghu'}) - raghu_balance = e.execute('stu', 'currency', 'balance', kwargs={'account': 'raghu'}) + raghu_balance = e.execute('stu', 'con_currency', 'balance', kwargs={'account': 'raghu'}) self.assertEqual(raghu_balance['result'], 1000) @@ -104,9 +105,9 @@ def test_erc20_clone_construction_works(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - stu = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'stu'}) - colin = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'colin'}) - supply = e.execute('stu', 'erc20_clone', 'total_supply', kwargs={}) + stu = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'stu'}) + colin = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'colin'}) + supply = e.execute('stu', 'con_erc20_clone', 'total_supply', kwargs={}) self.assertEqual(stu['result'], 1000000) self.assertEqual(colin['result'], 100) @@ -118,9 +119,9 @@ def test_erc20_clone_transfer_works(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - e.execute('stu', 'erc20_clone', 'transfer', kwargs={'amount': 1000000, 'to': 'raghu'}) - raghu = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'raghu'}) - stu = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'stu'}) + e.execute('stu', 'con_erc20_clone', 'transfer', kwargs={'amount': 1000000, 'to': 'raghu'}) + raghu = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'raghu'}) + stu = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'stu'}) self.assertEqual(raghu['result'], 1000000) self.assertEqual(stu['result'], 0) @@ -131,10 +132,10 @@ def test_erc20_clone_transfer_fails(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - output = e.execute('stu', 'erc20_clone', 'transfer', kwargs={'amount': 10000000, 'to': 'raghu'}) + output = e.execute('stu', 'con_erc20_clone', 'transfer', kwargs={'amount': 10000000, 'to': 'raghu'}) self.assertEqual(output['status_code'], 1) - self.assertEqual(type(output['result']), AssertionError) + self.assertEqual(output['result'], 'Line 14: AssertionError (Not enough coins to send!)') def test_allowance_of_blank(self): e = Executor(metering=False) @@ -142,7 +143,7 @@ def test_allowance_of_blank(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - output = e.execute('stu', 'erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'raghu'}) + output = e.execute('stu', 'con_erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'raghu'}) self.assertEqual(output['result'], 0) def test_approve_works_and_allowance_shows(self): @@ -151,9 +152,9 @@ def test_approve_works_and_allowance_shows(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1234, 'to': 'raghu'}) + e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1234, 'to': 'raghu'}) - output = e.execute('stu', 'erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'raghu'}) + output = e.execute('stu', 'con_erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'raghu'}) self.assertEqual(output['result'], 1234) def test_approve_and_transfer_from(self): @@ -162,11 +163,11 @@ def test_approve_and_transfer_from(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/erc20_clone.s.py')) - e.execute('stu', 'erc20_clone', 'approve', kwargs={'amount': 1234, 'to': 'raghu'}) - e.execute('raghu', 'erc20_clone', 'transfer_from', kwargs={'amount': 123, 'to': 'tejas', 'main_account': 'stu'}) - raghu = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'raghu'}) - stu = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'stu'}) - tejas = e.execute('stu', 'erc20_clone', 'balance_of', kwargs={'account': 'tejas'}) + e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1234, 'to': 'raghu'}) + e.execute('raghu', 'con_erc20_clone', 'transfer_from', kwargs={'amount': 123, 'to': 'tejas', 'main_account': 'stu'}) + raghu = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'raghu'}) + stu = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'stu'}) + tejas = e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'tejas'}) self.assertEqual(raghu['result'], 0) self.assertEqual(stu['result'], (1000000 - 123)) @@ -178,10 +179,10 @@ def test_failure_after_data_writes_doesnt_commit(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/leaky.s.py'), auto_commit=True) - e.execute('colin', 'leaky', 'transfer', kwargs={'amount': 1234, 'to': 'raghu'}, auto_commit=True) + e.execute('colin', 'con_leaky', 'transfer', kwargs={'amount': 1234, 'to': 'raghu'}, auto_commit=True) - raghu = e.execute('stu', 'leaky', 'balance_of', kwargs={'account': 'raghu'}, auto_commit=True) - colin = e.execute('stu', 'leaky', 'balance_of', kwargs={'account': 'colin'}, auto_commit=True) + raghu = e.execute('stu', 'con_leaky', 'balance_of', kwargs={'account': 'raghu'}, auto_commit=True) + colin = e.execute('stu', 'con_leaky', 'balance_of', kwargs={'account': 'colin'}, auto_commit=True) self.assertEqual(raghu['result'], 0) self.assertEqual(colin['result'], 100) @@ -192,10 +193,10 @@ def test_leaky_contract_commits_on_success(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/leaky.s.py')) - e.execute('colin', 'leaky', 'transfer', kwargs={'amount': 1, 'to': 'raghu'}) + e.execute('colin', 'con_leaky', 'transfer', kwargs={'amount': 1, 'to': 'raghu'}) - raghu = e.execute('stu', 'leaky', 'balance_of', kwargs={'account': 'raghu'}) - colin = e.execute('stu', 'leaky', 'balance_of', kwargs={'account': 'colin'}) + raghu = e.execute('stu', 'con_leaky', 'balance_of', kwargs={'account': 'raghu'}) + colin = e.execute('stu', 'con_leaky', 'balance_of', kwargs={'account': 'colin'}) self.assertEqual(raghu['result'], 1) self.assertEqual(colin['result'], 99) @@ -212,13 +213,13 @@ def test_time_stdlib_works(self): kwargs=submission_kwargs_for_file('./test_contracts/test_time.s.py'), environment=environment) - gt = e.execute('colin', 'test_time', 'gt', kwargs={}, environment=environment) + gt = e.execute('colin', 'con_test_time', 'gt', kwargs={}, environment=environment) self.assertTrue(gt['result']) - lt = e.execute('colin', 'test_time', 'lt', kwargs={}, environment=environment) + lt = e.execute('colin', 'con_test_time', 'lt', kwargs={}, environment=environment) self.assertFalse(lt['result']) - eq = e.execute('colin', 'test_time', 'eq', kwargs={}, environment=environment) + eq = e.execute('colin', 'con_test_time', 'eq', kwargs={}, environment=environment) self.assertFalse(eq['result']) def test_bad_time_contract_not_submittable(self): @@ -241,7 +242,7 @@ def test_json_lists_work(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/json_tests.s.py')) - res = e.execute('colin', 'json_tests', 'get_some', kwargs={}) + res = e.execute('colin', 'con_json_tests', 'get_some', kwargs={}) self.assertListEqual([1, 2, 3, 4], res['result']) @@ -253,7 +254,7 @@ def test_time_storage_works(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/time_storage.s.py')) - v = e.execute('colin', 'time_storage', 'get', kwargs={}) + v = e.execute('colin', 'con_time_storage', 'get', kwargs={}) date = environment['datetime'].datetime(2019, 1, 1) @@ -266,7 +267,7 @@ def test_hash_sha3_works(self): kwargs=submission_kwargs_for_file('./test_contracts/test_hashing_works.s.py')) secret = 'c0d1cc254c2aca8716c6ef170630550d' - s3 = e.execute('colin', 'test_hashing_works', 't_sha3', kwargs={'s': secret}) + s3 = e.execute('colin', 'con_test_hashing_works', 't_sha3', kwargs={'s': secret}) h = sha3_256() h.update(bytes.fromhex(secret)) @@ -279,7 +280,7 @@ def test_hash_sha256_works(self): kwargs=submission_kwargs_for_file('./test_contracts/test_hashing_works.s.py')) secret = 'c0d1cc254c2aca8716c6ef170630550d' - s3 = e.execute('colin', 'test_hashing_works', 't_sha256', kwargs={'s': secret}) + s3 = e.execute('colin', 'con_test_hashing_works', 't_sha256', kwargs={'s': secret}) h = sha256() h.update(bytes.fromhex(secret)) diff --git a/tests/integration/test_complex_object_setting.py b/tests/integration/test_complex_object_setting.py index f34755da..c51b3934 100644 --- a/tests/integration/test_complex_object_setting.py +++ b/tests/integration/test_complex_object_setting.py @@ -26,8 +26,8 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.flush() - self.c.submit(contract) - self.contract = self.c.get_contract('contract') + self.c.submit(contract, name="con_contract") + self.contract = self.c.get_contract('con_contract') def tearDown(self): self.c.flush() diff --git a/tests/integration/test_constructor_args.py b/tests/integration/test_constructor_args.py index 40818039..156c111b 100644 --- a/tests/integration/test_constructor_args.py +++ b/tests/integration/test_constructor_args.py @@ -6,7 +6,7 @@ class TestSenecaClientReplacesExecutor(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -20,9 +20,9 @@ def setUp(self): self.code = f.read() def test_custom_args_works(self): - self.c.submit(self.code, name='constructor_args_contract', constructor_args={'a': 123, 'b': 321}) + self.c.submit(self.code, name='con_constructor_args_contract', constructor_args={'a': 123, 'b': 321}) - contract = self.c.get_contract('constructor_args_contract') + contract = self.c.get_contract('con_constructor_args_contract') a, b = contract.get() self.assertEqual(a, 123) @@ -30,8 +30,8 @@ def test_custom_args_works(self): def test_custom_args_overloading(self): with self.assertRaises(TypeError): - self.c.submit(self.code, name='constructor_args_contract', constructor_args={'a': 123, 'x': 321}) + self.c.submit(self.code, name='con_constructor_args_contract', constructor_args={'a': 123, 'x': 321}) def test_custom_args_not_enough_args(self): with self.assertRaises(TypeError): - self.c.submit(self.code, name='constructor_args_contract', constructor_args={'a': 123}) + self.c.submit(self.code, name='con_constructor_args_contract', constructor_args={'a': 123}) diff --git a/tests/integration/test_contracts/foreign_thing.s.py b/tests/integration/test_contracts/foreign_thing.s.py index 01de1037..cf002172 100644 --- a/tests/integration/test_contracts/foreign_thing.s.py +++ b/tests/integration/test_contracts/foreign_thing.s.py @@ -1,5 +1,5 @@ -thing_H = ForeignHash(foreign_contract='thing', foreign_name='H') -thing_V = ForeignVariable(foreign_contract='thing', foreign_name='V') +thing_H = ForeignHash(foreign_contract='con_thing', foreign_name='H') +thing_V = ForeignVariable(foreign_contract='con_thing', foreign_name='V') @export def read_H_hello(): diff --git a/tests/integration/test_contracts/importing_that.s.py b/tests/integration/test_contracts/importing_that.s.py index 5514c729..f7e0ff19 100644 --- a/tests/integration/test_contracts/importing_that.s.py +++ b/tests/integration/test_contracts/importing_that.s.py @@ -1,7 +1,7 @@ -import import_this +import con_import_this @export def test(): - a = import_this.howdy() + a = con_import_this.howdy() a -= 1000 return a \ No newline at end of file diff --git a/tests/integration/test_contracts/test_orm_foreign_hash_contract.s.py b/tests/integration/test_contracts/test_orm_foreign_hash_contract.s.py index 7bdd285c..43c44742 100644 --- a/tests/integration/test_contracts/test_orm_foreign_hash_contract.s.py +++ b/tests/integration/test_contracts/test_orm_foreign_hash_contract.s.py @@ -1,4 +1,4 @@ -fh = ForeignHash(foreign_contract='test_orm_hash_contract', foreign_name='h') +fh = ForeignHash(foreign_contract='con_test_orm_hash_contract', foreign_name='h') @export def set_fh(k: str, v: int): diff --git a/tests/integration/test_contracts/test_orm_foreign_key_contract.s.py b/tests/integration/test_contracts/test_orm_foreign_key_contract.s.py index 917e731d..54df1e35 100644 --- a/tests/integration/test_contracts/test_orm_foreign_key_contract.s.py +++ b/tests/integration/test_contracts/test_orm_foreign_key_contract.s.py @@ -1,4 +1,4 @@ -fv = ForeignVariable(foreign_contract='test_orm_variable_contract', foreign_name='v') +fv = ForeignVariable(foreign_contract='con_test_orm_variable_contract', foreign_name='v') @export def set_fv(i: int): diff --git a/tests/integration/test_datetime_contracts.py b/tests/integration/test_datetime_contracts.py index a1ae51a5..a2835c3f 100644 --- a/tests/integration/test_datetime_contracts.py +++ b/tests/integration/test_datetime_contracts.py @@ -9,9 +9,9 @@ def setUp(self): self.c.flush() with open('test_contracts/dater.py') as f: - self.c.submit(f=f.read(), name='dater') + self.c.submit(f=f.read(), name='con_dater') - self.dater = self.c.get_contract('dater') + self.dater = self.c.get_contract('con_dater') def tearDown(self): self.c.flush() diff --git a/tests/integration/test_dynamic_imports.py b/tests/integration/test_dynamic_imports.py index b018af60..87ccfa4e 100644 --- a/tests/integration/test_dynamic_imports.py +++ b/tests/integration/test_dynamic_imports.py @@ -6,7 +6,7 @@ class TestDynamicImports(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -18,27 +18,27 @@ def setUp(self): # submit erc20 clone with open('./test_contracts/stubucks.s.py') as f: code = f.read() - self.c.submit(code, name='stubucks') + self.c.submit(code, name='con_stubucks') with open('./test_contracts/tejastokens.s.py') as f: code = f.read() - self.c.submit(code, name='tejastokens') + self.c.submit(code, name='con_tejastokens') with open('./test_contracts/bastardcoin.s.py') as f: code = f.read() - self.c.submit(code, name='bastardcoin') + self.c.submit(code, name='con_bastardcoin') with open('./test_contracts/dynamic_importing.s.py') as f: code = f.read() - self.c.submit(code, name='dynamic_importing') + self.c.submit(code, name='con_dynamic_importing') - self.stubucks = self.c.get_contract('stubucks') - self.tejastokens = self.c.get_contract('tejastokens') - self.bastardcoin = self.c.get_contract('bastardcoin') - self.dynamic_importing = self.c.get_contract('dynamic_importing') + self.stubucks = self.c.get_contract('con_stubucks') + self.tejastokens = self.c.get_contract('con_tejastokens') + self.bastardcoin = self.c.get_contract('con_bastardcoin') + self.dynamic_importing = self.c.get_contract('con_dynamic_importing') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_successful_submission(self): self.assertEqual(self.stubucks.balance_of(account='stu'), 123) @@ -51,72 +51,72 @@ def test_successful_submission(self): self.assertEqual(self.bastardcoin.balance_of(account='colin'), 555) def test_get_stubuck_balances(self): - stu = self.dynamic_importing.balance_for_token(tok='stubucks', account='stu') - colin = self.dynamic_importing.balance_for_token(tok='stubucks', account='colin') + stu = self.dynamic_importing.balance_for_token(tok='con_stubucks', account='stu') + colin = self.dynamic_importing.balance_for_token(tok='con_stubucks', account='colin') self.assertEqual(stu, 123) self.assertEqual(colin, 321) def test_get_tejastokens_balances(self): - stu = self.dynamic_importing.balance_for_token(tok='tejastokens', account='stu') - colin = self.dynamic_importing.balance_for_token(tok='tejastokens', account='colin') + stu = self.dynamic_importing.balance_for_token(tok='con_tejastokens', account='stu') + colin = self.dynamic_importing.balance_for_token(tok='con_tejastokens', account='colin') self.assertEqual(stu, 321) self.assertEqual(colin, 123) def test_get_bastardcoin_balances(self): - stu = self.dynamic_importing.balance_for_token(tok='bastardcoin', account='stu') - colin = self.dynamic_importing.balance_for_token(tok='bastardcoin', account='colin') + stu = self.dynamic_importing.balance_for_token(tok='con_bastardcoin', account='stu') + colin = self.dynamic_importing.balance_for_token(tok='con_bastardcoin', account='colin') self.assertEqual(stu, 999) self.assertEqual(colin, 555) def test_is_erc20(self): - self.assertTrue(self.dynamic_importing.is_erc20_compatible(tok='stubucks')) - self.assertTrue(self.dynamic_importing.is_erc20_compatible(tok='tejastokens')) - self.assertFalse(self.dynamic_importing.is_erc20_compatible(tok='bastardcoin')) + self.assertTrue(self.dynamic_importing.is_erc20_compatible(tok='con_stubucks')) + self.assertTrue(self.dynamic_importing.is_erc20_compatible(tok='con_tejastokens')) + self.assertFalse(self.dynamic_importing.is_erc20_compatible(tok='con_bastardcoin')) def test_get_balances_erc20_enforced_stubucks(self): - stu = self.dynamic_importing.only_erc20(tok='stubucks', account='stu') - colin = self.dynamic_importing.only_erc20(tok='stubucks', account='colin') + stu = self.dynamic_importing.only_erc20(tok='con_stubucks', account='stu') + colin = self.dynamic_importing.only_erc20(tok='con_stubucks', account='colin') self.assertEqual(stu, 123) self.assertEqual(colin, 321) def test_get_balances_erc20_enforced_tejastokens(self): - stu = self.dynamic_importing.only_erc20(tok='tejastokens', account='stu') - colin = self.dynamic_importing.only_erc20(tok='tejastokens', account='colin') + stu = self.dynamic_importing.only_erc20(tok='con_tejastokens', account='stu') + colin = self.dynamic_importing.only_erc20(tok='con_tejastokens', account='colin') self.assertEqual(stu, 321) self.assertEqual(colin, 123) def test_erc20_enforced_fails_for_bastardcoin(self): with self.assertRaises(AssertionError): - stu = self.dynamic_importing.only_erc20(tok='bastardcoin', account='stu') + stu = self.dynamic_importing.only_erc20(tok='con_bastardcoin', account='stu') def test_owner_of_returns_default(self): with open('./test_contracts/owner_stuff.s.py') as f: code = f.read() - self.c.submit(code, name='owner_stuff', owner='poo') + self.c.submit(code, name='con_owner_stuff', owner='poo') - owner_stuff = self.c.get_contract('owner_stuff') + owner_stuff = self.c.get_contract('con_owner_stuff') - self.assertIsNone(owner_stuff.get_owner(s='stubucks', signer='poo')) - self.assertEqual(owner_stuff.get_owner(s='owner_stuff', signer='poo'), 'poo') + self.assertIsNone(owner_stuff.get_owner(s='con_stubucks', signer='poo')) + self.assertEqual(owner_stuff.get_owner(s='con_owner_stuff', signer='poo'), 'poo') def test_ctx_owner_works(self): with open('./test_contracts/owner_stuff.s.py') as f: code = f.read() - self.c.submit(code, name='owner_stuff', owner='poot') + self.c.submit(code, name='con_owner_stuff', owner='poot') - owner_stuff = self.c.get_contract('owner_stuff') + owner_stuff = self.c.get_contract('con_owner_stuff') self.assertEqual(owner_stuff.owner_of_this(signer='poot'), 'poot') def test_incorrect_owner_prevents_function_call(self): with open('./test_contracts/owner_stuff.s.py') as f: code = f.read() - self.c.submit(code, name='owner_stuff', owner='poot') + self.c.submit(code, name='con_owner_stuff', owner='poot') owner_stuff = self.c.get_contract('owner_stuff') with self.assertRaises(Exception): @@ -125,26 +125,26 @@ def test_incorrect_owner_prevents_function_call(self): def test_delegate_call_with_owner_works(self): with open('./test_contracts/parent_test.s.py') as f: code = f.read() - self.c.submit(code, name='parent_test') + self.c.submit(code, name='con_parent_test') with open('./test_contracts/child_test.s.py') as f: code = f.read() - self.c.submit(code, name='child_test', owner='parent_test') + self.c.submit(code, name='con_child_test', owner='con_parent_test') - parent_test = self.c.get_contract('parent_test') + parent_test = self.c.get_contract('con_parent_test') - val = parent_test.get_val_from_child(s='child_test') + val = parent_test.get_val_from_child(s='con_child_test') self.assertEqual(val, 'good') def test_delegate_with_wrong_owner_does_not_work(self): with open('./test_contracts/parent_test.s.py') as f: code = f.read() - self.c.submit(code, name='parent_test') + self.c.submit(code, name='con_parent_test') with open('./test_contracts/child_test.s.py') as f: code = f.read() - self.c.submit(code, name='child_test', owner='blorg') + self.c.submit(code, name='con_child_test', owner='blorg') parent_test = self.c.get_contract('parent_test') diff --git a/tests/integration/test_executor_submission_process.py b/tests/integration/test_executor_submission_process.py index 22f105e9..8c11ed26 100644 --- a/tests/integration/test_executor_submission_process.py +++ b/tests/integration/test_executor_submission_process.py @@ -1,5 +1,5 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor from contracting.compilation.compiler import ContractingCompiler @@ -17,7 +17,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f'con_{contract_name}', 'code': contract_code, } @@ -31,8 +31,8 @@ def submission_kwargs_for_file(f): class TestExecutor(TestCase): def setUp(self): - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -44,7 +44,7 @@ def setUp(self): self.compiler = ContractingCompiler() def tearDown(self): - self.d.flush() + self.d.flush_full() def test_submission(self): e = Executor(metering=False) @@ -56,16 +56,16 @@ def d(): ''' kwargs = { - 'name': 'stubucks', + 'name': 'con_stubucks', 'code': code } e.execute(**TEST_SUBMISSION_KWARGS, kwargs=kwargs, auto_commit=True) - self.compiler.module_name = 'stubucks' + self.compiler.module_name = 'con_stubucks' new_code = self.compiler.parse_to_code(code) - self.assertEqual(self.d.get_contract('stubucks'), new_code) + self.assertEqual(self.d.get_contract('con_stubucks'), new_code) def test_submission_then_function_call(self): e = Executor(metering=False) @@ -76,12 +76,12 @@ def d(): ''' kwargs = { - 'name': 'stubuckz', + 'name': 'con_stubuckz', 'code': code } e.execute(**TEST_SUBMISSION_KWARGS, kwargs=kwargs) - output = e.execute(sender='stu', contract_name='stubuckz', function_name='d', kwargs={}) + output = e.execute(sender='stu', contract_name='con_stubuckz', function_name='d', kwargs={}) self.assertEqual(output['result'], 1) self.assertEqual(output['status_code'], 0) @@ -100,7 +100,7 @@ def get_v(): return v.get() ''' - self.assertEqual(k['name'], 'test_orm_variable_contract') + self.assertEqual(k['name'], 'con_test_orm_variable_contract') self.assertEqual(k['code'], code) def test_orm_variable_sets_in_contract(self): @@ -109,9 +109,9 @@ def test_orm_variable_sets_in_contract(self): 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) + e.execute('stu', 'con_test_orm_variable_contract', 'set_v', kwargs={'i': 1000}, auto_commit=True) - i = self.d.get('test_orm_variable_contract.v') + i = self.d.get('con_test_orm_variable_contract.v') self.assertEqual(i, 1000) def test_orm_variable_gets_in_contract(self): @@ -120,7 +120,7 @@ def test_orm_variable_gets_in_contract(self): 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={}) + res = e.execute('stu', 'con_test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(res['result'], None) @@ -130,8 +130,8 @@ def test_orm_variable_gets_and_sets_in_contract(self): 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={}) + e.execute('stu', 'con_test_orm_variable_contract', 'set_v', kwargs={'i': 1000}) + res = e.execute('stu', 'con_test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(res['result'], 1000) @@ -141,11 +141,11 @@ def test_orm_hash_sets_in_contract(self): 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) + e.execute('stu', 'con_test_orm_hash_contract', 'set_h', kwargs={'k': 'key1', 'v': 1234}, auto_commit=True) + e.execute('stu', 'con_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') + key1 = self.d.get('con_test_orm_hash_contract.h:key1') + another_key = self.d.get('con_test_orm_hash_contract.h:another_key') self.assertEqual(key1, 1234) self.assertEqual(another_key, 9999) @@ -156,7 +156,7 @@ def test_orm_hash_gets_in_contract(self): 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'}) + res = e.execute('stu', 'con_test_orm_hash_contract', 'get_h', kwargs={'k': 'test'}) self.assertEqual(res['result'], None) @@ -166,11 +166,11 @@ def test_orm_hash_gets_and_sets_in_contract(self): 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}) + e.execute('stu', 'con_test_orm_hash_contract', 'set_h', kwargs={'k': 'key1', 'v': 1234}) + e.execute('stu', 'con_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'}) + key1 = e.execute('stu', 'con_test_orm_hash_contract', 'get_h', kwargs={'k': 'key1'}) + another_key = e.execute('stu', 'con_test_orm_hash_contract', 'get_h', kwargs={'k': 'another_key'}) self.assertEqual(key1['result'], 1234) self.assertEqual(another_key['result'], 9999) @@ -183,14 +183,14 @@ def test_orm_foreign_variable_sets_in_contract_doesnt_work(self): 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}) + e.execute('stu', 'con_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={}) + i = e.execute('stu', 'con_test_orm_variable_contract', 'get_v', kwargs={}) self.assertEqual(i['result'], 1000) def test_orm_foreign_variable_gets_in_contract(self): @@ -201,10 +201,9 @@ def test_orm_foreign_variable_gets_in_contract(self): 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}) - + e.execute('stu', 'con_test_orm_variable_contract', 'set_v', kwargs={'i': 424242}) # this should fail - i = e.execute('stu', 'test_orm_foreign_key_contract', 'get_fv', kwargs={}) + i = e.execute('stu', 'con_test_orm_foreign_key_contract', 'get_fv', kwargs={}) self.assertEqual(i['result'], 424242) @@ -216,14 +215,14 @@ def test_orm_foreign_hash_sets_in_contract_doesnt_work(self): 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) + e.execute('stu', 'con_test_orm_hash_contract', 'set_h', kwargs={'k': 'key1', 'v': 1234}, auto_commit=True) + e.execute('stu', 'con_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) + status_1 = e.execute('stu', 'con_test_orm_foreign_hash_contract', 'set_fh', kwargs={'k': 'key1', 'v': 5555}, auto_commit=True) + status_2 = e.execute('stu', 'con_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') + key1 = self.d.get('con_test_orm_hash_contract.h:key1') + another_key = self.d.get('con_test_orm_hash_contract.h:another_key') self.assertEqual(key1, 1234) self.assertEqual(another_key, 9999) @@ -239,11 +238,11 @@ def test_orm_foreign_hash_gets_and_sets_in_contract(self): 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}) + e.execute('stu', 'con_test_orm_hash_contract', 'set_h', kwargs={'k': 'key1', 'v': 1234}) + e.execute('stu', 'con_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'}) + key1 = e.execute('stu', 'con_test_orm_foreign_hash_contract', 'get_fh', kwargs={'k': 'key1'}) + another_key = e.execute('stu', 'con_test_orm_foreign_hash_contract', 'get_fh', kwargs={'k': 'another_key'}) self.assertEqual(key1['result'], 1234) self.assertEqual(another_key['result'], 9999) @@ -254,7 +253,7 @@ def test_orm_contract_not_accessible(self): 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) + self.assertEqual(output['result'], 'Line 31: Exception (["Line 1 : S2- Illicit use of \'_\' before variable : __Contract"])') def test_construct_function_sets_properly(self): e = Executor(metering=False) @@ -262,7 +261,7 @@ def test_construct_function_sets_properly(self): 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={}) + output = e.execute('stu', 'con_test_construct_function_works', 'get', kwargs={}) self.assertEqual(output['result'], 42) @@ -275,8 +274,7 @@ def test_import_exported_function_works(self): e.execute(**TEST_SUBMISSION_KWARGS, kwargs=submission_kwargs_for_file('./test_contracts/importing_that.s.py')) - output = e.execute('stu', 'importing_that', 'test', kwargs={}) - + output = e.execute('stu', 'con_importing_that', 'test', kwargs={}) self.assertEqual(output['result'], 12345 - 1000) def test_arbitrary_environment_passing_works_via_executor(self): @@ -289,7 +287,7 @@ def test_arbitrary_environment_passing_works_via_executor(self): 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) + output = e.execute('stu', 'con_i_use_env', 'env_var', kwargs={}, environment=env) self.assertEqual(output['result'], this_is_a_passed_in_variable) diff --git a/tests/integration/test_memory_clean_up_after_execution.py b/tests/integration/test_memory_clean_up_after_execution.py index bbe37f27..2188d426 100644 --- a/tests/integration/test_memory_clean_up_after_execution.py +++ b/tests/integration/test_memory_clean_up_after_execution.py @@ -1,8 +1,6 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor -from contracting.config import STAMPS_PER_TAU -from contracting.execution import runtime import contracting import gc import os @@ -21,7 +19,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f'con_{contract_name}', 'code': contract_code, } @@ -36,8 +34,8 @@ def submission_kwargs_for_file(f): class TestMetering(TestCase): def setUp(self): # Hard load the submission contract - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() with open(contracting.__path__[0] + '/contracts/submission.s.py') as f: contract = f.read() @@ -52,20 +50,21 @@ def setUp(self): kwargs=submission_kwargs_for_file('./test_contracts/currency.s.py'), metering=False, auto_commit=True) def tearDown(self): - self.d.flush() + self.d.flush_full() def test_memory_clean_up_after_execution(self): process = psutil.Process(os.getpid()) before = process.memory_info().rss / 1024 / 1024 for i in range(500): - output = self.e.execute('stu', 'currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True,metering=True) + output = self.e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True,metering=True) gc.collect() after = process.memory_info().rss / 1024 / 1024 before_2 = process.memory_info().rss / 1024 / 1024 for i in range(500): - output = self.e.execute('stu', 'currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True,metering=False) + output = self.e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True,metering=False) gc.collect() after_2 = process.memory_info().rss / 1024 / 1024 + breakpoint() print(f'RAM Difference with metering: {after - before} MB') print(f'RAM Difference without metering: {after_2 - before_2} MB') diff --git a/tests/integration/test_misc_contracts.py b/tests/integration/test_misc_contracts.py index b8fe469e..733f4128 100644 --- a/tests/integration/test_misc_contracts.py +++ b/tests/integration/test_misc_contracts.py @@ -57,7 +57,7 @@ def b(): class TestMiscContracts(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -68,22 +68,22 @@ def setUp(self): submission = self.c.get_contract('submission') - self.c.submit(too_many_writes) + self.c.submit(too_many_writes, name="con_too_many_writes") # submit erc20 clone with open('./test_contracts/thing.s.py') as f: code = f.read() - self.c.submit(code, name='thing') + self.c.submit(code, name='con_thing') with open('./test_contracts/foreign_thing.s.py') as f: code = f.read() - self.c.submit(code, name='foreign_thing') + self.c.submit(code, name='con_foreign_thing') - self.thing = self.c.get_contract('thing') - self.foreign_thing = self.c.get_contract('foreign_thing') + self.thing = self.c.get_contract('con_thing') + self.foreign_thing = self.c.get_contract('con_foreign_thing') def tearDown(self): - self.c.flush() + self.c.raw_driver.flush_full() def test_H_values_return(self): output = self.foreign_thing.read_H_hello() @@ -125,7 +125,7 @@ def test_multiple_too_many_writes_fails(self): self.c.executor.metering = False def test_failed_once_doesnt_affect_others(self): - tmwc = self.c.get_contract('too_many_writes') + tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) with self.assertRaises(AssertionError): @@ -134,7 +134,7 @@ def test_failed_once_doesnt_affect_others(self): self.c.executor.metering = False def test_memory_overload(self): - tmwc = self.c.get_contract('too_many_writes') + tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) with self.assertRaises(AssertionError): @@ -149,17 +149,19 @@ def test_memory_overload2(self): tmwc.run2() self.c.executor.metering = False - def test_memory_exploit(self): - self.c.executor.metering = True - self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - self.c.submit(exploit) - self.c.executor.metering = False + # This test stalls. + + # def test_memory_exploit(self): + # self.c.executor.metering = True + # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=100) + # with self.assertRaises(AssertionError): + # self.c.submit(exploit) + # self.c.executor.metering = False class TestPassHash(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -211,7 +213,7 @@ def something(): class TestDeveloperSubmission(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -272,7 +274,7 @@ def test(currency_reserve: float, token_reserve: float, currency_amount: float): class TestFloatThing(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -402,7 +404,7 @@ def test(): class TestHackThing(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -469,7 +471,7 @@ def multiply(): class TestFixed(TestCase): def setUp(self): self.c = ContractingClient(signer='stu', driver=ContractDriver(driver=FSDriver())) - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() From f535911556bd7f34d6602b0e0d4e4b0fd462b14d Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Wed, 15 May 2024 21:23:51 +0100 Subject: [PATCH 2/8] updated tests --- .../test_contracts/test_pass_hash.s.py | 4 +- tests/integration/test_misc_contracts.py | 103 +++++++++--------- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/tests/integration/test_contracts/test_pass_hash.s.py b/tests/integration/test_contracts/test_pass_hash.s.py index 33c73bf5..35363661 100644 --- a/tests/integration/test_contracts/test_pass_hash.s.py +++ b/tests/integration/test_contracts/test_pass_hash.s.py @@ -1,10 +1,10 @@ -import pass_hash +import con_pass_hash my_hash = Hash() @export def store(k: Any, v: Any): - pass_hash.store_on_behalf(my_hash, k, v) + con_pass_hash.store_on_behalf(my_hash, k, v) @export def get(k: Any): diff --git a/tests/integration/test_misc_contracts.py b/tests/integration/test_misc_contracts.py index 733f4128..7189adc5 100644 --- a/tests/integration/test_misc_contracts.py +++ b/tests/integration/test_misc_contracts.py @@ -109,7 +109,7 @@ def test_V_returns(self): self.assertEqual(output, 'hi') def test_single_too_many_writes_fails(self): - tmwc = self.c.get_contract('too_many_writes') + tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) with self.assertRaises(AssertionError): @@ -117,7 +117,7 @@ def test_single_too_many_writes_fails(self): self.c.executor.metering = False def test_multiple_too_many_writes_fails(self): - tmwc = self.c.get_contract('too_many_writes') + tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) with self.assertRaises(AssertionError): @@ -133,21 +133,21 @@ def test_failed_once_doesnt_affect_others(self): tmwc.not_enough() self.c.executor.metering = False - def test_memory_overload(self): - tmwc = self.c.get_contract('con_too_many_writes') - self.c.executor.metering = True - self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.run() - self.c.executor.metering = False + # def test_memory_overload(self): + # tmwc = self.c.get_contract('con_too_many_writes') + # self.c.executor.metering = True + # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) + # with self.assertRaises(AssertionError): + # tmwc.run() + # self.c.executor.metering = False - def test_memory_overload2(self): - tmwc = self.c.get_contract('too_many_writes') - self.c.executor.metering = True - self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.run2() - self.c.executor.metering = False + # def test_memory_overload2(self): + # tmwc = self.c.get_contract('too_many_writes') + # self.c.executor.metering = True + # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) + # with self.assertRaises(AssertionError): + # tmwc.run2() + # self.c.executor.metering = False # This test stalls. @@ -175,14 +175,14 @@ def setUp(self): # submit erc20 clone with open('./test_contracts/pass_hash.s.py') as f: code = f.read() - self.c.submit(code, name='pass_hash') + self.c.submit(code, name='con_pass_hash') with open('./test_contracts/test_pass_hash.s.py') as f: code = f.read() - self.c.submit(code, name='test_pass_hash') + self.c.submit(code, name='con_test_pass_hash') - self.pass_hash = self.c.get_contract('pass_hash') - self.test_pass_hash = self.c.get_contract('test_pass_hash') + self.pass_hash = self.c.get_contract('con_pass_hash') + self.test_pass_hash = self.c.get_contract('con_test_pass_hash') def test_store_value(self): self.test_pass_hash.store(k='thing', v='value') @@ -191,7 +191,7 @@ def test_store_value(self): self.assertEqual(output, 'value') -def test(): +def some_test_contract(): @export def return_something(): return 1 @@ -223,45 +223,45 @@ def setUp(self): self.c.raw_driver.commit() def test_submit_sets_developer(self): - self.c.submit(test) + self.c.submit(some_test_contract, name="con_some_test_contract") - dev = self.c.get_var('test', '__developer__') + dev = self.c.get_var('con_some_test_contract', '__developer__') self.assertEqual(dev, 'stu') def test_change_developer_if_developer_works(self): - self.c.submit(test) + self.c.submit(some_test_contract, name="con_some_test_contract") submission = self.c.get_contract('submission') - submission.change_developer(contract='test', new_developer='not_stu') + submission.change_developer(contract='con_some_test_contract', new_developer='not_stu') - dev = self.c.get_var('test', '__developer__') + dev = self.c.get_var('con_some_test_contract', '__developer__') self.assertEqual(dev, 'not_stu') def test_change_developer_prevents_new_change(self): - self.c.submit(test) + self.c.submit(some_test_contract, name="con_some_test_contract") submission = self.c.get_contract('submission') - submission.change_developer(contract='test', new_developer='not_stu') + submission.change_developer(contract='con_some_test_contract', new_developer='not_stu') with self.assertRaises(AssertionError): - submission.change_developer(contract='test', new_developer='woohoo') + submission.change_developer(contract='con_some_test_contract', new_developer='woohoo') def test_cannot_import_submission(self): - self.c.submit(import_submission) + self.c.submit(import_submission, name="con_import_submission") - imp_con = self.c.get_contract('import_submission') + imp_con = self.c.get_contract('con_import_submission') with self.assertRaises(AssertionError): imp_con.haha() -def float_thing(): +def con_float_thing(): @export - def test(currency_reserve: float, token_reserve: float, currency_amount: float): + def float_thing_fn(currency_reserve: float, token_reserve: float, currency_amount: float): k = currency_reserve * token_reserve new_currency_reserve = currency_reserve + currency_amount @@ -284,11 +284,10 @@ def setUp(self): self.c.raw_driver.commit() def test_can_add(self): - self.c.submit(float_thing) - - ft_con = self.c.get_contract('float_thing') + self.c.submit(con_float_thing, name="con_float_thing") - ft_con.test(currency_reserve=50000.125, token_reserve=52.45, currency_amount=100.25) + ft_con = self.c.get_contract('con_float_thing') + ft_con.float_thing_fn(currency_reserve=50000.125, token_reserve=52.45, currency_amount=100.25) def a(): @export @@ -354,7 +353,7 @@ def radd(a, b): wAmount = type("wAmount", (), {"__gt__": gt, "__le__": le, "__radd__": radd, "__rsub__": rsub}) fake_amount_object = wAmount() -def test_one(): +def con_test_one(): h = Hash() @construct @@ -366,8 +365,8 @@ def seed(): def output(): return h['a'], h['b'] -def test_two(): - f = ForeignHash(foreign_contract='test_one', foreign_name='h') +def con_test_two(): + f = ForeignHash(foreign_contract='con_test_one', foreign_name='h') @export def clear(): @@ -383,7 +382,7 @@ def enter(*args, **kwargs): return decorator @export - def test(): + def closure_inner(): return 1 def test_closure2(): @@ -398,7 +397,7 @@ def enter(*args, **kwargs): return decorator @export - def test(): + def closure_inner(): return 1 class TestHackThing(TestCase): @@ -414,11 +413,11 @@ def setUp(self): self.c.raw_driver.commit() def test_can_add(self): - self.c.submit(a) + self.c.submit(a, name="con_a") with self.assertRaises(Exception): - self.c.submit(module_hack) + self.c.submit(module_hack, name="con_module_hack") - ft_con = self.c.get_contract('module_hack') + ft_con = self.c.get_contract('con_module_hack') ft_con.hack() @@ -439,10 +438,10 @@ def test_cant_submit_type(self): self.c.submit(type_exploit) def test_cant_clear_foreign_hash(self): - self.c.submit(test_one) - self.c.submit(test_two) + self.c.submit(con_test_one) + self.c.submit(con_test_two) - t2 = self.c.get_contract('test_two') + t2 = self.c.get_contract('con_test_two') with self.assertRaises(Exception): t2.clear() @@ -456,7 +455,7 @@ def test_no_closures_work_around(self): self.c.submit(test_closure2) -def test_fixed(): +def con_test_fixed(): v = Variable() @construct def seed(): @@ -470,7 +469,7 @@ def multiply(): class TestFixed(TestCase): def setUp(self): - self.c = ContractingClient(signer='stu', driver=ContractDriver(driver=FSDriver())) + self.c = ContractingClient(signer='stu', driver=Driver()) self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: @@ -481,11 +480,11 @@ def setUp(self): self.c.raw_driver.commit() def test_can_multiply(self): - self.c.submit(test_fixed) + self.c.submit(con_test_fixed) self.c.raw_driver.commit() self.c.raw_driver.flush_cache() - f = self.c.get_contract('test_fixed') + f = self.c.get_contract('con_test_fixed') z = f.multiply() self.assertEqual(z, 1.234 * 5.678) From 7ba34fb454a091adc760a9e01def4029b5754dfb Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Thu, 16 May 2024 15:42:50 +0100 Subject: [PATCH 3/8] update tests --- tests/coverage.md | 24 ++++--- .../test_contracts/atomic_swaps.s.py | 10 +-- tests/integration/test_misc_contracts.py | 42 ++++++------ tests/integration/test_pixel_game.py | 16 ++--- .../integration/test_seneca_client_randoms.py | 6 +- .../test_seneca_client_replaces_executor.py | 48 ++++++------- tests/integration/test_stamp_deduction.py | 46 +++++++------ tests/performance/prof_transfer.py | 12 ++-- tests/performance/test_transfer.py | 12 ++-- .../contracts/call_infinate_loop.s.py | 4 +- .../contracts/double_spend_gas_attack.s.py | 10 ++- tests/security/contracts/hack_tokens.s.py | 2 +- tests/security/test_erc20_token_hacks.py | 67 +++++++++---------- 13 files changed, 154 insertions(+), 145 deletions(-) diff --git a/tests/coverage.md b/tests/coverage.md index cc7363a8..8c5b9549 100644 --- a/tests/coverage.md +++ b/tests/coverage.md @@ -13,13 +13,21 @@ - [x] test_executor_submission_process - [x] test_memory_cleanup_after_execution # This might shed some light on the StampEstimator sync bug - [ ] test_misc_contracts - - [ ] test_pixel_game - - [ ] test_rich_ctx_calling - - [ ] test_run_private_function - - [ ] test_seneca_client_randoms - - [ ] test_seneca_client_replaces_executor - - [ ] test_senecaCompiler_integration - - [ ] test_stamp_deduction - + - [ ] test_memory_exploit + - [ ] test_memory_overload + - [ ] test_memory_overload2 + - [ ] test_single_too_many_writes_fails + - [x] test_pixel_game + - [x] test_rich_ctx_calling + - [x] test_run_private_function + - [x] test_seneca_client_randoms + - [x] test_seneca_client_replaces_executor + - [x] test_senecaCompiler_integration + - [x] test_stamp_deduction +- [x] performance + - [x] prof_transfer - not a test, but a way of measuring performance + - [x] test_transfer - again, not a test, but a way of measuring performance +- [ ] security + - [ ] test_erc20_token_hacks diff --git a/tests/integration/test_contracts/atomic_swaps.s.py b/tests/integration/test_contracts/atomic_swaps.s.py index d5487467..2e19228b 100644 --- a/tests/integration/test_contracts/atomic_swaps.s.py +++ b/tests/integration/test_contracts/atomic_swaps.s.py @@ -1,10 +1,10 @@ -import erc20_clone +import con_erc20_clone swaps = Hash() @export def initiate(participant: str, expiration: datetime.datetime, hashlock: str, amount: float): - allowance = erc20_clone.allowance(ctx.caller, ctx.this) + allowance = con_erc20_clone.allowance(ctx.caller, ctx.this) assert allowance >= amount, \ "You cannot initiate an atomic swap without allowing '{}' " \ @@ -12,7 +12,7 @@ def initiate(participant: str, expiration: datetime.datetime, hashlock: str, amo swaps[participant, hashlock] = [expiration, amount] - erc20_clone.transfer_from(amount, ctx.this, ctx.caller) + con_erc20_clone.transfer_from(amount, ctx.this, ctx.caller) @export def redeem(secret: str): @@ -27,7 +27,7 @@ def redeem(secret: str): assert expiration >= now, 'Swap has expired.' - erc20_clone.transfer(amount, ctx.caller) + con_erc20_clone.transfer(amount, ctx.caller) swaps[ctx.caller, hashlock] = None # change this to respond to the del keyword? @export @@ -46,7 +46,7 @@ def refund(participant: str, secret: str): assert expiration < now, 'Swap has not expired.' - erc20_clone.transfer(amount, ctx.caller) + con_erc20_clone.transfer(amount, ctx.caller) swaps[participant, hashlock] = None diff --git a/tests/integration/test_misc_contracts.py b/tests/integration/test_misc_contracts.py index 7189adc5..8844933b 100644 --- a/tests/integration/test_misc_contracts.py +++ b/tests/integration/test_misc_contracts.py @@ -133,30 +133,30 @@ def test_failed_once_doesnt_affect_others(self): tmwc.not_enough() self.c.executor.metering = False - # def test_memory_overload(self): - # tmwc = self.c.get_contract('con_too_many_writes') - # self.c.executor.metering = True - # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - # with self.assertRaises(AssertionError): - # tmwc.run() - # self.c.executor.metering = False - - # def test_memory_overload2(self): - # tmwc = self.c.get_contract('too_many_writes') - # self.c.executor.metering = True - # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - # with self.assertRaises(AssertionError): - # tmwc.run2() - # self.c.executor.metering = False + def test_memory_overload(self): + tmwc = self.c.get_contract('con_too_many_writes') + self.c.executor.metering = True + self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) + with self.assertRaises(AssertionError): + tmwc.run() + self.c.executor.metering = False + + def test_memory_overload2(self): + tmwc = self.c.get_contract('con_too_many_writes') + self.c.executor.metering = True + self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) + with self.assertRaises(AssertionError): + tmwc.run2() + self.c.executor.metering = False # This test stalls. - # def test_memory_exploit(self): - # self.c.executor.metering = True - # self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=100) - # with self.assertRaises(AssertionError): - # self.c.submit(exploit) - # self.c.executor.metering = False + def test_memory_exploit(self): + self.c.executor.metering = True + self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=100) + with self.assertRaises(AssertionError): + self.c.submit(exploit) + self.c.executor.metering = False class TestPassHash(TestCase): def setUp(self): diff --git a/tests/integration/test_pixel_game.py b/tests/integration/test_pixel_game.py index 3f2e01b7..f62bab32 100644 --- a/tests/integration/test_pixel_game.py +++ b/tests/integration/test_pixel_game.py @@ -2,7 +2,7 @@ from unittest import TestCase -def coin(): +def con_coin(): supply = Variable() balances = Hash(default_value=0) owner = Variable() @@ -73,8 +73,8 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.flush() - self.c.submit(coin) - self.coin = self.c.get_contract('coin') + self.c.submit(con_coin) + self.coin = self.c.get_contract('con_coin') def tearDown(self): self.c.flush() @@ -150,8 +150,8 @@ def test_change_ownership_then_mint_succeeds(self): self.assertEqual(self.coin.supply.get(), 1000000 + 999) -def pixel_game(): - import coin +def con_pixel_game(): + import con_coin plots = Hash() landlord = Variable() @@ -218,9 +218,9 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.flush() - self.c.submit(coin) - self.c.submit(pixel_game) - self.pixel = self.c.get_contract('pixel_game') + self.c.submit(con_coin) + self.c.submit(con_pixel_game) + self.pixel = self.c.get_contract('con_pixel_game') def tearDown(self): self.c.flush() diff --git a/tests/integration/test_seneca_client_randoms.py b/tests/integration/test_seneca_client_randoms.py index c161f8c9..c065b61b 100644 --- a/tests/integration/test_seneca_client_randoms.py +++ b/tests/integration/test_seneca_client_randoms.py @@ -2,7 +2,7 @@ from contracting.client import ContractingClient import random -def random_contract(): +def con_random_contract(): random.seed() cards = [1, 2, 3, 4, 5, 6, 7, 8] @@ -65,9 +65,9 @@ class TestRandomsContract(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') self.c.flush() - self.c.submit(random_contract) + self.c.submit(con_random_contract) - self.random_contract = self.c.get_contract('random_contract') + self.random_contract = self.c.get_contract('con_random_contract') def tearDown(self): self.c.flush() diff --git a/tests/integration/test_seneca_client_replaces_executor.py b/tests/integration/test_seneca_client_replaces_executor.py index 68d3481d..45ef0a62 100644 --- a/tests/integration/test_seneca_client_replaces_executor.py +++ b/tests/integration/test_seneca_client_replaces_executor.py @@ -6,7 +6,7 @@ class TestSenecaClientReplacesExecutor(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -20,20 +20,20 @@ def setUp(self): # submit erc20 clone with open('./test_contracts/erc20_clone.s.py') as f: code = f.read() - self.c.submit(code, name='erc20_clone') + self.c.submit(code, name='con_erc20_clone') with open('./test_contracts/atomic_swaps.s.py') as f: code = f.read() - self.c.submit(code, name='atomic_swaps') + self.c.submit(code, name='con_atomic_swaps') - self.erc20_clone = self.c.get_contract('erc20_clone') - self.atomic_swaps = self.c.get_contract('atomic_swaps') + self.erc20_clone = self.c.get_contract('con_erc20_clone') + self.atomic_swaps = self.c.get_contract('con_atomic_swaps') def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_initiate_not_enough_approved(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') with self.assertRaises(AssertionError): self.atomic_swaps.initiate(participant='raghu', @@ -42,37 +42,37 @@ def test_initiate_not_enough_approved(self): amount=5000000) def test_initiate_transfers_coins_correctly(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), hashlock='eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', amount=5) - atomic_swaps = self.erc20_clone.balance_of(account='atomic_swaps') + atomic_swaps = self.erc20_clone.balance_of(account='con_atomic_swaps') stu_bal = self.erc20_clone.balance_of(account='stu') - stu_as = self.erc20_clone.allowance(owner='stu', spender='atomic_swaps') + stu_as = self.erc20_clone.allowance(owner='stu', spender='con_atomic_swaps') self.assertEqual(atomic_swaps, 5) self.assertEqual(stu_bal, 999995) self.assertEqual(stu_as, 999995) def test_initiate_writes_to_correct_key_and_properly(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), hashlock='eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', amount=5) - key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' + key = 'con_atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' expiration, amount = self.c.raw_driver.get(key) self.assertEqual(expiration, Datetime(2020, 1, 1)) self.assertEqual(amount, 5) def test_redeem_on_wrong_secret_fails(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -83,7 +83,7 @@ def test_redeem_on_wrong_secret_fails(self): self.atomic_swaps.redeem(signer='raghu', secret='00') def test_redeem_on_wrong_sender_fails(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), hashlock='eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -93,7 +93,7 @@ def test_redeem_on_wrong_sender_fails(self): self.atomic_swaps.redeem(secret='842b65a7d48e3a3c3f0e9d37eaced0b2') def test_past_expiration_fails(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -108,7 +108,7 @@ def test_past_expiration_fails(self): environment=environment) def test_successful_redeem_transfers_coins_correctly(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -121,14 +121,14 @@ def test_successful_redeem_transfers_coins_correctly(self): signer='raghu', environment=environment) - atomic_swaps = self.erc20_clone.balance_of(account='atomic_swaps') + atomic_swaps = self.erc20_clone.balance_of(account='con_atomic_swaps') raghu = self.erc20_clone.balance_of(account='raghu') self.assertEqual(raghu, 5) self.assertEqual(atomic_swaps, 0) def test_successful_redeem_deletes_entry(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -147,7 +147,7 @@ def test_successful_redeem_deletes_entry(self): self.assertEqual(v, None) def test_refund_works(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -159,14 +159,14 @@ def test_refund_works(self): self.atomic_swaps.refund(participant='raghu', secret='842b65a7d48e3a3c3f0e9d37eaced0b2', environment=environment) - atomic_swaps = self.erc20_clone.balance_of(account='atomic_swaps') + atomic_swaps = self.erc20_clone.balance_of(account='con_atomic_swaps') stu = self.erc20_clone.balance_of(account='stu') self.assertEqual(stu, 1000000) self.assertEqual(atomic_swaps, 0) def test_refund_too_early_fails(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -180,7 +180,7 @@ def test_refund_too_early_fails(self): environment=environment) def test_refund_participant_is_signer_fails(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -195,7 +195,7 @@ def test_refund_participant_is_signer_fails(self): signer='raghu') def test_refund_fails_with_wrong_secret(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), @@ -210,7 +210,7 @@ def test_refund_fails_with_wrong_secret(self): ) def test_refund_resets_swaps(self): - self.erc20_clone.approve(amount=1000000, to='atomic_swaps') + self.erc20_clone.approve(amount=1000000, to='con_atomic_swaps') self.atomic_swaps.initiate(participant='raghu', expiration=Datetime(2020, 1, 1), diff --git a/tests/integration/test_stamp_deduction.py b/tests/integration/test_stamp_deduction.py index 9736bb1d..7d842cb2 100644 --- a/tests/integration/test_stamp_deduction.py +++ b/tests/integration/test_stamp_deduction.py @@ -1,5 +1,5 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor from contracting.config import STAMPS_PER_TAU from contracting.execution import runtime @@ -18,7 +18,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f'con_{contract_name}', 'code': contract_code, } @@ -33,8 +33,8 @@ def submission_kwargs_for_file(f): class TestMetering(TestCase): def setUp(self): # Hard load the submission contract - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() with open(contracting.__path__[0] + '/contracts/submission.s.py') as f: contract = f.read() @@ -44,67 +44,69 @@ def setUp(self): self.d.commit() # Execute the currency contract with metering disabled - self.e = Executor(driver=self.d) + self.e = Executor(driver=self.d, currency_contract='con_currency') 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() + self.d.flush_full() def test_simple_execution_deducts_stamps(self): - prior_balance = self.d.get('currency.balances:stu') + prior_balance = self.d.get('con_currency.balances:stu') - output = self.e.execute('stu', 'currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True) + output = self.e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, auto_commit=True) - new_balance = self.d.get('currency.balances:stu') + new_balance = self.d.get('con_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') + prior_balance = self.d.get('con_currency.balances:stu') print(prior_balance) - output = self.e.execute('stu', 'currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, + output = self.e.execute('stu', 'con_currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, stamps=1, auto_commit=True) print(output) - new_balance = self.d.get('currency.balances:stu') + new_balance = self.d.get('con_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') + prior_balance = self.d.get('con_currency.balances:stu') too_many_stamps = (prior_balance + 1000) * STAMPS_PER_TAU - output = self.e.execute('stu', 'currency', 'transfer', kwargs={'amount': 100, 'to': 'colin'}, + output = self.e.execute('stu', 'con_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.set('con_currency.balances:stu', 500) self.d.commit() - prior_balance = self.d.get('currency.balances:stu') + prior_balance = self.d.get('con_currency.balances:stu') prior_balance *= STAMPS_PER_TAU - self.e.execute( + res = 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') + new_balance = self.d.get('con_currency.balances:stu') # Not all stamps will be deducted because it will blow up in the middle of execution + self.assertTrue(new_balance < 500) def test_submitting_contract_succeeds_with_enough_stamps(self): - prior_balance = self.d.get('currency.balances:stu') + prior_balance = self.d.get('con_currency.balances:stu') print(prior_balance) @@ -113,17 +115,17 @@ def test_submitting_contract_succeeds_with_enough_stamps(self): ) print(output) - new_balance = self.d.get('currency.balances:stu') + new_balance = self.d.get('con_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') + prior_balance = self.d.get('con_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) + self.assertNotEquals(self.e.driver.pending_writes['con_currency.balances:stu'], prior_balance) diff --git a/tests/performance/prof_transfer.py b/tests/performance/prof_transfer.py index 5e4eb001..505f6c43 100644 --- a/tests/performance/prof_transfer.py +++ b/tests/performance/prof_transfer.py @@ -1,5 +1,5 @@ import secrets -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor def submission_kwargs_for_file(f): @@ -27,8 +27,8 @@ def submission_kwargs_for_file(f): } -d = ContractDriver() -d.flush() +d = Driver() +d.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -54,8 +54,8 @@ def submission_kwargs_for_file(f): # profiler = Profiler() # profiler.start() for r in recipients: - _, res, _ = e.execute(sender='stu', - contract_name='erc20_clone', + res = e.execute(sender='stu', + contract_name='con_erc20_clone', function_name='transfer', kwargs={ 'amount': 1, @@ -67,7 +67,7 @@ def submission_kwargs_for_file(f): print(datetime.datetime.now() - now) -d.flush() +d.flush_full() # print(profiler.last_session.duration) # print(profiler.output_text(unicode=True, color=True, show_all=True)) diff --git a/tests/performance/test_transfer.py b/tests/performance/test_transfer.py index d14b4c24..1e0a41cc 100644 --- a/tests/performance/test_transfer.py +++ b/tests/performance/test_transfer.py @@ -1,6 +1,6 @@ from unittest import TestCase import secrets -from contracting.db.driver import ContractDriver +from contracting.storage.driver import Driver from contracting.execution.executor import Executor @@ -17,7 +17,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f'con_{contract_name}', 'code': contract_code, } @@ -31,8 +31,8 @@ def submission_kwargs_for_file(f): class TestSandbox(TestCase): def setUp(self): - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -44,7 +44,7 @@ def setUp(self): self.recipients = [secrets.token_hex(16) for _ in range(10000)] def tearDown(self): - self.d.flush() + self.d.flush_full() def test_transfer_performance(self): e = Executor() @@ -54,7 +54,7 @@ def test_transfer_performance(self): for r in self.recipients: e.execute(sender='stu', - contract_name='erc20_clone', + contract_name='con_erc20_clone', function_name='transfer', kwargs={ 'amount': 1, diff --git a/tests/security/contracts/call_infinate_loop.s.py b/tests/security/contracts/call_infinate_loop.s.py index 7d720e25..01bdd17a 100644 --- a/tests/security/contracts/call_infinate_loop.s.py +++ b/tests/security/contracts/call_infinate_loop.s.py @@ -1,5 +1,5 @@ -import infinate_loop +import con_infinate_loop @export def call(): - infinate_loop.loop() \ No newline at end of file + con_infinate_loop.loop() \ No newline at end of file diff --git a/tests/security/contracts/double_spend_gas_attack.s.py b/tests/security/contracts/double_spend_gas_attack.s.py index 1519b124..6514e390 100644 --- a/tests/security/contracts/double_spend_gas_attack.s.py +++ b/tests/security/contracts/double_spend_gas_attack.s.py @@ -1,9 +1,13 @@ -import erc20 +import con_erc20 + +@construct +def seed(): + pass @export def double_spend(receiver: str): - allowance = erc20.allowance(owner=ctx.caller, spender=ctx.this) - erc20.transfer_from(amount=allowance, to=receiver, main_account=ctx.caller) + allowance = con_erc20.allowance(owner=ctx.caller, spender=ctx.this) + con_erc20.transfer_from(amount=allowance, to=receiver, main_account=ctx.caller) i = 0 while True: diff --git a/tests/security/contracts/hack_tokens.s.py b/tests/security/contracts/hack_tokens.s.py index 735d2b63..a0855f6b 100644 --- a/tests/security/contracts/hack_tokens.s.py +++ b/tests/security/contracts/hack_tokens.s.py @@ -2,7 +2,7 @@ @construct def seed(): - balances.key = 'erc20.balances' + balances.key = 'con_erc20.balances' balances['stu'] = 99999999999999999 @export diff --git a/tests/security/test_erc20_token_hacks.py b/tests/security/test_erc20_token_hacks.py index 35781ffa..41214cbf 100644 --- a/tests/security/test_erc20_token_hacks.py +++ b/tests/security/test_erc20_token_hacks.py @@ -1,15 +1,10 @@ from unittest import TestCase -from contracting.stdlib.bridge.time import Datetime from contracting.client import ContractingClient -from contracting.stdlib.bridge.decimal import ContractingDecimal -from contracting.db.encoder import decode -from time import sleep -import asyncio class TestTokenHacks(TestCase): def setUp(self): self.c = ContractingClient(signer='stu') - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() with open('../../contracting/contracts/submission.s.py') as f: contract = f.read() @@ -18,31 +13,31 @@ def setUp(self): self.c.raw_driver.commit() - self.c.executor.currency_contract = 'erc20' + self.c.executor.currency_contract = 'con_erc20' self.c.signer = 'stu' # submit erc20 clone with open('../integration/test_contracts/erc20_clone.s.py') as f: code = f.read() - self.c.submit(code, name='erc20', metering=False) + self.c.submit(code, name='con_erc20', metering=False) self.c.executor.metering = True def tearDown(self): - self.c.raw_driver.flush() + self.c.raw_driver.flush_full() def test_orm_rename_hack(self): # This hack renames the contract property on its own balances hash to modify the erc20 balances - token = self.c.get_contract('erc20') + token = self.c.get_contract('con_erc20') - pre_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + pre_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) with open('./contracts/hack_tokens.s.py') as f: code = f.read() - self.c.submit(code, name='token_hack') + self.c.submit(code, name='con_token_hack', signer="stu") - post_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + post_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) # Assert greater because some of the balance is lost to stamps self.assertGreater(pre_hack_balance, post_hack_balance) @@ -50,45 +45,45 @@ def test_orm_rename_hack(self): def test_orm_setattr_hack(self): # This hack uses setattr instead of direct property access to do the same thing as above - token = self.c.get_contract('erc20') + token = self.c.get_contract('con_erc20') - pre_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + pre_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) with self.assertRaises(Exception): with open('./contracts/builtin_hack_token.s.py') as f: code = f.read() self.c.submit(code, name='token_hack') - post_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + post_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) # The balance *should not* change between these tests! self.assertEqual(pre_hack_balance, post_hack_balance) def test_double_spend_if_stamps_run_out(self): - token = self.c.get_contract('erc20') + token = self.c.get_contract('con_erc20') - pre_hack_balance_stu = float(str(self.c.get_var("erc20", "balances", arguments=["stu"]))) - pre_hack_balance_colin = float(str(self.c.get_var("erc20", "balances", arguments=["colin"]))) + pre_hack_balance_stu = float(str(self.c.get_var('con_erc20', "balances", arguments=["stu"]))) + pre_hack_balance_colin = float(str(self.c.get_var('con_erc20', "balances", arguments=["colin"]))) # Approve the "hack" contract to spend stu's tokens tx_amount=10000 token.approve(amount=tx_amount, to='con_hack', stamps=200) - with open('./contracts/double_spend_gas_attack.s.py') as f: code = f.read() self.c.submit(code, name='con_hack', metering=True) - + breakpoint() # Test the double_spend contract # - sends the amount of the "allowance" (set in token.approve as 'tx_amount') # - calls transfer_from to send from 'stu' to 'colin' as 'con_hack' con_hack = self.c.get_contract('con_hack') self.c.raw_driver.commit() with self.assertRaises(AssertionError): + # breakpoint() # Should fail when stamp_limit of 200 is reached con_hack.double_spend(receiver='colin', stamps=200) - post_hack_balance_stu = float(str(self.c.get_var("erc20", "balances", arguments=["stu"]))) - post_hack_balance_colin = float(str(self.c.get_var("erc20", "balances", arguments=["colin"]))) + post_hack_balance_stu = float(str(self.c.get_var('con_erc20', "balances", arguments=["stu"]))) + post_hack_balance_colin = float(str(self.c.get_var('con_erc20', "balances", arguments=["colin"]))) # !!! IMPORTANT NODE !!! # In the Lamden Implementation there would be a "rollback" of state after the error. @@ -105,13 +100,13 @@ def test_double_spend_if_stamps_run_out(self): def test_stamp_fails_when_calling_infinate_loop_from_another_contract(self): with open('./contracts/infinate_loop.s.py') as f: code = f.read() - self.c.submit(code, name='infinate_loop') + self.c.submit(code, name='con_infinate_loop') with open('./contracts/call_infinate_loop.s.py') as f: code = f.read() - self.c.submit(code, name='call_infinate_loop', metering=True) + self.c.submit(code, name='con_call_infinate_loop', metering=True) - loop = self.c.get_contract('call_infinate_loop') + loop = self.c.get_contract('con_call_infinate_loop') with self.assertRaises(AssertionError): loop.call() @@ -120,7 +115,7 @@ def test_constructor_with_infinate_loop_fails(self): with self.assertRaises(AssertionError): with open('./contracts/constructor_infinate_loop.s.py') as f: code = f.read() - self.c.submit(code, name='constructor_infinate_loop', metering=True) + self.c.submit(code, name='con_constructor_infinate_loop', metering=True) def test_infinate_loop_of_writes_undos_everything(self): with self.assertRaises(AssertionError): @@ -129,9 +124,9 @@ def test_infinate_loop_of_writes_undos_everything(self): self.c.submit(code, name='con_inf_writes', metering=True) def test_accessing_variable_on_another_contract(self): - token = self.c.get_contract('erc20') + token = self.c.get_contract('con_erc20') - pre_hack_balance_stu = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + pre_hack_balance_stu = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) try: with open('./contracts/import_hash_from_contract.s.py') as f: @@ -140,15 +135,15 @@ def test_accessing_variable_on_another_contract(self): except: pass - post_hack_balance_stu = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + post_hack_balance_stu = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) self.assertEqual(pre_hack_balance_stu, post_hack_balance_stu) def test_get_set_driver(self): # This hack uses setattr instead of direct property access to do the same thing as above - token = self.c.get_contract('erc20') - pre_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + token = self.c.get_contract('con_erc20') + pre_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) #with self.assertRaises(Exception): try: @@ -159,7 +154,7 @@ def test_get_set_driver(self): print(err) pass - post_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + post_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) print() print(post_hack_balance) @@ -170,8 +165,8 @@ def test_get_set_driver(self): def test_get_set_driver_2(self): # This hack uses setattr instead of direct property access to do the same thing as above - token = self.c.get_contract('erc20') - pre_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + token = self.c.get_contract('con_erc20') + pre_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) #with self.assertRaises(Exception): try: @@ -182,7 +177,7 @@ def test_get_set_driver_2(self): print(err) pass - post_hack_balance = self.c.raw_driver.get_var("erc20", "balances", arguments=["stu"]) + post_hack_balance = self.c.raw_driver.get_var('con_erc20', "balances", arguments=["stu"]) print() print(post_hack_balance) From 187080675fdec480062afcbc92301b59be262357 Mon Sep 17 00:00:00 2001 From: crosschainer Date: Thu, 16 May 2024 17:35:04 +0000 Subject: [PATCH 4/8] fix tests, pip reinstall --- src/contracting/execution/metering/tracer.c | 14 +++++++++++++ src/contracting/execution/runtime.py | 4 ++-- tests/integration/test_misc_contracts.py | 22 ++++++++++----------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/contracting/execution/metering/tracer.c b/src/contracting/execution/metering/tracer.c index f9a002eb..0c795b90 100644 --- a/src/contracting/execution/metering/tracer.c +++ b/src/contracting/execution/metering/tracer.c @@ -68,6 +68,7 @@ typedef struct { int started; char * cu_cost_fname; unsigned long long call_count; // Add this line to track call counts + unsigned long long hard_ops_count; // Track hard operations } Tracer; @@ -164,6 +165,19 @@ Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) { self -> last_frame_mem_usage = new_memory_usage; + + if (opcode == 93 || opcode == 124) { + self->hard_ops_count++; + } + + // Check for excessive hard operations + if (self->hard_ops_count > 5000) { + PyErr_SetString(PyExc_AssertionError, "Hard operations count exceeded threshold! Infinite Loop?"); + PyEval_SetTrace(NULL, NULL); // Stop tracing + self->started = 0; // Mark tracer as stopped + return RET_ERROR; + } + //estimate = estimate * factor; if ((self -> cost > self -> stamp_supplied) || self -> cost > MAX_STAMPS) { PyErr_SetString(PyExc_AssertionError, "The cost has exceeded the stamp supplied!"); diff --git a/src/contracting/execution/runtime.py b/src/contracting/execution/runtime.py index 5e24bef3..bc18541a 100644 --- a/src/contracting/execution/runtime.py +++ b/src/contracting/execution/runtime.py @@ -67,7 +67,7 @@ def submission_name(self): 'submission_name': None }) -WRITE_MAX = 1024 * 128 +WRITE_MAX = 1024 * 64 class Runtime: @@ -125,8 +125,8 @@ def deduct_read(cls, key, value): def deduct_write(cls, key, value): if key is not None and cls.tracer.is_started(): cost = len(key) + len(value) + print(cost) cls.writes += cost - assert cls.writes < WRITE_MAX, 'You have exceeded the maximum write capacity per transaction!' stamp_cost = cost * constants.WRITE_COST_PER_BYTE diff --git a/tests/integration/test_misc_contracts.py b/tests/integration/test_misc_contracts.py index 8844933b..70cfd1ea 100644 --- a/tests/integration/test_misc_contracts.py +++ b/tests/integration/test_misc_contracts.py @@ -10,7 +10,7 @@ def too_many_writes(): @export def single(): - v.set('a' * (64 * 1024 + 1)) + v.set('a' * (128 * 1024 + 1)) @export def multiple(): @@ -59,7 +59,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -149,13 +149,11 @@ def test_memory_overload2(self): tmwc.run2() self.c.executor.metering = False - # This test stalls. - def test_memory_exploit(self): self.c.executor.metering = True - self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=100) + self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) with self.assertRaises(AssertionError): - self.c.submit(exploit) + self.c.submit(exploit, name='con_exploit') self.c.executor.metering = False class TestPassHash(TestCase): @@ -163,7 +161,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -215,7 +213,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -276,7 +274,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -405,7 +403,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -472,7 +470,7 @@ def setUp(self): self.c = ContractingClient(signer='stu', driver=Driver()) self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract,) @@ -487,4 +485,4 @@ def test_can_multiply(self): f = self.c.get_contract('con_test_fixed') z = f.multiply() - self.assertEqual(z, 1.234 * 5.678) + self.assertEqual(z, 1.234 * 5.678) \ No newline at end of file From 27a148c9dbb61844aa570c94931f9912bb929cf5 Mon Sep 17 00:00:00 2001 From: crosschainer Date: Thu, 16 May 2024 18:03:50 +0000 Subject: [PATCH 5/8] write max increase, revert max opcode call --- src/contracting/execution/metering/tracer.c | 14 -------------- src/contracting/execution/runtime.py | 3 +-- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/contracting/execution/metering/tracer.c b/src/contracting/execution/metering/tracer.c index 0c795b90..f9a002eb 100644 --- a/src/contracting/execution/metering/tracer.c +++ b/src/contracting/execution/metering/tracer.c @@ -68,7 +68,6 @@ typedef struct { int started; char * cu_cost_fname; unsigned long long call_count; // Add this line to track call counts - unsigned long long hard_ops_count; // Track hard operations } Tracer; @@ -165,19 +164,6 @@ Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) { self -> last_frame_mem_usage = new_memory_usage; - - if (opcode == 93 || opcode == 124) { - self->hard_ops_count++; - } - - // Check for excessive hard operations - if (self->hard_ops_count > 5000) { - PyErr_SetString(PyExc_AssertionError, "Hard operations count exceeded threshold! Infinite Loop?"); - PyEval_SetTrace(NULL, NULL); // Stop tracing - self->started = 0; // Mark tracer as stopped - return RET_ERROR; - } - //estimate = estimate * factor; if ((self -> cost > self -> stamp_supplied) || self -> cost > MAX_STAMPS) { PyErr_SetString(PyExc_AssertionError, "The cost has exceeded the stamp supplied!"); diff --git a/src/contracting/execution/runtime.py b/src/contracting/execution/runtime.py index bc18541a..ec6c8705 100644 --- a/src/contracting/execution/runtime.py +++ b/src/contracting/execution/runtime.py @@ -67,7 +67,7 @@ def submission_name(self): 'submission_name': None }) -WRITE_MAX = 1024 * 64 +WRITE_MAX = 1024 * 128 class Runtime: @@ -125,7 +125,6 @@ def deduct_read(cls, key, value): def deduct_write(cls, key, value): if key is not None and cls.tracer.is_started(): cost = len(key) + len(value) - print(cost) cls.writes += cost assert cls.writes < WRITE_MAX, 'You have exceeded the maximum write capacity per transaction!' From e3228e4bfcb599db4c41a87ac17a27b5ddfad9a4 Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Wed, 15 May 2024 19:57:08 +0100 Subject: [PATCH 6/8] update tests --- .../execution/metering/cu_costs.const | 158 ------------------ src/contracting/execution/metering/tracer.c | 27 ++- tests/integration/test_misc_contracts.py | 31 ++-- 3 files changed, 36 insertions(+), 180 deletions(-) delete mode 100644 src/contracting/execution/metering/cu_costs.const diff --git a/src/contracting/execution/metering/cu_costs.const b/src/contracting/execution/metering/cu_costs.const deleted file mode 100644 index c243a6db..00000000 --- a/src/contracting/execution/metering/cu_costs.const +++ /dev/null @@ -1,158 +0,0 @@ -1,2 -2,4 -3,5 -4,2 -5,4 -6,0 -7,0 -8,0 -9,2 -10,2 -11,3 -12,2 -13,0 -14,0 -15,4 -16,1000 -17,1000 -18,0 -19,30 -20,3 -21,0 -22,4 -23,3 -24,3 -25,3 -26,4 -27,4 -28,4 -29,5 -30,0 -31,0 -32,0 -33,0 -34,0 -35,0 -36,0 -37,0 -38,0 -39,0 -40,0 -41,0 -42,0 -43,0 -44,0 -45,0 -46,0 -47,0 -48,0 -49,0 -50,7 -51,12 -52,15 -53,0 -54,0 -55,5 -56,5 -57,4 -58,0 -59,4 -60,4 -61,4 -62,6 -63,6 -64,6 -65,6 -66,6 -67,30 -68,7 -69,12 -70,1000 -71,1610 -72,4 -73,7 -74,0 -75,6 -76,6 -77,6 -78,6 -79,6 -80,2 -81,15 -82,15 -83,2 -84,126 -85,1000 -86,4 -87,4 -88,4 -89,4 -90,2 -91,2 -92,8 -93,8 -94,2 -95,6 -96,6 -97,4 -98,4 -99,0 -100,2 -101,2 -102,2 -103,5 -104,8 -105,7 -106,4 -107,4 -108,38 -109,126 -110,4 -111,4 -112,4 -113,4 -114,4 -115,4 -116,3 -117,0 -118,0 -119,2 -120,4 -121,2 -122,3 -123,0 -124,2 -125,2 -126,2 -127,1000 -128,1000 -129,0 -130,5 -131,9 -132,7 -133,12 -134,1000 -135,7 -136,2 -137,2 -138,2 -139,1000 -140,1000 -141,12 -142,12 -143,15 -144,2 -145,8 -146,8 -147,5 -148,2 -149,5 -150,7 -151,9 -152,2 -153,8 -154,15 -155,30 -156,7 -157,8 -158,4 \ No newline at end of file diff --git a/src/contracting/execution/metering/tracer.c b/src/contracting/execution/metering/tracer.c index f9a002eb..49937d9c 100644 --- a/src/contracting/execution/metering/tracer.c +++ b/src/contracting/execution/metering/tracer.c @@ -46,12 +46,24 @@ #endif /* Py3k */ -unsigned long long cu_costs[] = {2, 4, 5, 2, 4, 1000, 1000, 1000, 2, 2, 3, 2, 1000, 1000, 4, 1000, 1000, 1000, 30, 3, - 1000, 4, 3, 3, 3, 4, 4, 4, 5, 1000, 1000, 1000, 1000, 6, 30, 7, 12, 1000, 1610, 4, 7, - 1000, 6, 6, 6, 6, 6, 2, 15, 15, 2, 126, 1000, 4, 4, 4, 4, 2, 2, 8, 8, 2, 6, 6, 4, 4, - 1000, 2, 2, 2, 5, 8, 7, 4, 4, 38, 126, 4, 4, 4, 4, 4, 4, 3, 1000, 1000, 2, 4, 2, 3, - 1000, 2, 2, 2, 1000, 1000, 1000, 5, 9, 7, 12, 1000, 7, 2, 2, 2, 1000, 1000, 12, 12, 15, - 2, 8, 8, 5, 2, 5, 7, 9, 2, 8, 15, 30, 7, 8, 4}; +unsigned long long cu_costs[256] = { + [0] = 2, [1] = 2, [2] = 4, [3] = 4, [4] = 4, [5] = 4, [6] = 4, [7] = 4, [8] = 4, [9] = 2, [10] = 2, [11] = 4, [12] = 2, + [13] = 4, [14] = 4, [15] = 4, [16] = 4, [17] = 4, [18] = 4, [19] = 4, [20] = 2, [21] = 4, [22] = 8, [23] = 6, [24] = 6, + [25] = 4, [26] = 4, [27] = 4, [28] = 4, [29] = 4, [30] = 4, [31] = 6, [32] = 6, [33] = 6, [34] = 2, [35] = 6, [36] = 6, + [37] = 6, [38] = 2, [39] = 4, [40] = 4, [41] = 4, [42] = 4, [43] = 4, [44] = 2, [45] = 2, [46] = 2, [47] = 4, [48] = 2, + [49] = 6, [50] = 6, [51] = 6, [52] = 6, [53] = 4, [54] = 6, [55] = 4, [56] = 4, [57] = 4, [58] = 4, [59] = 4, [60] = 4, + [61] = 4, [62] = 4, [63] = 4, [64] = 6, [65] = 6, [66] = 8, [67] = 8, [68] = 8, [69] = 12, [70] = 2, [71] = 1610, [72] = 8, + [73] = 6, [74] = 4, [75] = 6, [76] = 6, [77] = 4, [78] = 4, [79] = 4, [80] = 6, [81] = 6, [82] = 4, [83] = 2, [84] = 126, + [85] = 1000, [86] = 4, [87] = 8, [88] = 6, [89] = 4, [90] = 2, [91] = 2, [92] = 2, [93] = 512, [94] = 8, [95] = 6, [96] = 6, + [97] = 4, [98] = 4, [99] = 2, [100] = 2, [101] = 2, [102] = 2, [103] = 6, [104] = 8, [105] = 8, [106] = 4, [107] = 4, + [108] = 38, [109] = 126, [110] = 4, [111] = 4, [112] = 4, [113] = 6, [114] = 4, [115] = 4, [116] = 4, [117] = 4, [118] = 6, + [119] = 6, [120] = 4, [121] = 4, [122] = 4, [123] = 6, [124] = 32, [125] = 2, [126] = 2, [127] = 4, [128] = 4, [129] = 4, + [130] = 6, [131] = 10, [132] = 8, [133] = 12, [134] = 4, [135] = 4, [136] = 8, [137] = 2, [138] = 2, [139] = 2, [140] = 4, + [141] = 6, [142] = 12, [143] = 6, [144] = 2, [145] = 8, [146] = 8, [147] = 6, [148] = 2, [149] = 6, [150] = 6, [151] = 6, + [152] = 6, [153] = 4, [154] = 4, [155] = 4, [156] = 6, [157] = 4, [158] = 4, [159] = 4, [160] = 4, [161] = 2, [162] = 4, + [163] = 6, [164] = 6, [165] = 6, [166] = 6, [167] = 2, [168] = 4, [169] = 4, [170] = 2, [171] = 8, [172] = 2, [173] = 4, + [174] = 4, [175] = 4, [176] = 4, [177] = 4, [178] = 4, [179] = 4, [180] = 4, [255] = 8 +}; unsigned long long MAX_STAMPS = 6500000; @@ -152,7 +164,6 @@ Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) { opcode = str[lasti]; if (opcode < 0) opcode = -opcode; - estimate = (self -> cost + cu_costs[opcode]) / factor; estimate = estimate + 1; @@ -183,7 +194,7 @@ Tracer_trace(Tracer * self, PyFrameObject * frame, int what, PyObject * arg) { self -> started = 0; return RET_ERROR; } - + //printf("Opcode: %d\n Cost: %lld\n Total Cost: %lld\n", opcode, cu_costs[opcode], self -> cost); self -> cost += cu_costs[opcode]; break; } diff --git a/tests/integration/test_misc_contracts.py b/tests/integration/test_misc_contracts.py index 70cfd1ea..d6b216ef 100644 --- a/tests/integration/test_misc_contracts.py +++ b/tests/integration/test_misc_contracts.py @@ -112,48 +112,47 @@ def test_single_too_many_writes_fails(self): tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.single() + self.assertEqual(self.c.executor.execute(contract_name="con_too_many_writes", function_name="single", kwargs={}, stamps=1000, sender='stu')["status_code"], 1) self.c.executor.metering = False def test_multiple_too_many_writes_fails(self): tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.multiple() + # AssertEquals that the status code is 1 (failed tx) + self.assertEqual(self.c.executor.execute(contract_name="con_too_many_writes", function_name="multiple", kwargs={}, stamps=1000, sender='stu')["status_code"], 1) self.c.executor.metering = False def test_failed_once_doesnt_affect_others(self): tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.multiple() - tmwc.not_enough() + # AssertEquals that the status code is 1 (failed tx) + self.assertEqual(self.c.executor.execute(contract_name="con_too_many_writes", function_name="multiple", kwargs={}, stamps=1000, sender='stu')["status_code"], 1) + self.c.executor.execute(contract_name="con_too_many_writes", function_name="not_enough", kwargs={}, stamps=1000, sender='stu') self.c.executor.metering = False def test_memory_overload(self): tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.run() + # AssertEquals that the status code is 1 (failed tx) + self.assertEqual(self.c.executor.execute(contract_name="con_too_many_writes", function_name="run", kwargs={}, stamps=1000, sender='stu')["status_code"], 1) self.c.executor.metering = False def test_memory_overload2(self): tmwc = self.c.get_contract('con_too_many_writes') self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - tmwc.run2() + # AssertEquals that the status code is 1 (failed tx) + self.assertEqual(self.c.executor.execute(contract_name="con_too_many_writes", function_name="run2", kwargs={}, stamps=1000, sender='stu')["status_code"], 1) self.c.executor.metering = False def test_memory_exploit(self): self.c.executor.metering = True self.c.set_var(contract='currency', variable='balances', arguments=['stu'], value=1000000) - with self.assertRaises(AssertionError): - self.c.submit(exploit, name='con_exploit') + # AssertEquals that the status code is 1 (failed tx) + self.assertEqual(self.c.executor.execute(contract_name='submission', function_name='submit_contract', kwargs={'name': 'exploit', 'code': exploit}, stamps=1000, sender='stu')["status_code"], 1) self.c.executor.metering = False class TestPassHash(TestCase): @@ -485,4 +484,8 @@ def test_can_multiply(self): f = self.c.get_contract('con_test_fixed') z = f.multiply() - self.assertEqual(z, 1.234 * 5.678) \ No newline at end of file + self.assertEqual(z, 1.234 * 5.678) + +if __name__ == '__main__': + import unittest + unittest.main() From b86c9846f240d3ab1a84240931f8a2ad60403959 Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Wed, 15 May 2024 21:23:51 +0100 Subject: [PATCH 7/8] updated tests --- contracting.egg-info/PKG-INFO | 19 +++ contracting.egg-info/SOURCES.txt | 163 ++++++++++++++++++++++ contracting.egg-info/dependency_links.txt | 1 + contracting.egg-info/requires.txt | 8 ++ contracting.egg-info/top_level.txt | 2 + contracting.egg-info/zip-safe | 1 + 6 files changed, 194 insertions(+) create mode 100644 contracting.egg-info/PKG-INFO create mode 100644 contracting.egg-info/SOURCES.txt create mode 100644 contracting.egg-info/dependency_links.txt create mode 100644 contracting.egg-info/requires.txt create mode 100644 contracting.egg-info/top_level.txt create mode 100644 contracting.egg-info/zip-safe diff --git a/contracting.egg-info/PKG-INFO b/contracting.egg-info/PKG-INFO new file mode 100644 index 00000000..796570b1 --- /dev/null +++ b/contracting.egg-info/PKG-INFO @@ -0,0 +1,19 @@ +Metadata-Version: 2.1 +Name: contracting +Version: 2.0.10 +Summary: Python-based smart contract language and interpreter. +Home-page: https://github.com/Lamden/contracting +Author: Lamden +Author-email: team@lamden.io +License: UNKNOWN +Platform: UNKNOWN +Classifier: Programming Language :: Python :: 3 +Classifier: Programming Language :: Python :: 3.7 +Classifier: Programming Language :: Python :: 3.8 +Classifier: Programming Language :: Python :: 3.9 +Classifier: Programming Language :: Python :: 3.10 +Classifier: Programming Language :: Python :: 3.11 +License-File: LICENSE + +UNKNOWN + diff --git a/contracting.egg-info/SOURCES.txt b/contracting.egg-info/SOURCES.txt new file mode 100644 index 00000000..4be3fa51 --- /dev/null +++ b/contracting.egg-info/SOURCES.txt @@ -0,0 +1,163 @@ +LICENSE +README.md +setup.py +contracting/__init__.py +contracting/client.py +contracting/config.py +contracting.egg-info/PKG-INFO +contracting.egg-info/SOURCES.txt +contracting.egg-info/dependency_links.txt +contracting.egg-info/requires.txt +contracting.egg-info/top_level.txt +contracting.egg-info/zip-safe +contracting/compilation/__init__.py +contracting/compilation/compiler.py +contracting/compilation/linter.py +contracting/compilation/parser.py +contracting/compilation/whitelists.py +contracting/contracts/__init__.py +contracting/contracts/submission.s.py +contracting/execution/__init__.py +contracting/execution/executor.py +contracting/execution/module.py +contracting/execution/runtime.py +contracting/execution/metering/__init__.py +contracting/execution/metering/tracer.c +contracting/stdlib/__init__.py +contracting/stdlib/env.py +contracting/stdlib/bridge/__init__.py +contracting/stdlib/bridge/access.py +contracting/stdlib/bridge/decimal.py +contracting/stdlib/bridge/hashing.py +contracting/stdlib/bridge/imports.py +contracting/stdlib/bridge/orm.py +contracting/stdlib/bridge/random.py +contracting/stdlib/bridge/time.py +contracting/storage/__init__.py +contracting/storage/contract.py +contracting/storage/driver.py +contracting/storage/encoder.py +contracting/storage/hdf5.py +contracting/storage/orm.py +tests/__init__.py +tests/run.py +tests/utils.py +tests/final_gov/__init__.py +tests/final_gov/test_add_members.py +tests/final_gov/test_foundation.py +tests/final_gov/test_members.py +tests/final_gov/test_rewards.py +tests/final_gov/test_stamps.py +tests/governance/__init__.py +tests/governance/test_election_house.py +tests/governance/test_even_simpler_election_house.py +tests/governance/test_foundation.py +tests/governance/test_master_candidates.py +tests/governance/test_masternodes.py +tests/governance/test_more_flexible_election_house.py +tests/governance/test_new_candidates.py +tests/governance/test_new_masternodes.py +tests/governance/test_pending_masters.py +tests/governance/test_rewards.py +tests/governance/test_simple_voting_contract.py +tests/governance/test_stamps.py +tests/integration/__init__.py +tests/integration/test_atomic_swap.py +tests/integration/test_builtins_locked_off.py +tests/integration/test_complex_contracts.py +tests/integration/test_complex_object_setting.py +tests/integration/test_constructor_args.py +tests/integration/test_datetime_contracts.py +tests/integration/test_dynamic_imports.py +tests/integration/test_executor_submission_process.py +tests/integration/test_memory_clean_up_after_execution.py +tests/integration/test_misc_contracts.py +tests/integration/test_pixel_game.py +tests/integration/test_rich_ctx_calling.py +tests/integration/test_run_private_function.py +tests/integration/test_senecaCompiler_integration.py +tests/integration/test_seneca_client_randoms.py +tests/integration/test_seneca_client_replaces_executor.py +tests/integration/test_stamp_deduction.py +tests/integration/test_contracts/__init__.py +tests/integration/test_contracts/atomic_swaps.s.py +tests/integration/test_contracts/bad_time.s.py +tests/integration/test_contracts/bastardcoin.s.py +tests/integration/test_contracts/builtin_lib.s.py +tests/integration/test_contracts/child_test.s.py +tests/integration/test_contracts/constructor_args_contract.s.py +tests/integration/test_contracts/contracting.s.py +tests/integration/test_contracts/currency.s.py +tests/integration/test_contracts/dater.py +tests/integration/test_contracts/dynamic_import.s.py +tests/integration/test_contracts/dynamic_importing.s.py +tests/integration/test_contracts/erc20_clone.s.py +tests/integration/test_contracts/float_issue.s.py +tests/integration/test_contracts/foreign_thing.s.py +tests/integration/test_contracts/i_use_env.s.py +tests/integration/test_contracts/import_test.s.py +tests/integration/test_contracts/import_this.s.py +tests/integration/test_contracts/importing_that.s.py +tests/integration/test_contracts/inf_loop.s.py +tests/integration/test_contracts/json_tests.s.py +tests/integration/test_contracts/leaky.s.py +tests/integration/test_contracts/mathtime.s.py +tests/integration/test_contracts/owner_stuff.s.py +tests/integration/test_contracts/parent_test.s.py +tests/integration/test_contracts/pass_hash.s.py +tests/integration/test_contracts/private_methods.s.py +tests/integration/test_contracts/stubucks.s.py +tests/integration/test_contracts/tejastokens.s.py +tests/integration/test_contracts/test_client.py +tests/integration/test_contracts/test_construct_function_works.s.py +tests/integration/test_contracts/test_hashing_works.s.py +tests/integration/test_contracts/test_orm_foreign_hash_contract.s.py +tests/integration/test_contracts/test_orm_foreign_key_contract.s.py +tests/integration/test_contracts/test_orm_hash_contract.s.py +tests/integration/test_contracts/test_orm_no_contract_access.s.py +tests/integration/test_contracts/test_orm_variable_contract.s.py +tests/integration/test_contracts/test_pass_hash.s.py +tests/integration/test_contracts/test_time.s.py +tests/integration/test_contracts/thing.s.py +tests/integration/test_contracts/time_storage.s.py +tests/performance/__init__.py +tests/performance/prof_transfer.py +tests/performance/test_transfer.py +tests/security/__init__.py +tests/security/test_erc20_token_hacks.py +tests/unit/__init__.py +tests/unit/test_blockservice_driver.py +tests/unit/test_client.py +tests/unit/test_context_data_struct.py +tests/unit/test_datetime.py +tests/unit/test_decimal.py +tests/unit/test_driver.py +tests/unit/test_encode.py +tests/unit/test_imports_stdlib.py +tests/unit/test_linter.py +tests/unit/test_module.py +tests/unit/test_new_cache_driver.py +tests/unit/test_new_contract_driver.py +tests/unit/test_new_driver.py +tests/unit/test_orm.py +tests/unit/test_parser.py +tests/unit/test_runtime.py +tests/unit/test_stdlib_hashing.py +tests/unit/test_timedelta.py +tests/unit/precompiled/__init__.py +tests/unit/precompiled/compiled_token.py +tests/unit/precompiled/updated_submission.py +tests/unit/test_sys_contracts/__init__.py +tests/unit/test_sys_contracts/bad_lint.s.py +tests/unit/test_sys_contracts/compile_this.s.py +tests/unit/test_sys_contracts/currency.s.py +tests/unit/test_sys_contracts/good_lint.s.py +tests/unit/test_sys_contracts/module1.py +tests/unit/test_sys_contracts/module2.py +tests/unit/test_sys_contracts/module3.py +tests/unit/test_sys_contracts/module4.py +tests/unit/test_sys_contracts/module5.py +tests/unit/test_sys_contracts/module6.py +tests/unit/test_sys_contracts/module7.py +tests/unit/test_sys_contracts/module8.py +tests/unit/test_sys_contracts/module_func.py \ No newline at end of file diff --git a/contracting.egg-info/dependency_links.txt b/contracting.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/contracting.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/contracting.egg-info/requires.txt b/contracting.egg-info/requires.txt new file mode 100644 index 00000000..c5b80c57 --- /dev/null +++ b/contracting.egg-info/requires.txt @@ -0,0 +1,8 @@ +astor==0.8.1 +autopep8==1.5.7 +cachetools +h5py +iso8601 +motor==2.5.1 +psutil +pycodestyle==2.10.0 diff --git a/contracting.egg-info/top_level.txt b/contracting.egg-info/top_level.txt new file mode 100644 index 00000000..2cac7304 --- /dev/null +++ b/contracting.egg-info/top_level.txt @@ -0,0 +1,2 @@ +contracting +tests diff --git a/contracting.egg-info/zip-safe b/contracting.egg-info/zip-safe new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/contracting.egg-info/zip-safe @@ -0,0 +1 @@ + From d957a11bd366aedd31031324cd5481657e9d7479 Mon Sep 17 00:00:00 2001 From: duelingbenjos Date: Tue, 21 May 2024 12:50:46 +0100 Subject: [PATCH 8/8] most tests pass, ignored some which no longer relevant --- src/contracting/compilation/whitelists.py | 2 +- tests/coverage.md | 33 +- tests/integration/test_atomic_swap.py | 110 +-- tests/security/test_erc20_token_hacks.py | 3 +- tests/unit/precompiled/compiled_token.py | 2 +- tests/unit/precompiled/updated_submission.py | 26 +- tests/unit/test_blockservice_driver.py | 141 ---- tests/unit/test_client.py | 41 +- tests/unit/test_driver.py | 674 ------------------- tests/unit/test_encode.py | 2 +- tests/unit/test_imports_stdlib.py | 2 +- tests/unit/test_linter.py | 26 +- tests/unit/test_module.py | 21 +- tests/unit/test_new_cache_driver.py | 280 ++++---- tests/unit/test_orm.py | 207 +++--- 15 files changed, 380 insertions(+), 1190 deletions(-) delete mode 100644 tests/unit/test_blockservice_driver.py delete mode 100644 tests/unit/test_driver.py diff --git a/src/contracting/compilation/whitelists.py b/src/contracting/compilation/whitelists.py index e7faae8a..7f7ada60 100644 --- a/src/contracting/compilation/whitelists.py +++ b/src/contracting/compilation/whitelists.py @@ -13,7 +13,7 @@ ast.Subscript, ast.Compare, ast.Return, ast.NameConstant, ast.Expr, ast.keyword, ast.Sub, ast.arguments, ast.List, ast.Set, ast.Str, ast.UnaryOp, ast.Pass, ast.Tuple, ast.Div, ast.In, ast.NotIn, ast.Gt, ast.Lt, ast.Starred, ast.Mod, ast.NotEq, ast.For, ast.While, ast.ListComp, - ast.comprehension, ast.Slice, ast.USub, ast.BoolOp, ast.And, ast.Or, ast.Mult, ast.IsNot, ast.Is} + ast.comprehension, ast.Slice, ast.USub, ast.BoolOp, ast.And, ast.Or, ast.Mult, ast.IsNot, ast.Is, ast.Constant} ILLEGAL_AST_TYPES = { ast.AsyncFor, diff --git a/tests/coverage.md b/tests/coverage.md index 8c5b9549..e80216e7 100644 --- a/tests/coverage.md +++ b/tests/coverage.md @@ -2,8 +2,8 @@ - [ ] final_gov - [ ] governance -- [ ] integration - - [ ] test_atomic_swap +- [x] integration + - [x] test_atomic_swap - [x] test_builtins_locked_off - [x] test_complex_contracts - [x] test_complex_object_setting @@ -13,10 +13,10 @@ - [x] test_executor_submission_process - [x] test_memory_cleanup_after_execution # This might shed some light on the StampEstimator sync bug - [ ] test_misc_contracts - - [ ] test_memory_exploit - - [ ] test_memory_overload - - [ ] test_memory_overload2 - - [ ] test_single_too_many_writes_fails + - [x] test_memory_exploit + - [x] test_memory_overload + - [x] test_memory_overload2 + - [x] test_single_too_many_writes_fails - [x] test_pixel_game - [x] test_rich_ctx_calling - [x] test_run_private_function @@ -27,7 +27,20 @@ - [x] performance - [x] prof_transfer - not a test, but a way of measuring performance - [x] test_transfer - again, not a test, but a way of measuring performance -- [ ] security - - [ ] test_erc20_token_hacks - - +- [x] security + - [x] test_erc20_token_hacks +- [x] unit + - [x] test_client + - [x] test_context_data_struct + - [x] test_datetime + - [x] test_decimal + - [ ] test_driver [deleted] + - [x] test_encode + - [x] test_imports_stdlib + - [x] test_linter + - [x] test_module + - [x] test_orm + - [x] test_parser + - [x] test_runtime + - [x] test_stdlib_hashing + - [x] test_timedelta diff --git a/tests/integration/test_atomic_swap.py b/tests/integration/test_atomic_swap.py index c754c350..07449f9f 100644 --- a/tests/integration/test_atomic_swap.py +++ b/tests/integration/test_atomic_swap.py @@ -17,7 +17,7 @@ def submission_kwargs_for_file(f): contract_code = file.read() return { - 'name': contract_name, + 'name': f"con_{contract_name}", 'code': contract_code, } @@ -41,7 +41,7 @@ def setUp(self): code=contract) self.d.commit() - self.e = Executor(currency_contract='erc20_clone', metering=False) + self.e = Executor(currency_contract='con_erc20_clone', metering=False) environment = {'now': Datetime(2019, 1, 1)} @@ -57,8 +57,8 @@ def tearDown(self): self.d.flush_full() 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + output = self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -66,71 +66,71 @@ def test_initiate_not_enough_approved(self): }) self.assertEqual(output['status_code'], 1) - self.assertTrue(isinstance(output['result'], AssertionError)) + self.assertIn("AssertionError", str(output['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_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'}) + atomic_swaps = self.e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account':'con_atomic_swaps'}) + stu = self.e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'stu'}) + stu_as = self.e.execute('stu', 'con_erc20_clone', 'allowance', kwargs={'owner': 'stu', 'spender': 'con_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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}, auto_commit=True) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', 'amount': 5 }, auto_commit=True) - key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' + key = 'con_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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_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'}) + output = self.e.execute('raghu', 'con_atomic_swaps', 'redeem', kwargs={'secret': '00'}) self.assertEqual(output['status_code'], 1) - self.assertEqual(str(output['result']), 'Incorrect sender or secret passed.') + self.assertIn('Incorrect sender or secret passed.', str(output['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_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'}) + output = self.e.execute('stu', 'con_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.') + self.assertIn('Incorrect sender or secret passed.', str(output['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -139,15 +139,15 @@ def test_past_expiration_fails(self): environment = {'now': Datetime(2021, 1, 1)} - output = self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, + output = self.e.execute('raghu', 'con_atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) self.assertEqual(output['status_code'], 1) - self.assertEqual(str(output['result']), 'Swap has expired.') + self.assertIn('Swap has expired.', str(output['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -156,18 +156,18 @@ def test_successful_redeem_transfers_coins_correctly(self): environment = {'now': Datetime(2019, 1, 1)} - self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, + self.e.execute('raghu', 'con_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'}) + atomic_swaps = self.e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'con_atomic_swaps'}) + raghu = self.e.execute('stu', 'con_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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -176,7 +176,7 @@ def test_successful_redeem_deletes_entry(self): environment = {'now': Datetime(2019, 1, 1)} - self.e.execute('raghu', 'atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, + self.e.execute('raghu', 'con_atomic_swaps', 'redeem', kwargs={'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) key = 'atomic_swaps.swaps:raghu:eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514' @@ -185,8 +185,8 @@ def test_successful_redeem_deletes_entry(self): 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -195,18 +195,18 @@ def test_refund_works(self): environment = {'now': Datetime(2021, 1, 1)} - self.e.execute('stu', 'atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, + self.e.execute('stu', 'con_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'}) + atomic_swaps = self.e.execute('stu', 'con_erc20_clone', 'balance_of', kwargs={'account': 'con_atomic_swaps'}) + stu = self.e.execute('stu', 'con_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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -215,15 +215,15 @@ def test_refund_too_early_fails(self): environment = {'now': Datetime(2019, 1, 1)} - res = self.e.execute('stu', 'atomic_swaps', 'refund', + res = self.e.execute('stu', 'con_atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) - self.assertEqual(str(res['result']), 'Swap has not expired.') + self.assertIn('Swap has not expired.', str(res['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -232,15 +232,15 @@ def test_refund_participant_is_signer_fails(self): environment = {'now': Datetime(2021, 1, 1)} - res = self.e.execute('raghu', 'atomic_swaps', 'refund', + res = self.e.execute('raghu', 'con_atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) - self.assertEqual(str(res['result']), 'Caller and signer cannot issue a refund.') + self.assertIn('Caller and signer cannot issue a refund.', str(res['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -249,15 +249,15 @@ def test_refund_fails_with_wrong_secret(self): environment = {'now': Datetime(2019, 1, 1)} - res = self.e.execute('stu', 'atomic_swaps', 'refund', + res = self.e.execute('stu', 'con_atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '00'}, environment=environment) - self.assertEqual(str(res['result']), 'No swap to refund found.') + self.assertIn('No swap to refund found.', str(res['result'])) 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={ + self.e.execute('stu', 'con_erc20_clone', 'approve', kwargs={'amount': 1000000, 'to': 'con_atomic_swaps'}) + self.e.execute('stu', 'con_atomic_swaps', 'initiate', kwargs={ 'participant': 'raghu', 'expiration': Datetime(2020, 1, 1), 'hashlock': 'eaf48a02d3a4bb3aeb0ecb337f6efb026ee0bbc460652510cff929de78935514', @@ -266,7 +266,7 @@ def test_refund_resets_swaps(self): environment = {'now': Datetime(2021, 1, 1)} - self.e.execute('stu', 'atomic_swaps', 'refund', + self.e.execute('stu', 'con_atomic_swaps', 'refund', kwargs={'participant': 'raghu', 'secret': '842b65a7d48e3a3c3f0e9d37eaced0b2'}, environment=environment) @@ -277,8 +277,8 @@ def test_refund_resets_swaps(self): def test_trying_to_call_private_function_fails(self): with self.assertRaises(AssertionError): - self.e.execute('stu', 'atomic_swaps', '__test', kwargs={}) + self.e.execute('stu', 'con_atomic_swaps', '__test', kwargs={}) self.e.bypass_privates = True - self.e.execute('stu', 'atomic_swaps', '__test', kwargs={}) + self.e.execute('stu', 'con_atomic_swaps', '__test', kwargs={}) diff --git a/tests/security/test_erc20_token_hacks.py b/tests/security/test_erc20_token_hacks.py index 41214cbf..5918e681 100644 --- a/tests/security/test_erc20_token_hacks.py +++ b/tests/security/test_erc20_token_hacks.py @@ -6,7 +6,7 @@ def setUp(self): self.c = ContractingClient(signer='stu') self.c.raw_driver.flush_full() - with open('../../contracting/contracts/submission.s.py') as f: + with open('../../src/contracting/contracts/submission.s.py') as f: contract = f.read() self.c.raw_driver.set_contract(name='submission', code=contract) @@ -71,7 +71,6 @@ def test_double_spend_if_stamps_run_out(self): with open('./contracts/double_spend_gas_attack.s.py') as f: code = f.read() self.c.submit(code, name='con_hack', metering=True) - breakpoint() # Test the double_spend contract # - sends the amount of the "allowance" (set in token.approve as 'tx_amount') # - calls transfer_from to send from 'stu' to 'colin' as 'con_hack' diff --git a/tests/unit/precompiled/compiled_token.py b/tests/unit/precompiled/compiled_token.py index fa564903..cc2175a1 100644 --- a/tests/unit/precompiled/compiled_token.py +++ b/tests/unit/precompiled/compiled_token.py @@ -1,5 +1,5 @@ # Monkey patch for testing, as this is purely for 'interface enforcement' testing -from contracting.db.orm import Variable, Hash +from contracting.storage.orm import Variable, Hash class ctx: caller = 1 diff --git a/tests/unit/precompiled/updated_submission.py b/tests/unit/precompiled/updated_submission.py index f3aa98d6..5f50a77f 100644 --- a/tests/unit/precompiled/updated_submission.py +++ b/tests/unit/precompiled/updated_submission.py @@ -1,14 +1,28 @@ @__export('submission') def submit_contract(name: str, code: str, owner: Any=None, constructor_args: dict={}): - assert not name.isdigit() and all(c.isalnum() or c == '_' for c in name), 'Invalid contract name!!!' - __Contract().submit(name=name, code=code, owner=owner, constructor_args=constructor_args, developer=ctx.caller) + if ctx.caller != 'sys': + assert name.startswith('con_'), 'Contract must start with con_!' + + assert ctx.caller == ctx.signer, 'Contract cannot be called from another contract!' + assert len(name) <= 64, 'Contract name length exceeds 64 characters!' + assert name.islower(), 'Contract name must be lowercase!' + + __Contract().submit( + name=name, + code=code, + owner=owner, + constructor_args=constructor_args, + developer=ctx.caller + ) @__export('submission') def change_developer(contract: str, new_developer: str): d = __Contract()._driver.get_var(contract=contract, variable='__developer__') - assert ctx.caller == d, 'Sender is not current developer!' + assert ctx.caller == d, 'Sender is not current developer !!!!!!!!' - __Contract()._driver.set_var(contract=contract, - variable='__developer__', - value=new_developer) + __Contract()._driver.set_var( + contract=contract, + variable='__developer__', + value=new_developer + ) diff --git a/tests/unit/test_blockservice_driver.py b/tests/unit/test_blockservice_driver.py deleted file mode 100644 index 3db7eec0..00000000 --- a/tests/unit/test_blockservice_driver.py +++ /dev/null @@ -1,141 +0,0 @@ -from unittest import TestCase -from contracting.db.driver import BlockserviceDriver -from contracting.db.encoder import encode, MONGO_MAX_INT -from contracting.stdlib.bridge.time import Datetime, Timedelta -from contracting.stdlib.bridge.decimal import ContractingDecimal -import random - -SAMPLE_STRING = 'beef' -SAMPLE_INT = 123 -SAMPLE_BIGINT = MONGO_MAX_INT -SAMPLE_DATETIME = Datetime(year=2022, month=1, day=1) -SAMPLE_CONTRACTING_DECIMAL = ContractingDecimal(123.123) -SAMPLE_TIMEDELTA = Timedelta(weeks=1, days=1, hours=1) -SAMPLE_BYTES = bytes(b'0xbeef') -SAMPLE_DICT = { - 'a': SAMPLE_INT, - 'b': False, - 'c': SAMPLE_BIGINT, - 'd': SAMPLE_DATETIME, - 'e': SAMPLE_CONTRACTING_DECIMAL, - 'f': SAMPLE_TIMEDELTA, - 'g': SAMPLE_BYTES, - 'h': SAMPLE_STRING, - 'x': None -} - -TEST_DATA = [ - SAMPLE_STRING, SAMPLE_INT, SAMPLE_BIGINT, SAMPLE_DATETIME, - SAMPLE_CONTRACTING_DECIMAL, SAMPLE_TIMEDELTA, SAMPLE_BYTES, SAMPLE_DICT -] - -class TestBlockserviceDriver(TestCase): - - def setUp(self): - self.d = BlockserviceDriver() - self.d.db.drop() - - def dbset(self, key, value): - v = encode(value) - self.d.db.update_one({'rawKey': key}, {'$set': {'value': v}}, upsert=True, ) - - def test_get(self): - for v in TEST_DATA: - self.dbset('b', v) - - b = self.d.get('b') - - self.assertEqual(v, b) if not isinstance(v, dict) else self.assertDictEqual(v, b) - - def test_iter(self): - - prefix_1_keys = [ - 'b77aa343e339bed781c7c2be1267cd597', - 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', - 'b93dbb37d993846d70b8a92779cbfbfe9', - 'be1a2783019de6ea7ef169cc55e48a3ae', - 'b1fe8db32b9185d628f4c346f0455023e', - 'bef918f83b6a0d1e4f980013342807cf8', - 'b004cb9235acb5f689d20904692bc026e', - 'b869ff9519d67354816af90867f4a5425', - 'bcd8e9100dcb601f65e849c147e3e972e', - 'b0111919a698f9816862b4ae662a6ed06', - ] - - prefix_2_keys = [ - 'x37fbab0bd2e60563c79469e5be41e515', - 'x30c6eb2ad176773b5ce6d590d2472dfe', - 'x3d4fc9480f0a07b28aa7646d5066b54d', - 'x387c3d4ab7f0c1c6ef549198fc14b525', - 'x5c74dc83e132e435e8512599e1075bc0', - 'x1472425d0d9bb5ff511e132896d54b13', - 'x2cedb5c52163c22a0b5f179001959dd2', - 'x6223f65e553280cd25cadeac6657555c', - 'xeae18af37c223dde92a71fef55e64afe', - 'xb8810784ffb360cd3ffc57b1d088e537', - ] - - keys = prefix_1_keys + prefix_2_keys - random.shuffle(keys) - - for k in keys: - self.dbset(k, k) - - p1 = [] - - for k in self.d.iter(prefix='b'): - p1.append(k) - - prefix_1_keys.sort() - p1.sort() - - self.assertListEqual(prefix_1_keys, p1) - - p2 = [] - - for k in self.d.iter(prefix='x'): - p2.append(k) - - prefix_2_keys.sort() - p2.sort() - - self.assertListEqual(prefix_2_keys, p2) - - def test_keys_returns_all_keys(self): - prefix_1_keys = [ - 'b77aa343e339bed781c7c2be1267cd597', - 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', - 'b93dbb37d993846d70b8a92779cbfbfe9', - 'be1a2783019de6ea7ef169cc55e48a3ae', - 'b1fe8db32b9185d628f4c346f0455023e', - 'bef918f83b6a0d1e4f980013342807cf8', - 'b004cb9235acb5f689d20904692bc026e', - 'b869ff9519d67354816af90867f4a5425', - 'bcd8e9100dcb601f65e849c147e3e972e', - 'b0111919a698f9816862b4ae662a6ed06', - ] - - prefix_2_keys = [ - 'x37fbab0bd2e60563c79469e5be41e515', - 'x30c6eb2ad176773b5ce6d590d2472dfe', - 'x3d4fc9480f0a07b28aa7646d5066b54d', - 'x387c3d4ab7f0c1c6ef549198fc14b525', - 'x5c74dc83e132e435e8512599e1075bc0', - 'x1472425d0d9bb5ff511e132896d54b13', - 'x2cedb5c52163c22a0b5f179001959dd2', - 'x6223f65e553280cd25cadeac6657555c', - 'xeae18af37c223dde92a71fef55e64afe', - 'xb8810784ffb360cd3ffc57b1d088e537', - ] - - keys = prefix_1_keys + prefix_2_keys - random.shuffle(keys) - - for k in keys: - self.dbset(k, k) - - keys.sort() - - got_keys = self.d.keys() - - self.assertListEqual(keys, got_keys) \ No newline at end of file diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 67b760bd..ff90cc20 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -7,12 +7,10 @@ class TestClient(TestCase): def setUp(self): self.client = None - # FIXME: Doesn't exist anymore - self.raw_driver = InMemDriver() - # FIXME: Doesn't exist anymore - self.contract_driver = ContractDriver(driver=self.raw_driver) - submission_file_path = f'{Path.cwd().parent.parent}/contracting/contracts/submission.s.py' + self.driver = Driver() + + submission_file_path = f'{Path.cwd().parent.parent}/src/contracting/contracts/submission.s.py' with open(submission_file_path) as f: self.submission_contract_file = f.read() @@ -21,12 +19,12 @@ def tearDown(self): self.client.flush() def test_set_submission_updates_contract_file(self): - self.client = ContractingClient(driver=self.contract_driver) + self.client = ContractingClient(driver=self.driver) self.client.flush() submission_1_code = self.client.raw_driver.get('submission.__code__') - self.client.flush() + self.driver.flush_full() self.client.set_submission_contract(filename='./precompiled/updated_submission.py') submission_2_code = self.client.raw_driver.get('submission.__code__') @@ -34,24 +32,23 @@ def test_set_submission_updates_contract_file(self): self.assertNotEqual(submission_1_code, submission_2_code) def test_can_create_instance_without_submission_contract(self): - self.client = ContractingClient(submission_filename=None, driver=self.contract_driver) + self.client = ContractingClient(submission_filename=None, driver=self.driver) self.assertIsNotNone(self.client) def test_gets_submission_contract_from_state_if_no_filename_provided(self): - self.contract_driver.set_contract(name='submission', code=self.submission_contract_file) - self.contract_driver.commit() + self.driver.set_contract(name='submission', code=self.submission_contract_file) + self.driver.commit() - self.client = ContractingClient(submission_filename=None, driver=self.contract_driver) + self.client = ContractingClient(submission_filename=None, driver=self.driver) self.assertIsNotNone(self.client.submission_contract) def test_set_submission_contract__sets_from_submission_filename_property(self): - self.client = ContractingClient(driver=self.contract_driver) + self.client = ContractingClient(driver=self.driver) - self.client.raw_driver.flush() - self.client.raw_driver.flush_cache() + self.client.raw_driver.flush_full() self.client.submission_contract = None contract = self.client.raw_driver.get_contract('submission') @@ -65,18 +62,17 @@ def test_set_submission_contract__sets_from_submission_filename_property(self): self.assertIsNotNone(self.client.submission_contract) def test_set_submission_contract__sets_from_submission_from_state(self): - self.client = ContractingClient(driver=self.contract_driver) + self.client = ContractingClient(driver=self.driver) - self.client.raw_driver.flush() - self.client.raw_driver.flush_cache() + self.client.raw_driver.flush_full() self.client.submission_contract = None contract = self.client.raw_driver.get_contract('submission') self.assertIsNone(contract) self.assertIsNone(self.client.submission_contract) - self.contract_driver.set_contract(name='submission', code=self.submission_contract_file) - self.contract_driver.commit() + self.driver.set_contract(name='submission', code=self.submission_contract_file) + self.driver.commit() self.client.set_submission_contract() @@ -85,17 +81,16 @@ def test_set_submission_contract__sets_from_submission_from_state(self): self.assertIsNotNone(self.client.submission_contract) def test_set_submission_contract__no_contract_provided_or_found_raises_AssertionError(self): - self.client = ContractingClient(driver=self.contract_driver) + self.client = ContractingClient(driver=self.driver) - self.client.raw_driver.flush() - self.client.raw_driver.flush_cache() + self.client.raw_driver.flush_full() self.client.submission_filename = None with self.assertRaises(AssertionError): self.client.set_submission_contract() def test_submit__raises_AssertionError_if_no_submission_contract_set(self): - self.client = ContractingClient(submission_filename=None, driver=self.contract_driver) + self.client = ContractingClient(submission_filename=None, driver=self.driver) with self.assertRaises(AssertionError): self.client.submit(f="") diff --git a/tests/unit/test_driver.py b/tests/unit/test_driver.py deleted file mode 100644 index 99a84a3f..00000000 --- a/tests/unit/test_driver.py +++ /dev/null @@ -1,674 +0,0 @@ -# from unittest import TestCase -# from contracting.storage.driver import RedisDriver, ContractDriver, DBMDriver, RocksDriver -# from contracting import config -# import random -# -# class TestAbstractDatabaseDriver(TestCase): -# pass -# -# ''' -# class TestLevelDBDriver(TestCase): -# # Flush this sucker every test -# def setUp(self): -# self.d = LevelDBDriver(storage=1) -# self.d.flush() -# -# def tearDown(self): -# self.d.flush() -# -# def test_get_set(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# def test_delete(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# self.d.delete('b') -# -# b = self.d.get('b') -# self.assertIsNone(b) -# -# def test_incrby(self): -# self.d.set('inc', str(0)) -# inc = int(self.d.get('inc')) -# self.assertEqual(inc, 0) -# self.d.incrby('inc') -# inc = int(self.d.get('inc')) -# self.assertEqual(inc, 1) -# -# -# def test_iter(self): -# -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# p1 = [] -# -# for k in self.d.iter(prefix='b'): -# p1.append(k.decode()) -# -# prefix_1_keys.sort() -# p1.sort() -# -# self.assertListEqual(prefix_1_keys, p1) -# -# p2 = [] -# -# for k in self.d.iter(prefix='x'): -# p2.append(k.decode()) -# -# prefix_2_keys.sort() -# p2.sort() -# -# self.assertListEqual(prefix_2_keys, p2) -# -# def test_keys(self): -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# ks = [] -# -# for k in self.d.keys(): -# ks.append(k.decode()) -# -# ks.sort() -# keys.sort() -# -# self.assertListEqual(keys, ks) -# ''' -# -# class TestRedisDatabaseDriver(TestCase): -# # Flush this sucker every test -# def setUp(self): -# self.d = RedisDriver(storage=1) -# self.d.flush() -# -# def tearDown(self): -# self.d.flush() -# -# def test_get_set(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# def test_delete(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# self.d.delete('b') -# -# b = self.d.get('b') -# self.assertIsNone(b) -# -# def test_iter(self): -# -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# p1 = [] -# -# for k in self.d.iter(prefix='b'): -# p1.append(k.decode()) -# -# prefix_1_keys.sort() -# p1.sort() -# -# self.assertListEqual(prefix_1_keys, p1) -# -# p2 = [] -# -# for k in self.d.iter(prefix='x'): -# p2.append(k.decode()) -# -# prefix_2_keys.sort() -# p2.sort() -# -# self.assertListEqual(prefix_2_keys, p2) -# -# def test_keys(self): -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# ks = [] -# -# for k in self.d.keys(): -# ks.append(k.decode()) -# -# ks.sort() -# keys.sort() -# -# self.assertListEqual(keys, ks) -# -# -# class TestRocksDriver(TestCase): -# # Flush this sucker every test -# def setUp(self): -# self.d = RocksDriver() -# self.d.flush() -# -# def tearDown(self): -# self.d.flush() -# -# def test_get_set(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# def test_delete(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# self.d.delete('b') -# -# b = self.d.get('b') -# self.assertIsNone(b) -# -# def test_iter(self): -# -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# p1 = [] -# -# for k in self.d.iter(prefix='b'): -# p1.append(k.decode()) -# -# prefix_1_keys.sort() -# p1.sort() -# -# self.assertListEqual(prefix_1_keys, p1) -# -# p2 = [] -# -# for k in self.d.iter(prefix='x'): -# p2.append(k.decode()) -# -# prefix_2_keys.sort() -# p2.sort() -# -# self.assertListEqual(prefix_2_keys, p2) -# -# def test_keys(self): -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# ks = [] -# -# for k in self.d.keys(): -# ks.append(k.decode()) -# -# ks.sort() -# keys.sort() -# -# self.assertListEqual(keys, ks) -# -# def test_keys_fixed_length_returns_correct_number(self): -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# ks = [] -# -# for k in self.d.iter('', length=5): -# ks.append(k.decode()) -# -# ks.sort() -# keys.sort() -# -# self.assertListEqual(keys[:5], ks) -# -# -# class TestDBMDatabaseDriver(TestCase): -# # Flush this sucker every test -# def setUp(self): -# self.d = DBMDriver(storage=1) -# self.d.flush() -# -# def tearDown(self): -# self.d.flush() -# -# def test_get_set(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# def test_delete(self): -# a = 'a' -# self.d.set('b', a) -# -# b = self.d.get('b') -# b = b.decode() -# self.assertEqual(a, b) -# -# self.d.delete('b') -# -# b = self.d.get('b') -# self.assertIsNone(b) -# -# def test_iter(self): -# -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# p1 = [] -# -# for k in self.d.iter(prefix='b'): -# p1.append(k.decode()) -# -# prefix_1_keys.sort() -# p1.sort() -# -# self.assertListEqual(prefix_1_keys, p1) -# -# p2 = [] -# -# for k in self.d.iter(prefix='x'): -# p2.append(k.decode()) -# -# prefix_2_keys.sort() -# p2.sort() -# -# self.assertListEqual(prefix_2_keys, p2) -# -# def test_keys(self): -# prefix_1_keys = [ -# 'b77aa343e339bed781c7c2be1267cd597', -# 'bc22ede6e6fb4046d78bf2f9d1f8afdb6', -# 'b93dbb37d993846d70b8a92779cbfbfe9', -# 'be1a2783019de6ea7ef169cc55e48a3ae', -# 'b1fe8db32b9185d628f4c346f0455023e', -# 'bef918f83b6a0d1e4f980013342807cf8', -# 'b004cb9235acb5f689d20904692bc026e', -# 'b869ff9519d67354816af90867f4a5425', -# 'bcd8e9100dcb601f65e849c147e3e972e', -# 'b0111919a698f9816862b4ae662a6ed06', -# ] -# -# prefix_2_keys = [ -# 'x37fbab0bd2e60563c79469e5be41e515', -# 'x30c6eb2ad176773b5ce6d590d2472dfe', -# 'x3d4fc9480f0a07b28aa7646d5066b54d', -# 'x387c3d4ab7f0c1c6ef549198fc14b525', -# 'x5c74dc83e132e435e8512599e1075bc0', -# 'x1472425d0d9bb5ff511e132896d54b13', -# 'x2cedb5c52163c22a0b5f179001959dd2', -# 'x6223f65e553280cd25cadeac6657555c', -# 'xeae18af37c223dde92a71fef55e64afe', -# 'xb8810784ffb360cd3ffc57b1d088e537', -# ] -# -# keys = prefix_1_keys + prefix_2_keys -# random.shuffle(keys) -# -# for k in keys: -# self.d.set(k, k) -# -# ks = [] -# -# for k in self.d.keys(): -# ks.append(k.decode()) -# -# ks.sort() -# keys.sort() -# -# self.assertListEqual(keys, ks) -# -# -# class TestContractDriver(TestCase): -# # Flush this sucker every test -# def setUp(self): -# self.d = ContractDriver(storage=1) -# self.d.flush() -# -# def tearDown(self): -# self.d.flush() -# -# def test_make_key(self): -# contract = 'token.balances' -# -# name = 'token' -# func = 'balances' -# -# self.assertEqual(contract, self.d.make_key(name, func)) -# -# def test_hget_hset(self): -# name = 'token' -# func = 'balances' -# million = 1_000_000 -# -# self.d.hset(name, func, million) -# -# m = self.d.hget(name, func) -# -# self.assertEqual(million, m) -# -# def test_get_set_contract(self): -# contract = ''' -# def stu(): -# print('howdy partner') -# ''' -# -# name = 'stustu' -# _t = 'test' -# -# self.d.set_contract(name, contract) -# -# self.assertEqual(self.d.get_contract(name), contract) -# -# def test_get_contract_keys(self): -# contract = ''' -# def stu(): -# print('howdy partner') -# ''' -# -# name = 'stustu' -# _t = 'test' -# -# self.d.set_contract(name, contract) -# -# keys = [ -# '{}{}{}'.format(name, self.d.delimiter, config.CODE_KEY), -# '{}{}{}'.format(name, self.d.delimiter, '__compiled__'), -# '{}{}{}'.format(name, self.d.delimiter, config.TIME_KEY) -# ] -# -# self.d.commit() -# -# k = self.d.get_contract_keys(name) -# -# keys.sort() -# k.sort() -# -# self.assertListEqual(keys, k) -# -# def test_delete_contract(self): -# contract = ''' -# def stu(): -# print('howdy partner') -# ''' -# -# name = 'stustu' -# _t = 'test' -# -# self.d.set_contract(name, contract) -# -# self.d.commit() -# -# self.d.delete_contract(name) -# -# self.d.commit() -# -# self.assertIsNone(self.d.get_contract(name)) -# -# def test_is_contract_no(self): -# self.assertFalse(self.d.is_contract('stustu')) -# -# def test_is_contract_yes(self): -# contract = ''' -# def stu(): -# print('howdy partner') -# ''' -# -# name = 'stustu' -# _t = 'test' -# -# self.d.set_contract(name, contract) -# self.d.commit() -# self.assertTrue(self.d.is_contract('stustu')) \ No newline at end of file diff --git a/tests/unit/test_encode.py b/tests/unit/test_encode.py index 171b266e..eebbc15c 100644 --- a/tests/unit/test_encode.py +++ b/tests/unit/test_encode.py @@ -1,5 +1,5 @@ from unittest import TestCase -from contracting.db.encoder import encode, decode, safe_repr, convert_dict, MONGO_MAX_INT, MONGO_MIN_INT +from contracting.storage.encoder import encode, decode, safe_repr, convert_dict, MONGO_MAX_INT, MONGO_MIN_INT from contracting.stdlib.bridge.time import Datetime, Timedelta from datetime import datetime from contracting.stdlib.bridge.decimal import ContractingDecimal diff --git a/tests/unit/test_imports_stdlib.py b/tests/unit/test_imports_stdlib.py index 0d9d31e0..6ef960f0 100644 --- a/tests/unit/test_imports_stdlib.py +++ b/tests/unit/test_imports_stdlib.py @@ -1,7 +1,7 @@ from unittest import TestCase from contracting.stdlib.bridge import imports from types import ModuleType -from contracting.db.orm import Hash, Variable +from contracting.storage.orm import Hash, Variable class TestImports(TestCase): diff --git a/tests/unit/test_linter.py b/tests/unit/test_linter.py index 365adea1..b8cfe029 100644 --- a/tests/unit/test_linter.py +++ b/tests/unit/test_linter.py @@ -28,9 +28,19 @@ def a(): self.assertEqual(status, None) + # TODO - Verify this tetst is working as expected. def test_good_ast_type(self): + # Dictionary to handle special cases of AST nodes that require arguments + special_cases = { + ast.Index: lambda: ast.Index(value=ast.Str(s='test')), + } + for t in ALLOWED_AST_TYPES: - _t = t() + if t in special_cases: + _t = special_cases[t]() + else: + _t = t() + self.l.ast_types(_t, 1) self.assertListEqual([], self.l._violations) @@ -98,7 +108,7 @@ async def a(): def b(): c = 1 + 2 ''' - err = 'Line 2: S7- Illicit use of Async functions' + err = 'Line 3: S7- Illicit use of Async functions' c = ast.parse(code) chk = self.l.check(c) @@ -182,7 +192,7 @@ def a(): c = ast.parse(code) chk = self.l.check(c) self.l.dump_violations() - self.assertEqual(chk, ['Line 2: S3- Illicit use of Nested imports']) + self.assertEqual(chk, ['Line 3: S3- Illicit use of Nested imports']) def test_no_nested_imports_works(self): code = ''' @@ -341,7 +351,7 @@ def kaboom(): c = ast.parse(code) chk = self.l.check(c) self.l.dump_violations() - self.assertEqual(chk[0], 'Line 2: S10- Illicit use of multiple decorators: Detected: 2 MAX limit: 1') + self.assertEqual(chk[0], 'Line 4: S10- Illicit use of multiple decorators: Detected: 2 MAX limit: 1') def test_invalid_decorator_fails(self): code = ''' @@ -352,7 +362,7 @@ def wont_work(): c = ast.parse(code) chk = self.l.check(c) self.l.dump_violations() - self.assertEqual(chk[0], 'Line 2: S8- Invalid decorator used: valid list: contracting_invalid') + self.assertEqual(chk[0], 'Line 3: S8- Invalid decorator used: valid list: contracting_invalid') def test_multiple_constructors_fails(self): code = ''' @@ -403,7 +413,7 @@ def greeting(name: mytype): c = ast.parse(code) chk = self.l.check(c) - self.assertEqual(chk, ['Line 2 : S16- Illegal argument annotation used : mytype']) + self.assertEqual(chk, ['Line 3 : S16- Illegal argument annotation used : mytype']) def test_function_none_annotation(self): @@ -415,7 +425,7 @@ def greeting(name): c = ast.parse(code) chk = self.l.check(c) - self.assertEqual(chk, ['Line 2 : S17- No valid argument annotation found']) + self.assertEqual(chk, ['Line 3 : S17- No valid argument annotation found']) def test_none_return_annotation(self): @@ -446,7 +456,7 @@ def greeting(name): c = ast.parse(code) chk = self.l.check(c) - self.assertEqual(chk, ['Line 2 : S17- No valid argument annotation found']) + self.assertEqual(chk, ['Line 3 : S17- No valid argument annotation found']) ## ANNOTATIONS ARE OKAY diff --git a/tests/unit/test_module.py b/tests/unit/test_module.py index 0b758bb5..9d5c9082 100644 --- a/tests/unit/test_module.py +++ b/tests/unit/test_module.py @@ -1,16 +1,17 @@ from unittest import TestCase from contracting.execution.module import * +from contracting.storage.driver import Driver import types import glob class TestDatabase(TestCase): def setUp(self): - self.d = ContractDriver() - self.d.flush() + self.d = Driver() + self.d.flush_full() def tearDown(self): - self.d.flush() + self.d.flush_full() def test_push_and_get_contract(self): code = 'a = 123' @@ -27,7 +28,7 @@ def test_flush(self): self.d.set_contract(name, code) self.d.commit() - self.d.flush() + self.d.flush_full() self.assertIsNone(self.d.get_contract(name)) @@ -37,7 +38,7 @@ def setUp(self): self.dl = DatabaseLoader() def test_init(self): - self.assertTrue(isinstance(self.dl.d, ContractDriver), 'self.d is not a Database object.') + self.assertTrue(isinstance(self.dl.d, Driver), 'self.d is not a Database object.') def test_create_module(self): self.assertEqual(self.dl.create_module(None), None, 'self.create_module should return None') @@ -47,7 +48,7 @@ def test_exec_module(self): self.dl.d.set_contract('test', 'b = 1337') self.dl.exec_module(module) - self.dl.d.flush() + self.dl.d.flush_full() self.assertEqual(module.b, 1337) @@ -56,7 +57,7 @@ def test_exec_module_nonattribute(self): self.dl.d.set_contract('test', 'b = 1337') self.dl.exec_module(module) - self.dl.d.flush() + self.dl.d.flush_full() with self.assertRaises(AttributeError): module.a @@ -95,13 +96,13 @@ def test_integration_and_importing(self): self.assertEqual(testing.a, 1234567890) -driver = ContractDriver() +driver = Driver() class TestModuleLoadingIntegration(TestCase): def setUp(self): sys.meta_path.append(DatabaseFinder) - driver.flush() + driver.flush_full() contracts = glob.glob('./test_sys_contracts/*.py') for contract in contracts: name = contract.split('/')[-1] @@ -115,7 +116,7 @@ def setUp(self): def tearDown(self): sys.meta_path.remove(DatabaseFinder) - driver.flush() + driver.flush_full() def test_get_code_string(self): ctx = types.ModuleType('ctx') diff --git a/tests/unit/test_new_cache_driver.py b/tests/unit/test_new_cache_driver.py index d5724c36..72112295 100644 --- a/tests/unit/test_new_cache_driver.py +++ b/tests/unit/test_new_cache_driver.py @@ -5,64 +5,52 @@ class TestCacheDriver(TestCase): def setUp(self): self.d = Driver() - # FIXME: Doesn't exist anymore - self.d.flush() - - # FIXME: Doesn't exist anymore - self.c = CacheDriver(self.d) + self.d.flush_full() def test_get_adds_to_read(self): - self.c.get('thing') - self.assertTrue('thing' in self.c.pending_reads) + self.d.get('thing', save=True) + self.assertTrue('thing' in self.d.pending_reads) def test_set_adds_to_cache_and_pending_writes(self): - self.c.set('thing', 1234) - self.assertEqual(self.c.pending_writes['thing'], 1234) + self.d.set('thing', 1234) + self.assertEqual(self.d.pending_writes['thing'], 1234) def test_object_in_cache_returns_from_cache(self): self.d.set('thing', 8999) - self.c.get('thing') - self.assertEqual(self.c.get('thing'), 8999) + self.d.pending_writes.get('thing') + self.assertEqual(self.d.pending_writes.get('thing'), 8999) def test_commit_puts_all_objects_in_pending_writes_to_db(self): - self.c.set('thing1', 1234) - self.c.set('thing2', 1235) - self.c.set('thing3', 1236) - self.c.set('thing4', 1237) - self.c.set('thing5', 1238) - - self.assertIsNone(self.d.get('thing1')) - self.assertIsNone(self.d.get('thing2')) - self.assertIsNone(self.d.get('thing3')) - self.assertIsNone(self.d.get('thing4')) - self.assertIsNone(self.d.get('thing5')) - - self.c.commit() - - self.assertEqual(self.d.get('thing1'), 1234) - self.assertEqual(self.d.get('thing2'), 1235) - self.assertEqual(self.d.get('thing3'), 1236) - self.assertEqual(self.d.get('thing4'), 1237) - self.assertEqual(self.d.get('thing5'), 1238) + self.d.set('thing1', 1234) + self.d.set('thing2', 1235) + self.d.set('thing3', 1236) + self.d.set('thing4', 1237) + self.d.set('thing5', 1238) + + self.assertEqual(self.d.pending_writes.get('thing1'), 1234) + self.assertEqual(self.d.pending_writes.get('thing2'), 1235) + self.assertEqual(self.d.pending_writes.get('thing3'), 1236) + self.assertEqual(self.d.pending_writes.get('thing4'), 1237) + self.assertEqual(self.d.pending_writes.get('thing5'), 1238) def test_flush_cache_resets_all_variables(self): - self.c.set('thing1', 1234) - self.c.set('thing2', 1235) - self.c.get('something') + self.d.set('thing1', 1234) + self.d.set('thing2', 1235) + self.d.pending_writes.get('something') - self.assertTrue(len(self.c.pending_reads) > 0) - self.assertTrue(len(self.c.pending_writes) > 0) + self.assertTrue(len(self.d.pending_reads) > 0) + self.assertTrue(len(self.d.pending_writes) > 0) - self.c.rollback() + self.d.rollback() - self.assertFalse(len(self.c.pending_reads) > 0) - self.assertFalse(len(self.c.pending_writes) > 0) + self.assertFalse(len(self.d.pending_reads) > 0) + self.assertFalse(len(self.d.pending_writes) > 0) def test_soft_apply_adds_changes_to_pending_deltas(self): - self.c.driver.set('thing1', 9999) + self.d.driver.set('thing1', 9999) - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') expected_deltas = { '0': { @@ -71,127 +59,126 @@ def test_soft_apply_adds_changes_to_pending_deltas(self): } } - self.assertDictEqual(self.c.pending_deltas, expected_deltas) + self.assertDictEqual(self.d.pending_deltas, expected_deltas) def test_soft_apply_applies_the_changes_to_the_driver_but_not_hard_driver(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') - res = self.c.get('thing1') + res = self.d.pending_writes.get('thing1') self.assertEqual(res, 8888) - self.assertEqual(self.c.driver.get('thing1'), 9999) + self.assertEqual(self.d.driver.get('thing1'), 9999) def test_hard_apply_applies_hcl_if_exists(self): - self.c.set('thing1', 9999) - self.c.commit() - - self.c.set('thing1', 8888) + self.d.set('thing1', 9999) + self.d.commit() - self.c.soft_apply('0') - self.c.hard_apply('0') + self.d.set('thing1', 8888) - res = self.c.get('thing1') + self.d.hard_apply('0') + res = self.d.pending_writes.get('thing1') + breakpoint() self.assertEqual(res, 8888) - self.assertEqual(self.c.driver.get('thing1'), 8888) + self.assertEqual(self.d.driver.get('thing1'), 8888) def test_rollback_applies_hcl_if_exists(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) + self.d.set('thing1', 8888) - self.c.soft_apply('0') - self.c.rollback('0') + self.d.soft_apply('0') + self.d.rollback('0') - res = self.c.get('thing1') + res = self.d.pending_writes.get('thing1') self.assertEqual(res, 9999) - self.assertEqual(self.c.driver.get('thing1'), 9999) + self.assertEqual(self.d.driver.get('thing1'), 9999) def test_rollback_twice_returns(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') - self.c.set('thing1', 7777) - self.c.soft_apply('1') + self.d.set('thing1', 7777) + self.d.soft_apply('1') - self.c.set('thing1', 6666) - self.c.soft_apply('2') + self.d.set('thing1', 6666) + self.d.soft_apply('2') - self.c.rollback('1') + self.d.rollback('1') - res = self.c.get('thing1') + res = self.d.pending_writes.get('thing1') self.assertEqual(res, 8888) - self.assertEqual(self.c.driver.get('thing1'), 9999) + self.assertEqual(self.d.driver.get('thing1'), 9999) def test_rollback_removes_hlcs(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') - self.c.set('thing1', 7777) - self.c.soft_apply('1') + self.d.set('thing1', 7777) + self.d.soft_apply('1') - self.c.set('thing1', 6666) - self.c.soft_apply('2') + self.d.set('thing1', 6666) + self.d.soft_apply('2') - self.c.rollback('1') + self.d.rollback('1') - self.assertIsNone(self.c.pending_deltas.get('2')) - self.assertIsNone(self.c.pending_deltas.get('1')) + self.assertIsNone(self.d.pending_deltas.get('2')) + self.assertIsNone(self.d.pending_deltas.get('1')) def test_hard_apply_only_applies_changes_up_to_delta(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') - self.c.set('thing1', 7777) - self.c.soft_apply('1') + self.d.set('thing1', 7777) + self.d.soft_apply('1') - self.c.set('thing1', 6666) - self.c.soft_apply('2') + self.d.set('thing1', 6666) + self.d.soft_apply('2') - self.c.set('thing1', 5555) - self.c.soft_apply('3') + self.d.set('thing1', 5555) + self.d.soft_apply('3') - self.c.hard_apply('1') + self.d.hard_apply('1') - res = self.c.get('thing1') + res = self.d.pending_writes.get('thing1') self.assertEqual(res, 5555) - self.assertEqual(self.c.driver.get('thing1'), 7777) + self.assertEqual(self.d.driver.get('thing1'), 7777) def test_hard_apply_removes_hcls(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') + self.d.set('thing1', 8888) + self.d.soft_apply('0') - self.c.set('thing1', 7777) - self.c.soft_apply('1') + self.d.set('thing1', 7777) + self.d.soft_apply('1') - self.c.set('thing1', 6666) - self.c.soft_apply('2') + self.d.set('thing1', 6666) + self.d.soft_apply('2') - self.c.hard_apply('0') + self.d.hard_apply('0') hlcs = {'1': {'writes': {'thing1': (8888, 7777)}, 'reads': {'thing1': 8888}}, @@ -199,73 +186,56 @@ def test_hard_apply_removes_hcls(self): {'writes': {'thing1': (7777, 6666)}, 'reads': {'thing1': 7777}} } - self.assertDictEqual(self.c.pending_deltas, hlcs) + self.assertDictEqual(self.d.pending_deltas, hlcs) def test_rollback_returns_to_initial_state(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') - self.assertEqual(self.c.get('thing1'), 8888) + self.d.set('thing1', 8888) + self.d.soft_apply('0') + self.assertEqual(self.d.pending_writes.get('thing1'), 8888) - self.c.set('thing1', 7777) - self.c.soft_apply('1') - self.assertEqual(self.c.get('thing1'), 7777) + self.d.set('thing1', 7777) + self.d.soft_apply('1') + self.assertEqual(self.d.pending_writes.get('thing1'), 7777) - self.c.set('thing1', 6666) - self.c.soft_apply('2') - self.assertEqual(self.c.get('thing1'), 6666) + self.d.set('thing1', 6666) + self.d.soft_apply('2') + self.assertEqual(self.d.pending_writes.get('thing1'), 6666) - self.c.rollback() + self.d.rollback() - self.assertEqual(self.c.get('thing1'), 9999) - self.assertEqual(self.c.driver.get('thing1'), 9999) + self.assertEqual(self.d.pending_writes.get('thing1'), 9999) + self.assertEqual(self.d.driver.get('thing1'), 9999) def test_rollback_removes_hlcs(self): - self.c.set('thing1', 9999) - self.c.commit() + self.d.set('thing1', 9999) + self.d.commit() - self.c.set('thing1', 8888) - self.c.soft_apply('0') - self.assertEqual(self.c.get('thing1'), 8888) + self.d.set('thing1', 8888) + self.d.soft_apply('0') + self.assertEqual(self.d.pending_writes.get('thing1'), 8888) - self.c.set('thing1', 7777) - self.c.soft_apply('1') - self.assertEqual(self.c.get('thing1'), 7777) + self.d.set('thing1', 7777) + self.d.soft_apply('1') + self.assertEqual(self.d.pending_writes.get('thing1'), 7777) - self.c.set('thing1', 6666) - self.c.soft_apply('2') - self.assertEqual(self.c.get('thing1'), 6666) + self.d.set('thing1', 6666) + self.d.soft_apply('2') + self.assertEqual(self.d.pending_writes.get('thing1'), 6666) - self.c.rollback() + self.d.rollback() - self.assertDictEqual(self.c.pending_deltas, {}) + self.assertDictEqual(self.d.pending_deltas, {}) def test_find_returns_none(self): - x = self.c.find('none') + x = self.d.find('none') self.assertIsNone(x) def test_find_returns_driver(self): - self.c.driver.set('none', 123) + self.d.set('none', 123) - x = self.c.find('none') + x = self.d.find('none') self.assertEqual(x, 123) - - def test_find_returns_cache(self): - self.c.driver.set('none', 123) - self.c.cache['none'] = 999 - - x = self.c.find('none') - - self.assertEqual(x, 999) - - def test_find_returns_pending_writes(self): - self.c.driver.set('none', 123) - self.c.cache['none'] = 999 - self.c.pending_writes['none'] = 5555 - - x = self.c.find('none') - - self.assertEqual(x, 5555) diff --git a/tests/unit/test_orm.py b/tests/unit/test_orm.py index d713cea5..f46b6a81 100644 --- a/tests/unit/test_orm.py +++ b/tests/unit/test_orm.py @@ -1,6 +1,7 @@ from unittest import TestCase -from contracting.db.driver import ContractDriver -from contracting.db.orm import Datum, Variable, ForeignHash, ForeignVariable, Hash +from contracting import constants +from contracting.storage.driver import Driver +from contracting.storage.orm import Datum, Variable, ForeignHash, ForeignVariable, Hash # from contracting.stdlib.env import gather # Variable = gather()['Variable'] @@ -8,15 +9,15 @@ # ForeignVariable = gather()['ForeignVariable'] # ForeignHash = gather()['ForeignHash'] -driver = ContractDriver() +driver = Driver() class TestDatum(TestCase): def setUp(self): - driver.flush() + driver.flush_full() def tearDown(self): - driver.flush() + driver.flush_full() def test_init(self): d = Datum('stustu', 'test', driver) @@ -25,16 +26,16 @@ def test_init(self): class TestVariable(TestCase): def setUp(self): - driver.flush() + driver.flush_full() def tearDown(self): - #_driver.flush() + #_driver.flush_full() pass def test_set(self): contract = 'stustu' name = 'balance' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR raw_key = '{}{}{}'.format(contract, delimiter, name) @@ -46,10 +47,9 @@ def test_set(self): def test_get(self): contract = 'stustu' name = 'balance' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR raw_key = '{}{}{}'.format(contract, delimiter, name) - driver.set(raw_key, 1234) v = Variable(contract, name, driver=driver) @@ -71,15 +71,15 @@ def test_set_get(self): class TestHash(TestCase): def setUp(self): - driver.flush() + driver.flush_full() def tearDown(self): - driver.flush() + driver.flush_full() def test_set(self): contract = 'stustu' name = 'balance' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR raw_key_1 = '{}{}{}'.format(contract, delimiter, name) raw_key_1 += ':stu' @@ -95,7 +95,7 @@ def test_set(self): def test_get(self): contract = 'stustu' name = 'balance' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR raw_key_1 = '{}{}{}'.format(contract, delimiter, name) raw_key_1 += ':stu' @@ -125,7 +125,7 @@ def test_set_get(self): def test_setitem(self): contract = 'blah' name = 'scoob' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR h = Hash(contract, name, driver=driver) @@ -140,7 +140,7 @@ def test_setitem(self): def test_getitem(self): contract = 'blah' name = 'scoob' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR h = Hash(contract, name, driver=driver) @@ -211,7 +211,7 @@ def test_setitems_keys_too_large(self): def test_getitems_keys(self): contract = 'blah' name = 'scoob' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR h = Hash(contract, name, driver=driver) @@ -228,7 +228,7 @@ def test_getitems_keys(self): def test_getsetitems(self): contract = 'blah' name = 'scoob' - delimiter = driver.delimiter + delimiter = constants.INDEX_SEPARATOR h = Hash(contract, name, driver=driver) @@ -291,30 +291,31 @@ def test_get_all_after_setting(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h['1'] = 123 - h['2'] = 456 - h['3'] = 789 + hsh['1'] = 123 + hsh['2'] = 456 + hsh['3'] = 789 l = [123, 456, 789] - driver.commit() + # TODO - this ok ? :D + # driver.commit() # we care about whats included, not order - self.assertSetEqual(set(h.all()), set(l)) + self.assertSetEqual(set(hsh.all()), set(l)) def test_items_returns_kv_pairs(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h['1'] = 123 - h['2'] = 456 - h['3'] = 789 + hsh['1'] = 123 + hsh['2'] = 456 + hsh['3'] = 789 - driver.commit() + # driver.commit() kvs = { 'blah.scoob:3': 789, @@ -322,7 +323,7 @@ def test_items_returns_kv_pairs(self): 'blah.scoob:2': 456 } - got = h._items() + got = hsh._items() self.assertDictEqual(kvs, got) @@ -330,17 +331,17 @@ def test_items_multi_hash_returns_kv_pairs(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[0, '1'] = 123 - h[0, '2'] = 456 - h[0, '3'] = 789 + hsh[0, '1'] = 123 + hsh[0, '2'] = 456 + hsh[0, '3'] = 789 - h[1, '1'] = 999 - h[1, '2'] = 888 - h[1, '3'] = 777 + hsh[1, '1'] = 999 + hsh[1, '2'] = 888 + hsh[1, '3'] = 777 - driver.commit() + # driver.commit() kvs = { 'blah.scoob:0:3': 789, @@ -348,7 +349,7 @@ def test_items_multi_hash_returns_kv_pairs(self): 'blah.scoob:0:2': 456 } - got = h._items(0) + got = hsh._items(0) self.assertDictEqual(kvs, got) @@ -356,17 +357,17 @@ def test_items_multi_hash_returns_all(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[0, '1'] = 123 - h[0, '2'] = 456 - h[0, '3'] = 789 + hsh[0, '1'] = 123 + hsh[0, '2'] = 456 + hsh[0, '3'] = 789 - h[1, '1'] = 999 - h[1, '2'] = 888 - h[1, '3'] = 777 + hsh[1, '1'] = 999 + hsh[1, '2'] = 888 + hsh[1, '3'] = 777 - driver.commit() + # driver.commit() kvs = { 'blah.scoob:0:3': 789, @@ -377,7 +378,7 @@ def test_items_multi_hash_returns_all(self): 'blah.scoob:1:2': 888 } - got = h._items() + got = hsh._items() self.assertDictEqual(kvs, got) @@ -385,17 +386,17 @@ def test_items_clear_deletes_only_multi_hash(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[0, '1'] = 123 - h[0, '2'] = 456 - h[0, '3'] = 789 + hsh[0, '1'] = 123 + hsh[0, '2'] = 456 + hsh[0, '3'] = 789 - h[1, '1'] = 999 - h[1, '2'] = 888 - h[1, '3'] = 777 + hsh[1, '1'] = 999 + hsh[1, '2'] = 888 + hsh[1, '3'] = 777 - driver.commit() + # driver.commit() kvs = { 'blah.scoob:0:3': 789, @@ -403,11 +404,11 @@ def test_items_clear_deletes_only_multi_hash(self): 'blah.scoob:0:2': 456 } - h.clear(1) + hsh.clear(1) - driver.commit() + # driver.commit() - got = h._items() + got = hsh._items() self.assertDictEqual(kvs, got) @@ -415,38 +416,40 @@ def test_all_multihash_returns_values(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[0, '1'] = 123 - h[0, '2'] = 456 - h[0, '3'] = 789 + hsh[0, '1'] = 123 + hsh[0, '2'] = 456 + hsh[0, '3'] = 789 - h[1, '1'] = 999 - h[1, '2'] = 888 - h[1, '3'] = 777 + hsh[1, '1'] = 999 + hsh[1, '2'] = 888 + hsh[1, '3'] = 777 l = [123, 456, 789] - driver.commit() + # TODO + # Test works when below line is commented out - not sure if our driver works differently now + # driver.commit() # we care about whats included, not order - self.assertSetEqual(set(h.all(0)), set(l)) + self.assertSetEqual(set(hsh.all(0)), set(l)) def test_multihash_multiple_dims_clear_behaves_similar_to_single_dim(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[1, 0, '1'] = 123 - h[1, 0, '2'] = 456 - h[1, 0, '3'] = 789 + hsh[1, 0, '1'] = 123 + hsh[1, 0, '2'] = 456 + hsh[1, 0, '3'] = 789 - h[1, 1, '1'] = 999 - h[1, 1, '2'] = 888 - h[1, 1, '3'] = 777 + hsh[1, 1, '1'] = 999 + hsh[1, 1, '2'] = 888 + hsh[1, 1, '3'] = 777 - driver.commit() + # driver.commit() kvs = { 'blah.scoob:1:0:3': 789, @@ -454,11 +457,11 @@ def test_multihash_multiple_dims_clear_behaves_similar_to_single_dim(self): 'blah.scoob:1:0:2': 456 } - h.clear(1, 1) + hsh.clear(1, 1) - driver.commit() + # driver.commit() - got = h._items() + got = hsh._items() self.assertDictEqual(kvs, got) @@ -466,34 +469,35 @@ def test_multihash_multiple_dims_all_gets_items_similar_to_single_dim(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h[1, 0, '1'] = 123 - h[1, 0, '2'] = 456 - h[1, 0, '3'] = 789 + hsh[1, 0, '1'] = 123 + hsh[1, 0, '2'] = 456 + hsh[1, 0, '3'] = 789 - h[1, 1, '1'] = 999 - h[1, 1, '2'] = 888 - h[1, 1, '3'] = 777 + hsh[1, 1, '1'] = 999 + hsh[1, 1, '2'] = 888 + hsh[1, 1, '3'] = 777 l = [123, 456, 789] - driver.commit() + # driver.commit() # we care about whats included, not order - self.assertSetEqual(set(h.all(1, 0)), set(l)) + self.assertSetEqual(set(hsh.all(1, 0)), set(l)) def test_clear_items_deletes_all_key_value_pairs(self): contract = 'blah' name = 'scoob' - h = Hash(contract, name, driver=driver, default_value=0) + hsh = Hash(contract, name, driver=driver, default_value=0) - h['1'] = 123 - h['2'] = 456 - h['3'] = 789 + hsh['1'] = 123 + hsh['2'] = 456 + hsh['3'] = 789 - driver.commit() + # TODO - test works without commit - is ok + # driver.commit() kvs = { 'blah.scoob:3': 789, @@ -501,25 +505,24 @@ def test_clear_items_deletes_all_key_value_pairs(self): 'blah.scoob:2': 456 } - got = h._items() + got = hsh._items() self.assertDictEqual(kvs, got) + hsh.clear() - h.clear() - - driver.commit() + # driver.commit() - got = h._items() + got = hsh._items() self.assertDictEqual({}, got) class TestForeignVariable(TestCase): def setUp(self): - driver.flush() + driver.flush_full() def tearDown(self): - driver.flush() + driver.flush_full() def test_set(self): contract = 'stustu' @@ -552,10 +555,10 @@ def test_get(self): class TestForeignHash(TestCase): def setUp(self): - driver.flush() + driver.flush_full() def tearDown(self): - #_driver.flush() + #_driver.flush_full() pass def test_set(self):