11import { 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" ;
55import type { PendingTransaction } from "../PendingTransaction.js" ;
66import { sequencerModule , SequencerModule } from "../../sequencer/builder/SequencerModule" ;
77import { 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