Skip to content

Commit

Permalink
chore: fmt, feat: add request
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Jul 23, 2024
1 parent aadf112 commit 705b706
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 446 deletions.
21 changes: 5 additions & 16 deletions script/HdpExecutionStore.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,14 @@ contract HdpExecutionStoreDeployer is Script {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

IFactsRegistry factsRegistry = IFactsRegistry(
vm.envAddress("FACTS_REGISTRY_ADDRESS")
);
IAggregatorsFactory aggregatorsFactory = IAggregatorsFactory(
vm.envAddress("AGGREGATORS_FACTORY_ADDRESS")
);
IFactsRegistry factsRegistry = IFactsRegistry(vm.envAddress("FACTS_REGISTRY_ADDRESS"));
IAggregatorsFactory aggregatorsFactory = IAggregatorsFactory(vm.envAddress("AGGREGATORS_FACTORY_ADDRESS"));
bytes32 programHash = _getProgramHash();

// Deploy the HdpExecutionStore
HdpExecutionStore hdpExecutionStore = new HdpExecutionStore(
factsRegistry,
aggregatorsFactory,
programHash
);

console2.log(
"HdpExecutionStore deployed at: ",
address(hdpExecutionStore)
);
HdpExecutionStore hdpExecutionStore = new HdpExecutionStore(factsRegistry, aggregatorsFactory, programHash);

console2.log("HdpExecutionStore deployed at: ", address(hdpExecutionStore));

vm.stopBroadcast();
}
Expand Down
139 changes: 53 additions & 86 deletions src/HdpExecutionStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import {ISharpFactsAggregator} from "./interfaces/ISharpFactsAggregator.sol";
import {IAggregatorsFactory} from "./interfaces/IAggregatorsFactory.sol";

import {BlockSampledDatalake, BlockSampledDatalakeCodecs} from "./datatypes/datalake/BlockSampledDatalakeCodecs.sol";
import {TransactionsInBlockDatalake, TransactionsInBlockDatalakeCodecs} from "./datatypes/datalake/TransactionsInBlockDatalakeCodecs.sol";
import {
TransactionsInBlockDatalake,
TransactionsInBlockDatalakeCodecs
} from "./datatypes/datalake/TransactionsInBlockDatalakeCodecs.sol";
import {ComputationalTask, ComputationalTaskCodecs} from "./datatypes/datalake/ComputeCodecs.sol";
import {Module, ModuleCodecs} from "./datatypes/module/ModuleCodecs.sol";

