From 171466e80b62a0fb731720ece56dc409248756ea Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Thu, 9 Nov 2023 16:37:49 -0800 Subject: [PATCH 1/2] Revert "Adds final Oracle Logic & Tests rewards disbursement " --- contracts/Oracle.sol | 33 +-------- contracts/PreConfirmations.sol | 27 ++----- contracts/UserRegistry.sol | 7 -- contracts/interfaces/IPreConfirmations.sol | 13 +--- hardhat.config.js | 9 +-- test/OracleTest.sol | 85 ++-------------------- test/PreConfirmationConfTest.sol | 29 +++++++- 7 files changed, 51 insertions(+), 152 deletions(-) diff --git a/contracts/Oracle.sol b/contracts/Oracle.sol index 6d5ed13..92578ab 100644 --- a/contracts/Oracle.sol +++ b/contracts/Oracle.sol @@ -14,8 +14,6 @@ import {IPreConfCommitmentStore} from './interfaces/IPreConfirmations.sol'; */ contract Oracle is Ownable { - mapping(string => address) public blockBuilderNameToAddress; - // To shutup the compiler // TODO(@ckartik): remove or make Oracle non-payable receive() external payable { @@ -41,18 +39,13 @@ contract Oracle is Ownable { preConfContract = IPreConfCommitmentStore(_preConfContract); } - // mapping of txns to bool to check if txns exists - // Stores all proccessed txns for onw - // TODO(@ckartik): This may be too restricvie in the log run as an appraoch - mapping(string => bool) txnHashes; - // Event to request block data event BlockDataRequested(uint256 blockNumber); // Event to signal the reception of block data event BlockDataReceived( - string[] txnList, + bytes32[] txnList, uint256 blockNumber, string blockBuilderName ); @@ -60,10 +53,6 @@ contract Oracle is Ownable { // Event to signal the processing of a commitment event CommitmentProcessed(bytes32 commitmentHash, bool isSlash); - function addBuilderAddress(string memory builderName, address builderAddress) external onlyOwner { - blockBuilderNameToAddress[builderName] = builderAddress; - } - // Function to request the block data function requestBlockData(uint256 blockNumber) external { // Emit an event that data request has been made @@ -73,32 +62,14 @@ contract Oracle is Ownable { // Function to receive and process the block data (this would be automated in a real-world scenario) // TODO(@ckartik): Should restrict who can make this call function receiveBlockData( - string[] calldata txnList, + bytes32[] calldata txnList, uint256 blockNumber, string calldata blockBuilderName ) external { // Emit an event that the block data has been received emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - address builder = blockBuilderNameToAddress[blockBuilderName]; - - for (uint256 i = 0; i < txnList.length; i++) { - txnHashes[txnList[i]] = true; - } - // Placeholder: Process the block data and determine the commitment's validity // For demonstration, we'll call this with a dummy commitment hash and isSlash flag - bytes32[] memory commitmentHashes = preConfContract.getCommitmentsByBlockNumber(blockNumber); - for (uint256 i = 0; i < commitmentHashes.length; i++) { - IPreConfCommitmentStore.PreConfCommitment memory commitment = preConfContract.getCommitment(commitmentHashes[i]); - if (commitment.commiter == builder) { - if (txnHashes[commitment.txnHash]){ - this.processCommitment(commitmentHashes[i], false); - } - else { - this.processCommitment(commitmentHashes[i], true); - } - } - } } // Function to simulate the processing of a commitment (initiate a slash or a reward) diff --git a/contracts/PreConfirmations.sol b/contracts/PreConfirmations.sol index 5a62464..aea3d20 100644 --- a/contracts/PreConfirmations.sol +++ b/contracts/PreConfirmations.sol @@ -68,7 +68,7 @@ contract PreConfCommitmentStore is Ownable { uint64 blockNumber; bytes32 bidHash; string txnHash; - bytes32 commitmentHash; + string commitmentHash; bytes bidSignature; bytes commitmentSignature; } @@ -272,6 +272,7 @@ contract PreConfCommitmentStore is Ownable { * @param bid The bid amount. * @param blockNumber The block number. * @param txnHash The transaction hash. + * @param commitmentHash The commitment hash. * @param bidSignature The signature of the bid. * @param commitmentSignature The signature of the commitment. * @return commitmentIndex The index of the stored commitment @@ -280,6 +281,7 @@ contract PreConfCommitmentStore is Ownable { uint64 bid, uint64 blockNumber, string memory txnHash, + string memory commitmentHash, bytes calldata bidSignature, bytes memory commitmentSignature ) public returns (bytes32 commitmentIndex) { @@ -311,7 +313,7 @@ contract PreConfCommitmentStore is Ownable { blockNumber, bHash, txnHash, - preConfHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -347,28 +349,15 @@ contract PreConfCommitmentStore is Ownable { } - /** - * @dev Retrieves the list of commitments for a given block number. - * @param blockNumber The block number. - * @return A list of indexes referencing preconfimration structures for the specified block number. - */ - function getCommitmentsByBlockNumber(uint256 blockNumber) - public - view - returns (bytes32[] memory) - { - return blockCommitments[blockNumber]; - } - /** - * @dev Get a commitment by its commitmentIndex. - * @param commitmentIndex The index of the commitment. + * @dev Get a commitment by its hash. + * @param commitmentHash The hash of the commitment. * @return A PreConfCommitment structure representing the commitment. */ function getCommitment( - bytes32 commitmentIndex + bytes32 commitmentHash ) public view returns (PreConfCommitment memory) { - return commitments[commitmentIndex]; + return commitments[commitmentHash]; } /** diff --git a/contracts/UserRegistry.sol b/contracts/UserRegistry.sol index d107c42..71b4fe5 100644 --- a/contracts/UserRegistry.sol +++ b/contracts/UserRegistry.sol @@ -102,13 +102,6 @@ contract UserRegistry is IUserRegistry, Ownable, ReentrancyGuard { preConfirmationsContract = contractAddress; } - /** - * @dev Get the amount assigned to a provider. - */ - function getProviderAmount(address provider) external view returns (uint256) { - return providerAmount[provider]; - } - /** * @dev Internal function for user registration and staking. */ diff --git a/contracts/interfaces/IPreConfirmations.sol b/contracts/interfaces/IPreConfirmations.sol index 6cc442e..dbdd3f4 100644 --- a/contracts/interfaces/IPreConfirmations.sol +++ b/contracts/interfaces/IPreConfirmations.sol @@ -8,7 +8,6 @@ pragma solidity ^0.8.20; interface IPreConfCommitmentStore { // Structs, events, and errors can also be included in the interface if they are used in the external functions - /// @dev Struct for all the information around preconfirmations commitment struct PreConfCommitment { bool commitmentUsed; address bidder; @@ -17,12 +16,11 @@ interface IPreConfCommitmentStore { uint64 blockNumber; bytes32 bidHash; string txnHash; - bytes32 commitmentHash; + string commitmentHash; bytes bidSignature; bytes commitmentSignature; } - event SignatureVerified( address indexed signer, string txnHash, @@ -66,14 +64,11 @@ interface IPreConfCommitmentStore { bytes memory commitmentSignature ) external returns (uint256); - function getCommitmentsByBlockNumber(uint256 blockNumber) external view returns (bytes32[] memory); - - - function getCommitment(bytes32 commitmentIndex) external view returns (PreConfCommitment memory); + function getCommitment(bytes32 commitmentHash) external view returns (PreConfCommitment memory); - function initiateSlash(bytes32 commitmentIndex) external; + function initiateSlash(bytes32 commitmentHash) external; - function initateReward(bytes32 commitmentIndex) external; + function initateReward(bytes32 commitmentHash) external; function updateOracle(address newOracle) external; diff --git a/hardhat.config.js b/hardhat.config.js index bb03a13..6ebb520 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -9,11 +9,10 @@ module.exports = { localhost: { url: "http://127.0.0.1:8545", // Ganache default port }, - aws: { - url: "http://34.213.237.94:8123", - chainId: 1001, - accounts: ['0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e'] //account private key - } + hardhat: {}, + op_docker: { + url: "http://op-geth:8545", + }, }, docgen: { pages: "files", diff --git a/test/OracleTest.sol b/test/OracleTest.sol index 664a75d..275ac6b 100644 --- a/test/OracleTest.sol +++ b/test/OracleTest.sol @@ -9,7 +9,6 @@ import "../contracts/ProviderRegistry.sol"; import "../contracts/UserRegistry.sol"; contract OracleTest is Test { - using ECDSA for bytes32; Oracle internal oracle; PreConfCommitmentStore internal preConfCommitmentStore; uint16 internal feePercent; @@ -24,7 +23,7 @@ contract OracleTest is Test { // Events to match against event BlockDataRequested(uint256 blockNumber); - event BlockDataReceived(string[] txnList, uint256 blockNumber, string blockBuilderName); + event BlockDataReceived(bytes32[] txnList, uint256 blockNumber, string blockBuilderName); event CommitmentProcessed(bytes32 commitmentHash, bool isSlash); function setUp() public { @@ -50,13 +49,10 @@ contract OracleTest is Test { address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3; vm.deal(signer, 5 ether); - vm.startPrank(signer); + vm.prank(signer); userRegistry.registerAndStake{value: 2 ether}(); - - // vm.prank(signer); + oracle = new Oracle(address(preConfCommitmentStore)); - oracle.addBuilderAddress("mev builder", signer); - vm.stopPrank(); preConfCommitmentStore.updateOracle(address(oracle)); userRegistry.setPreconfirmationsContract(address(preConfCommitmentStore)); @@ -75,8 +71,8 @@ contract OracleTest is Test { } function test_ReceiveBlockData() public { - string[] memory txnList = new string[](1); - txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); + bytes32[] memory txnList = new bytes32[](1); + txnList[0] = keccak256("0xkartik"); uint256 blockNumber = block.number; string memory blockBuilderName = "mev builder"; vm.expectEmit(true, true, false, true); @@ -84,75 +80,6 @@ contract OracleTest is Test { oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); } - /** - constructAndStoreCommitment is a helper function to construct and store a commitment - */ - function constructAndStoreCommitment( - uint64 bid, - uint64 blockNumber, - string memory txnHash, - uint256 bidderPk, - uint256 signerPk - ) public returns (bytes32 commitmentIndex) { - bytes32 bidHash = preConfCommitmentStore.getBidHash( - txnHash, - bid, - blockNumber - ); - - - (uint8 v,bytes32 r, bytes32 s) = vm.sign(bidderPk, bidHash); - bytes memory bidSignature = abi.encodePacked(r, s, v); - - bytes32 commitmentHash = preConfCommitmentStore.getPreConfHash( - txnHash, - bid, - blockNumber, - bidHash, - _bytesToHexString(bidSignature) - ); - - (v,r,s) = vm.sign(signerPk, commitmentHash); - bytes memory commitmentSignature = abi.encodePacked(r, s, v); - - commitmentIndex = preConfCommitmentStore.storeCommitment( - bid, - blockNumber, - txnHash, - bidSignature, - commitmentSignature - ); - - return commitmentIndex; - } - - function test_ReceiveBlockDataWithCommitments() public { - string[] memory txnList = new string[](1); - txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); - uint64 blockNumber = 200; - uint64 bid = 2; - string memory blockBuilderName = "kartik builder"; - (address user, uint256 userPk) = makeAddrAndKey("alice"); - - vm.deal(user, 200000 ether); - vm.startPrank(user); - userRegistry.registerAndStake{value: 250 ether }(); - providerRegistry.registerAndStake{value: 250 ether}(); - vm.stopPrank(); - - bytes32 commitmentIndex = constructAndStoreCommitment(bid, blockNumber, txnList[0], userPk, userPk); - vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); - oracle.addBuilderAddress("kartik builder", user); - vm.expectEmit(true, true, false, true); - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); - - bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber); - assertEq(commitmentHashes.length, 1); - assertEq(userRegistry.getProviderAmount(user), bid); - - } - // function test_ProcessCommitment_Slash() public { // TODO(@ckartik): Add test // } @@ -171,7 +98,7 @@ contract OracleTest is Test { ); bytes memory commitmentSignature = hex"ff7e00cf5c2d0fa9ef7c5efdca68b285a664a3aab927eb779b464207f537551f4ff81b085acf78b58ecb8c96c9a4efcb2172a0287f5bf5819b49190f6e2d2d1e1b"; - bytes32 commitmentIndex = preConfCommitmentStore.storeCommitment(bid, blockNumber, txnHash, bidSignature, commitmentSignature); + bytes32 commitmentIndex = preConfCommitmentStore.storeCommitment(bid, blockNumber, txnHash, cHash, bidSignature, commitmentSignature); bytes32 bidHash = preConfCommitmentStore.getBidHash( txnHash, diff --git a/test/PreConfirmationConfTest.sol b/test/PreConfirmationConfTest.sol index 6eef26d..81782d4 100644 --- a/test/PreConfirmationConfTest.sol +++ b/test/PreConfirmationConfTest.sol @@ -73,7 +73,7 @@ contract TestPreConfCommitmentStore is Test { assertEq(user, recoveredAddress); assertEq(digest, bidHash); - preConfCommitmentStore.storeCommitment(200 wei, 3000, "0xkartik", signature, signature); + preConfCommitmentStore.storeCommitment(200 wei, 3000, "0xkartik", "0xkartik", signature, signature); } @@ -144,7 +144,8 @@ contract TestPreConfCommitmentStore is Test { vm.prank(signer); userRegistry.registerAndStake{value: 2 ether}(); string memory txnHash = "0xkartik"; - bytes32 commitmentHash = bytes32(hex"31dca6c6fd15593559dabb9e25285f727fd33f07e17ec2e8da266706020034dc"); + string + memory commitmentHash = "0x31dca6c6fd15593559dabb9e25285f727fd33f07e17ec2e8da266706020034dc"; bytes memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c"; uint64 bid = 2; @@ -163,6 +164,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -173,6 +175,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -208,6 +211,7 @@ contract TestPreConfCommitmentStore is Test { uint64 bid, uint64 blockNumber, string memory txnHash, + string memory commitmentHash, bytes memory bidSignature, bytes memory commitmentSignature ) internal returns (bytes32) { @@ -215,6 +219,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -227,10 +232,17 @@ contract TestPreConfCommitmentStore is Test { uint64 bid, uint64 blockNumber, string memory txnHash, + string memory commitmentHash, bytes memory bidSignature, bytes memory commitmentSignature ) public { + bytes32 reconstructedIndex = keccak256( + abi.encodePacked( + commitmentHash, + commitmentSignature + ) + ); (PreConfCommitmentStore.PreConfCommitment memory commitment) = preConfCommitmentStore .getCommitment(index); @@ -248,6 +260,11 @@ contract TestPreConfCommitmentStore is Test { assert(commitments.length >= 1); + assertEq( + index, + reconstructedIndex, + "Returned hash should match the preConfHash" + ); assertEq( commitment.commitmentUsed, false, @@ -264,6 +281,11 @@ contract TestPreConfCommitmentStore is Test { txnHash, "Stored txnHash should match input txnHash" ); + assertEq( + commitment.commitmentHash, + commitmentHash, + "Stored commitmentHash should match input commitmentHash" + ); assertEq( commitment.bidSignature, bidSignature, @@ -302,6 +324,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -360,6 +383,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); @@ -425,6 +449,7 @@ contract TestPreConfCommitmentStore is Test { bid, blockNumber, txnHash, + commitmentHash, bidSignature, commitmentSignature ); From 1e6a6b691b80b5750b6fe5e8ac617f3c7de60ac9 Mon Sep 17 00:00:00 2001 From: kant Date: Sat, 11 Nov 2023 15:59:53 -0800 Subject: [PATCH 2/2] deploy scripts using foundry --- README.md | 31 ++++++++++++++++++++++++++++++- scripts/DeployScript.s.sol | 29 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 scripts/DeployScript.s.sol diff --git a/README.md b/README.md index fde3a09..d27b490 100644 --- a/README.md +++ b/README.md @@ -122,12 +122,41 @@ sequenceDiagram ``` -#### Deploy Scripts +#### Deploy Scripts using hardhat ``` npx hardhat run scripts/deploy.js ``` +#### Deploy Scripts using forge + +- Install foundryup + +``` +curl -L https://foundry.paradigm.xyz | bash +``` + +- Running foundryup by itself will install the latest (nightly) precompiled binaries: forge, cast, anvil and chisel +``` +foundryup +``` + +- Setup ENV Vars + +``` +export RPC_URL="http://localhost:8545/" +export PRIVATE_KEY="your-private-key" +export CHAIN_ID=17864 +``` + +- Run the deploy script + +``` +forge script scripts/DeployScript.s.sol:DeployScript --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --chain-id $CHAIN_ID -vvvv + +``` + + #### Test Contracts ``` diff --git a/scripts/DeployScript.s.sol b/scripts/DeployScript.s.sol new file mode 100644 index 0000000..952db60 --- /dev/null +++ b/scripts/DeployScript.s.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.15; +import "forge-std/Script.sol"; +import "contracts/UserRegistry.sol"; +import "contracts/ProviderRegistry.sol"; +import "contracts/PreConfirmations.sol"; + +contract DeployScript is Script { + function run() external { + vm.startBroadcast(); + + // Replace these with your contract's constructor parameters + uint256 minStake = 1 ether; + address feeRecipient = address(0x388C818CA8B9251b393131C08a736A67ccB19297); + uint16 feePercent = 15; + + UserRegistry userRegistry = new UserRegistry(minStake, feeRecipient, feePercent); + console.log("UserRegistry deployed to:", address(userRegistry)); + + ProviderRegistry providerRegistry = new ProviderRegistry(minStake, feeRecipient, feePercent); + console.log("ProviderRegistry deployed to:", address(providerRegistry)); + + PreConfCommitmentStore preConfCommitmentStore = new PreConfCommitmentStore(address(userRegistry), address(providerRegistry), feeRecipient); + console.log("PreConfCommitmentStore deployed to:", address(preConfCommitmentStore)); + + vm.stopBroadcast(); + } +} +