Skip to content

Commit

Permalink
Merge pull request #169 from ckb-cell/release/v2.3.1
Browse files Browse the repository at this point in the history
Release/v2.3.1
  • Loading branch information
ahonn authored Jun 12, 2024
2 parents aa1f43e + 352416b commit ac342eb
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 85 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-assets-api",
"version": "2.3.0",
"version": "2.3.1",
"title": "Bitcoin/RGB++ Assets API",
"description": "",
"main": "index.js",
Expand Down
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
11 changes: 10 additions & 1 deletion src/routes/rgbpp/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ const transactionRoute: FastifyPluginCallback<Record<never, never>, Server, ZodT
tags: ['RGB++'],
body: z.object({
btc_txid: z.string(),
ckb_virtual_result: CKBVirtualResult,
ckb_virtual_result: CKBVirtualResult.or(z.string()).transform((value) => {
if (typeof value === 'string') {
value = JSON.parse(value);
}
const parsed = CKBVirtualResult.safeParse(value);
if (!parsed.success) {
throw new Error(`Invalid CKB virtual result: ${JSON.stringify(parsed.error.flatten())}`);
}
return parsed.data;
}),
}),
response: {
200: z.object({
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
73 changes: 73 additions & 0 deletions test/routes/rgbpp/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,79 @@ describe('/rgbpp/v1/transaction', () => {
sumInputsCapacity: '0x5e9f52f1f',
};

test('Post transaction', async () => {
const fastify = buildFastify();
await fastify.ready();

const response = await fastify.inject({
method: 'POST',
url: '/rgbpp/v1/transaction/ckb-tx',
headers: {
Authorization: `Bearer ${token}`,
Origin: 'https://test.com',
},
body: {
btc_txid: '0662cbe5a6666f36d3c6c431e22ef5073acf059d4ea1c7cd8d158b4107ab0d68',
ckb_virtual_result: mockCkbVirtualResult,
},
});

expect(response.statusCode).toBe(200);

await fastify.close();
});

test('Post transaction with ckb_virtual_result JSON string', async () => {
const fastify = buildFastify();
await fastify.ready();

const response = await fastify.inject({
method: 'POST',
url: '/rgbpp/v1/transaction/ckb-tx',
headers: {
Authorization: `Bearer ${token}`,
Origin: 'https://test.com',
},
body: {
btc_txid: '0662cbe5a6666f36d3c6c431e22ef5073acf059d4ea1c7cd8d158b4107ab0d68',
ckb_virtual_result: JSON.stringify(mockCkbVirtualResult),
},
});

expect(response.statusCode).toBe(200);

await fastify.close();
});

test('Post transaction with Invalid ckb_virtual_result JSON string', async () => {
const fastify = buildFastify();
await fastify.ready();

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { commitment, ...incorrectCkbVirtualResult } = mockCkbVirtualResult;

const response = await fastify.inject({
method: 'POST',
url: '/rgbpp/v1/transaction/ckb-tx',
headers: {
Authorization: `Bearer ${token}`,
Origin: 'https://test.com',
},
body: {
btc_txid: '0662cbe5a6666f36d3c6c431e22ef5073acf059d4ea1c7cd8d158b4107ab0d69',
ckb_virtual_result: JSON.stringify(incorrectCkbVirtualResult),
},
});
const data = response.json();

expect(response.statusCode).toBe(400);
expect(data).toEqual({
message: 'Invalid CKB virtual result: {"formErrors":[],"fieldErrors":{"commitment":["Required"]}}',
});

await fastify.close();
});

test('Get transaction job info with completed state', async () => {
const fastify = buildFastify();
await fastify.ready();
Expand Down
62 changes: 1 addition & 61 deletions test/services/__snapshots__/rgbpp.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -339,30 +339,7 @@ exports[`RgbppCollector > getRgbppCellsByBatchRequest: should return the batch r
"txIndex": "0x8",
},
],
[
{
"blockNumber": "0xcc3d9d",
"cellOutput": {
"capacity": "0x5e9f53e00",
"lock": {
"args": "0x01000000a7fd61a66986273e1e8500e276e5ade9e1d66658e73cc1a4123a440ef6f78d36",
"codeHash": "0x61ca7a4796a4eb19ca4f0d065cb9b10ddcf002f10f7cbb810c706cb6bb5c3248",
"hashType": "type",
},
"type": {
"args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"data": "0x00e1f505000000000000000000000000",
"outPoint": {
"index": "0x1",
"txHash": "0xefcada73aa020bfc4c4dc7192f9a6a6e6152af77d1bb2c2380314b4130751bc4",
},
"txIndex": "0x6",
},
],
[],
[
{
"blockNumber": "0xcb42df",
Expand Down Expand Up @@ -736,43 +713,6 @@ exports[`RgbppCollector > getRgbppCellsByBatchRequest: should return the utxo an
"vout": 2,
},
},
{
"cells": [
{
"blockNumber": "0xcc3d9d",
"cellOutput": {
"capacity": "0x5e9f53e00",
"lock": {
"args": "0x01000000a7fd61a66986273e1e8500e276e5ade9e1d66658e73cc1a4123a440ef6f78d36",
"codeHash": "0x61ca7a4796a4eb19ca4f0d065cb9b10ddcf002f10f7cbb810c706cb6bb5c3248",
"hashType": "type",
},
"type": {
"args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"data": "0x00e1f505000000000000000000000000",
"outPoint": {
"index": "0x1",
"txHash": "0xefcada73aa020bfc4c4dc7192f9a6a6e6152af77d1bb2c2380314b4130751bc4",
},
"txIndex": "0x6",
},
],
"utxo": {
"status": {
"block_hash": "000000000000000136b682e35e2ccce1a8b2d408f81607237ddf27f3acdb92ba",
"block_height": 2818010,
"block_time": 1716787908,
"confirmed": true,
},
"txid": "368df7f60e443a12a4c13ce75866d6e1e9ade576e200851e3e278669a661fda7",
"value": 546,
"vout": 1,
},
},
{
"cells": [
{
Expand Down

0 comments on commit ac342eb

Please sign in to comment.