Skip to content

Commit

Permalink
chore: some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
excaliborr committed Nov 21, 2023
1 parent bfffd8b commit 8fb2d78
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 96 deletions.
88 changes: 42 additions & 46 deletions solidity/contracts/VerifierModule.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.19;

import {Enum} from 'safe-contracts/common/Enum.sol';
import {RLPReader} from 'solidity-rlp/contracts/RLPReader.sol';
import {IStorageMirror} from 'interfaces/IStorageMirror.sol';
import {MerklePatriciaProofVerifier} from 'libraries/MerklePatriciaProofVerifier.sol';
import {StateVerifier} from 'libraries/StateVerifier.sol';
import {RLPReader} from 'solidity-rlp/contracts/RLPReader.sol';
import {IStorageMirrorRootRegistry} from 'interfaces/IStorageMirrorRootRegistry.sol';
import {IBlockHeaderOracle} from 'interfaces/IBlockHeaderOracle.sol';
import {IVerifierModule} from 'interfaces/IVerifierModule.sol';
import {ISafe} from 'interfaces/ISafe.sol';
import {Enum} from 'safe-contracts/common/Enum.sol';

/**
* @title VerifierModule
Expand All @@ -23,49 +23,36 @@ contract VerifierModule is IVerifierModule {
* @notice The start of the linked list for the owners of a safe
* @dev Used for updating the owners of a safe
*/

address internal constant _SENTINEL_OWNERS = address(0x1);

/**
* @notice The slot of the mapping of the safe to the keccak256 hash of the latest verified settings in the StorageMirror
*/

uint256 internal constant _LATEST_VERIFIED_SETTINGS_SLOT = 0;

/**
* @notice The interface of the StorageMirrorRootRegistry contract
*/

IStorageMirrorRootRegistry public immutable STORAGE_MIRROR_ROOT_REGISTRY;

/**
* @notice The interface of the block header oracle contract
*/

IBlockHeaderOracle public immutable BLOCK_HEADER_ORACLE;

/**
* @notice The address of the StorageMirror contract on the home chain
*/

address public immutable STORAGE_MIRROR;

/**
* @notice The mapping of the safe to the keccak256 hash of the latest verified settings
*/

mapping(address => bytes32) public latestVerifiedSettings;

/**
* @notice The mapping of the safe to the timestamp of when the settings where verified
*/

mapping(address => uint256) public latestVerifiedSettingsTimestamp;

