|
| 1 | +import { it, expect, describe, beforeEach, vi } from "vitest"; |
| 2 | +import { AccountOnchain } from "../../src/state/accountOnchain"; |
| 3 | +import { AccountStateMerkleIndexed } from "../../src/state/types"; |
| 4 | +import { IContract } from "../../src/chain/contract"; |
| 5 | +import { AccountNotOnChainError } from "../../src/errors"; |
| 6 | +import { |
| 7 | + Scalar, |
| 8 | + scalarToBigint |
| 9 | +} from "@cardinal-cryptography/shielder-sdk-crypto"; |
| 10 | +import { nativeToken } from "../../src/utils"; |
| 11 | +import { ContractFunctionRevertedError, encodeErrorResult } from "viem"; |
| 12 | + |
| 13 | +describe("AccountOnchain", () => { |
| 14 | + let accountOnchain: AccountOnchain; |
| 15 | + let mockContract: IContract; |
| 16 | + let testAccountState: AccountStateMerkleIndexed; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Create a mock contract |
| 20 | + mockContract = { |
| 21 | + getMerklePath: vi.fn() |
| 22 | + } as any; |
| 23 | + |
| 24 | + // Create test account state |
| 25 | + testAccountState = { |
| 26 | + id: Scalar.fromBigint(123n), |
| 27 | + token: nativeToken(), |
| 28 | + nonce: 1n, |
| 29 | + balance: 1000n, |
| 30 | + currentNote: Scalar.fromBigint(456n), |
| 31 | + currentNoteIndex: 10n |
| 32 | + }; |
| 33 | + |
| 34 | + accountOnchain = new AccountOnchain(mockContract); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("constructor", () => { |
| 38 | + it("should create an instance with the provided contract", () => { |
| 39 | + expect(accountOnchain).toBeInstanceOf(AccountOnchain); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("validateAccountState", () => { |
| 44 | + it("should validate successfully when merkle path matches current note", async () => { |
| 45 | + // Setup: Mock getMerklePath to return a path where first element matches currentNote |
| 46 | + const expectedMerklePath = [ |
| 47 | + scalarToBigint(testAccountState.currentNote), // First element matches |
| 48 | + 789n, |
| 49 | + 101112n |
| 50 | + ] as const; |
| 51 | + |
| 52 | + vi.mocked(mockContract.getMerklePath).mockResolvedValue( |
| 53 | + expectedMerklePath |
| 54 | + ); |
| 55 | + |
| 56 | + // Act & Assert: Should not throw |
| 57 | + await expect( |
| 58 | + accountOnchain.validateAccountState(testAccountState) |
| 59 | + ).resolves.toBeUndefined(); |
| 60 | + |
| 61 | + // Verify contract was called with correct index |
| 62 | + expect(mockContract.getMerklePath).toHaveBeenCalledWith( |
| 63 | + testAccountState.currentNoteIndex |
| 64 | + ); |
| 65 | + expect(mockContract.getMerklePath).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should throw AccountNotOnChainError when merkle path does not match current note", async () => { |
| 69 | + // Setup: Mock getMerklePath to return a path where first element does NOT match |
| 70 | + const mismatchedMerklePath = [ |
| 71 | + 999n, // Different from currentNote (456n) |
| 72 | + 789n, |
| 73 | + 101112n |
| 74 | + ] as const; |
| 75 | + |
| 76 | + vi.mocked(mockContract.getMerklePath).mockResolvedValue( |
| 77 | + mismatchedMerklePath |
| 78 | + ); |
| 79 | + |
| 80 | + // Act & Assert: Should throw AccountNotOnChainError |
| 81 | + await expect( |
| 82 | + accountOnchain.validateAccountState(testAccountState) |
| 83 | + ).rejects.toThrow(AccountNotOnChainError); |
| 84 | + |
| 85 | + await expect( |
| 86 | + accountOnchain.validateAccountState(testAccountState) |
| 87 | + ).rejects.toThrow( |
| 88 | + `Account state with merkle index ${testAccountState.currentNoteIndex} does not match on-chain data.` |
| 89 | + ); |
| 90 | + |
| 91 | + // Verify contract was called with correct index |
| 92 | + expect(mockContract.getMerklePath).toHaveBeenCalledWith( |
| 93 | + testAccountState.currentNoteIndex |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should throw AccountNotOnChainError when contract getMerklePath fails", async () => { |
| 98 | + // Setup: Mock getMerklePath to throw an error |
| 99 | + // const contractError = new Error("Contract connection failed"); |
| 100 | + const contractError = new ContractFunctionRevertedError({ |
| 101 | + abi: [ |
| 102 | + { |
| 103 | + type: "error", |
| 104 | + name: "LeafNotExisting", |
| 105 | + inputs: [] |
| 106 | + } |
| 107 | + ], |
| 108 | + functionName: "getMerklePath", |
| 109 | + data: encodeErrorResult({ |
| 110 | + abi: [ |
| 111 | + { |
| 112 | + type: "error", |
| 113 | + name: "LeafNotExisting", |
| 114 | + inputs: [] |
| 115 | + } |
| 116 | + ], |
| 117 | + errorName: "LeafNotExisting" |
| 118 | + }) |
| 119 | + }); |
| 120 | + vi.mocked(mockContract.getMerklePath).mockRejectedValue(contractError); |
| 121 | + |
| 122 | + // Act & Assert: Should throw AccountNotOnChainError with specific message |
| 123 | + await expect( |
| 124 | + accountOnchain.validateAccountState(testAccountState) |
| 125 | + ).rejects.toThrow(AccountNotOnChainError); |
| 126 | + |
| 127 | + await expect( |
| 128 | + accountOnchain.validateAccountState(testAccountState) |
| 129 | + ).rejects.toThrow( |
| 130 | + `Account state with index ${testAccountState.currentNoteIndex} does not exist on-chain.` |
| 131 | + ); |
| 132 | + |
| 133 | + expect(mockContract.getMerklePath).toHaveBeenCalledWith( |
| 134 | + testAccountState.currentNoteIndex |
| 135 | + ); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments