-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (101 loc) · 3.48 KB
/
index.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
import getRawBody from 'raw-body'
import assert from 'http-assert'
import { validate } from './lib/validate.js'
import timers from 'node:timers/promises'
import http from 'node:http'
import { ethers } from 'ethers'
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { db } from './lib/db.js'
import { newDelegatedEthAddress } from '@glif/filecoin-address'
import { Web3Storage } from 'web3.storage'
// Configuration
const {
IE_CONTRACT_ADDRESS = '0x3113b83ccec38a18df936f31297de490485d7b2e',
WALLET_SEED = 'test test test test test test test test test test test junk',
RPC_URL = 'https://api.calibration.node.glif.io/rpc/v0',
WEB3_STORAGE_API_TOKEN
} = process.env
// Set up contract
const provider = new ethers.providers.JsonRpcProvider(RPC_URL)
// provider.on('debug', d => console.log(JSON.stringify(d, null, 2)))
const signer = ethers.Wallet.fromMnemonic(WALLET_SEED).connect(provider)
const ieContract = new ethers.Contract(
IE_CONTRACT_ADDRESS,
JSON.parse(
await fs.readFile(
fileURLToPath(new URL('./abi.json', import.meta.url)),
'utf8'
)
),
provider
)
const ieContractWithSigner = ieContract.connect(signer)
console.log(
'Wallet address:',
signer.address,
newDelegatedEthAddress(signer.address, 't').toString()
)
// await ieContractWithSigner.setRoundReward(ethers.utils.parseUnits('1.0', 'ether'))
// await ieContractWithSigner.setNextRoundLength(60)
// await ieContractWithSigner.grantRole(ieContractWithSigner.EVALUATE_ROLE(), '0xB0a808b5C49f5Ed7Af9EcAAaF033B2d937692877')
const web3Storage = new Web3Storage({ token: WEB3_STORAGE_API_TOKEN })
//
// Phase 1: Store the measurements
//
const handler = async (req, res) => {
assert.strictEqual(req.url, '/measurements', 404)
assert.strictEqual(req.method, 'POST', 404)
const body = await getRawBody(req, { limit: '100kb' })
const measurement = JSON.parse(body)
validate(measurement, 'job_id', { type: 'string' })
validate(measurement, 'peer_id', { type: 'string' })
validate(measurement, 'started_at', { type: 'date' })
validate(measurement, 'ended_at', { type: 'date' })
db.measurements.push({
jobId: measurement.job_id,
peerId: measurement.peer_id,
startedAt: measurement.started_at,
endedAt: measurement.ended_at,
cid: null
})
res.end('OK')
}
http.createServer((req, res) => {
handler(req, res).catch(err => {
console.error(err)
res.statusCode = 500
res.end(String(err))
})
}).listen()
//
// Phase 2: Publish the measurements
//
const publish = async () => {
// Fetch measurements
const measurements = db.measurements.filter(m => m.cid === null)
console.log(`Publishing ${measurements.length} measurements`)
// Share measurements
const file = new File([JSON.stringify(measurements)], 'measurements.json', { type: 'application/json' })
const cid = await web3Storage.put([file])
console.log(`Measurements packaged in ${cid}`)
// Call contract with CID
console.log('ie.addMeasurements()...')
const tx = await ieContractWithSigner.addMeasurements(cid.toString())
const receipt = await tx.wait()
const event = receipt.events.find(e => e.event === 'MeasurementsAdded')
const { roundIndex } = event.args
console.log('Measurements added to round', roundIndex.toString())
// Mark measurements as shared
for (const m of measurements) {
m.cid = cid
}
console.log('Done!')
}
const startPublishLoop = async () => {
while (true) {
publish().catch(console.error)
await timers.setTimeout(120_000)
}
}
startPublishLoop()