Skip to content

Commit 9eaf510

Browse files
committed
Implemented mempool events
1 parent 35e09e9 commit 9eaf510

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

packages/common/src/events/EventEmitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export class EventEmitter<Events extends EventsRecord> {
1717
}
1818
}
1919

20-
public on(
21-
event: keyof Events,
22-
listener: (...args: Events[typeof event]) => void
20+
public on<Key extends keyof Events>(
21+
event: Key,
22+
listener: (...args: Events[Key]) => void
2323
) {
2424
(this.listeners[event] ??= []).push(listener);
2525
}

packages/sequencer/src/mempool/Mempool.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import type { Field } from "o1js";
2+
import { EventEmittingComponent, EventsRecord } from "@proto-kit/common";
23

34
import type { PendingTransaction } from "./PendingTransaction.js";
45

56
export interface MempoolCommitment {
67
transactionsHash: Field;
78
}
89

9-
export interface Mempool {
10+
export interface MempoolEvents extends EventsRecord {
11+
transactionAdded: [PendingTransaction, MempoolCommitment];
12+
transactionsRemoved: [PendingTransaction[]]
13+
}
14+
15+
export interface Mempool extends EventEmittingComponent<MempoolEvents> {
1016
/**
1117
* Add a transaction to the mempool
1218
* @returns The new commitment to the mempool

packages/sequencer/src/mempool/private/PrivateMempool.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Field, Poseidon } from "o1js";
2-
import { noop } from "@proto-kit/common";
2+
import { EventEmitter, noop } from "@proto-kit/common";
33

4-
import type { Mempool, MempoolCommitment } from "../Mempool.js";
4+
import type { Mempool, MempoolCommitment, MempoolEvents } from "../Mempool.js";
55
import type { PendingTransaction } from "../PendingTransaction.js";
66
import { sequencerModule, SequencerModule } from "../../sequencer/builder/SequencerModule";
77
import { TransactionValidator } from "../verification/TransactionValidator";
@@ -12,6 +12,8 @@ export class PrivateMempool extends SequencerModule<object> implements Mempool {
1212

1313
private queue: PendingTransaction[] = [];
1414

15+
public events = new EventEmitter<MempoolEvents>();
16+
1517
public constructor(
1618
private readonly transactionValidator: TransactionValidator
1719
) {
@@ -27,6 +29,8 @@ export class PrivateMempool extends SequencerModule<object> implements Mempool {
2729
// Figure out how to generalize this
2830
this.commitment = Poseidon.hash([this.commitment, tx.hash()]);
2931

32+
this.events.emit("transactionAdded", [tx, this.commitment]);
33+
3034
return { transactionsHash: this.commitment };
3135
}
3236
throw new Error(`Valdiation of tx failed: ${error ?? "unknown error"}`);
@@ -45,7 +49,12 @@ export class PrivateMempool extends SequencerModule<object> implements Mempool {
4549
public removeTxs(txs: PendingTransaction[]): boolean {
4650
const { length } = this.queue;
4751
this.queue = this.queue.filter((tx) => !txs.includes(tx));
52+
53+
this.events.emit("transactionsRemoved", [txs]);
54+
4855
// Check that all elements have been removed and were in the mempool prior
56+
// eslint-disable-next-line no-warning-comments,max-len
57+
// TODO Make sure that in case of return value false, it gets rolled back somehow
4958
// eslint-disable-next-line unicorn/consistent-destructuring
5059
return length === this.queue.length + txs.length;
5160
}

0 commit comments

Comments
 (0)