@@ -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 ;
0 commit comments