Skip to content

Commit

Permalink
optimize fetch status speed
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Aug 23, 2024
1 parent a274d0c commit b7929be
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
37 changes: 31 additions & 6 deletions apollo/src/lnv3/lnv3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export enum RelayUpdateType {
PAUSE_UPDATE,
}

interface SkipInfo {
fromChain: string;
toChain: string;
skip: number;
}

@Injectable()
export class Lnv3Service implements OnModuleInit {
private readonly logger = new Logger('lnv3');
Expand All @@ -33,7 +39,7 @@ export class Lnv3Service implements OnModuleInit {
protected fetchSendDataInterval = 5000;

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

Expand Down Expand Up @@ -64,7 +70,7 @@ export class Lnv3Service implements OnModuleInit {
await this.fetchProviderInfo(item, index);
await this.fetchRecords(item, index);
await this.batchFetchStatus(item, index);
await this.fetchStatus(item, index);
await this.fetchStatuses(item, index);
await this.fetchWithdrawCacheStatus(item, index);
this.fetchCache[index].isSyncingHistory = false;
return false;
Expand Down Expand Up @@ -256,6 +262,17 @@ export class Lnv3Service implements OnModuleInit {
const responseHash = '';
const result = RecordStatus.pending;
const endTime = 0;
const skip = this.skip.find(
(s) =>
s.fromChain === transfer.chainConfig.code && s.toChain === toChain.chainConfig.code
);
if (!skip) {
this.skip.push({
fromChain: transfer.chainConfig.code,
toChain: toChain.chainConfig.code,
skip: 0,
});
}
await this.aggregationService.createHistoryRecord({
id: this.genID(
transfer,
Expand Down Expand Up @@ -466,14 +483,22 @@ export class Lnv3Service implements OnModuleInit {
}
}

async fetchStatus(transfer: PartnerT2, index: number) {
async fetchStatuses(transfer: PartnerT2, index: number) {
const skips = this.skip.filter((s) => s.fromChain === transfer.chainConfig.code);
for (const skip of skips) {
await this.fetchStatus(transfer, index, skip);
}
}

async fetchStatus(transfer: PartnerT2, index: number, skip: SkipInfo) {
try {
const uncheckedRecords = await this.aggregationService
.queryHistoryRecords({
skip: this.skip[index],
skip: skip.skip,
take: this.takeEachTime,
where: {
fromChain: transfer.chainConfig.code,
toChain: skip.toChain,
bridge: `lnv3`,
result: RecordStatus.pending,
endTxHash: '',
Expand All @@ -482,9 +507,9 @@ export class Lnv3Service implements OnModuleInit {
.then((result) => result.records);

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

let size = 0;
Expand Down
72 changes: 48 additions & 24 deletions apollo/src/lnv3/source/super.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ export class Lnv3SuperService extends SourceService {
async queryRecordInfo(url: string, chainId: number, latestNonce: number): Promise<Lnv3Record[]> {
const query = `query { lnv3TransferRecords(first: 20, orderBy: nonce, orderDirection: asc, skip: ${latestNonce}, where: {localChainId: ${chainId}}) { id, nonce, messageNonce, remoteChainId, provider, sourceToken, targetToken, sourceAmount, targetAmount, sender, receiver, timestamp, transactionHash, fee, transferId, hasWithdrawn } }`;
return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3TransferRecords);
}

Expand All @@ -25,10 +29,14 @@ export class Lnv3SuperService extends SourceService {
): Promise<Lnv3UpdateRecords[]> {
const query = `query { lnv3RelayUpdateRecords(first: 20, orderBy: nonce, orderDirection: asc, skip: ${latestNonce}, where: {localChainId: ${chainId}}) { id, updateType, remoteChainId, provider, transactionHash, timestamp, sourceToken, targetToken, penalty, baseFee, liquidityFeeRate, transferLimit, paused } }`;
return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3RelayUpdateRecords);
}
async queryRelayStatus(
Expand All @@ -38,10 +46,14 @@ export class Lnv3SuperService extends SourceService {
): Promise<Lnv3RelayRecord> {
const query = `query { lnv3RelayRecord(id: "${transferId}") { id, relayer, timestamp, transactionHash, slashed, requestWithdrawTimestamp, fee }}`;
return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3RelayRecord);
}
async queryMultiRelayStatus(
Expand All @@ -52,10 +64,14 @@ export class Lnv3SuperService extends SourceService {
const idArray = '["' + transferIds.join('","') + '"]';
const query = `query { lnv3RelayRecords(first: 20, where: {id_in: ${idArray}, localChainId: ${chainId}}) { id, timestamp, requestWithdrawTimestamp, relayer, transactionHash, slashed, fee } }`;
return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3RelayRecords);
}
async batchQueryRelayStatus(
Expand All @@ -66,10 +82,14 @@ export class Lnv3SuperService extends SourceService {
const query = `query { lnv3RelayRecords(first: 20, orderBy: timestamp, orderDirection: asc, where: {timestamp_gt: ${latestTimestamp}, localChainId: ${chainId}, slashed: false}) { id, timestamp, requestWithdrawTimestamp, relayer, transactionHash, slashed, fee } }`;

return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3RelayRecords);
}
async queryWithdrawStatus(
Expand All @@ -79,10 +99,14 @@ export class Lnv3SuperService extends SourceService {
): Promise<Lnv3WithdrawStatus> {
const query = `query { lnv3TransferRecord(id: "${transferId}") { id, remoteChainId, hasWithdrawn }}`;
return await axios
.post(url, {
query: query,
variables: null,
}, { timeout: 10000 })
.post(
url,
{
query: query,
variables: null,
},
{ timeout: 10000 }
)
.then((res) => res.data?.data?.lnv3TransferRecord);
}
}

0 comments on commit b7929be

Please sign in to comment.