Skip to content

Commit

Permalink
Merge pull request #53 from ckb-cell/refactor/rgbpp-cell-capacity
Browse files Browse the repository at this point in the history
refactor(rgbpp-sdk/ckb): Check lock size and remove the same client cell
  • Loading branch information
duanyytop authored Mar 26, 2024
2 parents 91c5320 + 2fcc30a commit 1729406
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/ckb/src/rgbpp/btc-jump-ckb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { RgbppCkbVirtualTx, BtcJumpCkbVirtualTxParams, BtcJumpCkbVirtualTxResult } from '../types/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import { NoRgbppLiveCellError } from '../error';
import { append0x, calculateRgbppCellCapacity, calculateTransactionFee, u128ToLe } from '../utils';
import {
append0x,
calculateRgbppCellCapacity,
calculateTransactionFee,
isLockArgsSizeExceeded,
remove0x,
u128ToLe,
} from '../utils';
import {
buildPreLockArgs,
calculateCommitment,
Expand Down Expand Up @@ -55,6 +62,9 @@ export const genBtcJumpCkbVirtualTx = async ({
const outputsData = [append0x(u128ToLe(transferAmount))];

const toLock = addressToScript(toCkbAddress);
if (isLockArgsSizeExceeded(toLock.args)) {
throw new Error('The lock script size of the to ckb address is too large');
}
const outputs: CKBComponents.CellOutput[] = [
{
lock: genBtcTimeLockScript(toLock, isMainnet),
Expand Down
9 changes: 8 additions & 1 deletion packages/ckb/src/rgbpp/btc-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeOutPoint,
serializeWitnessArgs,
} from '@nervosnetwork/ckb-sdk-utils';
import {
Expand Down Expand Up @@ -71,6 +72,7 @@ export const buildBtcTimeCellsSpentTx = async ({
const witnesses: Hex[] = [];

const lockArgsSet: Set<string> = new Set();
const cellDepsSet: Set<string> = new Set();
for await (const { btcTimeCell, btcTxIndexInBlock } of sortedBtcTimeCellPairs) {
if (lockArgsSet.has(btcTimeCell.output.lock.args)) {
witnesses.push('0x');
Expand All @@ -82,7 +84,12 @@ export const buildBtcTimeCellsSpentTx = async ({
btcTxIndexInBlock,
confirmBlocks: BTC_JUMP_CONFIRMATION_BLOCKS,
});
cellDeps.push(buildSpvClientCellDep(spvClient));

if (!cellDepsSet.has(serializeOutPoint(spvClient))) {
cellDeps.push(buildSpvClientCellDep(spvClient));
cellDepsSet.add(serializeOutPoint(spvClient));
}

const btcTimeWitness = append0x(
serializeWitnessArgs({ lock: buildBtcTimeUnlockWitness(proof), inputType: '', outputType: '' }),
);
Expand Down
10 changes: 9 additions & 1 deletion packages/ckb/src/utils/ckb-tx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { describe, it, expect } from 'vitest';
import { calculateRgbppCellCapacity, calculateTransactionFee } from './ckb-tx';
import { calculateRgbppCellCapacity, calculateTransactionFee, isLockArgsSizeExceeded } from './ckb-tx';

describe('ckb tx utils', () => {
it('calculateTransactionFee', () => {
const fee = calculateTransactionFee(1245);
expect(BigInt(1370)).toBe(fee);
});

it('calculateTransactionFee', () => {
const longLockArgs = '0x06ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3f008906c849399';
expect(true).toBe(isLockArgsSizeExceeded(longLockArgs));

const shortLockArgs = '0x06ec22c2def100bba3e295a1ff279c490d227151bf3166a4f3';
expect(false).toBe(isLockArgsSizeExceeded(shortLockArgs));
});

it('calculateRgbppCellCapacity', () => {
const xudtType: CKBComponents.Script = {
codeHash: '0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb',
Expand Down
6 changes: 5 additions & 1 deletion packages/ckb/src/utils/ckb-tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BigNumber from 'bignumber.js';
import { remove0x } from './hex';
import { CKB_UNIT } from '../constants';
import { Hex } from '../types';

export const calculateTransactionFee = (txSize: number): bigint => {
const ratio = BigNumber(1000);
Expand All @@ -10,7 +11,10 @@ export const calculateTransactionFee = (txSize: number): bigint => {
};

// The BTC_TIME_CELL_INCREASED_SIZE is related to the specific lock script.
// We have left some redundancy based on the commonly used lock scripts.
// We assume that the maximum length of lock script args is 26 bytes. If it exceeds, an error will be thrown.
const LOCK_ARGS_HEX_MAX_SIZE = 26 * 2;
export const isLockArgsSizeExceeded = (args: Hex) => remove0x(args).length > LOCK_ARGS_HEX_MAX_SIZE;

const BTC_TIME_CELL_INCREASED_SIZE = 95;

// For simplicity, we keep the capacity of the RGBPP cell the same as the BTC time cell
Expand Down

1 comment on commit 1729406

@github-actions
Copy link

Choose a reason for hiding this comment

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

[{"name":"@rgbpp-sdk/btc","version":"0.0.0-snap-20240326062949"},{"name":"@rgbpp-sdk/ckb","version":"0.0.0-snap-20240326062949"}]

Please sign in to comment.