Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add lenPrefixedWrapper to support WrapperOpaque and `Wrappe…
Browse files Browse the repository at this point in the history
…rKeepOpaque` types (#288)
  • Loading branch information
tjjfvi authored Oct 12, 2022
1 parent cff5d2e commit b118d15
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 20 deletions.
9 changes: 8 additions & 1 deletion codegen/codecVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function createCodecVisitor(
files.set("codecs.ts", {
getContent: () => [
"\n",
["import { ChainError, BitSequence, Era, $, $era, $null } from", S.string(importSource)],
[
"import { ChainError, BitSequence, Era, $, $era, $null, $lenPrefixed } from",
S.string(importSource),
],
[`import type * as t from "./mod.ts"`],
...codecs,
[
Expand Down Expand Up @@ -51,6 +54,7 @@ export function createCodecVisitor(
},
compact: () => null,
bitSequence: () => null,
lenPrefixedWrapper: () => null,
circular: () => null,
});

Expand Down Expand Up @@ -164,6 +168,9 @@ export function createCodecVisitor(
era(ty) {
return addCodecDecl(ty, "$era");
},
lenPrefixedWrapper(ty, inner) {
return addCodecDecl(ty, ["$lenPrefixed(", this.visit(inner), ")"]);
},
circular(ty) {
return ["$.deferred(() =>", getName(getRawCodecPath(ty)), ")"];
},
Expand Down
1 change: 1 addition & 0 deletions codegen/genMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function genMetadata(metadata: M.Metadata, decls: Decl[]) {
primitive: () => false,
compact: () => false,
bitSequence: () => false,
lenPrefixedWrapper: () => false,
circular: () => false,
});

Expand Down
3 changes: 3 additions & 0 deletions codegen/typeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export function createTypeVisitor(tys: M.Ty[], decls: Decl[]) {
era() {
return "Era";
},
lenPrefixedWrapper(_ty, inner) {
return this.visit(inner);
},
circular(ty) {
return getPath(tys, ty) || this._visit(ty);
},
Expand Down
4 changes: 4 additions & 0 deletions frame_metadata/Codec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as $ from "../deps/scale.ts";
import { $lenPrefixed } from "../util/$lenPrefixed.ts";
import { $era } from "./Era.ts";
import type * as M from "./mod.ts";
import { TyVisitor } from "./TyVisitor.ts";
Expand Down Expand Up @@ -102,6 +103,9 @@ export function DeriveCodec(tys: M.Ty[]): DeriveCodec {
era() {
return $era;
},
lenPrefixedWrapper(_ty, inner) {
return $lenPrefixed(this.visit(inner));
},
circular(ty) {
return $.deferred(() => this.cache[ty.id]!);
},
Expand Down
21 changes: 2 additions & 19 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as $ from "../deps/scale.ts";
import { assert } from "../deps/std/testing/asserts.ts";
import * as H from "../hashers/mod.ts";
import * as ss58 from "../ss58/mod.ts";
import { $lenPrefixed } from "../util/$lenPrefixed.ts";
import { hex } from "../util/mod.ts";
import { $null, DeriveCodec } from "./Codec.ts";
import { Metadata } from "./Metadata.ts";
Expand Down Expand Up @@ -165,25 +166,7 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
},
});

return $.createCodec({
_metadata: [$extrinsic, props],
_staticSize: $.compactU32._staticSize + $baseExtrinsic._staticSize,
_encode(buffer, extrinsic) {
const lengthCursor = buffer.createCursor($.compactU32._staticSize);
const contentCursor = buffer.createCursor($baseExtrinsic._staticSize);
$baseExtrinsic._encode(contentCursor, extrinsic);
buffer.waitForBuffer(contentCursor, () => {
const length = contentCursor.finishedSize + contentCursor.index;
$.compactU32._encode(lengthCursor, length);
lengthCursor.close();
contentCursor.close();
});
},
_decode(buffer) {
const length = $.compactU32._decode(buffer);
return $baseExtrinsic.decode(buffer.array.subarray(buffer.index, buffer.index += length));
},
});
return $lenPrefixed($baseExtrinsic);

function findExtrinsicTypeParam(name: string) {
return metadata.extrinsic.ty.params.find((x) => x.name === name)?.ty;
Expand Down
4 changes: 4 additions & 0 deletions frame_metadata/TyVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface TyVisitorMethods<T> {

era?(ty: Ty & UnionTyDef): T;

lenPrefixedWrapper(ty: Ty & StructTyDef, inner: Ty): T;

circular(ty: Ty): T;
}

Expand Down Expand Up @@ -74,6 +76,8 @@ export class TyVisitor<T> {
return this.map(ty, ty.params[0]!.ty!, ty.params[1]!.ty!);
} else if (this.set && ty.path[0] === "BTreeSet") {
return this.set(ty, ty.params[0]!.ty!);
} else if (ty.path.at(-1) === "WrapperOpaque" || ty.path.at(-1) === "WrapperKeepOpaque") {
return this.lenPrefixedWrapper(ty, ty.params[0]!.ty!);
} else if (ty.fields.length === 0) {
return this.unitStruct(ty);
} else if (ty.fields[0]!.name === undefined) {
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * as M from "./frame_metadata/mod.ts";
export { $era, $null, ChainError, type Era } from "./frame_metadata/mod.ts";
export { kusama, moonbeam, polkadot, rococo, westend } from "./known/mod.ts"; // TODO: get rid of this!
export {
$lenPrefixed,
type CreateWatchHandler,
hex,
mapCreateWatchHandler,
Expand Down
23 changes: 23 additions & 0 deletions util/$lenPrefixed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as $ from "../deps/scale.ts";

export function $lenPrefixed<T>($inner: $.Codec<T>): $.Codec<T> {
return $.createCodec({
_metadata: [$lenPrefixed, $inner],
_staticSize: $.compactU32._staticSize + $inner._staticSize,
_encode(buffer, extrinsic) {
const lengthCursor = buffer.createCursor($.compactU32._staticSize);
const contentCursor = buffer.createCursor($inner._staticSize);
$inner._encode(contentCursor, extrinsic);
buffer.waitForBuffer(contentCursor, () => {
const length = contentCursor.finishedSize + contentCursor.index;
$.compactU32._encode(lengthCursor, length);
lengthCursor.close();
contentCursor.close();
});
},
_decode(buffer) {
const length = $.compactU32._decode(buffer);
return $inner.decode(buffer.array.subarray(buffer.index, buffer.index += length));
},
});
}
1 change: 1 addition & 0 deletions util/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./$lenPrefixed.ts";
export * from "./branded.ts";
export * from "./error.ts";
export * as hex from "./hex.ts";
Expand Down

0 comments on commit b118d15

Please sign in to comment.