Skip to content

Commit

Permalink
PRECBDApplication: tests for amount syncing with StakeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Jul 14, 2023
1 parent 8eafef3 commit f463e71
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion contracts/contracts/coordination/StakeInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract StakeInfo is AccessControl, IUpdatableStakeInfo, IAccessControlApplicat
}

mapping(address => Stake) public stakes;
mapping(address => address) public operatorToProvider;
mapping(address => address) private operatorToProvider;

function stakingProviderFromOperator(address _operator) external view returns (address){
return operatorToProvider[_operator];
Expand Down
46 changes: 44 additions & 2 deletions tests/application/test_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_authorization_increase(accounts, threshold_staking, pre_cbd_application
assert pre_cbd_application.stakingProviderInfo(staking_provider)[AUTHORIZATION_SLOT] == value
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == value
assert stake_info.authorizedStake(staking_provider) == value
assert pre_cbd_application.isAuthorized(staking_provider)

# Check that all events are emitted
Expand Down Expand Up @@ -85,6 +86,7 @@ def test_authorization_increase(accounts, threshold_staking, pre_cbd_application
assert pre_cbd_application.stakingProviderInfo(staking_provider)[AUTHORIZATION_SLOT] == value
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == value
assert stake_info.authorizedStake(staking_provider) == value
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationIncreased.from_receipt(tx)
Expand All @@ -108,6 +110,7 @@ def test_authorization_increase(accounts, threshold_staking, pre_cbd_application
)
assert pre_cbd_application.authorizedOverall() == authorization
assert pre_cbd_application.authorizedStake(staking_provider) == authorization
assert stake_info.authorizedStake(staking_provider) == authorization
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationIncreased.from_receipt(tx)
Expand All @@ -124,6 +127,7 @@ def test_authorization_increase(accounts, threshold_staking, pre_cbd_application
assert pre_cbd_application.stakingProviderInfo(staking_provider)[AUTHORIZATION_SLOT] == value
assert pre_cbd_application.authorizedOverall() == value
assert pre_cbd_application.authorizedStake(staking_provider) == value
assert stake_info.authorizedStake(staking_provider) == value
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationIncreased.from_receipt(tx)
Expand All @@ -133,6 +137,14 @@ def test_authorization_increase(accounts, threshold_staking, pre_cbd_application
assert event["fromAmount"] == value // 2
assert event["toAmount"] == value

# Increase again witjout syncing with StakeInfo
pre_cbd_application.setUpdatableStakeInfo(ZERO_ADDRESS, sender=creator)
tx = threshold_staking.authorizationIncreased(
staking_provider, value, 2 * value, sender=creator
)
assert pre_cbd_application.authorizedStake(staking_provider) == 2 * value
assert stake_info.authorizedStake(staking_provider) == value


def test_involuntary_authorization_decrease(
accounts, threshold_staking, pre_cbd_application, stake_info
Expand Down Expand Up @@ -164,6 +176,7 @@ def test_involuntary_authorization_decrease(
)
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == authorization
assert stake_info.authorizedStake(staking_provider) == authorization
assert pre_cbd_application.isAuthorized(staking_provider)
assert not pre_cbd_application.isOperatorConfirmed(staking_provider)
assert not pre_cbd_application.stakingProviderInfo(staking_provider)[OPERATOR_CONFIRMED_SLOT]
Expand Down Expand Up @@ -194,6 +207,7 @@ def test_involuntary_authorization_decrease(
)
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == authorization
assert stake_info.authorizedStake(staking_provider) == 0
assert pre_cbd_application.isAuthorized(staking_provider)
assert not pre_cbd_application.isOperatorConfirmed(staking_provider)
assert not pre_cbd_application.stakingProviderInfo(staking_provider)[OPERATOR_CONFIRMED_SLOT]
Expand Down Expand Up @@ -224,6 +238,7 @@ def test_involuntary_authorization_decrease(
)
assert pre_cbd_application.authorizedOverall() == authorization
assert pre_cbd_application.authorizedStake(staking_provider) == authorization
assert stake_info.authorizedStake(staking_provider) == 0
assert pre_cbd_application.isAuthorized(staking_provider)
assert pre_cbd_application.isOperatorConfirmed(staking_provider)
assert pre_cbd_application.getOperatorFromStakingProvider(staking_provider) == staking_provider
Expand All @@ -245,6 +260,7 @@ def test_involuntary_authorization_decrease(
assert pre_cbd_application.stakingProviderInfo(staking_provider)[DEAUTHORIZING_SLOT] == 0
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == 0
assert stake_info.authorizedStake(staking_provider) == 0
assert not pre_cbd_application.isAuthorized(staking_provider)
assert not pre_cbd_application.isOperatorConfirmed(staking_provider)
assert not pre_cbd_application.stakingProviderInfo(staking_provider)[OPERATOR_CONFIRMED_SLOT]
Expand Down Expand Up @@ -274,6 +290,7 @@ def test_involuntary_authorization_decrease(
)
assert pre_cbd_application.authorizedOverall() == authorization
assert pre_cbd_application.authorizedStake(staking_provider) == authorization
assert stake_info.authorizedStake(staking_provider) == authorization

events = pre_cbd_application.AuthorizationInvoluntaryDecreased.from_receipt(tx)
assert len(events) == 1
Expand All @@ -287,6 +304,7 @@ def test_involuntary_authorization_decrease(
threshold_staking.involuntaryAuthorizationDecrease(
staking_provider, authorization, 0, sender=creator
)
assert stake_info.authorizedStake(staking_provider) == authorization
assert pre_cbd_application.getOperatorFromStakingProvider(staking_provider) == ZERO_ADDRESS
assert stake_info.stakes(staking_provider)[STAKE_INFO_OPERATOR_SLOT] == staking_provider

Expand Down Expand Up @@ -339,6 +357,7 @@ def test_authorization_decrease_request(
)
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == value
assert stake_info.authorizedStake(staking_provider) == minimum_authorization
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationDecreaseRequested.from_receipt(tx)
Expand All @@ -365,6 +384,7 @@ def test_authorization_decrease_request(
)
assert pre_cbd_application.authorizedOverall() == value
assert pre_cbd_application.authorizedStake(staking_provider) == value
assert stake_info.authorizedStake(staking_provider) == 0
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationDecreaseRequested.from_receipt(tx)
Expand All @@ -386,6 +406,7 @@ def test_authorization_decrease_request(
)
assert pre_cbd_application.authorizedOverall() == value // 2
assert pre_cbd_application.authorizedStake(staking_provider) == value // 2
assert stake_info.authorizedStake(staking_provider) == 0

events = pre_cbd_application.AuthorizationDecreaseRequested.from_receipt(tx)
assert len(events) == 1
Expand All @@ -394,6 +415,13 @@ def test_authorization_decrease_request(
assert event["fromAmount"] == value // 2
assert event["toAmount"] == 0

# Request decrease without syncing with StakeInfo
pre_cbd_application.setUpdatableStakeInfo(ZERO_ADDRESS, sender=creator)
threshold_staking.authorizationDecreaseRequested(
staking_provider, value // 2, value // 2, sender=creator
)
assert stake_info.authorizedStake(staking_provider) == 0


def test_finish_authorization_decrease(
accounts, threshold_staking, pre_cbd_application, stake_info, chain
Expand Down Expand Up @@ -433,6 +461,7 @@ def test_finish_authorization_decrease(
assert pre_cbd_application.stakingProviderInfo(staking_provider)[END_DEAUTHORIZATION_SLOT] == 0
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == new_value
assert stake_info.authorizedStake(staking_provider) == new_value
assert pre_cbd_application.isAuthorized(staking_provider)
assert (
threshold_staking.authorizedStake(staking_provider, pre_cbd_application.address)
Expand Down Expand Up @@ -468,6 +497,7 @@ def test_finish_authorization_decrease(
assert pre_cbd_application.stakingProviderFromOperator(staking_provider) == staking_provider
assert pre_cbd_application.authorizedOverall() == new_value
assert pre_cbd_application.authorizedStake(staking_provider) == new_value
assert stake_info.authorizedStake(staking_provider) == new_value
assert pre_cbd_application.isAuthorized(staking_provider)
assert pre_cbd_application.isOperatorConfirmed(staking_provider)
assert (
Expand Down Expand Up @@ -496,6 +526,7 @@ def test_finish_authorization_decrease(
assert pre_cbd_application.stakingProviderFromOperator(staking_provider) == ZERO_ADDRESS
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == 0
assert stake_info.authorizedStake(staking_provider) == 0
assert not pre_cbd_application.isAuthorized(staking_provider)
assert not pre_cbd_application.isOperatorConfirmed(staking_provider)
assert not pre_cbd_application.stakingProviderInfo(staking_provider)[OPERATOR_CONFIRMED_SLOT]
Expand All @@ -511,13 +542,18 @@ def test_finish_authorization_decrease(

# Decrease everything again without syncing with StakeInfo
value = minimum_authorization
threshold_staking.authorizationIncreased(staking_provider, 0, value, sender=creator)
threshold_staking.authorizationIncreased(staking_provider, 0, 2 * value, sender=creator)
pre_cbd_application.bondOperator(staking_provider, staking_provider, sender=staking_provider)
pre_cbd_application.confirmOperatorAddress(sender=staking_provider)
threshold_staking.authorizationDecreaseRequested(staking_provider, value, 0, sender=creator)
threshold_staking.authorizationDecreaseRequested(
staking_provider, 2 * value, value, sender=creator
)
chain.pending_timestamp += deauthorization_duration
pre_cbd_application.setUpdatableStakeInfo(ZERO_ADDRESS, sender=creator)
threshold_staking.setDecreaseRequest(staking_provider, 0, sender=creator)
pre_cbd_application.finishAuthorizationDecrease(staking_provider, sender=creator)
assert pre_cbd_application.authorizedStake(staking_provider) == 0
assert stake_info.authorizedStake(staking_provider) == value
assert pre_cbd_application.getOperatorFromStakingProvider(staking_provider) == ZERO_ADDRESS
assert stake_info.stakes(staking_provider)[STAKE_INFO_OPERATOR_SLOT] == staking_provider

Expand Down Expand Up @@ -551,6 +587,7 @@ def test_resync(accounts, threshold_staking, pre_cbd_application, stake_info):
)
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == new_value
assert stake_info.authorizedStake(staking_provider) == new_value
assert pre_cbd_application.isAuthorized(staking_provider)

events = pre_cbd_application.AuthorizationReSynchronized.from_receipt(tx)
Expand All @@ -577,6 +614,7 @@ def test_resync(accounts, threshold_staking, pre_cbd_application, stake_info):
assert pre_cbd_application.stakingProviderFromOperator(staking_provider) == staking_provider
assert pre_cbd_application.authorizedOverall() == new_value
assert pre_cbd_application.authorizedStake(staking_provider) == new_value
assert stake_info.authorizedStake(staking_provider) == new_value
assert pre_cbd_application.isAuthorized(staking_provider)
assert pre_cbd_application.isOperatorConfirmed(staking_provider)
assert stake_info.stakes(staking_provider)[STAKE_INFO_OPERATOR_SLOT] == staking_provider
Expand Down Expand Up @@ -606,6 +644,7 @@ def test_resync(accounts, threshold_staking, pre_cbd_application, stake_info):
assert pre_cbd_application.stakingProviderFromOperator(staking_provider) == staking_provider
assert pre_cbd_application.authorizedOverall() == new_value
assert pre_cbd_application.authorizedStake(staking_provider) == new_value
assert stake_info.authorizedStake(staking_provider) == 0
assert pre_cbd_application.isAuthorized(staking_provider)
assert pre_cbd_application.isOperatorConfirmed(staking_provider)
assert stake_info.stakes(staking_provider)[STAKE_INFO_OPERATOR_SLOT] == staking_provider
Expand All @@ -628,6 +667,7 @@ def test_resync(accounts, threshold_staking, pre_cbd_application, stake_info):
assert pre_cbd_application.stakingProviderFromOperator(staking_provider) == ZERO_ADDRESS
assert pre_cbd_application.authorizedOverall() == 0
assert pre_cbd_application.authorizedStake(staking_provider) == 0
assert stake_info.authorizedStake(staking_provider) == 0
assert not pre_cbd_application.isAuthorized(staking_provider)
assert not pre_cbd_application.isOperatorConfirmed(staking_provider)
assert not pre_cbd_application.stakingProviderInfo(staking_provider)[OPERATOR_CONFIRMED_SLOT]
Expand All @@ -648,5 +688,7 @@ def test_resync(accounts, threshold_staking, pre_cbd_application, stake_info):
threshold_staking.setAuthorized(staking_provider, 0, sender=creator)
pre_cbd_application.setUpdatableStakeInfo(ZERO_ADDRESS, sender=creator)
pre_cbd_application.resynchronizeAuthorization(staking_provider, sender=creator)
assert pre_cbd_application.authorizedStake(staking_provider) == 0
assert stake_info.authorizedStake(staking_provider) == value
assert pre_cbd_application.getOperatorFromStakingProvider(staking_provider) == ZERO_ADDRESS
assert stake_info.stakes(staking_provider)[STAKE_INFO_OPERATOR_SLOT] == staking_provider
18 changes: 9 additions & 9 deletions tests/application/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.getStakingProvidersLength() == 1
assert pre_cbd_application.stakingProviders(0) == staking_provider_3
assert stake_info.stakes(staking_provider_3)[STAKE_INFO_OPERATOR_SLOT] == ZERO_ADDRESS
assert stake_info.operatorToProvider(operator1) == ZERO_ADDRESS
assert stake_info.stakingProviderFromOperator(operator1) == ZERO_ADDRESS

# No active stakingProviders before confirmation
all_locked, staking_providers = pre_cbd_application.getActiveStakingProviders(0, 0)
Expand All @@ -108,7 +108,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.stakingProviderInfo(staking_provider_3)[CONFIRMATION_SLOT]
assert pre_cbd_application.isOperatorConfirmed(operator1)
assert stake_info.stakes(staking_provider_3)[STAKE_INFO_OPERATOR_SLOT] == operator1
assert stake_info.operatorToProvider(operator1) == staking_provider_3
assert stake_info.stakingProviderFromOperator(operator1) == staking_provider_3

events = pre_cbd_application.OperatorBonded.from_receipt(tx)
assert len(events) == 1
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.getStakingProvidersLength() == 1
assert pre_cbd_application.stakingProviders(0) == staking_provider_3
assert stake_info.stakes(staking_provider_3)[STAKE_INFO_OPERATOR_SLOT] == ZERO_ADDRESS
assert stake_info.operatorToProvider(operator1) == ZERO_ADDRESS
assert stake_info.stakingProviderFromOperator(operator1) == ZERO_ADDRESS

# Resetting operator removes from active list before next confirmation
all_locked, staking_providers = pre_cbd_application.getActiveStakingProviders(0, 0)
Expand All @@ -186,7 +186,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.getStakingProvidersLength() == 1
assert pre_cbd_application.stakingProviders(0) == staking_provider_3
assert stake_info.stakes(staking_provider_3)[STAKE_INFO_OPERATOR_SLOT] == ZERO_ADDRESS
assert stake_info.operatorToProvider(operator2) == ZERO_ADDRESS
assert stake_info.stakingProviderFromOperator(operator2) == ZERO_ADDRESS

events = pre_cbd_application.OperatorBonded.from_receipt(tx)
assert len(events) == 1
Expand All @@ -205,7 +205,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.isOperatorConfirmed(operator2)
assert pre_cbd_application.stakingProviderInfo(staking_provider_3)[CONFIRMATION_SLOT]
assert stake_info.stakes(staking_provider_3)[STAKE_INFO_OPERATOR_SLOT] == operator2
assert stake_info.operatorToProvider(operator2) == staking_provider_3
assert stake_info.stakingProviderFromOperator(operator2) == staking_provider_3

# Another staking provider can bond a free operator
assert pre_cbd_application.authorizedOverall() == min_authorization
Expand All @@ -219,7 +219,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.stakingProviders(1) == staking_provider_4
assert pre_cbd_application.authorizedOverall() == min_authorization
assert stake_info.stakes(staking_provider_4)[STAKE_INFO_OPERATOR_SLOT] == ZERO_ADDRESS
assert stake_info.operatorToProvider(operator1) == ZERO_ADDRESS
assert stake_info.stakingProviderFromOperator(operator1) == ZERO_ADDRESS

events = pre_cbd_application.OperatorBonded.from_receipt(tx)
assert len(events) == 1
Expand All @@ -241,7 +241,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.stakingProviderInfo(staking_provider_4)[CONFIRMATION_SLOT]
assert pre_cbd_application.authorizedOverall() == 2 * min_authorization
assert stake_info.stakes(staking_provider_4)[STAKE_INFO_OPERATOR_SLOT] == operator1
assert stake_info.operatorToProvider(operator1) == staking_provider_4
assert stake_info.stakingProviderFromOperator(operator1) == staking_provider_4

chain.pending_timestamp += min_operator_seconds
tx = pre_cbd_application.bondOperator(staking_provider_4, operator3, sender=staking_provider_4)
Expand All @@ -256,7 +256,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
assert pre_cbd_application.stakingProviders(1) == staking_provider_4
assert pre_cbd_application.authorizedOverall() == min_authorization
assert stake_info.stakes(staking_provider_4)[STAKE_INFO_OPERATOR_SLOT] == ZERO_ADDRESS
assert stake_info.operatorToProvider(operator1) == ZERO_ADDRESS
assert stake_info.stakingProviderFromOperator(operator1) == ZERO_ADDRESS

# Resetting operator removes from active list before next confirmation
all_locked, staking_providers = pre_cbd_application.getActiveStakingProviders(1, 0)
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_bond_operator(accounts, threshold_staking, pre_cbd_application, stake_i
)
pre_cbd_application.confirmOperatorAddress(sender=staking_provider_1)
assert stake_info.stakes(staking_provider_1)[STAKE_INFO_OPERATOR_SLOT] == staking_provider_1
assert stake_info.operatorToProvider(staking_provider_1) == staking_provider_1
assert stake_info.stakingProviderFromOperator(staking_provider_1) == staking_provider_1

# If stake will be less than minimum then provider is not active
threshold_staking.authorizationIncreased(
Expand Down

0 comments on commit f463e71

Please sign in to comment.