Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style things #32

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions contracting/execution/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions contracting/stdlib/bridge/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand All @@ -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


Expand Down
4 changes: 2 additions & 2 deletions contracting/stdlib/bridge/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -220,4 +220,4 @@ def weeks(self):

exports = {
'datetime': datetime_module
}
}
9 changes: 8 additions & 1 deletion contracting/storage/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
COMPILED_KEY = "__compiled__"
DEVELOPER_KEY = "__developer__"


class Driver:
def __init__(self, bypass_cache=False):
self.pending_deltas = {}
Expand All @@ -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))
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Loading