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

refactor: optimize performance of queryBtcTimeLockTxByBtcTx() #212

Merged
merged 3 commits into from
Aug 23, 2024
Merged
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
5 changes: 2 additions & 3 deletions src/routes/rgbpp/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,9 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
const ckbTx = await fastify.ckb.rpc.getTransaction(rgbppLockTx.txHash);
setCkbTxAndStatus(ckbTx);
} else {
// XXX: this is a performance bottleneck, need to optimize
const btcTimeLockTx = await fastify.rgbppCollector.queryBtcTimeLockTxByBtcTxId(btcTx.txid);
const btcTimeLockTx = await fastify.rgbppCollector.queryBtcTimeLockTxByBtcTx(btcTx);
if (btcTimeLockTx) {
setCkbTxAndStatus(btcTimeLockTx as TransactionWithStatus);
setCkbTxAndStatus(btcTimeLockTx);
}
}
return isomorphicTx;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/rgbpp/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const transactionRoute: FastifyPluginCallback<Record<never, never>, Server, ZodT
reply.header(CUSTOM_HEADERS.ResponseCacheable, 'true');
return { txhash: rgbppLockTx.txHash };
}
const btcTimeLockTx = await fastify.rgbppCollector.queryBtcTimeLockTxByBtcTxId(btc_txid);
const btcTimeLockTx = await fastify.rgbppCollector.queryBtcTimeLockTxByBtcTx(btcTx);
if (btcTimeLockTx) {
reply.header(CUSTOM_HEADERS.ResponseCacheable, 'true');
return { txhash: btcTimeLockTx.transaction.hash };
Expand Down
68 changes: 35 additions & 33 deletions src/services/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as Sentry from '@sentry/node';
import {
IndexerCell,
leToU128,
isScriptEqual,
buildRgbppLockArgs,
genRgbppLockScript,
btcTxIdFromBtcTimeLockArgs,
Expand All @@ -13,19 +12,18 @@ import {
} from '@rgbpp-sdk/ckb';
import { remove0x } from '@rgbpp-sdk/btc';
import { unpackRgbppLockArgs } from '@rgbpp-sdk/btc/lib/ckb/molecule';
import { groupBy, uniq, findLastIndex } from 'lodash';
import { groupBy, findLastIndex } from 'lodash';
import { z } from 'zod';
import { Job } from 'bullmq';
import { BI, RPC, Script } from '@ckb-lumos/lumos';
import { TransactionWithStatus } from '@ckb-lumos/base';
import { computeScriptHash } from '@ckb-lumos/lumos/utils';
import { Cell, XUDTBalance } from '../routes/rgbpp/types';
import { Transaction, UTXO } from './bitcoin/schema';
import BaseQueueWorker from './base/queue-worker';
import DataCache from './base/data-cache';
import { Cradle } from '../container';
import { isCommitmentMatchToCkbTx, tryGetCommitmentFromBtcTx } from '../utils/commitment';
import { getBtcTimeLock, isBtcTimeLock, isRgbppLock } from '../utils/lockscript';
import { getRgbppLock, isBtcTimeLock, isRgbppLock } from '../utils/lockscript';
import { IS_MAINNET, TESTNET_TYPE } from '../constants';

type GetCellsParams = Parameters<RPC['getCells']>;
Expand Down Expand Up @@ -270,37 +268,41 @@ export default class RgbppCollector extends BaseQueueWorker<IRgbppCollectRequest
return null;
}

public async queryBtcTimeLockTxByBtcTxId(btcTxId: string) {
// XXX: unstable, need to be improved: https://github.com/ckb-cell/btc-assets-api/issues/45
const btcTimeLockTxs = await this.cradle.ckb.indexer.getTransactions(
{
script: {
...getBtcTimeLock(),
args: '0x',
},
scriptType: 'lock',
groupByTransaction: true,
},
{
order: 'asc',
},
);
public async queryBtcTimeLockTxByBtcTx(btcTx: Transaction) {
const rgbppLock = getRgbppLock();
const relatedCkbTxs = (
await Promise.all(
btcTx.vin.map(({ txid, vout }) => {
const args = buildRgbppLockArgs(vout, txid);
return this.cradle.ckb.rpc.getTransactions(
{
script: {
...rgbppLock,
args,
},
scriptType: 'lock',
groupByTransaction: true,
},
'asc',
'0x64',
);
}),
)
)
.map(({ objects }) => objects)
.flat();

const txHashes = uniq(btcTimeLockTxs.objects.map(({ txHash }) => txHash));
const batchRequest = this.cradle.ckb.rpc.createBatchRequest(txHashes.map((txHash) => ['getTransaction', txHash]));
const transactions: TransactionWithStatus[] = await batchRequest.exec();
if (transactions.length > 0) {
for (const tx of transactions) {
const isBtcTimeLockTx = tx.transaction.outputs.some((output) => {
if (!isScriptEqual(output.lock, getBtcTimeLock())) {
return false;
}
const outputBtcTxId = btcTxIdFromBtcTimeLockArgs(output.lock.args);
return remove0x(outputBtcTxId) === btcTxId;
});
if (isBtcTimeLockTx) {
return tx;
for (const tx of relatedCkbTxs) {
const ckbTx = await this.cradle.ckb.rpc.getTransaction(tx.txHash);
const isBtcTimeLockTx = ckbTx.transaction.outputs.some((output) => {
if (!isBtcTimeLock(output.lock)) {
return false;
}
const outputBtcTxId = btcTxIdFromBtcTimeLockArgs(output.lock.args);
return remove0x(outputBtcTxId) === btcTx.txid;
});
if (isBtcTimeLockTx) {
return ckbTx;
}
}
return null;
Expand Down
Loading
Loading