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

fix: reduce redis data size, exclude UTXOs data from job return value #168

Merged
merged 3 commits into from
Jun 11, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ data
redis-data
dump.rdb
.envrc
.env.signet
4 changes: 2 additions & 2 deletions src/plugins/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export default fp(async (fastify) => {
onCompleted: async (job) => {
fastify.log.info(`[UTXOSyncer] job completed: ${job.id}`);
if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
const { btcAddress, utxos } = job.returnvalue;
const { btcAddress } = job.data;
const rgbppCollector: RgbppCollector = fastify.container.resolve('rgbppCollector');
await rgbppCollector.enqueueCollectJob(btcAddress, utxos, true);
await rgbppCollector.enqueueCollectJob(btcAddress, true);
}
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/routes/bitcoin/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
no_cache === 'true',
);
if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
await fastify.rgbppCollector.enqueueCollectJob(address, utxos);
await fastify.rgbppCollector.enqueueCollectJob(address);
}

const rgbppUtxoMap = rgbppUtxoCellsPairs.reduce((map, { utxo }) => {
Expand Down Expand Up @@ -140,7 +140,7 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
? await fastify.rgbppCollector.getRgbppUtxoCellsPairs(address, utxos, no_cache === 'true')
: [];
if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
await fastify.rgbppCollector.enqueueCollectJob(address, utxos);
await fastify.rgbppCollector.enqueueCollectJob(address);
}
const rgbppUtxoSet = new Set(rgbppUtxoCellsPairs.map((pair) => pair.utxo.txid + ':' + pair.utxo.vout));

Expand Down
2 changes: 1 addition & 1 deletion src/routes/rgbpp/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
no_cache === 'true',
);
if (env.RGBPP_COLLECT_DATA_CACHE_ENABLE) {
await fastify.rgbppCollector.enqueueCollectJob(btc_address, utxos);
await fastify.rgbppCollector.enqueueCollectJob(btc_address);
}
const cells = rgbppUtxoCellsPairs.map((pair) => pair.cells).flat();
return cells;
Expand Down
2 changes: 1 addition & 1 deletion src/services/base/queue-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default abstract class BaseQueueWorker<T, R> {
);
}

abstract process(job: Job<T>): Promise<R>;
abstract process(job: Job<T>): Promise<R | void>;

/**
* Add a job to the queue
Expand Down
27 changes: 13 additions & 14 deletions src/services/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export type RgbppUtxoCellsPair = {

interface IRgbppCollectRequest {
btcAddress: string;
utxos: UTXO[];
}

interface IRgbppCollectJobReturn {
Expand Down Expand Up @@ -80,12 +79,9 @@ export default class RgbppCollector extends BaseQueueWorker<IRgbppCollectRequest
* @param err - the error
*/
private captureJobExceptionToSentryScope(job: Job<IRgbppCollectRequest>, err: Error) {
const { btcAddress, utxos } = job.data;
const { btcAddress } = job.data;
Sentry.withScope((scope) => {
scope.setTag('btcAddress', btcAddress);
scope.setContext('utxos', {
utxos: JSON.stringify(utxos),
});
this.cradle.logger.error(err);
scope.captureException(err);
});
Expand Down Expand Up @@ -235,18 +231,21 @@ export default class RgbppCollector extends BaseQueueWorker<IRgbppCollectRequest
* Enqueue a collect job to the queue
* @param utxos - the utxos to collect
*/
public async enqueueCollectJob(
btcAddress: string,
utxos: UTXO[],
allowDuplicate?: boolean,
): Promise<Job<IRgbppCollectRequest>> {
public async enqueueCollectJob(btcAddress: string, allowDuplicate?: boolean): Promise<Job<IRgbppCollectRequest>> {
let jobId = btcAddress;
if (allowDuplicate) {
// add a timestamp to the job id to allow duplicate jobs
// used for the case that the utxos are updated
jobId = `${btcAddress}:${Date.now()}`;
}
return this.addJob(jobId, { btcAddress, utxos });
return this.addJob(
jobId,
{ btcAddress },
{
removeOnComplete: true,
removeOnFail: true,
},
);
}

/**
Expand All @@ -256,10 +255,10 @@ export default class RgbppCollector extends BaseQueueWorker<IRgbppCollectRequest
*/
public async process(job: Job<IRgbppCollectRequest>) {
try {
const { btcAddress, utxos } = job.data;
const { btcAddress } = job.data;
const utxos = await this.cradle.utxoSyncer.getUtxosByAddress(btcAddress);
const pairs = await this.collectRgbppUtxoCellsPairs(utxos);
const data = await this.saveRgbppUtxoCellsPairsToCache(btcAddress, pairs);
return data;
await this.saveRgbppUtxoCellsPairsToCache(btcAddress, pairs);
} catch (e) {
const { message, stack } = e as Error;
const error = new RgbppCollectorError(message);
Expand Down
6 changes: 4 additions & 2 deletions src/services/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export default class UTXOSyncer extends BaseQueueWorker<IUTXOSyncRequest, IUTXOS
// https://github.com/taskforcesh/bullmq/blob/cce0774cffcee591407eee4d4530daa37aab3eca/src/classes/repeat.ts#L51
endDate: Date.now() + this.cradle.env.UTXO_SYNC_REPEAT_EXPRIED_DURATION,
},
removeOnComplete: true,
removeOnFail: true,
},
);
}
Expand All @@ -167,7 +169,7 @@ export default class UTXOSyncer extends BaseQueueWorker<IUTXOSyncRequest, IUTXOS
return this.enqueueSyncJobThrottle(btcAddress);
}

public async process(job: Job<IUTXOSyncRequest>): Promise<IUTXOSyncJobReturn> {
public async process(job: Job<IUTXOSyncRequest>) {
try {
const { btcAddress } = job.data;
if (!validateBitcoinAddress(btcAddress)) {
Expand All @@ -189,7 +191,7 @@ export default class UTXOSyncer extends BaseQueueWorker<IUTXOSyncRequest, IUTXOS

const utxos = await this.cradle.bitcoin.getAddressTxsUtxo({ address: btcAddress });
const data = { btcAddress, utxos, txsHash };
return this.dataCache.set(btcAddress, data);
await this.dataCache.set(btcAddress, data);
} catch (e) {
const { message, stack } = e as Error;
const error = new UTXOSyncerError(message);
Expand Down
Loading