Skip to content

Plugin updates #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2022
Merged

Plugin updates #297

merged 3 commits into from
Dec 21, 2022

Conversation

kumaryash90
Copy link
Member

No description provided.

constructor(Plugin[] memory _pluginsToRegister) Router(_pluginsToRegister) {}

/// @dev Returns whether plug-in can be set in the given execution context.
function _canSetPlugin() internal view override returns (bool) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make things explicit and return false here.


function getAllRegistered() external view returns (ExtensionMap[] memory registered);
function setPlugin(Plugin memory _plugin) external;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename to addPlugin instead of setPlugin.

add makes the job of the function explicit; set is generic.


function getAllFunctionsOfPlugin(address _pluginAddress) external view returns (bytes4[] memory registered);

function getAllRegistered() external view returns (Plugin[] memory registered);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename getAllRegistered -> getAllPlugins.

@@ -3,61 +3,103 @@ pragma solidity ^0.8.0;

import "../interface/plugin/IMap.sol";

import "../Multicall.sol";
import "../../openzeppelin-presets/utils/EnumerableSet.sol";

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's cleanup comments before merging.

import "../../openzeppelin-presets/utils/EnumerableSet.sol";

/**
* TODO:
* - Remove OZ EnumerableSet external dependency.
*/

contract Map is IMap, Multicall {
abstract contract Map is IMap {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments demarcating different sections of the contract like in e.g. Router.

}

function _setPlugin(Plugin memory _plugin) internal {
require(allSelectors.add(bytes32(_plugin.selector)), "REGISTERED");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're not facing the contract size limit, let's clean up require statement error messages.

Let's follow the rule that error messages begin as "{Contract Name}: ..." so that it is easier to locate where errors are occurring. The error messages themselves don't need to be elaborate.


function _setPlugin(Plugin memory _plugin) internal {
require(allSelectors.add(bytes32(_plugin.selector)), "REGISTERED");
require(_plugin.selector == bytes4(keccak256(abi.encodePacked(_plugin.functionString))), "Incorrect selector");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write consistent error messages; if we're going for all caps single word messages e.g. REGISTERED, let's keep it consistent across the contract; same applies if we opt to go for phrases e.g. Map: Incorrect selector. (preferred).


return ext;
pluginForSelector[_plugin.selector] = _plugin;
selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this add call in a require statement.

If updatePlugin is called twice with the same Plugin struct info, this add call will fail silently and the following remove call will remove the selector as from the selectorsForPlugin set, thus, tainting data / state of the contract.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, you can reverse the order of the add and remove calls.

uint256 len = allSelectors.length();
functionExtensionPairs = new ExtensionMap[](len);
functionExtensionPairs = new Plugin[](len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace all extension vocabulary with plugin vocabulary, even for variables e.g. functionExtensionPairs.

Comment on lines +104 to +116
function _updatePlugin(Plugin memory _plugin) internal {
address currentPlugin = getPluginForFunction(_plugin.selector);
require(
_plugin.selector == bytes4(keccak256(abi.encodePacked(_plugin.functionString))),
"Map: Incorrect selector"
);

pluginForSelector[_plugin.selector] = _plugin;
selectorsForPlugin[currentPlugin].remove(bytes32(_plugin.selector));
selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector));

emit PluginUpdated(_plugin.selector, currentPlugin, _plugin.pluginAddress);
}

Check warning

Code scanning / Slither

Unused return

Map._updatePlugin(IMap.Plugin) (contracts/extension/plugin/Map.sol#104-116) ignores return value by selectorsForPlugin[currentPlugin].remove(bytes32(_plugin.selector)) (contracts/extension/plugin/Map.sol#112)
Comment on lines +104 to +116
function _updatePlugin(Plugin memory _plugin) internal {
address currentPlugin = getPluginForFunction(_plugin.selector);
require(
_plugin.selector == bytes4(keccak256(abi.encodePacked(_plugin.functionString))),
"Map: Incorrect selector"
);

pluginForSelector[_plugin.selector] = _plugin;
selectorsForPlugin[currentPlugin].remove(bytes32(_plugin.selector));
selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector));

emit PluginUpdated(_plugin.selector, currentPlugin, _plugin.pluginAddress);
}

Check warning

Code scanning / Slither

Unused return

Map._updatePlugin(IMap.Plugin) (contracts/extension/plugin/Map.sol#104-116) ignores return value by selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector)) (contracts/extension/plugin/Map.sol#113)
Comment on lines +90 to +101
function _addPlugin(Plugin memory _plugin) internal {
require(allSelectors.add(bytes32(_plugin.selector)), "Map: Selector exists");
require(
_plugin.selector == bytes4(keccak256(abi.encodePacked(_plugin.functionString))),
"Map: Incorrect selector"
);

pluginForSelector[_plugin.selector] = _plugin;
selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector));

emit PluginAdded(_plugin.selector, _plugin.pluginAddress);
}

Check warning

Code scanning / Slither

Unused return

Map._addPlugin(IMap.Plugin) (contracts/extension/plugin/Map.sol#90-101) ignores return value by selectorsForPlugin[_plugin.pluginAddress].add(bytes32(_plugin.selector)) (contracts/extension/plugin/Map.sol#98)
Comment on lines +119 to +127
function _removePlugin(bytes4 _selector) internal {
address currentPlugin = getPluginForFunction(_selector);

delete pluginForSelector[_selector];
allSelectors.remove(_selector);
selectorsForPlugin[currentPlugin].remove(bytes32(_selector));

emit PluginRemoved(_selector, currentPlugin);
}

Check warning

Code scanning / Slither

Unused return

Map._removePlugin(bytes4) (contracts/extension/plugin/Map.sol#119-127) ignores return value by selectorsForPlugin[currentPlugin].remove(bytes32(_selector)) (contracts/extension/plugin/Map.sol#124)
Comment on lines +119 to +127
function _removePlugin(bytes4 _selector) internal {
address currentPlugin = getPluginForFunction(_selector);

delete pluginForSelector[_selector];
allSelectors.remove(_selector);
selectorsForPlugin[currentPlugin].remove(bytes32(_selector));

emit PluginRemoved(_selector, currentPlugin);
}

Check warning

Code scanning / Slither

Unused return

Map._removePlugin(bytes4) (contracts/extension/plugin/Map.sol#119-127) ignores return value by allSelectors.remove(_selector) (contracts/extension/plugin/Map.sol#123)
@kumaryash90 kumaryash90 merged commit 4709d1e into main Dec 21, 2022
@nkrishang nkrishang deleted the plugin-update branch December 26, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants