Some minor cleanups (#1966)

* Some minor cleanups

* Small additions

* Cleanup ExtrinsicEra decoding

* Minor adjustments

* Remove support for metadata v0 parsing

* Adjust refCountDelay

* refCountDelay comments

* Adjust Metadata tests

* Metadata default creation

* Funky lines

* Cleanup as AccountIdAndIndex

* Some derive returns

* Try and cleanup info extraction
This commit is contained in:
Jaco Greeff
2020-02-27 15:01:20 +01:00
committed by GitHub
parent efd6c5331d
commit 4e756704ea
28 changed files with 194 additions and 247 deletions
@@ -14,8 +14,10 @@ export default class MagicNumber extends U32 {
constructor (registry: Registry, value?: any) {
super(registry, value);
const magic = new U32(registry, MAGIC_NUMBER);
if (!this.isEmpty) {
const magic = new U32(registry, MAGIC_NUMBER);
assert(this.eq(magic), `MagicNumber mismatch: expected ${magic.toHex()}, found ${this.toHex()}`);
assert(this.eq(magic), `MagicNumber mismatch: expected ${magic.toHex()}, found ${this.toHex()}`);
}
}
}
+9 -21
View File
@@ -4,9 +4,8 @@
import { Registry } from '@polkadot/types/types';
import { bnToU8a, isHex, hexToU8a, u8aConcat } from '@polkadot/util';
import { isHex, hexToU8a } from '@polkadot/util';
import { MAGIC_NUMBER } from './MagicNumber';
import MetadataVersioned from './MetadataVersioned';
const VERSION_IDX = 4; // magic u32 preceding
@@ -23,28 +22,17 @@ function decodeMetadata (registry: Registry, _value: Uint8Array | string = new U
try {
return new MetadataVersioned(registry, value);
} catch (error) {
if (!error.message.includes('MagicNumber mismatch')) {
// This is an f-ing hack as a follow-up to another ugly hack
// https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2
// when we fail on V9, try to re-parse it as v10... yes... HACK
if (version === 9) {
value[VERSION_IDX] = 10;
// This is an f-ing hack as a follow-up to another ugly hack
// https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2
// when we fail on V9, try to re-parse it as v10... yes... HACK
if (version === 9) {
value[VERSION_IDX] = 10;
return decodeMetadata(registry, value);
}
throw error;
return decodeMetadata(registry, value);
}
}
return new MetadataVersioned(
registry,
u8aConcat(
bnToU8a(MAGIC_NUMBER), // manually add the magic number
Uint8Array.from([0]), // add the version for the original
value // the actual data as retrieved
)
);
throw error;
}
}
/**
+17 -21
View File
@@ -47,31 +47,27 @@ export function defaultValues (registry: Registry, rpcData: string, withThrow =
describe('storage with default values', (): void => {
const metadata = new Metadata(registry, rpcData);
metadata.asLatest.modules
.filter(({ storage }): boolean => storage.isSome)
.forEach((mod): void => {
mod.storage.unwrap().items.forEach(({ fallback, name, type }): void => {
const inner = unwrapStorageType(type);
const location = `${mod.name}.${name}: ${inner}`;
metadata.asLatest.modules.filter(({ storage }): boolean => storage.isSome).forEach((mod): void => {
mod.storage.unwrap().items.forEach(({ fallback, name, type }): void => {
const inner = unwrapStorageType(type);
const location = `${mod.name}.${name}: ${inner}`;
it(`creates default types for ${location}`, (): void => {
expect(
(): void => {
try {
createTypeUnsafe(registry, inner, [fallback]);
} catch (error) {
const message = `${location}:: ${error.message}`;
it(`creates default types for ${location}`, (): void => {
expect((): void => {
try {
createTypeUnsafe(registry, inner, [fallback]);
} catch (error) {
const message = `${location}:: ${error.message}`;
if (withThrow) {
throw new Error(message);
} else {
console.warn(message);
}
}
if (withThrow) {
throw new Error(message);
} else {
console.warn(message);
}
).not.toThrow();
});
}
}).not.toThrow();
});
});
});
});
}
@@ -1,33 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { TypeRegistry } from '@polkadot/types/create';
import Metadata from '../';
import latestSubstrateV0 from './static-substrate.json';
import rpcData from './static';
import { defaultValues, toLatest } from '../util/testUtil';
describe('Metadata', (): void => {
const registry = new TypeRegistry();
it('decodes into latest substrate', (): void => {
const metadata = new Metadata(registry, rpcData);
const json = metadata.asV0.toJSON();
try {
expect(metadata.version).toBe(0);
expect(metadata.asV0.modules.length).not.toBe(0);
expect(json).toEqual(latestSubstrateV0);
} catch (error) {
console.error(JSON.stringify(json));
throw error;
}
});
toLatest(registry, 0, rpcData, false);
defaultValues(registry, rpcData, false);
});