Add Identity hasher (#2044)
* Add Identity hasher * Expand new V11 storage types * Cleanups, use actual enum keys * Sort augmented output (fy-by enhancement) * Map get * DEFAULT_FN
This commit is contained in:
+153
-153
@@ -27,16 +27,6 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
**/
|
||||
expectedBlockTime: AugmentedConst<Moment>;
|
||||
};
|
||||
timestamp: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The minimum period between blocks. Beware that this is different to the *expected* period
|
||||
* that the block production apparatus provides. Your chosen consensus system will generally
|
||||
* work with this to determine a sensible block time. e.g. For Aura, it will be double this
|
||||
* period on default settings.
|
||||
**/
|
||||
minimumPeriod: AugmentedConst<Moment>;
|
||||
};
|
||||
balances: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
@@ -44,8 +34,70 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
**/
|
||||
existentialDeposit: AugmentedConst<Balance>;
|
||||
};
|
||||
transactionPayment: {
|
||||
contracts: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The maximum amount of gas that could be expended per block. A reasonable
|
||||
* default value is 10_000_000.
|
||||
**/
|
||||
blockGasLimit: AugmentedConst<Gas>;
|
||||
/**
|
||||
* The base fee charged for calling into a contract. A reasonable default
|
||||
* value is 135.
|
||||
**/
|
||||
callBaseFee: AugmentedConst<Gas>;
|
||||
/**
|
||||
* The fee required to instantiate a contract instance. A reasonable default value
|
||||
* is 21.
|
||||
**/
|
||||
contractFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The base fee charged for instantiating a contract. A reasonable default value
|
||||
* is 175.
|
||||
**/
|
||||
instantiateBaseFee: AugmentedConst<Gas>;
|
||||
/**
|
||||
* The maximum nesting level of a call/instantiate stack. A reasonable default
|
||||
* value is 100.
|
||||
**/
|
||||
maxDepth: AugmentedConst<u32>;
|
||||
/**
|
||||
* The maximum size of a storage value in bytes. A reasonable default is 16 KiB.
|
||||
**/
|
||||
maxValueSize: AugmentedConst<u32>;
|
||||
/**
|
||||
* Price of a byte of storage per one block interval. Should be greater than 0.
|
||||
**/
|
||||
rentByteFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The amount of funds a contract should deposit in order to offset
|
||||
* the cost of one byte.
|
||||
* Let's suppose the deposit is 1,000 BU (balance units)/byte and the rent is 1 BU/byte/day,
|
||||
* then a contract with 1,000,000 BU that uses 1,000 bytes of storage would pay no rent.
|
||||
* But if the balance reduced to 500,000 BU and the storage stayed the same at 1,000,
|
||||
* then it would pay 500 BU/day.
|
||||
**/
|
||||
rentDepositOffset: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* Number of block delay an extrinsic claim surcharge has.
|
||||
* When claim surcharge is called by an extrinsic the rent is checked
|
||||
* for current_block - delay
|
||||
**/
|
||||
signedClaimHandicap: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* Size of a contract at the time of instantiation. This is a simple way to ensure that
|
||||
* empty contracts eventually gets deleted.
|
||||
**/
|
||||
storageSizeOffset: AugmentedConst<u32>;
|
||||
/**
|
||||
* Reward that is received by the party whose touch has led
|
||||
* to removal of a contract.
|
||||
**/
|
||||
surchargeReward: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The minimum amount required to generate a tombstone.
|
||||
**/
|
||||
tombstoneDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The fee to be paid for making a transaction; the base.
|
||||
**/
|
||||
@@ -55,27 +107,16 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
**/
|
||||
transactionByteFee: AugmentedConst<BalanceOf>;
|
||||
};
|
||||
staking: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Number of sessions per era.
|
||||
**/
|
||||
sessionsPerEra: AugmentedConst<SessionIndex>;
|
||||
/**
|
||||
* Number of eras that staked funds must remain bonded for.
|
||||
**/
|
||||
bondingDuration: AugmentedConst<EraIndex>;
|
||||
};
|
||||
session: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Used as first key for `NextKeys` and `KeyOwner` to put all the data into the same branch
|
||||
* of the trie.
|
||||
**/
|
||||
dedupKeyPrefix: AugmentedConst<Bytes>;
|
||||
};
|
||||
democracy: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Period in blocks where an external proposal may not be re-submitted after being vetoed.
|
||||
**/
|
||||
cooloffPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* Minimum voting period allowed for an emergency referendum.
|
||||
**/
|
||||
emergencyVotingPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The minimum period of locking and the period between a proposal being approved and enacted.
|
||||
* It should generally be a little more than the unstake period to ensure that
|
||||
@@ -87,48 +128,113 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
* How often (in blocks) new public referenda are launched.
|
||||
**/
|
||||
launchPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* How often (in blocks) to check for new votes.
|
||||
**/
|
||||
votingPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The minimum amount to be used as a deposit for a public referendum proposal.
|
||||
**/
|
||||
minimumDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* Minimum voting period allowed for an emergency referendum.
|
||||
**/
|
||||
emergencyVotingPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* Period in blocks where an external proposal may not be re-submitted after being vetoed.
|
||||
**/
|
||||
cooloffPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The amount of balance that must be deposited per byte of preimage stored.
|
||||
**/
|
||||
preimageByteDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* How often (in blocks) to check for new votes.
|
||||
**/
|
||||
votingPeriod: AugmentedConst<BlockNumber>;
|
||||
};
|
||||
elections: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
candidacyBond: AugmentedConst<BalanceOf>;
|
||||
votingBond: AugmentedConst<BalanceOf>;
|
||||
desiredMembers: AugmentedConst<u32>;
|
||||
desiredRunnersUp: AugmentedConst<u32>;
|
||||
termDuration: AugmentedConst<BlockNumber>;
|
||||
votingBond: AugmentedConst<BalanceOf>;
|
||||
};
|
||||
finalityTracker: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The number of recent samples to keep from this chain. Default is 101.
|
||||
**/
|
||||
windowSize: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The delay after which point things become suspicious. Default is 1000.
|
||||
**/
|
||||
reportLatency: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The number of recent samples to keep from this chain. Default is 101.
|
||||
**/
|
||||
windowSize: AugmentedConst<BlockNumber>;
|
||||
};
|
||||
session: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Used as first key for `NextKeys` and `KeyOwner` to put all the data into the same branch
|
||||
* of the trie.
|
||||
**/
|
||||
dedupKeyPrefix: AugmentedConst<Bytes>;
|
||||
};
|
||||
society: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The minimum amount of a deposit required for a bid to be made.
|
||||
**/
|
||||
candidateDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The number of blocks between membership challenges.
|
||||
**/
|
||||
challengePeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The number of times a member may vote the wrong way (or not at all, when they are a skeptic)
|
||||
* before they become suspended.
|
||||
**/
|
||||
maxStrikes: AugmentedConst<u32>;
|
||||
/**
|
||||
* The amount of incentive paid within each period. Doesn't include VoterTip.
|
||||
**/
|
||||
periodSpend: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The number of blocks between candidate/membership rotation periods.
|
||||
**/
|
||||
rotationPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The amount of the unpaid reward that gets deducted in the case that either a skeptic
|
||||
* doesn't vote or someone votes in the wrong way.
|
||||
**/
|
||||
wrongSideDeduction: AugmentedConst<BalanceOf>;
|
||||
};
|
||||
staking: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Number of eras that staked funds must remain bonded for.
|
||||
**/
|
||||
bondingDuration: AugmentedConst<EraIndex>;
|
||||
/**
|
||||
* Number of sessions per era.
|
||||
**/
|
||||
sessionsPerEra: AugmentedConst<SessionIndex>;
|
||||
};
|
||||
timestamp: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The minimum period between blocks. Beware that this is different to the *expected* period
|
||||
* that the block production apparatus provides. Your chosen consensus system will generally
|
||||
* work with this to determine a sensible block time. e.g. For Aura, it will be double this
|
||||
* period on default settings.
|
||||
**/
|
||||
minimumPeriod: AugmentedConst<Moment>;
|
||||
};
|
||||
transactionPayment: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The fee to be paid for making a transaction; the base.
|
||||
**/
|
||||
transactionBaseFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The fee to be paid for making a transaction; the per-byte portion.
|
||||
**/
|
||||
transactionByteFee: AugmentedConst<BalanceOf>;
|
||||
};
|
||||
treasury: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Percentage of spare funds (if any) that are burnt per spend period.
|
||||
**/
|
||||
burn: AugmentedConst<Permill>;
|
||||
/**
|
||||
* Fraction of a proposal's value that should be bonded in order to place the proposal.
|
||||
* An accepted proposal gets these back. A rejected proposal does not.
|
||||
@@ -142,10 +248,6 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
* Period between successive spends.
|
||||
**/
|
||||
spendPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* Percentage of spare funds (if any) that are burnt per spend period.
|
||||
**/
|
||||
burn: AugmentedConst<Permill>;
|
||||
/**
|
||||
* The period for which a tip remains open after is has achieved threshold tippers.
|
||||
**/
|
||||
@@ -163,108 +265,6 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
|
||||
**/
|
||||
tipReportDepositPerByte: AugmentedConst<BalanceOf>;
|
||||
};
|
||||
contracts: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* Number of block delay an extrinsic claim surcharge has.
|
||||
* When claim surcharge is called by an extrinsic the rent is checked
|
||||
* for current_block - delay
|
||||
**/
|
||||
signedClaimHandicap: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The minimum amount required to generate a tombstone.
|
||||
**/
|
||||
tombstoneDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* Size of a contract at the time of instantiation. This is a simple way to ensure that
|
||||
* empty contracts eventually gets deleted.
|
||||
**/
|
||||
storageSizeOffset: AugmentedConst<u32>;
|
||||
/**
|
||||
* Price of a byte of storage per one block interval. Should be greater than 0.
|
||||
**/
|
||||
rentByteFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The amount of funds a contract should deposit in order to offset
|
||||
* the cost of one byte.
|
||||
* Let's suppose the deposit is 1,000 BU (balance units)/byte and the rent is 1 BU/byte/day,
|
||||
* then a contract with 1,000,000 BU that uses 1,000 bytes of storage would pay no rent.
|
||||
* But if the balance reduced to 500,000 BU and the storage stayed the same at 1,000,
|
||||
* then it would pay 500 BU/day.
|
||||
**/
|
||||
rentDepositOffset: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* Reward that is received by the party whose touch has led
|
||||
* to removal of a contract.
|
||||
**/
|
||||
surchargeReward: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The fee to be paid for making a transaction; the base.
|
||||
**/
|
||||
transactionBaseFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The fee to be paid for making a transaction; the per-byte portion.
|
||||
**/
|
||||
transactionByteFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The fee required to instantiate a contract instance. A reasonable default value
|
||||
* is 21.
|
||||
**/
|
||||
contractFee: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The base fee charged for calling into a contract. A reasonable default
|
||||
* value is 135.
|
||||
**/
|
||||
callBaseFee: AugmentedConst<Gas>;
|
||||
/**
|
||||
* The base fee charged for instantiating a contract. A reasonable default value
|
||||
* is 175.
|
||||
**/
|
||||
instantiateBaseFee: AugmentedConst<Gas>;
|
||||
/**
|
||||
* The maximum nesting level of a call/instantiate stack. A reasonable default
|
||||
* value is 100.
|
||||
**/
|
||||
maxDepth: AugmentedConst<u32>;
|
||||
/**
|
||||
* The maximum size of a storage value in bytes. A reasonable default is 16 KiB.
|
||||
**/
|
||||
maxValueSize: AugmentedConst<u32>;
|
||||
/**
|
||||
* The maximum amount of gas that could be expended per block. A reasonable
|
||||
* default value is 10_000_000.
|
||||
**/
|
||||
blockGasLimit: AugmentedConst<Gas>;
|
||||
};
|
||||
society: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
* The minimum amount of a deposit required for a bid to be made.
|
||||
**/
|
||||
candidateDeposit: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The amount of the unpaid reward that gets deducted in the case that either a skeptic
|
||||
* doesn't vote or someone votes in the wrong way.
|
||||
**/
|
||||
wrongSideDeduction: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The number of times a member may vote the wrong way (or not at all, when they are a skeptic)
|
||||
* before they become suspended.
|
||||
**/
|
||||
maxStrikes: AugmentedConst<u32>;
|
||||
/**
|
||||
* The amount of incentive paid within each period. Doesn't include VoterTip.
|
||||
**/
|
||||
periodSpend: AugmentedConst<BalanceOf>;
|
||||
/**
|
||||
* The number of blocks between candidate/membership rotation periods.
|
||||
**/
|
||||
rotationPeriod: AugmentedConst<BlockNumber>;
|
||||
/**
|
||||
* The number of blocks between membership challenges.
|
||||
**/
|
||||
challengePeriod: AugmentedConst<BlockNumber>;
|
||||
};
|
||||
vesting: {
|
||||
[index: string]: AugmentedConst<object & Codec>;
|
||||
/**
|
||||
|
||||
+592
-592
File diff suppressed because it is too large
Load Diff
+1413
-1413
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import metadataDefs from '@polkadot/types/interfaces/metadata/definitions';
|
||||
import { StorageHasher } from '@polkadot/types/interfaces';
|
||||
import { u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
import { blake2AsU8a, xxhashAsU8a } from '@polkadot/util-crypto';
|
||||
@@ -10,24 +11,26 @@ export type HasherInput = string | Buffer | Uint8Array;
|
||||
|
||||
export type HasherFunction = (data: HasherInput) => Uint8Array;
|
||||
|
||||
type HasherCheck = 'isBlake2128' | 'isBlake2128Concat' | 'isBlake2256' | 'isTwox128' | 'isTwox256' | 'isTwox64Concat';
|
||||
const DEFAULT_FN = (data: HasherInput): Uint8Array => xxhashAsU8a(data, 128);
|
||||
|
||||
const DEFAULT = (data: HasherInput): Uint8Array => xxhashAsU8a(data, 128);
|
||||
|
||||
const map: Record<HasherCheck, HasherFunction> = {
|
||||
isBlake2128: (data: HasherInput): Uint8Array => blake2AsU8a(data, 128),
|
||||
isBlake2128Concat: (data: HasherInput): Uint8Array => u8aConcat(blake2AsU8a(data, 128), u8aToU8a(data)),
|
||||
isBlake2256: (data: HasherInput): Uint8Array => blake2AsU8a(data, 256),
|
||||
isTwox128: (data: HasherInput): Uint8Array => xxhashAsU8a(data, 128),
|
||||
isTwox256: (data: HasherInput): Uint8Array => xxhashAsU8a(data, 256),
|
||||
isTwox64Concat: (data: HasherInput): Uint8Array => u8aConcat(xxhashAsU8a(data, 64), u8aToU8a(data))
|
||||
const HASHERS: Record<keyof typeof metadataDefs.types.StorageHasherV11._enum, HasherFunction> = {
|
||||
Blake2_128: (data: HasherInput): Uint8Array => // eslint-disable-line @typescript-eslint/camelcase
|
||||
blake2AsU8a(data, 128),
|
||||
Blake2_128Concat: (data: HasherInput): Uint8Array => // eslint-disable-line @typescript-eslint/camelcase
|
||||
u8aConcat(blake2AsU8a(data, 128), u8aToU8a(data)),
|
||||
Blake2_256: (data: HasherInput): Uint8Array => // eslint-disable-line @typescript-eslint/camelcase
|
||||
blake2AsU8a(data, 256),
|
||||
Identity: (data: HasherInput): Uint8Array =>
|
||||
u8aToU8a(data),
|
||||
Twox128: (data: HasherInput): Uint8Array =>
|
||||
xxhashAsU8a(data, 128),
|
||||
Twox256: (data: HasherInput): Uint8Array =>
|
||||
xxhashAsU8a(data, 256),
|
||||
Twox64Concat: (data: HasherInput): Uint8Array =>
|
||||
u8aConcat(xxhashAsU8a(data, 64), u8aToU8a(data))
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
export default function getHasher (hasher?: StorageHasher): HasherFunction {
|
||||
const [, fn] = (hasher && Object.entries(map).find(([check]): boolean =>
|
||||
hasher[check as HasherCheck]
|
||||
)) || [undefined, DEFAULT];
|
||||
|
||||
return fn;
|
||||
return HASHERS[hasher?.type as 'Identity'] || DEFAULT_FN;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ function generateModule (allDefs: object, { constants, name }: ModuleMetadataLat
|
||||
|
||||
return [indent(4)(`${stringCamelCase(name.toString())}: {`)]
|
||||
.concat(isStrict ? '' : indent(6)('[index: string]: AugmentedConst<object & Codec>;'))
|
||||
.concat(constants.map(({ documentation, name, type }): string => {
|
||||
.concat(constants.sort((a, b) => a.name.localeCompare(b.name.toString())).map(({ documentation, name, type }): string => {
|
||||
setImports(allDefs, imports, [type.toString()]);
|
||||
|
||||
return createDocComments(6, documentation) +
|
||||
@@ -42,7 +42,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: Record<strin
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
const body = meta.asLatest.modules.sort((a, b) => a.name.localeCompare(b.name.toString())).reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, mod, imports, isStrict));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER('chain'), imports, [
|
||||
|
||||
@@ -69,7 +69,7 @@ function generateModule (allDefs: object, registry: Registry, { name, storage }:
|
||||
|
||||
return [indent(4)(`${stringLowerFirst(name.toString())}: {`)]
|
||||
.concat(isStrict ? '' : indent(6)('[index: string]: QueryableStorageEntry<ApiType>;'))
|
||||
.concat(storage.unwrap().items.map((storageEntry): string => {
|
||||
.concat(storage.unwrap().items.sort((a, b) => a.name.localeCompare(b.name.toString())).map((storageEntry): string => {
|
||||
const [args, returnType] = entrySignature(allDefs, registry, storageEntry, imports);
|
||||
let entryType = 'AugmentedQuery';
|
||||
|
||||
@@ -91,7 +91,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc: string[], mod): string[] => {
|
||||
const body = meta.asLatest.modules.sort((a, b) => a.name.localeCompare(b.name.toString())).reduce((acc: string[], mod): string[] => {
|
||||
return acc.concat(generateModule(allDefs, registry, mod, imports, isStrict));
|
||||
}, []);
|
||||
const header = createImportCode(HEADER('chain'), imports, [
|
||||
|
||||
@@ -38,7 +38,7 @@ function generateModule (registry: Registry, allDefs: object, { calls, name }: M
|
||||
|
||||
return [indent(4)(`${stringCamelCase(name.toString())}: {`)]
|
||||
.concat(isStrict ? '' : indent(6)('[index: string]: SubmittableExtrinsicFunction<ApiType>;'))
|
||||
.concat(allCalls.map(({ args, documentation, name }): string => {
|
||||
.concat(allCalls.sort((a, b) => a.name.localeCompare(b.name.toString())).map(({ args, documentation, name }): string => {
|
||||
const params = args
|
||||
.map(({ name, type }): [string, string, string] => {
|
||||
const typeStr = type.toString();
|
||||
@@ -66,7 +66,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
const body = meta.asLatest.modules.reduce((acc, mod): string[] => {
|
||||
const body = meta.asLatest.modules.sort((a, b) => a.name.localeCompare(b.name.toString())).reduce((acc, mod): string[] => {
|
||||
return acc.concat(generateModule(registry, allDefs, mod, imports, isStrict));
|
||||
}, [] as string[]);
|
||||
const header = createImportCode(HEADER('chain'), imports, [
|
||||
|
||||
@@ -431,7 +431,13 @@ export default {
|
||||
},
|
||||
|
||||
// v11
|
||||
DoubleMapTypeV11: 'DoubleMapTypeV10',
|
||||
DoubleMapTypeV11: {
|
||||
hasher: 'StorageHasherV11',
|
||||
key1: 'Type',
|
||||
key2: 'Type',
|
||||
value: 'Type',
|
||||
key2Hasher: 'StorageHasherV11'
|
||||
},
|
||||
ErrorMetadataV11: 'ErrorMetadataV10',
|
||||
EventMetadataV11: 'EventMetadataV10',
|
||||
ExtrinsicMetadataV11: {
|
||||
@@ -440,19 +446,57 @@ export default {
|
||||
},
|
||||
FunctionArgumentMetadataV11: 'FunctionArgumentMetadataV10',
|
||||
FunctionMetadataV11: 'FunctionMetadataV10',
|
||||
MapTypeV11: 'MapTypeV10',
|
||||
MapTypeV11: {
|
||||
hasher: 'StorageHasherV11',
|
||||
key: 'Type',
|
||||
value: 'Type',
|
||||
linked: 'bool'
|
||||
},
|
||||
MetadataV11: {
|
||||
modules: 'Vec<ModuleMetadataV11>',
|
||||
extrinsic: 'ExtrinsicMetadataV11'
|
||||
},
|
||||
ModuleConstantMetadataV11: 'ModuleConstantMetadataV10',
|
||||
ModuleMetadataV11: 'ModuleMetadataV10',
|
||||
ModuleMetadataV11: {
|
||||
name: 'Text',
|
||||
storage: 'Option<StorageMetadataV11>',
|
||||
calls: 'Option<Vec<FunctionMetadataV11>>',
|
||||
events: 'Option<Vec<EventMetadataV11>>',
|
||||
constants: 'Vec<ModuleConstantMetadataV11>',
|
||||
errors: 'Vec<ErrorMetadataV11>'
|
||||
},
|
||||
PlainTypeV11: 'Type',
|
||||
StorageEntryModifierV11: 'StorageEntryModifierV10',
|
||||
StorageEntryMetadataV11: 'StorageEntryMetadataV10',
|
||||
StorageEntryTypeV11: 'StorageEntryTypeV10',
|
||||
StorageMetadataV11: 'StorageMetadataV10',
|
||||
StorageHasherV11: 'StorageHasherV10',
|
||||
StorageEntryMetadataV11: {
|
||||
name: 'Text',
|
||||
modifier: 'StorageEntryModifierV11',
|
||||
type: 'StorageEntryTypeV11',
|
||||
fallback: 'Bytes',
|
||||
documentation: 'Vec<Text>'
|
||||
},
|
||||
StorageEntryTypeV11: {
|
||||
_enum: {
|
||||
Plain: 'PlainTypeV11',
|
||||
Map: 'MapTypeV11',
|
||||
DoubleMap: 'DoubleMapTypeV11'
|
||||
}
|
||||
},
|
||||
StorageMetadataV11: {
|
||||
prefix: 'Text',
|
||||
items: 'Vec<StorageEntryMetadataV11>'
|
||||
},
|
||||
StorageHasherV11: {
|
||||
_enum: {
|
||||
Blake2_128: null, // eslint-disable-line @typescript-eslint/camelcase
|
||||
Blake2_256: null, // eslint-disable-line @typescript-eslint/camelcase
|
||||
Blake2_128Concat: null, // eslint-disable-line @typescript-eslint/camelcase
|
||||
Twox128: null,
|
||||
Twox256: null,
|
||||
Twox64Concat: null,
|
||||
// new in v11
|
||||
Identity: null
|
||||
}
|
||||
},
|
||||
|
||||
// This always maps to the latest
|
||||
DoubleMapTypeLatest: 'DoubleMapTypeV11',
|
||||
|
||||
@@ -24,7 +24,13 @@ export interface DoubleMapTypeV10 extends Struct {
|
||||
}
|
||||
|
||||
/** @name DoubleMapTypeV11 */
|
||||
export interface DoubleMapTypeV11 extends DoubleMapTypeV10 {}
|
||||
export interface DoubleMapTypeV11 extends Struct {
|
||||
readonly hasher: StorageHasherV11;
|
||||
readonly key1: Type;
|
||||
readonly key2: Type;
|
||||
readonly value: Type;
|
||||
readonly key2Hasher: StorageHasherV11;
|
||||
}
|
||||
|
||||
/** @name DoubleMapTypeV3 */
|
||||
export interface DoubleMapTypeV3 extends Struct {
|
||||
@@ -239,7 +245,12 @@ export interface MapTypeV10 extends Struct {
|
||||
}
|
||||
|
||||
/** @name MapTypeV11 */
|
||||
export interface MapTypeV11 extends MapTypeV10 {}
|
||||
export interface MapTypeV11 extends Struct {
|
||||
readonly hasher: StorageHasherV11;
|
||||
readonly key: Type;
|
||||
readonly value: Type;
|
||||
readonly linked: bool;
|
||||
}
|
||||
|
||||
/** @name MapTypeV2 */
|
||||
export interface MapTypeV2 extends Struct {
|
||||
@@ -421,7 +432,14 @@ export interface ModuleMetadataV10 extends Struct {
|
||||
}
|
||||
|
||||
/** @name ModuleMetadataV11 */
|
||||
export interface ModuleMetadataV11 extends ModuleMetadataV10 {}
|
||||
export interface ModuleMetadataV11 extends Struct {
|
||||
readonly name: Text;
|
||||
readonly storage: Option<StorageMetadataV11>;
|
||||
readonly calls: Option<Vec<FunctionMetadataV11>>;
|
||||
readonly events: Option<Vec<EventMetadataV11>>;
|
||||
readonly constants: Vec<ModuleConstantMetadataV11>;
|
||||
readonly errors: Vec<ErrorMetadataV11>;
|
||||
}
|
||||
|
||||
/** @name ModuleMetadataV2 */
|
||||
export interface ModuleMetadataV2 extends Struct {
|
||||
@@ -572,7 +590,13 @@ export interface StorageEntryMetadataV10 extends Struct {
|
||||
}
|
||||
|
||||
/** @name StorageEntryMetadataV11 */
|
||||
export interface StorageEntryMetadataV11 extends StorageEntryMetadataV10 {}
|
||||
export interface StorageEntryMetadataV11 extends Struct {
|
||||
readonly name: Text;
|
||||
readonly modifier: StorageEntryModifierV11;
|
||||
readonly type: StorageEntryTypeV11;
|
||||
readonly fallback: Bytes;
|
||||
readonly documentation: Vec<Text>;
|
||||
}
|
||||
|
||||
/** @name StorageEntryMetadataV6 */
|
||||
export interface StorageEntryMetadataV6 extends StorageFunctionMetadataV5 {}
|
||||
@@ -621,7 +645,14 @@ export interface StorageEntryTypeV10 extends Enum {
|
||||
}
|
||||
|
||||
/** @name StorageEntryTypeV11 */
|
||||
export interface StorageEntryTypeV11 extends StorageEntryTypeV10 {}
|
||||
export interface StorageEntryTypeV11 extends Enum {
|
||||
readonly isPlain: boolean;
|
||||
readonly asPlain: PlainTypeV11;
|
||||
readonly isMap: boolean;
|
||||
readonly asMap: MapTypeV11;
|
||||
readonly isDoubleMap: boolean;
|
||||
readonly asDoubleMap: DoubleMapTypeV11;
|
||||
}
|
||||
|
||||
/** @name StorageEntryTypeV6 */
|
||||
export interface StorageEntryTypeV6 extends StorageFunctionTypeV5 {}
|
||||
@@ -768,7 +799,15 @@ export interface StorageHasherV10 extends Enum {
|
||||
}
|
||||
|
||||
/** @name StorageHasherV11 */
|
||||
export interface StorageHasherV11 extends StorageHasherV10 {}
|
||||
export interface StorageHasherV11 extends Enum {
|
||||
readonly isBlake2128: boolean;
|
||||
readonly isBlake2256: boolean;
|
||||
readonly isBlake2128Concat: boolean;
|
||||
readonly isTwox128: boolean;
|
||||
readonly isTwox256: boolean;
|
||||
readonly isTwox64Concat: boolean;
|
||||
readonly isIdentity: boolean;
|
||||
}
|
||||
|
||||
/** @name StorageHasherV4 */
|
||||
export interface StorageHasherV4 extends Enum {
|
||||
@@ -810,7 +849,10 @@ export interface StorageMetadataV10 extends Struct {
|
||||
}
|
||||
|
||||
/** @name StorageMetadataV11 */
|
||||
export interface StorageMetadataV11 extends StorageMetadataV10 {}
|
||||
export interface StorageMetadataV11 extends Struct {
|
||||
readonly prefix: Text;
|
||||
readonly items: Vec<StorageEntryMetadataV11>;
|
||||
}
|
||||
|
||||
/** @name StorageMetadataV7 */
|
||||
export interface StorageMetadataV7 extends Struct {
|
||||
|
||||
@@ -32,39 +32,45 @@ interface StorageKeyExtra {
|
||||
section: string;
|
||||
}
|
||||
|
||||
// order important, matching enum
|
||||
const HASHER_MAP: Record<keyof typeof metadataDefs.types.StorageHasherV10._enum, [number, boolean]> = {
|
||||
const HASHER_MAP: Record<keyof typeof metadataDefs.types.StorageHasherV11._enum, [number, boolean]> = {
|
||||
// opaque
|
||||
Blake2_128: [16, false], // eslint-disable-line @typescript-eslint/camelcase
|
||||
Blake2_256: [32, false], // eslint-disable-line @typescript-eslint/camelcase
|
||||
Blake2_128Concat: [16, true], // eslint-disable-line @typescript-eslint/camelcase
|
||||
Twox128: [16, false],
|
||||
Twox256: [32, false],
|
||||
Twox64Concat: [8, true]
|
||||
};
|
||||
const HASHER_OPTS = Object.values(HASHER_MAP);
|
||||
|
||||
function getStorageType (type: StorageEntryTypeLatest): string {
|
||||
// can decode
|
||||
Blake2_128Concat: [16, true], // eslint-disable-line @typescript-eslint/camelcase
|
||||
Twox64Concat: [8, true],
|
||||
Identity: [0, true]
|
||||
};
|
||||
|
||||
function getStorageType (type: StorageEntryTypeLatest, isOptionalLinked?: boolean): [boolean, string] {
|
||||
if (type.isPlain) {
|
||||
return type.asPlain.toString() as keyof InterfaceTypes;
|
||||
return [false, type.asPlain.toString()];
|
||||
} else if (type.isDoubleMap) {
|
||||
return type.asDoubleMap.value.toString() as keyof InterfaceTypes;
|
||||
return [false, type.asDoubleMap.value.toString()];
|
||||
}
|
||||
|
||||
const map = type.asMap;
|
||||
|
||||
if (map.linked.isTrue) {
|
||||
return `(${map.value.toString()}, Linkage<${map.key.toString()}>)` as keyof InterfaceTypes;
|
||||
const [pre, post] = isOptionalLinked
|
||||
? ['Option<', '>']
|
||||
: ['', ''];
|
||||
|
||||
return [true, `(${pre}${map.value.toString()}${post}, Linkage<${map.key.toString()}>)`];
|
||||
}
|
||||
|
||||
return map.value.toString();
|
||||
return [false, map.value.toString()];
|
||||
}
|
||||
|
||||
// we unwrap the type here, turning into an output usable for createType
|
||||
/** @internal */
|
||||
export function unwrapStorageType (type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes {
|
||||
const outputType = getStorageType(type);
|
||||
const [hasWrapper, outputType] = getStorageType(type, isOptional);
|
||||
|
||||
return isOptional
|
||||
return isOptional && !hasWrapper
|
||||
? `Option<${outputType}>` as keyof InterfaceTypes
|
||||
: outputType as keyof InterfaceTypes;
|
||||
}
|
||||
@@ -104,14 +110,13 @@ function decodeStorageKey (value?: AnyU8a | StorageKey | StorageEntry | [Storage
|
||||
|
||||
function decodeHashers (registry: Registry, value: Uint8Array, hashers: [StorageHasher, string][]): Codec[] {
|
||||
// the storage entry is xxhashAsU8a(prefix, 128) + xxhashAsU8a(method, 128), 256 bits total
|
||||
const encoded = value.subarray(32);
|
||||
let offset = 0;
|
||||
let offset = 32;
|
||||
|
||||
return hashers.reduce((result: Codec[], [hasher, type]): Codec[] => {
|
||||
const [hashLen, canDecode] = HASHER_OPTS[hasher.index];
|
||||
const [hashLen, canDecode] = HASHER_MAP[hasher.type as 'Identity'];
|
||||
const decoded = canDecode
|
||||
? registry.createType(type as 'Raw', encoded.subarray(offset + hashLen))
|
||||
: registry.createType('Raw', encoded.subarray(offset, offset + hashLen));
|
||||
? registry.createType(type as 'Raw', value.subarray(offset + hashLen))
|
||||
: registry.createType('Raw', value.subarray(offset, offset + hashLen));
|
||||
|
||||
offset += hashLen + (canDecode ? decoded.encodedLength : 0);
|
||||
result.push(decoded);
|
||||
|
||||
Reference in New Issue
Block a user