@xyo-network/chain-orchestration 1.19.2 → 1.19.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ import {
2
+ assertEx,
3
+ creatable,
4
+ } from '@xylabs/sdk-js'
5
+ import { ArchivistInstance } from '@xyo-network/archivist-model'
6
+ import {
7
+ Actor, ActorParams,
8
+ MempoolRunner,
9
+ } from '@xyo-network/xl1-sdk'
10
+ import { Mutex } from 'async-mutex'
11
+
12
+ export type MempoolActorParams = ActorParams<{
13
+ mempoolRunner: MempoolRunner
14
+ pendingBlocksArchivist: ArchivistInstance
15
+ pendingTransactionsArchivist: ArchivistInstance
16
+ }>
17
+
18
+ @creatable()
19
+ export class MempoolActor extends Actor<MempoolActorParams> {
20
+ private _blockTimerId: ReturnType<typeof setInterval> | null = null
21
+ private _blockTimerMutex = new Mutex()
22
+
23
+ private _transactionTimerId: ReturnType<typeof setInterval> | null = null
24
+ private _transactionTimerMutex = new Mutex()
25
+
26
+ protected get mempoolRunner() {
27
+ return this.params.mempoolRunner
28
+ }
29
+
30
+ protected get pendingBlocksArchivist() {
31
+ return this.params.pendingBlocksArchivist
32
+ }
33
+
34
+ protected get pendingTransactionsArchivist() {
35
+ return this.params.pendingTransactionsArchivist
36
+ }
37
+
38
+ static override async paramsHandler(params?: Partial<MempoolActorParams>) {
39
+ return {
40
+ ...await super.paramsHandler(params),
41
+ mempoolRunner: assertEx(
42
+ params?.mempoolRunner,
43
+ () => 'mempoolRunner is required for MempoolActor',
44
+ ),
45
+ pendingBlocksArchivist: assertEx(
46
+ params?.pendingBlocksArchivist,
47
+ () => 'pendingBlocksArchivist is required for MempoolActor',
48
+ ),
49
+ pendingTransactionsArchivist: assertEx(
50
+ params?.pendingTransactionsArchivist,
51
+ () => 'pendingTransactionsArchivist is required for MempoolActor',
52
+ ),
53
+ } satisfies MempoolActorParams
54
+ }
55
+
56
+ override async startHandler() {
57
+ await super.startHandler()
58
+ // this.restartTransactionTimer()
59
+ this.restartBlockTimer()
60
+ }
61
+
62
+ override async stopHandler() {
63
+ await super.stopHandler()
64
+ // this.stopTransactionTimer()
65
+ this.stopBlockTimer()
66
+ }
67
+
68
+ private restartBlockTimer() {
69
+ this.stopBlockTimer()
70
+ this._blockTimerId = setInterval(() => {
71
+ if (this._blockTimerMutex.isLocked()) {
72
+ return
73
+ }
74
+ void this._blockTimerMutex.runExclusive(async () => {
75
+ await this.mempoolRunner.prunePendingBlocks({ batchSize: 50 })
76
+ })
77
+ }, 1000)
78
+ }
79
+
80
+ private restartTransactionTimer() {
81
+ this.stopTransactionTimer()
82
+ this._transactionTimerId = setInterval(() => {
83
+ if (this._transactionTimerMutex.isLocked()) {
84
+ return
85
+ }
86
+ void this._transactionTimerMutex.runExclusive(async () => {
87
+ await this.mempoolRunner.prunePendingTransactions({ batchSize: 50 })
88
+ })
89
+ }, 1000)
90
+ }
91
+
92
+ private stopBlockTimer() {
93
+ if (this._blockTimerId !== null) {
94
+ clearInterval(this._blockTimerId)
95
+ }
96
+ this._blockTimerId = null
97
+ }
98
+
99
+ private stopTransactionTimer() {
100
+ if (this._transactionTimerId !== null) {
101
+ clearInterval(this._transactionTimerId)
102
+ }
103
+ this._transactionTimerId = null
104
+ }
105
+ }
@@ -9,27 +9,24 @@ import {
9
9
  Actor,
10
10
  ActorParams,
11
11
  BlockViewer,
12
- BlockViewerMoniker,
13
- MempoolViewer, MempoolViewerMoniker,
12
+ MempoolViewer,
14
13
  } from '@xyo-network/xl1-sdk'
15
14
 
16
15
  export type ValidatorActorParams = ActorParams<
17
16
  {
18
17
  account: AccountInstance
19
- blockViewer?: BlockViewer
18
+ blockViewer: BlockViewer
20
19
  finalizedArchivist: ArchivistInstance
21
- mempoolViewer?: MempoolViewer
20
+ mempoolViewer: MempoolViewer
22
21
  }>
23
22
 
24
23
  @creatable()
25
24
  export class ValidatorActor extends Actor<ValidatorActorParams> {
26
- protected _blockViewer: BlockViewer | undefined
27
25
  protected _lastValidatedBlock: BlockBoundWitness | undefined
28
26
  protected _lastValidatedBlockHash: Hash | undefined
29
- protected _mempoolViewer: MempoolViewer | undefined
30
27
 
31
28
  protected get blockViewer() {
32
- return this._blockViewer!
29
+ return this.params.blockViewer
33
30
  }
34
31
 
35
32
  protected get finalizedArchivist() {
@@ -37,21 +34,29 @@ export class ValidatorActor extends Actor<ValidatorActorParams> {
37
34
  }
38
35
 
39
36
  protected get mempoolViewer() {
40
- return this._mempoolViewer!
37
+ return this.params.mempoolViewer
41
38
  }
42
39
 
43
- override async createHandler() {
44
- await super.createHandler()
45
-
46
- this._blockViewer = assertEx(
47
- this.params.blockViewer ?? await this.locator?.getInstance<BlockViewer>(BlockViewerMoniker),
48
- () => 'Unable to locate a BlockViewer',
49
- )
50
-
51
- this._mempoolViewer = assertEx(
52
- this.params.mempoolViewer ?? await this.locator?.getInstance<MempoolViewer>(MempoolViewerMoniker),
53
- () => 'Unable to locate a MempoolViewer',
54
- )
40
+ static override async paramsHandler(params?: Partial<ValidatorActorParams>) {
41
+ return {
42
+ ...await super.paramsHandler(params),
43
+ mempoolViewer: assertEx(
44
+ params?.mempoolViewer,
45
+ () => 'mempoolViewer is required for ValidatorActor',
46
+ ),
47
+ blockViewer: assertEx(
48
+ params?.blockViewer,
49
+ () => 'blockViewer is required for ValidatorActor',
50
+ ),
51
+ finalizedArchivist: assertEx(
52
+ params?.finalizedArchivist,
53
+ () => 'finalizedArchivist is required for ValidatorActor',
54
+ ),
55
+ account: assertEx(
56
+ params?.account,
57
+ () => 'account is required for ValidatorActor',
58
+ ),
59
+ } satisfies ValidatorActorParams
55
60
  }
56
61
 
57
62
  override async startHandler(): Promise<void> {
@@ -1,2 +1,3 @@
1
1
  export * from './BalanceActor.ts'
2
+ export * from './MempoolActor.ts'
2
3
  export * from './ValidatorActor.ts'