From 187199b5ed84cf9e16682cb1ed33ef57222f0f77 Mon Sep 17 00:00:00 2001 From: T6 Date: Sun, 6 Nov 2022 18:23:44 -0800 Subject: [PATCH] chore: update to scale 0.9.0 (#363) --- _tasks/dnt.ts | 4 ++-- codegen/codecVisitor.test.ts | 6 +++--- deps/scale.ts | 2 +- effects/metadata.ts | 2 +- frame_metadata/Codec.ts | 2 +- frame_metadata/Era.ts | 6 ++++-- frame_metadata/Extrinsic.ts | 9 +++++---- frame_metadata/Key.ts | 10 ++++++---- frame_metadata/Metadata.ts | 4 ++-- frame_metadata/scale_info.ts | 12 ++++++++---- hashers/mod.ts | 10 ++++++---- 11 files changed, 39 insertions(+), 28 deletions(-) diff --git a/_tasks/dnt.ts b/_tasks/dnt.ts index b94b99aaf..cf46b1dd1 100755 --- a/_tasks/dnt.ts +++ b/_tasks/dnt.ts @@ -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/zones@v0.1.0-beta.6/mod.ts": { name: "zones", diff --git a/codegen/codecVisitor.test.ts b/codegen/codecVisitor.test.ts index da8ed543c..3c9763184 100644 --- a/codegen/codecVisitor.test.ts +++ b/codegen/codecVisitor.test.ts @@ -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; @@ -36,6 +36,6 @@ for (const config of T.configs) { assertEquals(derivedCodecs[i], codegenCodecs[i]); } } - Codec.prototype._inspect = origInspect; + Codec.prototype["_inspect"] = origInspect; }); } diff --git a/deps/scale.ts b/deps/scale.ts index 5c3699acc..0218158f1 100644 --- a/deps/scale.ts +++ b/deps/scale.ts @@ -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"; diff --git a/effects/metadata.ts b/effects/metadata.ts index c76753b07..8ccd9225b 100644 --- a/effects/metadata.ts +++ b/effects/metadata.ts @@ -15,7 +15,7 @@ export function metadata]>( try { return M.fromPrefixedHex(call.result); } catch (e) { - return e as $.CodecError; + return e as $.ScaleError; } }, ); diff --git a/frame_metadata/Codec.ts b/frame_metadata/Codec.ts index 3f4a3da5a..e0d31af2a 100644 --- a/frame_metadata/Codec.ts +++ b/frame_metadata/Codec.ts @@ -9,7 +9,7 @@ export type DeriveCodec = (typeI: number | M.Ty) => $.Codec; * 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 { diff --git a/frame_metadata/Era.ts b/frame_metadata/Era.ts index 23db08a09..549bb8172 100644 --- a/frame_metadata/Era.ts +++ b/frame_metadata/Era.ts @@ -15,8 +15,7 @@ export function mortalEra(period: bigint, current: bigint): Era { } export const $era: $.Codec = $.createCodec({ - name: "era", - _metadata: null, + _metadata: $.metadata("$era"), _staticSize: 2, _encode(buffer, value) { if (value.type === "Immortal") { @@ -44,6 +43,9 @@ export const $era: $.Codec = $.createCodec({ } } }, + _assert() { + // TODO + }, }); function maxN(a: bigint, b: bigint) { diff --git a/frame_metadata/Extrinsic.ts b/frame_metadata/Extrinsic.ts index 87d423ae9..0b789a1b0 100644 --- a/frame_metadata/Extrinsic.ts +++ b/frame_metadata/Extrinsic.ts @@ -65,8 +65,7 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec { const totalSize = 1 + $address._staticSize + $sig._staticSize + toSignSize; const $baseExtrinsic: $.Codec = $.createCodec({ - name: "", - _metadata: null, + _metadata: [], _staticSize: totalSize, _encode(buffer, extrinsic) { const firstByte = (+!!extrinsic.signature << 7) | extrinsic.protocolVersion; @@ -164,11 +163,13 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec { 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), ); diff --git a/frame_metadata/Key.ts b/frame_metadata/Key.ts index 4f6828873..28a296465 100644 --- a/frame_metadata/Key.ts +++ b/frame_metadata/Key.ts @@ -18,10 +18,10 @@ export function $storageKey(props: StorageKeyProps): $.Codec { 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 = []; @@ -40,8 +40,7 @@ export function $storageKey(props: StorageKeyProps): $.Codec { {} as Record>, ); 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); @@ -54,5 +53,8 @@ export function $storageKey(props: StorageKeyProps): $.Codec { buffer.index += 32; return $keys[Object.values($keys).length - 1]!._decode(buffer); }, + _assert() { + // TODO #362 + }, }); } diff --git a/frame_metadata/Metadata.ts b/frame_metadata/Metadata.ts index 677ed5ff4..da02e300a 100644 --- a/frame_metadata/Metadata.ts +++ b/frame_metadata/Metadata.ts @@ -138,8 +138,8 @@ export interface Metadata { extrinsic: ExtrinsicDef; } export const $metadata: $.Codec = $.object( - ["magicNumber", $.constantPattern(magicNumber, $.u32)], - ["version", $.constantPattern(14, $.u8)], + ["magicNumber", $.constant(magicNumber, $.u32)], + ["version", $.constant<14>(14, $.u8)], ["tys", $tys], ["pallets", $.array($pallet)], ["extrinsic", $extrinsicDef], diff --git a/frame_metadata/scale_info.ts b/frame_metadata/scale_info.ts index fac31adca..630115b5b 100644 --- a/frame_metadata/scale_info.ts +++ b/frame_metadata/scale_info.ts @@ -6,8 +6,7 @@ export class TyDecodeCtx { const $compactU32 = $.compact($.u32); export const $tys: $.Codec = $.createCodec({ - name: "tys", - _metadata: null, + _metadata: $.metadata("$tys"), _staticSize: $compactU32._staticSize, _encode(buffer, value) { $.array($ty)._encode(buffer, value); @@ -21,11 +20,13 @@ export const $tys: $.Codec = $.createCodec({ } return tys; }, + _assert() { + // TODO #362 + }, }); export const $tyId: $.Codec = $.createCodec({ - name: "tyId", - _metadata: null, + _metadata: $.metadata("$tyId"), _staticSize: $compactU32._staticSize, _encode(buffer, value) { $.compact($.u32)._encode(buffer, value.id); @@ -35,6 +36,9 @@ export const $tyId: $.Codec = $.createCodec({ const id = $compactU32._decode(buffer); return ctx.tys?.[id] ?? { id } as any; }, + _assert() { + // TODO #362 + }, }); export interface Field { diff --git a/hashers/mod.ts b/hashers/mod.ts index dcb5e895c..d97748b13 100644 --- a/hashers/mod.ts +++ b/hashers/mod.ts @@ -27,10 +27,9 @@ export abstract class Hasher { function $hash(hasher: Hasher, $inner: $.Codec): $.Codec { 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) @@ -45,11 +44,14 @@ function $hash(hasher: Hasher, $inner: $.Codec): $.Codec { 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); + }, }); }