Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize collect allocation #595

Merged
merged 31 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9696e4a
feat: optimize collect allocation
cool-firer Feb 6, 2025
4ffeba3
feat: add sentry
cool-firer Feb 7, 2025
24dd09b
chore: bump to v2.8.5
cool-firer Feb 7, 2025
41c69bc
feat: config sentry dsn
cool-firer Feb 7, 2025
92090b8
feat: ignore unhandled exception
cool-firer Feb 8, 2025
1e7cbd4
feat: remove filter condition
cool-firer Feb 11, 2025
832ade6
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
7d72b1c
build: bump to v2.8.5-2
cool-firer Feb 11, 2025
29511dc
feat: terminate channel without sign
cool-firer Feb 11, 2025
86045d3
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
908ba17
build: bump tp v2.8.5-3
cool-firer Feb 11, 2025
803d77c
fix: fix remote value
cool-firer Feb 11, 2025
fad8ba7
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
9e1a886
build: bump to v2.8.5-4
cool-firer Feb 11, 2025
15321dd
fix: fix array error
cool-firer Feb 11, 2025
e8e79a1
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
2ac5df6
build: bump to v2.8.5-5
cool-firer Feb 11, 2025
464f6a9
feat: protect
cool-firer Feb 11, 2025
791b17a
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
5378ca4
build: bump to v2.8.5-6
cool-firer Feb 11, 2025
e0fdb8f
fix: fix protect
cool-firer Feb 11, 2025
c44bfea
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
3df58d5
build: bump v2.8.5-7
cool-firer Feb 11, 2025
15f192b
fix: fix boot error
cool-firer Feb 11, 2025
b963d80
Merge branch 'feat/terminate-channel' into feat/optimize-collect-allo…
cool-firer Feb 11, 2025
d39a751
feat: retry status
cool-firer Feb 12, 2025
eaf3403
feat: add log
cool-firer Feb 12, 2025
9155579
fix: fix failed retry
cool-firer Feb 12, 2025
675f2ef
fix: fix last collect time
cool-firer Feb 12, 2025
7f00ed9
style: remove extra logs
cool-firer Feb 12, 2025
9deafb7
build: bump to v2.8.4
cool-firer Feb 13, 2025
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
2 changes: 2 additions & 0 deletions apps/indexer-coordinator/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum ConfigType {
FLEX_VALID_PERIOD = 'flex_valid_period',
FLEX_ENABLED = 'flex_enabled',
ALLOCATION_REWARD_THRESHOLD = 'allocation_reward_threshold',
ALLOCATION_REWARD_LAST_FORCE_TIME = 'allocation_reward_last_force_time',
STATE_CHANNEL_REWARD_THRESHOLD = 'state_channel_reward_threshold',
AUTO_REDUCE_ALLOCATION_ENABLED = 'auto_reduce_allocation_enabled',
}
Expand All @@ -20,6 +21,7 @@ const defaultConfig: Record<string, string> = {
[ConfigType.FLEX_VALID_PERIOD]: `${60 * 60 * 24 * 3}`, // 3 days
[ConfigType.FLEX_ENABLED]: 'true',
[ConfigType.ALLOCATION_REWARD_THRESHOLD]: '2000000000000000000000', // 2000 sqt
[ConfigType.ALLOCATION_REWARD_LAST_FORCE_TIME]: '0',
[ConfigType.STATE_CHANNEL_REWARD_THRESHOLD]: '2000000000000000000000', // 2000 sqt
[ConfigType.AUTO_REDUCE_ALLOCATION_ENABLED]: 'true',
};
Expand Down
99 changes: 77 additions & 22 deletions apps/indexer-coordinator/src/reward/reward.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export type DeploymentReduce = {
status: string;
};

export type DeploymentAllocation = {
deploymentId: string;
retry: number;
};

