Skip to content

Commit

Permalink
Implemented mempool events
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Nov 7, 2023
1 parent 35e09e9 commit 9eaf510
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/common/src/events/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export class EventEmitter<Events extends EventsRecord> {
}
}

public on(
event: keyof Events,
listener: (...args: Events[typeof event]) => void
public on<Key extends keyof Events>(
event: Key,
listener: (...args: Events[Key]) => void
) {
(this.listeners[event] ??= []).push(listener);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/sequencer/src/mempool/Mempool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { Field } from "o1js";
import { EventEmittingComponent, EventsRecord } from "@proto-kit/common";

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

export interface MempoolCommitment {
transactionsHash: Field;
}

export interface Mempool {
export interface MempoolEvents extends EventsRecord {
transactionAdded: [PendingTransaction, MempoolCommitment];
transactionsRemoved: [PendingTransaction[]]
}

export interface Mempool extends EventEmittingComponent<MempoolEvents> {
/**
* Add a transaction to the mempool
* @returns The new commitment to the mempool
Expand Down
13 changes: 11 additions & 2 deletions packages/sequencer/src/mempool/private/PrivateMempool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Field, Poseidon } from "o1js";
import { noop } from "@proto-kit/common";
import { EventEmitter, noop } from "@proto-kit/common";

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

private queue: PendingTransaction[] = [];

public events = new EventEmitter<MempoolEvents>();

public constructor(
private readonly transactionValidator: TransactionValidator
) {
Expand All @@ -27,6 +29,8 @@ export class PrivateMempool extends SequencerModule<object> implements Mempool {
// Figure out how to generalize this
this.commitment = Poseidon.hash([this.commitment, tx.hash()]);

this.events.emit("transactionAdded", [tx, this.commitment]);

return { transactionsHash: this.commitment };
}
throw new Error(`Valdiation of tx failed: ${error ?? "unknown error"}`);
Expand All @@ -45,7 +49,12 @@ export class PrivateMempool extends SequencerModule<object> implements Mempool {
public removeTxs(txs: PendingTransaction[]): boolean {
const { length } = this.queue;
this.queue = this.queue.filter((tx) => !txs.includes(tx));

this.events.emit("transactionsRemoved", [txs]);

// Check that all elements have been removed and were in the mempool prior
// eslint-disable-next-line no-warning-comments,max-len
// TODO Make sure that in case of return value false, it gets rolled back somehow
// eslint-disable-next-line unicorn/consistent-destructuring
return length === this.queue.length + txs.length;
}
Expand Down

0 comments on commit 9eaf510

Please sign in to comment.