constructor(address _storageMirrorRootRegistry, address _storageMirror, address _blockHeaderOracle) payable {
STORAGE_MIRROR_ROOT_REGISTRY = IStorageMirrorRootRegistry(_storageMirrorRootRegistry);
constructor(IStorageMirrorRootRegistry _storageMirrorRootRegistry, address _storageMirror) payable {
STORAGE_MIRROR_ROOT_REGISTRY = _storageMirrorRootRegistry;
STORAGE_MIRROR = _storageMirror;
BLOCK_HEADER_ORACLE = IBlockHeaderOracle(_blockHeaderOracle);
}

/**
Expand All @@ -76,13 +63,50 @@ contract VerifierModule is IVerifierModule {
* @param _storageMirrorStorageProof The storage proof of the StorageMirror contract on the home chain
* @param _arbitraryTxnParams The transaction parameters for the arbitrary safe transaction that will execute
*/

function proposeAndVerifyUpdate(
address _safe,
IStorageMirror.SafeSettings calldata _proposedSettings,
bytes memory _storageMirrorStorageProof,
SafeTxnParams calldata _arbitraryTxnParams
) external {
_proposeAndVerifyUpdate(_safe, _proposedSettings, _storageMirrorStorageProof, _arbitraryTxnParams);
}

/**
* @notice The function extracts the storage root of the StorageMirror contract from a given account proof
*
* @param _storageMirrorAccountProof The account proof of the StorageMirror contract from the latest block
* @param _blockHeader The block header of the latest block
*/
function extractStorageMirrorStorageRoot(
bytes memory _storageMirrorAccountProof,
bytes memory _blockHeader
) external view returns (bytes32 _storageRoot) {
StateVerifier.BlockHeader memory _parsedBlockHeader = StateVerifier.verifyBlockHeader(_blockHeader);

bytes memory _rlpAccount = MerklePatriciaProofVerifier.extractProofValue(
_parsedBlockHeader.stateRootHash,
abi.encodePacked(keccak256(abi.encode(STORAGE_MIRROR))),
_storageMirrorAccountProof.toRlpItem().toList()
);

_storageRoot = StateVerifier.extractStorageRootFromAccount(_rlpAccount);
}

/**
* @notice Verifies the new settings that are incoming against a storage proof from the StorageMirror on the home chain
*
* @param _safe The address of the safe that has new settings
* @param _proposedSettings The new settings that are being proposed
* @param _storageMirrorStorageProof The storage proof of the StorageMirror contract on the home chain
* @param _arbitraryTxnParams The transaction parameters for the arbitrary safe transaction that will execute
*/
function _proposeAndVerifyUpdate(
address _safe,
IStorageMirror.SafeSettings calldata _proposedSettings,
bytes memory _storageMirrorStorageProof,
SafeTxnParams calldata _arbitraryTxnParams
) internal {
bytes32 _hashedProposedSettings = _verifyNewSettings(_safe, _proposedSettings, _storageMirrorStorageProof);

// If we dont revert from the _verifyNewSettings() call, then we can update the safe
Expand Down Expand Up @@ -114,30 +138,6 @@ contract VerifierModule is IVerifierModule {
emit VerifiedUpdate(_safe, _hashedProposedSettings);
}

/**
* @notice The function extracts the storage root of the StorageMirror contract from a given account proof
*
* @param _storageMirrorAccountProof The account proof of the StorageMirror contract from the latest block
*/

function extractStorageMirrorStorageRoot(bytes memory _storageMirrorAccountProof)
external
view
returns (bytes32 _storageRoot)
{
(bytes memory _blockHeader,) = BLOCK_HEADER_ORACLE.getLatestBlockHeader();

StateVerifier.BlockHeader memory _parsedBlockHeader = StateVerifier.verifyBlockHeader(_blockHeader);

bytes memory _rlpAccount = MerklePatriciaProofVerifier.extractProofValue(
_parsedBlockHeader.stateRootHash,
abi.encodePacked(keccak256(abi.encode(STORAGE_MIRROR))),
_storageMirrorAccountProof.toRlpItem().toList()
);

_storageRoot = StateVerifier.extractStorageRootFromAccount(_rlpAccount);
}

/**
* @notice The function that verifies a given storage proof for the proposed settings
*
Expand All @@ -146,7 +146,6 @@ contract VerifierModule is IVerifierModule {
* @param _storageMirrorStorageProof The storage proof of the StorageMirror contract on the home chain
* @return _hashedProposedSettings The keccak256 hash of the proposed settings
*/

function _verifyNewSettings(
address _safe,
IStorageMirror.SafeSettings memory _proposedSettings,
Expand Down Expand Up @@ -177,7 +176,6 @@ contract VerifierModule is IVerifierModule {
* @param _safe The address of the safe that has new settings
* @param _proposedSettings The new settings that are being updated to
*/

function _updateLatestVerifiedSettings(
address _safe,
IStorageMirror.SafeSettings calldata _proposedSettings
Expand Down Expand Up @@ -244,7 +242,6 @@ contract VerifierModule is IVerifierModule {
* @param _owners The array of addresses to search through
* @return _result If the address was found or not
*/

function _linearSearchOwners(address _owner, address[] memory _owners) internal pure returns (bool _result) {
for (uint256 _i; _i < _owners.length;) {
if (_owners[_i] == _owner) {
Expand All @@ -264,7 +261,6 @@ contract VerifierModule is IVerifierModule {
* @param _source The bytes to convert
* @return _result The bytes32 variable
*/

function _bytesToBytes32(bytes memory _source) internal pure returns (bytes32 _result) {
// Ensure the source data is 32 bytes or less

Expand Down
26 changes: 6 additions & 20 deletions solidity/interfaces/IVerifierModule.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.19;

import {Enum} from 'safe-contracts/common/Enum.sol';
import {IStorageMirror} from 'interfaces/IStorageMirror.sol';
import {IStorageMirrorRootRegistry} from 'interfaces/IStorageMirrorRootRegistry.sol';
import {IBlockHeaderOracle} from 'interfaces/IBlockHeaderOracle.sol';
import {Enum} from 'safe-contracts/common/Enum.sol';

interface IVerifierModule {
/*///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -54,32 +53,21 @@ interface IVerifierModule {
*
* @return _storageMirror The address of the StorageMirror contract.
*/

function STORAGE_MIRROR() external view returns (address _storageMirror);

/**
* @notice The address of the StorageMirrorRootRegistry contract.
*
* @return _storageMirrorRootRegistry The interface of the StorageMirrorRootRegistry contract.
*/

function STORAGE_MIRROR_ROOT_REGISTRY() external view returns (IStorageMirrorRootRegistry _storageMirrorRootRegistry);

/**
* @notice The interface of the BlockHeaderOracle contract.
*
* @return _blockHeaderOracle The interface of the BlockHeaderOracle contract.
*/

function BLOCK_HEADER_ORACLE() external view returns (IBlockHeaderOracle _blockHeaderOracle);

/**
* @notice The hash of the latest verified settings for a given safe
* @param _safe The address of the safe
*
* @return _latestVerifiedSettings The hash of the latest verified settings
*/

function latestVerifiedSettings(address _safe) external view returns (bytes32 _latestVerifiedSettings);

/**
Expand All @@ -88,7 +76,6 @@ interface IVerifierModule {
*
* @return _timestamp The timestamp of when it was saved
*/

function latestVerifiedSettingsTimestamp(address _safe) external view returns (uint256 _timestamp);

/*///////////////////////////////////////////////////////////////
Expand All @@ -99,12 +86,12 @@ interface IVerifierModule {
* @notice The function extracts the storage root of the StorageMirror contract from a given account proof
*
* @param _storageMirrorAccountProof The account proof of the StorageMirror contract from the latest block
* @param _blockHeader The block header of the latest block
*/

function extractStorageMirrorStorageRoot(bytes memory _storageMirrorAccountProof)
external
view
returns (bytes32 _storageRoot);
function extractStorageMirrorStorageRoot(
bytes memory _storageMirrorAccountProof,
bytes memory _blockHeader
) external view returns (bytes32 _storageRoot);

/**
* @notice Verifies the new settings that are incoming against a storage proof from the StorageMirror on the home chain
Expand All @@ -114,7 +101,6 @@ interface IVerifierModule {
* @param _storageMirrorStorageProof The storage proof of the StorageMirror contract on the home chain
* @param _arbitraryTxnParams The transaction parameters for the arbitrary safe transaction that will execute
*/

function proposeAndVerifyUpdate(
address _safe,
IStorageMirror.SafeSettings memory _proposedSettings,
Expand Down
Loading

0 comments on commit 8fb2d78

Please sign in to comment.