This repository was archived by the owner on Jun 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathchecker.js
More file actions
144 lines (133 loc) · 4.54 KB
/
checker.js
File metadata and controls
144 lines (133 loc) · 4.54 KB
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
import { join } from 'node:path'
import * as zinniaRuntime from '../lib/zinnia.js'
import { formatActivityObject, activities } from '../lib/activity.js'
import { runPingLoop, runMachinesLoop } from '../lib/telemetry.js'
import fs from 'node:fs/promises'
import { metrics } from '../lib/metrics.js'
import { paths } from '../lib/paths.js'
import { getCheckerId } from '../lib/checker-id.js'
import pRetry from 'p-retry'
import { fetch } from 'undici'
import { ethAddressFromDelegated, isEthAddress } from '@glif/filecoin-address'
import { ethers, formatEther } from 'ethers'
import { runUpdateRewardsLoop } from '../lib/rewards.js'
import { runUpdateContractsLoop } from '../lib/contracts.js'
const {
FIL_WALLET_ADDRESS,
PASSPHRASE
} = process.env
const runtimeNames = [
'zinnia'
]
/**
* @param {string} msg
* @param {number} [exitCode]
*/
const panic = (msg, exitCode = 1) => {
console.error(msg)
process.exit(exitCode)
}
export const checker = async ({ json, recreateCheckerIdOnError, experimental }) => {
if (!FIL_WALLET_ADDRESS) panic('FIL_WALLET_ADDRESS required')
if (FIL_WALLET_ADDRESS.startsWith('f1')) {
panic('Invalid FIL_WALLET_ADDRESS: f1 addresses are currently not supported. Please use an f4 or 0x address.')
}
if (
!FIL_WALLET_ADDRESS.startsWith('f410') &&
!FIL_WALLET_ADDRESS.startsWith('0x')
) {
panic('FIL_WALLET_ADDRESS must start with f410 or 0x')
}
if (FIL_WALLET_ADDRESS.startsWith('0x') && !isEthAddress(FIL_WALLET_ADDRESS)) {
panic('Invalid FIL_WALLET_ADDRESS ethereum address', 2)
}
const keypair = await getCheckerId({ secretsDir: paths.secrets, passphrase: PASSPHRASE, recreateOnError: recreateCheckerIdOnError })
const CHECKER_ID = keypair.publicKey
const fetchRes = await pRetry(
() => fetch(`https://station-wallet-screening.fly.dev/${FIL_WALLET_ADDRESS}`),
{
retries: 1000,
onFailedAttempt: () =>
console.error('Failed to validate FIL_WALLET_ADDRESS address. Retrying...')
}
)
if (fetchRes.status === 403) panic('Invalid FIL_WALLET_ADDRESS address', 2)
if (!fetchRes.ok) panic('Failed to validate FIL_WALLET_ADDRESS address')
const ethAddress = FIL_WALLET_ADDRESS.startsWith('0x')
? FIL_WALLET_ADDRESS
: ethAddressFromDelegated(FIL_WALLET_ADDRESS)
for (const runtimeName of runtimeNames) {
await fs.mkdir(join(paths.runtimeCache, runtimeName), { recursive: true })
await fs.mkdir(join(paths.runtimeState, runtimeName), { recursive: true })
}
activities.onActivity(activity => {
if (json) {
console.log(JSON.stringify({
type: `activity:${activity.type}`,
subnet: activity.source,
message: activity.message
}))
} else {
process.stdout.write(formatActivityObject(activity))
}
})
metrics.onUpdate(metrics => {
if (json) {
console.log(JSON.stringify({
type: 'jobs-completed',
total: metrics.totalJobsCompleted,
rewardsScheduledForAddress: formatEther(metrics.rewardsScheduledForAddress)
}))
} else {
console.log(JSON.stringify({
totalJobsCompleted: metrics.totalJobsCompleted,
rewardsScheduledForAddress:
formatEther(metrics.rewardsScheduledForAddress)
}, null, 2))
}
})
const contracts = []
const fetchRequest = new ethers.FetchRequest(
'https://api.node.glif.io/rpc/v1'
)
fetchRequest.setHeader(
'Authorization',
'Bearer RXQ2SKH/BVuwN7wisZh3b5uXStGPj1JQIrIWD+rxF0Y='
)
const provider = new ethers.JsonRpcProvider(fetchRequest)
await Promise.all([
zinniaRuntime.run({
provider,
CHECKER_ID,
FIL_WALLET_ADDRESS: ethAddress,
ethAddress,
STATE_ROOT: join(paths.runtimeState, 'zinnia'),
CACHE_ROOT: join(paths.runtimeCache, 'zinnia'),
subnetVersionsDir: paths.subnetVersionsDir,
subnetSourcesDir: paths.subnetSourcesDir,
onActivity: activity => {
activities.submit({
...activity,
// Zinnia will try to overwrite `source` if a subnet created the
// activity. Using the spread syntax won't work because a
// `source: null` would overwrite the default value.
source: activity.source || 'Zinnia'
})
},
onMetrics: m => metrics.submit('zinnia', m),
experimental
}),
runPingLoop({ CHECKER_ID }),
runMachinesLoop({ CHECKER_ID }),
runUpdateContractsLoop({
provider,
contracts,
onActivity: (activity) => activities.submit(activity)
}),
runUpdateRewardsLoop({
contracts,
ethAddress,
onMetrics: m => metrics.submit('zinnia', m)
})
])
}