@sentio/runtime 4.0.0-rc.8 → 4.0.0-rc.9

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.
package/src/provider.ts DELETED
@@ -1,195 +0,0 @@
1
- import { JsonRpcProvider, Network } from 'ethers'
2
-
3
- import PQueue from 'p-queue'
4
- import { Endpoints } from './endpoints.js'
5
- import { EthChainId } from '@sentio/chain'
6
- import { LRUCache } from 'lru-cache'
7
- import { providerMetrics, processMetrics, metricsStorage } from './metrics.js'
8
- import { GLOBAL_CONFIG } from './global-config.js'
9
- const { miss_count, hit_count, queue_size } = providerMetrics
10
-
11
- export const DummyProvider = new JsonRpcProvider('', Network.from(1))
12
-
13
- const providers = new Map<string, JsonRpcProvider>()
14
-
15
- // export function getEthChainId(networkish?: EthContext | EthChainId): EthChainId {
16
- // if (!networkish) {
17
- // networkish = EthChainId.ETHEREUM
18
- // }
19
- // if (networkish instanceof BaseContext) {
20
- // networkish = networkish.getChainId()
21
- // }
22
- // return networkish
23
- // }
24
-
25
- export function getProvider(chainId?: EthChainId): JsonRpcProvider {
26
- // const network = getNetworkFromCtxOrNetworkish(networkish)
27
- if (!chainId) {
28
- chainId = EthChainId.ETHEREUM
29
- }
30
- const network = Network.from(parseInt(chainId))
31
- // TODO check if other key needed
32
-
33
- const address = Endpoints.INSTANCE.getChainRpcUrl(chainId)
34
- const key = network.chainId.toString() + '-' + address
35
-
36
- // console.debug(`init provider for ${chainId}, address: ${address}`)
37
- let provider = providers.get(key)
38
-
39
- if (provider) {
40
- return provider
41
- }
42
-
43
- if (address === undefined) {
44
- throw Error(
45
- 'Provider not found for chain ' +
46
- network.chainId +
47
- ', configured chains: ' +
48
- [...Endpoints.INSTANCE.chainRpc.keys()].join(' ')
49
- )
50
- }
51
- // console.log(
52
- // `init provider for chain ${network.chainId}, concurrency: ${Endpoints.INSTANCE.concurrency}, batchCount: ${Endpoints.INSTANCE.batchCount}`
53
- // )
54
- provider = new QueuedStaticJsonRpcProvider(
55
- address,
56
- network,
57
- Endpoints.INSTANCE.concurrency,
58
- Endpoints.INSTANCE.batchCount
59
- )
60
- providers.set(key, provider)
61
- return provider
62
- }
63
-
64
- function getTag(prefix: string, value: any): string {
65
- return (
66
- prefix +
67
- ':' +
68
- JSON.stringify(value, (k, v) => {
69
- if (v == null) {
70
- return 'null'
71
- }
72
- if (typeof v === 'bigint') {
73
- return `bigint:${v.toString()}`
74
- }
75
- if (typeof v === 'string') {
76
- return v.toLowerCase()
77
- }
78
-
79
- // Sort object keys
80
- if (typeof v === 'object' && !Array.isArray(v)) {
81
- const keys = Object.keys(v)
82
- keys.sort()
83
- return keys.reduce(
84
- (accum, key) => {
85
- accum[key] = v[key]
86
- return accum
87
- },
88
- <any>{}
89
- )
90
- }
91
-
92
- return v
93
- })
94
- )
95
- }
96
-
97
- export class QueuedStaticJsonRpcProvider extends JsonRpcProvider {
98
- executor: PQueue
99
- #performCache = new LRUCache<string, Promise<any>>({
100
- max: 300000, // 300k items
101
- maxSize: 500 * 1024 * 1024 // 500mb key size for cache
102
- // ttl: 1000 * 60 * 60, // 1 hour no ttl for better performance
103
- // sizeCalculation: (value: any) => {
104
- // assume each item is 1kb for simplicity
105
- // return 1024
106
- // }
107
- })
108
- #retryCache = new LRUCache<string, number>({
109
- max: 300000 // 300k items
110
- })
111
-
112
- constructor(url: string, network: Network, concurrency: number, batchCount = 1) {
113
- // TODO re-enable match when possible
114
- super(url, network, { staticNetwork: network, batchMaxCount: batchCount })
115
- this.executor = new PQueue({ concurrency: concurrency })
116
- }
117
-
118
- async send(method: string, params: Array<any>): Promise<any> {
119
- if (method !== 'eth_call' || params.length > 2) {
120
- return await this.executor.add(() => super.send(method, params))
121
- }
122
- const tag = getTag(method, params)
123
- const block = params[params.length - 1]
124
- let perform = this.#performCache.get(tag)
125
- if (!perform) {
126
- miss_count.add(1)
127
- const handler = metricsStorage.getStore()
128
- const queued: number = Date.now()
129
- perform = this.executor.add(() => {
130
- const started = Date.now()
131
- processMetrics.processor_rpc_queue_duration.record(started - queued, {
132
- chain_id: this._network.chainId.toString(),
133
- handler
134
- })
135
-
136
- let success = true
137
- return super
138
- .send(method, params)
139
- .catch((e) => {
140
- success = false
141
- throw e
142
- })
143
- .finally(() => {
144
- processMetrics.processor_rpc_duration.record(Date.now() - started, {
145
- chain_id: this._network.chainId.toString(),
146
- handler,
147
- success
148
- })
149
- })
150
- })
151
-
152
- queue_size.record(this.executor.size)
153
-
154
- this.#performCache.set(tag, perform, {
155
- size: tag.length
156
- })
157
- // For non latest block call, we cache permanently, otherwise we cache for one minute
158
- if (block === 'latest') {
159
- setTimeout(() => {
160
- if (this.#performCache.get(tag) === perform) {
161
- this.#performCache.delete(tag)
162
- }
163
- }, 60 * 1000)
164
- }
165
- } else {
166
- hit_count.add(1)
167
- }
168
-
169
- let result
170
- try {
171
- result = await perform
172
- } catch (e: any) {
173
- this.#performCache.delete(tag)
174
- if (e.code === 'TIMEOUT') {
175
- let retryCount = this.#retryCache.get(tag)
176
- if (GLOBAL_CONFIG.execution.rpcRetryTimes && retryCount === undefined) {
177
- retryCount = GLOBAL_CONFIG.execution.rpcRetryTimes
178
- }
179
- if (retryCount) {
180
- this.#retryCache.set(tag, retryCount - 1)
181
- return this.send(method, params)
182
- }
183
- }
184
- throw e
185
- }
186
- if (!result) {
187
- throw Error('Unexpected null response')
188
- }
189
- return result
190
- }
191
-
192
- toString() {
193
- return 'QueuedStaticJsonRpcProvider'
194
- }
195
- }