Skip to content

Commit

Permalink
pass checked
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayerleier committed Dec 5, 2024
1 parent 6222fe8 commit f73b35a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 172 deletions.
58 changes: 43 additions & 15 deletions src/iBTC_NetworkMiddleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
EnumerableMap.AddressToUintMap private operators;
EnumerableMap.AddressToUintMap private vaults;

modifier updateStakeCache(uint48 epoch) {
modifier updateStakeCache(
uint48 epoch
) {
if (!totalStakeCached[epoch]) {
calcAndCacheStakes(epoch);
}
Expand Down Expand Up @@ -107,11 +109,15 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
subnetworksCnt = 1;
}

function getEpochStartTs(uint48 epoch) public view returns (uint48 timestamp) {
function getEpochStartTs(
uint48 epoch
) public view returns (uint48 timestamp) {
return START_TIME + epoch * EPOCH_DURATION;
}

function getEpochAtTs(uint48 timestamp) public view returns (uint48 epoch) {
function getEpochAtTs(
uint48 timestamp
) public view returns (uint48 epoch) {
return (timestamp - START_TIME) / EPOCH_DURATION;
}

Expand Down Expand Up @@ -171,15 +177,21 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
updateKey(operator, key);
}

function pauseOperator(address operator) external onlyOwner {
function pauseOperator(
address operator
) external onlyOwner {
operators.disable(operator);
}

function unpauseOperator(address operator) external onlyOwner {
function unpauseOperator(
address operator
) external onlyOwner {
operators.enable(operator);
}

function unregisterOperator(address operator) external onlyOwner {
function unregisterOperator(
address operator
) external onlyOwner {
(, uint48 disabledTime) = operators.getTimes(operator);

if (disabledTime == 0 || disabledTime + SLASHING_WINDOW > Time.timestamp()) {
Expand All @@ -189,7 +201,9 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
operators.remove(operator);
}

function registerVault(address vault) external onlyOwner {
function registerVault(
address vault
) external onlyOwner {
if (vaults.contains(vault)) {
revert VaultAlreadyRegistred();
}
Expand All @@ -213,15 +227,21 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
vaults.enable(vault);
}

function pauseVault(address vault) external onlyOwner {
function pauseVault(
address vault
) external onlyOwner {
vaults.disable(vault);
}

function unpauseVault(address vault) external onlyOwner {
function unpauseVault(
address vault
) external onlyOwner {
vaults.enable(vault);
}

function unregisterVault(address vault) external onlyOwner {
function unregisterVault(
address vault
) external onlyOwner {
(, uint48 disabledTime) = vaults.getTimes(vault);

if (disabledTime == 0 || disabledTime + SLASHING_WINDOW > Time.timestamp()) {
Expand Down Expand Up @@ -266,14 +286,18 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
return stake;
}

function getTotalStake(uint48 epoch) public view returns (uint256) {
function getTotalStake(
uint48 epoch
) public view returns (uint256) {
if (totalStakeCached[epoch]) {
return totalStakeCache[epoch];
}
return _calcTotalStake(epoch);
}

function getValidatorSet(uint48 epoch) public view returns (ValidatorData[] memory validatorsData) {
function getValidatorSet(
uint48 epoch
) public view returns (ValidatorData[] memory validatorsData) {
uint48 epochStartTs = getEpochStartTs(epoch);

validatorsData = new ValidatorData[](operators.length());
Expand Down Expand Up @@ -325,7 +349,7 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {

// simple pro-rata slasher
for (uint256 i; i < vaults.length(); ++i) {
(address vault, uint48 enabledTime, uint48 disabledTime) = operators.atWithTimes(i);
(address vault, uint48 enabledTime, uint48 disabledTime) = vaults.atWithTimes(i);

// just skip the vault if it was enabled after the target epoch or not enabled
if (!_wasActiveAt(enabledTime, disabledTime, epochStartTs)) {
Expand All @@ -340,7 +364,9 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
}
}

function calcAndCacheStakes(uint48 epoch) public returns (uint256 totalStake) {
function calcAndCacheStakes(
uint48 epoch
) public returns (uint256 totalStake) {
uint48 epochStartTs = getEpochStartTs(epoch);

// for epoch older than SLASHING_WINDOW total stake can be invalidated (use cache)
Expand Down Expand Up @@ -370,7 +396,9 @@ contract NetworkMiddleware is SimpleKeyRegistry32, Ownable {
totalStakeCache[epoch] = totalStake;
}

function _calcTotalStake(uint48 epoch) private view returns (uint256 totalStake) {
function _calcTotalStake(
uint48 epoch
) private view returns (uint256 totalStake) {
uint48 epochStartTs = getEpochStartTs(epoch);

// for epoch older than SLASHING_WINDOW total stake can be invalidated (use cache)
Expand Down
Loading

0 comments on commit f73b35a

Please sign in to comment.