Skip to content

Commit

Permalink
test: test setProgramHash
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Aug 12, 2024
1 parent b7fbbc7 commit fd33031
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/HdpExecutionStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.4;

import {MerkleProof} from "openzeppelin-contracts/contracts/utils/cryptography/MerkleProof.sol";
import {OwnableUpgradeable} from "openzeppelin-contracts-upgradeable/contracts/access/OwnableUpgradeable.sol";
import {AccessControl} from "openzeppelin-contracts/contracts/access/AccessControl.sol";
import {IFactsRegistry} from "./interfaces/IFactsRegistry.sol";
import {ISharpFactsAggregator} from "./interfaces/ISharpFactsAggregator.sol";
import {IAggregatorsFactory} from "./interfaces/IAggregatorsFactory.sol";
Expand All @@ -15,6 +15,8 @@ import {
import {ComputationalTask, ComputationalTaskCodecs} from "./datatypes/datalake/ComputeCodecs.sol";
import {ModuleTask, ModuleCodecs} from "./datatypes/module/ModuleCodecs.sol";

/// Caller is not authorized to perform the action
error Unauthorized();
/// Task is already registered
error DoubleRegistration();
/// Fact doesn't exist in the registry
Expand All @@ -27,7 +29,7 @@ error NotFinalized();
/// @title HdpExecutionStore
/// @author Herodotus Dev Ltd
/// @notice A contract to store the execution results of HDP tasks
contract HdpExecutionStore is OwnableUpgradeable {
contract HdpExecutionStore is AccessControl {
using MerkleProof for bytes32[];
using BlockSampledDatalakeCodecs for BlockSampledDatalake;
using TransactionsInBlockDatalakeCodecs for TransactionsInBlockDatalake;
Expand Down Expand Up @@ -59,6 +61,9 @@ contract HdpExecutionStore is OwnableUpgradeable {
/// @notice emitted when a new module task is scheduled
event ModuleTaskScheduled(ModuleTask moduleTask);

/// @notice constant representing role of operator
bytes32 public constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");

/// @notice constant representing the pedersen hash of the Cairo HDP program
bytes32 public PROGRAM_HASH;

Expand All @@ -82,10 +87,19 @@ contract HdpExecutionStore is OwnableUpgradeable {
AGGREGATORS_FACTORY = aggregatorsFactory;
PROGRAM_HASH = programHash;
CHAIN_ID = block.chainid;

_setRoleAdmin(OPERATOR_ROLE, OPERATOR_ROLE);
_grantRole(OPERATOR_ROLE, _msgSender());
}

/// @notice Reverts if the caller is not an operator
modifier onlyOperator() {
require(hasRole(OPERATOR_ROLE, _msgSender()), "Ownable: caller is not the owner");
_;
}

/// @notice Set the program hash for the HDP program
function setProgramHash(bytes32 programHash) external onlyOwner {
function setProgramHash(bytes32 programHash) external onlyOperator {
PROGRAM_HASH = programHash;
}

Expand Down
72 changes: 72 additions & 0 deletions test/BasicHdpExecutionStore.t.sol
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

import {Test} from "forge-std/Test.sol";
import {HdpExecutionStore} from "../src/HdpExecutionStore.sol";
import {IFactsRegistry} from "../src/interfaces/IFactsRegistry.sol";
import {IAggregatorsFactory} from "../src/interfaces/IAggregatorsFactory.sol";
import {ISharpFactsAggregator} from "../src/interfaces/ISharpFactsAggregator.sol";

contract MockFactsRegistry is IFactsRegistry {
mapping(bytes32 => bool) public isValid;

function markValid(bytes32 fact) public {
isValid[fact] = true;
}
}

contract MockAggregatorsFactory is IAggregatorsFactory {
mapping(uint256 => ISharpFactsAggregator) public aggregatorsById;

function createAggregator(uint256 id, ISharpFactsAggregator aggregator) external {
aggregatorsById[id] = aggregator;
}
}

contract MockSharpFactsAggregator is ISharpFactsAggregator {
uint256 public usedMmrSize;
bytes32 public usedMmrRoot;

constructor(bytes32 poseidonMmrRoot, uint256 mmrSize) {
usedMmrRoot = poseidonMmrRoot;
usedMmrSize = mmrSize;
}

function aggregatorState() external view returns (AggregatorState memory) {
return AggregatorState({
poseidonMmrRoot: usedMmrRoot,
keccakMmrRoot: bytes32(0),
mmrSize: usedMmrSize,
continuableParentHash: bytes32(0)
});
}
}

contract HdpExecutionStoreTest is Test {
HdpExecutionStore private hdp;
IFactsRegistry private factsRegistry;
IAggregatorsFactory private aggregatorsFactory;
ISharpFactsAggregator private sharpFactsAggregator;

function testSetProgramHash() public {
vm.chainId(11155111);

// Registery for facts that has been processed through SHARP
factsRegistry = new MockFactsRegistry();
// Factory for creating SHARP facts aggregators
aggregatorsFactory = new MockAggregatorsFactory();

bytes32 oldPrgramHash = bytes32(uint256(1));
hdp = new HdpExecutionStore(factsRegistry, aggregatorsFactory, oldPrgramHash);

assertEq(hdp.PROGRAM_HASH(), oldPrgramHash);
bytes32 newProgramHash = bytes32(uint256(2));

hdp.setProgramHash(newProgramHash);
assertEq(hdp.PROGRAM_HASH(), newProgramHash);

vm.prank(address(1));
bytes32 malProgramHash = bytes32(uint256(3));
vm.expectRevert("Ownable: caller is not the owner");
hdp.setProgramHash(malProgramHash);
}
}

0 comments on commit fd33031

Please sign in to comment.