/// Caller is not authorized to perform the action
error Unauthorized();
Expand All @@ -31,6 +35,7 @@ contract HdpExecutionStore is AccessControl {
using BlockSampledDatalakeCodecs for BlockSampledDatalake;
using TransactionsInBlockDatalakeCodecs for TransactionsInBlockDatalake;
using ComputationalTaskCodecs for ComputationalTask;
using ModuleCodecs for Module;

/// @notice The status of a task
enum TaskStatus {
Expand All @@ -49,16 +54,13 @@ contract HdpExecutionStore is AccessControl {
event MmrRootCached(uint256 mmrId, uint256 mmrSize, bytes32 mmrRoot);

/// @notice emitted when a new task with block sampled datalake is scheduled
event TaskWithBlockSampledDatalakeScheduled(
BlockSampledDatalake datalake,
ComputationalTask task
);
event TaskWithBlockSampledDatalakeScheduled(BlockSampledDatalake datalake, ComputationalTask task);

/// @notice emitted when a new task with transactions in block datalake is scheduled
event TaskWithTransactionsInBlockDatalakeScheduled(
TransactionsInBlockDatalake datalake,
ComputationalTask task
);
event TaskWithTransactionsInBlockDatalakeScheduled(TransactionsInBlockDatalake datalake, ComputationalTask task);

/// @notice emitted when a new task with module is scheduled
event ModuleTaskScheduled(Module moduleTask);

/// @notice constant representing role of operator
bytes32 public constant OPERATOR_ROLE = keccak256("OPERATOR_ROLE");
Expand All @@ -79,14 +81,9 @@ contract HdpExecutionStore is AccessControl {
mapping(bytes32 => TaskResult) public cachedTasksResult;

/// @notice mapping of chain id => mmr id => mmr size => mmr root
mapping(uint256 => mapping(uint256 => mapping(uint256 => bytes32)))
public cachedMMRsRoots;

constructor(
IFactsRegistry factsRegistry,
IAggregatorsFactory aggregatorsFactory,
bytes32 programHash
) {
mapping(uint256 => mapping(uint256 => mapping(uint256 => bytes32))) public cachedMMRsRoots;

constructor(IFactsRegistry factsRegistry, IAggregatorsFactory aggregatorsFactory, bytes32 programHash) {
SHARP_FACTS_REGISTRY = factsRegistry;
AGGREGATORS_FACTORY = aggregatorsFactory;
PROGRAM_HASH = programHash;
Expand All @@ -105,20 +102,11 @@ contract HdpExecutionStore is AccessControl {
/// @notice Caches the MMR root for a given MMR id
/// @notice Get MMR size and root from the aggregator and cache it
function cacheMmrRoot(uint256 mmrId) public {
ISharpFactsAggregator aggregator = AGGREGATORS_FACTORY.aggregatorsById(
mmrId
);
ISharpFactsAggregator.AggregatorState
memory aggregatorState = aggregator.aggregatorState();
cachedMMRsRoots[CHAIN_ID][mmrId][
aggregatorState.mmrSize
] = aggregatorState.poseidonMmrRoot;

emit MmrRootCached(
mmrId,
aggregatorState.mmrSize,
aggregatorState.poseidonMmrRoot
);
ISharpFactsAggregator aggregator = AGGREGATORS_FACTORY.aggregatorsById(mmrId);
ISharpFactsAggregator.AggregatorState memory aggregatorState = aggregator.aggregatorState();
cachedMMRsRoots[CHAIN_ID][mmrId][aggregatorState.mmrSize] = aggregatorState.poseidonMmrRoot;

emit MmrRootCached(mmrId, aggregatorState.mmrSize, aggregatorState.poseidonMmrRoot);
}

/// @notice Requests the execution of a task with a block sampled datalake
Expand All @@ -137,15 +125,9 @@ contract HdpExecutionStore is AccessControl {
}

// Store the task result
cachedTasksResult[taskCommitment] = TaskResult({
status: TaskStatus.SCHEDULED,
result: ""
});

emit TaskWithBlockSampledDatalakeScheduled(
blockSampledDatalake,
computationalTask
);
cachedTasksResult[taskCommitment] = TaskResult({status: TaskStatus.SCHEDULED, result: ""});

emit TaskWithBlockSampledDatalakeScheduled(blockSampledDatalake, computationalTask);
}

/// @notice Requests the execution of a task with a transactions in block datalake
Expand All @@ -164,15 +146,25 @@ contract HdpExecutionStore is AccessControl {
}

// Store the task result
cachedTasksResult[taskCommitment] = TaskResult({
status: TaskStatus.SCHEDULED,
result: ""
});

emit TaskWithTransactionsInBlockDatalakeScheduled(
transactionsInBlockDatalake,
computationalTask
);
cachedTasksResult[taskCommitment] = TaskResult({status: TaskStatus.SCHEDULED, result: ""});

emit TaskWithTransactionsInBlockDatalakeScheduled(transactionsInBlockDatalake, computationalTask);
}

/// @notice Requests the execution of a task with a module
/// @param moduleTask module task
function requestExecutionOfModuleTask(Module calldata moduleTask) external {
bytes32 taskCommitment = moduleTask.commit();

// Ensure task is not already scheduled
if (cachedTasksResult[taskCommitment].status != TaskStatus.NONE) {
revert DoubleRegistration();
}

// Store the task result
cachedTasksResult[taskCommitment] = TaskResult({status: TaskStatus.SCHEDULED, result: ""});

emit ModuleTaskScheduled(moduleTask);
}

/// @notice Authenticates the execution of a task is finalized
Expand Down Expand Up @@ -222,9 +214,7 @@ contract HdpExecutionStore is AccessControl {
bytes32 programOutputHash = keccak256(abi.encodePacked(programOutput));

// Compute GPS fact hash
bytes32 gpsFactHash = keccak256(
abi.encode(PROGRAM_HASH, programOutputHash)
);
bytes32 gpsFactHash = keccak256(abi.encode(PROGRAM_HASH, programOutputHash));

// Ensure GPS fact is registered
if (!SHARP_FACTS_REGISTRY.isValid(gpsFactHash)) {
Expand All @@ -238,63 +228,42 @@ contract HdpExecutionStore is AccessControl {
bytes32[] memory resultInclusionProof = resultsInclusionProofs[i];

// Convert the low and high 128 bits to a single 256 bit value
bytes32 resultMerkleRoot = bytes32(
(resultMerkleRootHigh << 128) | resultMerkleRootLow
);
bytes32 taskMerkleRoot = bytes32(
(taskMerkleRootHigh << 128) | taskMerkleRootLow
);
bytes32 resultMerkleRoot = bytes32((resultMerkleRootHigh << 128) | resultMerkleRootLow);
bytes32 taskMerkleRoot = bytes32((taskMerkleRootHigh << 128) | taskMerkleRootLow);

// Compute the Merkle leaf of the task
bytes32 taskCommitment = taskCommitments[i];
bytes32 taskMerkleLeaf = standardLeafHash(taskCommitment);
// Ensure that the task is included in the batch, by verifying the Merkle proof
bool isVerifiedTask = taskInclusionProof.verify(
taskMerkleRoot,
taskMerkleLeaf
);
bool isVerifiedTask = taskInclusionProof.verify(taskMerkleRoot, taskMerkleLeaf);

if (!isVerifiedTask) {
revert NotInBatch();
}

// Compute the Merkle leaf of the task result
bytes32 taskResultCommitment = keccak256(
abi.encode(taskCommitment, computationalTaskResult)
);
bytes32 taskResultMerkleLeaf = standardLeafHash(
taskResultCommitment
);
bytes32 taskResultCommitment = keccak256(abi.encode(taskCommitment, computationalTaskResult));
bytes32 taskResultMerkleLeaf = standardLeafHash(taskResultCommitment);
// Ensure that the task result is included in the batch, by verifying the Merkle proof
bool isVerifiedResult = resultInclusionProof.verify(
resultMerkleRoot,
taskResultMerkleLeaf
);
bool isVerifiedResult = resultInclusionProof.verify(resultMerkleRoot, taskResultMerkleLeaf);

if (!isVerifiedResult) {
revert NotInBatch();
}

// Store the task result
cachedTasksResult[taskCommitment] = TaskResult({
status: TaskStatus.FINALIZED,
result: computationalTaskResult
});
cachedTasksResult[taskCommitment] =
TaskResult({status: TaskStatus.FINALIZED, result: computationalTaskResult});
}
}

/// @notice Load MMR root from cache with given mmrId and mmrSize
function loadMmrRoot(
uint256 mmrId,
uint256 mmrSize
) public view returns (bytes32) {
function loadMmrRoot(uint256 mmrId, uint256 mmrSize) public view returns (bytes32) {
return cachedMMRsRoots[CHAIN_ID][mmrId][mmrSize];
}

/// @notice Returns the result of a finalized task
function getFinalizedTaskResult(
bytes32 taskCommitment
) external view returns (bytes32) {
function getFinalizedTaskResult(bytes32 taskCommitment) external view returns (bytes32) {
// Ensure task is finalized
if (cachedTasksResult[taskCommitment].status != TaskStatus.FINALIZED) {
revert NotFinalized();
Expand All @@ -303,9 +272,7 @@ contract HdpExecutionStore is AccessControl {
}

/// @notice Returns the status of a task
function getTaskStatus(
bytes32 taskCommitment
) external view returns (TaskStatus) {
function getTaskStatus(bytes32 taskCommitment) external view returns (TaskStatus) {
return cachedTasksResult[taskCommitment].status;
}

Expand Down
63 changes: 22 additions & 41 deletions src/datatypes/datalake/BlockSampledDatalakeCodecs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,79 +21,60 @@ struct BlockSampledDatalake {
library BlockSampledDatalakeCodecs {
/// @dev Encodes a BlockSampledDatalake.
/// @param datalake The BlockSampledDatalake to encode.
function encode(
BlockSampledDatalake memory datalake
) internal pure returns (bytes memory) {
return
abi.encode(
DatalakeCode.BlockSampled,
datalake.chainId,
datalake.blockRangeStart,
datalake.blockRangeEnd,
datalake.increment,
datalake.sampledProperty
);
function encode(BlockSampledDatalake memory datalake) internal pure returns (bytes memory) {
return abi.encode(
DatalakeCode.BlockSampled,
datalake.chainId,
datalake.blockRangeStart,
datalake.blockRangeEnd,
datalake.increment,
datalake.sampledProperty
);
}

/// @dev Get the commitment of a BlockSampledDatalake.
/// @param datalake The BlockSampledDatalake to commit.
function commit(
BlockSampledDatalake memory datalake
) internal pure returns (bytes32) {
function commit(BlockSampledDatalake memory datalake) internal pure returns (bytes32) {
return keccak256(encode(datalake));
}

/// @dev Encodes a sampled property for a header property.
/// @param headerPropId The header field from rlp decoded block header.
function encodeSampledPropertyForHeaderProp(
uint8 headerPropId
) internal pure returns (bytes memory) {
function encodeSampledPropertyForHeaderProp(uint8 headerPropId) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(1), headerPropId);
}

/// @dev Encodes a sampled property for an account property.
/// @param account The account address.
/// @param propertyId The account field from rlp decoded account.
function encodeSampledPropertyForAccount(
address account,
uint8 propertyId
) internal pure returns (bytes memory) {
function encodeSampledPropertyForAccount(address account, uint8 propertyId) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(2), account, propertyId);
}

/// @dev Encodes a sampled property for a storage.
/// @param account The account address.
/// @param slot The storage key.
function encodeSampledPropertyForStorage(
address account,
bytes32 slot
) internal pure returns (bytes memory) {
function encodeSampledPropertyForStorage(address account, bytes32 slot) internal pure returns (bytes memory) {
return abi.encodePacked(uint8(3), account, slot);
}

/// @dev Decodes a BlockSampledDatalake.
/// @param data The encoded BlockSampledDatalake.
function decode(
bytes memory data
) internal pure returns (BlockSampledDatalake memory) {
function decode(bytes memory data) internal pure returns (BlockSampledDatalake memory) {
(
,
uint256 chainId,
uint256 blockRangeStart,
uint256 blockRangeEnd,
uint256 increment,
bytes memory sampledProperty
) = abi.decode(
data,
(DatalakeCode, uint256, uint256, uint256, uint256, bytes)
);
return
BlockSampledDatalake({
chainId: chainId,
blockRangeStart: blockRangeStart,
blockRangeEnd: blockRangeEnd,
increment: increment,
sampledProperty: sampledProperty
});
) = abi.decode(data, (DatalakeCode, uint256, uint256, uint256, uint256, bytes));
return BlockSampledDatalake({
chainId: chainId,
blockRangeStart: blockRangeStart,
blockRangeEnd: blockRangeEnd,
increment: increment,
sampledProperty: sampledProperty
});
}
}
15 changes: 2 additions & 13 deletions src/datatypes/datalake/ComputeCodecs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,7 @@ library ComputationalTaskCodecs {
/// @notice The commitment embeds the datalake commitment.
/// @param task The ComputationalTask to commit.
/// @param datalakeCommitment The commitment of the datalake.
function commit(
ComputationalTask memory task,
bytes32 datalakeCommitment
) internal pure returns (bytes32) {
return
keccak256(
abi.encode(
datalakeCommitment,
task.aggregateFnId,
task.operatorId,
task.valueToCompare
)
);
function commit(ComputationalTask memory task, bytes32 datalakeCommitment) internal pure returns (bytes32) {
return keccak256(abi.encode(datalakeCommitment, task.aggregateFnId, task.operatorId, task.valueToCompare));
}
}
Loading

0 comments on commit 705b706

Please sign in to comment.