Skip to content

Commit

Permalink
feat: include signer version
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Nov 5, 2024
1 parent 5323999 commit 190aea7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/api/routes/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const CycleRoutes: FastifyPluginCallback<
proposals_missed_count: result.proposals_missed_count,
average_response_time_ms: result.average_response_time_ms,
last_seen: result.last_block_response_time?.toISOString() ?? null,
version: result.last_metadata_server_version ?? null,
};
return cycleSinger;
});
Expand Down Expand Up @@ -144,6 +145,7 @@ export const CycleRoutes: FastifyPluginCallback<
proposals_missed_count: signer.proposals_missed_count,
average_response_time_ms: signer.average_response_time_ms,
last_seen: signer.last_block_response_time?.toISOString() ?? null,
version: signer.last_metadata_server_version ?? null,
};
return cycleSigner;
});
Expand Down
3 changes: 3 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export const CycleSignerSchema = Type.Object({
last_seen: Type.Union([Type.String(), Type.Null()], {
description: 'ISO timestamp of the last time a message from this signer was seen',
}),
version: Type.Union([Type.String(), Type.Null()], {
description: 'The last seen signer binary version reported by this signer',
}),
// TODO: implement these nice-to-have fields
/*
mined_blocks_accepted_included_count: Type.Integer({
Expand Down
33 changes: 22 additions & 11 deletions src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export class PgStore extends BasePgStore {
proposals_missed_count: number;
average_response_time_ms: number;
last_block_response_time: Date | null;
last_metadata_server_version: string | null;
}[]
>`
WITH signer_data AS (
Expand Down Expand Up @@ -330,17 +331,19 @@ export class PgStore extends BasePgStore {
br.signer_sighash,
br.accepted,
br.received_at,
br.metadata_server_version,
br.id
FROM block_responses br
JOIN proposal_data pd ON br.signer_sighash = pd.block_hash -- Only responses linked to selected proposals
),
latest_response_data AS (
-- Find the latest response time for each signer
SELECT
-- Find the latest response time and corresponding metadata_server_version for each signer
SELECT DISTINCT ON (signer_key)
signer_key,
MAX(received_at) AS last_block_response_time
received_at AS last_block_response_time,
metadata_server_version
FROM response_data
GROUP BY signer_key
ORDER BY signer_key, received_at DESC
),
signer_proposal_data AS (
-- Cross join signers with proposals and left join filtered responses
Expand Down Expand Up @@ -387,14 +390,15 @@ export class PgStore extends BasePgStore {
ad.proposals_rejected_count,
ad.proposals_missed_count,
COALESCE(ad.average_response_time_ms, 0) AS average_response_time_ms,
COALESCE(lrd.last_block_response_time, NULL) AS last_block_response_time
COALESCE(lrd.last_block_response_time, NULL) AS last_block_response_time,
COALESCE(lrd.metadata_server_version, NULL) AS last_metadata_server_version
FROM signer_data sd
LEFT JOIN aggregated_data ad
ON sd.signer_key = ad.signer_key
LEFT JOIN signer_rank sr
ON sd.signer_key = sr.signer_key
LEFT JOIN latest_response_data lrd
ON sd.signer_key = lrd.signer_key -- Join the latest response time data
ON sd.signer_key = lrd.signer_key -- Join the latest response time and metadata server version data
ORDER BY sd.signer_stacked_amount DESC, sd.signer_key ASC
`;
return dbRewardSetSigners;
Expand All @@ -414,6 +418,7 @@ export class PgStore extends BasePgStore {
proposals_missed_count: number;
average_response_time_ms: number;
last_block_response_time: Date | null;
last_metadata_server_version: string | null;
}[]
>`
WITH signer_data AS (
Expand Down Expand Up @@ -442,16 +447,20 @@ export class PgStore extends BasePgStore {
br.signer_sighash,
br.accepted,
br.received_at,
br.metadata_server_version,
br.id
FROM block_responses br
JOIN proposal_data pd ON br.signer_sighash = pd.block_hash
WHERE br.signer_key = ${normalizeHexString(signerId)} -- Filter for the specific signer
),
latest_response_data AS (
-- Find the latest response time for the specific signer
SELECT
MAX(received_at) AS last_block_response_time
-- Find the latest response time and corresponding metadata_server_version for the specific signer
SELECT DISTINCT ON (signer_key)
signer_key,
received_at AS last_block_response_time,
metadata_server_version
FROM response_data
ORDER BY signer_key, received_at DESC
),
signer_proposal_data AS (
-- Cross join the specific signer with proposals and left join filtered responses
Expand All @@ -461,6 +470,7 @@ export class PgStore extends BasePgStore {
pd.proposal_received_at,
rd.accepted,
rd.received_at AS response_received_at,
rd.metadata_server_version,
EXTRACT(MILLISECOND FROM (rd.received_at - pd.proposal_received_at)) AS response_time_ms
FROM signer_data sd
CROSS JOIN proposal_data pd
Expand Down Expand Up @@ -498,14 +508,15 @@ export class PgStore extends BasePgStore {
ad.proposals_rejected_count,
ad.proposals_missed_count,
COALESCE(ad.average_response_time_ms, 0) AS average_response_time_ms,
COALESCE(lrd.last_block_response_time, NULL) AS last_block_response_time
COALESCE(lrd.last_block_response_time, NULL) AS last_block_response_time,
COALESCE(lrd.metadata_server_version, NULL) AS last_metadata_server_version
FROM signer_data sd
LEFT JOIN aggregated_data ad
ON sd.signer_key = ad.signer_key
LEFT JOIN signer_rank sr
ON sd.signer_key = sr.signer_key
LEFT JOIN latest_response_data lrd
ON true
ON sd.signer_key = lrd.signer_key
`;
return dbRewardSetSigner[0];
}
Expand Down
10 changes: 10 additions & 0 deletions tests/db/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ describe('Endpoint tests', () => {
proposals_missed_count: 3,
average_response_time_ms: 26273.979,
last_seen: '2024-11-02T13:33:21.831Z',
version:
'stacks-signer signer-3.0.0.0.0.1 (release/signer-3.0.0.0.0.1:b26f406, release build, linux [x86_64])',
};
expect(testSigner).toEqual(expectedSignerData);

Expand All @@ -158,6 +160,7 @@ describe('Endpoint tests', () => {
proposals_missed_count: 99,
average_response_time_ms: 0,
last_seen: null,
version: null,
};
expect(miaSigner).toEqual(expectedMiaSignerData);
});
Expand Down Expand Up @@ -197,6 +200,8 @@ describe('Endpoint tests', () => {
proposals_missed_count: 1,
average_response_time_ms: 28515,
last_seen: '2024-11-02T13:33:21.831Z',
version:
'stacks-signer signer-3.0.0.0.0.1 (release/signer-3.0.0.0.0.1:b26f406, release build, linux [x86_64])',
};
expect(testSigner1).toEqual(expectedSignerData1);

Expand Down Expand Up @@ -232,6 +237,8 @@ describe('Endpoint tests', () => {
proposals_missed_count: 0,
average_response_time_ms: 29020,
last_seen: '2024-11-02T13:30:43.731Z',
version:
'stacks-signer signer-3.0.0.0.0.1 (release/signer-3.0.0.0.0.1:b26f406, release build, linux [x86_64])',
};
expect(testSigner3).toEqual(expected3);
});
Expand All @@ -255,6 +262,8 @@ describe('Endpoint tests', () => {
proposals_missed_count: 3,
average_response_time_ms: 26273.979,
last_seen: '2024-11-02T13:33:21.831Z',
version:
'stacks-signer signer-3.0.0.0.0.1 (release/signer-3.0.0.0.0.1:b26f406, release build, linux [x86_64])',
};
expect(body).toEqual(expectedSignerData);

Expand All @@ -276,6 +285,7 @@ describe('Endpoint tests', () => {
proposals_missed_count: 99,
average_response_time_ms: 0,
last_seen: null,
version: null,
};
expect(miaSigner).toEqual(expectedMiaSignerData);
});
Expand Down

0 comments on commit 190aea7

Please sign in to comment.