Skip to content

Commit

Permalink
Adjusted WalletRegistry authorization unit tests to changes in stakin…
Browse files Browse the repository at this point in the history
…g contract

The change in the staking contract modified how authorized stake is reduced
after slashing, making the reduction "global", to all authorized applications,
and not only "local" to a single application, as it was initially.

The original approach was to decrease authorization on the slashing application
and decrease authorizations on other applications only if necessary, that is, if
the staked amount after the slashing was lower than the authorization on the
other application.

For example, let's assume the staking provider has 1000 T delegated and three
applications authorized:
- A application authorized for 950 T
- B application authorized for 500 T
- C application authorized for 100 T

If C slashed for 50 T, authorization on C would be reduced by 50 T,
to 100 - 50 = 50 T.

If A slashed for 950 T, authorizations on A would be reduced to 0, and
authorizations on B and C would be reduced to 50 T.

The new approach assumes all applications are decreasing authorizations in case
of slashing, no matter which application executed the slash.

If C slashed for 50 T, authorization on A would be reduced to 950 - 50 = 900 T,
authorization on B would be reduced to 500 - 50 = 450 T, and authorization on C
would be reduced to 100 - 50 = 50 T.

If A slashed for 950 T, authorizations on A, B, and C would be reduced to 0 T.

The unit tests had to be adjusted to reflect the new reality.

See threshold-network/solidity-contracts#87
  • Loading branch information
pdyraga committed Apr 26, 2022
1 parent 77faca6 commit a5a83e7
Showing 1 changed file with 10 additions and 33 deletions.
43 changes: 10 additions & 33 deletions solidity/ecdsa/test/WalletRegistry.Authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1473,12 +1473,7 @@ describe("WalletRegistry - Authorization", () => {
)

const slashingTo = minimumAuthorization.sub(1)
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing. To go below the minimum stake, we need
// to start slashing from the entire staked amount, not just the
// one authorized for WalletRegistry.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = authorizedAmount.sub(slashingTo)

await staking
.connect(slasher.wallet)
Expand Down Expand Up @@ -2232,12 +2227,7 @@ describe("WalletRegistry - Authorization", () => {
)

const slashingTo = minimumAuthorization.sub(1)
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing. To go below the minimum stake, we need
// to start slashing from the entire staked amount, not just the
// one authorized for WalletRegistry.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = authorizedAmount.sub(slashingTo)

await staking
.connect(slasher.wallet)
Expand Down Expand Up @@ -2782,21 +2772,15 @@ describe("WalletRegistry - Authorization", () => {
await createSnapshot()

slashingTo = minimumAuthorization.sub(1)
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing. To go below the minimum stake, we need
// to start slashing from the entire staked amount, not just the
// one authorized for WalletRegistry.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = initialIncrease.sub(slashingTo)

await staking
.connect(slasher.wallet)
.slash(slashedAmount, [stakingProvider.address])
await staking.connect(thirdParty).processSlashing(1)

// Given that we slashed from the entire staked amount, we need to give
// the stake owner some more T and let them top-up the stake before they
// increase the authorization again.
// Give the stake owner some more T and let them top-up the stake before
// they increase the authorization again.
secondIncrease = to1e18(10000)
await t.connect(deployer).mint(owner.address, secondIncrease)
await t.connect(owner).approve(staking.address, secondIncrease)
Expand Down Expand Up @@ -2851,10 +2835,7 @@ describe("WalletRegistry - Authorization", () => {
.updateOperatorStatus(operator.address)

slashingTo = initialIncrease.sub(to1e18(100))
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = initialIncrease.sub(slashingTo)

await staking
.connect(slasher.wallet)
Expand Down Expand Up @@ -2898,10 +2879,7 @@ describe("WalletRegistry - Authorization", () => {
.updateOperatorStatus(operator.address)

slashingTo = initialIncrease.sub(to1e18(100))
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = initialIncrease.sub(slashingTo)

await staking
.connect(slasher.wallet)
Expand Down Expand Up @@ -2955,10 +2933,9 @@ describe("WalletRegistry - Authorization", () => {
)

slashingTo = initialIncrease.sub(to1e18(2500))
// Note that we slash from the entire staked amount given that the
// initially authorized amount is less than staked amount and it is
// another application slashing.
const slashedAmount = stakedAmount.sub(slashingTo)
const slashedAmount = initialIncrease
.sub(decreasedAmount)
.sub(slashingTo)

await staking
.connect(slasher.wallet)
Expand Down

0 comments on commit a5a83e7

Please sign in to comment.