@Injectable()
export class RewardService implements OnModuleInit {
private readonly rewardThreshold = BigNumber.from('2000000000000000000000');
Expand All @@ -41,6 +46,8 @@ export class RewardService implements OnModuleInit {
[this.collectStateChannelRewards.name]: false,
};

private failedDeploymentAllocations: DeploymentAllocation[] = [];

private lastSingleReduce: DeploymentReduce[] = [];
private lastTotalReduce: BigNumber = BigNumber.from(0);

Expand Down Expand Up @@ -77,9 +84,8 @@ export class RewardService implements OnModuleInit {

@Cron('0 */30 * * * *')
async checkTasks() {
if (this.txOngoingMap[this.collectAllocationRewards.name]) {
await this.collectAllocationRewards(TxType.postponed);
}
await this.collectAllocationRewardsRetry(TxType.postponed);

if (this.txOngoingMap[this.collectStateChannelRewards.name]) {
await this.collectStateChannelRewards(TxType.postponed);
}
Expand All @@ -93,7 +99,24 @@ export class RewardService implements OnModuleInit {
const deploymentAllocations = await this.networkService.getIndexerAllocationSummaries(
indexerId
);
this.txOngoingMap[this.collectAllocationRewards.name] = false;

let startTime = Number(
await this.configService.get(ConfigType.ALLOCATION_REWARD_LAST_FORCE_TIME)
);
if (!startTime) {
startTime = Date.now();
await this.configService.set(
ConfigType.ALLOCATION_REWARD_LAST_FORCE_TIME,
startTime.toString()
);
}

const thresholdConfig = await this.configService.get(ConfigType.ALLOCATION_REWARD_THRESHOLD);
const threshold = BigNumber.from(thresholdConfig);
const timeLimit = this.allocationBypassTimeLimit;
const forceCollect = Date.now() - startTime >= timeLimit;
const failedDeploymentAllocations: DeploymentAllocation[] = [];

for (const allocation of deploymentAllocations) {
const rewards = await this.onChainService.getAllocationRewards(
allocation.deploymentId,
Expand All @@ -102,20 +125,12 @@ export class RewardService implements OnModuleInit {
if (rewards.eq(0)) {
continue;
}
const thresholdConfig = await this.configService.get(ConfigType.ALLOCATION_REWARD_THRESHOLD);
const threshold = BigNumber.from(thresholdConfig);
const timeLimit = this.allocationBypassTimeLimit;
let startTime = this.allocationStartTimes.get(allocation.deploymentId);
if (!startTime) {
startTime = Date.now();
this.allocationStartTimes.set(allocation.deploymentId, startTime);
}
if (rewards.lt(threshold) && Date.now() - startTime < timeLimit) {
this.logger.debug(
`Bypassed reward [${rewards.toString()}] for deployment ${allocation.deploymentId} ${(
(Date.now() - startTime) /
this.oneDay
).toFixed(2)}/${timeLimit / this.oneDay} days`

if (rewards.lt(threshold) && !forceCollect) {
this.logger.info(
`[collectAllocationRewards] Bypassed reward [${rewards.toString()}] for deployment ${
allocation.deploymentId
} ${((Date.now() - startTime) / this.oneDay).toFixed(2)}/${timeLimit / this.oneDay} days`
);
continue;
}
Expand All @@ -125,16 +140,56 @@ export class RewardService implements OnModuleInit {
txType
);
if (!success) {
this.txOngoingMap[this.collectAllocationRewards.name] = true;
failedDeploymentAllocations.push({
deploymentId: allocation.deploymentId,
retry: 0,
});
continue;
}
this.allocationStartTimes.delete(allocation.deploymentId);
this.logger.debug(
`Collected reward [${rewards.toString()}] for deployment ${
this.logger.info(
`[collectAllocationRewards] Collected reward [${rewards.toString()}] for deployment ${
allocation.deploymentId
} ${rewards.toString()}`
);
}

this.failedDeploymentAllocations = failedDeploymentAllocations;
if (forceCollect) {
await this.configService.set(
ConfigType.ALLOCATION_REWARD_LAST_FORCE_TIME,
Date.now().toString()
);
}
}

async collectAllocationRewardsRetry(txType: TxType) {
const indexerId = await this.accountService.getIndexer();
if (!indexerId) {
return;
}
const failedDeploymentAllocations = this.failedDeploymentAllocations || [];

for (let i = 0; i < failedDeploymentAllocations.length; ) {
let success = true;
const info = failedDeploymentAllocations[i];
const rewards = await this.onChainService.getAllocationRewards(info.deploymentId, indexerId);
if (rewards.gt(0)) {
success = await this.onChainService.collectAllocationReward(
info.deploymentId,
indexerId,
txType
);
this.logger.info(
`[collectAllocationRewards] retry deployment:${info.deploymentId}, count:${info.retry}, ${success}`
);
}
if (success) {
failedDeploymentAllocations.splice(i, 1);
} else {
info.retry++;
i++;
}
}
}

async reduceAllocation(txType: TxType) {
Expand Down
Loading