zero-copy.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. const anchor = require("@project-serum/anchor");
  2. const assert = require("assert");
  3. describe("zero-copy", () => {
  4. // Configure the client to use the local cluster.
  5. anchor.setProvider(anchor.Provider.env());
  6. const program = anchor.workspace.ZeroCopy;
  7. const foo = new anchor.web3.Account();
  8. it("Is creates a zero copy account", async () => {
  9. await program.rpc.createFoo({
  10. accounts: {
  11. foo: foo.publicKey,
  12. authority: program.provider.wallet.publicKey,
  13. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  14. },
  15. instructions: [await program.account.foo.createInstruction(foo)],
  16. signers: [foo],
  17. });
  18. const account = await program.account.foo(foo.publicKey);
  19. assert.ok(
  20. JSON.stringify(account.authority.toBuffer()) ===
  21. JSON.stringify(program.provider.wallet.publicKey.toBuffer())
  22. );
  23. assert.ok(account.data.toNumber() === 0);
  24. assert.ok(account.secondData.toNumber() === 0);
  25. assert.ok(
  26. JSON.stringify(account.secondAuthority) ===
  27. JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
  28. );
  29. });
  30. it("Updates a zero copy account field", async () => {
  31. await program.rpc.updateFoo(new anchor.BN(1234), {
  32. accounts: {
  33. foo: foo.publicKey,
  34. authority: program.provider.wallet.publicKey,
  35. },
  36. });
  37. const account = await program.account.foo(foo.publicKey);
  38. assert.ok(
  39. JSON.stringify(account.authority.toBuffer()) ===
  40. JSON.stringify(program.provider.wallet.publicKey.toBuffer())
  41. );
  42. assert.ok(account.data.toNumber() === 1234);
  43. assert.ok(account.secondData.toNumber() === 0);
  44. assert.ok(
  45. JSON.stringify(account.secondAuthority) ===
  46. JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
  47. );
  48. });
  49. it("Updates a a second zero copy account field", async () => {
  50. await program.rpc.updateFooSecond(new anchor.BN(55), {
  51. accounts: {
  52. foo: foo.publicKey,
  53. secondAuthority: program.provider.wallet.publicKey,
  54. },
  55. });
  56. const account = await program.account.foo(foo.publicKey);
  57. assert.ok(
  58. JSON.stringify(account.authority.toBuffer()) ===
  59. JSON.stringify(program.provider.wallet.publicKey.toBuffer())
  60. );
  61. assert.ok(account.data.toNumber() === 1234);
  62. assert.ok(account.secondData.toNumber() === 55);
  63. assert.ok(
  64. JSON.stringify(account.secondAuthority) ===
  65. JSON.stringify([...program.provider.wallet.publicKey.toBuffer()])
  66. );
  67. });
  68. it("Creates an associated zero copy account", async () => {
  69. await program.rpc.createBar({
  70. accounts: {
  71. bar: await program.account.bar.associatedAddress(
  72. program.provider.wallet.publicKey,
  73. foo.publicKey
  74. ),
  75. authority: program.provider.wallet.publicKey,
  76. foo: foo.publicKey,
  77. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  78. systemProgram: anchor.web3.SystemProgram.programId,
  79. },
  80. });
  81. const bar = await program.account.bar.associated(
  82. program.provider.wallet.publicKey,
  83. foo.publicKey
  84. );
  85. assert.ok(bar.authority.equals(program.provider.wallet.publicKey));
  86. assert.ok(bar.data.toNumber() === 0);
  87. });
  88. it("Updates an associated zero copy account", async () => {
  89. await program.rpc.updateBar(new anchor.BN(99), {
  90. accounts: {
  91. bar: await program.account.bar.associatedAddress(
  92. program.provider.wallet.publicKey,
  93. foo.publicKey
  94. ),
  95. authority: program.provider.wallet.publicKey,
  96. },
  97. });
  98. const bar = await program.account.bar.associated(
  99. program.provider.wallet.publicKey,
  100. foo.publicKey
  101. );
  102. assert.ok(bar.authority.equals(program.provider.wallet.publicKey));
  103. assert.ok(bar.data.toNumber() === 99);
  104. });
  105. const eventQ = new anchor.web3.Account();
  106. const size = 1000000 + 8; // Account size in bytes.
  107. it("Creates a large event queue", async () => {
  108. await program.rpc.createLargeAccount({
  109. accounts: {
  110. eventQ: eventQ.publicKey,
  111. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  112. },
  113. instructions: [
  114. await program.account.eventQ.createInstruction(eventQ, size),
  115. ],
  116. signers: [eventQ],
  117. });
  118. const account = await program.account.eventQ(eventQ.publicKey);
  119. assert.ok(account.events.length === 25000);
  120. account.events.forEach((event) => {
  121. assert.ok(event.from.equals(new anchor.web3.PublicKey()));
  122. assert.ok(event.data.toNumber() === 0);
  123. });
  124. });
  125. it("Updates a large event queue", async () => {
  126. // Set index 0.
  127. await program.rpc.updateLargeAccount(0, new anchor.BN(48), {
  128. accounts: {
  129. eventQ: eventQ.publicKey,
  130. from: program.provider.wallet.publicKey,
  131. },
  132. });
  133. // Verify update.
  134. let account = await program.account.eventQ(eventQ.publicKey);
  135. assert.ok(account.events.length === 25000);
  136. account.events.forEach((event, idx) => {
  137. if (idx === 0) {
  138. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  139. assert.ok(event.data.toNumber() === 48);
  140. } else {
  141. assert.ok(event.from.equals(new anchor.web3.PublicKey()));
  142. assert.ok(event.data.toNumber() === 0);
  143. }
  144. });
  145. // Set index 11111.
  146. await program.rpc.updateLargeAccount(11111, new anchor.BN(1234), {
  147. accounts: {
  148. eventQ: eventQ.publicKey,
  149. from: program.provider.wallet.publicKey,
  150. },
  151. });
  152. // Verify update.
  153. account = await program.account.eventQ(eventQ.publicKey);
  154. assert.ok(account.events.length === 25000);
  155. account.events.forEach((event, idx) => {
  156. if (idx === 0) {
  157. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  158. assert.ok(event.data.toNumber() === 48);
  159. } else if (idx === 11111) {
  160. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  161. assert.ok(event.data.toNumber() === 1234);
  162. } else {
  163. assert.ok(event.from.equals(new anchor.web3.PublicKey()));
  164. assert.ok(event.data.toNumber() === 0);
  165. }
  166. });
  167. // Set last index.
  168. await program.rpc.updateLargeAccount(24999, new anchor.BN(99), {
  169. accounts: {
  170. eventQ: eventQ.publicKey,
  171. from: program.provider.wallet.publicKey,
  172. },
  173. });
  174. // Verify update.
  175. account = await program.account.eventQ(eventQ.publicKey);
  176. assert.ok(account.events.length === 25000);
  177. account.events.forEach((event, idx) => {
  178. if (idx === 0) {
  179. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  180. assert.ok(event.data.toNumber() === 48);
  181. } else if (idx === 11111) {
  182. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  183. assert.ok(event.data.toNumber() === 1234);
  184. } else if (idx === 24999) {
  185. assert.ok(event.from.equals(program.provider.wallet.publicKey));
  186. assert.ok(event.data.toNumber() === 99);
  187. } else {
  188. assert.ok(event.from.equals(new anchor.web3.PublicKey()));
  189. assert.ok(event.data.toNumber() === 0);
  190. }
  191. });
  192. });
  193. it("Errors when setting an out of bounds index", async () => {
  194. // Fail to set non existing index.
  195. await assert.rejects(
  196. async () => {
  197. await program.rpc.updateLargeAccount(25000, new anchor.BN(1), {
  198. accounts: {
  199. eventQ: eventQ.publicKey,
  200. from: program.provider.wallet.publicKey,
  201. },
  202. });
  203. },
  204. (err) => {
  205. console.log("err", err);
  206. return true;
  207. }
  208. );
  209. });
  210. });