Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tracking of overall authorized amount for each application #166

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adds tracking of overall authorized amount for each application
vzotova committed Mar 23, 2024
commit ede54f440907766bd8c01b1d8ea1cbbb6368045f
44 changes: 43 additions & 1 deletion contracts/staking/TokenStaking.sol
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
struct ApplicationInfo {
ApplicationStatus status;
address panicButton;
uint96 authorizedOverall;
}

struct SlashingEvent {
@@ -328,6 +329,36 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
emit ApplicationStatusChanged(application, ApplicationStatus.APPROVED);
}

/// @notice Update `authorizedOverall` field for each application.
/// Can only be called by Governance. `authorizedOVerall` should be
/// equal to the sum of all authorization for particular application.
// TODO remove after use
function updateAuthorizedOverall(int96[] memory authorizedOverallValues)
external
onlyGovernance
{
require(
authorizedOverallValues.length == applications.length,
"Wrong input parameters"
);
for (uint256 i = 0; i < applications.length; i++) {
address application = applications[i];
ApplicationInfo storage applicationStruct = applicationInfo[
application
];
int96 authorizedOverall = authorizedOverallValues[i];
if (authorizedOverall >= 0) {
applicationStruct.authorizedOverall += uint96(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly setting applicationStruct.authorizedOverall, rather than using int96 increments/decrements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because with just setting value we always can face frontrun tx that will change authorization and now we have to execute one more tx. With addition or subtraction - maximum we need two txs even if one/many will be executed right before upgrade

authorizedOverall
);
} else {
applicationStruct.authorizedOverall -= uint96(
-authorizedOverall
);
}
}
}

/// @notice Increases the authorization of the given staking provider for
/// the given application by the given amount. Can only be called by
/// the given staking provider’s authorizer.
@@ -370,6 +401,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
);
require(availableTValue >= amount, "Not enough stake to authorize");
authorization.authorized += amount;
applicationStruct.authorizedOverall += amount;
emit AuthorizationIncreased(
stakingProvider,
application,
@@ -446,6 +478,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {

uint96 fromAmount = authorization.authorized;
authorization.authorized -= authorization.deauthorizing;
applicationStruct.authorizedOverall -= authorization.deauthorizing;
authorization.deauthorizing = 0;
emit AuthorizationDecreaseApproved(
stakingProvider,
@@ -469,8 +502,11 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
address stakingProvider,
address application
) external override {
ApplicationInfo storage applicationStruct = applicationInfo[
application
];
require(
applicationInfo[application].status == ApplicationStatus.DISABLED,
applicationStruct.status == ApplicationStatus.DISABLED,
"Application is not disabled"
);

@@ -483,6 +519,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
require(fromAmount > 0, "Application is not authorized");
authorization.authorized = 0;
authorization.deauthorizing = 0;
applicationStruct.authorizedOverall -= fromAmount;

emit AuthorizationDecreaseApproved(
stakingProvider,
@@ -612,6 +649,8 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
.authorizations[application];
uint96 fromAmount = authorization.authorized;
authorization.authorized += amount;
//slither-disable-next-line reentrancy-benign
applicationInfo[application].authorizedOverall += amount;
emit AuthorizationIncreased(
stakingProvider,
application,
@@ -1116,6 +1155,9 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
if (authorization.authorized > totalStake) {
authorization.authorized = totalStake;
}
applicationInfo[authorizedApplication].authorizedOverall -=
fromAmount -
authorization.authorized;

bool successful = true;
//slither-disable-next-line calls-loop
Loading