CLI method to remove unused sub-stake

pull/2384/head
vzotova 2020-10-19 20:35:41 +03:00
parent b443ab7007
commit d1ede31ebc
3 changed files with 87 additions and 1 deletions

View File

@ -73,7 +73,7 @@ from nucypher.cli.literature import (
INSUFFICIENT_BALANCE_TO_CREATE, PROMPT_STAKE_CREATE_VALUE, PROMPT_STAKE_CREATE_LOCK_PERIODS,
ONLY_DISPLAYING_MERGEABLE_STAKES_NOTE, CONFIRM_MERGE, SUCCESSFUL_STAKES_MERGE, SUCCESSFUL_ENABLE_SNAPSHOTS,
SUCCESSFUL_DISABLE_SNAPSHOTS, CONFIRM_ENABLE_SNAPSHOTS,
CONFIRM_STAKE_USE_UNLOCKED)
CONFIRM_STAKE_USE_UNLOCKED, CONFIRM_REMOVE, SUCCESSFUL_STAKE_REMOVING)
from nucypher.cli.options import (
group_options,
option_config_file,
@ -1068,6 +1068,61 @@ def merge(general_config: GroupGeneralConfig,
paint_stakes(emitter=emitter, staker=STAKEHOLDER)
@stake.command()
@group_transacting_staker_options
@option_config_file
@option_force
@group_general_config
@click.option('--index', help="Index of unused stake to remove", type=click.INT)
def remove_unused(general_config: GroupGeneralConfig,
transacting_staker_options: TransactingStakerOptions,
config_file, force, index):
"""Remove unused stake."""
# Setup
emitter = setup_emitter(general_config)
STAKEHOLDER = transacting_staker_options.create_character(emitter, config_file)
action_period = STAKEHOLDER.staking_agent.get_current_period()
blockchain = transacting_staker_options.get_blockchain()
client_account, staking_address = select_client_account_for_staking(
emitter=emitter,
stakeholder=STAKEHOLDER,
staking_address=transacting_staker_options.staker_options.staking_address,
individual_allocation=STAKEHOLDER.individual_allocation,
force=force)
# Handle stake update and selection
if index is not None: # 0 is valid.
current_stake = STAKEHOLDER.stakes[index]
else:
current_stake = select_stake(staker=STAKEHOLDER, emitter=emitter, stakes_status=Stake.Status.INACTIVE)
if not force:
click.confirm(CONFIRM_REMOVE.format(stake_index=current_stake.index), abort=True)
# Authenticate
password = get_password(stakeholder=STAKEHOLDER,
blockchain=blockchain,
client_account=client_account,
hw_wallet=transacting_staker_options.hw_wallet)
STAKEHOLDER.assimilate(password=password)
# Non-interactive: Consistency check to prevent the above agreement from going stale.
last_second_current_period = STAKEHOLDER.staking_agent.get_current_period()
if action_period != last_second_current_period:
emitter.echo(PERIOD_ADVANCED_WARNING, color='red')
raise click.Abort
# Execute
receipt = STAKEHOLDER.remove_unused_stake(stake=current_stake)
# Report
emitter.echo(SUCCESSFUL_STAKE_REMOVING, color='green', verbosity=1)
paint_receipt_summary(emitter=emitter, receipt=receipt, chain_name=blockchain.client.chain_name)
paint_stakes(emitter=emitter, staker=STAKEHOLDER)
@stake.command('collect-reward')
@group_transacting_staker_options
@option_config_file

View File

@ -271,6 +271,10 @@ CONFIRM_MERGE = "Publish merging of {stake_index_1} and {stake_index_2} stakes?"
SUCCESSFUL_STAKES_MERGE = 'Successfully Merged Stakes'
CONFIRM_REMOVE = "Publish removing of {stake_index} stake?"
SUCCESSFUL_STAKE_REMOVING = 'Successfully Removed Stake'
#
# Rewards
#

View File

@ -286,6 +286,33 @@ def test_merge_stakes(click_runner,
assert stakes[selection_2].last_period == 1
def test_remove_unused(click_runner,
stakeholder_configuration_file_location,
token_economics,
testerchain,
agency_local_registry,
manual_staker,
stake_value):
staking_agent = ContractAgency.get_agent(StakingEscrowAgent, registry=agency_local_registry)
original_stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
selection = 2
assert original_stakes[selection].last_period == 1
stake_args = ('stake', 'remove-unused',
'--config-file', stakeholder_configuration_file_location,
'--staking-address', manual_staker,
'--index', selection,
'--force')
user_input = f'0\n' + f'{INSECURE_DEVELOPMENT_PASSWORD}\n' + YES_ENTER
result = click_runner.invoke(nucypher_cli, stake_args, input=user_input, catch_exceptions=False)
assert result.exit_code == 0
stakes = list(staking_agent.get_all_stakes(staker_address=manual_staker))
assert len(stakes) == len(original_stakes) - 1
def test_stake_bond_worker(click_runner,
testerchain,
agency_local_registry,