From 5dceb34207ef6a54752cf7695318752153329a35 Mon Sep 17 00:00:00 2001 From: "Kieran R. Prasch" Date: Thu, 6 Feb 2020 19:20:25 -0800 Subject: [PATCH] Give some attention to layering get missing confirmations via agent and actor; Fixes prolong duration sugestion. --- nucypher/blockchain/eth/actors.py | 23 ++++++++++------------- nucypher/blockchain/eth/agents.py | 16 ++++++++++++++++ nucypher/cli/commands/stake.py | 2 +- nucypher/cli/painting.py | 2 +- nucypher/network/nodes.py | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/nucypher/blockchain/eth/actors.py b/nucypher/blockchain/eth/actors.py index a96b8e51c..542d37daa 100644 --- a/nucypher/blockchain/eth/actors.py +++ b/nucypher/blockchain/eth/actors.py @@ -1150,6 +1150,12 @@ class Staker(NucypherTokenActor): raise TypeError("This method can only be used when staking via a contract") return receipt + @property + def missing_confirmations(self) -> int: + staker_address = self.checksum_address + missing = self.staking_agent.get_missing_confirmations(checksum_address=staker_address) + return missing + class Worker(NucypherTokenActor): """ @@ -1223,20 +1229,11 @@ class Worker(NucypherTokenActor): receipt = self.staking_agent.confirm_activity(worker_address=self.__worker_address) return receipt - def get_missing_confirmations(self) -> int: + @property + def missing_confirmations(self) -> int: staker_address = self.checksum_address - last_confirmed_period = self.staking_agent.get_last_active_period(staker_address) - current_period = self.staking_agent.get_current_period() - missing_confirmations = current_period - last_confirmed_period - if missing_confirmations in (0, -1): - result = 0 - elif missing_confirmations == current_period: # never confirmed - stakes = self.staking_agent.get_all_stakes(staker_address=staker_address) - initial_staking_period = min(stakes, key=lambda s: s[0]) - result = current_period - initial_staking_period - else: - result = missing_confirmations - return result + missing = self.staking_agent.get_missing_confirmations(checksum_address=staker_address) + return missing class BlockchainPolicyAuthor(NucypherTokenActor): diff --git a/nucypher/blockchain/eth/agents.py b/nucypher/blockchain/eth/agents.py index 05b28d680..1bb42c296 100644 --- a/nucypher/blockchain/eth/agents.py +++ b/nucypher/blockchain/eth/agents.py @@ -605,6 +605,22 @@ class StakingEscrowAgent(EthereumContractAgent): total_completed_work = self.contract.functions.getCompletedWork(bidder_address).call() return total_completed_work + @validate_checksum_address + def get_missing_confirmations(self, checksum_address: str) -> int: + # TODO: Move this up one layer, since it utilizes a combination of contract API methods. + last_confirmed_period = self.get_last_active_period(checksum_address) + current_period = self.get_current_period() + missing_confirmations = current_period - last_confirmed_period + if missing_confirmations in (0, -1): + result = 0 + elif missing_confirmations == current_period: # never confirmed + stakes = list(self.get_all_stakes(staker_address=checksum_address)) + initial_staking_period = min(stakes, key=lambda s: s[0])[0] + result = current_period - initial_staking_period + else: + result = missing_confirmations + return result + class PolicyManagerAgent(EthereumContractAgent): diff --git a/nucypher/cli/commands/stake.py b/nucypher/cli/commands/stake.py index ce2a54d2b..c4a62daf1 100644 --- a/nucypher/cli/commands/stake.py +++ b/nucypher/cli/commands/stake.py @@ -703,7 +703,7 @@ def prolong(general_config, transacting_staker_options, config_file, force, lock # Interactive if not lock_periods: stake_extension_range = click.IntRange(min=1, max=economics.maximum_allowed_locked, clamp=False) - max_extension = economics.maximum_allowed_locked - current_stake.periods_remaining + max_extension = economics.maximum_rewarded_periods - current_stake.periods_remaining lock_periods = click.prompt(f"Enter number of periods to extend (1-{max_extension})", type=stake_extension_range) if not force: click.confirm(f"Publish stake extension of {lock_periods} period(s) to the blockchain?", abort=True) diff --git a/nucypher/cli/painting.py b/nucypher/cli/painting.py index f6e17c368..b87dc6672 100644 --- a/nucypher/cli/painting.py +++ b/nucypher/cli/painting.py @@ -475,7 +475,7 @@ def paint_stakes(emitter, stakeholder, paint_inactive: bool = False, staker_addr fees = staker.policy_agent.get_reward_amount(staker.checksum_address) gwei_fees = f"{Web3.fromWei(fees, 'gwei')} Gwei" last_confirmed = staker.staking_agent.get_last_active_period(staker.checksum_address) - missing = staker.staking_agent.get_missing_confirmations(staker_address=staker.checksum_address) + missing = staker.missing_confirmations staker_data = [f'Missing {missing} confirmation{"s" if missing > 1 else ""}' if missing else f'Confirmed #{last_confirmed}', f'{"Yes" if staker.is_restaking else "No"} ({"Locked" if staker.restaking_lock_enabled else "Unlocked"})', diff --git a/nucypher/network/nodes.py b/nucypher/network/nodes.py index 7ad08bd15..9c01a8967 100644 --- a/nucypher/network/nodes.py +++ b/nucypher/network/nodes.py @@ -1388,6 +1388,6 @@ class Teacher: if not self.federated_only: payload.update({ "balances": dict(eth=float(self.eth_balance), nu=float(self.token_balance.to_tokens())), - "missing_confirmations": self.get_missing_confirmations(), + "missing_confirmations": self.missing_confirmations, "last_active_period": self.last_active_period}) return payload