Skip to content

Commit

Permalink
Introduces validation of dispatch timestamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Apr 9, 2024
1 parent d2fa528 commit 4cd7315
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 32 deletions.
13 changes: 10 additions & 3 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ contract PreConfCommitmentStore is Ownable {
bytes32 public constant EIP712_BID_TYPEHASH =
keccak256("PreConfBid(string txnHash,uint64 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp)");

uint64 public constant COMMITMENT_DISPATCH_WINDOW = 500;

/// @dev commitment counter
uint256 public commitmentCount;

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

/// @dev Event to log successful verifications
Expand Down Expand Up @@ -296,6 +298,7 @@ contract PreConfCommitmentStore is Ownable {
* @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 +308,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 +319,9 @@ contract PreConfCommitmentStore is Ownable {
txnHash,
bidSignature
);

require(block.timestamp >= dispatchTimestamp, "Invalid dispatch timestamp, block.timestamp < dispatchTimestamp");
require(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 +352,7 @@ contract PreConfCommitmentStore is Ownable {
commitmentDigest,
bidSignature,
commitmentSignature,
block.number
dispatchTimestamp
);

commitmentIndex = getCommitmentIndex(newCommitment);
Expand Down
8 changes: 5 additions & 3 deletions contracts/interfaces/IPreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ interface IPreConfCommitmentStore {
uint64 bid,
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);

Expand Down
40 changes: 23 additions & 17 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ contract OracleTest is Test {
bytes32 commitmentDigest;
bytes bidSignature;
bytes commitmentSignature;
uint64 dispatchTimestamp;
}

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

feePercent = 10;
Expand Down Expand Up @@ -86,6 +88,7 @@ contract OracleTest is Test {
preConfCommitmentStore.updateOracle(address(oracle));
bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
providerRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
vm.warp(1010);

}

Expand Down Expand Up @@ -142,7 +145,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 +176,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, 1000);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -202,7 +206,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, 1000);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -236,8 +240,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,1000);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk,1000);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand Down Expand Up @@ -276,10 +280,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,1000);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, 10, 20, bidderPk, providerPk,1000);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, 10, 20, bidderPk, providerPk,1000);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, 10, 20, bidderPk, providerPk,1000);


vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand Down Expand Up @@ -323,13 +327,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 ,1000);
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 ,1000);
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, 1000);
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, 1000);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 4*bid);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand Down Expand Up @@ -370,7 +374,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, 1000);
PreConfCommitmentStore.PreConfCommitment memory commitment = preConfCommitmentStore.getCommitment(index);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
Expand All @@ -397,7 +401,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 +436,8 @@ contract OracleTest is Test {
decayStartTimestamp,
decayEndTimestamp,
bidSignature,
commitmentSignature
commitmentSignature,
dispatchTimestamp
);

return commitmentIndex;
Expand Down
77 changes: 68 additions & 9 deletions test/PreConfirmationConfTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract TestPreConfCommitmentStore is Test {
bytes32 commitmentDigest;
bytes bidSignature;
bytes commitmentSignature;
uint64 dispatchTimestamp;
}

TestCommitment internal _testCommitmentAliceBob;
Expand All @@ -42,7 +43,8 @@ contract TestPreConfCommitmentStore is Test {
0xa0327970258c49b922969af74d60299a648c50f69a2d98d6ab43f32f64ac2100,
0x54c118e537dd7cf63b5388a5fc8322f0286a978265d0338b108a8ca9d155dccc,
hex"876c1216c232828be9fabb14981c8788cebdf6ed66e563c4a2ccc82a577d052543207aeeb158a32d8977736797ae250c63ef69a82cd85b727da21e20d030fb311b",
hex"ec0f11f77a9e96bb9c2345f031a5d12dca8d01de8a2e957cf635be14802f9ad01c6183688f0c2672639e90cc2dce0662d9bea3337306ca7d4b56dd80326aaa231b"
hex"ec0f11f77a9e96bb9c2345f031a5d12dca8d01de8a2e957cf635be14802f9ad01c6183688f0c2672639e90cc2dce0662d9bea3337306ca7d4b56dd80326aaa231b",
1000
);

