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

feat: molecule codec #88

Merged
merged 26 commits into from
Dec 9, 2024
Merged

Conversation

ashuralyk
Copy link
Contributor

#Description

migrate whole @ckb-lumos/codec package into ccc and replace with this new codec of ccc for native Spore package, which test cases have passed

Copy link

changeset-bot bot commented Nov 26, 2024

🦋 Changeset detected

Latest commit: bfbab2c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
@ckb-ccc/core Patch
@ckb-ccc/spore Patch
@ckb-ccc/ccc Patch
@ckb-ccc/eip6963 Patch
@ckb-ccc/joy-id Patch
@ckb-ccc/lumos-patches Patch
@ckb-ccc/nip07 Patch
@ckb-ccc/okx Patch
@ckb-ccc/rei Patch
@ckb-ccc/uni-sat Patch
@ckb-ccc/utxo-global Patch
@ckb-ccc/xverse Patch
ckb-ccc Patch
@ckb-ccc/connector Patch
@ckb-ccc/examples Patch
@ckb-ccc/connector-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

packages/core/src/codec/number.ts Outdated Show resolved Hide resolved
packages/core/src/codec/number.ts Outdated Show resolved Hide resolved
packages/core/src/codec/utils.ts Outdated Show resolved Hide resolved
packages/core/src/codec/base.ts Outdated Show resolved Hide resolved
packages/core/src/codec/base.ts Outdated Show resolved Hide resolved
packages/core/src/codec/blockchain.ts Outdated Show resolved Hide resolved
packages/core/src/codec/blockchain.ts Outdated Show resolved Hide resolved
packages/core/src/codec/blockchain.ts Outdated Show resolved Hide resolved
packages/core/src/codec/molecule/index.ts Outdated Show resolved Hide resolved
packages/core/src/barrel.ts Outdated Show resolved Hide resolved
numToBytes,
} from "../num/index.js";

export interface Codec<Encodable, Decodable = BytesLike> {
Copy link
Member

Choose a reason for hiding this comment

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

export interface Codec<Encodable, Decoded = Encodable> {
  byteLength?: number;
  encode: (encodable: Encodable) => Bytes;
  decode: (decodable: BytesLike) => Decoded;
}

So we can have

const TransactionCodec: Codec<ccc.TransactionLike, ccc.Transaction>;

How do you think? It seems that the original Decodable is never used.

Copy link
Contributor Author

@ashuralyk ashuralyk Dec 5, 2024

Choose a reason for hiding this comment

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

good pattern, but I'm afraid it's not possible to explicitly declare the Decoded as ccc.Transaction, because the generic Decoded cannot recognize the from method in ccc.Transaction, while we cannot directly use as to cast ccc.TransactionLike to ccc.Transaction.

so in case of molecule, I think making the Decoded as xxxLike can fit into most of cases, for example, users should manually use ccc.Transaction.from to cast the decoded result of molecule decoders

Copy link
Member

Choose a reason for hiding this comment

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

good pattern, but I'm afraid it's not possible to explicitly declare the Decoded as ccc.Transaction, because the generic Decoded cannot recognize the from method in ccc.Transaction, while we cannot directly use as to cast ccc.TransactionLike to ccc.Transaction.

so in case of molecule, I think making the Decoded as xxxLike can fit into most of cases, for example, users should manually use ccc.Transaction.from to cast the decoded result of molecule decoders

Maybe we can add a method?

Codec<Encodable, Decoded>.map<NewDecoded>(transformer: (decoded: Decoded) => NewDecoded): Codec<Encodable, NewDecoded>

@@ -6,6 +6,7 @@ export * from "./fixedPoint/index.js";
export * from "./hasher/index.js";
export * from "./hex/index.js";
export * from "./keystore/index.js";
export * as molecule from "./molecule/index.js";
Copy link
Member

Choose a reason for hiding this comment

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

Maybe mol for easier typing?

packages/core/src/molecule/codec.advanced.ts Outdated Show resolved Hide resolved
itemCodec.encode(item),
);
return bytesConcat(
Uint32LE.encode(BigInt(userDefinedItems.length)),
Copy link
Member

Choose a reason for hiding this comment

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

See above, this is why Uint32LE should be Codec<NumLike, Num>

Uint32LE.encode(BigInt(userDefinedItems.length)),
encodedArray.reduce(
(concated, item) => bytesConcat(concated, item),
new ArrayBuffer(0),
Copy link
Member

Choose a reason for hiding this comment

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

Bytes is Uint8Array, new Uint8Array([]) is more precise or [] is simpler.

packages/core/src/molecule/codec.advanced.ts Outdated Show resolved Hide resolved
const field = keys[i];
const codec = codecLayout[field];
const payload = value.slice(start, end);
Object.assign(object, { [field]: codec.decode(payload) });
Copy link
Member

Choose a reason for hiding this comment

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

Why not just object[field] = codec.decode(payload);?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

R extends object = { [key in keyof T]: EncodableType<T[key]> },
>(codecLayout: T): Codec<R> {
const codecArray = Object.values(codecLayout);
if (codecArray.find((codec) => codec.byteLength === undefined)) {
Copy link
Member

Choose a reason for hiding this comment

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

Array.some()

packages/core/src/molecule/codec.ts Outdated Show resolved Hide resolved
packages/core/src/molecule/codec.advanced.ts Outdated Show resolved Hide resolved
@ashuralyk ashuralyk requested a review from Hanssen0 December 9, 2024 03:24
@ashuralyk
Copy link
Contributor Author

@Hanssen0 already merged "spore searcher" into this PR, if no more problems, this PR could also be merged.

@Hanssen0 Hanssen0 merged commit f07a506 into ckb-devrel:master Dec 9, 2024
1 check passed
@Hanssen0 Hanssen0 mentioned this pull request Dec 10, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants