diff --git a/api/index.js b/api/index.js index 8bc2e057..04f7d074 100644 --- a/api/index.js +++ b/api/index.js @@ -98,6 +98,16 @@ const createMeasurement = async (req, res, client) => { validate(measurement, 'stationId', { type: 'string', required: true }) assert(measurement.stationId.match(/^[0-9a-fA-F]{88}$/), 400, 'Invalid Station ID') + if (measurement.alternativeProviderCheck) { + validate(measurement, 'alternativeProviderCheck', { type: 'object', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.statusCode', { type: 'number', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.timeout', { type: 'boolean', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.carTooLarge', { type: 'boolean', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.endAt', { type: 'date', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.protocol', { type: 'string', required: false }) + validate(measurement.alternativeProviderCheck, 'alternativeProviderCheck.providerId', { type: 'string', required: false }) + } + const inetGroup = await mapRequestToInetGroup(client, req) logNetworkInfo(req.headers, measurement.stationId, recordNetworkInfoTelemetry) @@ -124,10 +134,16 @@ const createMeasurement = async (req, res, client) => { indexer_result, miner_id, provider_id, + alternative_provider_check_status_code, + alternative_provider_check_timeout, + alternative_provider_check_car_too_large, + alternative_provider_check_end_at, + alternative_provider_check_protocol, + alternative_provider_check_provider_id, completed_at_round ) SELECT - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, + $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 id as completed_at_round FROM spark_rounds ORDER BY id DESC @@ -154,7 +170,13 @@ const createMeasurement = async (req, res, client) => { measurement.carChecksum, measurement.indexerResult, measurement.minerId, - measurement.providerId + measurement.providerId, + measurement.alternativeProviderCheck?.statusCode, + measurement.alternativeProviderCheck?.timeout, + measurement.alternativeProviderCheck?.carTooLarge ?? false, + measurement.alternativeProviderCheck?.endAt, + measurement.alternativeProviderCheck?.protocol, + measurement.alternativeProviderCheck?.providerId ]) json(res, { id: rows[0].id }) } @@ -190,7 +212,15 @@ const getMeasurement = async (req, res, client, measurementId) => { endAt: resultRow.end_at, byteLength: resultRow.byte_length, carTooLarge: resultRow.car_too_large, - attestation: resultRow.attestation + attestation: resultRow.attestation, + alternativeProviderCheck: { + statusCode: resultRow.alternative_provider_check_status_code, + timeout: resultRow.alternative_provider_check_timeout, + carTooLarge: resultRow.alternative_provider_check_car_too_large, + endAt: resultRow.alternative_provider_check_end_at, + protocol: resultRow.alternative_provider_check_protocol, + providerId: resultRow.alternative_provider_check_provider_id + } }) } diff --git a/api/test/test.js b/api/test/test.js index d730e94c..00b81419 100644 --- a/api/test/test.js +++ b/api/test/test.js @@ -38,7 +38,15 @@ const VALID_MEASUREMENT = { carChecksum: 'somehash', minerId: 'f02abc', providerId: 'provider-pubkey', - indexerResult: 'OK' + indexerResult: 'OK', + alternativeProviderCheck: { + statusCode: 200, + timeout: false, + carTooLarge: false, + endAt: new Date(), + protocol: 'graphsync', + providerId: 'alt-provider-pubkey' + } } const assertResponseStatus = async (res, status) => { @@ -194,6 +202,15 @@ describe('Routes', () => { assert.strictEqual(measurementRow.miner_id, measurement.minerId) assert.strictEqual(measurementRow.provider_id, measurement.providerId) assert.strictEqual(measurementRow.station_id, measurement.stationId) + assert.strictEqual(measurementRow.alternative_provider_check_status_code, measurement.alternativeProviderCheck.statusCode) + assert.strictEqual(measurementRow.alternative_provider_check_timeout, measurement.alternativeProviderCheck.timeout) + assert.strictEqual(measurementRow.alternative_provider_check_car_too_large, measurement.alternativeProviderCheck.carTooLarge) + assert.strictEqual( + measurementRow.alternative_provider_check_end_at.toJSON(), + measurement.alternativeProviderCheck.endAt.toJSON() + ) + assert.strictEqual(measurementRow.alternative_provider_check_protocol, measurement.alternativeProviderCheck.protocol) + assert.strictEqual(measurementRow.alternative_provider_check_provider_id, measurement.alternativeProviderCheck.providerId) }) it('allows older format with walletAddress', async () => { diff --git a/migrations/067.do.measurement-alternative-provider-check.sql b/migrations/067.do.measurement-alternative-provider-check.sql new file mode 100644 index 00000000..6d13319c --- /dev/null +++ b/migrations/067.do.measurement-alternative-provider-check.sql @@ -0,0 +1,7 @@ +ALTER TABLE measurements + ADD COLUMN alternative_provider_check_status_code INTEGER, + ADD COLUMN alternative_provider_check_timeout BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN alternative_provider_check_car_too_large BOOLEAN NOT NULL DEFAULT FALSE, + ADD COLUMN alternative_provider_check_end_at TIMESTAMPTZ, + ADD COLUMN alternative_provider_check_protocol protocol, + ADD COLUMN alternative_provider_check_provider_id TEXT; diff --git a/publish/index.js b/publish/index.js index 72068d2a..c77b8bb6 100644 --- a/publish/index.js +++ b/publish/index.js @@ -37,7 +37,15 @@ export const publish = async ({ provider_id, cid, provider_address, - protocol + protocol, + jsonb_build_object( + 'status_code', alternative_provider_check_status_code, + 'timeout', alternative_provider_check_timeout, + 'car_too_large', alternative_provider_check_car_too_large, + 'end_at', alternative_provider_check_end_at, + 'protocol', alternative_provider_check_protocol, + 'provider_id', alternative_provider_check_provider_id + ) AS alternative_provider_check FROM measurements LIMIT $1 `, [ diff --git a/publish/test/test.js b/publish/test/test.js index fff95127..a7cfd2a5 100644 --- a/publish/test/test.js +++ b/publish/test/test.js @@ -164,8 +164,17 @@ describe('integration', () => { carTooLarge: true, minerId: 'f02abc', providerId: 'provider-pubkey', - round: 42 - }] + round: 42, + alternativeProviderCheck: { + statusCode: 200, + timeout: false, + carTooLarge: false, + endAt: new Date(), + protocol: 'graphsync', + providerId: 'alt-provider-pubkey' + } + } + ] for (const measurement of measurements) { await insertMeasurement(client, measurement)