Skip to content

Commit

Permalink
test: add unlock getNextBatchLockCell unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Mar 22, 2024
1 parent 09950de commit d79be55
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage
data
redis.conf
dump.rdb
.envrc
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@fastify/sensible": "^5.5.0",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@rgbpp-sdk/btc": "0.0.0-snap-20240321073744",
"@rgbpp-sdk/ckb": "0.0.0-snap-20240321073744",
"@sentry/node": "^7.102.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/services/unlocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class Unlocker implements IUnlocker {
/**
* Get next batch of BTC time lock cells
*/
private async getNextBatchLockCell() {
public async getNextBatchLockCell() {
const collect = this.collector.collect();
const cells: IndexerCell[] = [];

Expand Down
94 changes: 94 additions & 0 deletions test/services/unlocker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import container from '../../src/container';
import { describe, test, beforeEach, vi, expect, afterEach } from 'vitest';
import Unlocker from '../../src/services/unlocker';
import { Cell } from '@ckb-lumos/lumos';
import { BTCTimeLock, genBtcTimeLockScript } from '@rgbpp-sdk/ckb';

describe('Unlocker', () => {
let unlocker: Unlocker;

beforeEach(async () => {
const cradle = container.cradle;
// TODO: mock env.TRANSACTION_SPV_SERVICE_URL
unlocker = new Unlocker(cradle);
});

afterEach(() => {
vi.unstubAllEnvs();
});

function mockBtcTimeLockCell() {
vi.spyOn(BTCTimeLock, 'unpack').mockReturnValue({
after: 6,
btcTxid: '0x12345',
lockScript: {} as unknown as CKBComponents.Script,
});
vi.spyOn(unlocker['collector'], 'collect').mockImplementation(async function* () {
const toLock: CKBComponents.Script = {
args: '0xc0a45d9d7c024adcc8076c18b3f07c08de7c42120cdb7e6cbc05a28266b15b5f',
codeHash: '0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5',
hashType: 'data',
};
yield {
blockNumber: '0x123',
outPoint: {
txHash: '0x',
index: '0x0',
},
cellOutput: {
lock: genBtcTimeLockScript(toLock, false),
capacity: '0x123',
},
data: '0x',
} as Cell;
yield {
blockNumber: '0x456',
outPoint: {
txHash: '0x',
index: '0x0',
},
cellOutput: {
lock: genBtcTimeLockScript(toLock, false),
capacity: '0x456',
},
data: '0x',
} as Cell;
});
}

test('getNextBatchLockCell: should skip unconfirmed btc tx', async () => {
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getBlockchainInfo').mockResolvedValue({ blocks: 100 });
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getTransaction').mockResolvedValue({ blockheight: 95 });
mockBtcTimeLockCell();

const cells = await unlocker.getNextBatchLockCell();
expect(cells).toHaveLength(0);
});

test('getNextBatchLockCell: should return cells when btc tx is confirmed', async () => {
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getBlockchainInfo').mockResolvedValue({ blocks: 101 });
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getTransaction').mockResolvedValue({ blockheight: 95 });
mockBtcTimeLockCell();

const cells = await unlocker.getNextBatchLockCell();
expect(cells).toHaveLength(2);
});

test('getNextBatchLockCell: should break when cells reach batch size', async () => {
unlocker['cradle'].env.UNLOCKER_CELL_BATCH_SIZE = 1;

// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getBlockchainInfo').mockResolvedValue({ blocks: 101 });
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoind, 'getTransaction').mockResolvedValue({ blockheight: 95 });
mockBtcTimeLockCell();

const cells = await unlocker.getNextBatchLockCell();
expect(cells).toHaveLength(1);
});
});

0 comments on commit d79be55

Please sign in to comment.