Cleanup encodedLength calculations (#3441)
* Cleanup encodedLength calculations * Remove array santize * Test adjustments
This commit is contained in:
@@ -30,9 +30,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.reduce((total, raw): number => {
|
||||
return total + raw.encodedLength;
|
||||
}, compactToU8a(this.length).length);
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, compactToU8a(this.length).length);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,11 +74,7 @@ export class Tuple extends AbstractArray<Codec> {
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.reduce((length: number, entry: Codec): number => {
|
||||
length += entry.encodedLength;
|
||||
|
||||
return length;
|
||||
}, 0);
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,7 +63,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, 0);
|
||||
}
|
||||
|
||||
public toU8a (): Uint8Array {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { alias, removeColons } from './sanitize';
|
||||
|
||||
describe('sanitize', (): void => {
|
||||
describe('alias', (): void => {
|
||||
const exec = alias(['String'], 'Text');
|
||||
const exec = alias('String', 'Text');
|
||||
|
||||
it('replaces all occurrences for types', (): void => {
|
||||
expect(exec('(String,Address,MasterString,String)')).toEqual(
|
||||
@@ -27,7 +27,7 @@ describe('sanitize', (): void => {
|
||||
});
|
||||
|
||||
it('handles emdedded Vec/Tuples', (): void => {
|
||||
const ann = alias(['Announcement'], 'ProxyAnnouncement');
|
||||
const ann = alias('Announcement', 'ProxyAnnouncement');
|
||||
|
||||
expect(ann('(Vec<Announcement>,BalanceOf)')).toEqual(
|
||||
'(Vec<ProxyAnnouncement>,BalanceOf)'
|
||||
|
||||
@@ -12,8 +12,8 @@ const BOX_PRECEDING = ['<', '(', '[', '"', ',', ' ']; // start of vec, tuple, fi
|
||||
|
||||
const mappings: Mapper[] = [
|
||||
// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
|
||||
alias(['<T::InherentOfflineReport as InherentOfflineReport>::Inherent'], 'InherentOfflineReport', false),
|
||||
alias(['VecDeque<'], 'Vec<', false),
|
||||
alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport', false),
|
||||
alias('VecDeque<', 'Vec<', false),
|
||||
// <T::Balance as HasCompact>
|
||||
cleanupCompact(),
|
||||
// Change BoundedVec<Type, Size> to Vec<Type>
|
||||
@@ -27,16 +27,17 @@ const mappings: Mapper[] = [
|
||||
// remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
|
||||
removeGenerics(),
|
||||
// alias String -> Text (compat with jsonrpc methods)
|
||||
alias(['String'], 'Text'),
|
||||
alias('String', 'Text'),
|
||||
// alias Vec<u8> -> Bytes
|
||||
alias(['Vec<u8>', '&\\[u8\\]'], 'Bytes'),
|
||||
alias('Vec<u8>', 'Bytes'),
|
||||
alias('&\\[u8\\]', 'Bytes'),
|
||||
// alias RawAddress -> Address
|
||||
alias(['RawAddress'], 'Address'),
|
||||
alias('RawAddress', 'Address'),
|
||||
// lookups, mapped to Address/AccountId as appropriate in runtime
|
||||
alias(['Lookup::Source'], 'LookupSource'),
|
||||
alias(['Lookup::Target'], 'LookupTarget'),
|
||||
alias('Lookup::Source', 'LookupSource'),
|
||||
alias('Lookup::Target', 'LookupTarget'),
|
||||
// HACK duplication between contracts & primitives, however contracts prefixed with exec
|
||||
alias(['exec::StorageKey'], 'ContractStorageKey'),
|
||||
alias('exec::StorageKey', 'ContractStorageKey'),
|
||||
// flattens tuples with one value, `(AccountId)` -> `AccountId`
|
||||
flattenSingleTuple(),
|
||||
// converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
|
||||
@@ -62,17 +63,14 @@ export function findClosing (value: string, start: number): number {
|
||||
throw new Error(`Unable to find closing matching <> on '${value}' (start ${start})`);
|
||||
}
|
||||
|
||||
export function alias (src: string[], dest: string, withChecks = true): Mapper {
|
||||
export function alias (src: string, dest: string, withChecks = true): Mapper {
|
||||
return (value: string): string =>
|
||||
src.reduce((value, src): string =>
|
||||
value
|
||||
.replace(
|
||||
new RegExp(`(^${src}|${BOX_PRECEDING.map((box) => `\\${box}${src}`).join('|')})`, 'g'),
|
||||
(src): string =>
|
||||
withChecks && BOX_PRECEDING.includes(src[0])
|
||||
? `${src[0]}${dest}`
|
||||
: dest
|
||||
), value
|
||||
value.replace(
|
||||
new RegExp(`(^${src}|${BOX_PRECEDING.map((box) => `\\${box}${src}`).join('|')})`, 'g'),
|
||||
(src): string =>
|
||||
withChecks && BOX_PRECEDING.includes(src[0])
|
||||
? `${src[0]}${dest}`
|
||||
: dest
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
const total = offset + length.toNumber();
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Text length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return u8aToString(value.subarray(offset, total));
|
||||
|
||||
Reference in New Issue
Block a user