Skip to content

Commit 85a1c01

Browse files
committed
Replace all integer casts with SafeCast
1 parent ef22657 commit 85a1c01

File tree

8 files changed

+41
-27
lines changed

8 files changed

+41
-27
lines changed

contracts/contract/deposit/RocketDepositPool.sol

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22
pragma solidity 0.8.30;
33

4+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
5+
46
import {RocketNetworkBalancesInterface} from "../../interface/network/RocketNetworkBalancesInterface.sol";
57
import {AddressQueueStorageInterface} from "../../interface/util/AddressQueueStorageInterface.sol";
68
import {LinkedListStorageInterface} from "../../interface/util/LinkedListStorageInterface.sol";
@@ -488,10 +490,10 @@ contract RocketDepositPool is RocketBase, RocketDepositPoolInterface, RocketVaul
488490
// Enqueue megapool
489491
bytes32 namespace = getQueueNamespace(_expressQueue);
490492
LinkedListStorageInterface.DepositQueueValue memory value = LinkedListStorageInterface.DepositQueueValue({
491-
receiver: msg.sender, // Megapool address
492-
validatorId: _validatorId, // Incrementing id per validator in a megapool
493-
suppliedValue: uint32(_bondAmount / milliToWei), // NO bond amount
494-
requestedValue: uint32(_amount / milliToWei) // Amount being requested
493+
receiver: msg.sender, // Megapool address
494+
validatorId: _validatorId, // Incrementing id per validator in a megapool
495+
suppliedValue: SafeCast.toUint32(_bondAmount / milliToWei), // NO bond amount
496+
requestedValue: SafeCast.toUint32(_amount / milliToWei) // Amount being requested
495497
});
496498
LinkedListStorageInterface linkedListStorage = LinkedListStorageInterface(getContractAddress("linkedListStorage"));
497499
linkedListStorage.enqueueItem(namespace, value);

contracts/contract/megapool/RocketMegapoolDelegate.sol

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22
pragma solidity 0.8.30;
33

