From 1f7eb9d9cd96be7453edd82bb3698d61b95d79f4 Mon Sep 17 00:00:00 2001 From: Mario Cao Date: Thu, 12 Dec 2024 11:31:33 +0000 Subject: [PATCH] refactor(contracts): change some public modifiers to external --- contracts/core/SedaCoreV1.sol | 2 +- .../core/abstract/RequestHandlerBase.sol | 16 +++-- contracts/core/abstract/ResultHandlerBase.sol | 64 +++++++++---------- contracts/provers/Secp256k1ProverV1.sol | 12 ++-- contracts/provers/abstract/ProverBase.sol | 4 +- 5 files changed, 50 insertions(+), 48 deletions(-) diff --git a/contracts/core/SedaCoreV1.sol b/contracts/core/SedaCoreV1.sol index 551bf44..358cfc4 100644 --- a/contracts/core/SedaCoreV1.sol +++ b/contracts/core/SedaCoreV1.sol @@ -59,7 +59,7 @@ contract SedaCoreV1 is ISedaCore, RequestHandlerBase, ResultHandlerBase, UUPSUpg __UUPSUpgradeable_init(); } - // ============ External Functions ============ + // ============ Public Functions ============ /// @inheritdoc RequestHandlerBase /// @dev Overrides the base implementation to also add the request ID and timestamp to storage diff --git a/contracts/core/abstract/RequestHandlerBase.sol b/contracts/core/abstract/RequestHandlerBase.sol index 5bd1adc..de60e99 100644 --- a/contracts/core/abstract/RequestHandlerBase.sol +++ b/contracts/core/abstract/RequestHandlerBase.sol @@ -23,6 +23,15 @@ abstract contract RequestHandlerBase is IRequestHandler { // ============ External Functions ============ + /// @notice Derives a request ID from the given inputs + /// @param inputs The request inputs + /// @return The derived request ID + function deriveRequestId(SedaDataTypes.RequestInputs calldata inputs) external pure returns (bytes32) { + return SedaDataTypes.deriveRequestId(inputs); + } + + // ============ Public Functions ============ + /// @inheritdoc IRequestHandler function postRequest( SedaDataTypes.RequestInputs calldata inputs @@ -67,13 +76,6 @@ abstract contract RequestHandlerBase is IRequestHandler { return _requestHandlerStorage().requests[requestId]; } - /// @notice Derives a request ID from the given inputs - /// @param inputs The request inputs - /// @return The derived request ID - function deriveRequestId(SedaDataTypes.RequestInputs calldata inputs) public pure returns (bytes32) { - return SedaDataTypes.deriveRequestId(inputs); - } - // ============ Internal Functions ============ /// @notice Returns the storage struct for the contract diff --git a/contracts/core/abstract/ResultHandlerBase.sol b/contracts/core/abstract/ResultHandlerBase.sol index fa88f91..285e936 100644 --- a/contracts/core/abstract/ResultHandlerBase.sol +++ b/contracts/core/abstract/ResultHandlerBase.sol @@ -42,6 +42,38 @@ abstract contract ResultHandlerBase is IResultHandler, Initializable { // ============ External Functions ============ + /// @inheritdoc IResultHandler + function getSedaProver() external view override(IResultHandler) returns (address) { + return address(_resultHandlerStorage().sedaProver); + } + + /// @notice Verifies the result without storing it + /// @param result The result to verify + /// @param batchHeight The height of the batch the result belongs to + /// @param proof The proof associated with the result + /// @return A boolean indicating whether the result is valid + function verifyResult( + SedaDataTypes.Result calldata result, + uint64 batchHeight, + bytes32[] calldata proof + ) external view returns (bytes32) { + bytes32 resultId = SedaDataTypes.deriveResultId(result); + if (!_resultHandlerStorage().sedaProver.verifyResultProof(resultId, batchHeight, proof)) { + revert InvalidResultProof(resultId); + } + + return resultId; + } + + /// @notice Derives a result ID from the given result + /// @param result The result data + /// @return The derived result ID + function deriveResultId(SedaDataTypes.Result calldata result) external pure returns (bytes32) { + return SedaDataTypes.deriveResultId(result); + } + + // ============ Public Functions ============ + /// @inheritdoc IResultHandler function postResult( SedaDataTypes.Result calldata result, @@ -62,8 +94,6 @@ abstract contract ResultHandlerBase is IResultHandler, Initializable { return resultId; } - // ============ Public View Functions ============ - /// @inheritdoc IResultHandler function getResult(bytes32 requestId) public view override(IResultHandler) returns (SedaDataTypes.Result memory) { SedaDataTypes.Result memory result = _resultHandlerStorage().results[requestId]; @@ -73,36 +103,6 @@ abstract contract ResultHandlerBase is IResultHandler, Initializable { return _resultHandlerStorage().results[requestId]; } - /// @inheritdoc IResultHandler - function getSedaProver() public view override(IResultHandler) returns (address) { - return address(_resultHandlerStorage().sedaProver); - } - - /// @notice Verifies the result without storing it - /// @param result The result to verify - /// @param batchHeight The height of the batch the result belongs to - /// @param proof The proof associated with the result - /// @return A boolean indicating whether the result is valid - function verifyResult( - SedaDataTypes.Result calldata result, - uint64 batchHeight, - bytes32[] calldata proof - ) public view returns (bytes32) { - bytes32 resultId = SedaDataTypes.deriveResultId(result); - if (!_resultHandlerStorage().sedaProver.verifyResultProof(resultId, batchHeight, proof)) { - revert InvalidResultProof(resultId); - } - - return resultId; - } - - /// @notice Derives a result ID from the given result - /// @param result The result data - /// @return The derived result ID - function deriveResultId(SedaDataTypes.Result calldata result) public pure returns (bytes32) { - return SedaDataTypes.deriveResultId(result); - } - // ============ Internal Functions ============ /// @notice Returns the storage struct for the contract diff --git a/contracts/provers/Secp256k1ProverV1.sol b/contracts/provers/Secp256k1ProverV1.sol index 004a42e..6400639 100644 --- a/contracts/provers/Secp256k1ProverV1.sol +++ b/contracts/provers/Secp256k1ProverV1.sol @@ -80,7 +80,7 @@ contract Secp256k1ProverV1 is ProverBase, Initializable, UUPSUpgradeable, Ownabl SedaDataTypes.Batch calldata newBatch, bytes[] calldata signatures, SedaDataTypes.ValidatorProof[] calldata validatorProofs - ) public override { + ) external override(ProverBase) { Secp256k1ProverStorage storage s = _storageV1(); // Check that new batch invariants hold if (newBatch.batchHeight <= s.lastBatchHeight) { @@ -117,7 +117,7 @@ contract Secp256k1ProverV1 is ProverBase, Initializable, UUPSUpgradeable, Ownabl emit BatchPosted(newBatch.batchHeight, batchId); } - // ============ Public View Functions ============ + // ============ External View Functions ============ /// @notice Verifies a result proof against a batch's results root /// @param resultId The ID of the result to verify @@ -128,7 +128,7 @@ contract Secp256k1ProverV1 is ProverBase, Initializable, UUPSUpgradeable, Ownabl bytes32 resultId, uint64 batchHeight, bytes32[] calldata merkleProof - ) public view override returns (bool) { + ) external view override(ProverBase) returns (bool) { Secp256k1ProverStorage storage s = _storageV1(); bytes32 leaf = keccak256(abi.encodePacked(RESULT_DOMAIN_SEPARATOR, resultId)); return MerkleProof.verify(merkleProof, s.batchToResultsRoot[batchHeight], leaf); @@ -136,20 +136,20 @@ contract Secp256k1ProverV1 is ProverBase, Initializable, UUPSUpgradeable, Ownabl /// @notice Returns the last processed batch height /// @return The height of the last batch - function getLastBatchHeight() public view override returns (uint64) { + function getLastBatchHeight() external view override returns (uint64) { return _storageV1().lastBatchHeight; } /// @notice Returns the last validators root hash /// @return The Merkle root of the last validator set - function getLastValidatorsRoot() public view returns (bytes32) { + function getLastValidatorsRoot() external view returns (bytes32) { return _storageV1().lastValidatorsRoot; } /// @notice Returns the results root for a specific batch height /// @param batchHeight The batch height to query /// @return The results root for the specified batch - function getBatchResultsRoot(uint64 batchHeight) public view returns (bytes32) { + function getBatchResultsRoot(uint64 batchHeight) external view returns (bytes32) { return _storageV1().batchToResultsRoot[batchHeight]; } diff --git a/contracts/provers/abstract/ProverBase.sol b/contracts/provers/abstract/ProverBase.sol index 3b5019d..ddbf85e 100644 --- a/contracts/provers/abstract/ProverBase.sol +++ b/contracts/provers/abstract/ProverBase.sol @@ -27,14 +27,14 @@ abstract contract ProverBase is IProver { SedaDataTypes.Batch calldata newBatch, bytes[] calldata signatures, SedaDataTypes.ValidatorProof[] calldata validatorProofs - ) public virtual override(IProver); + ) external virtual override(IProver); /// @inheritdoc IProver function verifyResultProof( bytes32 resultId, uint64 batchHeight, bytes32[] calldata merkleProof - ) public view virtual override(IProver) returns (bool); + ) external view virtual override(IProver) returns (bool); /// @inheritdoc IProver function getLastBatchHeight() external view virtual override(IProver) returns (uint64);