envio 3.15.0 → 3.16.0
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/README.md +40 -34
- package/evm.schema.json +11 -2
- package/fuel.schema.json +10 -1
- package/index.d.ts +6 -0
- package/licenses/README.md +1 -1
- package/package.json +8 -8
- package/src/Batch.res.mjs +5 -5
- package/src/BatchProcessing.res +1 -0
- package/src/BatchProcessing.res.mjs +1 -0
- package/src/ChainState.res +48 -7
- package/src/ChainState.res.mjs +27 -11
- package/src/Clock.res +57 -0
- package/src/Clock.res.mjs +72 -0
- package/src/Config.res +10 -3
- package/src/Config.res.mjs +4 -2
- package/src/Core.res +4 -40
- package/src/EffectState.res +10 -2
- package/src/EffectState.res.mjs +8 -4
- package/src/EffectState.resi +3 -1
- package/src/EntityId.res +2 -7
- package/src/EntityId.res.mjs +1 -8
- package/src/EntityTables.res +4 -1
- package/src/EntityTables.res.mjs +1 -1
- package/src/FetchState.res +157 -69
- package/src/FetchState.res.mjs +144 -49
- package/src/FinalizeBackfill.res +2 -4
- package/src/FinalizeBackfill.res.mjs +3 -4
- package/src/InMemoryTable.res +17 -16
- package/src/InMemoryTable.res.mjs +14 -15
- package/src/IndexerState.res +28 -4
- package/src/IndexerState.res.mjs +19 -6
- package/src/IndexerState.resi +4 -0
- package/src/Internal.res +0 -49
- package/src/Internal.res.mjs +1 -80
- package/src/LoadLayer.res +14 -8
- package/src/LoadLayer.res.mjs +8 -6
- package/src/Main.res.mjs +1 -1
- package/src/Persistence.res +3 -23
- package/src/Persistence.res.mjs +3 -3
- package/src/Rollback.res +0 -1
- package/src/Rollback.res.mjs +1 -1
- package/src/SafeCheckpointTracking.res +4 -4
- package/src/SafeCheckpointTracking.res.mjs +1 -1
- package/src/StagedRows.res +106 -0
- package/src/StagedRows.res.mjs +102 -0
- package/src/Staging.res +32 -14
- package/src/Staging.res.mjs +17 -20
- package/src/Storage.res +277 -948
- package/src/Storage.res.mjs +215 -707
- package/src/Supervisor.res +75 -56
- package/src/Supervisor.res.mjs +55 -26
- package/src/TestIndexer.res +22 -27
- package/src/TestIndexer.res.mjs +20 -17
- package/src/Throttler.res +15 -8
- package/src/Throttler.res.mjs +9 -6
- package/src/Throttler.resi +1 -1
- package/src/Utils.res +0 -6
- package/src/Utils.res.mjs +0 -5
- package/src/Writing.res +0 -1
- package/src/Writing.res.mjs +1 -1
- package/src/bindings/ClickHouse.res +0 -322
- package/src/bindings/ClickHouse.res.mjs +0 -338
- package/src/bindings/StorageClient.res +107 -174
- package/src/bindings/StorageClient.res.mjs +19 -8
- package/src/bindings/Vitest.res +0 -26
- package/src/bindings/Vitest.res.mjs +0 -3
- package/src/db/Table.res +24 -150
- package/src/db/Table.res.mjs +38 -272
- package/src/sources/ChainSources.res +2 -0
- package/src/sources/ChainSources.res.mjs +2 -2
- package/src/sources/EvmChain.res +3 -0
- package/src/sources/EvmChain.res.mjs +4 -2
- package/src/sources/EvmHyperSyncSource.res +2 -2
- package/src/sources/EvmHyperSyncSource.res.mjs +1 -1
- package/src/sources/EvmRpcClient.res +13 -3
- package/src/sources/EvmRpcClient.res.mjs +4 -3
- package/src/sources/EvmRpcWs.res +4 -3
- package/src/sources/EvmRpcWs.res.mjs +2 -2
- package/src/sources/HeightFeed.res +31 -10
- package/src/sources/HeightFeed.res.mjs +10 -6
- package/src/sources/HeightStream.res +32 -17
- package/src/sources/HeightStream.res.mjs +10 -10
- package/src/sources/HyperSyncSSE.res +4 -6
- package/src/sources/HyperSyncSSE.res.mjs +2 -2
- package/src/sources/RpcSource.res +10 -1
- package/src/sources/RpcSource.res.mjs +4 -3
- package/src/sources/Source.res +3 -0
- package/src/sources/SourceManager.res +84 -77
- package/src/sources/SourceManager.res.mjs +17 -20
- package/src/sources/SourceManager.resi +3 -4
- package/src/sources/StartBlockResolver.res +1 -0
- package/src/sources/StartBlockResolver.res.mjs +3 -2
- package/src/sources/SvmHyperSyncSource.res +2 -2
- package/src/sources/SvmHyperSyncSource.res.mjs +1 -1
- package/svm.schema.json +11 -2
- package/src/PgWriting.res +0 -36
- package/src/PgWriting.res.mjs +0 -34
- package/src/db/IndexDefinition.res +0 -47
- package/src/db/IndexDefinition.res.mjs +0 -67
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<h1>HyperIndex
|
|
3
|
-
<p><strong>
|
|
2
|
+
<h1>HyperIndex</h1>
|
|
3
|
+
<p><strong>Build custom blockchain APIs and analytics. Real-time, with enterprise SLAs.</strong></p>
|
|
4
4
|
<p>
|
|
5
5
|
<a href="https://github.com/enviodev/hyperindex/releases"><img src="https://img.shields.io/github/release/enviodev/hyperindex.svg" alt="GitHub release" /></a>
|
|
6
6
|
<a href="https://github.com/enviodev/hyperindex/issues"><img src="https://img.shields.io/github/issues/enviodev/hyperindex.svg" alt="GitHub issues" /></a>
|
|
@@ -10,18 +10,20 @@
|
|
|
10
10
|
</p>
|
|
11
11
|
<p>
|
|
12
12
|
<a href="https://docs.envio.dev">Documentation</a> ·
|
|
13
|
-
<a href="https://envio.dev">
|
|
13
|
+
<a href="https://envio.dev">Envio Cloud</a> ·
|
|
14
|
+
<a href="https://github.com/enviodev/open-indexer-benchmark">Benchmark</a> ·
|
|
15
|
+
<a href="https://docs.envio.dev/docs/HyperIndex/quickstart-with-ai">Build with AI</a> ·
|
|
14
16
|
<a href="https://discord.gg/DhfFhzuJQh">Discord</a> ·
|
|
15
|
-
<a href="https://
|
|
17
|
+
<a href="https://envio.dev/pricing">Contact sales</a>
|
|
16
18
|
</p>
|
|
17
19
|
</div>
|
|
18
20
|
|
|
19
21
|
---
|
|
20
22
|
## What is HyperIndex?
|
|
21
23
|
|
|
22
|
-
HyperIndex is Envio's
|
|
24
|
+
HyperIndex is Envio's blockchain indexing framework. Choose the contracts and events to track, write TypeScript handlers that shape the data, and HyperIndex keeps it up to date across EVM chains, Solana and Fuel, handles chain reorgs, and serves the result as a GraphQL API or direct Postgres/ClickHouse SQL.
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
It reads chain data through [HyperSync](https://docs.envio.dev/docs/HyperSync/overview), Envio's blockchain data API, which is up to 2,000x faster than JSON-RPC. Run it yourself with Docker, or deploy to [Envio Cloud](https://docs.envio.dev/docs/HyperIndex/hosted-service). Cloud starts with a free development plan and scales with your project up to Dedicated infrastructure with a 99.99% uptime SLA.
|
|
25
27
|
|
|
26
28
|

|
|
27
29
|
|
|
@@ -31,12 +33,9 @@ Powered by [HyperSync](https://docs.envio.dev/docs/HyperSync/overview), Envio's
|
|
|
31
33
|
|
|
32
34
|
Most blockchain indexers are bottlenecked by RPC. HyperIndex isn't. HyperSync, the data engine underneath, is a purpose-built, Rust-based query layer that retrieves multiple blocks per round trip, cuts out the overhead, and makes historical backfills genuinely fast.
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
The [open indexer benchmark](https://github.com/enviodev/open-indexer-benchmark) tracks current speed results across scenarios, and HyperIndex passes every reliability check. Every number there comes from code in the repository, so you can run it yourself.
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
- **LBTC Token with RPC calls** (April 2025): HyperIndex completed in 3 minutes vs 3 hours 9 minutes for The Graph
|
|
38
|
-
|
|
39
|
-
[View full benchmark results →](https://docs.envio.dev/docs/HyperIndex/benchmarks)
|
|
38
|
+
[How the benchmarks work →](https://docs.envio.dev/docs/HyperIndex/benchmarks)
|
|
40
39
|
|
|
41
40
|
---
|
|
42
41
|
|
|
@@ -44,15 +43,14 @@ Independent benchmarks run by Sentio confirm it:
|
|
|
44
43
|
|
|
45
44
|
HyperIndex and HyperSync are the data layer for DeFi dashboards, protocol analytics, block explorers, stablecoin monitors, liquidation trackers, oracle comparisons, NFT explorers, and any application that needs fast, structured onchain data.
|
|
46
45
|
|
|
47
|
-
A few
|
|
46
|
+
A few apps built with them, from the showcase:
|
|
48
47
|
|
|
49
|
-
- [v4.xyz](https://v4.xyz): the hub for Uniswap V4 data and analytics,
|
|
50
|
-
- [Stable Volume](https://www.stablevolume.com/): real-time stablecoin transaction monitoring across
|
|
48
|
+
- [v4.xyz](https://v4.xyz): the hub for Uniswap V4 data and analytics, indexed in real time across chains
|
|
49
|
+
- [Stable Volume](https://www.stablevolume.com/): real-time stablecoin transaction monitoring across chains
|
|
51
50
|
- [Stable Radar](https://stable-radar.com): real-time USDC transaction dashboard across multiple chains
|
|
52
51
|
- [Liqo](https://liqo.xyz): multichain liquidation tracking for DeFi lending protocols
|
|
53
|
-
- [Safe Stats](https://safe-stats.vercel.app/): real-time analytics for Safe multisig activity across all chains
|
|
54
52
|
- [Oracle Wars](https://oraclewars.xyz/): real-time oracle price comparison across multiple oracles
|
|
55
|
-
- [Chain Density](https://chaindensity.xyz/): transaction and event density analysis for any address across
|
|
53
|
+
- [Chain Density](https://chaindensity.xyz/): transaction and event density analysis for any address across chains
|
|
56
54
|
- [LogTUI](https://www.npmjs.com/package/logtui): terminal UI for monitoring blockchain events in real-time
|
|
57
55
|
|
|
58
56
|
[See the full showcase →](https://docs.envio.dev/showcase)
|
|
@@ -62,34 +60,33 @@ A few things already running in production:
|
|
|
62
60
|
## Key Features
|
|
63
61
|
|
|
64
62
|
**Performance**
|
|
65
|
-
- Historical backfills at
|
|
66
|
-
- Up to
|
|
67
|
-
- Sync times reduced from days to minutes
|
|
63
|
+
- Historical backfills at 100,000+ events per second ([benchmarking](https://docs.envio.dev/docs/HyperIndex/benchmarking))
|
|
64
|
+
- Up to 2,000x faster than JSON-RPC via HyperSync (on by default, no config required)
|
|
68
65
|
- Fallback RPC support for reliability without touching your indexer code
|
|
69
66
|
|
|
70
67
|
**Multichain indexing**
|
|
71
68
|
- Index EVM, SVM, and Fuel blockchains from a single indexer
|
|
72
|
-
-
|
|
69
|
+
- Every chain on the [HyperSync network list](https://docs.envio.dev/docs/HyperSync/hypersync-supported-networks), plus any EVM chain via RPC
|
|
73
70
|
- Real-time indexing with reorg handling built in
|
|
74
71
|
|
|
75
72
|
**Developer experience**
|
|
76
73
|
- Auto-generate an indexer directly from a smart contract address or ABI, no manual setup required
|
|
77
|
-
- Write handlers in TypeScript, JavaScript
|
|
74
|
+
- Write handlers in TypeScript or ReScript, and plain JavaScript handler files load too
|
|
78
75
|
- Full local development environment with Docker
|
|
79
76
|
- GraphQL API generated automatically from your schema
|
|
80
77
|
- Wildcard topic indexing: index by event signatures across any contract, not just specified addresses
|
|
81
|
-
- Factory contract support for
|
|
78
|
+
- Factory contract support for dynamically registered contracts
|
|
82
79
|
- Onchain and off-chain data integration
|
|
83
80
|
- External API actions triggered by blockchain events
|
|
84
81
|
- Detailed logging and error reporting
|
|
85
82
|
|
|
86
83
|
**Deployment**
|
|
87
|
-
-
|
|
84
|
+
- [Envio Cloud](https://docs.envio.dev/docs/HyperIndex/hosted-service), managed hosting with static endpoints and built-in alerts, from a free development plan up to Dedicated deployments with an SLA of up to 99.99% uptime
|
|
88
85
|
- Self-hosted via Docker
|
|
89
86
|
- No vendor lock-in. Switch between HyperSync and RPC at any time
|
|
90
87
|
|
|
91
88
|
**Agentic development**
|
|
92
|
-
-
|
|
89
|
+
- Build and deploy with an AI agent using the Envio Cloud CLI (`envio-cloud`) and the [HyperIndex skills](https://github.com/enviodev/hyperindex/tree/main/packages/cli/templates/static/shared/.claude/skills) that `envio init` adds to every project
|
|
93
90
|
- An agent can scaffold, configure, and deploy a production-ready indexer without touching a config file. [400,000 events indexed in ~20 seconds](https://docs.envio.dev/blog/agentic-blockchain-indexing-envio-hyperindex)
|
|
94
91
|
- Powerful built-in testing framework purpose-built for AI-driven development. Agents can write, run, and iterate on handler tests in a tight feedback loop, catching regressions before deploy
|
|
95
92
|
|
|
@@ -97,7 +94,7 @@ A few things already running in production:
|
|
|
97
94
|
|
|
98
95
|
## Getting started
|
|
99
96
|
|
|
100
|
-
**Requirements**: Node.js, Docker
|
|
97
|
+
**Requirements**: Node.js 22.15 or newer, and Docker for local development. pnpm is recommended.
|
|
101
98
|
|
|
102
99
|
```bash
|
|
103
100
|
pnpx envio init
|
|
@@ -119,7 +116,7 @@ From there, three files define your indexer:
|
|
|
119
116
|
|
|
120
117
|
HyperSync is the data engine that makes HyperIndex fast. It's active by default for all supported networks, no configuration needed.
|
|
121
118
|
|
|
122
|
-
Instead of making individual RPC calls per block, HyperSync retrieves multiple data points per round trip with advanced filtering. The result
|
|
119
|
+
Instead of making individual RPC calls per block, HyperSync retrieves multiple data points per round trip with advanced filtering. The result is sync speeds up to 2,000x faster than JSON-RPC, lower infrastructure costs, and no rate limits to work around on supported networks.
|
|
123
120
|
|
|
124
121
|
HyperSync can also be used directly for custom data pipelines in Python, Rust, Node.js, and Go, independent of HyperIndex.
|
|
125
122
|
|
|
@@ -129,19 +126,17 @@ HyperSync can also be used directly for custom data pipelines in Python, Rust, N
|
|
|
129
126
|
|
|
130
127
|
## Supported networks
|
|
131
128
|
|
|
132
|
-
HyperIndex supports any EVM-compatible L1, L2, or L3, plus Fuel and Solana (
|
|
129
|
+
HyperIndex supports any EVM-compatible L1, L2, or L3, plus Fuel and Solana. Chains on the [HyperSync network list](https://docs.envio.dev/docs/HyperSync/hypersync-supported-networks) get first-class HyperSync support. For any EVM chain not on the HyperSync list, RPC-based indexing works out of the box. Solana indexing runs on HyperSync for Solana, with instruction-level handlers and IDL-aware decoding.
|
|
133
130
|
|
|
134
131
|
[Full network list →](https://docs.envio.dev/docs/HyperIndex/supported-networks)
|
|
135
132
|
|
|
136
133
|
---
|
|
137
134
|
|
|
138
|
-
## Migrating
|
|
139
|
-
|
|
140
|
-
HyperIndex has a dedicated migration guide that covers config conversion, schema mapping, and query differences in 3 steps. Envio also offers white-glove migration support. Reach out on Discord and the team will help you get set up.
|
|
135
|
+
## Migrating an existing indexer
|
|
141
136
|
|
|
142
|
-
|
|
137
|
+
The migration guides cover config conversion, schema mapping and query differences, and you can [migrate with an AI agent](https://docs.envio.dev/docs/HyperIndex/migrate-with-ai). If you run subgraphs, Envio can also migrate and host them for you with Subgraph Hosting, while your applications keep querying a subgraph-compatible endpoint. For hands-on help, see [services](https://envio.dev/pricing/services) or ask on Discord.
|
|
143
138
|
|
|
144
|
-
[Migration
|
|
139
|
+
[Migration guides →](https://docs.envio.dev/docs/HyperIndex/migration-guide)
|
|
145
140
|
|
|
146
141
|
---
|
|
147
142
|
|
|
@@ -155,16 +150,27 @@ Key sections:
|
|
|
155
150
|
- [Multichain Indexing](https://docs.envio.dev/docs/HyperIndex/multichain-indexing)
|
|
156
151
|
- [Wildcard Indexing](https://docs.envio.dev/docs/HyperIndex/wildcard-indexing)
|
|
157
152
|
- [Reorg Support](https://docs.envio.dev/docs/HyperIndex/reorgs-support)
|
|
158
|
-
- [
|
|
153
|
+
- [Envio Cloud](https://docs.envio.dev/docs/HyperIndex/hosted-service)
|
|
159
154
|
- [HyperSync as Data Source](https://docs.envio.dev/docs/HyperIndex/hypersync)
|
|
160
155
|
- [Migration Guide](https://docs.envio.dev/docs/HyperIndex/migration-guide)
|
|
161
156
|
|
|
162
157
|
---
|
|
163
158
|
|
|
159
|
+
## Build with an AI agent
|
|
160
|
+
|
|
161
|
+
HyperIndex works with Claude Code, Cursor, Codex and other coding agents.
|
|
162
|
+
|
|
163
|
+
- **Docs for agents.** Point your agent at [llms.txt](https://docs.envio.dev/llms.txt), or the full text at [llms-full.txt](https://docs.envio.dev/llms-full.txt).
|
|
164
|
+
- **Docs MCP server.** Add `https://docs.envio.dev/mcp` to your agent for `docs_search` and `docs_fetch`. See the [MCP server guide](https://docs.envio.dev/docs/HyperIndex/mcp-server).
|
|
165
|
+
- **Skills.** `pnpx envio init` adds the [HyperIndex skills](https://github.com/enviodev/hyperindex/tree/main/packages/cli/templates/static/shared/.claude/skills) to every new project. After upgrading `envio`, run `pnpm envio skills update` to refresh them.
|
|
166
|
+
- **Quickstart.** Follow [Quickstart with AI](https://docs.envio.dev/docs/HyperIndex/quickstart-with-ai) to go from a contract address to a deployed GraphQL API.
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
164
170
|
## Community and support
|
|
165
171
|
|
|
166
172
|
- Follow us on [X](https://twitter.com/envio_indexer)
|
|
167
|
-
- Join the [Discord](https://discord.gg/DhfFhzuJQh), fastest
|
|
173
|
+
- Join the [Discord](https://discord.gg/DhfFhzuJQh) or [Telegram](https://t.me/+BeS5ihVUFONjNGFk), the fastest ways to get help
|
|
168
174
|
- Open an issue on [GitHub](https://github.com/enviodev/hyperindex/issues/new/choose)
|
|
169
175
|
- Browse [common issues](https://docs.envio.dev/docs/common-issues) for quick troubleshooting
|
|
170
176
|
|
package/evm.schema.json
CHANGED
|
@@ -289,7 +289,7 @@
|
|
|
289
289
|
"type": "string"
|
|
290
290
|
},
|
|
291
291
|
"name": {
|
|
292
|
-
"description": "Name of the event in the
|
|
292
|
+
"description": "Name of the event in the generated types. When ommitted, the event field will be used. Should be unique per contract",
|
|
293
293
|
"type": [
|
|
294
294
|
"string",
|
|
295
295
|
"null"
|
|
@@ -421,7 +421,7 @@
|
|
|
421
421
|
]
|
|
422
422
|
},
|
|
423
423
|
"rpc": {
|
|
424
|
-
"description": "RPC configuration for your indexer. If not specified otherwise, for chains supported by HyperSync, RPC serves as a fallback for added reliability. For others, it acts as the primary data-source. HyperSync offers significant performance improvements, up to
|
|
424
|
+
"description": "RPC configuration for your indexer. If not specified otherwise, for chains supported by HyperSync, RPC serves as a fallback for added reliability. For others, it acts as the primary data-source. HyperSync offers significant performance improvements, up to 2000x faster than traditional RPC.",
|
|
425
425
|
"anyOf": [
|
|
426
426
|
{
|
|
427
427
|
"$ref": "#/$defs/RpcSelection"
|
|
@@ -460,6 +460,15 @@
|
|
|
460
460
|
"format": "uint32",
|
|
461
461
|
"minimum": 0
|
|
462
462
|
},
|
|
463
|
+
"block_throttle": {
|
|
464
|
+
"description": "Once caught up, wait for this many new blocks before fetching them. Delays new events by up to as many blocks.",
|
|
465
|
+
"type": [
|
|
466
|
+
"integer",
|
|
467
|
+
"null"
|
|
468
|
+
],
|
|
469
|
+
"format": "uint32",
|
|
470
|
+
"minimum": 0
|
|
471
|
+
},
|
|
463
472
|
"start_block": {
|
|
464
473
|
"description": "The block at which the indexer should start ingesting data, or \"latest\" to start from the chain's current head block when the indexer is first deployed. Once resolved, the concrete block is persisted and reused every time the indexer resumes normally (for example recovering from a crash), so downtime is backfilled instead of skipped. Running `envio start`/`dev` with -r (--restart) resets this like any other config change: \"latest\" resolves again, against the head at that time.",
|
|
465
474
|
"$ref": "#/$defs/StartBlock"
|
package/fuel.schema.json
CHANGED
|
@@ -241,7 +241,7 @@
|
|
|
241
241
|
"type": "object",
|
|
242
242
|
"properties": {
|
|
243
243
|
"name": {
|
|
244
|
-
"description": "Name of the event in the
|
|
244
|
+
"description": "Name of the event in the generated types",
|
|
245
245
|
"type": "string"
|
|
246
246
|
},
|
|
247
247
|
"type": {
|
|
@@ -336,6 +336,15 @@
|
|
|
336
336
|
"format": "uint32",
|
|
337
337
|
"minimum": 0
|
|
338
338
|
},
|
|
339
|
+
"block_throttle": {
|
|
340
|
+
"description": "Once caught up, wait for this many new blocks before fetching them. Delays new events by up to as many blocks.",
|
|
341
|
+
"type": [
|
|
342
|
+
"integer",
|
|
343
|
+
"null"
|
|
344
|
+
],
|
|
345
|
+
"format": "uint32",
|
|
346
|
+
"minimum": 0
|
|
347
|
+
},
|
|
339
348
|
"contracts": {
|
|
340
349
|
"description": "All the contracts that should be indexed on the given chain",
|
|
341
350
|
"type": [
|
package/index.d.ts
CHANGED
|
@@ -398,6 +398,8 @@ type EvmChainConfig<Id extends number = number> = {
|
|
|
398
398
|
readonly maxReorgDepth?: number;
|
|
399
399
|
/** Number of blocks behind the chain head to lag (default: 0). */
|
|
400
400
|
readonly blockLag?: number;
|
|
401
|
+
/** Once caught up, wait for this many new blocks before fetching them. Delays new events (default: 0). */
|
|
402
|
+
readonly blockThrottle?: number;
|
|
401
403
|
};
|
|
402
404
|
|
|
403
405
|
/** EVM chain value (for runtime Indexer). */
|
|
@@ -454,6 +456,8 @@ type FuelChainConfig<Id extends number = number> = {
|
|
|
454
456
|
readonly maxReorgDepth?: number;
|
|
455
457
|
/** Number of blocks behind the chain head to lag (default: 0). */
|
|
456
458
|
readonly blockLag?: number;
|
|
459
|
+
/** Once caught up, wait for this many new blocks before fetching them. Delays new events (default: 0). */
|
|
460
|
+
readonly blockThrottle?: number;
|
|
457
461
|
};
|
|
458
462
|
|
|
459
463
|
/** Fuel chain value (for runtime Indexer). */
|
|
@@ -490,6 +494,8 @@ type SvmChainConfig<Id extends number = number> = {
|
|
|
490
494
|
readonly maxReorgDepth?: number;
|
|
491
495
|
/** Number of blocks behind the chain head to lag (default: 0). */
|
|
492
496
|
readonly blockLag?: number;
|
|
497
|
+
/** Once caught up, wait for this many new slots before fetching them. Delays new instructions (default: 0). */
|
|
498
|
+
readonly blockThrottle?: number;
|
|
493
499
|
};
|
|
494
500
|
|
|
495
501
|
/** SVM chain value (for runtime Indexer). */
|
package/licenses/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
Envio's licensing reflects open source ethos but is not OSI recognized. Developers can use Envio's services without vendor lock-in, either by self-hosting or specifying an RPC URL. The
|
|
5
|
+
Envio's licensing reflects open source ethos but is not OSI recognized. Developers can use Envio's services without vendor lock-in, either by self-hosting or specifying an RPC URL. The code in this repository is public.
|
|
6
6
|
|
|
7
7
|
Our licenses allows self-hosting but restricts third-party hosting with Envio's hosted service. Envio may consider open-sourcing in the future but prioritizes stakeholder interests and market traction. Please reach out to us on discord if you would like to explore hosting indexers as a third-party.
|
|
8
8
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "envio",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A latency and sync speed optimized, developer friendly blockchain data indexer.",
|
|
6
6
|
"bin": "./bin.mjs",
|
|
@@ -43,16 +43,16 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"bignumber.js": "9.3.1",
|
|
45
45
|
"eventsource": "4.1.0",
|
|
46
|
-
"express": "4.
|
|
47
|
-
"@rescript/runtime": "12.
|
|
46
|
+
"express": "4.22.3",
|
|
47
|
+
"@rescript/runtime": "12.3.1",
|
|
48
48
|
"rescript-schema": "9.5.1",
|
|
49
49
|
"dotenv": "16.4.5"
|
|
50
50
|
},
|
|
51
51
|
"optionalDependencies": {
|
|
52
|
-
"envio-linux-x64": "3.
|
|
53
|
-
"envio-linux-x64-musl": "3.
|
|
54
|
-
"envio-linux-arm64": "3.
|
|
55
|
-
"envio-darwin-x64": "3.
|
|
56
|
-
"envio-darwin-arm64": "3.
|
|
52
|
+
"envio-linux-x64": "3.16.0",
|
|
53
|
+
"envio-linux-x64-musl": "3.16.0",
|
|
54
|
+
"envio-linux-arm64": "3.16.0",
|
|
55
|
+
"envio-darwin-x64": "3.16.0",
|
|
56
|
+
"envio-darwin-arm64": "3.16.0"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/Batch.res.mjs
CHANGED
|
@@ -109,9 +109,9 @@ function make(sequence, frontier, chainsBeforeBatch, batchSizeTarget, history) {
|
|
|
109
109
|
chainEventsProcessed = chainEventsProcessed + 1;
|
|
110
110
|
}
|
|
111
111
|
if (blockNumber !== prevBlockNumber) {
|
|
112
|
-
let chainId = fetchState.chainId;
|
|
113
|
-
let fromBlockExclusive = prevBlockNumber;
|
|
114
112
|
let scannedHashes = chainBeforeBatch.scannedHashes;
|
|
113
|
+
let fromBlockExclusive = prevBlockNumber;
|
|
114
|
+
let chainId = fetchState.chainId;
|
|
115
115
|
if (chainBeforeBatch.shouldRollbackOnReorg) {
|
|
116
116
|
let blockNumbers = scannedHashes.blockNumbers;
|
|
117
117
|
let length = blockNumbers.length;
|
|
@@ -154,10 +154,10 @@ function make(sequence, frontier, chainsBeforeBatch, batchSizeTarget, history) {
|
|
|
154
154
|
};
|
|
155
155
|
}
|
|
156
156
|
let progressBlockNumberAfterBatch = FetchState.getProgressBlockNumberAt(fetchState, chainBatchSize);
|
|
157
|
-
let chainId$1 = fetchState.chainId;
|
|
158
|
-
let toBlockExclusive = progressBlockNumberAfterBatch + 1;
|
|
159
|
-
let fromBlockExclusive$1 = prevBlockNumber;
|
|
160
157
|
let scannedHashes$1 = chainBeforeBatch.scannedHashes;
|
|
158
|
+
let fromBlockExclusive$1 = prevBlockNumber;
|
|
159
|
+
let toBlockExclusive = progressBlockNumberAfterBatch + 1;
|
|
160
|
+
let chainId$1 = fetchState.chainId;
|
|
161
161
|
if (chainBeforeBatch.shouldRollbackOnReorg) {
|
|
162
162
|
let blockNumbers$1 = scannedHashes$1.blockNumbers;
|
|
163
163
|
let length$1 = blockNumbers$1.length;
|
package/src/BatchProcessing.res
CHANGED
|
@@ -108,6 +108,7 @@ async function startProcessing(state, runtime, scheduleFetch, scheduleRollback)
|
|
|
108
108
|
}
|
|
109
109
|
};
|
|
110
110
|
IndexerState.endProcessing(state);
|
|
111
|
+
IndexerState.reportArrivalAtHead(state);
|
|
111
112
|
if (IndexerState.isResolvingReorg(state)) {
|
|
112
113
|
return scheduleRollback();
|
|
113
114
|
} else if (!IndexerState.isStopped(state) && !Object.values(IndexerState.chainStates(state)).every(ChainState.isFetchingAtHead)) {
|
package/src/ChainState.res
CHANGED
|
@@ -308,11 +308,16 @@ let makeInternal = (
|
|
|
308
308
|
// FIXME: Shouldn't set with full history
|
|
309
309
|
// The resumed depth, not the config one: the pre-threshold lag must match
|
|
310
310
|
// the window reorg detection compares, or a reduced config depth would let
|
|
311
|
-
// fetching enter the stored rollback window without history.
|
|
311
|
+
// fetching enter the stored rollback window without history. One block
|
|
312
|
+
// more than the window, so the last pre-threshold page ends below it and
|
|
313
|
+
// the window's first block is read by a page that knows it is reorgable.
|
|
312
314
|
~blockLag=Pervasives.max(
|
|
313
|
-
!config.shouldRollbackOnReorg || isInReorgThreshold
|
|
315
|
+
!config.shouldRollbackOnReorg || isInReorgThreshold || maxReorgDepth == 0
|
|
316
|
+
? 0
|
|
317
|
+
: maxReorgDepth + 1,
|
|
314
318
|
chainConfig.blockLag,
|
|
315
319
|
),
|
|
320
|
+
~blockThrottle=chainConfig.blockThrottle,
|
|
316
321
|
~onBlockRegistrations,
|
|
317
322
|
~firstEventBlock,
|
|
318
323
|
// Fuel and SVM route through the address store like EVM does, so the
|
|
@@ -349,6 +354,7 @@ let makeInternal = (
|
|
|
349
354
|
~blockStore,
|
|
350
355
|
~transactionStore,
|
|
351
356
|
~logger,
|
|
357
|
+
~maxAddressesPerRequest=config.maxAddrInPartition,
|
|
352
358
|
)
|
|
353
359
|
|
|
354
360
|
// Seed the stored reorg checkpoints (hash-only rows) so detection resumes
|
|
@@ -776,10 +782,10 @@ let takeFinished = (cs: t) =>
|
|
|
776
782
|
}
|
|
777
783
|
}
|
|
778
784
|
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
785
|
+
// The block reorg detection compares from is itself in the window, so the
|
|
786
|
+
// highest block below it is the one before, where catch-up stops.
|
|
787
|
+
let getHighestBlockBelowThreshold = (cs: t): int =>
|
|
788
|
+
Pervasives.max(cs.fetchState.knownHeight - cs.maxReorgDepth - 1, 0)
|
|
783
789
|
|
|
784
790
|
// True once the fetch frontier has reached the head/endBlock for this chain.
|
|
785
791
|
let isFetchingAtHead = (cs: t) => cs.fetchState->FetchState.isFetchingAtHead
|
|
@@ -977,6 +983,34 @@ let materializePageItems = async (
|
|
|
977
983
|
))
|
|
978
984
|
}
|
|
979
985
|
|
|
986
|
+
// Near the head: each new block in as few queries as possible, of a shape that
|
|
987
|
+
// stays the same while contracts keep being registered. Once every partition is
|
|
988
|
+
// close enough, the chain's dynamic contracts leave the server-side address
|
|
989
|
+
// filter for the rest of the run.
|
|
990
|
+
//
|
|
991
|
+
// Judged only when the chain learns a newer head. An RPC response repeats the
|
|
992
|
+
// height its query went out with, which can be hours old by the end of a
|
|
993
|
+
// backfill: judged against it, every block produced since would be fetched
|
|
994
|
+
// address-free.
|
|
995
|
+
let switchDynamicContractsToClientSideFilteringNearHead = (cs: t) =>
|
|
996
|
+
if (
|
|
997
|
+
!cs.fetchState.clientFilterPolicy.allDynamicContracts &&
|
|
998
|
+
cs.fetchState->FetchState.isWithinHeadClientFilteringDistance
|
|
999
|
+
) {
|
|
1000
|
+
cs.fetchState =
|
|
1001
|
+
cs.fetchState->FetchState.switchDynamicContractsToClientSideFiltering(
|
|
1002
|
+
~addressStore=cs.addressStore,
|
|
1003
|
+
)
|
|
1004
|
+
if cs.onEventRegistrations->Array.some(registration => registration.contractRegister !== None) {
|
|
1005
|
+
cs.logger->Logger.trace(
|
|
1006
|
+
"Near the head: dynamic contracts are fetched without their addresses for the rest of the run, and filtered on our side.",
|
|
1007
|
+
~params={
|
|
1008
|
+
"contractNames": cs.fetchState.optimizedPartitions.dynamicContracts->Utils.Set.toArray,
|
|
1009
|
+
},
|
|
1010
|
+
)
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
980
1014
|
let handleQueryResult = (
|
|
981
1015
|
cs: t,
|
|
982
1016
|
~query: FetchState.query,
|
|
@@ -1000,6 +1034,7 @@ let handleQueryResult = (
|
|
|
1000
1034
|
)
|
|
1001
1035
|
}
|
|
1002
1036
|
|
|
1037
|
+
let previousKnownHeight = cs.fetchState.knownHeight
|
|
1003
1038
|
cs.fetchState =
|
|
1004
1039
|
fs
|
|
1005
1040
|
->FetchState.handleQueryResult(~query, ~latestFetchedBlock, ~newItems)
|
|
@@ -1007,6 +1042,9 @@ let handleQueryResult = (
|
|
|
1007
1042
|
|
|
1008
1043
|
// The query is no longer in flight, so release its reservation.
|
|
1009
1044
|
cs.pendingBudget = Pervasives.max(0., cs.pendingBudget -. query.itemsEst->Int.toFloat)
|
|
1045
|
+
if cs.fetchState.knownHeight > previousKnownHeight {
|
|
1046
|
+
cs->switchDynamicContractsToClientSideFilteringNearHead
|
|
1047
|
+
}
|
|
1010
1048
|
}
|
|
1011
1049
|
|
|
1012
1050
|
// Run reorg detection against a fetch response by merging its block-store page
|
|
@@ -1041,8 +1079,11 @@ let prepareReorg = (cs: t, ~eventsProcessedDiff) => {
|
|
|
1041
1079
|
cs->resetPendingQueries
|
|
1042
1080
|
}
|
|
1043
1081
|
|
|
1044
|
-
|
|
1082
|
+
// A chain waiting at its head goes address-free before querying the new block.
|
|
1083
|
+
let updateKnownHeight = (cs: t, ~knownHeight) => {
|
|
1045
1084
|
cs.fetchState = cs.fetchState->FetchState.updateKnownHeight(~knownHeight)
|
|
1085
|
+
cs->switchDynamicContractsToClientSideFilteringNearHead
|
|
1086
|
+
}
|
|
1046
1087
|
|
|
1047
1088
|
// In auto-exit mode, pin the endBlock to the earliest observed event block.
|
|
1048
1089
|
let setEndBlockToFirstEvent = (cs: t, ~blockNumber) =>
|
package/src/ChainState.res.mjs
CHANGED
|
@@ -188,7 +188,7 @@ function makeFromDbState(chainConfig, resumedChainState, reorgCheckpoints, isInR
|
|
|
188
188
|
tmp = undefined;
|
|
189
189
|
break;
|
|
190
190
|
}
|
|
191
|
-
let fetchState = FetchState.make(startBlock, endBlock, onEventRegistrations, addressStore, addressRows, config.maxAddrInPartition, chainConfig.id, notice => logFetchStateNotice(logger$1, notice), (config.batchSize << 1), knownHeight, progressBlockNumber, onBlockRegistrations, Primitive_int.max(!config.shouldRollbackOnReorg || isInReorgThreshold ? 0 : maxReorgDepth, chainConfig.blockLag), Primitive_option.some(firstEventBlock), Primitive_option.some(tmp), isResumed);
|
|
191
|
+
let fetchState = FetchState.make(startBlock, endBlock, onEventRegistrations, addressStore, addressRows, config.maxAddrInPartition, chainConfig.id, notice => logFetchStateNotice(logger$1, notice), (config.batchSize << 1), knownHeight, progressBlockNumber, onBlockRegistrations, Primitive_int.max(!config.shouldRollbackOnReorg || isInReorgThreshold || maxReorgDepth === 0 ? 0 : maxReorgDepth + 1 | 0, chainConfig.blockLag), chainConfig.blockThrottle, Primitive_option.some(firstEventBlock), Primitive_option.some(tmp), isResumed);
|
|
192
192
|
let chainReorgCheckpoints = Stdlib_Array.filterMap(reorgCheckpoints, reorgCheckpoint => {
|
|
193
193
|
if (reorgCheckpoint.chain_id === chainConfig.id) {
|
|
194
194
|
return reorgCheckpoint;
|
|
@@ -196,7 +196,7 @@ function makeFromDbState(chainConfig, resumedChainState, reorgCheckpoints, isInR
|
|
|
196
196
|
});
|
|
197
197
|
let blockStore = BlockStore.make(config.ecosystem.name);
|
|
198
198
|
let transactionStore = TransactionStore.make(config.ecosystem.name);
|
|
199
|
-
let sources = ChainSources.make(chainConfig, onEventRegistrations, onBlockRegistrations, addressStore, lowercaseAddresses, blockStore, transactionStore, logger$1);
|
|
199
|
+
let sources = ChainSources.make(chainConfig, onEventRegistrations, onBlockRegistrations, addressStore, lowercaseAddresses, blockStore, transactionStore, logger$1, config.maxAddrInPartition);
|
|
200
200
|
if (Utils.$$Array.notEmpty(chainReorgCheckpoints)) {
|
|
201
201
|
let seedPage = BlockStore.fromJs(chainReorgCheckpoints.map(cp => ({
|
|
202
202
|
blockNumber: cp.block_number,
|
|
@@ -494,12 +494,7 @@ function takeFinished(cs) {
|
|
|
494
494
|
}
|
|
495
495
|
|
|
496
496
|
function getHighestBlockBelowThreshold(cs) {
|
|
497
|
-
|
|
498
|
-
if (highestBlockBelowThreshold < 0) {
|
|
499
|
-
return 0;
|
|
500
|
-
} else {
|
|
501
|
-
return highestBlockBelowThreshold;
|
|
502
|
-
}
|
|
497
|
+
return Primitive_int.max((cs.fetchState.knownHeight - cs.maxReorgDepth | 0) - 1 | 0, 0);
|
|
503
498
|
}
|
|
504
499
|
|
|
505
500
|
function isFetchingAtHead(cs) {
|
|
@@ -647,10 +642,27 @@ async function materializePageItems(items, transactionStore, blockStore, shouldC
|
|
|
647
642
|
]);
|
|
648
643
|
}
|
|
649
644
|
|
|
645
|
+
function switchDynamicContractsToClientSideFilteringNearHead(cs) {
|
|
646
|
+
if (!cs.fetchState.clientFilterPolicy.allDynamicContracts && FetchState.isWithinHeadClientFilteringDistance(cs.fetchState)) {
|
|
647
|
+
cs.fetchState = FetchState.switchDynamicContractsToClientSideFiltering(cs.fetchState, cs.addressStore);
|
|
648
|
+
if (cs.onEventRegistrations.some(registration => registration.contractRegister !== undefined)) {
|
|
649
|
+
return Logger.trace(cs.logger, "Near the head: dynamic contracts are fetched without their addresses for the rest of the run, and filtered on our side.", {
|
|
650
|
+
contractNames: Array.from(cs.fetchState.optimizedPartitions.dynamicContracts)
|
|
651
|
+
});
|
|
652
|
+
} else {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
650
658
|
function handleQueryResult(cs, query, newItems, newRegistrations, latestFetchedBlock, knownHeight) {
|
|
651
659
|
let fs = newRegistrations.length !== 0 ? FetchState.registerDynamicContracts(cs.fetchState, cs.addressStore, notice => logFetchStateNotice(cs.logger, notice), Primitive_int.max(knownHeight, latestFetchedBlock), newRegistrations) : cs.fetchState;
|
|
660
|
+
let previousKnownHeight = cs.fetchState.knownHeight;
|
|
652
661
|
cs.fetchState = FetchState.updateKnownHeight(FetchState.handleQueryResult(fs, query, latestFetchedBlock, newItems), knownHeight);
|
|
653
662
|
cs.pendingBudget = Primitive_float.max(0, cs.pendingBudget - query.itemsEst);
|
|
663
|
+
if (cs.fetchState.knownHeight > previousKnownHeight) {
|
|
664
|
+
return switchDynamicContractsToClientSideFilteringNearHead(cs);
|
|
665
|
+
}
|
|
654
666
|
}
|
|
655
667
|
|
|
656
668
|
function registerReorgGuard(cs, blockStore, knownHeight) {
|
|
@@ -683,6 +695,7 @@ function prepareReorg(cs, eventsProcessedDiff) {
|
|
|
683
695
|
|
|
684
696
|
function updateKnownHeight(cs, knownHeight) {
|
|
685
697
|
cs.fetchState = FetchState.updateKnownHeight(cs.fetchState, knownHeight);
|
|
698
|
+
switchDynamicContractsToClientSideFilteringNearHead(cs);
|
|
686
699
|
}
|
|
687
700
|
|
|
688
701
|
function setEndBlockToFirstEvent(cs, blockNumber) {
|
|
@@ -700,12 +713,13 @@ function setEndBlockToFirstEvent(cs, blockNumber) {
|
|
|
700
713
|
chainId: init.chainId,
|
|
701
714
|
latestOnBlockBlockNumber: init.latestOnBlockBlockNumber,
|
|
702
715
|
blockLag: init.blockLag,
|
|
716
|
+
blockThrottle: init.blockThrottle,
|
|
703
717
|
buffer: init.buffer,
|
|
704
718
|
maxOnBlockBufferSize: init.maxOnBlockBufferSize,
|
|
705
719
|
onBlockRegistrations: init.onBlockRegistrations,
|
|
706
720
|
knownHeight: init.knownHeight,
|
|
707
721
|
firstEventBlock: init.firstEventBlock,
|
|
708
|
-
|
|
722
|
+
clientFilterPolicy: init.clientFilterPolicy
|
|
709
723
|
};
|
|
710
724
|
return;
|
|
711
725
|
}
|
|
@@ -718,12 +732,13 @@ function setEndBlockToFirstEvent(cs, blockNumber) {
|
|
|
718
732
|
chainId: init$1.chainId,
|
|
719
733
|
latestOnBlockBlockNumber: init$1.latestOnBlockBlockNumber,
|
|
720
734
|
blockLag: init$1.blockLag,
|
|
735
|
+
blockThrottle: init$1.blockThrottle,
|
|
721
736
|
buffer: init$1.buffer,
|
|
722
737
|
maxOnBlockBufferSize: init$1.maxOnBlockBufferSize,
|
|
723
738
|
onBlockRegistrations: init$1.onBlockRegistrations,
|
|
724
739
|
knownHeight: init$1.knownHeight,
|
|
725
740
|
firstEventBlock: init$1.firstEventBlock,
|
|
726
|
-
|
|
741
|
+
clientFilterPolicy: init$1.clientFilterPolicy
|
|
727
742
|
};
|
|
728
743
|
}
|
|
729
744
|
|
|
@@ -881,12 +896,13 @@ function applyBatchProgress(cs, batch, blockTimestampName) {
|
|
|
881
896
|
chainId: init.chainId,
|
|
882
897
|
latestOnBlockBlockNumber: init.latestOnBlockBlockNumber,
|
|
883
898
|
blockLag: init.blockLag,
|
|
899
|
+
blockThrottle: init.blockThrottle,
|
|
884
900
|
buffer: init.buffer,
|
|
885
901
|
maxOnBlockBufferSize: init.maxOnBlockBufferSize,
|
|
886
902
|
onBlockRegistrations: init.onBlockRegistrations,
|
|
887
903
|
knownHeight: init.knownHeight,
|
|
888
904
|
firstEventBlock: firstEventBlock,
|
|
889
|
-
|
|
905
|
+
clientFilterPolicy: init.clientFilterPolicy
|
|
890
906
|
};
|
|
891
907
|
}
|
|
892
908
|
}
|
package/src/Clock.res
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// What the source layer reads time from and arms its timers on. A test can hand
|
|
2
|
+
// it a clock that only moves when the test moves it, which takes the scheduler
|
|
3
|
+
// out of every race between a timer and the step a test is taking. The label
|
|
4
|
+
// names a timer in that clock's timeline; the real clock ignores it.
|
|
5
|
+
type timer
|
|
6
|
+
|
|
7
|
+
type t = {
|
|
8
|
+
now: unit => float,
|
|
9
|
+
setTimeout: (~label: string, unit => unit, int) => timer,
|
|
10
|
+
clearTimeout: timer => unit,
|
|
11
|
+
random: unit => float,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Test-only. Set while a test drives a run on virtual time, so code that reaches
|
|
15
|
+
// past the clock it was given fails where it does, instead of arming a timer
|
|
16
|
+
// the test can neither see nor move.
|
|
17
|
+
let refuseReal = ref(false)
|
|
18
|
+
|
|
19
|
+
let refused = what =>
|
|
20
|
+
JsError.throwWithMessage(
|
|
21
|
+
`Clock.real ${what} on virtual time. Use the clock the code was given, so advanceTime can see and move it.`,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
// Monotonic: every reading is only ever compared with another, and a wall
|
|
25
|
+
// clock corrected mid-run would make a connection look like it served for
|
|
26
|
+
// minutes, or a benched source look recovered.
|
|
27
|
+
let real: t = {
|
|
28
|
+
now: () => refuseReal.contents ? refused("read the time") : Performance.now(),
|
|
29
|
+
// A zero delay is a turn of the event loop on either clock.
|
|
30
|
+
setTimeout: (~label, callback, millis) =>
|
|
31
|
+
refuseReal.contents && millis > 0
|
|
32
|
+
? refused(`armed "${label}"`)
|
|
33
|
+
: setTimeout(callback, millis)->(Utils.magic: timeoutId => timer),
|
|
34
|
+
clearTimeout: timer => clearTimeout(timer->(Utils.magic: timer => timeoutId)),
|
|
35
|
+
random: Math.random,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let delay = (clock: t, ~label, millis) =>
|
|
39
|
+
Promise.make((resolve, _reject) => clock.setTimeout(~label, () => resolve(), millis)->ignore)
|
|
40
|
+
|
|
41
|
+
// Half the delay fixed, half spread across it. Every indexer pointed at one
|
|
42
|
+
// provider loses its stream, or gives up on a quiet chain, in the same instant
|
|
43
|
+
// that provider blinks, and putting them all back on the same schedule is how a
|
|
44
|
+
// blip becomes a stampede.
|
|
45
|
+
let jitter = (clock: t, delay) =>
|
|
46
|
+
delay / 2 + (clock.random() *. (delay / 2)->Int.toFloat)->Float.toInt
|
|
47
|
+
|
|
48
|
+
// Clearing a pending timer and forgetting it are one action. A ref left holding
|
|
49
|
+
// a spent timer reads as one still pending, and the next clear looks like it
|
|
50
|
+
// did something.
|
|
51
|
+
let clearRef = (clock: t, timer: ref<option<timer>>) => {
|
|
52
|
+
switch timer.contents {
|
|
53
|
+
| Some(timer) => clock.clearTimeout(timer)
|
|
54
|
+
| None => ()
|
|
55
|
+
}
|
|
56
|
+
timer := None
|
|
57
|
+
}
|