Skip to content

Commit 1339235

Browse files
committed
update submit function, fn names and update OptimisticCommitment
1 parent 5d97c3b commit 1339235

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

protocol-units/dispute/src/RStarM.sol

+21-23
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
6666

6767
struct OptimisticCommitment {
6868
bytes32 blockHash;
69-
bytes stateCommitment; // The state commitment associated with the block
70-
uint256 validatorCount; // Number of validators who have submitted this commitment
69+
mapping(bytes => uint256) stateCommitments;
70+
bytes highestCommitState;
71+
uint256 highestCommitCount;
7172
}
7273

7374
struct Dispute {
@@ -125,7 +126,7 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
125126
event DisputeResolved(bytes32 indexed disputeHash, DisputeState state);
126127
event ProofSubmitted(bytes32 indexed blockHash, bool isValid);
127128
event ProofVerified(bytes32 indexed blockHash, bool isValid);
128-
event BlockAccepted(bytes32 indexed blockHash);
129+
event BlockAccepted(bytes32 indexed blockHash, bytes stateCommitment);
129130
event OptimisticCommitmentSubmitted(bytes32 indexed blockHash, bytes stateCommitment, uint256 validatorCount);
130131

131132
constructor(
@@ -153,7 +154,7 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
153154
return (uint256(uint128(uint256(reversed))), uint256(reversed >> 128));
154155
}
155156

156-
function registerValidator() external payable {
157+
function stake() external payable {
157158
require(msg.value >= MIN_STAKE, "Insufficient stake");
158159
require(!validators[msg.sender].isRegistered, "Validator already registered");
159160
validators[msg.sender] = Validator(true, msg.value);
@@ -164,14 +165,14 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
164165
return (validators[validator].isRegistered, validators[validator].stake);
165166
}
166167

167-
function deregisterValidator() external {
168+
function unstake() external {
168169
Validator storage validator = validators[msg.sender];
169170
require(validator.isRegistered, "Validator not registered");
170171
require(validator.stake > 0, "No stake to withdraw");
171-
uint256 stake = validator.stake;
172+
uint256 validatorStake = validator.stake;
172173
validator.isRegistered = false;
173174
validator.stake = 0;
174-
payable(msg.sender).transfer(stake);
175+
payable(msg.sender).transfer(validatorStake);
175176
emit ValidatorDeregistered(msg.sender);
176177
}
177178

@@ -228,32 +229,29 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
228229
// The camel case here is not standard solidity practice. But we use it because its the implemntation of the interface.
229230
function verify_integrity(Receipt memory receipt) public view returns (bool) {
230231
(uint256 claim0, uint256 claim1) = splitDigest(receipt.claim.digest());
231-
console2.log("claim0: ", claim0);
232-
console2.log("claim1: ", claim1);
233232
Seal memory seal = abi.decode(receipt.seal, (Seal));
234-
console2.log("sealed");
235233
bool is_verified = this.verifyProof(seal.a, seal.b, seal.c, [CONTROL_ID_0, CONTROL_ID_1, claim0, claim1, BN254_CONTROL_ID]);
236-
console2.log("verified", is_verified);
237234
return is_verified;
238235
}
239236

240237
function submitOptimisticCommitment(bytes32 blockHash, bytes calldata stateCommitment) external {
241238
require(validators[msg.sender].isRegistered, "Validator not registered");
242239
OptimisticCommitment storage commitment = optimisticCommitments[blockHash];
243-
if (commitment.validatorCount == 0) {
244-
commitment.blockHash = blockHash;
245-
commitment.stateCommitment = stateCommitment;
246-
commitment.validatorCount = 1;
247-
} else {
248-
require(keccak256(commitment.stateCommitment) == keccak256(stateCommitment), "State commitment mismatch");
249-
commitment.validatorCount += 1;
240+
241+
// Increment the count for the submitted stateCommitment
242+
uint256 currentCount = ++commitment.stateCommitments[stateCommitment];
243+
244+
// Update the highest commit count and state if the current count is higher
245+
if (currentCount > commitment.highestCommitCount) {
246+
commitment.highestCommitCount = currentCount;
247+
commitment.highestCommitState = stateCommitment;
250248
}
251-
252-
emit OptimisticCommitmentSubmitted(blockHash, stateCommitment, commitment.validatorCount);
253-
254-
if (commitment.validatorCount >= m) {
249+
250+
emit OptimisticCommitmentSubmitted(blockHash, stateCommitment, currentCount);
251+
252+
if (commitment.highestCommitCount >= m) {
255253
// Block is accepted optimistically after receiving minimum number of commitments
256-
emit BlockAccepted(blockHash);
254+
emit BlockAccepted(blockHash, commitment.highestCommitState);
257255
}
258256
}
259257
}

protocol-units/dispute/test/RStarM.t.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ contract RStartM is DSTest {
5555
uint256 minStake = rStarM.MIN_STAKE();
5656

5757
vm.prank(signer1);
58-
rStarM.registerValidator{value: minStake}();
58+
rStarM.stake{value: minStake}();
5959

6060
(bool isRegistered, uint256 stake) = rStarM.validators(signer1);
6161
assertTrue(isRegistered, "Validator should be registered");

0 commit comments

Comments
 (0)