Skip to content

Commit

Permalink
improved removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mudgen committed Sep 22, 2020
1 parent 09d5cbc commit cc29c68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions contracts/libraries/LibDiamondCut.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ library LibDiamondCut {
} else {
// replace
if (oldFacet != _newFacetAddress) {
removeSelector(selector);
removeSelector(oldFacet, selector);
addSelector(_newFacetAddress, selector);
}
}
}
} else {
// remove selectors
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
removeSelector(_selectors[selectorIndex]);
bytes4 selector = _selectors[selectorIndex];
removeSelector(ds.selectorToFacetAndPosition[selector].facetAddress, selector);
}
}
}
Expand All @@ -70,39 +71,38 @@ library LibDiamondCut {
ds.selectorToFacetAndPosition[_selector].functionSelectorPosition = uint16(selectorPosition);
}

function removeSelector(bytes4 _selector) internal {
function removeSelector(address _oldFacet, bytes4 _selector) internal {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
address oldFacet = ds.selectorToFacetAndPosition[_selector].facetAddress;
// if function does not exist then do nothing and return
if (oldFacet == address(0)) {
if (_oldFacet == address(0)) {
return;
}
require(oldFacet != address(this), "LibDiamondCut: Can't remove or replace immutable function");
require(_oldFacet != address(this), "LibDiamondCut: Can't remove or replace immutable function");
// replace selector with last selector, then delete last selector
uint256 selectorPosition = ds.selectorToFacetAndPosition[_selector].functionSelectorPosition;
uint256 lastSelectorPosition = ds.facetFunctionSelectors[oldFacet].functionSelectors.length - 1;
bytes4 lastSelector = ds.facetFunctionSelectors[oldFacet].functionSelectors[lastSelectorPosition];
uint256 lastSelectorPosition = ds.facetFunctionSelectors[_oldFacet].functionSelectors.length - 1;
bytes4 lastSelector = ds.facetFunctionSelectors[_oldFacet].functionSelectors[lastSelectorPosition];
// if not the same then replace _selector with lastSelector
if (lastSelector != _selector) {
ds.facetFunctionSelectors[oldFacet].functionSelectors[selectorPosition] = lastSelector;
ds.facetFunctionSelectors[_oldFacet].functionSelectors[selectorPosition] = lastSelector;
ds.selectorToFacetAndPosition[lastSelector].functionSelectorPosition = uint16(selectorPosition);
}
// delete the last selector
ds.facetFunctionSelectors[oldFacet].functionSelectors.pop();
ds.facetFunctionSelectors[_oldFacet].functionSelectors.pop();
delete ds.selectorToFacetAndPosition[_selector];

// if no more selectors for facet address then delete the facet address
if (lastSelectorPosition == 0) {
// replace facet address with last facet address and delete last facet address
uint256 lastFacetAddressPosition = ds.facetAddresses.length - 1;
address lastFacetAddress = ds.facetAddresses[lastFacetAddressPosition];
uint256 facetAddressPosition = ds.facetFunctionSelectors[oldFacet].facetAddressPosition;
if (oldFacet != lastFacetAddress) {
uint256 facetAddressPosition = ds.facetFunctionSelectors[_oldFacet].facetAddressPosition;
if (_oldFacet != lastFacetAddress) {
ds.facetAddresses[facetAddressPosition] = lastFacetAddress;
ds.facetFunctionSelectors[lastFacetAddress].facetAddressPosition = uint16(facetAddressPosition);
}
ds.facetAddresses.pop();
delete ds.facetFunctionSelectors[oldFacet];
delete ds.facetFunctionSelectors[_oldFacet];
}
}

Expand Down
2 changes: 1 addition & 1 deletion truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = {

// Set default mocha options here, use special reporters etc.
mocha: {
// reporter: 'eth-gas-reporter'
reporter: 'eth-gas-reporter'
// timeout: 100000
},

Expand Down

0 comments on commit cc29c68

Please sign in to comment.