Skip to content

Commit

Permalink
fix: simplify query if signature validation skipped (#56)
Browse files Browse the repository at this point in the history
* fix: simplify query of signature validation skipped

* fix

* fix: ch query

---------

Co-authored-by: Eugene Formanenko <[email protected]>
  • Loading branch information
belopash and mo4islona authored Oct 18, 2024
1 parent 7cf1ea4 commit fc97220
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
46 changes: 45 additions & 1 deletion packages/rewards-calculator/src/clickhouseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,44 @@ export class ClickhouseClient {
}

public async getActiveWorkers(shouldSkipSignatureValidation = false) {
if (shouldSkipSignatureValidation) {
const columns = [
"worker_id",
"sum(num_read_chunks) as num_read_chunks",
"sum(output_size) as output_size",
"count(*) as totalRequests",
];
await this.logTotalQueries();
const query = `
select ${columns.join(",")}
from ${
config.clickhouse.logsTableName
}
where
${config.clickhouse.logsTableName}.worker_timestamp >= '${formatDate(this.from)}' and
${config.clickhouse.logsTableName}.worker_timestamp <= '${formatDate(this.to)}' and
(toUnixTimestamp64Micro(collector_timestamp) - toUnixTimestamp64Micro(worker_timestamp)) / 60000000 < 20
group by worker_id
`;
const res: any[] = await clickhouse.query(query).toPromise()



let rows = 0
for await (const row of res) {
const worker = this.workers.add(row.worker_id);
worker.totalRequests = row.totalRequests;
worker.requestsProcessed = row.totalRequests;
worker.bytesSent = row.output_size;
worker.chunksRead = row.num_read_chunks;
rows+= row.totalRequests
}

console.log('ROWS', rows)

return this.workers;
}

const columns = [
"client_id",
"worker_id",
Expand Down Expand Up @@ -62,10 +100,15 @@ export class ClickhouseClient {
}.worker_timestamp >= '${formatDate(this.from)}' and ${
config.clickhouse.logsTableName
}.worker_timestamp <= '${formatDate(this.to)}' and timeDiff < 20 order by query_hash`;

let rows = 0
for await (const row of clickhouse.query(query).stream()) {
rows++
const worker = this.workers.add(row.worker_id);
await worker.processQuery(row, shouldSkipSignatureValidation);
await worker.processQuery(row, false);
}
console.log('ROWS', rows)

return this.workers;
}

Expand All @@ -85,6 +128,7 @@ export class ClickhouseClient {
for await (const row of clickhouse.query(query).stream()) {
pings[row.worker_id] = row.timestamps;
}

return pings;
}

Expand Down
1 change: 1 addition & 0 deletions packages/rewards-calculator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const config = {
rewardEpochLength: Number(env("REWARD_EPOCH_LENGTH_BLOCKS", 7000)),
epochConfirmationBlocks: Number(env("EPOCH_CONFIRMATION_BLOCKS", 150)),
maxEpochsPerCommit: Number(env("MAX_EPOCHS_PER_COMMIT", 1)),
skipSignatureValidation: env<string>("SKIP_SIGNATURE_VALIDATION", 'false') === 'true',
clickhouse: {
username: env("CLICKHOUSE_USERNAME", "sqd_read"),
password: env("CLICKHOUSE_PASSWORD"),
Expand Down
2 changes: 1 addition & 1 deletion packages/rewards-calculator/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function rewards(
const _epochStats = await epochStats(
Number(fromBlock),
Number(toBlock),
true,
config.skipSignatureValidation,
);
const _duration = await duration(BigInt(fromBlock), BigInt(toBlock));
const workerStats = _epochStats.map((worker) => ({
Expand Down
4 changes: 2 additions & 2 deletions packages/rewards-calculator/src/rewardBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class RewardBot {
* We need to calculate 2 epochs to get the correct period for the rewards
* because of splitting the rewards to chunks
*/
const workers = await epochStats(fromBlock - epochLen, toBlock);
const workers = await epochStats(fromBlock - epochLen, toBlock, config.skipSignatureValidation);

/**
* We send to blockchain original epoch length due to a flaw in the contract
Expand Down Expand Up @@ -117,7 +117,7 @@ export class RewardBot {
* We need to calculate 2 epochs to get the correct period for the rewards
* because of splitting the rewards to chunks
*/
const workers = await epochStats(ranges.fromBlock - ranges.epochLen, ranges.toBlock);
const workers = await epochStats(ranges.fromBlock - ranges.epochLen, ranges.toBlock, config.skipSignatureValidation);

/**
* We send to blockchain original epoch length due to a flaw in the contract
Expand Down

2 comments on commit fc97220

@github-actions
Copy link

Choose a reason for hiding this comment

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

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.