Skip to content

Commit

Permalink
Refactor: constant names, simplify custom storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriian Chestnykh committed Nov 28, 2023
1 parent 9242794 commit d86f95c
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions contracts/identitytreestore/IdentityTreeStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,15 @@ contract IdentityTreeStore is
using ReverseHashLib for ReverseHashLib.Data;

/// @custom:storage-location erc7201:iden3.storage.IdentityTreeStore.ReverseHashLibData
struct ReverseHashLibDataStorage {
ReverseHashLib.Data _data;
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.IdentityTreeStore.ReverseHashLibData")) - 1)) &
// ~bytes32(uint256(0xff));
bytes32 private constant ReverseHashLibDataStorageLocation =
bytes32 private constant REVERSE_HASH_LIB_DATA_STORAGE_LOCATION =
0x0f7e3bdc6cc0e880d509aa1f6b8d1a88e5fcb7274e18dfba772424a36fe9b400;

function _getReverseHashLibDataStorage()
private
pure
returns (ReverseHashLibDataStorage storage $)
{
function _getReverseHashLibDataStorage() private pure returns (ReverseHashLib.Data storage $) {
assembly {

Check warning on line 42 in contracts/identitytreestore/IdentityTreeStore.sol

View workflow job for this annotation

GitHub Actions / solhint

Avoid to use inline assembly. It is acceptable only in rare cases
$.slot := ReverseHashLibDataStorageLocation
$.slot := REVERSE_HASH_LIB_DATA_STORAGE_LOCATION
}
}

Expand All @@ -57,26 +50,26 @@ contract IdentityTreeStore is
}

// keccak256(abi.encode(uint256(keccak256("iden3.storage.IdentityTreeStore.Main")) - 1)) & ~bytes32(uint256(0xff));
bytes32 private constant MainStorageLocation =
bytes32 private constant MAIN_STORAGE_LOCATION =
0x95ca427007e091a13a7ccfcb233b8a2ed19d987330a248c445b1b483a35bb800;

function _getMainStorage() private pure returns (MainStorage storage $) {
assembly {

Check warning on line 57 in contracts/identitytreestore/IdentityTreeStore.sol

View workflow job for this annotation

GitHub Actions / solhint

Avoid to use inline assembly. It is acceptable only in rare cases
$.slot := MainStorageLocation
$.slot := MAIN_STORAGE_LOCATION
}
}

function initialize(address state) public initializer {
_getMainStorage()._state = IState(state);
_getReverseHashLibDataStorage()._data.hashFunction = _hashFunc;
_getReverseHashLibDataStorage().hashFunction = _hashFunc;
}

/**
* @dev Saves nodes array. Note that each node contains an array itself.
* @param nodes An array of nodes
*/
function saveNodes(uint256[][] memory nodes) external {
return _getReverseHashLibDataStorage()._data.savePreimages(nodes);
return _getReverseHashLibDataStorage().savePreimages(nodes);
}

/**
Expand All @@ -85,7 +78,7 @@ contract IdentityTreeStore is
* @return The node
*/
function getNode(uint256 key) public view returns (uint256[] memory) {
uint256[] memory preim = _getReverseHashLibDataStorage()._data.getPreimage(key);
uint256[] memory preim = _getReverseHashLibDataStorage().getPreimage(key);
require(preim.length > 0, "Node not found");

Check warning on line 82 in contracts/identitytreestore/IdentityTreeStore.sol

View workflow job for this annotation

GitHub Actions / solhint

Use Custom Errors instead of require statements
return preim;
}
Expand Down Expand Up @@ -160,10 +153,10 @@ contract IdentityTreeStore is
uint256 nextNodeHash = root;
uint256[] memory children;

ReverseHashLibDataStorage storage $ = _getReverseHashLibDataStorage();
ReverseHashLib.Data storage $ = _getReverseHashLibDataStorage();

for (uint256 i = 0; i <= MAX_SMT_DEPTH; i++) {
children = $._data.hashesToPreimages[nextNodeHash];
children = $.hashesToPreimages[nextNodeHash];

NodeType nodeType = _nodeType(children);
if (nodeType == NodeType.Empty) {
Expand Down

0 comments on commit d86f95c

Please sign in to comment.