From 36219f28fcdfb5ce82f671fbc7b2cc343d532852 Mon Sep 17 00:00:00 2001 From: endogen <081081@gmx.net> Date: Mon, 13 May 2024 02:50:04 +0200 Subject: [PATCH] Code style things --- contracting/execution/executor.py | 22 +++++++++++----------- contracting/stdlib/bridge/imports.py | 6 +++--- contracting/stdlib/bridge/time.py | 4 ++-- contracting/storage/driver.py | 9 ++++++++- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/contracting/execution/executor.py b/contracting/execution/executor.py index bbd83bfe..e515e17a 100644 --- a/contracting/execution/executor.py +++ b/contracting/execution/executor.py @@ -42,6 +42,7 @@ def execute(self, sender, contract_name, function_name, kwargs, stamps=constants.DEFAULT_STAMPS, stamp_cost=constants.STAMPS_PER_TAU, metering=None) -> dict: + if not self.bypass_privates: assert not function_name.startswith(constants.PRIVATE_METHOD_PREFIX), 'Private method not callable.' @@ -61,11 +62,12 @@ def execute(self, sender, contract_name, function_name, kwargs, try: if metering: - balances_key = '{}{}{}{}{}'.format(self.currency_contract, - constants.INDEX_SEPARATOR, - self.balances_hash, - constants.DELIMITER, - sender) + balances_key = (f'{self.currency_contract}' + f'{constants.INDEX_SEPARATOR}' + f'{self.balances_hash}' + f'{constants.DELIMITER}' + f'{sender}') + if self.bypass_balance_amount: balance = 9999999 @@ -84,9 +86,8 @@ def execute(self, sender, contract_name, function_name, kwargs, 'stamps': stamps }) - assert balance * stamp_cost >= stamps, 'Sender does not have enough stamps for the transaction. \ - Balance at key {} is {}'.format(balances_key, - balance) + assert balance * stamp_cost >= stamps, (f'Sender does not have enough stamps for the transaction. ' + f'Balance at key {balances_key} is {balance}') runtime.rt.env.update(environment) status_code = 0 @@ -159,9 +160,7 @@ def execute(self, sender, contract_name, function_name, kwargs, assert balances_key is not None, 'Balance key was not set properly. Cannot deduct stamps.' to_deduct = stamps_used - to_deduct /= stamp_cost - to_deduct = ContractingDecimal(to_deduct) balance = driver.get(balances_key) @@ -171,13 +170,14 @@ def execute(self, sender, contract_name, function_name, kwargs, balance = max(balance - to_deduct, 0) driver.set(balances_key, balance) - #mark=False) # This makes sure that the key isnt modified every time in the block + if auto_commit: driver.commit() Seeded.s = False runtime.rt.clean_up() runtime.rt.env.update({'__Driver': driver}) + output = { 'status_code': status_code, 'result': result, diff --git a/contracting/stdlib/bridge/imports.py b/contracting/stdlib/bridge/imports.py index 19978bc2..50acd3b7 100644 --- a/contracting/stdlib/bridge/imports.py +++ b/contracting/stdlib/bridge/imports.py @@ -31,6 +31,7 @@ def is_of(self, f: FunctionType): if f.__code__.co_name == self.name and f.__code__.co_varnames[:num_args] == self.args: return True + return False @@ -61,9 +62,7 @@ def import_module(name): if _driver.get_contract(name) is None: raise ImportError - m = importlib.import_module(name, package=None) - - return m + return importlib.import_module(name, package=None) def enforce_interface(m: ModuleType, interface: list): @@ -82,6 +81,7 @@ def enforce_interface(m: ModuleType, interface: list): if isinstance(attribute, FunctionType): if not i.is_of(attribute): return False + return True diff --git a/contracting/stdlib/bridge/time.py b/contracting/stdlib/bridge/time.py index 65996ec3..cc481be5 100644 --- a/contracting/stdlib/bridge/time.py +++ b/contracting/stdlib/bridge/time.py @@ -107,7 +107,6 @@ def __init__(self, weeks=0, weeks=int(weeks), days=int(days), hours=int(hours), minutes=int(minutes), seconds=int(seconds) ) - # For fast access to how many hours are in a timedelta. self.__raw_seconds = get_raw_seconds( weeks=int(weeks), days=int(days), hours=int(hours), minutes=int(minutes), seconds=int(seconds) @@ -173,6 +172,7 @@ def __mul__(self, other): elif isinstance(other, int): return Timedelta(days=self._timedelta.days * other, seconds=self._timedelta.seconds * other) + return NotImplemented def __str__(self): @@ -220,4 +220,4 @@ def weeks(self): exports = { 'datetime': datetime_module -} \ No newline at end of file +} diff --git a/contracting/storage/driver.py b/contracting/storage/driver.py index 3742a82d..4ca72667 100644 --- a/contracting/storage/driver.py +++ b/contracting/storage/driver.py @@ -36,6 +36,7 @@ COMPILED_KEY = "__compiled__" DEVELOPER_KEY = "__developer__" + class Driver: def __init__(self, bypass_cache=False): self.pending_deltas = {} @@ -61,7 +62,10 @@ def __parse_key(self, key): return filename, variable def __filename_to_path(self, filename): - return (str(self.run_state.joinpath(filename)) if filename.startswith("__") else str(self.contract_state.joinpath(filename))) + if filename.startswith("__"): + return str(self.run_state.joinpath(filename)) + else: + return str(self.contract_state.joinpath(filename)) def __get_files(self): return sorted(os.listdir(self.contract_state) + os.listdir(self.run_state)) @@ -308,6 +312,7 @@ def commit(self): hdf5.delete_key_from_disk(self.__filename_to_path(k), k) else: hdf5.set_value_to_disk(self.__filename_to_path(k), k, v, None) + self.cache.clear() self.pending_writes.clear() self.pending_reads.clear() @@ -362,6 +367,7 @@ def get_all_contract_state(self): full_key = f"{filename}{DELIMITER}{key}" value = hdf5.get_value_from_disk(self.__filename_to_path(filename), key) all_contract_state[full_key] = value + return all_contract_state def get_run_state(self): @@ -376,6 +382,7 @@ def get_run_state(self): full_key = f"{filename}{DELIMITER}{key}" value = hdf5.get_value_from_disk(self.__filename_to_path(filename), key) run_state[full_key] = value + return run_state def reset_cache(self):