Skip to content

Commit 07e49aa

Browse files
authored
Merge pull request #87 from threshold-network/reduce-all
Involuntarily decrease all authorizations in case of slashing
2 parents 702aa5f + 225c4e2 commit 07e49aa

File tree

2 files changed

+91
-116
lines changed

2 files changed

+91
-116
lines changed

contracts/staking/TokenStaking.sol

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
7777
struct SlashingEvent {
7878
address stakingProvider;
7979
uint96 amount;
80-
address application;
8180
}
8281

8382
uint256 internal constant SLASHING_REWARD_PERCENT = 5;
@@ -1000,8 +999,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
1000999
authorizationDecrease(
10011000
stakingProvider,
10021001
stakingProviderStruct,
1003-
slashedAmount,
1004-
address(0)
1002+
slashedAmount
10051003
);
10061004
}
10071005

@@ -1044,8 +1042,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
10441042
authorizationDecrease(
10451043
stakingProvider,
10461044
stakingProviderStruct,
1047-
slashedAmount,
1048-
address(0)
1045+
slashedAmount
10491046
);
10501047
decreaseStakeCheckpoint(
10511048
stakingProvider,
@@ -1472,11 +1469,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
14721469
continue;
14731470
}
14741471
slashingQueue.push(
1475-
SlashingEvent(
1476-
stakingProvider,
1477-
amountToSlash.toUint96(),
1478-
msg.sender
1479-
)
1472+
SlashingEvent(stakingProvider, amountToSlash.toUint96())
14801473
);
14811474
}
14821475

@@ -1548,8 +1541,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
15481541
authorizationDecrease(
15491542
slashing.stakingProvider,
15501543
stakingProviderStruct,
1551-
slashedAmount,
1552-
slashing.application
1544+
slashedAmount
15531545
);
15541546
uint96 newStake = stakingProviderStruct.tStake +
15551547
stakingProviderStruct.keepInTStake +
@@ -1561,8 +1553,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
15611553
function authorizationDecrease(
15621554
address stakingProvider,
15631555
StakingProviderInfo storage stakingProviderStruct,
1564-
uint96 slashedAmount,
1565-
address application
1556+
uint96 slashedAmount
15661557
) internal {
15671558
uint96 totalStake = stakingProviderStruct.tStake +
15681559
stakingProviderStruct.nuInTStake +
@@ -1578,16 +1569,11 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
15781569
AppAuthorization storage authorization = stakingProviderStruct
15791570
.authorizations[authorizedApplication];
15801571
uint96 fromAmount = authorization.authorized;
1581-
if (
1582-
application == address(0) ||
1583-
authorizedApplication == application
1584-
) {
1585-
authorization.authorized -= MathUpgradeable
1586-
.min(fromAmount, slashedAmount)
1587-
.toUint96();
1588-
} else if (fromAmount <= totalStake) {
1589-
continue;
1590-
}
1572+
1573+
authorization.authorized -= MathUpgradeable
1574+
.min(fromAmount, slashedAmount)
1575+
.toUint96();
1576+
15911577
if (authorization.authorized > totalStake) {
15921578
authorization.authorized = totalStake;
15931579
}

test/staking/TokenStaking.test.js

Lines changed: 81 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6454,12 +6454,10 @@ describe("TokenStaking", () => {
64546454
expect(await tokenStaking.slashingQueue(0)).to.deep.equal([
64556455
stakingProvider.address,
64566456
amount,
6457-
application1Mock.address,
64586457
])
64596458
expect(await tokenStaking.slashingQueue(1)).to.deep.equal([
64606459
otherStaker.address,
64616460
amountToSlash,
6462-
application1Mock.address,
64636461
])
64646462
expect(await tokenStaking.getSlashingQueueLength()).to.equal(2)
64656463
})
@@ -6520,12 +6518,10 @@ describe("TokenStaking", () => {
65206518
expect(await tokenStaking.slashingQueue(0)).to.deep.equal([
65216519
stakingProvider.address,
65226520
amountToSlash,
6523-
application1Mock.address,
65246521
])
65256522
expect(await tokenStaking.slashingQueue(1)).to.deep.equal([
65266523
otherStaker.address,
65276524
amountToSlash,
6528-
application1Mock.address,
65296525
])
65306526
expect(await tokenStaking.getSlashingQueueLength()).to.equal(2)
65316527
})
@@ -6603,12 +6599,10 @@ describe("TokenStaking", () => {
66036599
expect(await tokenStaking.slashingQueue(0)).to.deep.equal([
66046600
otherStaker.address,
66056601
amountToSlash,
6606-
application1Mock.address,
66076602
])
66086603
expect(await tokenStaking.slashingQueue(1)).to.deep.equal([
66096604
stakingProvider.address,
66106605
amountToSlash,
6611-
application1Mock.address,
66126606
])
66136607
expect(await tokenStaking.getSlashingQueueLength()).to.equal(2)
66146608
})
@@ -6644,7 +6638,6 @@ describe("TokenStaking", () => {
66446638
expect(await tokenStaking.slashingQueue(0)).to.deep.equal([
66456639
stakingProvider.address,
66466640
amountToSlash,
6647-
application1Mock.address,
66486641
])
66496642
expect(await tokenStaking.getSlashingQueueLength()).to.equal(1)
66506643
})
@@ -6687,7 +6680,6 @@ describe("TokenStaking", () => {
66876680
expect(await tokenStaking.slashingQueue(0)).to.deep.equal([
66886681
otherStaker.address,
66896682
amountToSlash,
6690-
application1Mock.address,
66916683
])
66926684
})
66936685

@@ -7001,7 +6993,7 @@ describe("TokenStaking", () => {
70016993
).to.equal(0)
70026994
})
70036995

