-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeal-observer-backend.js
175 lines (161 loc) · 6.04 KB
/
deal-observer-backend.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/** @import {PgPool} from '@filecoin-station/deal-observer-db' */
import assert from 'node:assert'
import { createPgPool } from '@filecoin-station/deal-observer-db'
import * as Sentry from '@sentry/node'
import timers from 'node:timers/promises'
import slug from 'slug'
import '../lib/instrument.js'
import { createInflux } from '../lib/telemetry.js'
import { getChainHead, rpcRequest } from '../lib/rpc-service/service.js'
import { fetchDealWithHighestActivatedEpoch, countStoredActiveDeals, observeBuiltinActorEvents } from '../lib/deal-observer.js'
import { indexPieces } from '../lib/piece-indexer.js'
import { findAndSubmitUnsubmittedDeals, submitDealsToSparkApi } from '../lib/spark-api-submit-deals.js'
import { getDealPayloadCid } from '../lib/piece-indexer-service.js'
/** @import {Queryable} from '@filecoin-station/deal-observer-db' */
const {
INFLUXDB_TOKEN,
SPARK_API_BASE_URL,
SPARK_API_TOKEN,
SPARK_API_SUBMIT_DEALS_BATCH_SIZE = 100
} = process.env
if (!INFLUXDB_TOKEN) {
console.error('INFLUXDB_TOKEN not provided. Telemetry will not be recorded.')
}
assert(SPARK_API_BASE_URL, 'SPARK_API_BASE_URL required')
assert(SPARK_API_TOKEN, 'SPARK_API_TOKEN required')
const LOOP_INTERVAL = 10 * 1000
// Filecoin will need some epochs to reach finality.
// We do not want to fetch deals that are newer than the current chain head - 940 epochs.
const finalityEpochs = 940
// The free tier of the glif rpc endpoint only allows us to go back 2000 blocks.
const maxPastEpochs = 1999
assert(finalityEpochs <= maxPastEpochs)
const pgPool = await createPgPool()
const { recordTelemetry } = createInflux(INFLUXDB_TOKEN)
/**
* @param {(method:string,params:any[]) => Promise<any>} makeRpcRequest
* @param {Queryable} pgPool
*/
const observeActorEventsLoop = async (makeRpcRequest, pgPool) => {
const LOOP_NAME = 'Observe actor events'
while (true) {
const start = Date.now()
try {
const currentChainHead = await getChainHead(makeRpcRequest)
const lastInsertedDeal = await fetchDealWithHighestActivatedEpoch(pgPool)
const startEpoch = Math.max(
currentChainHead.Height - maxPastEpochs,
lastInsertedDeal ? (lastInsertedDeal.activated_at_epoch ?? -1) + 1 : 0
)
const endEpoch = currentChainHead.Height - finalityEpochs
for (let epoch = startEpoch; epoch <= endEpoch; epoch++) {
await observeBuiltinActorEvents(epoch, pgPool, makeRpcRequest)
}
const newLastInsertedDeal = await fetchDealWithHighestActivatedEpoch(pgPool)
const numberOfStoredDeals = await countStoredActiveDeals(pgPool)
if (INFLUXDB_TOKEN) {
recordTelemetry('observed_deals_stats', point => {
point.intField('last_searched_epoch', newLastInsertedDeal?.activated_at_epoch || 0)
point.intField('number_of_stored_active_deals', numberOfStoredDeals)
})
}
} catch (e) {
console.error(e)
Sentry.captureException(e)
}
const dt = Date.now() - start
console.log(`Loop "${LOOP_NAME}" took ${dt}ms`)
if (INFLUXDB_TOKEN) {
recordTelemetry(`loop_${slug(LOOP_NAME, '_')}`, point => {
point.intField('interval_ms', LOOP_INTERVAL)
point.intField('duration_ms', dt)
})
}
if (dt < LOOP_INTERVAL) {
await timers.setTimeout(LOOP_INTERVAL - dt)
}
}
}
/**
* Periodically fetches unsubmitted deals from the database and submits them to Spark API.
*
* @param {PgPool} pgPool
* @param {object} args
* @param {string} args.sparkApiBaseUrl
* @param {string} args.sparkApiToken
* @param {number} args.sparkApiSubmitDealsBatchSize
*/
const sparkApiSubmitDealsLoop = async (pgPool, { sparkApiBaseUrl, sparkApiToken, sparkApiSubmitDealsBatchSize }) => {
const LOOP_NAME = 'Submit deals to spark-api'
while (true) {
const start = Date.now()
try {
const { submitted, ingested, skipped } = await findAndSubmitUnsubmittedDeals(
pgPool,
sparkApiSubmitDealsBatchSize,
deals => submitDealsToSparkApi(sparkApiBaseUrl, sparkApiToken, deals)
)
if (INFLUXDB_TOKEN) {
recordTelemetry('submitted_deals_stats', point => {
point.intField('submitted_deals', submitted)
point.intField('ingested_deals', ingested)
point.intField('skipped_deals', skipped)
})
}
} catch (e) {
console.error(e)
Sentry.captureException(e)
}
const dt = Date.now() - start
console.log(`Loop "${LOOP_NAME}" took ${dt}ms`)
if (INFLUXDB_TOKEN) {
recordTelemetry(`loop_${slug(LOOP_NAME, '_')}`, point => {
point.intField('interval_ms', LOOP_INTERVAL)
point.intField('duration_ms', dt)
})
}
if (dt < LOOP_INTERVAL) {
await timers.setTimeout(LOOP_INTERVAL - dt)
}
}
}
/**
* @param {(method:string,params:object) => object} makeRpcRequest
* @param {(providerId:string,pieceCid:string) => Promise<string|null>} getDealPayloadCid
* @param {*} pgPool
*/
export const pieceIndexerLoop = async (makeRpcRequest, getDealPayloadCid, pgPool) => {
const LOOP_NAME = 'Piece Indexer'
while (true) {
const start = Date.now()
// Maximum number of deals to index in one loop iteration
const maxDeals = 1000
try {
await indexPieces(makeRpcRequest, getDealPayloadCid, pgPool, maxDeals)
} catch (e) {
console.error(e)
Sentry.captureException(e)
}
const dt = Date.now() - start
console.log(`Loop "${LOOP_NAME}" took ${dt}ms`)
// For local monitoring and debugging, we can omit sending data to InfluxDB
if (INFLUXDB_TOKEN) {
recordTelemetry(`loop_${slug(LOOP_NAME, '_')}`, point => {
point.intField('interval_ms', LOOP_INTERVAL)
point.intField('duration_ms', dt)
})
}
if (dt < LOOP_INTERVAL) {
await timers.setTimeout(LOOP_INTERVAL - dt)
}
}
}
await Promise.all([
pieceIndexerLoop(rpcRequest, getDealPayloadCid, pgPool),
observeActorEventsLoop(rpcRequest, pgPool),
sparkApiSubmitDealsLoop(pgPool, {
sparkApiBaseUrl: SPARK_API_BASE_URL,
sparkApiToken: SPARK_API_TOKEN,
sparkApiSubmitDealsBatchSize: Number(SPARK_API_SUBMIT_DEALS_BATCH_SIZE)
})
])