Skip to content

Commit

Permalink
Merge pull request #32 from xian-network/code_style
Browse files Browse the repository at this point in the history
Code style things
  • Loading branch information
Endogen authored May 13, 2024
2 parents c815284 + 36219f2 commit f429823
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
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 @@ -105,7 +105,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 @@ -171,6 +170,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 @@ -218,4 +218,4 @@ def weeks(self):

exports = {
'datetime': datetime_module
}
}
8 changes: 7 additions & 1 deletion contracting/storage/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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 @@ -301,6 +304,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 @@ -355,6 +359,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 @@ -369,6 +374,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

0 comments on commit f429823

Please sign in to comment.