7004-
it("should decrease authorized amount only for one application", async () => {
6996+
it("should decrease authorized amounts only for one provider", async () => {
70056997
expect(
70066998
await tokenStaking.authorizedStake(
70076999
stakingProvider.address,
@@ -7013,7 +7005,7 @@ describe("TokenStaking", () => {
70137005
stakingProvider.address,
70147006
application2Mock.address
70157007
)
7016-
).to.equal(provider1Authorized2)
7008+
).to.equal(provider1Authorized2.sub(amountToSlash))
70177009
expect(
70187010
await tokenStaking.authorizedStake(
70197011
otherStaker.address,
@@ -7045,13 +7037,13 @@ describe("TokenStaking", () => {
70457037
).to.be.revertedWith("Too many applications")
70467038
})
70477039

7048-
it("should inform only one application", async () => {
7040+
it("should inform all applications", async () => {
70497041
expect(
70507042
await application1Mock.stakingProviders(stakingProvider.address)
70517043
).to.deep.equal([provider1Authorized1.sub(amountToSlash), Zero])
70527044
expect(
70537045
await application2Mock.stakingProviders(stakingProvider.address)
7054-
).to.deep.equal([provider1Authorized2, Zero])
7046+
).to.deep.equal([provider1Authorized2.sub(amountToSlash), Zero])
70557047
expect(
70567048
await application1Mock.stakingProviders(otherStaker.address)
70577049
).to.deep.equal([provider2Authorized1, Zero])
@@ -7268,99 +7260,96 @@ describe("TokenStaking", () => {
72687260
})
72697261
})
72707262

7271-
context(
7272-
"when decrease authorized amount to zero for one application",
7273-
() => {
7274-
const tAmount = initialStakerBalance
7263+
context("when decrease authorized amount to zero", () => {
7264+
const tAmount = initialStakerBalance
72757265

7276-
const amountToSlash = tAmount.div(3)
7277-
const authorized = amountToSlash
7266+
const amountToSlash = tAmount.div(3)
7267+
const authorized = amountToSlash
72787268

7279-
beforeEach(async () => {
7280-
await tokenStaking.connect(deployer).setAuthorizationCeiling(2)
7281-
await tokenStaking
7282-
.connect(deployer)
7283-
.approveApplication(application1Mock.address)
7284-
await tokenStaking
7285-
.connect(deployer)
7286-
.approveApplication(application2Mock.address)
7269+
beforeEach(async () => {
7270+
await tokenStaking.connect(deployer).setAuthorizationCeiling(2)
7271+
await tokenStaking
7272+
.connect(deployer)
7273+
.approveApplication(application1Mock.address)
7274+
await tokenStaking
7275+
.connect(deployer)
7276+
.approveApplication(application2Mock.address)
72877277

7288-
await tToken.connect(staker).approve(tokenStaking.address, tAmount)
7289-
await tokenStaking
7290-
.connect(staker)
7291-
.stake(
7292-
stakingProvider.address,
7293-
staker.address,
7294-
staker.address,
7295-
tAmount
7296-
)
7297-
await tokenStaking
7298-
.connect(staker)
7299-
.increaseAuthorization(
7300-
stakingProvider.address,
7301-
application2Mock.address,
7302-
authorized
7303-
)
7304-
await tokenStaking
7305-
.connect(staker)
7306-
.increaseAuthorization(
7307-
stakingProvider.address,
7308-
application1Mock.address,
7309-
authorized
7310-
)
7278+
await tToken.connect(staker).approve(tokenStaking.address, tAmount)
7279+
await tokenStaking
7280+
.connect(staker)
7281+
.stake(
7282+
stakingProvider.address,
7283+
staker.address,
7284+
staker.address,
7285+
tAmount
7286+
)
7287+
await tokenStaking
7288+
.connect(staker)
7289+
.increaseAuthorization(
7290+
stakingProvider.address,
7291+
application2Mock.address,
7292+
authorized
7293+
)
7294+
await tokenStaking
7295+
.connect(staker)
7296+
.increaseAuthorization(
7297+
stakingProvider.address,
7298+
application1Mock.address,
7299+
authorized
7300+
)
73117301

7312-
await application1Mock.slash(amountToSlash, [stakingProvider.address])
7302+
await application1Mock.slash(amountToSlash, [stakingProvider.address])
73137303

7314-
await tokenStaking.processSlashing(1)
7315-
})
7304+
await tokenStaking.processSlashing(1)
7305+
})
73167306

7317-
it("should decrease authorized amount", async () => {
7318-
expect(
7319-
await tokenStaking.authorizedStake(
7320-
stakingProvider.address,
7321-
application1Mock.address
7322-
)
7323-
).to.equal(0)
7324-
expect(
7325-
await tokenStaking.authorizedStake(
7326-
stakingProvider.address,
7327-
application2Mock.address
7328-
)
7329-
).to.equal(authorized)
7330-
})
7307+
it("should decrease authorized amount", async () => {
7308+
expect(
7309+
await tokenStaking.authorizedStake(
7310+
stakingProvider.address,
7311+
application1Mock.address
7312+
)
7313+
).to.equal(0)
7314+
expect(
7315+
await tokenStaking.authorizedStake(
7316+
stakingProvider.address,
7317+
application2Mock.address
7318+
)
7319+
).to.equal(0)
7320+
})
73317321

7332-
it("should allow to authorize one more application", async () => {
7333-
await tokenStaking
7334-
.connect(staker)
7335-
.increaseAuthorization(
7336-
stakingProvider.address,
7337-
application1Mock.address,
7338-
authorized
7339-
)
7322+
it("should allow to authorize one more application", async () => {
7323+
await tokenStaking
7324+
.connect(staker)
7325+
.increaseAuthorization(
7326+
stakingProvider.address,
7327+
application1Mock.address,
7328+
authorized
7329+
)
73407330

7341-
await tokenStaking
7331+
await tokenStaking
7332+
.connect(staker)
7333+
.increaseAuthorization(
7334+
stakingProvider.address,
7335+
application2Mock.address,
7336+
authorized
7337+
)
7338+
7339+
await tokenStaking
7340+
.connect(deployer)
7341+
.approveApplication(auxiliaryAccount.address)
7342+
await expect(
7343+
tokenStaking
73427344
.connect(staker)
73437345
.increaseAuthorization(
73447346
stakingProvider.address,
7345-
application2Mock.address,
7347+
auxiliaryAccount.address,
73467348
authorized
73477349
)
7348-
7349-
await tokenStaking
7350-
.connect(deployer)
7351-
.approveApplication(auxiliaryAccount.address)
7352-
await expect(
7353-
tokenStaking
7354-
.connect(staker)
7355-
.increaseAuthorization(
7356-
stakingProvider.address,
7357-
auxiliaryAccount.address,
7358-
authorized
7359-
)
7360-
).to.be.revertedWith("Too many applications")
7361-
})
7362-
}
7363-
)
7350+
).to.be.revertedWith("Too many applications")
7351+
})
7352+
})
73647353
})
73657354

73667355
describe("cleanAuthorizedApplications", () => {

0 commit comments

Comments
 (0)