From 265c80dd353c78d3b7caf87994204829e435394a Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 16:41:52 +0545 Subject: [PATCH 1/7] make a variable for optimization purpose --- authorization/authorization.py | 35 ++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index 8c057f3..88a74f8 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -269,22 +269,24 @@ def submit_game_proposal(self, _gamedata: str) -> None: revert(f'50 ICX is required for submitting game proposal') metadata = json_loads(_gamedata) self._check_game_metadata(metadata) - score_at_address = self.create_interface_score(Address.from_string(metadata['scoreAddress']), - ScoreOwnerInterface) + scoreAddress = metadata['scoreAddress'] + game_address = Address.from_string(scoreAddress) + score_at_address = self.create_interface_score(game_address, ScoreOwnerInterface) if self.msg.sender != score_at_address.get_score_owner(): revert('Owner not matched') - self.ProposalSubmitted(self.msg.sender, Address.from_string(metadata['scoreAddress'])) - if Address.from_string(metadata['scoreAddress']) in self._proposal_list: + + self.ProposalSubmitted(self.msg.sender, game_address) + if game_address in self._proposal_list: revert(f'Already listed scoreAddress in the proposal list.') - self._proposal_list.put(Address.from_string(metadata['scoreAddress'])) - self._owner_data[Address.from_string(metadata['scoreAddress'])] = self.msg.sender + self._proposal_list.put(game_address) + self._owner_data[game_address] = self.msg.sender - self._status_data[Address.from_string(metadata['scoreAddress'])] = 'waiting' - self._proposal_data[Address.from_string(metadata['scoreAddress'])] = _gamedata + self._status_data[game_address] = 'waiting' + self._proposal_data[game_address] = _gamedata if self._apply_watch_dog_method.get(): - self._maximum_payouts[Address.from_string(metadata['scoreAddress'])] = metadata['maxPayout'] + self._maximum_payouts[game_address] = metadata['maxPayout'] @external def set_game_status(self, _status: str, _scoreAddress: Address) -> None: @@ -300,14 +302,15 @@ def set_game_status(self, _status: str, _scoreAddress: Address) -> None: revert('Sender not an admin') if _status not in self.STATUS_TYPE: revert('Invalid status') - if _status == 'gameRejected' and self._status_data[_scoreAddress] != 'gameReady': - revert(f'This game cannot be rejected from state {self._status_data[_scoreAddress]}') - if _status == 'gameApproved' and not (self._status_data[_scoreAddress] == 'gameReady' - or self._status_data[_scoreAddress] == 'gameSuspended'): - revert(f'This game cannot be approved from state {self._status_data[_scoreAddress]}') - if _status == 'gameSuspended' and self._status_data[_scoreAddress] != 'gameApproved': + gameAddress = self._status_data[_scoreAddress] + if _status == 'gameRejected' and gameAddress != 'gameReady': + revert(f'This game cannot be rejected from state {gameAddress}') + if _status == 'gameApproved' and not (gameAddress == 'gameReady' + or gameAddress == 'gameSuspended'): + revert(f'This game cannot be approved from state {gameAddress}') + if _status == 'gameSuspended' and gameAddress != 'gameApproved': revert('Only approved games may be suspended.') - if _status == 'gameDeleted' and self._status_data[_scoreAddress] != 'gameSuspended': + if _status == 'gameDeleted' and gameAddress != 'gameSuspended': revert('Only suspended games may be deleted.') self._status_data[_scoreAddress] = _status From 1afc669809c416743f45177d67dd4858832026f7 Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 16:42:08 +0545 Subject: [PATCH 2/7] Remove Python runtime exception as eventlog --- authorization/authorization.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index 88a74f8..30ad042 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -428,17 +428,16 @@ def accumulate_daily_payouts(self, game: Address, payout: int) -> bool: day = (self.now() // U_SECONDS_DAY) if self._apply_watch_dog_method.get(): - try: - if payout > self._maximum_payouts[game]: - revert(f'Preventing Overpayment. Requested payout: {payout}. MaxPayout for this game: ' - f'{self._maximum_payouts[game]}. {TAG}') - - if self._payouts[day][game] + payout - self._wagers[day][game] >= self._maximum_loss.get(): - revert(f'Limit loss. MaxLoss: {self._maximum_loss.get()}. Loss Incurred if payout: ' - f'{self._payouts[day][game] + payout - self._wagers[day][game]}, {TAG}') - except BaseException as e: + if payout > self._maximum_payouts[game]: self._status_data[game] = 'gameSuspended' - self.GameSuspended(game, str(e)) + self.GameSuspended(game, f'To prevent overpayment. Requested payout: {payout}. ' + f'MaxPayout: {self._maximum_payouts[game]}. {TAG}') + return False + + if self._payouts[day][game] + payout - self._wagers[day][game] >= self._maximum_loss.get(): + self._status_data[game] = 'gameSuspended' + self.GameSuspended(game, f'To limit loss. MaxLoss: {self._maximum_loss.get()}. ' + f'Loss Incurred if payout: {self._payouts[day][game] + payout - self._wagers[day][game]}, {TAG}') return False self._payouts[day][game] += payout From 42d646f02ca0551c169e9e631429ce7b27301e40 Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 16:42:19 +0545 Subject: [PATCH 3/7] remove baseException and replace with Exception --- authorization/authorization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index 30ad042..d1cb7b5 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -372,8 +372,8 @@ def _check_game_metadata(self, _metadata: dict): revWalletAddress = Address.from_string(revwallet) if revWalletAddress.is_contract: revert('Not a wallet address') - except BaseException as e: - Logger.debug(f'Failed. Exception: {e}', TAG) + except Exception: + Logger.debug(f'Failed.', TAG) revert('Invalid address') @external From ef4c80a0bee4b09013b9f4bf5c522d4f2850e759 Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 17:40:55 +0545 Subject: [PATCH 4/7] Reuse the variable for optimization --- authorization/authorization.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index d1cb7b5..3157901 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -269,8 +269,7 @@ def submit_game_proposal(self, _gamedata: str) -> None: revert(f'50 ICX is required for submitting game proposal') metadata = json_loads(_gamedata) self._check_game_metadata(metadata) - scoreAddress = metadata['scoreAddress'] - game_address = Address.from_string(scoreAddress) + game_address = Address.from_string(metadata['scoreAddress']) score_at_address = self.create_interface_score(game_address, ScoreOwnerInterface) if self.msg.sender != score_at_address.get_score_owner(): @@ -302,13 +301,13 @@ def set_game_status(self, _status: str, _scoreAddress: Address) -> None: revert('Sender not an admin') if _status not in self.STATUS_TYPE: revert('Invalid status') - gameAddress = self._status_data[_scoreAddress] - if _status == 'gameRejected' and gameAddress != 'gameReady': - revert(f'This game cannot be rejected from state {gameAddress}') - if _status == 'gameApproved' and not (gameAddress == 'gameReady' - or gameAddress == 'gameSuspended'): - revert(f'This game cannot be approved from state {gameAddress}') - if _status == 'gameSuspended' and gameAddress != 'gameApproved': + gameStatus = self._status_data[_scoreAddress] + if _status == 'gameRejected' and gameStatus != 'gameReady': + revert(f'This game cannot be rejected from state {gameStatus}') + if _status == 'gameApproved' and not (gameStatus == 'gameReady' + or gameStatus == 'gameSuspended'): + revert(f'This game cannot be approved from state {gameStatus}') + if _status == 'gameSuspended' and gameStatus != 'gameApproved': revert('Only approved games may be suspended.') if _status == 'gameDeleted' and gameAddress != 'gameSuspended': revert('Only suspended games may be deleted.') From c400cd07fb0ead8a4ba7dfb411d858249e646edf Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 17:41:34 +0545 Subject: [PATCH 5/7] Reuse the variable for optimization --- authorization/authorization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index 3157901..bd675ab 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -309,7 +309,7 @@ def set_game_status(self, _status: str, _scoreAddress: Address) -> None: revert(f'This game cannot be approved from state {gameStatus}') if _status == 'gameSuspended' and gameStatus != 'gameApproved': revert('Only approved games may be suspended.') - if _status == 'gameDeleted' and gameAddress != 'gameSuspended': + if _status == 'gameDeleted' and gameStatus != 'gameSuspended': revert('Only suspended games may be deleted.') self._status_data[_scoreAddress] = _status From 73ed879e27bf4fda9b6d8e2ce215f9225ffd603b Mon Sep 17 00:00:00 2001 From: cupiddev Date: Mon, 6 Sep 2021 17:49:55 +0545 Subject: [PATCH 6/7] make revert message understandable. --- authorization/authorization.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index bd675ab..0591d63 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -372,8 +372,7 @@ def _check_game_metadata(self, _metadata: dict): if revWalletAddress.is_contract: revert('Not a wallet address') except Exception: - Logger.debug(f'Failed.', TAG) - revert('Invalid address') + revert('Invalid address while getting game metadata.') @external def accumulate_daily_wagers(self, game: Address, wager: int) -> None: From d4e186a2195e711efe10afb85545c189212d4971 Mon Sep 17 00:00:00 2001 From: cupiddev Date: Wed, 8 Sep 2021 00:09:50 +0545 Subject: [PATCH 7/7] remove `on_update()` variables --- authorization/authorization.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/authorization/authorization.py b/authorization/authorization.py index 0591d63..e31da75 100644 --- a/authorization/authorization.py +++ b/authorization/authorization.py @@ -86,8 +86,6 @@ def on_install(self) -> None: def on_update(self) -> None: super().on_update() - self._day.set(self.now() // U_SECONDS_DAY) - self._game_developers_share.set(20) @external def untether(self) -> None: @@ -390,7 +388,7 @@ def accumulate_daily_wagers(self, game: Address, wager: int) -> None: day = (self.now() // U_SECONDS_DAY) self._wagers[day][game] += wager if (self._new_div_changing_time.get() is not None - and self.now() >= self._new_div_changing_time.get()): + and self.now() >= self._new_div_changing_time.get()): self._todays_games_excess[game] += wager @external(readonly=True) @@ -440,7 +438,7 @@ def accumulate_daily_payouts(self, game: Address, payout: int) -> bool: self._payouts[day][game] += payout if (self._new_div_changing_time.get() is not None - and self.now() >= self._new_div_changing_time.get()): + and self.now() >= self._new_div_changing_time.get()): self._todays_games_excess[game] -= payout return True