@@ -66,8 +66,9 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
66
66
67
67
struct OptimisticCommitment {
68
68
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;
71
72
}
72
73
73
74
struct Dispute {
@@ -125,7 +126,7 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
125
126
event DisputeResolved (bytes32 indexed disputeHash , DisputeState state );
126
127
event ProofSubmitted (bytes32 indexed blockHash , bool isValid );
127
128
event ProofVerified (bytes32 indexed blockHash , bool isValid );
128
- event BlockAccepted (bytes32 indexed blockHash );
129
+ event BlockAccepted (bytes32 indexed blockHash , bytes stateCommitment );
129
130
event OptimisticCommitmentSubmitted (bytes32 indexed blockHash , bytes stateCommitment , uint256 validatorCount );
130
131
131
132
constructor (
@@ -153,7 +154,7 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
153
154
return (uint256 (uint128 (uint256 (reversed))), uint256 (reversed >> 128 ));
154
155
}
155
156
156
- function registerValidator () external payable {
157
+ function stake () external payable {
157
158
require (msg .value >= MIN_STAKE, "Insufficient stake " );
158
159
require (! validators[msg .sender ].isRegistered, "Validator already registered " );
159
160
validators[msg .sender ] = Validator (true , msg .value );
@@ -164,14 +165,14 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
164
165
return (validators[validator].isRegistered, validators[validator].stake);
165
166
}
166
167
167
- function deregisterValidator () external {
168
+ function unstake () external {
168
169
Validator storage validator = validators[msg .sender ];
169
170
require (validator.isRegistered, "Validator not registered " );
170
171
require (validator.stake > 0 , "No stake to withdraw " );
171
- uint256 stake = validator.stake;
172
+ uint256 validatorStake = validator.stake;
172
173
validator.isRegistered = false ;
173
174
validator.stake = 0 ;
174
- payable (msg .sender ).transfer (stake );
175
+ payable (msg .sender ).transfer (validatorStake );
175
176
emit ValidatorDeregistered (msg .sender );
176
177
}
177
178
@@ -228,32 +229,29 @@ contract RStarM is IRiscZeroVerifier, Groth16Verifier {
228
229
// The camel case here is not standard solidity practice. But we use it because its the implemntation of the interface.
229
230
function verify_integrity (Receipt memory receipt ) public view returns (bool ) {
230
231
(uint256 claim0 , uint256 claim1 ) = splitDigest (receipt.claim.digest ());
231
- console2.log ("claim0: " , claim0);
232
- console2.log ("claim1: " , claim1);
233
232
Seal memory seal = abi.decode (receipt.seal, (Seal));
234
- console2.log ("sealed " );
235
233
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);
237
234
return is_verified;
238
235
}
239
236
240
237
function submitOptimisticCommitment (bytes32 blockHash , bytes calldata stateCommitment ) external {
241
238
require (validators[msg .sender ].isRegistered, "Validator not registered " );
242
239
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;
250
248
}
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) {
255
253
// Block is accepted optimistically after receiving minimum number of commitments
256
- emit BlockAccepted (blockHash);
254
+ emit BlockAccepted (blockHash, commitment.highestCommitState );
257
255
}
258
256
}
259
257
}
0 commit comments