4+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
5+
46
import {RocketStorageInterface} from "../../interface/RocketStorageInterface.sol";
57
import {DepositInterface} from "../../interface/casper/DepositInterface.sol";
68
import {RocketDAOProtocolSettingsMegapoolInterface} from "../../interface/dao/protocol/settings/RocketDAOProtocolSettingsMegapoolInterface.sol";
@@ -194,15 +196,15 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
194196
require(debt == 0, "Cannot create validator while debt exists");
195197
// Setup new validator
196198
RocketDepositPoolInterface rocketDepositPool = _getRocketDepositPool();
197-
uint32 validatorId = uint32(numValidators);
199+
uint32 validatorId = numValidators;
198200
unchecked { // Infeasible overflow
199201
numValidators += 1;
200202
}
201203
{
202204
ValidatorInfo memory validator;
203205
validator.inQueue = true;
204-
validator.lastRequestedBond = uint32(_bondAmount / milliToWei);
205-
validator.lastRequestedValue = uint32(fullDepositValue / milliToWei);
206+
validator.lastRequestedBond = SafeCast.toUint32(_bondAmount / milliToWei);
207+
validator.lastRequestedValue = SafeCast.toUint32(fullDepositValue / milliToWei);
206208
validator.expressUsed = _useExpressTicket;
207209
validators[validatorId] = validator;
208210
}
@@ -211,7 +213,7 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
211213
pubkeys[validatorId] = _validatorPubkey;
212214
// Compute and verify supplied deposit data root is correct
213215
// Note: We check this here to ensure the deposit contract will not revert when executing prestake
214-
bytes32 depositDataRoot = _computeDepositDataRoot(_validatorPubkey, _validatorSignature, uint64(prestakeValue / 1 gwei));
216+
bytes32 depositDataRoot = _computeDepositDataRoot(_validatorPubkey, _validatorSignature, SafeCast.toUint64(prestakeValue / 1 gwei));
215217
require(depositDataRoot == _depositDataRoot, "Invalid deposit data root");
216218
// Increase queued capital balances
217219
userQueuedCapital += fullDepositValue - _bondAmount;
@@ -296,15 +298,15 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
296298
// Update validator status
297299
validator.inQueue = false;
298300
validator.inPrestake = true;
299-
validator.lastAssignmentTime = uint32(block.timestamp);
301+
validator.lastAssignmentTime = SafeCast.toUint32(block.timestamp);
300302
// Record value assigned from deposit pool (subtract prestakeValue as it is going to deposit contract now)
301-
validator.depositValue += uint32(prestakeValue / milliToWei);
303+
validator.depositValue += SafeCast.toUint32(prestakeValue / milliToWei);
302304
assignedValue += msg.value - prestakeValue;
303305
validators[_validatorId] = validator;
304306
// Execute prestake operation
305307
bytes memory signature = prestakeSignatures[_validatorId];
306308
bytes memory pubkey = pubkeys[_validatorId];
307-
bytes32 depositDataRoot = _computeDepositDataRoot(pubkey, signature, uint64(prestakeValue / 1 gwei));
309+
bytes32 depositDataRoot = _computeDepositDataRoot(pubkey, signature, SafeCast.toUint64(prestakeValue / 1 gwei));
308310
casperDeposit.deposit{value: prestakeValue}(pubkey, abi.encodePacked(getWithdrawalCredentials()), signature, depositDataRoot);
309311
// Remove value from queued balances and add to staking values
310312
uint256 assignedUserCapital = (validator.lastRequestedValue - validator.lastRequestedBond) * milliToWei;
@@ -339,13 +341,13 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
339341
validator.lastAssignmentTime = 0;
340342
validator.lastRequestedBond = 0;
341343
validator.lastRequestedValue = 0;
342-
validator.depositValue += uint32(lastRequestedValue - prestakeValue / milliToWei);
344+
validator.depositValue += SafeCast.toUint32(lastRequestedValue - prestakeValue / milliToWei);
343345
validators[_validatorId] = validator;
344346
// Perform remaining 31 ETH stake onto beaconchain
345347
// Note: Signature is not verified on subsequent deposits and we know the validator is valid due to state proof
346348
bytes memory signature = hex"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
347349
bytes memory pubkey = pubkeys[_validatorId];
348-
bytes32 depositDataRoot = _computeDepositDataRoot(pubkey, signature, uint64(assignedUsed / 1 gwei));
350+
bytes32 depositDataRoot = _computeDepositDataRoot(pubkey, signature, SafeCast.toUint64(assignedUsed / 1 gwei));
349351
casperDeposit.deposit{value: assignedUsed}(pubkey, abi.encodePacked(getWithdrawalCredentials()), signature, depositDataRoot);
350352
// Emit event
351353
emit MegapoolValidatorStaked(_validatorId, block.timestamp);
@@ -507,7 +509,7 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
507509
if (lastDistributionTime == 0) return (_amount, 0, 0, 0);
508510
// Calculate split based on capital ratio and average commission since last distribute
509511
RocketNetworkRevenuesInterface rocketNetworkRevenues = RocketNetworkRevenuesInterface(getContractAddress("rocketNetworkRevenues"));
510-
uint64 lastDistributionTime64 = uint64(lastDistributionTime);
512+
uint64 lastDistributionTime64 = SafeCast.toUint64(lastDistributionTime);
511513
(, uint256 voterShare, uint256 protocolDAOShare, uint256 rethShare) = rocketNetworkRevenues.calculateSplit(lastDistributionTime64);
512514
uint256 averageCapitalRatio = rocketNetworkRevenues.getNodeAverageCapitalRatioSince(nodeAddress, lastDistributionTime64);
513515
// Sanity check input values
@@ -538,7 +540,7 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
538540
}
539541
}
540542
// Update lockedTime to current time
541-
validator.lockedTime = uint64(block.timestamp);
543+
validator.lockedTime = SafeCast.toUint64(block.timestamp);
542544
validators[_validatorId] = validator;
543545
// Emit event
544546
emit MegapoolValidatorLocked(_validatorId, block.timestamp);
@@ -633,7 +635,7 @@ contract RocketMegapoolDelegate is RocketMegapoolDelegateBase, RocketMegapoolDel
633635
// Mark as exited
634636
validator.exited = true;
635637
validator.exiting = false;
636-
validator.exitBalance = uint64(_amountInGwei);
638+
validator.exitBalance = _amountInGwei;
637639
uint256 withdrawalBalance = uint256(_amountInGwei) * 1 gwei;
638640
validators[_validatorId] = validator;
639641
// Handle dissolved recovery

