Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integrate recent participants contract #355

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions bin/cancel-pending-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ethers } from 'ethers'
import { CoinType, newDelegatedEthAddress } from '@glif/filecoin-address'
import pRetry from 'p-retry'

import { createMeridianContract } from '../lib/ie-contract.js'
import { provider } from '../lib/contracts.js'

const {
WALLET_SEED
Expand All @@ -17,8 +17,6 @@ const [, , tx] = process.argv
assert(WALLET_SEED, 'WALLET_SEED required')
assert(tx, 'Transaction hash must be provided as the first argument')

const { provider } = await createMeridianContract()

const signer = ethers.Wallet.fromPhrase(WALLET_SEED, provider)
const walletDelegatedAddress = newDelegatedEthAddress(/** @type {any} */(signer.address), CoinType.MAIN).toString()
console.log(
Expand Down
6 changes: 3 additions & 3 deletions bin/dry-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'
import pg from 'pg'
import { RoundData } from '../lib/round.js'
import { createMeridianContract } from '../lib/ie-contract.js'
import { createMeridianContract, provider } from '../lib/contracts.js'
import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'

/** @typedef {import('../lib/preprocess.js').Measurement} Measurement */
Expand Down Expand Up @@ -213,7 +213,7 @@ async function fetchMeasurementsAddedEvents (contractAddress, roundIndex) {
* @param {bigint} roundIndex
*/
async function fetchMeasurementsAddedFromChain (contractAddress, roundIndex) {
const { ieContract, provider } = await createMeridianContract(contractAddress)
const ieContract = createMeridianContract(contractAddress)

console.log('Fetching MeasurementsAdded events from the ledger')

Expand Down Expand Up @@ -268,6 +268,6 @@ function isEventLog (logOrEventLog) {
* @param {string} contractAddress
*/
async function fetchLastRoundIndex (contractAddress) {
const { ieContract } = await createMeridianContract(contractAddress)
const ieContract = createMeridianContract(contractAddress)
return await ieContract.currentRoundIndex()
}
4 changes: 2 additions & 2 deletions bin/fetch-recent-miner-measurements.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import pMap from 'p-map'
import { createMeridianContract } from '../lib/ie-contract.js'
import { createMeridianContract } from '../lib/contracts.js'
import { fetchMeasurements, preprocess } from '../lib/preprocess.js'
import { RoundData } from '../lib/round.js'
import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'
Expand Down Expand Up @@ -140,7 +140,7 @@ console.error('Wrote human-readable summary for %s to %s', minerId, MINER_SUMMAR
* @returns
*/
async function getRecentMeasurementsAddedEvents (contractAddress, blocksToQuery = Number.POSITIVE_INFINITY) {
const { ieContract } = await createMeridianContract(contractAddress)
const ieContract = createMeridianContract(contractAddress)

// max look-back period allowed by Glif.io is 2000 blocks (approx 16h40m)
// in practice, requests for the last 2000 blocks are usually rejected,
Expand Down
4 changes: 2 additions & 2 deletions bin/spark-evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { recordTelemetry } from '../lib/telemetry.js'
import { fetchMeasurements } from '../lib/preprocess.js'
import { migrateWithPgConfig } from '../lib/migrate.js'
import pg from 'pg'
import { createMeridianContract } from '../lib/ie-contract.js'
import { createMeridianContract, provider } from '../lib/contracts.js'
import { startCancelStuckTxs } from '../lib/cancel-stuck-txs.js'

const {
Expand All @@ -28,7 +28,7 @@ assert(WALLET_SEED, 'WALLET_SEED required')

await migrateWithPgConfig({ connectionString: DATABASE_URL })

const { ieContract, provider } = await createMeridianContract()
const ieContract = await createMeridianContract()

const signer = ethers.Wallet.fromPhrase(WALLET_SEED, provider)
const walletDelegatedAddress = newDelegatedEthAddress(/** @type {any} */(signer.address), CoinType.MAIN).toString()
Expand Down
30 changes: 30 additions & 0 deletions lib/contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ethers } from 'ethers'
import { rpcUrls, GLIF_TOKEN } from './config.js'
import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'
import * as SparkEvaluationsRecentParticipants from '@filecoin-station/spark-evaluations-recent-participants'

export const provider = new ethers.FallbackProvider(rpcUrls.map(url => {
const fetchRequest = new ethers.FetchRequest(url)
fetchRequest.setHeader('Authorization', `Bearer ${GLIF_TOKEN}`)
return new ethers.JsonRpcProvider(fetchRequest, null, {
polling: true,
batchMaxCount: 10
})
}))

// Uncomment for troubleshooting
// provider.on('debug', d => console.log('[ethers:debug %s] %s %o', new Date().toISOString().split('T')[1], d.action, d.payload ?? d.result))

export const createMeridianContract = (contractAddress = SparkImpactEvaluator.ADDRESS) => {
return new ethers.Contract(
contractAddress,
SparkImpactEvaluator.ABI,
provider
)
}

export const recentParticipantsContract = new ethers.Contract(
SparkEvaluationsRecentParticipants.ADDRESS,
SparkEvaluationsRecentParticipants.ABI,
provider
)
Comment on lines +18 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why we need a factory function for the meridian contract (createMeridianContract)? I find it surprising that there isn't a factory function for "recent participants" contract as well.

25 changes: 0 additions & 25 deletions lib/ie-contract.js

This file was deleted.

17 changes: 17 additions & 0 deletions lib/platform-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'node:assert'
import createDebug from 'debug'
import * as Sentry from '@sentry/node'
import pRetry from 'p-retry'
import { recentParticipantsContract } from './contracts.js'

const debug = createDebug('spark:platform-stats')

Expand Down Expand Up @@ -105,6 +106,22 @@ export const updateDailyStationStats = async (
*/
export const updateDailyParticipants = async (pgClient, participantIds) => {
debug('Updating daily participants, count=%s', participantIds.length)
;(async () => {
const { rows } = await pgClient.query(`
SELECT participant_address FROM participants WHERE id = ANY($1::INT[])
`, [
participantIds
])
const addresses = rows.map(row => row.participant_address)
try {
await recentParticipantsContract.set(new Date().getDay(), addresses)
} catch (err) {
console.error('Error updating spark-evaluations-recent-participants', err)
Sentry.captureException(err)
}
})()
// FIXME: Remove this part once `spark-evaluations-recent-participants` is in
// full use
await pgClient.query(`
INSERT INTO daily_participants (day, participant_id)
SELECT now() as day, UNNEST($1::INT[]) AS participant_id
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"typescript": "^5.6.2"
},
"dependencies": {
"@filecoin-station/spark-evaluations-recent-participants": "^3.0.0",
"@filecoin-station/spark-impact-evaluator": "^1.1.1",
"@glif/filecoin-address": "^3.0.12",
"@influxdata/influxdb-client": "^1.35.0",
Expand Down