Skip to content
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
1 change: 1 addition & 0 deletions packages/network-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"packageManager": "[email protected]",
"dependencies": {
"@metamask/eth-sig-util": "^7.0.0",
"bignumber.js": "^9.1.2",
"cross-fetch": "^4.0.0",
"crypto-js": "^4.2.0",
"js-base64": "^3.7.5",
Expand Down
40 changes: 26 additions & 14 deletions packages/network-support/src/scoreManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0

import BigNumber from 'bignumber.js';
import { Order, ScoreWithDetail } from './types';
import { Logger, IStore, createStore } from './utils';
import { getBlockScoreWeight, getLatencyScoreWeight, scoreMap } from './utils/score';
import {
calculateBigIntPercentile,
getBlockScoreWeight,
getLatencyScoreWeight,
} from './utils/score';
import { Version } from './utils/version';

type Options = {
Expand Down Expand Up @@ -182,26 +187,33 @@ export class ScoreManager {
}

async updatePriceScore(orders: Order[]) {
let minPrice = BigInt(`-${orders[0].price}`);
let maxPrice = BigInt(`-${orders[0].price}`);
const prices = orders.map((o) => BigNumber(o.price));
const percenTile = calculateBigIntPercentile(prices, 95);

for (const o of orders) {
const p = BigInt(`-${o.price}`);
if (p < minPrice) minPrice = p;
if (p > maxPrice) maxPrice = p;
}
const key = this.getPriceScoreKey();
for (const { indexer, price } of orders) {
const p = BigInt(`-${price}`);
let weight = scoreMap(p, [minPrice, maxPrice], [1, 2]);
weight = Math.floor(weight * 10) / 10;
await this.scoreStore.set(`${key}:${indexer}_${this.projectId}`, weight);
const blockWeight = await getBlockScoreWeight(this.scoreStore, o.indexer, this.projectId);
const latencyWeight = await getLatencyScoreWeight(this.scoreStore, o.indexer, this.projectId);

let factor = 4;
if (blockWeight >= 1 && latencyWeight >= 1) {
factor = 9;
}
let weight = 1;
const diff = percenTile.minus(new BigNumber(o.price));
if (diff.gt(0)) {
weight = 1 + diff.dividedBy(percenTile).times(factor).toNumber();
weight = Number(weight.toFixed(2));
}
this.logger?.info({
type: 'updateScore',
target: 'priceWeight',
deploymentId: this.projectId,
indexer: indexer,
indexer: o.indexer,
price: o.price,
to: weight,
percenTile: percenTile.toString(),
factor,
diff: diff.toString(),
});
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/network-support/src/utils/score.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: Apache-2.0

import BigNumber from 'bignumber.js';
import { IStore } from './store';

const BLOCK_WEIGHT_OUTPUT_RANGE: [number, number] = [0.2, 1];
Expand Down Expand Up @@ -130,6 +131,14 @@ function getMedian(arr: number[]) {
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
}

export function calculateBigIntPercentile(arr: BigNumber[], percentile: number): BigNumber {
if (arr.length === 0) return new BigNumber(0);
const sortedArr = arr.slice().sort((a, b) => (a.lt(b) ? -1 : 1));
// const sortedArr = arr.slice().sort((a, b) => (a < b ? -1 : 1));
const index = Math.floor((percentile / 100) * (sortedArr.length - 1) + 0.5);
return sortedArr[index];
}

function getBlockScoreKey(): string {
return 'score:block';
}
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3280,6 +3280,7 @@ __metadata:
"@metamask/eth-sig-util": ^7.0.0
"@types/crypto-js": ^4.2.2
"@types/node": 18
bignumber.js: ^9.1.2
cross-fetch: ^4.0.0
crypto-js: ^4.2.0
js-base64: ^3.7.5
Expand Down