Skip to content

Commit 4497b70

Browse files
feat(txm): added on new block hook
1 parent 5e90c92 commit 4497b70

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

packages/randomness-service/src/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class RandomnessService {
3737
async start() {
3838
await Promise.all([this.txm.start(), this.randomnessRepository.start()])
3939
this.txm.addHook(this.onTransactionStatusChange.bind(this), TxmHookType.TransactionStatusChanged)
40+
this.txm.addHook(this.onNewBlock.bind(this), TxmHookType.NewBlock)
4041
}
4142

4243
private onTransactionStatusChange(payload: { transaction: Transaction }) {
@@ -70,6 +71,15 @@ class RandomnessService {
7071
}
7172
}
7273

74+
private async onNewBlock(payload: { block: LatestBlock }) {
75+
this.handleRevealNotSubmittedOnTime(payload.block)
76+
this.randomnessRepository.pruneRandomnesses(payload.block.timestamp).then((result) => {
77+
if (result.isErr()) {
78+
console.error("Failed to prune commitments", result.error)
79+
}
80+
})
81+
}
82+
7383
private async handleRevealNotSubmittedOnTime(block: LatestBlock) {
7484
const randomnesses = this.randomnessRepository.getRandomnessInStatus(RandomnessStatus.COMMITMENT_EXECUTED)
7585

@@ -148,13 +158,6 @@ class RandomnessService {
148158
}
149159
})
150160

151-
// We don't await for pruning, because we don't want to block the transaction collection
152-
this.randomnessRepository.pruneRandomnesses(block.timestamp).then((result) => {
153-
if (result.isErr()) {
154-
console.error("Failed to prune commitments", result.error)
155-
}
156-
})
157-
158161
return transactions
159162
}
160163
}
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
import type { LatestBlock } from "./BlockMonitor"
12
import { Topics, eventBus } from "./EventBus.js"
23
import type { Transaction } from "./Transaction.js"
34

45
export enum TxmHookType {
56
All = "All",
67
TransactionStatusChanged = "TransactionStatusChanged",
8+
NewBlock = "NewBlock",
79
}
810

9-
export type TxmHookPayload = {
10-
type: TxmHookType
11+
export type TxmTransactionStatusChangedHookPayload = {
12+
type: TxmHookType.TransactionStatusChanged
1113
transaction: Transaction
1214
}
1315

14-
export type TxmHookHandler = (event: TxmHookPayload) => void
16+
export type TxmNewBlockHookPayload = {
17+
type: TxmHookType.NewBlock
18+
block: LatestBlock
19+
}
20+
21+
export type TxmHookPayload = TxmTransactionStatusChangedHookPayload | TxmNewBlockHookPayload
22+
23+
export type TxmHookHandler<T extends TxmHookType = TxmHookType> = (
24+
event: Extract<TxmHookPayload, { type: T }> extends never ? TxmHookPayload : Extract<TxmHookPayload, { type: T }>,
25+
) => void
1526

1627
/**
1728
* This module manages the hooks system. A hook in the transaction manager is a callback function that
@@ -24,31 +35,42 @@ export type TxmHookHandler = (event: TxmHookPayload) => void
2435
* - The callback function that executes when the event occurs.
2536
*/
2637
export class HookManager {
27-
private hooks: Record<TxmHookType, TxmHookHandler[]>
38+
private hooks: {
39+
[T in TxmHookType]: TxmHookHandler<T>[]
40+
}
2841

2942
constructor() {
3043
this.hooks = {
3144
[TxmHookType.All]: [],
3245
[TxmHookType.TransactionStatusChanged]: [],
46+
[TxmHookType.NewBlock]: [],
3347
}
3448
eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this))
49+
eventBus.on(Topics.NewBlock, this.onNewBlock.bind(this))
3550
}
3651

37-
public async addHook(handler: TxmHookHandler, type: TxmHookType): Promise<void> {
52+
public async addHook<T extends TxmHookType>(handler: TxmHookHandler<T>, type: T): Promise<void> {
3853
if (!this.hooks[type]) {
3954
this.hooks[type] = []
4055
}
4156
this.hooks[type].push(handler)
4257
}
4358

44-
private async onTransactionStatusChanged(payload: {
45-
transaction: Transaction
46-
}): Promise<void> {
47-
this.hooks[TxmHookType.TransactionStatusChanged].concat(this.hooks[TxmHookType.All]).map((h) =>
59+
private async onTransactionStatusChanged(payload: { transaction: Transaction }): Promise<void> {
60+
;[...this.hooks[TxmHookType.TransactionStatusChanged], ...this.hooks[TxmHookType.All]].forEach((h) =>
4861
h({
4962
type: TxmHookType.TransactionStatusChanged,
5063
transaction: payload.transaction,
5164
}),
5265
)
5366
}
67+
68+
private async onNewBlock(block: LatestBlock): Promise<void> {
69+
;[...this.hooks[TxmHookType.NewBlock], ...this.hooks[TxmHookType.All]].forEach((h) =>
70+
h({
71+
type: TxmHookType.NewBlock,
72+
block,
73+
}),
74+
)
75+
}
5476
}

packages/transaction-manager/lib/TransactionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class TransactionManager {
263263
* @param type - The type of hook to add.
264264
* @param handler - The handler function to add.
265265
*/
266-
public async addHook(handler: TxmHookHandler, type: TxmHookType): Promise<void> {
266+
public async addHook<T extends TxmHookType>(handler: TxmHookHandler<T>, type: T): Promise<void> {
267267
await this.hookManager.addHook(handler, type)
268268
}
269269

0 commit comments

Comments
 (0)