Skip to content

Commit

Permalink
Merge branch 'main' of github.com:primevprotocol/rollup-preconf
Browse files Browse the repository at this point in the history
  • Loading branch information
kant committed Nov 12, 2023
2 parents 6162303 + 3c3ba4e commit a3c2aec
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 15 deletions.
10 changes: 5 additions & 5 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.15;

import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

import {IProviderRegistry} from "./interfaces/IProviderRegistry.sol";
import {IUserRegistry} from "./interfaces/IUserRegistry.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

/**
* @title PreConfCommitmentStore - A contract for managing preconfirmation commitments and bids.
Expand Down Expand Up @@ -113,7 +113,7 @@ contract PreConfCommitmentStore is Ownable {
address _providerRegistry,
address _userRegistry,
address _oracle
) Ownable(msg.sender) {
) {
oracle = _oracle;
providerRegistry = IProviderRegistry(_providerRegistry);
userRegistry = IUserRegistry(_userRegistry);
Expand Down Expand Up @@ -149,7 +149,7 @@ contract PreConfCommitmentStore is Ownable {
uint64 _blockNumber
) public view returns (bytes32) {
return
MessageHashUtils.toTypedDataHash(
ECDSA.toTypedDataHash(
DOMAIN_SEPARATOR_BID,
keccak256(
abi.encode(
Expand Down Expand Up @@ -178,7 +178,7 @@ contract PreConfCommitmentStore is Ownable {
string memory _bidSignature
) public view returns (bytes32) {
return
MessageHashUtils.toTypedDataHash(
ECDSA.toTypedDataHash(
DOMAIN_SEPARATOR_PRECONF,
keccak256(
abi.encode(
Expand Down
6 changes: 3 additions & 3 deletions contracts/ProviderRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.15;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {PreConfCommitmentStore} from "./PreConfirmations.sol";
import {IProviderRegistry} from "./interfaces/IProviderRegistry.sol";

Expand Down Expand Up @@ -75,7 +75,7 @@ contract ProviderRegistry is IProviderRegistry, Ownable, ReentrancyGuard {
uint256 _minStake,
address _feeRecipient,
uint16 _feePercent
) Ownable(msg.sender) {
) {
minStake = _minStake;
feeRecipient = _feeRecipient;
feePercent = _feePercent;
Expand Down
6 changes: 3 additions & 3 deletions contracts/UserRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.15;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {IUserRegistry} from "./interfaces/IUserRegistry.sol";

/// @title User Registry
Expand Down Expand Up @@ -71,7 +71,7 @@ contract UserRegistry is IUserRegistry, Ownable, ReentrancyGuard {
uint256 _minStake,
address _feeRecipient,
uint16 _feePercent
) Ownable(msg.sender) {
) {
minStake = _minStake;
feeRecipient = _feeRecipient;
feePercent = _feePercent;
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IProviderRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.15;

interface IProviderRegistry {
function registerAndStake() external payable;
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IUserRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.15;

interface IUserRegistry {
struct PreConfCommitment {
Expand Down
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require("solidity-docgen");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.20",
solidity: "0.8.15",
networks: {
localhost: {
url: "http://127.0.0.1:8545", // Ganache default port
Expand Down
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
105 changes: 105 additions & 0 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,111 @@ 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_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 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();
uint256 ogStake = providerRegistry.checkStake(user);

string memory commitedTxn = string(abi.encodePacked(keccak256("0xSlash")));
bytes32 commitmentIndex = constructAndStoreCommitment(bid, blockNumber, commitedTxn, 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);

// Ensuring no rewards
assertEq(userRegistry.getProviderAmount(user), 0);

// Detect slashing
uint256 postSlashStake = providerRegistry.checkStake(user);
assertEq(postSlashStake + bid, ogStake);

}

// function test_ProcessCommitment_Slash() public {
// TODO(@ckartik): Add test
// }
Expand Down

0 comments on commit a3c2aec

Please sign in to comment.