Align Data.Raw decoding with Substrate (32-bytes) (#3573)

* Align Data.Raw decoding with Substrate (32-bytes)

* CHANGELOG

* Encoding test updates

* provider.disconnect

* Adjust provider tests

* Unique connection URLs

* Dangling tests

* detectOpenHandles

* Additional

* Remove rx test (catered for by Promise wrap)

* Additional cleanups to tests
This commit is contained in:
Jaco
2021-05-27 10:18:56 +02:00
committed by GitHub
parent 7497ac723f
commit 2d02f61fa7
34 changed files with 1173 additions and 1209 deletions
+16
View File
@@ -24,4 +24,20 @@ describe('Data', (): void => {
new Data(registry, { Raw: '0x0102030405060708' }).toHex()
).toEqual('0x090102030405060708');
});
it('throws on a very long string (only 32 bytes)', (): void => {
expect(
() => new Data(registry, { Raw: 'this is some very long string that is added here that will fail' })
).toThrow('Data.Raw values are limited to a maximum length of 32 bytes');
});
it('encodes a large value correctly (max 32)', (): void => {
expect(
// without the assert (and above test), add extra bytes to the end to check the encoding
new Data(registry, { Raw: '0x0102030405060708091011121314151617181920212223242526272829303132' }).toHex()
).toEqual(
// first byte is 32 + 1 === 33 === 0x21
'0x210102030405060708091011121314151617181920212223242526272829303132'
);
});
});
+7 -3
View File
@@ -4,7 +4,7 @@
import type { H256 } from '../interfaces/runtime';
import type { Registry } from '../types';
import { isString, isU8a, u8aToU8a } from '@polkadot/util';
import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { Enum } from '../codec/Enum';
import { Bytes } from './Bytes';
@@ -57,6 +57,8 @@ export class Data extends Enum {
Keccak256: 'H256', // 4
ShaThree256: 'H256' // 5
}, ...decodeData(registry, value));
assert(!this.isRaw || this.asRaw.length <= 32, 'Data.Raw values are limited to a maximum length of 32 bytes');
}
get asBlakeTwo256 (): H256 {
@@ -119,10 +121,12 @@ export class Data extends Enum {
} else if (this.index === 1) {
// don't add the length, just the data
const data = this.value.toU8a(true);
const length = Math.min(data.length, 32);
const length = data.length >= 32
? 32
: data.length;
const u8a = new Uint8Array(length + 1);
u8a.set([data.length + 1], 0);
u8a.set([length + 1], 0);
u8a.set(data.subarray(0, length), 1);
return u8a;