From 784ca48eac6b44d4407c8469e6b058a6d9a8069e Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Fri, 23 Apr 2021 17:10:07 +0800 Subject: [PATCH 1/3] feat(ScaleSet): add SscaleSet --- assembly/ScaleSet.ts | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 assembly/ScaleSet.ts diff --git a/assembly/ScaleSet.ts b/assembly/ScaleSet.ts new file mode 100644 index 0000000..8116c93 --- /dev/null +++ b/assembly/ScaleSet.ts @@ -0,0 +1,101 @@ +import { CompactInt } from "./Int/CompactInt"; +import { BytesReader } from "./BytesReader"; +import { Codec } from "./interfaces/Codec"; +import { UnwrappableCodec } from "./interfaces/UnwrappableCodec"; + +/** + * @description SCALE Codec support for native Map type + */ +export class ScaleSet + extends Set + implements UnwrappableCodec> { + /** + * @description return underlying native type + */ + @inline + unwrap(): Set { + // TODO: remove unwrap? + return this; + } + + /** + * The number of bytes this Map has + */ + @inline + encodedLength(): i32 { + return this.toU8a().length; + } + /** + * Convert ScaleMap to u8[] + * Length is encoded first, followed by all key and value encodings concatenated + */ + toU8a(): u8[] { + const values: T[] = this.values(); + let len: CompactInt = new CompactInt(values.length); + const result: Array> = [len.toU8a()]; + for (let i = 0; i < values.length; i++) { + result.push(values[i].toU8a()); + } + return result.flat(); + } + + /** + * @description Non-static constructor + * @param bytes + * @param index + */ + populateFromBytes(bytes: u8[], index: i32 = 0): i32 { + const bytesReader = new BytesReader(bytes, index); + const lenComp = bytesReader.readInto(); + for (let i: i32 = 0; i < lenComp.unwrap(); i++) { + const value = bytesReader.readInto(); + this.add(value); + } + + return bytesReader.currentIndex(); + } + /** + * @description Overloaded == operator + * @param a instance of ExtrinsicData + * @param b Instance of ExtrinsicData + */ + @operator("==") + eq(other: ScaleSet): bool { + const aLen = this.size; + const bLen = other.size; + const aValues = this.values(); + const bValues = other.values(); + + if (aLen != bLen) { + return false; + } + for (let i = 0; i < aValues.length; i++) { + if (aValues[i] != bValues[i]) { + return false; + } + } + return true; + } + + /** + * @description Overloaded != operator + * @param a instance of ExtrinsicData + * @param b Instance of ExtrinsicData + */ + @operator("!=") + notEq(other: ScaleSet): bool { + return !this.eq(other); + } + + static fromU8a(input: u8[], index: i32 = 0): ScaleSet { + const set = new ScaleSet(); + const bytesReader = new BytesReader(input, index); + const len = bytesReader.readInto().unwrap(); + + for (let i: i32 = 0; i < len; i++) { + const val = bytesReader.readInto(); + set.add(val); + } + return set; + } +} From cea81c50ea2d14fce4759af4925fbecfac06187a Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Fri, 23 Apr 2021 17:17:21 +0800 Subject: [PATCH 2/3] docs(ScaleSet): fix --- assembly/ScaleSet.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/assembly/ScaleSet.ts b/assembly/ScaleSet.ts index 8116c93..89c7788 100644 --- a/assembly/ScaleSet.ts +++ b/assembly/ScaleSet.ts @@ -4,7 +4,7 @@ import { Codec } from "./interfaces/Codec"; import { UnwrappableCodec } from "./interfaces/UnwrappableCodec"; /** - * @description SCALE Codec support for native Map type + * @description SCALE Codec support for native Set type */ export class ScaleSet extends Set @@ -19,14 +19,14 @@ export class ScaleSet } /** - * The number of bytes this Map has + * The number of bytes this Set has */ @inline encodedLength(): i32 { return this.toU8a().length; } /** - * Convert ScaleMap to u8[] + * Convert it to u8[] * Length is encoded first, followed by all key and value encodings concatenated */ toU8a(): u8[] { @@ -54,11 +54,7 @@ export class ScaleSet return bytesReader.currentIndex(); } - /** - * @description Overloaded == operator - * @param a instance of ExtrinsicData - * @param b Instance of ExtrinsicData - */ + @operator("==") eq(other: ScaleSet): bool { const aLen = this.size; @@ -77,11 +73,6 @@ export class ScaleSet return true; } - /** - * @description Overloaded != operator - * @param a instance of ExtrinsicData - * @param b Instance of ExtrinsicData - */ @operator("!=") notEq(other: ScaleSet): bool { return !this.eq(other); From a0e2b9053b13cd4009baaadbab7134910f37f86e Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Sat, 8 May 2021 14:48:05 +0800 Subject: [PATCH 3/3] fix(ScaleSet): use `===` for `eq` --- assembly/ScaleSet.ts | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/assembly/ScaleSet.ts b/assembly/ScaleSet.ts index 89c7788..672af2c 100644 --- a/assembly/ScaleSet.ts +++ b/assembly/ScaleSet.ts @@ -56,26 +56,13 @@ export class ScaleSet } @operator("==") - eq(other: ScaleSet): bool { - const aLen = this.size; - const bLen = other.size; - const aValues = this.values(); - const bValues = other.values(); - - if (aLen != bLen) { - return false; - } - for (let i = 0; i < aValues.length; i++) { - if (aValues[i] != bValues[i]) { - return false; - } - } - return true; + eq(other: this): bool { + return this === other; } @operator("!=") - notEq(other: ScaleSet): bool { - return !this.eq(other); + notEq(other: this): bool { + return !this.eq(this); } static fromU8a(input: u8[], index: i32 = 0): ScaleSet {