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

Commit

Permalink
chore: update to scale 0.9.0 (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Nov 7, 2022
1 parent 628dca3 commit 187199b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ await Promise.all([
}],
outDir,
mappings: {
"https://deno.land/x/scale@v0.8.2/mod.ts": {
"https://deno.land/x/scale@v0.9.0/mod.ts": {
name: "scale-codec",
version: "^0.8.2",
version: "^0.9.0",
},
"https://deno.land/x/[email protected]/mod.ts": {
name: "zones",
Expand Down
6 changes: 3 additions & 3 deletions codegen/codecVisitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ for (const config of T.configs) {
const deriveCodec = M.DeriveCodec(metadata.tys);
const derivedCodecs = metadata.tys.map(deriveCodec);
const codegenCodecs = codegen._metadata.types;
const origInspect = Codec.prototype._inspect!;
const origInspect = Codec.prototype["_inspect"]!;
let inspecting = 0;
Codec.prototype._inspect = function(inspect) {
Codec.prototype["_inspect"] = function(inspect) {
if (inspecting) {
const di = derivedCodecs.indexOf(this);
if (di !== -1) return "$" + di;
Expand All @@ -36,6 +36,6 @@ for (const config of T.configs) {
assertEquals(derivedCodecs[i], codegenCodecs[i]);
}
}
Codec.prototype._inspect = origInspect;
Codec.prototype["_inspect"] = origInspect;
});
}
2 changes: 1 addition & 1 deletion deps/scale.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/x/scale@v0.8.2/mod.ts";
export * from "https://deno.land/x/scale@v0.9.0/mod.ts";
2 changes: 1 addition & 1 deletion effects/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function metadata<Rest extends [blockHash?: Z.$<U.HexHash | undefined>]>(
try {
return M.fromPrefixedHex(call.result);
} catch (e) {
return e as $.CodecError;
return e as $.ScaleError;
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion frame_metadata/Codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type DeriveCodec = (typeI: number | M.Ty) => $.Codec<unknown>;
* All derived codecs for ZSTs will use this exact codec,
* so `derivedCodec === $null` is true iff the type is a ZST.
*/
export const $null = $.dummy(null);
export const $null = $.constant(null);

// TODO: tuple/array element skip optimization
export function DeriveCodec(tys: M.Ty[]): DeriveCodec {
Expand Down
6 changes: 4 additions & 2 deletions frame_metadata/Era.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export function mortalEra(period: bigint, current: bigint): Era {
}

export const $era: $.Codec<Era> = $.createCodec({
name: "era",
_metadata: null,
_metadata: $.metadata("$era"),
_staticSize: 2,
_encode(buffer, value) {
if (value.type === "Immortal") {
Expand Down Expand Up @@ -44,6 +43,9 @@ export const $era: $.Codec<Era> = $.createCodec({
}
}
},
_assert() {
// TODO
},
});

function maxN(a: bigint, b: bigint) {
Expand Down
9 changes: 5 additions & 4 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const totalSize = 1 + $address._staticSize + $sig._staticSize + toSignSize;

const $baseExtrinsic: $.Codec<Extrinsic> = $.createCodec({
name: "",
_metadata: null,
_metadata: [],
_staticSize: totalSize,
_encode(buffer, extrinsic) {
const firstByte = (+!!extrinsic.signature << 7) | extrinsic.protocolVersion;
Expand Down Expand Up @@ -164,11 +163,13 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
const { type: palletName, value: { type: methodName, ...args } } = call;
return { protocolVersion, signature, palletName, methodName, args };
},
_assert() {
// TODO
},
});

return $.withMetadata(
"$extrinsic",
[$extrinsic, props],
$.metadata("$extrinsic", $extrinsic, props),
$.lenPrefixed($baseExtrinsic),
);

Expand Down
10 changes: 6 additions & 4 deletions frame_metadata/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export function $storageKey(props: StorageKeyProps): $.Codec<unknown[]> {
if (props.storageEntry.hashers.length === 1) {
keyCodecs = [codec];
} else {
if (codec._metadata?.[0] !== $.tuple) {
if (codec._metadata[0]?.factory !== $.tuple) {
throw new Error("Expected key codec to be a tuple since there are multiple hashers");
}
keyCodecs = codec._metadata.slice(1) as any;
keyCodecs = codec._metadata[0]!.args;
}
} else {
keyCodecs = [];
Expand All @@ -40,8 +40,7 @@ export function $storageKey(props: StorageKeyProps): $.Codec<unknown[]> {
{} as Record<number, $.Codec<any[]>>,
);
return $.createCodec({
name: "$storageKey",
_metadata: [$storageKey, props],
_metadata: $.metadata("$storageKey", $storageKey, props),
_staticSize: $keys[Object.values($keys).length - 1]!._staticSize,
_encode(buffer, key) {
buffer.insertArray(palletHash);
Expand All @@ -54,5 +53,8 @@ export function $storageKey(props: StorageKeyProps): $.Codec<unknown[]> {
buffer.index += 32;
return $keys[Object.values($keys).length - 1]!._decode(buffer);
},
_assert() {
// TODO #362
},
});
}
4 changes: 2 additions & 2 deletions frame_metadata/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export interface Metadata {
extrinsic: ExtrinsicDef;
}
export const $metadata: $.Codec<Metadata> = $.object(
["magicNumber", $.constantPattern(magicNumber, $.u32)],
["version", $.constantPattern(14, $.u8)],
["magicNumber", $.constant<typeof magicNumber>(magicNumber, $.u32)],
["version", $.constant<14>(14, $.u8)],
["tys", $tys],
["pallets", $.array($pallet)],
["extrinsic", $extrinsicDef],
Expand Down
12 changes: 8 additions & 4 deletions frame_metadata/scale_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export class TyDecodeCtx {

const $compactU32 = $.compact($.u32);
export const $tys: $.Codec<Ty[]> = $.createCodec({
name: "tys",
_metadata: null,
_metadata: $.metadata("$tys"),
_staticSize: $compactU32._staticSize,
_encode(buffer, value) {
$.array($ty)._encode(buffer, value);
Expand All @@ -21,11 +20,13 @@ export const $tys: $.Codec<Ty[]> = $.createCodec({
}
return tys;
},
_assert() {
// TODO #362
},
});

export const $tyId: $.Codec<Ty> = $.createCodec({
name: "tyId",
_metadata: null,
_metadata: $.metadata("$tyId"),
_staticSize: $compactU32._staticSize,
_encode(buffer, value) {
$.compact($.u32)._encode(buffer, value.id);
Expand All @@ -35,6 +36,9 @@ export const $tyId: $.Codec<Ty> = $.createCodec({
const id = $compactU32._decode(buffer);
return ctx.tys?.[id] ?? { id } as any;
},
_assert() {
// TODO #362
},
});

export interface Field {
Expand Down
10 changes: 6 additions & 4 deletions hashers/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export abstract class Hasher {

function $hash<T>(hasher: Hasher, $inner: $.Codec<T>): $.Codec<T> {
return $.createCodec({
name: "$hash",
_metadata: [$hash, hasher, $inner],
_metadata: $.metadata("$hash", $hash, hasher, $inner),
_staticSize: hasher.digestLength + $inner._staticSize,
_encode: (buffer, value) => {
_encode(buffer, value) {
const hashArray = buffer.array.subarray(buffer.index, buffer.index += hasher.digestLength);
const cursor = hasher.concat
? buffer.createCursor($inner._staticSize)
Expand All @@ -45,11 +44,14 @@ function $hash<T>(hasher: Hasher, $inner: $.Codec<T>): $.Codec<T> {
hashing.dispose?.();
});
},
_decode: (buffer) => {
_decode(buffer) {
if (!hasher.concat) throw new DecodeNonTransparentKeyError();
buffer.index += hasher.digestLength;
return $inner._decode(buffer);
},
_assert(assert) {
$inner._assert(assert);
},
});
}

Expand Down

0 comments on commit 187199b

Please sign in to comment.