Skip to content

Commit c3f6611

Browse files
committed
feat: keeper bot passes the period to Voting before auto-revealing
and support for the Shutter testnet API endpoint
1 parent 7bbe6df commit c3f6611

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

contracts/scripts/keeperBot.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ type CustomError = {
7171
errorSignature: string;
7272
};
7373

74+
enum Period {
75+
EVIDENCE = "evidence",
76+
COMMIT = "commit",
77+
VOTE = "vote",
78+
APPEAL = "appeal",
79+
EXECUTION = "execution",
80+
}
81+
82+
const PERIODS = Object.values(Period);
83+
7484
enum Phase {
7585
STAKING = "staking",
7686
GENERATING = "generating",
@@ -484,6 +494,10 @@ const filterDisputesToSkip = (disputes: Dispute[]) => {
484494
return disputes.filter((dispute) => !DISPUTES_TO_SKIP.includes(dispute.id));
485495
};
486496

497+
const filterDisputesByPeriod = (disputes: Dispute[], period: Period) => {
498+
return disputes.filter((dispute) => dispute.period === period);
499+
};
500+
487501
const mapAsync = <T, U>(array: T[], callbackfn: (value: T, index: number, array: T[]) => Promise<U>): Promise<U[]> => {
488502
return Promise.all(array.map(callbackfn));
489503
};
@@ -554,14 +568,6 @@ async function main() {
554568

555569
await sendHeartbeat();
556570

557-
logger.info("Auto-revealing disputes");
558-
await shutterAutoReveal(disputeKitShutter, DISPUTES_TO_SKIP);
559-
if (SHUTTER_AUTO_REVEAL_ONLY) {
560-
logger.debug("Shutter auto-reveal only, skipping other actions");
561-
await shutdown();
562-
return;
563-
}
564-
565571
logger.info(`Current phase: ${PHASES[getNumber(await sortition.phase())]}`);
566572

567573
// Retrieve the disputes which are in a non-final period
@@ -588,6 +594,22 @@ async function main() {
588594
for (const dispute of disputes) {
589595
logger.info(`Dispute #${dispute.id}, round #${dispute.currentRoundIndex}, ${dispute.period} period`);
590596
}
597+
598+
// ----------------------------------------------- //
599+
// AUTO-REVEAL //
600+
// ----------------------------------------------- //
601+
logger.info("Auto-revealing disputes");
602+
// Ensure that the disputes ready to be auto-revealed are passed to the voting period otherwise they won't be picked up.
603+
for (const dispute of filterDisputesByPeriod(filterDisputesToSkip(disputes), Period.COMMIT)) {
604+
await passPeriod(dispute);
605+
}
606+
await shutterAutoReveal(disputeKitShutter, DISPUTES_TO_SKIP);
607+
if (SHUTTER_AUTO_REVEAL_ONLY) {
608+
logger.debug("Shutter auto-reveal only, skipping other actions");
609+
await shutdown();
610+
return;
611+
}
612+
591613
logger.info(`Disputes needing more jurors: ${disputesWithoutJurors.map((dispute) => dispute.id)}`);
592614
if ((await hasMinStakingTimePassed()) && disputesWithoutJurors.length > 0) {
593615
// ----------------------------------------------- //

contracts/scripts/keeperBotShutter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ const getShutterDisputesToReveal = async (disputeKitShutter: DisputeKitShutter):
176176
};
177177
});
178178

179-
// Filter out the votes where commited is false or voted is true
180-
const filteredDisputeVotes = disputeVotes.map((item) => ({
181-
...item,
182-
votes: item.votes.filter((vote) => vote.commited && !vote.voted),
183-
}));
179+
// Filter out the disputes without votes and the votes where committed is false or voted is true
180+
const filteredDisputeVotes = disputeVotes
181+
.map((item) => ({
182+
...item,
183+
votes: item.votes.filter((vote) => vote.commited && !vote.voted),
184+
}))
185+
.filter((item) => item.votes.length > 0);
184186

185187
return filteredDisputeVotes;
186188
};

contracts/scripts/shutter.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ import { encryptData, decrypt as shutterDecrypt } from "@shutter-network/shutter
22
import { Hex, stringToHex, hexToString } from "viem";
33
import crypto from "crypto";
44
import "isomorphic-fetch";
5+
import env from "./utils/env";
56

67
// Time in seconds to wait before the message can be decrypted
78
export const DECRYPTION_DELAY = 5;
89

10+
const SHUTTER_API_URL = {
11+
mainnet: "https://shutter-api.shutter.network",
12+
testnet: "https://shutter-api.chiado.staging.shutter.network",
13+
};
14+
15+
const SHUTTER_API = env.optional("SHUTTER_API", "mainnet") as keyof typeof SHUTTER_API_URL;
16+
917
interface ShutterApiMessageData {
1018
eon: number;
1119
identity: string;
@@ -38,7 +46,7 @@ async function fetchShutterData(decryptionTimestamp: number): Promise<ShutterApi
3846
const identityPrefix = generateRandomBytes32();
3947
console.log(`Generated identity prefix: ${identityPrefix}`);
4048

41-
const response = await fetch("https://shutter-api.shutter.network/api/register_identity", {
49+
const response = await fetch(`${SHUTTER_API_URL[SHUTTER_API]}/api/register_identity`, {
4250
method: "POST",
4351
headers: {
4452
accept: "application/json",
@@ -88,7 +96,7 @@ async function fetchShutterData(decryptionTimestamp: number): Promise<ShutterApi
8896
async function fetchDecryptionKey(identity: string): Promise<ShutterDecryptionKeyData> {
8997
console.log(`Fetching decryption key for identity: ${identity}`);
9098

91-
const response = await fetch(`https://shutter-api.shutter.network/api/get_decryption_key?identity=${identity}`, {
99+
const response = await fetch(`${SHUTTER_API_URL[SHUTTER_API]}/api/get_decryption_key?identity=${identity}`, {
92100
method: "GET",
93101
headers: {
94102
accept: "application/json",
@@ -232,6 +240,7 @@ Examples:
232240
console.error("Usage: yarn ts-node shutter.ts encrypt <message>");
233241
process.exit(1);
234242
}
243+
console.log(`Using Shutter API ${SHUTTER_API_URL[SHUTTER_API]}...`);
235244
const { encryptedCommitment, identity } = await encrypt(message);
236245
console.log("\nEncrypted Commitment:", encryptedCommitment);
237246
console.log("Identity:", identity);
@@ -245,6 +254,7 @@ Examples:
245254
console.error("Note: The identity is the one returned during encryption");
246255
process.exit(1);
247256
}
257+
console.log(`Using Shutter API ${SHUTTER_API_URL[SHUTTER_API]}...`);
248258
const decryptedMessage = await decrypt(encryptedMessage, identity);
249259
console.log("\nDecrypted Message:", decryptedMessage);
250260
break;

0 commit comments

Comments
 (0)