|
@@ -1,56 +1,17 @@
|
|
|
-const anchor = require("@project-serum/anchor");
|
|
|
-const PublicKey = anchor.web3.PublicKey;
|
|
|
-const BN = anchor.BN;
|
|
|
-const assert = require("assert");
|
|
|
+const anchor = require('@project-serum/anchor')
|
|
|
+const PublicKey = anchor.web3.PublicKey
|
|
|
+const BN = anchor.BN
|
|
|
+const assert = require('assert')
|
|
|
|
|
|
-describe("zero-copy", () => {
|
|
|
+describe('zero-copy', () => {
|
|
|
// Configure the client to use the local cluster.
|
|
|
- anchor.setProvider(anchor.Provider.env());
|
|
|
+ anchor.setProvider(anchor.Provider.env())
|
|
|
|
|
|
- const program = anchor.workspace.ZeroCopy;
|
|
|
+ const program = anchor.workspace.ZeroCopy
|
|
|
+ const programCpi = anchor.workspace.ZeroCpi
|
|
|
|
|
|
- const foo = anchor.web3.Keypair.generate();
|
|
|
-
|
|
|
- it("Creates zero copy state", async () => {
|
|
|
- await program.state.rpc.new({
|
|
|
- accounts: {
|
|
|
- authority: program.provider.wallet.publicKey,
|
|
|
- },
|
|
|
- });
|
|
|
- const state = await program.state.fetch();
|
|
|
- assert.ok(state.authority.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(state.events.length === 250);
|
|
|
- state.events.forEach((event, idx) => {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- it("Updates zero copy state", async () => {
|
|
|
- let event = {
|
|
|
- from: PublicKey.default,
|
|
|
- data: new BN(1234),
|
|
|
- };
|
|
|
- await program.state.rpc.setEvent(5, event, {
|
|
|
- accounts: {
|
|
|
- authority: program.provider.wallet.publicKey,
|
|
|
- },
|
|
|
- });
|
|
|
- const state = await program.state.fetch();
|
|
|
- assert.ok(state.authority.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(state.events.length === 250);
|
|
|
- state.events.forEach((event, idx) => {
|
|
|
- if (idx === 5) {
|
|
|
- assert.ok(event.from.equals(event.from));
|
|
|
- assert.ok(event.data.eq(event.data));
|
|
|
- } else {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- it("Is creates a zero copy account", async () => {
|
|
|
+ const foo = anchor.web3.Keypair.generate()
|
|
|
+ it('Is creates a zero copy account', async () => {
|
|
|
await program.rpc.createFoo({
|
|
|
accounts: {
|
|
|
foo: foo.publicKey,
|
|
@@ -59,73 +20,70 @@ describe("zero-copy", () => {
|
|
|
},
|
|
|
instructions: [await program.account.foo.createInstruction(foo)],
|
|
|
signers: [foo],
|
|
|
- });
|
|
|
- const account = await program.account.foo.fetch(foo.publicKey);
|
|
|
+ })
|
|
|
+ const account = await program.account.foo.fetch(foo.publicKey)
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.authority.toBuffer()) ===
|
|
|
JSON.stringify(program.provider.wallet.publicKey.toBuffer())
|
|
|
- );
|
|
|
- assert.ok(account.data.toNumber() === 0);
|
|
|
- assert.ok(account.secondData.toNumber() === 0);
|
|
|
+ )
|
|
|
+ assert.ok(account.data.toNumber() === 0)
|
|
|
+ assert.ok(account.secondData.toNumber() === 0)
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.secondAuthority) ===
|
|
|
JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
|
|
|
- );
|
|
|
- });
|
|
|
+ )
|
|
|
+ })
|
|
|
|
|
|
- it("Updates a zero copy account field", async () => {
|
|
|
+ it('Updates a zero copy account field', async () => {
|
|
|
await program.rpc.updateFoo(new BN(1234), {
|
|
|
accounts: {
|
|
|
foo: foo.publicKey,
|
|
|
authority: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
|
|
|
- const account = await program.account.foo.fetch(foo.publicKey);
|
|
|
+ const account = await program.account.foo.fetch(foo.publicKey)
|
|
|
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.authority.toBuffer()) ===
|
|
|
JSON.stringify(program.provider.wallet.publicKey.toBuffer())
|
|
|
- );
|
|
|
- assert.ok(account.data.toNumber() === 1234);
|
|
|
- assert.ok(account.secondData.toNumber() === 0);
|
|
|
+ )
|
|
|
+ assert.ok(account.data.toNumber() === 1234)
|
|
|
+ assert.ok(account.secondData.toNumber() === 0)
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.secondAuthority) ===
|
|
|
JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
|
|
|
- );
|
|
|
- });
|
|
|
+ )
|
|
|
+ })
|
|
|
|
|
|
- it("Updates a a second zero copy account field", async () => {
|
|
|
+ it('Updates a a second zero copy account field', async () => {
|
|
|
await program.rpc.updateFooSecond(new BN(55), {
|
|
|
accounts: {
|
|
|
foo: foo.publicKey,
|
|
|
secondAuthority: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
|
|
|
- const account = await program.account.foo.fetch(foo.publicKey);
|
|
|
+ const account = await program.account.foo.fetch(foo.publicKey)
|
|
|
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.authority.toBuffer()) ===
|
|
|
JSON.stringify(program.provider.wallet.publicKey.toBuffer())
|
|
|
- );
|
|
|
- assert.ok(account.data.toNumber() === 1234);
|
|
|
- assert.ok(account.secondData.toNumber() === 55);
|
|
|
+ )
|
|
|
+ assert.ok(account.data.toNumber() === 1234)
|
|
|
+ assert.ok(account.secondData.toNumber() === 55)
|
|
|
assert.ok(
|
|
|
JSON.stringify(account.secondAuthority) ===
|
|
|
JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
|
|
|
- );
|
|
|
- });
|
|
|
+ )
|
|
|
+ })
|
|
|
|
|
|
- it("Creates an associated zero copy account", async () => {
|
|
|
+ it('Creates an associated zero copy account', async () => {
|
|
|
await program.rpc.createBar({
|
|
|
accounts: {
|
|
|
bar: (
|
|
|
await PublicKey.findProgramAddress(
|
|
|
- [
|
|
|
- program.provider.wallet.publicKey.toBuffer(),
|
|
|
- foo.publicKey.toBuffer(),
|
|
|
- ],
|
|
|
+ [program.provider.wallet.publicKey.toBuffer(), foo.publicKey.toBuffer()],
|
|
|
program.programId
|
|
|
)
|
|
|
)[0],
|
|
@@ -133,86 +91,90 @@ describe("zero-copy", () => {
|
|
|
foo: foo.publicKey,
|
|
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
|
|
|
const bar = (
|
|
|
await PublicKey.findProgramAddress(
|
|
|
- [
|
|
|
- program.provider.wallet.publicKey.toBuffer(),
|
|
|
- foo.publicKey.toBuffer(),
|
|
|
- ],
|
|
|
+ [program.provider.wallet.publicKey.toBuffer(), foo.publicKey.toBuffer()],
|
|
|
program.programId
|
|
|
)
|
|
|
- )[0];
|
|
|
- const barAccount = await program.account.bar.fetch(bar);
|
|
|
- assert.ok(barAccount.authority.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(barAccount.data.toNumber() === 0);
|
|
|
- });
|
|
|
+ )[0]
|
|
|
+ const barAccount = await program.account.bar.fetch(bar)
|
|
|
+ assert.ok(barAccount.authority.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(barAccount.data.toNumber() === 0)
|
|
|
+ })
|
|
|
|
|
|
- it("Updates an associated zero copy account", async () => {
|
|
|
+ it('Updates an associated zero copy account', async () => {
|
|
|
const bar = (
|
|
|
await PublicKey.findProgramAddress(
|
|
|
- [
|
|
|
- program.provider.wallet.publicKey.toBuffer(),
|
|
|
- foo.publicKey.toBuffer(),
|
|
|
- ],
|
|
|
+ [program.provider.wallet.publicKey.toBuffer(), foo.publicKey.toBuffer()],
|
|
|
program.programId
|
|
|
)
|
|
|
- )[0];
|
|
|
+ )[0]
|
|
|
await program.rpc.updateBar(new BN(99), {
|
|
|
accounts: {
|
|
|
bar,
|
|
|
authority: program.provider.wallet.publicKey,
|
|
|
foo: foo.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
- const barAccount = await program.account.bar.fetch(bar);
|
|
|
- assert.ok(barAccount.authority.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(barAccount.data.toNumber() === 99);
|
|
|
- });
|
|
|
+ })
|
|
|
+ const barAccount = await program.account.bar.fetch(bar)
|
|
|
+ assert.ok(barAccount.authority.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(barAccount.data.toNumber() === 99)
|
|
|
+ // Check zero_copy CPI
|
|
|
+ await programCpi.rpc.checkCpi(new BN(1337), {
|
|
|
+ accounts: {
|
|
|
+ bar,
|
|
|
+ authority: program.provider.wallet.publicKey,
|
|
|
+ foo: foo.publicKey,
|
|
|
+ zeroCopyProgram: program.programId,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ const barAccountAfterCpi = await program.account.bar.fetch(bar)
|
|
|
+ assert.ok(barAccountAfterCpi.authority.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(barAccountAfterCpi.data.toNumber() === 1337)
|
|
|
+ })
|
|
|
|
|
|
- const eventQ = anchor.web3.Keypair.generate();
|
|
|
- const size = 1000000 + 8; // Account size in bytes.
|
|
|
+ const eventQ = anchor.web3.Keypair.generate()
|
|
|
+ const size = 1000000 + 8 // Account size in bytes.
|
|
|
|
|
|
- it("Creates a large event queue", async () => {
|
|
|
+ it('Creates a large event queue', async () => {
|
|
|
await program.rpc.createLargeAccount({
|
|
|
accounts: {
|
|
|
eventQ: eventQ.publicKey,
|
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
},
|
|
|
- instructions: [
|
|
|
- await program.account.eventQ.createInstruction(eventQ, size),
|
|
|
- ],
|
|
|
+ instructions: [await program.account.eventQ.createInstruction(eventQ, size)],
|
|
|
signers: [eventQ],
|
|
|
- });
|
|
|
- const account = await program.account.eventQ.fetch(eventQ.publicKey);
|
|
|
- assert.ok(account.events.length === 25000);
|
|
|
+ })
|
|
|
+ const account = await program.account.eventQ.fetch(eventQ.publicKey)
|
|
|
+ assert.ok(account.events.length === 25000)
|
|
|
account.events.forEach((event) => {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
- });
|
|
|
- });
|
|
|
+ assert.ok(event.from.equals(PublicKey.default))
|
|
|
+ assert.ok(event.data.toNumber() === 0)
|
|
|
+ })
|
|
|
+ })
|
|
|
|
|
|
- it("Updates a large event queue", async () => {
|
|
|
+ it('Updates a large event queue', async () => {
|
|
|
// Set index 0.
|
|
|
await program.rpc.updateLargeAccount(0, new BN(48), {
|
|
|
accounts: {
|
|
|
eventQ: eventQ.publicKey,
|
|
|
from: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
// Verify update.
|
|
|
- let account = await program.account.eventQ.fetch(eventQ.publicKey);
|
|
|
- assert.ok(account.events.length === 25000);
|
|
|
+ let account = await program.account.eventQ.fetch(eventQ.publicKey)
|
|
|
+ assert.ok(account.events.length === 25000)
|
|
|
account.events.forEach((event, idx) => {
|
|
|
if (idx === 0) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 48);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 48)
|
|
|
} else {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
+ assert.ok(event.from.equals(PublicKey.default))
|
|
|
+ assert.ok(event.data.toNumber() === 0)
|
|
|
}
|
|
|
- });
|
|
|
+ })
|
|
|
|
|
|
// Set index 11111.
|
|
|
await program.rpc.updateLargeAccount(11111, new BN(1234), {
|
|
@@ -220,22 +182,22 @@ describe("zero-copy", () => {
|
|
|
eventQ: eventQ.publicKey,
|
|
|
from: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
// Verify update.
|
|
|
- account = await program.account.eventQ.fetch(eventQ.publicKey);
|
|
|
- assert.ok(account.events.length === 25000);
|
|
|
+ account = await program.account.eventQ.fetch(eventQ.publicKey)
|
|
|
+ assert.ok(account.events.length === 25000)
|
|
|
account.events.forEach((event, idx) => {
|
|
|
if (idx === 0) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 48);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 48)
|
|
|
} else if (idx === 11111) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 1234);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 1234)
|
|
|
} else {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
+ assert.ok(event.from.equals(PublicKey.default))
|
|
|
+ assert.ok(event.data.toNumber() === 0)
|
|
|
}
|
|
|
- });
|
|
|
+ })
|
|
|
|
|
|
// Set last index.
|
|
|
await program.rpc.updateLargeAccount(24999, new BN(99), {
|
|
@@ -243,28 +205,28 @@ describe("zero-copy", () => {
|
|
|
eventQ: eventQ.publicKey,
|
|
|
from: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
// Verify update.
|
|
|
- account = await program.account.eventQ.fetch(eventQ.publicKey);
|
|
|
- assert.ok(account.events.length === 25000);
|
|
|
+ account = await program.account.eventQ.fetch(eventQ.publicKey)
|
|
|
+ assert.ok(account.events.length === 25000)
|
|
|
account.events.forEach((event, idx) => {
|
|
|
if (idx === 0) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 48);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 48)
|
|
|
} else if (idx === 11111) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 1234);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 1234)
|
|
|
} else if (idx === 24999) {
|
|
|
- assert.ok(event.from.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(event.data.toNumber() === 99);
|
|
|
+ assert.ok(event.from.equals(program.provider.wallet.publicKey))
|
|
|
+ assert.ok(event.data.toNumber() === 99)
|
|
|
} else {
|
|
|
- assert.ok(event.from.equals(PublicKey.default));
|
|
|
- assert.ok(event.data.toNumber() === 0);
|
|
|
+ assert.ok(event.from.equals(PublicKey.default))
|
|
|
+ assert.ok(event.data.toNumber() === 0)
|
|
|
}
|
|
|
- });
|
|
|
- });
|
|
|
+ })
|
|
|
+ })
|
|
|
|
|
|
- it("Errors when setting an out of bounds index", async () => {
|
|
|
+ it('Errors when setting an out of bounds index', async () => {
|
|
|
// Fail to set non existing index.
|
|
|
await assert.rejects(
|
|
|
async () => {
|
|
@@ -273,12 +235,12 @@ describe("zero-copy", () => {
|
|
|
eventQ: eventQ.publicKey,
|
|
|
from: program.provider.wallet.publicKey,
|
|
|
},
|
|
|
- });
|
|
|
+ })
|
|
|
},
|
|
|
(err) => {
|
|
|
- console.log("err", err);
|
|
|
- return true;
|
|
|
+ console.log('err', err)
|
|
|
+ return true
|
|
|
}
|
|
|
- );
|
|
|
- });
|
|
|
-});
|
|
|
+ )
|
|
|
+ })
|
|
|
+})
|