Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kant777 authored Nov 12, 2023
2 parents 9a012ef + 1e6a6b6 commit 3cff9c6
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 84 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
33 changes: 2 additions & 31 deletions contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,29 +39,20 @@ 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
);

// 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
Expand All @@ -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)
Expand Down
27 changes: 8 additions & 19 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract PreConfCommitmentStore is Ownable {
uint64 blockNumber;
bytes32 bidHash;
string txnHash;
bytes32 commitmentHash;
string commitmentHash;
bytes bidSignature;
bytes commitmentSignature;
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -311,7 +313,7 @@ contract PreConfCommitmentStore is Ownable {
blockNumber,
bHash,
txnHash,
preConfHash,
commitmentHash,
bidSignature,
commitmentSignature
);
Expand Down Expand Up @@ -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];
}

/**
Expand Down
7 changes: 0 additions & 7 deletions contracts/UserRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
13 changes: 4 additions & 9 deletions contracts/interfaces/IPreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 4 additions & 5 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions scripts/DeployScript.s.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}

16 changes: 6 additions & 10 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -207,7 +203,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,
Expand Down
Loading

0 comments on commit 3cff9c6

Please sign in to comment.