feePercent = 10;
Expand All @@ -65,6 +67,9 @@ contract TestPreConfCommitmentStore is Test {
);

bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));

// Sets fake block timestamp
vm.warp(1000);
}

function test_Initialize() public {
Expand Down Expand Up @@ -115,7 +120,51 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
signature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);
}


function test_StoreCommitmentFailureDueToTimestampValidation() public {
bytes32 bidHash = preConfCommitmentStore.getBidHash(
_testCommitmentAliceBob.txnHash,
_testCommitmentAliceBob.bid,
_testCommitmentAliceBob.blockNumber,
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp
);
(address bidder, uint256 bidderPk) = makeAddrAndKey("alice");
// Wallet memory kartik = vm.createWallet('test wallet');
(uint8 v,bytes32 r, bytes32 s) = vm.sign(bidderPk, bidHash);
bytes memory signature = abi.encodePacked(r, s, v);

vm.deal(bidder, 200000 ether);
vm.prank(bidder);
bidderRegistry.prepay{value: 1e18 wei}();

(bytes32 digest, address recoveredAddress, uint256 stake) = preConfCommitmentStore.verifyBid(
_testCommitmentAliceBob.bid,
_testCommitmentAliceBob.blockNumber,
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.txnHash,
signature);

assertEq(stake, 1e18 wei);
assertEq(bidder, recoveredAddress);
assertEq(digest, bidHash);

vm.expectRevert("Invalid dispatch timestamp, block.timestamp - dispatchTimestamp < COMMITMENT_DISPATCH_WINDOW");
preConfCommitmentStore.storeCommitment(
_testCommitmentAliceBob.bid,
_testCommitmentAliceBob.blockNumber,
_testCommitmentAliceBob.txnHash,
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
signature,
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp - 500
);

}
Expand Down Expand Up @@ -219,7 +268,8 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.bidSignature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);

// Step 3: Verify the stored commitment
Expand Down Expand Up @@ -277,7 +327,8 @@ contract TestPreConfCommitmentStore is Test {
uint64 decayStartTimestamp,
uint64 decayEndTimestamp,
bytes memory bidSignature,
bytes memory commitmentSignature
bytes memory commitmentSignature,
uint64 dispatchTimestamp
) internal returns (bytes32) {
bytes32 commitmentIndex = preConfCommitmentStore.storeCommitment(
bid,
Expand All @@ -286,7 +337,8 @@ contract TestPreConfCommitmentStore is Test {
decayStartTimestamp,
decayEndTimestamp,
bidSignature,
commitmentSignature
commitmentSignature,
dispatchTimestamp
);

return commitmentIndex;
Expand Down Expand Up @@ -372,7 +424,8 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.bidSignature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);
PreConfCommitmentStore.PreConfCommitment
memory storedCommitment = preConfCommitmentStore.getCommitment(
Expand Down Expand Up @@ -427,7 +480,8 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.bidSignature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);
providerRegistry.setPreconfirmationsContract(
address(preConfCommitmentStore)
Expand Down Expand Up @@ -485,7 +539,8 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.bidSignature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);
(address commiter, ) = makeAddrAndKey("bob");
vm.deal(commiter, 5 ether);
Expand Down Expand Up @@ -541,7 +596,8 @@ contract TestPreConfCommitmentStore is Test {
_testCommitmentAliceBob.decayStartTimestamp,
_testCommitmentAliceBob.decayEndTimestamp,
_testCommitmentAliceBob.bidSignature,
_testCommitmentAliceBob.commitmentSignature
_testCommitmentAliceBob.commitmentSignature,
_testCommitmentAliceBob.dispatchTimestamp
);
(address commiter, ) = makeAddrAndKey("bob");
vm.deal(commiter, 5 ether);
Expand Down Expand Up @@ -573,3 +629,6 @@ contract TestPreConfCommitmentStore is Test {
return string(_string);
}
}



0 comments on commit 4cd7315

Please sign in to comment.