From 4206d7eacaefc273e33deea188cb0e41e8067e7b Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Wed, 24 Jul 2024 11:08:07 +0800 Subject: [PATCH 1/3] ref: Skip if the block height less than spv start block height --- src/constants.ts | 8 ++++++++ src/services/unlocker.ts | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/constants.ts b/src/constants.ts index a1b05e6b..c99b8e47 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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; diff --git a/src/services/unlocker.ts b/src/services/unlocker.ts index 623948c2..38d10aa4 100644 --- a/src/services/unlocker.ts +++ b/src/services/unlocker.ts @@ -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; @@ -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 */ @@ -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; From 29ec9664e94bc6082ac5a67b72c7f2d02915945e Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Wed, 24 Jul 2024 11:24:05 +0800 Subject: [PATCH 2/3] tests: Update mock block height for btc time cell unlocker --- test/services/unlocker.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/services/unlocker.test.ts b/test/services/unlocker.test.ts index 7512ca29..74a84ad9 100644 --- a/test/services/unlocker.test.ts +++ b/test/services/unlocker.test.ts @@ -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(); @@ -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(); @@ -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(); From 2f59061b58f4420738431c4598cbb8e5dc0fcfae Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Wed, 24 Jul 2024 11:56:13 +0800 Subject: [PATCH 3/3] chore: Add BTC signet SPV start block height --- src/constants.ts | 5 +++++ src/services/unlocker.ts | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index c99b8e47..d80c4590 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -33,6 +33,11 @@ export const VERCEL_MAX_DURATION = 300; // ref: https://mempool.space/testnet/block/000000000000000493ba5eebf0602f3e0e5381dd35f763a62ca7ea135343a0d6 export const BTC_TESTNET_SPV_START_BLOCK_HEIGHT = 2584900; +// estimate time: 2024-06-13 18:31:56 +// ref: https://mempool.space/signet/block/000000b2af39a66ec81d414b102804d975c5c4527adfd9bd3cabf2b7b4634737 +// Signet BTC SPV deployment time: https://pudge.explorer.nervos.org/transaction/0x61efdeddbaa0bb4132c0eb174b3e8002ff5ec430f61ba46f30768d683c516eec +export const BTC_SIGNET_SPV_START_BLOCK_HEIGHT = 199800; + // estimate time: 2024-04-02 06:20:03 // ref: https://mempool.space/block/0000000000000000000077d98a103858c7d7cbc5ba67a4135f348a436bec1748 export const BTC_MAINNET_SPV_START_BLOCK_HEIGHT = 837300; diff --git a/src/services/unlocker.ts b/src/services/unlocker.ts index 38d10aa4..f8ffbc2b 100644 --- a/src/services/unlocker.ts +++ b/src/services/unlocker.ts @@ -17,7 +17,12 @@ import { import { btcTxIdFromBtcTimeLockArgs } from '@rgbpp-sdk/ckb/lib/utils/rgbpp'; import { BtcAssetsApi } from '@rgbpp-sdk/service'; import { Cradle } from '../container'; -import { BTC_MAINNET_SPV_START_BLOCK_HEIGHT, BTC_TESTNET_SPV_START_BLOCK_HEIGHT, TestnetTypeMap } from '../constants'; +import { + BTC_MAINNET_SPV_START_BLOCK_HEIGHT, + BTC_SIGNET_SPV_START_BLOCK_HEIGHT, + BTC_TESTNET_SPV_START_BLOCK_HEIGHT, + TestnetTypeMap, +} from '../constants'; interface IUnlocker { getNextBatchLockCell(): Promise; @@ -55,7 +60,14 @@ export default class Unlocker implements IUnlocker { } private get btcSpvStartBlockHeight() { - return this.isMainnet ? BTC_MAINNET_SPV_START_BLOCK_HEIGHT : BTC_TESTNET_SPV_START_BLOCK_HEIGHT; + const network = this.cradle.env.NETWORK; + if (network === 'mainnet') { + return BTC_MAINNET_SPV_START_BLOCK_HEIGHT; + } + if (network === 'testnet') { + return BTC_TESTNET_SPV_START_BLOCK_HEIGHT; + } + return BTC_SIGNET_SPV_START_BLOCK_HEIGHT; } /**