Skip to content

Commit

Permalink
Merge pull request #179 from helix-bridge/xiaoch05-bugfix-status-cong…
Browse files Browse the repository at this point in the history
…estion

bugfix: too long time to fetch record status
  • Loading branch information
xiaoch05 authored Aug 21, 2024
2 parents c1f319a + da5905f commit e14f78a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 121 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ jobs:
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/apollo:${{ steps.tag-name.outputs.tag }}
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/apollo:latest
- name: Build ponder with sha
uses: docker/build-push-action@v3
with:
push: true
context: ponder/lnv3
tags: |
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:sha-${{ steps.short-sha.outputs.sha }}
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:staging
# - name: Build ponder with sha
# uses: docker/build-push-action@v3
# with:
# push: true
# context: ponder/lnv3
# tags: |
# ${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:sha-${{ steps.short-sha.outputs.sha }}
# ${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:staging

- name: Build ponder with tag
uses: docker/build-push-action@v3
if: startsWith(github.ref, 'refs/tags/v')
with:
push: true
context: ponder/lnv3
tags: |
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:${{ steps.tag-name.outputs.tag }}
${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:latest
# - name: Build ponder with tag
# uses: docker/build-push-action@v3
# if: startsWith(github.ref, 'refs/tags/v')
# with:
# push: true
# context: ponder/lnv3
# tags: |
# ${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:${{ steps.tag-name.outputs.tag }}
# ${{ env.DOCKER_REGISTRY }}/${{ github.repository }}/ponder:latest

# deploy-railway:
# name: Deploy railway
Expand Down
172 changes: 78 additions & 94 deletions apollo/src/lnv3/lnv3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Lnv3Service implements OnModuleInit {

private readonly takeEachTime = 2;
private skip = new Array(this.transferService.transfers.length).fill(0);
private skipForWithdrawLiquidity = new Array(this.transferService.transfers.length).fill(0);
private sourceServices = new Map();

constructor(
Expand Down Expand Up @@ -404,7 +405,27 @@ export class Lnv3Service implements OnModuleInit {
return;
}
cache.waitingWithdrawInterval = 0;
const records = cache.waitingWithdrawRecords;

const records = await this.aggregationService
.queryHistoryRecords({
skip: this.skipForWithdrawLiquidity[index],
take: this.takeEachTime,
where: {
fromChain: transfer.chainConfig.code,
bridge: `lnv3`,
NOT: { result: RecordStatus.pending },
needWithdrawLiquidity: true,
endTxHash: '',
},
})
.then((result) => result.records);

if (records.length < this.takeEachTime) {
this.skipForWithdrawLiquidity[index] = 0;
} else {
this.skipForWithdrawLiquidity[index] += this.takeEachTime;
}

const transferIdMap = new Map<string, string[]>();
for (const record of records) {
const recordSplitted = record.id.split('-');
Expand Down Expand Up @@ -443,7 +464,6 @@ export class Lnv3Service implements OnModuleInit {
}
}
}
cache.waitingWithdrawRecords = [];
}

async fetchStatus(transfer: PartnerT2, index: number) {
Expand All @@ -455,6 +475,7 @@ export class Lnv3Service implements OnModuleInit {
where: {
fromChain: transfer.chainConfig.code,
bridge: `lnv3`,
result: RecordStatus.pending,
endTxHash: '',
},
})
Expand All @@ -471,104 +492,67 @@ export class Lnv3Service implements OnModuleInit {
const recordSplitted = record.id.split('-');
const transferId = last(recordSplitted);
const dstChainId = recordSplitted[2];

if (record.endTxHash === '') {
// if record.result === RecordStatus.success, it must wait for withdraw liquidity
// then we need to reduce the frequency of requests
// push the request to the cache
if (record.result === RecordStatus.success && record.needWithdrawLiquidity) {
if (
!this.fetchCache[index].waitingWithdrawRecords.some((item) => item.id === record.id)
) {
this.fetchCache[index].waitingWithdrawRecords.push(record);
const toChain = this.getDestChain(dstChainId);
const relayRecord = await this.queryRecordRelayStatus(toChain, transferId);

if (relayRecord) {
let needWithdrawLiquidity = record.needWithdrawLiquidity;
const requestWithdrawTimestamp = Number(relayRecord.requestWithdrawTimestamp);
let endTxHash = record.endTxHash;
if (record.result !== RecordStatus.success) {
const providerId = this.genRelayInfoID(
transfer.chainConfig.id,
toChain.chainConfig.id,
record.relayer,
record.sendTokenAddress
);
const relayerInfo = await this.aggregationService.queryLnBridgeRelayInfoById({
id: providerId,
});
// waiting for relayer info update
if (!relayerInfo) {
this.logger.log(
`lnv3 [${transfer.chainConfig.code}->${toChain.chainConfig.code}] waiting for relayer info update, id: ${providerId}`
);
continue;
}
continue;
}

const toChain = this.getDestChain(dstChainId);
const relayRecord = await this.queryRecordRelayStatus(toChain, transferId);

if (relayRecord) {
let needWithdrawLiquidity = record.needWithdrawLiquidity;
const requestWithdrawTimestamp = Number(relayRecord.requestWithdrawTimestamp);
let endTxHash = record.endTxHash;
if (record.result !== RecordStatus.success) {
const providerId = this.genRelayInfoID(
transfer.chainConfig.id,
toChain.chainConfig.id,
record.relayer,
record.sendTokenAddress
);
const relayerInfo = await this.aggregationService.queryLnBridgeRelayInfoById({
id: providerId,
});
// waiting for relayer info update
if (!relayerInfo) {
this.logger.log(
`lnv3 [${transfer.chainConfig.code}->${toChain.chainConfig.code}] waiting for relayer info update, id: ${providerId}`
);
continue;
}

if (relayRecord.slashed) {
needWithdrawLiquidity = false;
}
if (!needWithdrawLiquidity) {
endTxHash = relayRecord.transactionHash;
}
const updateData = {
result: RecordStatus.success,
responseTxHash: relayRecord.transactionHash,
endTxHash: endTxHash,
endTime: Number(relayRecord.timestamp),
relayer: relayRecord.relayer.toLowerCase(),
needWithdrawLiquidity: needWithdrawLiquidity,
lastRequestWithdraw: requestWithdrawTimestamp,
};
if (relayRecord.slashed) {
needWithdrawLiquidity = false;
}
if (!needWithdrawLiquidity) {
endTxHash = relayRecord.transactionHash;
}
const updateData = {
result: RecordStatus.success,
responseTxHash: relayRecord.transactionHash,
endTxHash: endTxHash,
endTime: Number(relayRecord.timestamp),
relayer: relayRecord.relayer.toLowerCase(),
needWithdrawLiquidity: needWithdrawLiquidity,
lastRequestWithdraw: requestWithdrawTimestamp,
};

await this.aggregationService.updateHistoryRecord({
where: { id: record.id },
data: updateData,
});
await this.aggregationService.updateHistoryRecord({
where: { id: record.id },
data: updateData,
});

const cost = relayRecord.slashed ? 0 : relayRecord.fee;
const profit = relayRecord.slashed ? 0 : record.fee;
const cost = relayRecord.slashed ? 0 : relayRecord.fee;
const profit = relayRecord.slashed ? 0 : record.fee;

await this.aggregationService.updateLnBridgeRelayInfo({
where: { id: providerId },
data: {
cost: (BigInt(relayerInfo.cost) + BigInt(cost)).toString(),
profit: (BigInt(relayerInfo.profit) + BigInt(profit)).toString(),
},
});
await this.aggregationService.updateLnBridgeRelayInfo({
where: { id: providerId },
data: {
cost: (BigInt(relayerInfo.cost) + BigInt(cost)).toString(),
profit: (BigInt(relayerInfo.profit) + BigInt(profit)).toString(),
},
});

size += 1;
this.logger.log(
`lnv3 [${transfer.chainConfig.code}->${toChain.chainConfig.code}] new status id: ${record.id} relayed responseTxHash: ${relayRecord.transactionHash}`
);
}
// query withdrawLiquidity result
if (needWithdrawLiquidity && requestWithdrawTimestamp > 0) {
// query result on source
const transferRecord = await this.queryRecordWithdrawStatus(transfer, transferId);
if (
transferRecord &&
(transferRecord.hasWithdrawn ||
record.lastRequestWithdraw < requestWithdrawTimestamp)
) {
await this.aggregationService.updateHistoryRecord({
where: { id: record.id },
data: {
needWithdrawLiquidity: !transferRecord.hasWithdrawn,
endTxHash: transferRecord.hasWithdrawn ? record.responseTxHash : '',
lastRequestWithdraw: requestWithdrawTimestamp,
},
});
this.logger.log(
`lnv3 [${transfer.chainConfig.code}->${toChain.chainConfig.code}] tx withdrawn id: ${record.id}, time: ${requestWithdrawTimestamp}, done: ${transferRecord.hasWithdrawn}`
);
}
}
size += 1;
this.logger.log(
`lnv3 [${transfer.chainConfig.code}->${toChain.chainConfig.code}] new status id: ${record.id} relayed responseTxHash: ${relayRecord.transactionHash}`
);
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions apollo/src/lnv3/transfer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,6 @@ export class TransferService extends BaseTransferServiceT2 {
],
chainConfig: HelixChain.taikoHekla,
},
{
level0Indexers: [
{
indexerType: Level0IndexerType.superindex,
url: this.superindexEndpoint,
},
],
chainConfig: HelixChain.bera,
},
{
level0Indexers: [
{
Expand Down
2 changes: 1 addition & 1 deletion apollo/src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TasksService {
const callTimes: number = this.healthChecks.get(name);
const busy = await callback();
if (!busy) {
this.healthChecks.set(name, callTimes + 1);
this.healthChecks.set(name, callTimes + 1);
}
}, milliseconds);
this.schedulerRegistry.addInterval(name, interval);
Expand Down

0 comments on commit e14f78a

Please sign in to comment.