Skip to content

Commit 2431104

Browse files
committed
feat: add LiteSVM integration and enhance anchor realloc tests
* Update anchor-realloc test descriptions for clarity * Remove unnecessary console logs from anchor-realloc tests * Introduce new LiteSVM test for realloc functionality, validating initialization and updates of message accounts
1 parent 020ebe7 commit 2431104

File tree

2 files changed

+182
-60
lines changed

2 files changed

+182
-60
lines changed
Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,72 @@
1-
import type { Program } from '@coral-xyz/anchor';
2-
import * as anchor from '@coral-xyz/anchor';
3-
import { Keypair } from '@solana/web3.js';
4-
import { assert } from 'chai';
5-
import type { AnchorRealloc } from '../target/types/anchor_realloc';
1+
import type { Program } from "@coral-xyz/anchor";
2+
import * as anchor from "@coral-xyz/anchor";
3+
import { Keypair } from "@solana/web3.js";
4+
import { assert } from "chai";
5+
import type { AnchorRealloc } from "../target/types/anchor_realloc.ts";
66

7-
describe('anchor-realloc', () => {
8-
// Configure the client to use the local cluster.
9-
const provider = anchor.AnchorProvider.env();
10-
anchor.setProvider(provider);
11-
const payer = provider.wallet as anchor.Wallet;
12-
const connection = provider.connection;
7+
describe("Anchor: realloc", () => {
8+
// Configure the client to use the local cluster.
9+
const provider = anchor.AnchorProvider.env();
10+
anchor.setProvider(provider);
11+
const payer = provider.wallet as anchor.Wallet;
12+
const connection = provider.connection;
1313

14-
const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>;
14+
const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>;
1515

16-
const messageAccount = new Keypair();
16+
const messageAccount = new Keypair();
1717

18-
// helper function to check the account data and message
19-
async function checkAccount(publicKey, expectedMessage) {
20-
const accountInfo = await connection.getAccountInfo(publicKey);
21-
const accountData = await program.account.message.fetch(publicKey);
18+
// helper function to check the account data and message
19+
async function checkAccount(publicKey, expectedMessage) {
20+
const accountInfo = await connection.getAccountInfo(publicKey);
21+
const accountData = await program.account.message.fetch(publicKey);
2222

23-
// 8 bytes for the discriminator,
24-
// 4 bytes for the length of the message,
25-
// and the length of the message
26-
assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length);
27-
assert.equal(accountData.message, expectedMessage);
23+
// 8 bytes for the discriminator,
24+
// 4 bytes for the length of the message,
25+
// and the length of the message
26+
assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length);
27+
assert.equal(accountData.message, expectedMessage);
28+
}
2829

29-
console.log(`Account Data Length: ${accountInfo.data.length}`);
30-
console.log(`Message: ${accountData.message}`);
31-
}
30+
it("Is initialized!", async () => {
31+
const input = "hello";
3232

33-
it('Is initialized!', async () => {
34-
const input = 'hello';
33+
await program.methods
34+
.initialize(input)
35+
.accounts({
36+
payer: payer.publicKey,
37+
messageAccount: messageAccount.publicKey,
38+
})
39+
.signers([messageAccount])
40+
.rpc();
3541

36-
await program.methods
37-
.initialize(input)
38-
.accounts({
39-
payer: payer.publicKey,
40-
messageAccount: messageAccount.publicKey,
41-
})
42-
.signers([messageAccount])
43-
.rpc();
42+
await checkAccount(messageAccount.publicKey, input);
43+
});
4444

45-
await checkAccount(messageAccount.publicKey, input);
46-
});
45+
it("Update", async () => {
46+
const input = "hello world";
4747

48-
it('Update', async () => {
49-
const input = 'hello world';
48+
await program.methods
49+
.update(input)
50+
.accounts({
51+
payer: payer.publicKey,
52+
messageAccount: messageAccount.publicKey,
53+
})
54+
.rpc();
5055

51-
await program.methods
52-
.update(input)
53-
.accounts({
54-
payer: payer.publicKey,
55-
messageAccount: messageAccount.publicKey,
56-
})
57-
.rpc();
56+
await checkAccount(messageAccount.publicKey, input);
57+
});
5858

59-
await checkAccount(messageAccount.publicKey, input);
60-
});
59+
it("Again update", async () => {
60+
const input = "hi";
6161

62-
it('Update', async () => {
63-
const input = 'hi';
62+
await program.methods
63+
.update(input)
64+
.accounts({
65+
payer: payer.publicKey,
66+
messageAccount: messageAccount.publicKey,
67+
})
68+
.rpc();
6469

65-
await program.methods
66-
.update(input)
67-
.accounts({
68-
payer: payer.publicKey,
69-
messageAccount: messageAccount.publicKey,
70-
})
71-
.rpc();
72-
73-
await checkAccount(messageAccount.publicKey, input);
74-
});
70+
await checkAccount(messageAccount.publicKey, input);
71+
});
7572
});
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import anchor from "@coral-xyz/anchor";
2+
import {
3+
Keypair,
4+
PublicKey,
5+
SystemProgram,
6+
Transaction,
7+
TransactionInstruction,
8+
} from "@solana/web3.js";
9+
import { assert } from "chai";
10+
import { LiteSVM } from "litesvm";
11+
import IDL from "../target/idl/anchor_realloc.json" with { type: "json" };
12+
13+
describe("LiteSVM: realloc", () => {
14+
const svm = new LiteSVM();
15+
const programId = new PublicKey(IDL.address);
16+
const coder = new anchor.BorshCoder(IDL as anchor.Idl);
17+
18+
const payer = Keypair.generate();
19+
svm.airdrop(payer.publicKey, BigInt(1000000000));
20+
21+
const programPath = new URL(
22+
"../target/deploy/anchor_realloc.so",
23+
import.meta.url,
24+
).pathname;
25+
svm.addProgramFromFile(programId, programPath);
26+
27+
// PDA for the message account
28+
const messageAccount = new Keypair();
29+
30+
it("Is initialized!", () => {
31+
const message = "hello";
32+
const data = coder.instruction.encode("initialize", {
33+
input: message,
34+
});
35+
const ix = new TransactionInstruction({
36+
keys: [
37+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
38+
{ pubkey: messageAccount.publicKey, isSigner: true, isWritable: true },
39+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
40+
],
41+
programId,
42+
data,
43+
});
44+
45+
const tx = new Transaction().add(ix);
46+
tx.feePayer = payer.publicKey;
47+
tx.recentBlockhash = svm.latestBlockhash();
48+
tx.sign(payer, messageAccount);
49+
svm.sendTransaction(tx);
50+
51+
//Fetch the message account and check it message
52+
const messageAccInfo = svm.getAccount(messageAccount.publicKey);
53+
const messageAcc = coder.accounts.decode(
54+
"Message",
55+
Buffer.from(messageAccInfo.data),
56+
);
57+
assert.equal(messageAccInfo.data.length, 8 + 4 + message.length);
58+
assert.equal(messageAcc.message, message);
59+
});
60+
61+
it("Update", () => {
62+
const message = "hello world";
63+
const data = coder.instruction.encode("update", {
64+
input: message,
65+
});
66+
const ix = new TransactionInstruction({
67+
keys: [
68+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
69+
{ pubkey: messageAccount.publicKey, isSigner: false, isWritable: true },
70+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
71+
],
72+
programId,
73+
data,
74+
});
75+
76+
const tx = new Transaction().add(ix);
77+
tx.feePayer = payer.publicKey;
78+
tx.recentBlockhash = svm.latestBlockhash();
79+
tx.sign(payer);
80+
svm.sendTransaction(tx);
81+
svm.expireBlockhash();
82+
83+
//Fetch the message account and check it message
84+
const messageAccInfo = svm.getAccount(messageAccount.publicKey);
85+
const messageAcc = coder.accounts.decode(
86+
"Message",
87+
Buffer.from(messageAccInfo.data),
88+
);
89+
assert.equal(messageAccInfo.data.length, 8 + 4 + message.length);
90+
assert.equal(messageAcc.message, message);
91+
});
92+
93+
it("Again update", () => {
94+
const message = "hi";
95+
const data = coder.instruction.encode("update", {
96+
input: message,
97+
});
98+
const ix = new TransactionInstruction({
99+
keys: [
100+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
101+
{ pubkey: messageAccount.publicKey, isSigner: false, isWritable: true },
102+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
103+
],
104+
programId,
105+
data,
106+
});
107+
108+
const tx = new Transaction().add(ix);
109+
tx.feePayer = payer.publicKey;
110+
tx.recentBlockhash = svm.latestBlockhash();
111+
tx.sign(payer);
112+
svm.sendTransaction(tx);
113+
svm.expireBlockhash();
114+
115+
//Fetch the message account and check it message
116+
const messageAccInfo = svm.getAccount(messageAccount.publicKey);
117+
const messageAcc = coder.accounts.decode(
118+
"Message",
119+
Buffer.from(messageAccInfo.data),
120+
);
121+
122+
assert.equal(messageAccInfo.data.length, 8 + 4 + message.length);
123+
assert.equal(messageAcc.message, message);
124+
});
125+
});

0 commit comments

Comments
 (0)