From 69858ffea820b90620a2b76d2de741be06cbce55 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Wed, 27 Dec 2023 12:15:31 -0500 Subject: [PATCH 1/3] Updates oracle with new processing logic. --- contracts/Oracle.sol | 57 ++--- contracts/PreConfirmations.sol | 9 +- test/OracleTest.sol | 395 +++++++++++++++++++++++---------- 3 files changed, 304 insertions(+), 157 deletions(-) diff --git a/contracts/Oracle.sol b/contracts/Oracle.sol index 4ae95f5..9e4f0cb 100644 --- a/contracts/Oracle.sol +++ b/contracts/Oracle.sol @@ -55,18 +55,18 @@ contract Oracle is Ownable { // 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; + // mapping(string => bool) txnHashes; - // Event to request block data - event BlockDataRequested(uint256 blockNumber); + // // Event to request block data + // event BlockDataRequested(uint256 blockNumber); - // Event to signal the reception of block data - event BlockDataReceived( - string[] txnList, - uint256 blockNumber, - string blockBuilderName - ); + // // Event to signal the reception of block data + // event BlockDataReceived( + // string[] txnList, + // uint256 blockNumber, + // string blockBuilderName + // ); // Event to signal the processing of a commitment event CommitmentProcessed(bytes32 commitmentHash, bool isSlash); @@ -75,40 +75,26 @@ contract Oracle is Ownable { blockBuilderNameToAddress[builderName] = builderAddress; } - // Function to request the block data - function requestBlockData(uint256 blockNumber) external { - // Emit an event that data request has been made - emit BlockDataRequested(blockNumber); + + function getBuilder(string calldata builderNameGrafiti) external view returns (address) { + return blockBuilderNameToAddress[builderNameGrafiti]; } // 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, + // Note: Long term we will want to + function processBuilderCommitmentForBlockNumber( + bytes32 commitmentHash, uint256 blockNumber, - string calldata blockBuilderName + string calldata blockBuilderName, + bool isSlash ) external { - // Emit an event that the block data has been received - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); + // Check grafiti against registered builder IDs 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); - } - } + IPreConfCommitmentStore.PreConfCommitment memory commitment = preConfContract.getCommitment(commitmentHash); + if (commitment.commiter == builder) { + this.processCommitment(commitmentHash, isSlash); } if (nextRequestedBlockNumber <= blockNumber) { @@ -116,6 +102,7 @@ contract Oracle is Ownable { } } + // Function to simulate the processing of a commitment (initiate a slash or a reward) function processCommitment(bytes32 commitmentIndex, bool isSlash) external { if (isSlash) { diff --git a/contracts/PreConfirmations.sol b/contracts/PreConfirmations.sol index 864c66b..f7e56f8 100644 --- a/contracts/PreConfirmations.sol +++ b/contracts/PreConfirmations.sol @@ -260,12 +260,13 @@ contract PreConfCommitmentStore is Ownable { } function getCommitmentIndex( - PreConfCommitment memory commitment + bytes32 commitmentHash, + bytes memory commitmentSignature ) public pure returns (bytes32){ return keccak256( abi.encodePacked( - commitment.commitmentHash, - commitment.commitmentSignature + commitmentHash, + commitmentSignature ) ); } @@ -319,7 +320,7 @@ contract PreConfCommitmentStore is Ownable { commitmentSignature ); - commitmentIndex = getCommitmentIndex(newCommitment); + commitmentIndex = getCommitmentIndex(newCommitment.commitmentHash, newCommitment.commitmentSignature); // Store commitment diff --git a/test/OracleTest.sol b/test/OracleTest.sol index 30f9f75..a906880 100644 --- a/test/OracleTest.sol +++ b/test/OracleTest.sol @@ -85,11 +85,16 @@ contract OracleTest is Test { function test_builderUnidentified() public { vm.startPrank(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3); + // Unregistered Builder (address bidder, uint256 bidderPk) = makeAddrAndKey("k builder"); (address provider, uint256 providerPk) = makeAddrAndKey("primev builder"); + (address builder3,) = makeAddrAndKey("titan builder"); (address builder4,) = makeAddrAndKey("zk builder"); + uint64 blockNumber = 2; + uint64 bid = 2; + oracle.addBuilderAddress("titan builder", builder3); oracle.addBuilderAddress("zk builder", builder4); @@ -108,92 +113,71 @@ contract OracleTest is Test { providerRegistry.registerAndStake{value: 250 ether}(); vm.stopPrank(); - constructAndStoreCommitment(2, 2, "0xkartik", bidderPk, providerPk); + bytes32 commitmentIndex = constructAndStoreCommitment(bid, blockNumber, "0xkartik", bidderPk, providerPk); string[] memory txnList = new string[](1); txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); - oracle.receiveBlockData(txnList, 2, "primev builder"); + oracle.processBuilderCommitmentForBlockNumber(commitmentIndex, blockNumber, "k builder", false); assertEq(bidderRegistry.getProviderAmount(provider), 0); assertEq(providerRegistry.checkStake(provider), 250 ether); } - function test_RequestBlockData() public { - address signer = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3; - vm.deal(signer, 5 ether); - vm.prank(signer); - uint256 blockNumber = block.number; - vm.expectEmit(); - emit BlockDataRequested(blockNumber); - oracle.requestBlockData(blockNumber); - } - function test_ReceiveBlockData() public { - string[] memory txnList = new string[](1); - txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); - uint256 blockNumber = 3; - string memory blockBuilderName = "mev builder"; - vm.expectEmit(true, true, false, true); - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); - assertEq(oracle.nextRequestedBlockNumber(), blockNumber + 1); - } - function test_ReceiveBlockData_Empty() public { - string[] memory txnList = new string[](1); - uint256 blockNumber = 13; - string memory blockBuilderName = "mev builder"; - vm.expectEmit(true, true, false, true); - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); - assertEq(oracle.nextRequestedBlockNumber(), blockNumber + 1); - } + // function test_ReceiveBlockData() public { + // string[] memory txnList = new string[](1); + // txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); + // uint256 blockNumber = 3; + // string memory blockBuilderName = "mev builder"; + // vm.expectEmit(true, true, false, true); + // emit BlockDataReceived(txnList, blockNumber, blockBuilderName); + // oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); + // assertEq(oracle.nextRequestedBlockNumber(), blockNumber + 1); + // } - /** - 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 - ); + // function test_ReceiveBlockData_Empty() public { + // string[] memory txnList = new string[](1); + // uint256 blockNumber = 13; + // string memory blockBuilderName = "mev builder"; + // vm.expectEmit(true, true, false, true); + // emit BlockDataReceived(txnList, blockNumber, blockBuilderName); + // oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); + // assertEq(oracle.nextRequestedBlockNumber(), blockNumber + 1); + // } + function test_process_commitment_payment_payout() public { + string memory txn = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; + uint64 blockNumber = 200; + uint64 bid = 2; + string memory blockBuilderName = "kartik builder"; + (address bidder, uint256 bidderPk) = makeAddrAndKey("alice"); + (address provider, uint256 providerPk) = makeAddrAndKey("kartik"); - (uint8 v,bytes32 r, bytes32 s) = vm.sign(bidderPk, bidHash); - bytes memory bidSignature = abi.encodePacked(r, s, v); + vm.deal(bidder, 200000 ether); + vm.startPrank(bidder); + bidderRegistry.prepay{value: 250 ether }(); + vm.stopPrank(); - bytes32 commitmentHash = preConfCommitmentStore.getPreConfHash( - txnHash, - bid, - blockNumber, - bidHash, - _bytesToHexString(bidSignature) - ); + vm.deal(provider, 200000 ether); + vm.startPrank(provider); + providerRegistry.registerAndStake{value: 250 ether}(); + vm.stopPrank(); - (v,r,s) = vm.sign(signerPk, commitmentHash); - bytes memory commitmentSignature = abi.encodePacked(r, s, v); + bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, bidderPk, providerPk); - commitmentIndex = preConfCommitmentStore.storeCommitment( - bid, - blockNumber, - txnHash, - bidSignature, - commitmentSignature - ); + vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); + oracle.addBuilderAddress(blockBuilderName, provider); + + oracle.processBuilderCommitmentForBlockNumber(index, blockNumber, blockBuilderName, false); + + assertEq(bidderRegistry.getProviderAmount(provider), bid); - return commitmentIndex; } - function test_ReceiveBlockDataWithCommitments() public { - string[] memory txnList = new string[](1); - txnList[0] = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; + + function test_process_commitment_slash() public { + string memory txn = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; uint64 blockNumber = 200; uint64 bid = 2; string memory blockBuilderName = "kartik builder"; @@ -210,92 +194,267 @@ contract OracleTest is Test { providerRegistry.registerAndStake{value: 250 ether}(); vm.stopPrank(); - constructAndStoreCommitment(bid, blockNumber, txnList[0], bidderPk, providerPk); + bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, bidderPk, providerPk); + vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); - oracle.addBuilderAddress("kartik builder", provider); - vm.expectEmit(true, true, false, true); - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); + oracle.addBuilderAddress(blockBuilderName, provider); - bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber); - assertEq(commitmentHashes.length, 1); - assertEq(bidderRegistry.getProviderAmount(provider), bid); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index, true); + oracle.processBuilderCommitmentForBlockNumber(index, blockNumber, blockBuilderName, true); + assertEq(providerRegistry.checkStake(provider) + bid, 250 ether); } - function test_ReceiveBlockDataWithCommitmentsSlashed() public { - string[] memory txnList = new string[](1); - txnList[0] = string(abi.encodePacked(keccak256("0xkartik"))); - uint64 blockNumber = 200; - uint64 bid = 2; + function test_process_commitment_slash_and_reward() public { + string memory txn1 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; + string memory txn2 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d09"; + uint64 blockNumber = 201; + uint64 bid = 5; string memory blockBuilderName = "kartik builder"; (address bidder, uint256 bidderPk) = makeAddrAndKey("alice"); - (address provider, uint256 providerPk) = makeAddrAndKey("bob"); + (address provider, uint256 providerPk) = makeAddrAndKey("kartik"); vm.deal(bidder, 200000 ether); + vm.startPrank(bidder); + bidderRegistry.prepay{value: 250 ether }(); + vm.stopPrank(); + vm.deal(provider, 200000 ether); + vm.startPrank(provider); + providerRegistry.registerAndStake{value: 250 ether}(); + vm.stopPrank(); + + bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, bidderPk, providerPk); + bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, bidderPk, providerPk); + + vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); + oracle.addBuilderAddress(blockBuilderName, provider); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index1, true); + oracle.processBuilderCommitmentForBlockNumber(index1, blockNumber, blockBuilderName, true); + + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index2, false); + oracle.processBuilderCommitmentForBlockNumber(index2, blockNumber, blockBuilderName, false); + + assertEq(providerRegistry.checkStake(provider), 250 ether - bid); + assertEq(bidderRegistry.getProviderAmount(provider), bid); + } + + + function test_process_commitment_slash_multiple() public { + string memory txn1 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; + string memory txn2 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d09"; + string memory txn3 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d10"; + string memory txn4 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d11"; + uint64 blockNumber = 201; + uint64 bid = 5; + string memory blockBuilderName = "kartik builder"; + (address bidder, uint256 bidderPk) = makeAddrAndKey("alice"); + (address provider, uint256 providerPk) = makeAddrAndKey("kartik"); + + vm.deal(bidder, 200000 ether); vm.startPrank(bidder); bidderRegistry.prepay{value: 250 ether }(); vm.stopPrank(); + vm.deal(provider, 200000 ether); vm.startPrank(provider); providerRegistry.registerAndStake{value: 250 ether}(); vm.stopPrank(); - uint256 ogStake = providerRegistry.checkStake(provider); + bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, bidderPk, providerPk); + bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, bidderPk, providerPk); + bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, bidderPk, providerPk); + bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, bidderPk, providerPk); + - string memory commitedTxn = string(abi.encodePacked(keccak256("0xSlash"))); - constructAndStoreCommitment(bid, blockNumber, commitedTxn, bidderPk, providerPk); vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); - oracle.addBuilderAddress("kartik builder", provider); - vm.expectEmit(true, true, false, true); - emit BlockDataReceived(txnList, blockNumber, blockBuilderName); - oracle.receiveBlockData(txnList, blockNumber, blockBuilderName); + oracle.addBuilderAddress(blockBuilderName, provider); - bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber); - assertEq(commitmentHashes.length, 1); - - // Ensuring no rewards + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index1, true); + oracle.processBuilderCommitmentForBlockNumber(index1, blockNumber, blockBuilderName, true); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index2, true); + oracle.processBuilderCommitmentForBlockNumber(index2, blockNumber, blockBuilderName, true); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index3, true); + oracle.processBuilderCommitmentForBlockNumber(index3, blockNumber, blockBuilderName, true); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index4, true); + oracle.processBuilderCommitmentForBlockNumber(index4, blockNumber, blockBuilderName, true); + + assertEq(providerRegistry.checkStake(provider), 250 ether - bid*4); assertEq(bidderRegistry.getProviderAmount(provider), 0); + } + + function test_process_commitment_reward_multiple() public { + string memory txn1 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08"; + string memory txn2 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d09"; + string memory txn3 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d10"; + string memory txn4 = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d11"; + uint64 blockNumber = 201; + uint64 bid = 5; + string memory blockBuilderName = "kartik builder"; + (address bidder, uint256 bidderPk) = makeAddrAndKey("alice"); + (address provider, uint256 providerPk) = makeAddrAndKey("kartik"); + + vm.deal(bidder, 200000 ether); + vm.startPrank(bidder); + bidderRegistry.prepay{value: 250 ether }(); + vm.stopPrank(); + + vm.deal(provider, 200000 ether); + vm.startPrank(provider); + providerRegistry.registerAndStake{value: 250 ether}(); + vm.stopPrank(); + + bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, bidderPk, providerPk); + bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, bidderPk, providerPk); + bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, bidderPk, providerPk); + bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, bidderPk, providerPk); + - // Detect slashing - uint256 postSlashStake = providerRegistry.checkStake(provider); - assertEq(postSlashStake + bid, ogStake); - assertEq(bidderRegistry.getAllowance(bidder), 250 ether); + vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); + oracle.addBuilderAddress(blockBuilderName, provider); + + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index1, false); + oracle.processBuilderCommitmentForBlockNumber(index1, blockNumber, blockBuilderName, false); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index2, false); + oracle.processBuilderCommitmentForBlockNumber(index2, blockNumber, blockBuilderName, false); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index3, false); + oracle.processBuilderCommitmentForBlockNumber(index3, blockNumber, blockBuilderName, false); + vm.expectEmit(true, false, false, true); + emit CommitmentProcessed(index4, false); + oracle.processBuilderCommitmentForBlockNumber(index4, blockNumber, blockBuilderName, false); + assertEq(providerRegistry.checkStake(provider), 250 ether); + assertEq(bidderRegistry.getProviderAmount(provider), 4*bid); } - // function test_ProcessCommitment_Slash() public { - // TODO(@ckartik): Add test + // function test_ReceiveBlockDataWithCommitmentsSlashed() 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 bidder, uint256 bidderPk) = makeAddrAndKey("alice"); + // (address provider, uint256 providerPk) = makeAddrAndKey("bob"); + + // vm.deal(bidder, 200000 ether); + // vm.deal(provider, 200000 ether); + + // vm.startPrank(bidder); + // bidderRegistry.prepay{value: 250 ether }(); + // vm.stopPrank(); + + // vm.startPrank(provider); + // providerRegistry.registerAndStake{value: 250 ether}(); + // vm.stopPrank(); + + // uint256 ogStake = providerRegistry.checkStake(provider); + + // string memory commitedTxn = string(abi.encodePacked(keccak256("0xSlash"))); + // constructAndStoreCommitment(bid, blockNumber, commitedTxn, bidderPk, providerPk); + // vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3)); + // oracle.addBuilderAddress("kartik builder", provider); + // 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); + + // // Ensuring no rewards + // assertEq(bidderRegistry.getProviderAmount(provider), 0); + + // // Detect slashing + // uint256 postSlashStake = providerRegistry.checkStake(provider); + // assertEq(postSlashStake + bid, ogStake); + // assertEq(bidderRegistry.getAllowance(bidder), 250 ether); + // } - function test_ProcessCommitment_Reward() public { + // // function test_ProcessCommitment_Slash() public { + // // TODO(@ckartik): Add test + // // } + + // function test_ProcessCommitment_Reward() public { + + // string memory txnHash = "0xkartik"; + // /* + // Temporarily hardcoding the values for the following variables for future testing. + // string + // memory cHash = "0x31dca6c6fd15593559dabb9e25285f727fd33f07e17ec2e8da266706020034dc"; + // bytes + // memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c"; + // */ + // uint64 bid = 2; + // uint64 blockNumber = 2; + // bytes memory bidSignature = bytes( + // hex"c10688ea554c1dae605619fa7f75103fb483ab6b5ad424e4e232f5da4449503a27ef6aed49b85bfd0e598650831c861a55a5eb197d9279d6a5667efaa46ab8831c" + // ); + // bytes + // memory commitmentSignature = hex"ff7e00cf5c2d0fa9ef7c5efdca68b285a664a3aab927eb779b464207f537551f4ff81b085acf78b58ecb8c96c9a4efcb2172a0287f5bf5819b49190f6e2d2d1e1b"; + // bytes32 commitmentIndex = preConfCommitmentStore.storeCommitment(bid, blockNumber, txnHash, bidSignature, commitmentSignature); + + // bool isSlash = false; + // vm.expectEmit(true, false, false, true); + // emit CommitmentProcessed(commitmentIndex, isSlash); + // oracle.processCommitment(commitmentIndex, isSlash); + // } - string memory txnHash = "0xkartik"; - /* - Temporarily hardcoding the values for the following variables for future testing. - string - memory cHash = "0x31dca6c6fd15593559dabb9e25285f727fd33f07e17ec2e8da266706020034dc"; - bytes - memory signature = "0xb170d082db1bf77fa0b589b9438444010dcb1e6dd326b661b02eb92abe4c066e243bb0d214b01667750ba2c53ff1ab445fd784b441dbc1f30280c379f002cc571c"; - */ - uint64 bid = 2; - uint64 blockNumber = 2; - bytes memory bidSignature = bytes( - hex"c10688ea554c1dae605619fa7f75103fb483ab6b5ad424e4e232f5da4449503a27ef6aed49b85bfd0e598650831c861a55a5eb197d9279d6a5667efaa46ab8831c" + + /** + 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 ); - bytes - memory commitmentSignature = hex"ff7e00cf5c2d0fa9ef7c5efdca68b285a664a3aab927eb779b464207f537551f4ff81b085acf78b58ecb8c96c9a4efcb2172a0287f5bf5819b49190f6e2d2d1e1b"; - bytes32 commitmentIndex = preConfCommitmentStore.storeCommitment(bid, blockNumber, txnHash, bidSignature, commitmentSignature); - bool isSlash = false; - vm.expectEmit(true, false, false, true); - emit CommitmentProcessed(commitmentIndex, isSlash); - oracle.processCommitment(commitmentIndex, isSlash); + + (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 _bytesToHexString( bytes memory _bytes ) public pure returns (string memory) { From b554eb20e9315a68fd154634f7c9a73073c64279 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Wed, 27 Dec 2023 12:30:49 -0500 Subject: [PATCH 2/3] moves block number iteration to other functions. --- contracts/Oracle.sol | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/contracts/Oracle.sol b/contracts/Oracle.sol index 9e4f0cb..657555b 100644 --- a/contracts/Oracle.sol +++ b/contracts/Oracle.sol @@ -82,9 +82,8 @@ 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 - // Note: Long term we will want to function processBuilderCommitmentForBlockNumber( - bytes32 commitmentHash, + bytes32 commitmentIndex, uint256 blockNumber, string calldata blockBuilderName, bool isSlash @@ -92,16 +91,22 @@ contract Oracle is Ownable { // Check grafiti against registered builder IDs address builder = blockBuilderNameToAddress[blockBuilderName]; - IPreConfCommitmentStore.PreConfCommitment memory commitment = preConfContract.getCommitment(commitmentHash); + IPreConfCommitmentStore.PreConfCommitment memory commitment = preConfContract.getCommitment(commitmentIndex); if (commitment.commiter == builder) { - this.processCommitment(commitmentHash, isSlash); + this.processCommitment(commitmentIndex, isSlash); } - if (nextRequestedBlockNumber <= blockNumber) { - nextRequestedBlockNumber = blockNumber + 1; - } } + function setNextBlock(uint64 newBlockNumber) external { + nextRequestedBlockNumber = newBlockNumber; + } + + function moveToNextBlock() external { + nextRequestedBlockNumber++; + } + + // Function to simulate the processing of a commitment (initiate a slash or a reward) function processCommitment(bytes32 commitmentIndex, bool isSlash) external { From b03510915b6b700818dc7b57def8aadde29376e6 Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Wed, 27 Dec 2023 13:43:09 -0500 Subject: [PATCH 3/3] go back to orignal use of commitment indexing. --- contracts/PreConfirmations.sol | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/contracts/PreConfirmations.sol b/contracts/PreConfirmations.sol index f7e56f8..864c66b 100644 --- a/contracts/PreConfirmations.sol +++ b/contracts/PreConfirmations.sol @@ -260,13 +260,12 @@ contract PreConfCommitmentStore is Ownable { } function getCommitmentIndex( - bytes32 commitmentHash, - bytes memory commitmentSignature + PreConfCommitment memory commitment ) public pure returns (bytes32){ return keccak256( abi.encodePacked( - commitmentHash, - commitmentSignature + commitment.commitmentHash, + commitment.commitmentSignature ) ); } @@ -320,7 +319,7 @@ contract PreConfCommitmentStore is Ownable { commitmentSignature ); - commitmentIndex = getCommitmentIndex(newCommitment.commitmentHash, newCommitment.commitmentSignature); + commitmentIndex = getCommitmentIndex(newCommitment); // Store commitment