Skip to content

Commit

Permalink
fix(molecule): incorrect fallback Bytes codec in codegen (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Aug 5, 2024
1 parent 3cf82d9 commit 66bbbfd
Show file tree
Hide file tree
Showing 14 changed files with 1,234 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-beans-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/molecule": minor
---

fix: incorrect fallback `Bytes` codec from codegen
7 changes: 2 additions & 5 deletions examples/molecule-codegen-dir/generated/common/basic_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
import { Uint32, Uint64, Uint128, DepType, HashType } from '../../customized'

const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down
7 changes: 2 additions & 5 deletions examples/molecule-codegen-dir/generated/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-l
import { Uint32, Uint64, Uint128, DepType, HashType } from '../customized'
import { AttrValue, SkillLevel, Uint8, Uint16 } from './common/basic_types'

const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down
9 changes: 3 additions & 6 deletions examples/molecule-codegen/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// This file is generated by @ckb-lumos/molecule, please do not modify it manually.
/* eslint-disable */

import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
import { Uint32, Uint64, Uint128, DepType, HashType } from './customized'

const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;

const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });

function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down Expand Up @@ -140,3 +136,4 @@ export const BlockV1 = table({
proposals: ProposalShortIdVec,
extension: Bytes
}, ['header', 'uncles', 'transactions', 'proposals', 'extension']);

4 changes: 3 additions & 1 deletion packages/molecule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
},
"devDependencies": {
"@ckb-lumos/base": "0.24.0-next.1",
"jsbi": "^4.1.0"
"jsbi": "^4.1.0",
"js-yaml": "^4.1.0",
"@types/js-yaml": "^4.0.9"
}
}
2 changes: 1 addition & 1 deletion packages/molecule/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ function outputSingleToConsole(schemaFile: string) {
codegenOption
);

console.log(generated);
console.log(generated.code);
}
7 changes: 2 additions & 5 deletions packages/molecule/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,9 @@ export function codegen(schema: string, options: Options = {}): CodegenResult {
import { bytes, createBytesCodec, createFixedBytesCodec, molecule } from "@ckb-lumos/codec";
${options.prepend || ""}
const { array, vector, union, option, struct, table } = molecule;
const { array, vector, union, option, struct, table, byteVecOf } = molecule;
const fallbackBytesCodec = createBytesCodec({
pack: bytes.bytify,
unpack: bytes.hexify,
});
const fallbackBytesCodec = byteVecOf({ pack: bytes.bytify, unpack: bytes.hexify });
function createFallbackFixedBytesCodec(byteLength: number) {
return createFixedBytesCodec({
Expand Down
5 changes: 5 additions & 0 deletions packages/molecule/tests/codegen/customization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { molecule } from "@ckb-lumos/codec";

// the compiler does not support empty block declaration
// so the Table0 is manually declared
export const Table0 = molecule.table({}, []);
Loading

1 comment on commit 66bbbfd

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🚀 New canary release: 0.0.0-canary-66bbbfd-20240805132534

npm install @ckb-lumos/[email protected]

Please sign in to comment.