-
Notifications
You must be signed in to change notification settings - Fork 0
/
entityIndexer.ts
138 lines (116 loc) · 4.14 KB
/
entityIndexer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
Blockchain,
IndexableEntityType,
ParsedEntity,
} from '../../../types.js'
import { FetcherMsClient } from '../../fetcher/client.js'
import { ParserMsClient } from '../../parser/client.js'
import { IndexerMsClient } from '../client.js'
import { BaseAccountEntityIndexer } from './accountEntityIndexer.js'
import { EntityIndexerStateStorage } from './dal/entityIndexerState.js'
import { EntityRequest } from './dal/entityRequest.js'
import { BaseIndexerEntityFetcher } from './entityFetcher.js'
import {
AccountIndexerEntityRequestArgs,
GetAccountIndexingEntityStateRequestArgs,
IndexerWorkerDomainI,
GetEntityPendingRequestsRequestArgs,
AccountEntityIndexerState,
} from './types.js'
export class BaseEntityIndexer<
PE extends ParsedEntity<unknown> = ParsedEntity<unknown>,
> {
protected entityHandler: (chunk: PE[]) => Promise<void>
protected accountIndexers: Record<string, BaseAccountEntityIndexer<PE>> = {}
/**
* @param blockchainId The blockchain identifier.
* @param fetcherClient Allows communication with the fetcher service.
* @param indexerClient Allows communication with the sibling indexer instances.
* @param parserClient Allows communication with the parser service.
* @param entityIndexerStateDAL Stores the entity indexer state.
* @param entityFetcher Fetches actual entity data by their signatures.
*/
constructor(
protected type: IndexableEntityType,
protected blockchainId: Blockchain,
protected domain: IndexerWorkerDomainI,
protected indexerClient: IndexerMsClient,
protected fetcherClient: FetcherMsClient,
protected parserClient: ParserMsClient,
protected entityIndexerStateDAL: EntityIndexerStateStorage,
protected entityFetcher: BaseIndexerEntityFetcher<PE>,
) {
this.entityHandler = this.onEntities.bind(this)
}
/**
* Waits for the fetchers and parsers to be ready.
* Registers the onEntities event on the parser,
* such that it can parse ixns from the txns.
*/
async start(): Promise<void> {
await this.fetcherClient.waitForService()
await this.parserClient.waitForService()
this.parserClient.on(
`parser.${this.blockchainId}.${this.type}`,
this.entityHandler,
)
await this.entityFetcher.start().catch(() => 'ignore')
}
async stop(): Promise<void> {
this.parserClient.off(
`parser.${this.blockchainId}.${this.type}`,
this.entityHandler,
)
await this.entityFetcher.stop().catch(() => 'ignore')
}
async addAccount(args: AccountIndexerEntityRequestArgs): Promise<void> {
const { account } = args
let accountIndexer = this.accountIndexers[account]
if (accountIndexer) return
accountIndexer = new BaseAccountEntityIndexer<PE>(
args,
this.domain,
this.fetcherClient,
this.entityFetcher,
this.entityIndexerStateDAL,
)
await accountIndexer.start()
this.accountIndexers[account] = accountIndexer
}
async delAccount(account: string): Promise<void> {
const accountIndexer = this.accountIndexers[account]
if (!accountIndexer) return
await accountIndexer.stop()
delete this.accountIndexers[account]
}
async getAccountState(
args: GetAccountIndexingEntityStateRequestArgs,
): Promise<AccountEntityIndexerState | undefined> {
const { account } = args
const indexer = this.accountIndexers[account]
if (!indexer) return
const state = await indexer.getIndexingState()
if (!state) return
state.indexer = this.indexerClient.getNodeId()
return state
}
async getEntityPendingRequests(
filters: GetEntityPendingRequestsRequestArgs,
): Promise<EntityRequest[]> {
const requests = await this.entityFetcher.getRequests(filters)
return this.mapWithIndexerId(requests)
}
protected async onEntities(chunk: PE[]): Promise<void> {
console.log(
`${this.blockchainId} ${this.type} | 💌 ${chunk.length} entities received by the indexer...`,
)
await this.entityFetcher.onEntities(chunk)
}
protected mapWithIndexerId<T>(items: T[]): T[] {
const indexer = this.indexerClient.getNodeId()
return items.map((item) => {
;(item as any).indexer = indexer
return item
})
}
}