Skip to content

Commit a7023b8

Browse files
fix pg test bug, naming convention, and error message reliance (#134)
1 parent d4d3ce1 commit a7023b8

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

observer/lib/observer.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,21 @@ export const observeTransferEvents = async (pgPool, ieContract, provider) => {
1010
const { rows } = await pgPool.query(
1111
'SELECT MAX(last_checked_block) FROM daily_reward_transfers'
1212
)
13-
const lastCheckedBlock = rows[0].last_checked_block
13+
let queryFromBlock = rows[0].last_checked_block
14+
const currentBlockNumber = await provider.getBlockNumber()
1415

15-
console.log('Querying impact evaluator Transfer events after block', lastCheckedBlock)
16-
let events
17-
try {
18-
events = await ieContract.queryFilter(ieContract.filters.Transfer(), lastCheckedBlock)
19-
} catch (error) {
20-
console.error('Error querying impact evaluator Transfer events', error)
21-
if (error.message.includes('bad tipset height')) {
22-
console.log('Block number too old, GLIF only provides last 2000 blocks, querying from -1900')
23-
events = await ieContract.queryFilter(ieContract.filters.Transfer(), -1900)
24-
} else {
25-
throw error
26-
}
16+
if (!queryFromBlock || queryFromBlock < currentBlockNumber - 1900) {
17+
queryFromBlock = currentBlockNumber - 1900
18+
console.log('Block number too old, GLIF only provides last 2000 blocks, querying from -1900')
2719
}
28-
const currentBlockNumber = await provider.getBlockNumber()
29-
console.log('Current block number:', currentBlockNumber)
20+
21+
console.log('Querying impact evaluator Transfer events after block', queryFromBlock)
22+
const events = await ieContract.queryFilter(ieContract.filters.Transfer(), queryFromBlock)
23+
3024
console.log(`Found ${events.length} Transfer events`)
3125
for (const event of events) {
3226
const transferEvent = {
33-
to_address: event.args.to,
27+
toAddress: event.args.to,
3428
amount: event.args.amount
3529
}
3630
console.log('Transfer event:', transferEvent)

observer/lib/platform-stats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('pg').Client} pgClient
33
* @param {Object} transferEvent
4-
* @param {string} transferEvent.to_address
4+
* @param {string} transferEvent.toAddress
55
* @param {number} transferEvent.amount
66
* @param {number} currentBlockNumber
77
*/
@@ -12,5 +12,5 @@ export const updateDailyTransferStats = async (pgClient, transferEvent, currentB
1212
ON CONFLICT (day, to_address) DO UPDATE SET
1313
amount = daily_reward_transfers.amount + EXCLUDED.amount,
1414
last_checked_block = EXCLUDED.last_checked_block
15-
`, [transferEvent.to_address, transferEvent.amount, currentBlockNumber])
15+
`, [transferEvent.toAddress, transferEvent.amount, currentBlockNumber])
1616
}

observer/test/platform-stats.test.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,46 @@ import { DATABASE_URL } from '../lib/config.js'
66
import { updateDailyTransferStats } from '../lib/platform-stats.js'
77
import { migrateWithPgClient } from '@filecoin-station/spark-stats-db-migrations'
88

9+
const createPgClient = async () => {
10+
const pgClient = new pg.Client({ connectionString: DATABASE_URL })
11+
await pgClient.connect()
12+
return pgClient
13+
}
14+
915
describe('platform-stats-generator', () => {
10-
/** @type {pg.Pool} */
11-
let pgPool
16+
/** @type {pg.Client} */
17+
let pgClient
1218

1319
before(async () => {
14-
pgPool = new pg.Pool({ connectionString: DATABASE_URL })
15-
await migrateWithPgClient(pgPool)
20+
pgClient = await createPgClient()
21+
await migrateWithPgClient(pgClient)
1622
})
1723

1824
let today
1925
beforeEach(async () => {
20-
await pgPool.query('DELETE FROM daily_reward_transfers')
26+
await pgClient.query('DELETE FROM daily_reward_transfers')
2127

2228
// Run all tests inside a transaction to ensure `now()` always returns the same value
2329
// See https://dba.stackexchange.com/a/63549/125312
2430
// This avoids subtle race conditions when the tests are executed around midnight.
25-
await pgPool.query('BEGIN TRANSACTION')
31+
await pgClient.query('BEGIN TRANSACTION')
2632
today = await getCurrentDate()
2733
})
2834

2935
afterEach(async () => {
30-
await pgPool.query('END TRANSACTION')
36+
await pgClient.query('END TRANSACTION')
3137
})
3238

3339
after(async () => {
34-
await pgPool.end()
40+
await pgClient.end()
3541
})
3642

3743
describe('updateDailyTransferStats', () => {
3844
it('should correctly update daily Transfer stats with new transfer events', async () => {
39-
await updateDailyTransferStats(pgPool, { to_address: 'address1', amount: 100 }, 1)
40-
await updateDailyTransferStats(pgPool, { to_address: 'address1', amount: 200 }, 2)
45+
await updateDailyTransferStats(pgClient, { toAddress: 'address1', amount: 100 }, 1)
46+
await updateDailyTransferStats(pgClient, { toAddress: 'address1', amount: 200 }, 2)
4147

42-
const { rows } = await pgPool.query(`
48+
const { rows } = await pgClient.query(`
4349
SELECT day::TEXT, to_address, amount, last_checked_block FROM daily_reward_transfers
4450
`)
4551
assert.strictEqual(rows.length, 1)
@@ -49,10 +55,10 @@ describe('platform-stats-generator', () => {
4955
})
5056

5157
it('should handle multiple addresses in daily Transfer stats', async () => {
52-
await updateDailyTransferStats(pgPool, { to_address: 'address1', amount: 50 }, 1)
53-
await updateDailyTransferStats(pgPool, { to_address: 'address2', amount: 150 }, 1)
58+
await updateDailyTransferStats(pgClient, { toAddress: 'address1', amount: 50 }, 1)
59+
await updateDailyTransferStats(pgClient, { toAddress: 'address2', amount: 150 }, 1)
5460

55-
const { rows } = await pgPool.query(`
61+
const { rows } = await pgClient.query(`
5662
SELECT day::TEXT, to_address, amount, last_checked_block FROM daily_reward_transfers
5763
ORDER BY to_address
5864
`)
@@ -66,7 +72,7 @@ describe('platform-stats-generator', () => {
6672
})
6773

6874
const getCurrentDate = async () => {
69-
const { rows: [{ today }] } = await pgPool.query('SELECT now()::DATE::TEXT as today')
75+
const { rows: [{ today }] } = await pgClient.query('SELECT now()::DATE::TEXT as today')
7076
return today
7177
}
7278
})

stats/test/platform-routes.test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ describe('Platform Routes HTTP request handler', () => {
5555
describe('GET /stations/daily', () => {
5656
it('returns daily station metrics for the given date range', async () => {
5757
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-10', [
58-
{ station_id: 'station1', accepted_measurement_count: 1 }
58+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
5959
])
6060
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-11', [
61-
{ station_id: 'station2', accepted_measurement_count: 1 }
61+
{ stationId: 'station2', acceptedMeasurementCount: 1 }
6262
])
6363
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-12', [
64-
{ station_id: 'station2', accepted_measurement_count: 2 },
65-
{ station_id: 'station3', accepted_measurement_count: 1 }
64+
{ stationId: 'station2', acceptedMeasurementCount: 2 },
65+
{ stationId: 'station3', acceptedMeasurementCount: 1 }
6666
])
6767
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-13', [
68-
{ station_id: 'station1', accepted_measurement_count: 1 }
68+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
6969
])
7070

7171
const res = await fetch(
@@ -89,25 +89,25 @@ describe('Platform Routes HTTP request handler', () => {
8989
it('returns monthly station metrics for the given date range ignoring the day number', async () => {
9090
// before the date range
9191
await givenDailyStationMetrics(pgPoolEvaluateDb, '2023-12-31', [
92-
{ station_id: 'station1', accepted_measurement_count: 1 }
92+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
9393
])
9494
// in the date range
9595
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-10', [
96-
{ station_id: 'station1', accepted_measurement_count: 1 }
96+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
9797
])
9898
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-11', [
99-
{ station_id: 'station2', accepted_measurement_count: 1 }
99+
{ stationId: 'station2', acceptedMeasurementCount: 1 }
100100
])
101101
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-12', [
102-
{ station_id: 'station2', accepted_measurement_count: 2 },
103-
{ station_id: 'station3', accepted_measurement_count: 1 }
102+
{ stationId: 'station2', acceptedMeasurementCount: 2 },
103+
{ stationId: 'station3', acceptedMeasurementCount: 1 }
104104
])
105105
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-02-13', [
106-
{ station_id: 'station1', accepted_measurement_count: 1 }
106+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
107107
])
108108
// after the date range
109109
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-03-01', [
110-
{ station_id: 'station1', accepted_measurement_count: 1 }
110+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
111111
])
112112

