Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keystore optimization #136

Merged
merged 19 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions contracts/contracts/PreConfCommitmentStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,19 @@ contract PreConfCommitmentStore is OwnableUpgradeable {
* @dev Store an encrypted commitment.
* @param commitmentDigest The digest of the commitment.
* @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 storeEncryptedCommitment(
bytes32 commitmentDigest,
bytes memory commitmentSignature,
uint64 dispatchTimestamp
) public returns (bytes32 commitmentIndex) {
require(
dispatchTimestamp >= block.timestamp ||
block.timestamp - dispatchTimestamp < commitmentDispatchWindow,
"Invalid dispatch timestamp, block.timestamp - dispatchTimestamp < commitmentDispatchWindow"
);
// Calculate the minimum valid timestamp for dispatching the commitment
uint256 minTime = block.timestamp - commitmentDispatchWindow;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to leave some commentary on why it was changed this way to avoid unwanted changes that would reintroduce the problem.

Copy link
Member Author

@Mikelle Mikelle Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I describe previous way in the comments then?


// Check if the dispatch timestamp is within the allowed dispatch window
require(dispatchTimestamp > minTime, "Invalid dispatch timestamp");

address commiterAddress = commitmentDigest.recover(commitmentSignature);

Expand All @@ -528,7 +529,6 @@ contract PreConfCommitmentStore is OwnableUpgradeable {

commitmentIndex = getEncryptedCommitmentIndex(newCommitment);

// Store commitment
encryptedCommitments[commitmentIndex] = newCommitment;

emit EncryptedCommitmentStored(
Expand Down
83 changes: 14 additions & 69 deletions contracts/test/PreConfirmationConfTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ contract TestPreConfCommitmentStore is Test {
);

// Sets fake block timestamp
vm.warp(16);
vm.warp(500);
bidderRegistry.setPreconfirmationsContract(
address(preConfCommitmentStore)
);
Expand Down Expand Up @@ -179,7 +179,7 @@ contract TestPreConfCommitmentStore is Test {

vm.warp(1000);
vm.expectRevert(
"Invalid dispatch timestamp, block.timestamp - dispatchTimestamp < commitmentDispatchWindow"
"Invalid dispatch timestamp"
);

preConfCommitmentStore.storeEncryptedCommitment(
Expand All @@ -189,9 +189,7 @@ contract TestPreConfCommitmentStore is Test {
);
}

function test_StoreCommitmentFailureDueToTimestampValidationWithNewWindow()
public
{
function test_StoreCommitmentFailureDueToTimestampValidationWithNewWindow() public {
bytes32 commitmentDigest = keccak256(
abi.encodePacked("commitment data")
);
Expand All @@ -207,7 +205,7 @@ contract TestPreConfCommitmentStore is Test {
preConfCommitmentStore.updateCommitmentDispatchWindow(200);
vm.warp(200 + _testCommitmentAliceBob.dispatchTimestamp);
vm.expectRevert(
"Invalid dispatch timestamp, block.timestamp - dispatchTimestamp < commitmentDispatchWindow"
"Invalid dispatch timestamp"
);
preConfCommitmentStore.storeEncryptedCommitment(
commitmentDigest,
Expand Down Expand Up @@ -389,21 +387,8 @@ contract TestPreConfCommitmentStore is Test {
_bytesToHexString(sharedSecretKey)
);

(
bool isUsed,
,
,
,
,
,
,
,
,
,
,
,
,
) = preConfCommitmentStore.commitments(preConfHash);
(bool isUsed, , , , , , , , , , , , , ) = preConfCommitmentStore
.commitments(preConfHash);
assertEq(isUsed, false);

return bidHash;
Expand Down Expand Up @@ -439,7 +424,6 @@ contract TestPreConfCommitmentStore is Test {
_bytesToHexString(bidSignature),
_bytesToHexString(sharedSecretKey)
);

vm.startPrank(commiter);
bytes32 commitmentIndex = preConfCommitmentStore
.storeEncryptedCommitment(
Expand Down Expand Up @@ -612,21 +596,8 @@ contract TestPreConfCommitmentStore is Test {
);

// Verify that the commitment has not been set before
(
bool isUsed,
,
,
,
,
,
,
,
,
,
,
,
,
) = preConfCommitmentStore.commitments(preConfHash);
(bool isUsed, , , , , , , , , , , , , ) = preConfCommitmentStore
.commitments(preConfHash);
assert(isUsed == false);
(address commiter, ) = makeAddrAndKey("bob");
vm.deal(commiter, 5 ether);
Expand Down Expand Up @@ -665,7 +636,7 @@ contract TestPreConfCommitmentStore is Test {
vm.prank(feeRecipient);
preConfCommitmentStore.initiateSlash(index, 100);

(isUsed, , , , , , , , , , , , ,) = preConfCommitmentStore
(isUsed, , , , , , , , , , , , , ) = preConfCommitmentStore
.commitments(index);
// Verify that the commitment has been deleted
assert(isUsed == true);
Expand Down Expand Up @@ -707,21 +678,8 @@ contract TestPreConfCommitmentStore is Test {
);

// Verify that the commitment has not been used before
(
bool isUsed,
,
,
,
,
,
,
,
,
,
,
,
,
) = preConfCommitmentStore.commitments(preConfHash);
(bool isUsed, , , , , , , , , , , , , ) = preConfCommitmentStore
.commitments(preConfHash);
assert(isUsed == false);
(address commiter, ) = makeAddrAndKey("bob");
vm.deal(commiter, 5 ether);
Expand Down Expand Up @@ -801,25 +759,12 @@ contract TestPreConfCommitmentStore is Test {
);

// Verify that the commitment has not been used before
(
bool isUsed,
,
,
,
,
,
,
,
,
,
,
,
,

) = preConfCommitmentStore.commitments(preConfHash);
(bool isUsed, , , , , , , , , , , , , ) = preConfCommitmentStore
.commitments(preConfHash);
assert(isUsed == false);
(address commiter, ) = makeAddrAndKey("bob");
vm.deal(commiter, 5 ether);

bytes32 encryptedIndex = storeCommitment(
commiter,
_testCommitmentAliceBob.bid,
Expand Down
2 changes: 1 addition & 1 deletion oracle/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func NewNode(opts *Options) (*Node, error) {
txmonitor.NewEVMHelper(settlementClient.Client()),
st,
nd.logger.With("component", "tx_monitor"),
128,
1024,
)

monitorClosed := monitor.Start(ctx)
Expand Down
Loading
Loading