Skip to content

Commit 062b970

Browse files
authored
fix: fixed system-coder tests (solana-foundation#4210)
* fix: fixed system-coder tests that depend on SYSVAR_RECENT_BLOCKHASHES_PUBKEY by waiting to the next slot * longer sleeps * attempting nonce transactions using durable nonces workflow * Fixed provider overwriting blockhash. Additional fix to nonce tests. * Preserving blockhash behaviour on default '11111111111111111111111111111111' value
1 parent 300bfd8 commit 062b970

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

tests/custom-coder/tests/system-coder.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
PublicKey,
99
SystemProgram,
1010
SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
11+
Transaction,
1112
} from "@solana/web3.js";
1213
import * as assert from "assert";
1314
import BN from "bn.js";
@@ -299,19 +300,28 @@ describe("system-coder", () => {
299300
])
300301
.signers([nonceKeypair])
301302
.rpc();
303+
304+
let nonceAccount = await program.account.nonce.fetch(
305+
nonceKeypair.publicKey
306+
);
307+
let previousNonce = nonceAccount.nonce.toString();
308+
302309
// These have to be separate to make sure advance is in another slot.
303-
await program.methods
310+
const tx = await program.methods
304311
.advanceNonceAccount(provider.wallet.publicKey)
305312
.accounts({
306313
nonce: nonceKeypair.publicKey,
307314
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
308315
})
309-
.rpc();
316+
.transaction();
317+
tx.recentBlockhash = previousNonce;
318+
tx.feePayer = provider.publicKey;
319+
320+
await provider.sendAndConfirm(tx, []);
310321
// assert
311-
const nonceAccount = await program.account.nonce.fetch(
312-
nonceKeypair.publicKey
313-
);
322+
nonceAccount = await program.account.nonce.fetch(nonceKeypair.publicKey);
314323
assert.notEqual(nonceAccount, null);
324+
assert.notEqual(nonceAccount.nonce.toString(), previousNonce);
315325
});
316326

317327
it("Authorizes a nonce account", async () => {
@@ -383,7 +393,13 @@ describe("system-coder", () => {
383393
])
384394
.signers([nonceKeypair])
385395
.rpc();
386-
await program.methods
396+
397+
let nonceAccount = await program.account.nonce.fetch(
398+
nonceKeypair.publicKey
399+
);
400+
let previousNonce = nonceAccount.nonce.toString();
401+
402+
const tx = await program.methods
387403
.advanceNonceAccount(provider.wallet.publicKey)
388404
.accounts({
389405
nonce: nonceKeypair.publicKey,
@@ -398,24 +414,47 @@ describe("system-coder", () => {
398414
})
399415
.instruction(),
400416
])
401-
.rpc();
417+
.transaction();
418+
tx.feePayer = provider.publicKey;
419+
tx.recentBlockhash = previousNonce;
420+
421+
await provider.sendAndConfirm(tx, []);
422+
423+
nonceAccount = await program.account.nonce.fetch(nonceKeypair.publicKey);
424+
previousNonce = nonceAccount.nonce.toString();
425+
402426
await program.methods
403427
.authorizeNonceAccount(aliceKeypair.publicKey)
404428
.accounts({
405429
nonce: nonceKeypair.publicKey,
406430
authorized: provider.wallet.publicKey,
407431
})
408432
.rpc();
409-
await program.methods
410-
.withdrawNonceAccount(new BN(amount))
433+
434+
let withdrawTx = await program.methods
435+
.advanceNonceAccount(aliceKeypair.publicKey)
411436
.accounts({
412-
authorized: aliceKeypair.publicKey,
413437
nonce: nonceKeypair.publicKey,
414438
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
415-
to: aliceKeypair.publicKey,
439+
authorized: aliceKeypair.publicKey,
416440
})
417-
.signers([aliceKeypair])
418-
.rpc();
441+
.postInstructions([
442+
await program.methods
443+
.withdrawNonceAccount(new BN(amount))
444+
.accounts({
445+
authorized: aliceKeypair.publicKey,
446+
nonce: nonceKeypair.publicKey,
447+
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
448+
to: aliceKeypair.publicKey,
449+
})
450+
.instruction(),
451+
])
452+
.transaction();
453+
454+
withdrawTx.feePayer = provider.publicKey;
455+
withdrawTx.recentBlockhash = previousNonce;
456+
await provider.sendAndConfirm(withdrawTx, [aliceKeypair]);
457+
419458
// assert
420459
const aliceBalanceAfter = (
421460
await program.provider.connection.getAccountInfo(aliceKeypair.publicKey)

ts/packages/anchor/src/provider.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ export class AnchorProvider implements Provider {
150150
}
151151
} else {
152152
tx.feePayer = tx.feePayer ?? this.wallet.publicKey;
153-
tx.recentBlockhash = (
154-
await this.connection.getLatestBlockhash(opts.preflightCommitment)
155-
).blockhash;
153+
154+
if (
155+
!tx.recentBlockhash ||
156+
tx.recentBlockhash === "11111111111111111111111111111111"
157+
) {
158+
tx.recentBlockhash = (
159+
await this.connection.getLatestBlockhash(opts.preflightCommitment)
160+
).blockhash;
161+
}
156162

157163
if (signers) {
158164
for (const signer of signers) {

0 commit comments

Comments
 (0)