Skip to content

Commit

Permalink
fix: molecule dynVec for empty vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Dec 20, 2024
1 parent 13c66d3 commit 7118921
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 9 additions & 5 deletions packages/core/src/molecule/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function fixedItemVec<Encodable, Decoded>(

try {
const decodedArray: Array<Decoded> = [];
for (let offset = 0; offset < byteLength; offset += itemByteLength) {
for (let offset = 4; offset < byteLength; offset += itemByteLength) {
decodedArray.push(
itemCodec.decode(value.slice(offset, offset + itemByteLength)),
);
Expand Down Expand Up @@ -151,16 +151,20 @@ export function dynItemVec<Encodable, Decoded>(
},
decode(buffer) {
const value = bytesFrom(buffer);
if (value.byteLength < 4) {
throw new Error(
`dynItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`,
);
}
const byteLength = uint32From(value.slice(0, 4));
if (byteLength !== value.byteLength) {
throw new Error(
`dynItemVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`,
);
}
if (value.byteLength < 4) {
throw new Error(
`fixedItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`,
);

if (byteLength === 4) {
return [];
}

const offset = uint32From(value.slice(4, 8));
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/molecule/predefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,25 @@ export const CellDep: Codec<ckb.CellDepLike, ckb.CellDep> = struct({
}).map({ outMap: ckb.CellDep.from });
export const CellDepVec = vector(CellDep);

export const Transaction: Codec<ckb.TransactionLike, ckb.Transaction> = table({
export const RawTransaction = table({
version: Uint32,
cellDeps: CellDepVec,
headerDeps: Byte32Vec,
inputs: CellInputVec,
outputs: CellOutputVec,
outputsData: BytesVec,
witnesses: BytesVec,
}).map({ outMap: ckb.Transaction.from });

export const Transaction: Codec<ckb.TransactionLike, ckb.Transaction> = table({
raw: RawTransaction,
witnesses: BytesVec,
}).map({
inMap: (txLike: ckb.TransactionLike) => {
const tx = ckb.Transaction.from(txLike);
return {
raw: tx,
witnesses: tx.witnesses,
};
},
outMap: (tx) => ckb.Transaction.from({ ...tx.raw, witnesses: tx.witnesses }),
});

0 comments on commit 7118921

Please sign in to comment.