|
| 1 | +import "reflect-metadata"; |
| 2 | + |
| 3 | +import { runtimeMethod, runtimeModule, RuntimeModule } from "@proto-kit/module"; |
| 4 | +import { PrivateKey } from "o1js"; |
| 5 | +import { expectDefined, noop } from "@proto-kit/common"; |
| 6 | +import { inject } from "tsyringe"; |
| 7 | +import { Balance, Balances, BalancesKey, TokenId } from "@proto-kit/library"; |
| 8 | + |
| 9 | +import { TestingAppChain } from "../src"; |
| 10 | + |
| 11 | +// This test is designed to check what happens when we have multiple zkPrograms. |
| 12 | +// Currently, the hardcoded maximum for methods per zkProgram is 8 (see Runtime.ts). |
| 13 | +// We will create 20 runtime methods to ensure 2 zkPrograms are created. |
| 14 | + |
| 15 | +@runtimeModule() |
| 16 | +class TestModule1 extends RuntimeModule<unknown> { |
| 17 | + @runtimeMethod() |
| 18 | + public async Method_1() { |
| 19 | + noop(); |
| 20 | + } |
| 21 | + |
| 22 | + @runtimeMethod() |
| 23 | + public async Method_2() { |
| 24 | + noop(); |
| 25 | + } |
| 26 | + |
| 27 | + @runtimeMethod() |
| 28 | + public async Method_3() { |
| 29 | + noop(); |
| 30 | + } |
| 31 | + |
| 32 | + @runtimeMethod() |
| 33 | + public async Method_4() { |
| 34 | + noop(); |
| 35 | + } |
| 36 | + |
| 37 | + @runtimeMethod() |
| 38 | + public async Method_5() { |
| 39 | + noop(); |
| 40 | + } |
| 41 | + |
| 42 | + @runtimeMethod() |
| 43 | + public async Method_6() { |
| 44 | + noop(); |
| 45 | + } |
| 46 | + |
| 47 | + @runtimeMethod() |
| 48 | + public async Method_7() { |
| 49 | + noop(); |
| 50 | + } |
| 51 | + |
| 52 | + @runtimeMethod() |
| 53 | + public async Method_8() { |
| 54 | + noop(); |
| 55 | + } |
| 56 | + |
| 57 | + @runtimeMethod() |
| 58 | + public async Method_9() { |
| 59 | + noop(); |
| 60 | + } |
| 61 | + |
| 62 | + @runtimeMethod() |
| 63 | + public async Method_10() { |
| 64 | + noop(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +@runtimeModule() |
| 69 | +class TestModule2 extends RuntimeModule<unknown> { |
| 70 | + @runtimeMethod() |
| 71 | + public async Method_1() { |
| 72 | + noop(); |
| 73 | + } |
| 74 | + |
| 75 | + @runtimeMethod() |
| 76 | + public async Method_2() { |
| 77 | + noop(); |
| 78 | + } |
| 79 | + |
| 80 | + @runtimeMethod() |
| 81 | + public async Method_3() { |
| 82 | + noop(); |
| 83 | + } |
| 84 | + |
| 85 | + @runtimeMethod() |
| 86 | + public async Method_4() { |
| 87 | + noop(); |
| 88 | + } |
| 89 | + |
| 90 | + @runtimeMethod() |
| 91 | + public async Method_5() { |
| 92 | + noop(); |
| 93 | + } |
| 94 | + |
| 95 | + @runtimeMethod() |
| 96 | + public async Method_6() { |
| 97 | + noop(); |
| 98 | + } |
| 99 | + |
| 100 | + @runtimeMethod() |
| 101 | + public async Method_7() { |
| 102 | + noop(); |
| 103 | + } |
| 104 | + |
| 105 | + @runtimeMethod() |
| 106 | + public async Method_8() { |
| 107 | + noop(); |
| 108 | + } |
| 109 | + |
| 110 | + @runtimeMethod() |
| 111 | + public async Method_9() { |
| 112 | + noop(); |
| 113 | + } |
| 114 | + |
| 115 | + @runtimeMethod() |
| 116 | + public async Method_10() { |
| 117 | + noop(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +@runtimeModule() |
| 122 | +class Faucet extends RuntimeModule<unknown> { |
| 123 | + public constructor(@inject("Balances") public balances: Balances) { |
| 124 | + super(); |
| 125 | + } |
| 126 | + |
| 127 | + @runtimeMethod() |
| 128 | + public async drip() { |
| 129 | + await this.balances.mint( |
| 130 | + TokenId.from(0), |
| 131 | + this.transaction.sender.value, |
| 132 | + Balance.from(1000) |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +describe("check fee analyzer", () => { |
| 138 | + const feeRecipientKey = PrivateKey.random(); |
| 139 | + const senderKey = PrivateKey.random(); |
| 140 | + |
| 141 | + const appChain = TestingAppChain.fromRuntime({ |
| 142 | + TestModule1, |
| 143 | + TestModule2, |
| 144 | + Faucet, |
| 145 | + }); |
| 146 | + |
| 147 | + beforeAll(async () => { |
| 148 | + appChain.configurePartial({ |
| 149 | + Runtime: { |
| 150 | + TestModule1, |
| 151 | + TestModule2, |
| 152 | + Faucet, |
| 153 | + Balances, |
| 154 | + }, |
| 155 | + |
| 156 | + Protocol: { |
| 157 | + ...appChain.config.Protocol!, |
| 158 | + TransactionFee: { |
| 159 | + tokenId: 0n, |
| 160 | + feeRecipient: feeRecipientKey.toPublicKey().toBase58(), |
| 161 | + baseFee: 0n, |
| 162 | + perWeightUnitFee: 0n, |
| 163 | + methods: { |
| 164 | + "TestModule1.Method_1": { |
| 165 | + baseFee: 10n, |
| 166 | + weight: 0n, |
| 167 | + perWeightUnitFee: 0n, |
| 168 | + }, |
| 169 | + "TestModule1.Method_10": { |
| 170 | + baseFee: 10n, |
| 171 | + weight: 0n, |
| 172 | + perWeightUnitFee: 0n, |
| 173 | + }, |
| 174 | + "TestModule2.Method_4": { |
| 175 | + baseFee: 10n, |
| 176 | + weight: 0n, |
| 177 | + perWeightUnitFee: 0n, |
| 178 | + }, |
| 179 | + "TestModule2.Method_7": { |
| 180 | + baseFee: 10n, |
| 181 | + weight: 0n, |
| 182 | + perWeightUnitFee: 0n, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }); |
| 188 | + |
| 189 | + await appChain.start(); |
| 190 | + appChain.setSigner(senderKey); |
| 191 | + }); |
| 192 | + |
| 193 | + it("with multiple zk programs", async () => { |
| 194 | + expect.assertions(4); |
| 195 | + const testModule1 = appChain.runtime.resolve("TestModule1"); |
| 196 | + const testModule2 = appChain.runtime.resolve("TestModule2"); |
| 197 | + const faucet = appChain.runtime.resolve("Faucet"); |
| 198 | + |
| 199 | + const tx1 = await appChain.transaction( |
| 200 | + senderKey.toPublicKey(), |
| 201 | + async () => { |
| 202 | + await faucet.drip(); |
| 203 | + } |
| 204 | + ); |
| 205 | + |
| 206 | + await tx1.sign(); |
| 207 | + await tx1.send(); |
| 208 | + |
| 209 | + await appChain.produceBlock(); |
| 210 | + |
| 211 | + const tx2 = await appChain.transaction( |
| 212 | + senderKey.toPublicKey(), |
| 213 | + async () => { |
| 214 | + await testModule1.Method_1(); |
| 215 | + } |
| 216 | + ); |
| 217 | + |
| 218 | + await tx2.sign(); |
| 219 | + await tx2.send(); |
| 220 | + |
| 221 | + const tx3 = await appChain.transaction( |
| 222 | + senderKey.toPublicKey(), |
| 223 | + async () => { |
| 224 | + await testModule2.Method_4(); |
| 225 | + } |
| 226 | + ); |
| 227 | + |
| 228 | + await tx3.sign(); |
| 229 | + await tx3.send(); |
| 230 | + |
| 231 | + const tx4 = await appChain.transaction( |
| 232 | + senderKey.toPublicKey(), |
| 233 | + async () => { |
| 234 | + await testModule2.Method_7(); |
| 235 | + } |
| 236 | + ); |
| 237 | + |
| 238 | + await tx4.sign(); |
| 239 | + await tx4.send(); |
| 240 | + |
| 241 | + const tx5 = await appChain.transaction( |
| 242 | + senderKey.toPublicKey(), |
| 243 | + async () => { |
| 244 | + await testModule1.Method_10(); |
| 245 | + } |
| 246 | + ); |
| 247 | + |
| 248 | + await tx5.sign(); |
| 249 | + await tx5.send(); |
| 250 | + |
| 251 | + await appChain.produceBlock(); |
| 252 | + |
| 253 | + const senderBalance = await appChain.query.runtime.Balances.balances.get( |
| 254 | + new BalancesKey({ |
| 255 | + tokenId: new TokenId(0), |
| 256 | + address: senderKey.toPublicKey(), |
| 257 | + }) |
| 258 | + ); |
| 259 | + |
| 260 | + const feeRecipientBalance = |
| 261 | + await appChain.query.runtime.Balances.balances.get( |
| 262 | + new BalancesKey({ |
| 263 | + tokenId: new TokenId(0), |
| 264 | + address: feeRecipientKey.toPublicKey(), |
| 265 | + }) |
| 266 | + ); |
| 267 | + |
| 268 | + expectDefined(senderBalance); |
| 269 | + expect(senderBalance.toString()).toBe("990"); |
| 270 | + |
| 271 | + expectDefined(feeRecipientBalance); |
| 272 | + expect(feeRecipientBalance.toString()).toBe("10"); |
| 273 | + }); |
| 274 | +}); |
0 commit comments