@@ -4,6 +4,7 @@ pragma solidity >=0.8.27;
4
4
5
5
import {Errors} from "@aztec/core/libraries/Errors.sol " ;
6
6
import {Timestamp, Epoch, TimeLib} from "@aztec/core/libraries/TimeLib.sol " ;
7
+ import {SafeCast} from "@oz/utils/math/SafeCast.sol " ;
7
8
8
9
/**
9
10
* @notice Structure to store a set of addresses with their historical snapshots
@@ -35,6 +36,8 @@ struct AddressSnapshot {
35
36
* and allows querying the state of addresses at any point in time
36
37
*/
37
38
library AddressSnapshotLib {
39
+ using SafeCast for uint256 ;
40
+
38
41
/**
39
42
* @notice Bit mask used to indicate presence of a validator in the set
40
43
*/
@@ -61,7 +64,7 @@ library AddressSnapshotLib {
61
64
62
65
_self.validatorToIndex[_validator] = indexWithBit;
63
66
_self.checkpoints[index].push (
64
- AddressSnapshot ({addr: _validator, epochNumber: uint96 ( Epoch.unwrap (_epochNumber))})
67
+ AddressSnapshot ({addr: _validator, epochNumber: Epoch.unwrap (_epochNumber). toUint96 ( )})
65
68
);
66
69
_self.size += 1 ;
67
70
return true ;
@@ -83,20 +86,19 @@ library AddressSnapshotLib {
83
86
AddressSnapshot memory lastSnapshot = _self.checkpoints[lastIndex][lastSnapshotLength - 1 ];
84
87
85
88
address lastValidator = lastSnapshot.addr;
86
- uint256 newLocationWithMask = _index | PRESENCE_BIT;
87
-
88
- _self.validatorToIndex[lastValidator] = newLocationWithMask; // Remove the last validator from the index
89
+ uint256 newLocation = _index | PRESENCE_BIT;
89
90
90
91
// If we are removing the last item, we cannot swap it with anything
91
92
// so we append a new address of zero for this epoch
93
+ // And since we are removing it, we set the location to 0
92
94
if (lastIndex == _index) {
93
95
lastSnapshot.addr = address (0 );
94
-
95
- // TODO: reuse value above, only insert once
96
- _self.validatorToIndex[lastValidator] = 0 ;
96
+ newLocation = 0 ;
97
97
}
98
98
99
- lastSnapshot.epochNumber = uint96 (epochNow);
99
+ _self.validatorToIndex[lastValidator] = 0 ;
100
+
101
+ lastSnapshot.epochNumber = epochNow.toUint96 ();
100
102
// Check if there's already a checkpoint for this index in the current epoch
101
103
uint256 checkpointCount = _self.checkpoints[_index].length ;
102
104
if (
@@ -164,7 +166,7 @@ library AddressSnapshotLib {
164
166
Epoch _epoch
165
167
) internal view returns (address ) {
166
168
uint256 numCheckpoints = _self.checkpoints[_index].length ;
167
- uint96 epoch = uint96 ( Epoch.unwrap (_epoch));
169
+ uint96 epoch = Epoch.unwrap (_epoch). toUint96 ( );
168
170
169
171
if (numCheckpoints == 0 ) {
170
172
return address (0 );
0 commit comments