Skip to content

Commit

Permalink
Passes in dispatch window and allows for update.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Apr 9, 2024
1 parent 4cd7315 commit 690e433
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
17 changes: 15 additions & 2 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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;
uint64 public COMMITMENT_DISPATCH_WINDOW = 500;

/// @dev commitment counter
uint256 public commitmentCount;
Expand Down Expand Up @@ -121,7 +121,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 @@ -144,6 +145,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 @@ -291,6 +301,8 @@ contract PreConfCommitmentStore is Ownable {
);
}



/**
* @dev Store a commitment.
* @param bid The bid amount.
Expand Down Expand Up @@ -322,6 +334,7 @@ contract PreConfCommitmentStore is Ownable {

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
1 change: 1 addition & 0 deletions contracts/interfaces/IPreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ interface IPreConfCommitmentStore {

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 scripts/DeployScripts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ contract DeployScript is Script, Create2Deployer {
address feeRecipient = address(0x68bC10674b265f266b4b1F079Fa06eF4045c3ab9);
uint16 feePercent = 2;
uint256 nextRequestedBlockNumber = 4958905;
uint64 commitmentDispatchWindow = 500;

// Forge deploy with salt uses create2 proxy from https://github.com/primevprotocol/deterministic-deployment-proxy
bytes32 salt = 0x8989000000000000000000000000000000000000000000000000000000000000;
Expand All @@ -54,7 +55,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
3 changes: 2 additions & 1 deletion test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 Down
50 changes: 49 additions & 1 deletion test/PreConfirmationConfTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ contract TestPreConfCommitmentStore is Test {
address(providerRegistry), // Provider Registry
address(bidderRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this),
500
);

bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
Expand Down Expand Up @@ -169,6 +170,53 @@ contract TestPreConfCommitmentStore is Test {

}


function test_StoreCommitmentFailureDueToTimestampValidationWithNewWindow() 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.prank(preConfCommitmentStore.owner());
preConfCommitmentStore.updateCommitmentDispatchWindow(200);

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 - 200
);

}

function test_UpdateOracle() public {
preConfCommitmentStore.updateOracle(feeRecipient);
assertEq(preConfCommitmentStore.oracle(), feeRecipient);
Expand Down
3 changes: 2 additions & 1 deletion test/ProviderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ contract ProviderRegistryTest is Test {
address(providerRegistry), // Provider Registry
address(bidderRegistry), // User Registry
feeRecipient, // Oracle
address(this) // Owner
address(this),
500
);

provider = vm.addr(1);
Expand Down

0 comments on commit 690e433

Please sign in to comment.