Skip to content

Commit 1cec310

Browse files
committed
fix: address tmnt 156
1 parent 07650da commit 1cec310

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

l1-contracts/src/governance/libraries/AddressSnapshotLib.sol

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,19 @@ library AddressSnapshotLib {
121121
// then update Charlie in addressToCurrentIndex to reflect the new index of 1.
122122

123123
uint224 lastIndex = currentSize - 1;
124-
address lastValidator = address(_self.indexToAddressHistory[lastIndex].latest().toUint160());
125-
126124
uint32 key = block.timestamp.toUint32();
127125

128-
// If we are removing the last item, we cannot swap it with anything
129-
// so we append a new address of zero for this timestamp
130-
// And since we are removing it, we set the location to 0
131-
if (lastIndex == _index) {
132-
_self.indexToAddressHistory[_index].push(key, uint224(0));
133-
} else {
134-
// Otherwise, we swap the last item with the item we are removing
135-
// and update the location of the last item
126+
// If not removing the last item, swap the value of the last item into the `_index` to remove
127+
if (lastIndex != _index) {
128+
address lastValidator = address(_self.indexToAddressHistory[lastIndex].latest().toUint160());
129+
136130
_self.addressToCurrentIndex[lastValidator] = Index({exists: true, index: _index.toUint224()});
137131
_self.indexToAddressHistory[_index].push(key, uint160(lastValidator).toUint224());
138132
}
139133

134+
// Then "pop" the last index by setting the value to `address(0)`
135+
_self.indexToAddressHistory[lastIndex].push(key, uint224(0));
136+
140137
// Finally, we update the size to reflect the new size of the set.
141138
_self.size.push(key, (lastIndex).toUint224());
142139
return true;

l1-contracts/test/governance/governance/address_snapshots/AddressSnapshotsBase.t.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ contract AddressSetWrapper {
5050
function valuesAtTimestamp(uint32 _timestamp) public view returns (address[] memory) {
5151
return validatorSet.valuesAtTimestamp(_timestamp);
5252
}
53+
54+
function unsafeGetRecentAddressFromIndexAtTimestamp(uint256 _index, uint32 _timestamp) public view returns (address) {
55+
return validatorSet.unsafeGetRecentAddressFromIndexAtTimestamp(_index, _timestamp);
56+
}
5357
}
5458

5559
contract AddressSnapshotsBase is Test {

l1-contracts/test/governance/governance/address_snapshots/remove.t.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ contract AddressSnapshotRemoveTest is AddressSnapshotsBase {
9898
assertEq(validatorSet.getAddressFromIndexAtTimestamp(0, ts2), address(1));
9999
assertEq(validatorSet.getAddressFromIndexAtTimestamp(1, ts2), address(3));
100100

101+
// Ensure that the values read outside the boundary are `address(0)`
102+
assertEq(validatorSet.unsafeGetRecentAddressFromIndexAtTimestamp(2, ts2), address(0));
103+
assertEq(validatorSet.unsafeGetRecentAddressFromIndexAtTimestamp(2000, ts2), address(0));
104+
101105
assertEq(validatorSet.getAddressFromIndexAtTimestamp(0, ts3), address(3));
102106

103107
vals = validatorSet.valuesAtTimestamp(ts);

0 commit comments

Comments
 (0)