Skip to content

Commit 3c54701

Browse files
committed
fix: more realistic autovote
1 parent 7fd5997 commit 3c54701

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

contracts/scripts/shutterAutoVote.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createPublicClient, createWalletClient, http, Hex, getContract } from "viem";
1+
import { createPublicClient, createWalletClient, http, Hex, getContract, Address, decodeEventLog } from "viem";
22
import { mnemonicToAccount } from "viem/accounts";
33
import { hardhat } from "viem/chains";
44
import { encrypt, decrypt, DECRYPTION_DELAY } from "./shutter";
@@ -11,11 +11,10 @@ const SEPARATOR = "␟"; // U+241F
1111
// Store encrypted votes for later decryption
1212
type EncryptedVote = {
1313
coreDisputeID: bigint;
14-
encryptedCommitment: string;
14+
juror: Address;
1515
identity: Hex;
16+
encryptedVote: string;
1617
timestamp: number;
17-
voteIDs: bigint[];
18-
salt: Hex;
1918
};
2019

2120
const encryptedVotes: EncryptedVote[] = [];
@@ -43,6 +42,15 @@ const disputeKit = getContract({
4342
client: { public: publicClient, wallet: walletClient },
4443
});
4544

45+
type CommitCastEventArgs = {
46+
_coreDisputeID: bigint;
47+
_juror: Address;
48+
_voteIDs: bigint[];
49+
_commit: Hex;
50+
_identity: Hex;
51+
_encryptedVote: string;
52+
};
53+
4654
/**
4755
* Generate a random salt
4856
*/
@@ -97,13 +105,19 @@ async function castCommit({
97105
});
98106

99107
// Encrypt the message using shutter.ts
100-
const { encryptedCommitment, identity } = await encrypt(message);
108+
const { encryptedCommitment: encryptedVote, identity } = await encrypt(message);
101109

102110
// Compute hash using all vote IDs
103111
const commitHash = await disputeKit.read.hashVote([choice, salt, justification]);
104112

105113
// Cast the commit on-chain
106-
const txHash = await disputeKit.write.castCommit([coreDisputeID, voteIDs, commitHash, identity as Hex]);
114+
const txHash = await disputeKit.write.castCommit([
115+
coreDisputeID,
116+
voteIDs,
117+
commitHash,
118+
identity as Hex,
119+
encryptedVote,
120+
]);
107121

108122
// Wait for transaction to be mined
109123
await publicClient.waitForTransactionReceipt({ hash: txHash });
@@ -115,11 +129,10 @@ async function castCommit({
115129
// Store encrypted vote for later decryption
116130
encryptedVotes.push({
117131
coreDisputeID,
118-
encryptedCommitment,
132+
juror: account.address,
119133
identity: identity as Hex,
134+
encryptedVote,
120135
timestamp: Math.floor(Date.now() / 1000),
121-
voteIDs,
122-
salt,
123136
});
124137

125138
return { commitHash, identity, salt };
@@ -143,16 +156,34 @@ export async function autoVote() {
143156

144157
for (const vote of readyVotes) {
145158
try {
159+
// Retrieve the CommitCast event
160+
const filter = await publicClient.createContractEventFilter({
161+
abi: DisputeKitShutterPoCAbi,
162+
eventName: "CommitCast",
163+
args: [vote.coreDisputeID, vote.juror],
164+
});
165+
let events = await publicClient.getLogs(filter);
166+
if (events.length !== 1) {
167+
throw new Error("No CommitCast event found");
168+
}
169+
const { args } = decodeEventLog({
170+
abi: DisputeKitShutterPoCAbi,
171+
eventName: "CommitCast",
172+
topics: events[0].topics,
173+
data: events[0].data,
174+
});
175+
const commitCast = args as unknown as CommitCastEventArgs; // Workaround getLogs type inference issue
176+
146177
// Attempt to decrypt the vote
147-
const decryptedMessage = await decrypt(vote.encryptedCommitment, vote.identity);
178+
const decryptedMessage = await decrypt(commitCast._encryptedVote, commitCast._identity);
148179

149180
// Decode the decrypted message
150181
const { choice, salt, justification } = decode(decryptedMessage);
151182

152183
// Cast the vote on-chain
153184
const txHash = await disputeKit.write.castVote([
154185
vote.coreDisputeID,
155-
vote.voteIDs,
186+
commitCast._voteIDs,
156187
choice,
157188
salt,
158189
justification,
@@ -162,14 +193,14 @@ export async function autoVote() {
162193
await publicClient.waitForTransactionReceipt({ hash: txHash });
163194

164195
// Watch for VoteCast event
165-
const events = await disputeKit.getEvents.VoteCast();
196+
events = await disputeKit.getEvents.VoteCast();
166197
console.log("VoteCast event:", (events[0] as any).args);
167198

168199
// Remove the processed vote
169200
const index = encryptedVotes.indexOf(vote);
170201
if (index > -1) encryptedVotes.splice(index, 1);
171202
} catch (error) {
172-
console.error(`Error processing vote ${vote.voteIDs.join(",")}:`, error);
203+
console.error(`Error processing vote for identity ${vote.identity}:`, error);
173204
}
174205
}
175206

contracts/src/arbitration/dispute-kits/DisputeKitShutterPoC.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ contract DisputeKitShutterPoC {
2323
address indexed _juror,
2424
uint256[] _voteIDs,
2525
bytes32 _commit,
26-
bytes32 _identity
26+
bytes32 _identity,
27+
bytes _encryptedVote
2728
);
2829

2930
event VoteCast(
@@ -57,7 +58,8 @@ contract DisputeKitShutterPoC {
5758
uint256 _coreDisputeID,
5859
uint256[] calldata _voteIDs,
5960
bytes32 _commit,
60-
bytes32 _identity
61+
bytes32 _identity,
62+
bytes calldata _encryptedVote
6163
) external {
6264
// Store the commitment hash for each voteID
6365
for (uint256 i = 0; i < _voteIDs.length; i++) {
@@ -66,7 +68,7 @@ contract DisputeKitShutterPoC {
6668
}
6769

6870
totalCommitted += _voteIDs.length;
69-
emit CommitCast(_coreDisputeID, msg.sender, _voteIDs, _commit, _identity);
71+
emit CommitCast(_coreDisputeID, msg.sender, _voteIDs, _commit, _identity, _encryptedVote);
7072
}
7173

7274
function castVote(

0 commit comments

Comments
 (0)