|
| 1 | +import "reflect-metadata"; |
| 2 | +import { |
| 3 | + afterAll, |
| 4 | + afterEach, |
| 5 | + beforeAll, |
| 6 | + describe, |
| 7 | + expect, |
| 8 | +} from "@jest/globals"; |
| 9 | +import { expectDefined, equalProvable } from "@proto-kit/common"; |
| 10 | +import { Balance, TokenId } from "@proto-kit/library"; |
| 11 | +import { NetworkState } from "@proto-kit/protocol"; |
| 12 | +import { AppChainTransaction } from "@proto-kit/sdk"; |
| 13 | +import { ComputedBlock, UnprovenBlock } from "@proto-kit/sequencer"; |
| 14 | +import { PrivateKey, PublicKey } from "o1js"; |
| 15 | +import { container } from "tsyringe"; |
| 16 | +import { |
| 17 | + PrismaBatchStore, |
| 18 | + PrismaBlockStorage, |
| 19 | + PrismaTransactionStorage |
| 20 | +} from "../src"; |
| 21 | +import { |
| 22 | + createPrismaAppchain, |
| 23 | + IntegrationTestDBConfig, |
| 24 | + prepareBlock |
| 25 | +} from "./utils"; |
| 26 | + |
| 27 | +describe("Prisma integration", () => { |
| 28 | + let appChain: ReturnType<typeof createPrismaAppchain>; |
| 29 | + |
| 30 | + const sender = PrivateKey.random(); |
| 31 | + let senderNonce = 0; |
| 32 | + |
| 33 | + const setup = async () => { |
| 34 | + const { prismaConfig, redisConfig } = IntegrationTestDBConfig; |
| 35 | + appChain = createPrismaAppchain(prismaConfig, redisConfig); |
| 36 | + |
| 37 | + appChain.configurePartial({ |
| 38 | + Signer: { |
| 39 | + signer: sender, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + await appChain.start(container.createChildContainer()); |
| 44 | + |
| 45 | + const db = appChain.sequencer.resolve("Database"); |
| 46 | + await db.prisma.clearDatabase(); |
| 47 | + await db.redis.clearDatabase(); |
| 48 | + |
| 49 | + senderNonce = 0; |
| 50 | + }; |
| 51 | + |
| 52 | + const teardown = async () => { |
| 53 | + await appChain.sequencer.resolve("Database").close(); |
| 54 | + }; |
| 55 | + |
| 56 | + describe("produce fuzzed block", () => { |
| 57 | + let block: UnprovenBlock | undefined; |
| 58 | + let batch: ComputedBlock | undefined; |
| 59 | + |
| 60 | + beforeAll(async () => { |
| 61 | + await setup(); |
| 62 | + |
| 63 | + await prepareBlock(appChain, sender.toPublicKey(), senderNonce); |
| 64 | + senderNonce++; |
| 65 | + |
| 66 | + [block, batch] = await appChain.sequencer |
| 67 | + .resolve("BlockTrigger") |
| 68 | + .produceBlock(); |
| 69 | + }, 30000); |
| 70 | + |
| 71 | + afterAll(async () => { |
| 72 | + await teardown(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should have produced a block and batch", () => { |
| 76 | + expectDefined(block); |
| 77 | + expectDefined(batch); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should save and retrieve the same block", async () => { |
| 81 | + expectDefined(block); |
| 82 | + |
| 83 | + // Check equality of block |
| 84 | + const blockStorage = await appChain.sequencer.resolveOrFail( |
| 85 | + "UnprovenBlockStorage", |
| 86 | + PrismaBlockStorage |
| 87 | + ); |
| 88 | + const retrievedBlock = await blockStorage.getBlockAt(0); |
| 89 | + expectDefined(retrievedBlock); |
| 90 | + |
| 91 | + // Check that transactions match |
| 92 | + expect(retrievedBlock.transactions).toHaveLength(1); |
| 93 | + expect(retrievedBlock.transactions[0].tx.hash().toString()).toStrictEqual( |
| 94 | + block.transactions[0].tx.hash().toString() |
| 95 | + ); |
| 96 | + |
| 97 | + expect(retrievedBlock.hash.toString()).toStrictEqual( |
| 98 | + block.hash.toString() |
| 99 | + ); |
| 100 | + |
| 101 | + equalProvable( |
| 102 | + NetworkState.toFields(retrievedBlock.networkState.before), |
| 103 | + NetworkState.toFields(block.networkState.before) |
| 104 | + ); |
| 105 | + equalProvable( |
| 106 | + NetworkState.toFields(retrievedBlock.networkState.during), |
| 107 | + NetworkState.toFields(block.networkState.during) |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should save and retrieve the same batch", async () => { |
| 112 | + expectDefined(batch); |
| 113 | + |
| 114 | + // Check equality of batch |
| 115 | + const batchStorage = await appChain.sequencer.resolveOrFail( |
| 116 | + "BlockStorage", |
| 117 | + PrismaBatchStore |
| 118 | + ); |
| 119 | + const retrievedBatch = await batchStorage.getBlockAt(0); |
| 120 | + expectDefined(retrievedBatch); |
| 121 | + |
| 122 | + expect(retrievedBatch.height).toStrictEqual(batch.height); |
| 123 | + expect(retrievedBatch.bundles).toHaveLength( |
| 124 | + retrievedBatch.bundles.length |
| 125 | + ); |
| 126 | + expect(retrievedBatch.bundles).toStrictEqual(retrievedBatch.bundles); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should query fetches correct nonce", async () => { |
| 130 | + const accountState = |
| 131 | + await appChain.query.protocol.AccountState.accountState.get( |
| 132 | + sender.toPublicKey() |
| 133 | + ); |
| 134 | + |
| 135 | + expectDefined(accountState); |
| 136 | + |
| 137 | + expect(accountState.nonce.toString()).toBe("1"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should query fetches undefined for unset account", async () => { |
| 141 | + const accountState = |
| 142 | + await appChain.query.protocol.AccountState.accountState.get( |
| 143 | + PublicKey.empty() |
| 144 | + ); |
| 145 | + |
| 146 | + expect(accountState).toBeUndefined(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe("persisted mempool", () => { |
| 151 | + let transaction: AppChainTransaction; |
| 152 | + |
| 153 | + beforeAll(async () => { |
| 154 | + await setup(); |
| 155 | + |
| 156 | + transaction = await prepareBlock(appChain, sender.toPublicKey(), senderNonce); |
| 157 | + senderNonce++; |
| 158 | + }); |
| 159 | + |
| 160 | + afterAll(async () => { |
| 161 | + await teardown(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("mempool should give 1 transaction", async () => { |
| 165 | + const mempool = appChain.sequencer.resolve("Mempool"); |
| 166 | + const txs = await mempool.getTxs(); |
| 167 | + |
| 168 | + expectDefined(transaction.transaction); |
| 169 | + |
| 170 | + expect(txs).toHaveLength(1); |
| 171 | + expect(txs[0].hash().toString()).toStrictEqual( |
| 172 | + transaction.transaction.hash().toString() |
| 173 | + ); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should resolve transaction from storage as pending", async () => { |
| 177 | + const txResolver = appChain.sequencer.resolveOrFail("TransactionStorage", PrismaTransactionStorage); |
| 178 | + |
| 179 | + const txs = await txResolver.getPendingUserTransactions(); |
| 180 | + |
| 181 | + expectDefined(transaction.transaction); |
| 182 | + |
| 183 | + expect(txs).toHaveLength(1); |
| 184 | + expect(txs[0].hash().toString()).toStrictEqual( |
| 185 | + transaction.transaction.hash().toString() |
| 186 | + ); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments