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: Filter invalid btc block for btc time cells unlock #200

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ export const JWT_IGNORE_URLS = ['/token', '/docs', '/cron', '/internal', '/healt
export const SWAGGER_PROD_IGNORE_URLS = isAdminMode ? ['/token', '/cron'] : ['/cron'];

export const VERCEL_MAX_DURATION = 300;

// estimate time: 2024-04-03 09:45:17
// ref: https://mempool.space/testnet/block/000000000000000493ba5eebf0602f3e0e5381dd35f763a62ca7ea135343a0d6
export const BTC_TESTNET_SPV_START_BLOCK_HEIGHT = 2584900;

// estimate time: 2024-04-02 06:20:03
// ref: https://mempool.space/block/0000000000000000000077d98a103858c7d7cbc5ba67a4135f348a436bec1748
export const BTC_MAINNET_SPV_START_BLOCK_HEIGHT = 837300;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the starting block height for Signet be included?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2f59061

11 changes: 10 additions & 1 deletion src/services/unlocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { btcTxIdFromBtcTimeLockArgs } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';
import { BtcAssetsApi } from '@rgbpp-sdk/service';
import { Cradle } from '../container';
import { TestnetTypeMap } from '../constants';
import { BTC_MAINNET_SPV_START_BLOCK_HEIGHT, BTC_TESTNET_SPV_START_BLOCK_HEIGHT, TestnetTypeMap } from '../constants';

interface IUnlocker {
getNextBatchLockCell(): Promise<IndexerCell[]>;
Expand Down Expand Up @@ -54,6 +54,10 @@ export default class Unlocker implements IUnlocker {
return getBtcTimeLockScript(this.isMainnet, this.testnetType);
}

private get btcSpvStartBlockHeight() {
return this.isMainnet ? BTC_MAINNET_SPV_START_BLOCK_HEIGHT : BTC_TESTNET_SPV_START_BLOCK_HEIGHT;
}

/**
* Get next batch of BTC time lock cells
*/
Expand All @@ -73,6 +77,11 @@ export default class Unlocker implements IUnlocker {
const btcTx = await this.cradle.bitcoin.getTx({ txid: btcTxid });
const blockHeight = btcTx.status.block_height;

// skip if the block height of the btc txid is less than the BTC SPV start block height
if (blockHeight && blockHeight < this.btcSpvStartBlockHeight) {
continue;
}

// skip if btc tx not confirmed $after blocks yet
if (!blockHeight || blocks - blockHeight < after) {
continue;
Expand Down
12 changes: 6 additions & 6 deletions test/services/unlocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ describe('Unlocker', () => {

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

const cells = await unlocker.getNextBatchLockCell();
Expand All @@ -86,9 +86,9 @@ describe('Unlocker', () => {

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

const cells = await unlocker.getNextBatchLockCell();
Expand All @@ -99,9 +99,9 @@ describe('Unlocker', () => {
unlocker['cradle'].env.UNLOCKER_CELL_BATCH_SIZE = 1;

// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoin, 'getBlockchainInfo').mockResolvedValue({ blocks: 101 });
vi.spyOn(unlocker['cradle'].bitcoin, 'getBlockchainInfo').mockResolvedValue({ blocks: 2685001 });
// @ts-expect-error
vi.spyOn(unlocker['cradle'].bitcoin, 'getTx').mockResolvedValue({ status: { block_height: 95 } });
vi.spyOn(unlocker['cradle'].bitcoin, 'getTx').mockResolvedValue({ status: { block_height: 2684995 } });
mockBtcTimeLockCell();

const cells = await unlocker.getNextBatchLockCell();
Expand Down
Loading