113113
const res = await fetch(
@@ -130,17 +130,17 @@ describe('Platform Routes HTTP request handler', () => {
130130
describe('GET /measurements/daily', () => {
131131
it('returns daily total accepted measurement count for the given date range', async () => {
132132
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-10', [
133-
{ station_id: 'station1', accepted_measurement_count: 1 }
133+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
134134
])
135135
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-11', [
136-
{ station_id: 'station2', accepted_measurement_count: 1 }
136+
{ stationId: 'station2', acceptedMeasurementCount: 1 }
137137
])
138138
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-12', [
139-
{ station_id: 'station2', accepted_measurement_count: 2 },
140-
{ station_id: 'station3', accepted_measurement_count: 1 }
139+
{ stationId: 'station2', acceptedMeasurementCount: 2 },
140+
{ stationId: 'station3', acceptedMeasurementCount: 1 }
141141
])
142142
await givenDailyStationMetrics(pgPoolEvaluateDb, '2024-01-13', [
143-
{ station_id: 'station1', accepted_measurement_count: 1 }
143+
{ stationId: 'station1', acceptedMeasurementCount: 1 }
144144
])
145145

146146
const res = await fetch(
@@ -163,17 +163,17 @@ describe('Platform Routes HTTP request handler', () => {
163163
describe('GET /transfers/daily', () => {
164164
it('returns daily total Rewards sent for the given date range', async () => {
165165
await givenDailyRewardTransferMetrics(pgPoolStatsDb, '2024-01-10', [
166-
{ to_address: 'to1', amount: 100, last_checked_block: 1 }
166+
{ toAddress: 'to1', amount: 100, lastCheckedBlock: 1 }
167167
])
168168
await givenDailyRewardTransferMetrics(pgPoolStatsDb, '2024-01-11', [
169-
{ to_address: 'to2', amount: 150, last_checked_block: 1 }
169+
{ toAddress: 'to2', amount: 150, lastCheckedBlock: 1 }
170170
])
171171
await givenDailyRewardTransferMetrics(pgPoolStatsDb, '2024-01-12', [
172-
{ to_address: 'to2', amount: 300, last_checked_block: 1 },
173-
{ to_address: 'to3', amount: 250, last_checked_block: 1 }
172+
{ toAddress: 'to2', amount: 300, lastCheckedBlock: 1 },
173+
{ toAddress: 'to3', amount: 250, lastCheckedBlock: 1 }
174174
])
175175
await givenDailyRewardTransferMetrics(pgPoolStatsDb, '2024-01-13', [
176-
{ to_address: 'to1', amount: 100, last_checked_block: 1 }
176+
{ toAddress: 'to1', amount: 100, lastCheckedBlock: 1 }
177177
])
178178

179179
const res = await fetch(
@@ -201,8 +201,8 @@ const givenDailyStationMetrics = async (pgPoolEvaluateDb, day, stationStats) =>
201201
ON CONFLICT DO NOTHING
202202
`, [
203203
day,
204-
stationStats.map(s => s.station_id),
205-
stationStats.map(s => s.accepted_measurement_count)
204+
stationStats.map(s => s.stationId),
205+
stationStats.map(s => s.acceptedMeasurementCount)
206206
])
207207
}
208208

@@ -213,8 +213,8 @@ const givenDailyRewardTransferMetrics = async (pgPoolStatsDb, day, transferStats
213213
ON CONFLICT DO NOTHING
214214
`, [
215215
day,
216-
transferStats.map(s => s.to_address),
216+
transferStats.map(s => s.toAddress),
217217
transferStats.map(s => s.amount),
218-
transferStats.map(s => s.last_checked_block)
218+
transferStats.map(s => s.lastCheckedBlock)
219219
])
220220
}

0 commit comments

Comments
 (0)