From cd9662ab4cb29315a2c8667a0a879eb3c34a36ab Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 21 Feb 2022 19:07:11 +0100 Subject: [PATCH] Ensure `BTreeSet` has the correct decoded length (#4588) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- CHANGELOG.md | 7 +++++++ .../types-codec/src/extended/BTreeSet.spec.ts | 17 +++++++++++++++++ packages/types-codec/src/extended/BTreeSet.ts | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdca14049f..2f1c59388b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Ensure `BTreeSet` has the correct decoded length + + ## 7.9.1 Feb 20, 2022 Contributed: diff --git a/packages/types-codec/src/extended/BTreeSet.spec.ts b/packages/types-codec/src/extended/BTreeSet.spec.ts index b1ab001466..39437e6fbf 100644 --- a/packages/types-codec/src/extended/BTreeSet.spec.ts +++ b/packages/types-codec/src/extended/BTreeSet.spec.ts @@ -133,6 +133,23 @@ describe('BTreeSet', (): void => { ).toEqual(17); }); + it('correctly encodes/decodes the length', (): void => { + const none = new (BTreeSet.with(U32))(registry, []).toHex(); + const some = new (BTreeSet.with(U32))(registry, [1, 2]).toHex(); + + // only the length byte + expect(none).toEqual('0x00'); + expect( + (new (BTreeSet.with(U32))(registry, none)).initialU8aLength + ).toEqual(1); + + // length byte + 2 values, 2 << 2 with u32 values + expect(some).toEqual('0x080100000002000000'); + expect( + (new (BTreeSet.with(U32))(registry, some)).initialU8aLength + ).toEqual(1 + (2 * 4)); + }); + it('correctly sorts numeric values', (): void => { expect( Array.from(new (BTreeSet.with(I32))(registry, mockI32SetObj)).map((k) => k.toNumber()) diff --git a/packages/types-codec/src/extended/BTreeSet.ts b/packages/types-codec/src/extended/BTreeSet.ts index 725ae83982..f3451ddc9c 100644 --- a/packages/types-codec/src/extended/BTreeSet.ts +++ b/packages/types-codec/src/extended/BTreeSet.ts @@ -20,7 +20,7 @@ function decodeSetFromU8a (registry: Registry, ValClass: CodecC output.add(values[i]); } - return [output, decodedLength + offset]; + return [output, decodedLength]; } /** @internal */