contracts/contract/megapool/RocketMegapoolManager.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ contract RocketMegapoolManager is RocketBase, RocketMegapoolManagerInterface {
4444
function addValidator(address _megapoolAddress, uint32 _validatorId, bytes calldata _pubkey) override external onlyLatestContract("rocketMegapoolManager", address(this)) onlyLatestContract("rocketNodeDeposit", msg.sender) {
4545
uint256 index = getUint(setCountKey);
4646
setUint(setCountKey, index + 1);
47-
uint256 encoded = (uint256(uint160(_megapoolAddress)) << 96) | uint32(_validatorId);
47+
uint256 encoded = (uint256(uint160(_megapoolAddress)) << 96) | _validatorId;
4848
setUint(keccak256(abi.encodePacked("megapool.validator.set", index)), encoded);
4949
// Add pubkey => megapool mapping and ensure uniqueness
5050
bytes32 key = keccak256(abi.encodePacked("validator.megapool", _pubkey));

contracts/contract/megapool/RocketMegapoolPenalties.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22
pragma solidity 0.8.30;
33

4+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
5+
46
import {RocketBase} from "../RocketBase.sol";
57
import {RocketDAONodeTrustedInterface} from "../../interface/dao/node/RocketDAONodeTrustedInterface.sol";
68
import {RocketDAOProtocolSettingsMegapoolInterface} from "../../interface/dao/protocol/settings/RocketDAOProtocolSettingsMegapoolInterface.sol";
@@ -96,7 +98,7 @@ contract RocketMegapoolPenalties is RocketBase, RocketMegapoolPenaltiesInterface
9698
if (block.timestamp > penaltyMaximumPeriod) {
9799
earlierTime = block.timestamp - penaltyMaximumPeriod;
98100
}
99-
uint256 earlierRunningTotal = uint256(rocketNetworkSnapshotsTime.lookup(penaltyKey, uint64(earlierTime)));
101+
uint256 earlierRunningTotal = uint256(rocketNetworkSnapshotsTime.lookup(penaltyKey, SafeCast.toUint64(earlierTime)));
100102
// Get current running total
101103
(,, uint192 currentRunningTotal) = rocketNetworkSnapshotsTime.latest(penaltyKey);
102104
// Cap the penalty at the maximum amount based on past 7 days
@@ -137,7 +139,7 @@ contract RocketMegapoolPenalties is RocketBase, RocketMegapoolPenaltiesInterface
137139
if (block.timestamp > penaltyMaximumPeriod) {
138140
earlierTime = block.timestamp - penaltyMaximumPeriod;
139141
}
140-
uint256 earlierRunningTotal = rocketNetworkSnapshotsTime.lookup(penaltyKey, uint64(earlierTime));
142+
uint256 earlierRunningTotal = rocketNetworkSnapshotsTime.lookup(penaltyKey, SafeCast.toUint64(earlierTime));
141143
// Get current running total
142144
(,, uint192 currentRunningTotal) = rocketNetworkSnapshotsTime.latest(penaltyKey);
143145
// Prevent the running penalty total from exceeding the maximum amount
@@ -146,7 +148,7 @@ contract RocketMegapoolPenalties is RocketBase, RocketMegapoolPenaltiesInterface
146148
uint256 currentMaxPenalty = maxPenalty - currentTotal;
147149
require(_amount <= currentMaxPenalty, "Max penalty exceeded");
148150
// Insert new running total
149-
rocketNetworkSnapshotsTime.push(penaltyKey, currentRunningTotal + uint192(_amount));
151+
rocketNetworkSnapshotsTime.push(penaltyKey, currentRunningTotal + SafeCast.toUint192(_amount));
150152
// Call megapool to increase debt
151153
RocketMegapoolDelegateInterface(_megapool).applyPenalty(_amount);
152154
}

contracts/contract/network/RocketNetworkPenalties.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22
pragma solidity 0.8.30;
33

4+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
5+
46
import {RocketDAONodeTrustedInterface} from "../../interface/dao/node/RocketDAONodeTrustedInterface.sol";
57
import {RocketDAOProtocolSettingsMinipoolInterface} from "../../interface/dao/protocol/settings/RocketDAOProtocolSettingsMinipoolInterface.sol";
68
import {RocketDAOProtocolSettingsNetworkInterface} from "../../interface/dao/protocol/settings/RocketDAOProtocolSettingsNetworkInterface.sol";
@@ -91,7 +93,7 @@ contract RocketNetworkPenalties is RocketBase, RocketNetworkPenaltiesInterface {
9193
if (block.timestamp > penaltyMaximumPeriod) {
9294
earlierTime = block.timestamp - penaltyMaximumPeriod;
9395
}
94-
uint256 earlierRunningTotal = uint256(rocketNetworkSnapshotsTime.lookup(penaltyKey, uint64(earlierTime)));
96+
uint256 earlierRunningTotal = uint256(rocketNetworkSnapshotsTime.lookup(penaltyKey, SafeCast.toUint64(earlierTime)));
9597
// Get current running total
9698
(,, uint192 currentRunningTotal) = rocketNetworkSnapshotsTime.latest(penaltyKey);
9799
// Cap the penalty at the maximum amount based on past 7 days
@@ -131,7 +133,7 @@ contract RocketNetworkPenalties is RocketBase, RocketNetworkPenaltiesInterface {
131133
if (block.timestamp > penaltyMaximumPeriod) {
132134
earlierTime = block.timestamp - penaltyMaximumPeriod;
133135
}
134-
uint256 earlierRunningTotal = rocketNetworkSnapshotsTime.lookup(penaltyKey, uint64(earlierTime));
136+
uint256 earlierRunningTotal = rocketNetworkSnapshotsTime.lookup(penaltyKey, SafeCast.toUint64(earlierTime));
135137
// Get current running total
136138
(,, uint192 currentRunningTotal) = rocketNetworkSnapshotsTime.latest(penaltyKey);
137139
// Prevent the running penalty total from exceeding the maximum amount

contracts/contract/network/RocketNetworkRevenues.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-3.0-only
22
pragma solidity 0.8.30;
33

4+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
5+
46
import {RocketBase} from "../RocketBase.sol";
57
import {RocketNetworkRevenuesInterface} from "../../interface/network/RocketNetworkRevenuesInterface.sol";
68
import {RocketNetworkSnapshotsTimeInterface} from "../../interface/network/RocketNetworkSnapshotsTimeInterface.sol";
@@ -177,7 +179,7 @@ contract RocketNetworkRevenues is RocketBase, RocketNetworkRevenuesInterface {
177179
uint256 durationSinceCheckpoint = (block.timestamp - checkpointTime);
178180
uint256 currentAccum = uint256(checkpointValue) + (valueAtTime * durationSinceCheckpoint);
179181
// Calculate the accumulator at _sinceTime
180-
(checkpointExists, checkpointTime, checkpointValue) = _rocketNetworkSnapshotsTime.lookupCheckpoint(_key, uint64(_sinceTime));
182+
(checkpointExists, checkpointTime, checkpointValue) = _rocketNetworkSnapshotsTime.lookupCheckpoint(_key, SafeCast.toUint64(_sinceTime));
181183
require(!_mustExist || checkpointExists, "Snapshot does not exist");
182184
valueKey = bytes32(uint256(_key) + checkpointTime);
183185
valueAtTime = getUint(valueKey);
@@ -191,7 +193,7 @@ contract RocketNetworkRevenues is RocketBase, RocketNetworkRevenuesInterface {
191193

192194
/// @dev Calculates the cumulative value of the accumulator at a given timestamp
193195
function _getAccumulatorAt(RocketNetworkSnapshotsTimeInterface _rocketNetworkSnapshotsTime, bytes32 _key, uint256 _time, bool _mustExist) internal view returns (uint256) {
194-
(bool checkpointExists, uint64 checkpointTime, uint192 checkpointValue) = _rocketNetworkSnapshotsTime.lookupCheckpoint(_key, uint64(_time));
196+
(bool checkpointExists, uint64 checkpointTime, uint192 checkpointValue) = _rocketNetworkSnapshotsTime.lookupCheckpoint(_key, SafeCast.toUint64(_time));
195197
require(!_mustExist || checkpointExists, "Snapshot does not exist");
196198
if (!checkpointExists) return 0;
197199
bytes32 valueKey = bytes32(uint256(_key) + checkpointTime);
@@ -215,7 +217,7 @@ contract RocketNetworkRevenues is RocketBase, RocketNetworkRevenuesInterface {
215217
function _setShare(bytes32 _key, uint256 _newShare) internal {
216218
RocketNetworkSnapshotsTimeInterface rocketNetworkSnapshotsTime = RocketNetworkSnapshotsTimeInterface(getContractAddress("rocketNetworkSnapshotsTime"));
217219
uint256 currentAccum = _getAccumulatorAt(rocketNetworkSnapshotsTime, _key, block.timestamp, false);
218-
rocketNetworkSnapshotsTime.push(_key, uint192(currentAccum));
220+
rocketNetworkSnapshotsTime.push(_key, SafeCast.toUint192(currentAccum));
219221
uint256 newShareScaled = _newShare / shareScale;
220222
bytes32 valueKey = bytes32(uint256(_key) + block.timestamp);
221223
setUint(valueKey, newShareScaled);

contracts/contract/network/RocketNetworkSnapshots.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Adapted from OpenZeppelin `Checkpoints` contract
44
pragma solidity 0.8.30;
55

6+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
7+
68
import {RocketStorageInterface} from "../../interface/RocketStorageInterface.sol";
79
import {RocketNetworkSnapshotsInterface} from "../../interface/network/RocketNetworkSnapshotsInterface.sol";
810
import {RocketBase} from "../RocketBase.sol";
@@ -106,7 +108,7 @@ contract RocketNetworkSnapshots is RocketBase, RocketNetworkSnapshotsInterface {
106108

107109
/// @dev Inserts a value into a snapshot set
108110
function _insert(bytes32 _key, uint224 _value) internal {
109-
uint32 blockNumber = uint32(block.number);
111+
uint32 blockNumber = SafeCast.toUint32(block.number);
110112
uint256 pos = length(_key);
111113

112114
if (pos > 0) {

contracts/contract/network/RocketNetworkSnapshotsTime.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Adapted from OpenZeppelin `Checkpoints` contract
44
pragma solidity 0.8.30;
55

6+
import {SafeCast} from "@openzeppelin4/contracts/utils/math/SafeCast.sol";
7+
68
import {RocketStorageInterface} from "../../interface/RocketStorageInterface.sol";
79
import {RocketNetworkSnapshotsTimeInterface} from "../../interface/network/RocketNetworkSnapshotsTimeInterface.sol";
810
import {RocketBase} from "../RocketBase.sol";
@@ -100,7 +102,7 @@ contract RocketNetworkSnapshotsTime is RocketBase, RocketNetworkSnapshotsTimeInt
100102

101103
/// @dev Inserts a value into a snapshot set
102104
function _insert(bytes32 _key, uint192 _value) internal {
103-
uint64 blockTimestamp = uint64(block.timestamp);
105+
uint64 blockTimestamp = SafeCast.toUint64(block.timestamp);
104106
uint256 pos = length(_key);
105107

106108
if (pos > 0) {

0 commit comments

Comments
 (0)