From ada28eebb939b7ef063cd568657e7897baf78636 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 13 Apr 2023 09:26:11 +0000 Subject: [PATCH] refactor: minor sequencer cleanup --- .../circuit_block_builder.test.ts | 4 +- .../block_builder/circuit_block_builder.ts | 2 +- .../src/deps/running_promise.ts | 53 ------------------- yarn-project/sequencer-client/src/index.ts | 4 +- .../src/{deps => mocks}/tx.ts | 0 .../src/{deps => mocks}/verification_keys.ts | 0 .../src/sequencer/sequencer.ts | 5 +- 7 files changed, 7 insertions(+), 61 deletions(-) delete mode 100644 yarn-project/sequencer-client/src/deps/running_promise.ts rename yarn-project/sequencer-client/src/{deps => mocks}/tx.ts (100%) rename yarn-project/sequencer-client/src/{deps => mocks}/verification_keys.ts (100%) diff --git a/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.test.ts b/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.test.ts index da0a4099c229..b4e41e77b62e 100644 --- a/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.test.ts +++ b/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.test.ts @@ -19,8 +19,8 @@ import { MockProxy, mock } from 'jest-mock-extended'; import { default as levelup } from 'levelup'; import flatMap from 'lodash.flatmap'; import { default as memdown } from 'memdown'; -import { makeEmptyTx, makeEmptyUnverifiedData } from '../deps/tx.js'; -import { VerificationKeys, getVerificationKeys } from '../deps/verification_keys.js'; +import { makeEmptyTx, makeEmptyUnverifiedData } from '../mocks/tx.js'; +import { VerificationKeys, getVerificationKeys } from '../mocks/verification_keys.js'; import { EmptyProver } from '../prover/empty.js'; import { Prover } from '../prover/index.js'; import { Simulator } from '../simulator/index.js'; diff --git a/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.ts b/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.ts index e5d514ffb90f..90df4173574c 100644 --- a/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.ts +++ b/yarn-project/sequencer-client/src/block_builder/circuit_block_builder.ts @@ -28,7 +28,7 @@ import { MerkleTreeId, MerkleTreeOperations } from '@aztec/world-state'; import chunk from 'lodash.chunk'; import flatMap from 'lodash.flatmap'; import times from 'lodash.times'; -import { VerificationKeys } from '../deps/verification_keys.js'; +import { VerificationKeys } from '../mocks/verification_keys.js'; import { Proof, Prover } from '../prover/index.js'; import { Simulator } from '../simulator/index.js'; diff --git a/yarn-project/sequencer-client/src/deps/running_promise.ts b/yarn-project/sequencer-client/src/deps/running_promise.ts deleted file mode 100644 index 8c52f464ef3c..000000000000 --- a/yarn-project/sequencer-client/src/deps/running_promise.ts +++ /dev/null @@ -1,53 +0,0 @@ -// yarn-project/data-archiver/src/polling_rollup_emitter.ts -export class RunningPromise { - private running = false; - private runningPromise = Promise.resolve(); - private interruptPromise = Promise.resolve(); - private interruptResolve = () => {}; - - private pollingInterval: number; - private includeRunningTime: boolean; - - constructor(private fn: () => Promise, opts?: { pollingInterval?: number; includeRunningTime?: boolean }) { - this.pollingInterval = opts?.pollingInterval ?? 10_000; - this.includeRunningTime = opts?.includeRunningTime ?? false; - } - - /** - * Starts the running promise. - */ - public start() { - this.running = true; - this.interruptPromise = new Promise(resolve => (this.interruptResolve = resolve)); - - const poll = async () => { - while (this.running) { - const start = Date.now(); - await this.fn(); - const elapsed = Date.now() - start; - const sleepTime = this.includeRunningTime ? Math.max(this.pollingInterval - elapsed, 0) : this.pollingInterval; - await this.interruptableSleep(sleepTime); - } - }; - this.runningPromise = poll(); - } - - async stop(): Promise { - this.running = false; - this.interruptResolve(); - await this.runningPromise; - } - - private async interruptableSleep(timeInMs: number) { - let timeout!: NodeJS.Timeout; - const sleepPromise = new Promise(resolve => { - timeout = setTimeout(resolve, timeInMs); - }); - await Promise.race([sleepPromise, this.interruptPromise]); - clearTimeout(timeout); - } - - public isRunning() { - return this.running; - } -} diff --git a/yarn-project/sequencer-client/src/index.ts b/yarn-project/sequencer-client/src/index.ts index e7afc8e9c2c8..0ec05149d651 100644 --- a/yarn-project/sequencer-client/src/index.ts +++ b/yarn-project/sequencer-client/src/index.ts @@ -4,7 +4,7 @@ import { PublisherConfig, TxSenderConfig } from './publisher/config.js'; export * from './sequencer/index.js'; export * from './publisher/index.js'; export * from './client/index.js'; -export * from './deps/tx.js'; -export * from './deps/verification_keys.js'; +export * from './mocks/tx.js'; +export * from './mocks/verification_keys.js'; export type SequencerClientConfig = PublisherConfig & TxSenderConfig & SequencerConfig; diff --git a/yarn-project/sequencer-client/src/deps/tx.ts b/yarn-project/sequencer-client/src/mocks/tx.ts similarity index 100% rename from yarn-project/sequencer-client/src/deps/tx.ts rename to yarn-project/sequencer-client/src/mocks/tx.ts diff --git a/yarn-project/sequencer-client/src/deps/verification_keys.ts b/yarn-project/sequencer-client/src/mocks/verification_keys.ts similarity index 100% rename from yarn-project/sequencer-client/src/deps/verification_keys.ts rename to yarn-project/sequencer-client/src/mocks/verification_keys.ts diff --git a/yarn-project/sequencer-client/src/sequencer/sequencer.ts b/yarn-project/sequencer-client/src/sequencer/sequencer.ts index 81b1471329ab..8f476163b036 100644 --- a/yarn-project/sequencer-client/src/sequencer/sequencer.ts +++ b/yarn-project/sequencer-client/src/sequencer/sequencer.ts @@ -1,10 +1,9 @@ -import { createDebugLogger } from '@aztec/foundation'; +import { RunningPromise, createDebugLogger } from '@aztec/foundation'; import { P2P } from '@aztec/p2p'; import { Tx, UnverifiedData } from '@aztec/types'; import { MerkleTreeId, WorldStateStatus, WorldStateSynchroniser } from '@aztec/world-state'; import times from 'lodash.times'; import { BlockBuilder } from '../block_builder/index.js'; -import { RunningPromise } from '../deps/running_promise.js'; import { makeEmptyTx } from '../index.js'; import { L1Publisher } from '../publisher/l1-publisher.js'; import { ceilPowerOfTwo } from '../utils.js'; @@ -40,7 +39,7 @@ export class Sequencer { public async start() { await this.initialSync(); - this.runningPromise = new RunningPromise(this.work.bind(this), { pollingInterval: this.pollingIntervalMs }); + this.runningPromise = new RunningPromise(this.work.bind(this), this.pollingIntervalMs); this.runningPromise.start(); this.state = SequencerState.IDLE; this.log('Sequencer started');