|
| 1 | +import { promiseWithResolvers } from '@aztec/foundation/promise'; |
| 2 | +import type { AztecNode, AztecNodeAdmin, AztecNodeAdminConfig } from '@aztec/stdlib/interfaces/client'; |
| 3 | +import type { EmbeddedWallet } from '@aztec/wallets/embedded'; |
| 4 | + |
| 5 | +import { mock } from 'jest-mock-extended'; |
| 6 | + |
| 7 | +import { type BotConfig, getBotDefaultConfig } from './config.js'; |
| 8 | +import { BotFactory } from './factory.js'; |
| 9 | +import type { BotStore } from './store/index.js'; |
| 10 | + |
| 11 | +class TestBotFactory extends BotFactory { |
| 12 | + public override withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> { |
| 13 | + return super.withNoMinTxsPerBlock(fn); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +describe('BotFactory.withNoMinTxsPerBlock', () => { |
| 18 | + let minTxsPerBlock: number | undefined; |
| 19 | + let setConfigCalls: (number | undefined)[]; |
| 20 | + let factory: TestBotFactory; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + minTxsPerBlock = 3; |
| 24 | + setConfigCalls = []; |
| 25 | + |
| 26 | + const aztecNodeAdmin = mock<AztecNodeAdmin>(); |
| 27 | + aztecNodeAdmin.getConfig.mockImplementation(() => Promise.resolve({ minTxsPerBlock } as AztecNodeAdminConfig)); |
| 28 | + aztecNodeAdmin.setConfig.mockImplementation(config => { |
| 29 | + setConfigCalls.push(config.minTxsPerBlock); |
| 30 | + minTxsPerBlock = config.minTxsPerBlock; |
| 31 | + return Promise.resolve(); |
| 32 | + }); |
| 33 | + |
| 34 | + const config: BotConfig = { ...getBotDefaultConfig(), flushSetupTransactions: true }; |
| 35 | + const wallet = mock<EmbeddedWallet>(); |
| 36 | + factory = new TestBotFactory(config, wallet, mock<BotStore>(), mock<AztecNode>(), aztecNodeAdmin); |
| 37 | + }); |
| 38 | + |
| 39 | + it('zeroes minTxsPerBlock around a call and restores it after', async () => { |
| 40 | + await factory.withNoMinTxsPerBlock(() => { |
| 41 | + expect(minTxsPerBlock).toBe(0); |
| 42 | + return Promise.resolve(); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(setConfigCalls).toEqual([0, 3]); |
| 46 | + expect(minTxsPerBlock).toBe(3); |
| 47 | + }); |
| 48 | + |
| 49 | + it('zeroes once and restores once across overlapping calls', async () => { |
| 50 | + const gates = [promiseWithResolvers<void>(), promiseWithResolvers<void>(), promiseWithResolvers<void>()]; |
| 51 | + const calls = gates.map(gate => factory.withNoMinTxsPerBlock(() => gate.promise)); |
| 52 | + |
| 53 | + gates[0].resolve(); |
| 54 | + await calls[0]; |
| 55 | + expect(minTxsPerBlock).toBe(0); |
| 56 | + |
| 57 | + gates[1].resolve(); |
| 58 | + await calls[1]; |
| 59 | + expect(minTxsPerBlock).toBe(0); |
| 60 | + |
| 61 | + gates[2].resolve(); |
| 62 | + await calls[2]; |
| 63 | + expect(setConfigCalls).toEqual([0, 3]); |
| 64 | + expect(minTxsPerBlock).toBe(3); |
| 65 | + }); |
| 66 | + |
| 67 | + it('restores minTxsPerBlock when the wrapped call fails', async () => { |
| 68 | + await expect(factory.withNoMinTxsPerBlock(() => Promise.reject(new Error('boom')))).rejects.toThrow('boom'); |
| 69 | + |
| 70 | + expect(setConfigCalls).toEqual([0, 3]); |
| 71 | + expect(minTxsPerBlock).toBe(3); |
| 72 | + }); |
| 73 | + |
| 74 | + it('restores minTxsPerBlock when one of several overlapping calls fails', async () => { |
| 75 | + const gate = promiseWithResolvers<void>(); |
| 76 | + const failing = factory.withNoMinTxsPerBlock(() => Promise.reject(new Error('boom'))); |
| 77 | + const succeeding = factory.withNoMinTxsPerBlock(() => gate.promise); |
| 78 | + |
| 79 | + await expect(failing).rejects.toThrow('boom'); |
| 80 | + expect(minTxsPerBlock).toBe(0); |
| 81 | + |
| 82 | + gate.resolve(); |
| 83 | + await succeeding; |
| 84 | + expect(setConfigCalls).toEqual([0, 3]); |
| 85 | + expect(minTxsPerBlock).toBe(3); |
| 86 | + }); |
| 87 | +}); |
0 commit comments