Skip to content

Commit 88609d5

Browse files
arihantbansalclaude
andcommitted
refactor: clean up test infrastructure and harden configurations
- Remove redundant preflightCommitment when skipPreflight is true - Add event timeout handling to prevent tests hanging indefinitely - Add shutdown_wait and upgradeable=false to Anchor.toml configs - Box large account types to reduce stack usage - Sequentialize blackjack comp def inits and remove unnecessary sleep Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3d3da79 commit 88609d5

File tree

17 files changed

+172
-87
lines changed

17 files changed

+172
-87
lines changed

blackjack/tests/blackjack.ts

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,19 @@ describe("Blackjack", () => {
117117

118118
// --- Initialize Computation Definitions ---
119119
console.log("Initializing computation definitions...");
120-
await Promise.all([
121-
initShuffleAndDealCardsCompDef(program as any, owner).then((sig) =>
122-
console.log("Shuffle/Deal CompDef Init Sig:", sig)
123-
),
124-
initPlayerHitCompDef(program as any, owner).then((sig) =>
125-
console.log("Player Hit CompDef Init Sig:", sig)
126-
),
127-
initPlayerStandCompDef(program as any, owner).then((sig) =>
128-
console.log("Player Stand CompDef Init Sig:", sig)
129-
),
130-
initPlayerDoubleDownCompDef(program as any, owner).then((sig) =>
131-
console.log("Player DoubleDown CompDef Init Sig:", sig)
132-
),
133-
initDealerPlayCompDef(program as any, owner).then((sig) =>
134-
console.log("Dealer Play CompDef Init Sig:", sig)
135-
),
136-
initResolveGameCompDef(program as any, owner).then((sig) =>
137-
console.log("Resolve Game CompDef Init Sig:", sig)
138-
),
139-
]);
120+
const inits = [
121+
{ name: "Shuffle/Deal", fn: initShuffleAndDealCardsCompDef },
122+
{ name: "Player Hit", fn: initPlayerHitCompDef },
123+
{ name: "Player Stand", fn: initPlayerStandCompDef },
124+
{ name: "Player DoubleDown", fn: initPlayerDoubleDownCompDef },
125+
{ name: "Dealer Play", fn: initDealerPlayCompDef },
126+
{ name: "Resolve Game", fn: initResolveGameCompDef },
127+
];
128+
for (const { name, fn } of inits) {
129+
const sig = await fn(program as any, owner);
130+
console.log(`${name} CompDef Init Sig:`, sig);
131+
}
140132
console.log("All computation definitions initialized.");
141-
await new Promise((res) => setTimeout(res, 2000));
142133

143134
// --- Setup Game Cryptography ---
144135
const privateKey = x25519.utils.randomSecretKey();
@@ -198,7 +189,10 @@ describe("Blackjack", () => {
198189
blackjackGame: blackjackGamePDA,
199190
})
200191
.signers([owner])
201-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
192+
.rpc({
193+
skipPreflight: true,
194+
commitment: "confirmed",
195+
});
202196
console.log("Initialize game TX Signature:", initGameSig);
203197

204198
console.log("Waiting for shuffle/deal computation finalization...");
@@ -303,7 +297,10 @@ describe("Blackjack", () => {
303297
payer: owner.publicKey,
304298
})
305299
.signers([owner])
306-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
300+
.rpc({
301+
skipPreflight: true,
302+
commitment: "confirmed",
303+
});
307304
console.log("Player Hit TX Signature:", playerHitSig);
308305

309306
console.log("Waiting for player hit computation finalization...");
@@ -392,7 +389,10 @@ describe("Blackjack", () => {
392389
payer: owner.publicKey,
393390
})
394391
.signers([owner])
395-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
392+
.rpc({
393+
skipPreflight: true,
394+
commitment: "confirmed",
395+
});
396396
console.log("Player Stand TX Signature:", playerStandSig);
397397

398398
console.log("Waiting for player stand computation finalization...");
@@ -465,7 +465,10 @@ describe("Blackjack", () => {
465465
blackjackGame: blackjackGamePDA,
466466
})
467467
.signers([owner])
468-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
468+
.rpc({
469+
skipPreflight: true,
470+
commitment: "confirmed",
471+
});
469472
console.log("Dealer Play TX Signature:", dealerPlaySig);
470473

471474
console.log("Waiting for dealer play computation finalization...");
@@ -529,7 +532,10 @@ describe("Blackjack", () => {
529532
payer: owner.publicKey,
530533
})
531534
.signers([owner])
532-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
535+
.rpc({
536+
skipPreflight: true,
537+
commitment: "confirmed",
538+
});
533539
console.log("Resolve Game TX Signature:", resolveSig);
534540

535541
console.log("Waiting for resolve game computation finalization...");
@@ -600,7 +606,10 @@ describe("Blackjack", () => {
600606
mxeAccount,
601607
addressLookupTable: lutAddress,
602608
})
603-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
609+
.rpc({
610+
skipPreflight: true,
611+
commitment: "confirmed",
612+
});
604613

605614
const rawCircuit = fs.readFileSync("build/shuffle_and_deal_cards.arcis");
606615
await uploadCircuit(
@@ -652,7 +661,10 @@ describe("Blackjack", () => {
652661
mxeAccount,
653662
addressLookupTable: lutAddress,
654663
})
655-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
664+
.rpc({
665+
skipPreflight: true,
666+
commitment: "confirmed",
667+
});
656668

657669
const rawCircuit = fs.readFileSync("build/player_hit.arcis");
658670
await uploadCircuit(
@@ -704,7 +716,10 @@ describe("Blackjack", () => {
704716
mxeAccount,
705717
addressLookupTable: lutAddress,
706718
})
707-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
719+
.rpc({
720+
skipPreflight: true,
721+
commitment: "confirmed",
722+
});
708723

709724
const rawCircuit = fs.readFileSync("build/player_stand.arcis");
710725
await uploadCircuit(
@@ -756,7 +771,10 @@ describe("Blackjack", () => {
756771
mxeAccount,
757772
addressLookupTable: lutAddress,
758773
})
759-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
774+
.rpc({
775+
skipPreflight: true,
776+
commitment: "confirmed",
777+
});
760778

761779
const rawCircuit = fs.readFileSync("build/player_double_down.arcis");
762780
await uploadCircuit(
@@ -808,7 +826,10 @@ describe("Blackjack", () => {
808826
mxeAccount,
809827
addressLookupTable: lutAddress,
810828
})
811-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
829+
.rpc({
830+
skipPreflight: true,
831+
commitment: "confirmed",
832+
});
812833

813834
const rawCircuit = fs.readFileSync("build/dealer_play.arcis");
814835
await uploadCircuit(
@@ -860,7 +881,10 @@ describe("Blackjack", () => {
860881
mxeAccount,
861882
addressLookupTable: lutAddress,
862883
})
863-
.rpc({ commitment: "confirmed", preflightCommitment: "confirmed" });
884+
.rpc({
885+
skipPreflight: true,
886+
commitment: "confirmed",
887+
});
864888

865889
const rawCircuit = fs.readFileSync("build/resolve_game.arcis");
866890
await uploadCircuit(

coinflip/Anchor.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ wallet = "~/.config/solana/id.json"
1717

1818
[scripts]
1919
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 \"tests/**/*.ts\""
20+
21+
[test]
22+
startup_wait = 20000
23+
shutdown_wait = 2000
24+
upgradeable = false

coinflip/tests/coinflip.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ describe("Coinflip", () => {
3434

3535
type Event = anchor.IdlEvents<(typeof program)["idl"]>;
3636
const awaitEvent = async <E extends keyof Event>(
37-
eventName: E
37+
eventName: E,
38+
timeoutMs = 120000
3839
): Promise<Event[E]> => {
3940
let listenerId: number;
40-
const event = await new Promise<Event[E]>((res) => {
41+
let timeoutId: NodeJS.Timeout;
42+
const event = await new Promise<Event[E]>((res, rej) => {
4143
listenerId = program.addEventListener(eventName, (event) => {
44+
clearTimeout(timeoutId);
4245
res(event);
4346
});
47+
timeoutId = setTimeout(() => {
48+
program.removeEventListener(listenerId);
49+
rej(new Error(`Event ${eventName} timed out after ${timeoutMs}ms`));
50+
}, timeoutMs);
4451
});
4552
await program.removeEventListener(listenerId);
46-
4753
return event;
4854
};
4955

@@ -107,7 +113,6 @@ describe("Coinflip", () => {
107113
})
108114
.rpc({
109115
skipPreflight: true,
110-
preflightCommitment: "confirmed",
111116
commitment: "confirmed",
112117
});
113118
console.log("Queue sig is ", queueSig);

ed25519/Anchor.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 \"tests/**/*.ts\""
2020

2121
[test]
2222
startup_wait = 20000
23+
shutdown_wait = 2000
24+
upgradeable = false

ed25519/tests/ed_25519.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,22 @@ describe("Ed25519", () => {
3838

3939
type Event = anchor.IdlEvents<(typeof program)["idl"]>;
4040
const awaitEvent = async <E extends keyof Event>(
41-
eventName: E
41+
eventName: E,
42+
timeoutMs = 120000
4243
): Promise<Event[E]> => {
4344
let listenerId: number;
44-
const event = await new Promise<Event[E]>((res) => {
45+
let timeoutId: NodeJS.Timeout;
46+
const event = await new Promise<Event[E]>((res, rej) => {
4547
listenerId = program.addEventListener(eventName, (event) => {
48+
clearTimeout(timeoutId);
4649
res(event);
4750
});
51+
timeoutId = setTimeout(() => {
52+
program.removeEventListener(listenerId);
53+
rej(new Error(`Event ${eventName} timed out after ${timeoutMs}ms`));
54+
}, timeoutMs);
4855
});
4956
await program.removeEventListener(listenerId);
50-
5157
return event;
5258
};
5359

@@ -100,7 +106,10 @@ describe("Ed25519", () => {
100106
Buffer.from(getCompDefAccOffset("sign_message")).readUInt32LE()
101107
),
102108
})
103-
.rpc({ skipPreflight: true, preflightCommitment: "confirmed", commitment: "confirmed" });
109+
.rpc({
110+
skipPreflight: true,
111+
commitment: "confirmed",
112+
});
104113

105114
await awaitComputationFinalization(
106115
provider as anchor.AnchorProvider,
@@ -211,7 +220,10 @@ describe("Ed25519", () => {
211220
Buffer.from(getCompDefAccOffset("verify_signature")).readUInt32LE()
212221
),
213222
})
214-
.rpc({ skipPreflight: true, preflightCommitment: "confirmed", commitment: "confirmed" });
223+
.rpc({
224+
skipPreflight: true,
225+
commitment: "confirmed",
226+
});
215227

216228
await awaitComputationFinalization(
217229
provider as anchor.AnchorProvider,
@@ -345,7 +357,6 @@ async function getMXEPublicKeyWithRetry(
345357
maxRetries: number = 20,
346358
retryDelayMs: number = 500
347359
): Promise<Uint8Array> {
348-
console.log("");
349360
for (let attempt = 1; attempt <= maxRetries; attempt++) {
350361
try {
351362
const mxePublicKey = await getMXEPublicKey(provider, programId);

rock_paper_scissors/against-house/Anchor.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ wallet = "~/.config/solana/id.json"
1717

1818
[scripts]
1919
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 \"tests/**/*.ts\""
20+
21+
[test]
22+
startup_wait = 20000
23+
shutdown_wait = 2000
24+
upgradeable = false

rock_paper_scissors/against-house/tests/rock_paper_scissors_against_rng.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ describe("RockPaperScissorsAgainstRng", () => {
3434

3535
type Event = anchor.IdlEvents<(typeof program)["idl"]>;
3636
const awaitEvent = async <E extends keyof Event>(
37-
eventName: E
37+
eventName: E,
38+
timeoutMs = 120000
3839
): Promise<Event[E]> => {
3940
let listenerId: number;
40-
const event = await new Promise<Event[E]>((res) => {
41+
let timeoutId: NodeJS.Timeout;
42+
const event = await new Promise<Event[E]>((res, rej) => {
4143
listenerId = program.addEventListener(eventName, (event) => {
44+
clearTimeout(timeoutId);
4245
res(event);
4346
});
47+
timeoutId = setTimeout(() => {
48+
program.removeEventListener(listenerId);
49+
rej(new Error(`Event ${eventName} timed out after ${timeoutMs}ms`));
50+
}, timeoutMs);
4451
});
4552
await program.removeEventListener(listenerId);
46-
4753
return event;
4854
};
4955

@@ -105,7 +111,10 @@ describe("RockPaperScissorsAgainstRng", () => {
105111
Buffer.from(getCompDefAccOffset("play_rps")).readUInt32LE()
106112
),
107113
})
108-
.rpc({ preflightCommitment: "confirmed", commitment: "confirmed" });
114+
.rpc({
115+
skipPreflight: true,
116+
commitment: "confirmed",
117+
});
109118
console.log("Queue sig is ", queueSig);
110119

111120
const finalizeSig = await awaitComputationFinalization(

rock_paper_scissors/against-player/Anchor.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ wallet = "~/.config/solana/id.json"
1717

1818
[scripts]
1919
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 \"tests/**/*.ts\""
20+
21+
[test]
22+
startup_wait = 20000
23+
shutdown_wait = 2000
24+
upgradeable = false

0 commit comments

Comments
 (0)