identify.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. accountNode,
  3. constantDiscriminatorNode,
  4. constantValueNodeFromBytes,
  5. instructionNode,
  6. programNode,
  7. rootNode,
  8. sizeDiscriminatorNode,
  9. } from '@codama/nodes';
  10. import { describe, expect, test } from 'vitest';
  11. import { identifyAccountData, identifyInstructionData } from '../src';
  12. import { hex } from './_setup';
  13. describe('identifyAccountData', () => {
  14. test('it identifies an account using its discriminator nodes', () => {
  15. const root = rootNode(
  16. programNode({
  17. accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
  18. name: 'myProgram',
  19. publicKey: '1111',
  20. }),
  21. );
  22. const result = identifyAccountData(root, hex('01020304'));
  23. expect(result).toStrictEqual([root, root.program, root.program.accounts[0]]);
  24. });
  25. test('it fails to identify accounts whose discriminator nodes do not match the given data', () => {
  26. const root = rootNode(
  27. programNode({
  28. accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(999)], name: 'myAccount' })],
  29. name: 'myProgram',
  30. publicKey: '1111',
  31. }),
  32. );
  33. const result = identifyAccountData(root, hex('01020304'));
  34. expect(result).toBeUndefined();
  35. });
  36. test('it fails to identify accounts with no discriminator nodes', () => {
  37. const root = rootNode(
  38. programNode({ accounts: [accountNode({ name: 'myAccount' })], name: 'myProgram', publicKey: '1111' }),
  39. );
  40. const result = identifyAccountData(root, hex('01020304'));
  41. expect(result).toBeUndefined();
  42. });
  43. test('it identifies the first matching account if multiple accounts match', () => {
  44. const root = rootNode(
  45. programNode({
  46. accounts: [
  47. accountNode({
  48. discriminators: [sizeDiscriminatorNode(4)],
  49. name: 'accountA',
  50. }),
  51. accountNode({
  52. discriminators: [constantDiscriminatorNode(constantValueNodeFromBytes('base16', 'ff'))],
  53. name: 'accountB',
  54. }),
  55. ],
  56. name: 'myProgram',
  57. publicKey: '1111',
  58. }),
  59. );
  60. const result = identifyAccountData(root, hex('ff010203'));
  61. expect(result).toStrictEqual([root, root.program, root.program.accounts[0]]);
  62. });
  63. test('it does not identify accounts in additional programs', () => {
  64. const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
  65. programNode({
  66. accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
  67. name: 'myProgram',
  68. publicKey: '1111',
  69. }),
  70. ]);
  71. const result = identifyAccountData(root, hex('01020304'));
  72. expect(result).toBeUndefined();
  73. });
  74. test('it does not identify accounts using instruction discriminators', () => {
  75. const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
  76. programNode({
  77. instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
  78. name: 'myProgram',
  79. publicKey: '1111',
  80. }),
  81. ]);
  82. const result = identifyAccountData(root, hex('01020304'));
  83. expect(result).toBeUndefined();
  84. });
  85. });
  86. describe('identifyInstructionData', () => {
  87. test('it identifies an instruction using its discriminator nodes', () => {
  88. const root = rootNode(
  89. programNode({
  90. instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
  91. name: 'myProgram',
  92. publicKey: '1111',
  93. }),
  94. );
  95. const result = identifyInstructionData(root, hex('01020304'));
  96. expect(result).toStrictEqual([root, root.program, root.program.instructions[0]]);
  97. });
  98. test('it fails to identify instructions whose discriminator nodes do not match the given data', () => {
  99. const root = rootNode(
  100. programNode({
  101. instructions: [
  102. instructionNode({ discriminators: [sizeDiscriminatorNode(999)], name: 'myInstruction' }),
  103. ],
  104. name: 'myProgram',
  105. publicKey: '1111',
  106. }),
  107. );
  108. const result = identifyInstructionData(root, hex('01020304'));
  109. expect(result).toBeUndefined();
  110. });
  111. test('it fails to identify instructions with no discriminator nodes', () => {
  112. const root = rootNode(
  113. programNode({
  114. instructions: [instructionNode({ name: 'myInstruction' })],
  115. name: 'myProgram',
  116. publicKey: '1111',
  117. }),
  118. );
  119. const result = identifyInstructionData(root, hex('01020304'));
  120. expect(result).toBeUndefined();
  121. });
  122. test('it identifies the first matching instruction if multiple instructions match', () => {
  123. const root = rootNode(
  124. programNode({
  125. instructions: [
  126. instructionNode({
  127. discriminators: [sizeDiscriminatorNode(4)],
  128. name: 'instructionA',
  129. }),
  130. instructionNode({
  131. discriminators: [constantDiscriminatorNode(constantValueNodeFromBytes('base16', 'ff'))],
  132. name: 'instructionB',
  133. }),
  134. ],
  135. name: 'myProgram',
  136. publicKey: '1111',
  137. }),
  138. );
  139. const result = identifyInstructionData(root, hex('ff010203'));
  140. expect(result).toStrictEqual([root, root.program, root.program.instructions[0]]);
  141. });
  142. test('it does not identify instructions in additional programs', () => {
  143. const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
  144. programNode({
  145. instructions: [instructionNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myInstruction' })],
  146. name: 'myProgram',
  147. publicKey: '1111',
  148. }),
  149. ]);
  150. const result = identifyInstructionData(root, hex('01020304'));
  151. expect(result).toBeUndefined();
  152. });
  153. test('it does not identify instructions using account discriminators', () => {
  154. const root = rootNode(programNode({ name: 'myProgram', publicKey: '1111' }), [
  155. programNode({
  156. accounts: [accountNode({ discriminators: [sizeDiscriminatorNode(4)], name: 'myAccount' })],
  157. name: 'myProgram',
  158. publicKey: '1111',
  159. }),
  160. ]);
  161. const result = identifyInstructionData(root, hex('01020304'));
  162. expect(result).toBeUndefined();
  163. });
  164. });