Skip to content

Commit

Permalink
feat: introduce the use of dispatch timestamp (#12)
Browse files Browse the repository at this point in the history
* feat(decay): adds decay v2 mechanism

* feat(decay): update name of structure variable

* feat(decay): allow future dispatchTimestamp

* feat(decay): resolves comments made by shawn

* chore: update abi and gocodegen

* feat(decayv2): use direct timestamp in oracle

* feat(decayv2): add timestamp to rpc api

* feat(decayv2): updates decay in p2p layer

* chore: removes redundant l2client

* chore: start docker network in cli script

* chore: uses existing gitignore from main

* chore: update test provider to send dispatch time

* chore: adds some seconds decay end timestamp for testing

* chore: fixes underflow of dispatch drift

* chore: removes --no-cache flag from sl build

* chore: update decay range of real bidder

* feat: fix bugs in dockerfile for e2e

* chore: print decay percentage from oracle

* feat: use free holesky endpoint

* chore: avoid real bidder from pnaic

* chore: nit fix real-bidder

* chore: constructs demo mode

* chore: bugfix for negative decay

* feat: update demo to send bid externally

* feat: removes demo

* chore: nit rmv testing var

* feat: minor script improvement

* chore: remove demo profile from docker

* chore: redundant spaces

* chore: move to constant for testing dispatch

* chore: nit fix warning

* feat: adds struct to protobuf

* feat: moves struct to proto file

* chore: lowercase a variable

* chore: update expected error to lowercase

* feat: reverts moves struct to proto file

This reverts commit 99f5dbe.

* feat: reverts adds struct to protobuf

This reverts commit be832af.

* chore: remove sepolia key from script
  • Loading branch information
ckartik authored May 2, 2024
1 parent 67392ea commit 9def6a6
Show file tree
Hide file tree
Showing 32 changed files with 704 additions and 436 deletions.
54 changes: 45 additions & 9 deletions contracts-abi/abi/PreConfCommitmentStore.abi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"name": "_owner",
"type": "address",
"internalType": "address"
},
{
"name": "_commitment_dispatch_window",
"type": "uint64",
"internalType": "uint64"
}
],
"stateMutability": "nonpayable"
Expand Down Expand Up @@ -154,6 +159,19 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "commitment_dispatch_window",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint64",
"internalType": "uint64"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "commitments",
Expand Down Expand Up @@ -226,9 +244,9 @@
"internalType": "bytes"
},
{
"name": "blockCommitedAt",
"type": "uint256",
"internalType": "uint256"
"name": "dispatchTimestamp",
"type": "uint64",
"internalType": "uint64"
}
],
"stateMutability": "view"
Expand Down Expand Up @@ -368,9 +386,9 @@
"internalType": "bytes"
},
{
"name": "blockCommitedAt",
"type": "uint256",
"internalType": "uint256"
"name": "dispatchTimestamp",
"type": "uint64",
"internalType": "uint64"
}
]
}
Expand Down Expand Up @@ -447,9 +465,9 @@
"internalType": "bytes"
},
{
"name": "blockCommitedAt",
"type": "uint256",
"internalType": "uint256"
"name": "dispatchTimestamp",
"type": "uint64",
"internalType": "uint64"
}
]
}
Expand Down Expand Up @@ -726,6 +744,11 @@
"name": "commitmentSignature",
"type": "bytes",
"internalType": "bytes"
},
{
"name": "dispatchTimestamp",
"type": "uint64",
"internalType": "uint64"
}
],
"outputs": [
Expand Down Expand Up @@ -776,6 +799,19 @@
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "updateCommitmentDispatchWindow",
"inputs": [
{
"name": "newDispatchWindow",
"type": "uint64",
"internalType": "uint64"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "updateOracle",
Expand Down
114 changes: 83 additions & 31 deletions contracts-abi/clients/PreConfCommitmentStore/PreConfCommitmentStore.go

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions contracts/contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ contract PreConfCommitmentStore is Ownable {
bytes32 public constant EIP712_BID_TYPEHASH =
keccak256("PreConfBid(string txnHash,uint64 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp)");

// Represents the dispatch window in milliseconds
uint64 public commitment_dispatch_window;

/// @dev commitment counter
uint256 public commitmentCount;

Expand Down Expand Up @@ -75,7 +78,7 @@ contract PreConfCommitmentStore is Ownable {
bytes32 commitmentHash;
bytes bidSignature;
bytes commitmentSignature;
uint256 blockCommitedAt;
uint64 dispatchTimestamp;
}

/// @dev Event to log successful verifications
Expand Down Expand Up @@ -119,7 +122,8 @@ contract PreConfCommitmentStore is Ownable {
address _providerRegistry,
address _bidderRegistry,
address _oracle,
address _owner
address _owner,
uint64 _commitment_dispatch_window
) {
oracle = _oracle;
providerRegistry = IProviderRegistry(_providerRegistry);
Expand All @@ -142,6 +146,15 @@ contract PreConfCommitmentStore is Ownable {
keccak256("1")
)
);
commitment_dispatch_window = _commitment_dispatch_window;
}

/**
* @dev Updates the commitment dispatch window to a new value. This function can only be called by the contract owner.
* @param newDispatchWindow The new dispatch window value to be set.
*/
function updateCommitmentDispatchWindow(uint64 newDispatchWindow) external onlyOwner {
commitment_dispatch_window = newDispatchWindow;
}

/**
Expand Down Expand Up @@ -289,13 +302,16 @@ contract PreConfCommitmentStore is Ownable {
);
}



/**
* @dev Store a commitment.
* @param bid The bid amount.
* @param blockNumber The block number.
* @param txnHash The transaction hash.
* @param bidSignature The signature of the bid.
* @param commitmentSignature The signature of the commitment.
* @param dispatchTimestamp The timestamp at which the commitment is dispatched
* @return commitmentIndex The index of the stored commitment
*/
function storeCommitment(
Expand All @@ -305,7 +321,8 @@ contract PreConfCommitmentStore is Ownable {
uint64 decayStartTimeStamp,
uint64 decayEndTimeStamp,
bytes calldata bidSignature,
bytes memory commitmentSignature
bytes memory commitmentSignature,
uint64 dispatchTimestamp
) public returns (bytes32 commitmentIndex) {
(bytes32 bHash, address bidderAddress, uint256 stake) = verifyBid(
bid,
Expand All @@ -315,6 +332,9 @@ contract PreConfCommitmentStore is Ownable {
txnHash,
bidSignature
);

require(dispatchTimestamp >= block.timestamp || block.timestamp - dispatchTimestamp < commitment_dispatch_window, "Invalid dispatch timestamp, block.timestamp - dispatchTimestamp < commitment_dispatch_window");

// This helps in avoiding stack too deep
{
bytes32 commitmentDigest = getPreConfHash(
Expand Down Expand Up @@ -345,7 +365,7 @@ contract PreConfCommitmentStore is Ownable {
commitmentDigest,
bidSignature,
commitmentSignature,
block.number
dispatchTimestamp
);

commitmentIndex = getCommitmentIndex(newCommitment);
Expand Down
10 changes: 7 additions & 3 deletions contracts/contracts/interfaces/IPreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface IPreConfCommitmentStore {
bytes32 commitmentHash;
bytes bidSignature;
bytes commitmentSignature;
uint256 blockCommitedAt;
uint64 dispatchTimestamp;
}


Expand Down Expand Up @@ -66,12 +66,16 @@ interface IPreConfCommitmentStore {
uint64 blockNumber,
string memory txnHash,
string memory commitmentHash,
uint64 decayStartTimeStamp,
uint64 decayEndTimeStamp,
bytes calldata bidSignature,
bytes memory commitmentSignature
) external returns (uint256);
bytes memory commitmentSignature,
uint64 dispatchTimestamp
) external returns (bytes32 commitmentIndex);

function getCommitmentsByBlockNumber(uint256 blockNumber) external view returns (bytes32[] memory);

function updateCommitmentDispatchWindow(uint64 newDispatchWindow) external;

function getCommitment(bytes32 commitmentIndex) external view returns (PreConfCommitment memory);

Expand Down
3 changes: 2 additions & 1 deletion contracts/scripts/DeployScripts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ contract DeployScript is Script, Create2Deployer {
address feeRecipient = address(0x68bC10674b265f266b4b1F079Fa06eF4045c3ab9);
uint16 feePercent = 2;
uint256 nextRequestedBlockNumber = 4958905;
uint64 commitmentDispatchWindow = 250;

// Forge deploy with salt uses create2 proxy from https://github.com/primevprotocol/deterministic-deployment-proxy
bytes32 salt = 0x8989000000000000000000000000000000000000000000000000000000000000;
Expand All @@ -57,7 +58,7 @@ contract DeployScript is Script, Create2Deployer {
ProviderRegistry providerRegistry = new ProviderRegistry{salt: salt}(minStake, feeRecipient, feePercent, msg.sender);
console.log("ProviderRegistry deployed to:", address(providerRegistry));

PreConfCommitmentStore preConfCommitmentStore = new PreConfCommitmentStore{salt: salt}(address(providerRegistry), address(bidderRegistry), feeRecipient, msg.sender);
PreConfCommitmentStore preConfCommitmentStore = new PreConfCommitmentStore{salt: salt}(address(providerRegistry), address(bidderRegistry), feeRecipient, msg.sender, commitmentDispatchWindow);
console.log("PreConfCommitmentStore deployed to:", address(preConfCommitmentStore));

providerRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
Expand Down
47 changes: 29 additions & 18 deletions contracts/test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract OracleTest is Test {
uint64 testNumber2;
BidderRegistry internal bidderRegistry;
TestCommitment internal _testCommitmentAliceBob;
uint64 internal dispatchTimestampTesting;

struct TestCommitment {
uint64 bid;
Expand All @@ -32,6 +33,7 @@ contract OracleTest is Test {
bytes32 commitmentDigest;
bytes bidSignature;
bytes commitmentSignature;
uint64 dispatchTimestamp;
}

// Events to match against
Expand All @@ -53,7 +55,8 @@ contract OracleTest is Test {
0xa0327970258c49b922969af74d60299a648c50f69a2d98d6ab43f32f64ac2100,
0x54c118e537dd7cf63b5388a5fc8322f0286a978265d0338b108a8ca9d155dccc,
hex"876c1216c232828be9fabb14981c8788cebdf6ed66e563c4a2ccc82a577d052543207aeeb158a32d8977736797ae250c63ef69a82cd85b727da21e20d030fb311b",
hex"ec0f11f77a9e96bb9c2345f031a5d12dca8d01de8a2e957cf635be14802f9ad01c6183688f0c2672639e90cc2dce0662d9bea3337306ca7d4b56dd80326aaa231b"
hex"ec0f11f77a9e96bb9c2345f031a5d12dca8d01de8a2e957cf635be14802f9ad01c6183688f0c2672639e90cc2dce0662d9bea3337306ca7d4b56dd80326aaa231b",
1000
);

feePercent = 10;
Expand All @@ -71,7 +74,8 @@ contract OracleTest is Test {
address(providerRegistry), // Provider Registry
address(bidderRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this),
500
);

address ownerInstance = 0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3;
Expand All @@ -87,6 +91,10 @@ contract OracleTest is Test {
bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
providerRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));

// We set the system time to 1010 and dispatchTimestamps for testing to 1000
dispatchTimestampTesting = 1000;
vm.warp(1010);

}

function test_MultipleBlockBuildersRegistred() public {
Expand Down Expand Up @@ -142,7 +150,8 @@ contract OracleTest is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
bidderPk,
providerPk
providerPk,
_testCommitmentAliceBob.dispatchTimestamp
);

string[] memory txnList = new string[](1);
Expand Down Expand Up @@ -172,7 +181,7 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk);
bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk, dispatchTimestampTesting);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -202,7 +211,7 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk);
bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk, dispatchTimestampTesting);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -236,8 +245,8 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk);
bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -276,10 +285,10 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, 10, 20, bidderPk, providerPk);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, 10, 20, bidderPk, providerPk);
bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, 10, 20, bidderPk, providerPk,dispatchTimestampTesting);


vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand Down Expand Up @@ -323,13 +332,13 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk);
bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, 10, 20, bidderPk, providerPk ,dispatchTimestampTesting);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - bid);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk ,dispatchTimestampTesting);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 2*bid);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, 10, 20, bidderPk, providerPk);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, 10, 20, bidderPk, providerPk, dispatchTimestampTesting);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 3*bid);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, 10, 20, bidderPk, providerPk);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, 10, 20, bidderPk, providerPk, dispatchTimestampTesting);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 4*bid);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand Down Expand Up @@ -370,7 +379,7 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk);
bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, 10, 20, bidderPk, providerPk, dispatchTimestampTesting);
PreConfCommitmentStore.PreConfCommitment memory commitment = preConfCommitmentStore.getCommitment(index);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand All @@ -397,7 +406,8 @@ contract OracleTest is Test {
uint64 decayStartTimestamp,
uint64 decayEndTimestamp,
uint256 bidderPk,
uint256 signerPk
uint256 signerPk,
uint64 dispatchTimestamp
) public returns (bytes32 commitmentIndex) {
bytes32 bidHash = preConfCommitmentStore.getBidHash(
txnHash,
Expand Down Expand Up @@ -431,7 +441,8 @@ contract OracleTest is Test {
decayStartTimestamp,
decayEndTimestamp,
bidSignature,
commitmentSignature
commitmentSignature,
dispatchTimestamp
);

return commitmentIndex;
Expand Down
Loading

0 comments on commit 9def6a6

Please sign in to comment.