-
Notifications
You must be signed in to change notification settings - Fork 377
Extrinsic v2 #1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extrinsic v2 #1150
Changes from 7 commits
8736fc9
118402b
924cb1a
3422329
c3f6a60
8798656
7b8baae
e6ee306
c2cf69c
9b50084
8e95203
419663b
8e8c5f9
64193d0
ed8227a
7d8ad6d
73e0e40
236b7c7
680a38b
4fb312c
c4f0789
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,8 +22,9 @@ import { Storage } from '@polkadot/api-metadata/storage/types'; | |
| import storageFromMeta from '@polkadot/api-metadata/storage/fromMetadata'; | ||
| import RpcCore from '@polkadot/rpc-core'; | ||
| import { WsProvider } from '@polkadot/rpc-provider'; | ||
| import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, Null, U64 } from '@polkadot/types'; | ||
| import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, SignedBlock, Null, U64 } from '@polkadot/types'; | ||
| import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage'; | ||
| import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/primitive/Extrinsic/constants'; | ||
| import { MethodFunction, ModulesWithMethods } from '@polkadot/types/primitive/Method'; | ||
| import * as srmlTypes from '@polkadot/types/srml/definitions'; | ||
| import { StorageEntry } from '@polkadot/types/primitive/StorageKey'; | ||
|
|
@@ -72,6 +73,8 @@ export default abstract class ApiBase<ApiType> { | |
|
|
||
| private _extrinsics?: SubmittableExtrinsics<ApiType>; | ||
|
|
||
| private _extrinsicVersion: number = EXTRINSIC_DEFAULT_VERSION; | ||
|
|
||
| private _genesisHash?: Hash; | ||
|
|
||
| protected _isConnected: BehaviorSubject<boolean>; | ||
|
|
@@ -150,6 +153,13 @@ export default abstract class ApiBase<ApiType> { | |
| this.init(); | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns th version of extrinsics in-use on this chain | ||
| */ | ||
| public get extrinsicVersion (): number { | ||
| return this._extrinsicVersion; | ||
| } | ||
|
|
||
| /** | ||
| * @description Contains the genesis Hash of the attached chain. Apart from being useful to determine the actual chain, it can also be used to sign immortal transactions. | ||
| */ | ||
|
|
@@ -498,7 +508,9 @@ export default abstract class ApiBase<ApiType> { | |
| this._rpcCore.chain.getBlockHash(0).toPromise(), | ||
| this._rpcCore.chain.getRuntimeVersion().toPromise() | ||
| ]); | ||
|
|
||
| const metadataKey = `${this._genesisHash}-${(this._runtimeVersion as RuntimeVersion).specVersion}`; | ||
|
|
||
| if (metadataKey in metadata) { | ||
| this._runtimeMetadata = new Metadata(metadata[metadataKey]); | ||
| } else { | ||
|
|
@@ -508,6 +520,7 @@ export default abstract class ApiBase<ApiType> { | |
| // get unique types & validate | ||
| this.runtimeMetadata.getUniqTypes(false); | ||
| } else { | ||
| this._extrinsicVersion = this._options.source.extrinsicVersion; | ||
| this._runtimeMetadata = this._options.source.runtimeMetadata; | ||
| this._runtimeVersion = this._options.source.runtimeVersion; | ||
| this._genesisHash = this._options.source.genesisHash; | ||
|
|
@@ -517,22 +530,29 @@ export default abstract class ApiBase<ApiType> { | |
| const storage = storageFromMeta(this.runtimeMetadata); | ||
| const constants = constantsFromMeta(this.runtimeMetadata); | ||
|
|
||
| // only inject if we are not a clone (global init) | ||
| if (!this._options.source) { | ||
| Event.injectMetadata(this.runtimeMetadata); | ||
| Method.injectMethods(extrinsics); | ||
|
|
||
| // detect the extrinsic version in-use based on the last block | ||
| const lastBlock: SignedBlock = await this._rpcCore.chain.getBlock().toPromise(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now determine the extrinsic version from the chain - basically, based on the last block, we pull out the version from contained extrinsics and then use this in any future construction of this type. |
||
|
|
||
| this._extrinsicVersion = lastBlock.block.extrinsics[0].versionFormat; | ||
| } | ||
|
|
||
| this._extrinsics = this.decorateExtrinsics(extrinsics, this.decorateMethod); | ||
| this._query = this.decorateStorage(storage, this.decorateMethod); | ||
| this._consts = constants; | ||
|
|
||
| this._rx.extrinsicVersion = this._extrinsicVersion; | ||
| this._rx.genesisHash = this._genesisHash; | ||
| this._rx.runtimeVersion = this._runtimeVersion; | ||
| this._rx.tx = this.decorateExtrinsics(extrinsics, rxDecorateMethod); | ||
| this._rx.query = this.decorateStorage(storage, rxDecorateMethod); | ||
| this._rx.consts = constants; | ||
| this._derive = this.decorateDerive(this._rx as ApiInterfaceRx, this.decorateMethod); | ||
|
|
||
| // only inject if we are not a clone (global init) | ||
| if (!this._options.source) { | ||
| Event.injectMetadata(this.runtimeMetadata); | ||
| Method.injectMethods(extrinsics); | ||
| } | ||
| this._derive = this.decorateDerive(this._rx as ApiInterfaceRx, this.decorateMethod); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,9 @@ These primitive types are available: | |
| | [[Data]] | A raw data structure. It is an encoding of a U8a without any length encoding | | ||
| | [[Event]] | Wrapper for the actual data that forms part of an [[Event]] | | ||
| | [[EventRecord]] | A record for an [[Event]] (as specified by [[Metadata]]) with the specific [[Phase]] of application | | ||
| | [[Extrinsic]] | Representation of an Extrinsic in the system | | ||
| | [[ExtrinsicEra]] | The era for an extrinsic, indicating either a mortal or immortal extrinsic | | ||
| | [[ExtrinsicSignature]] | A container for the [[Signature]] associated with a specific [[Extrinsic]] | | ||
| | [[H160]] | Hash containing 160 bits (20 bytes), typically used in blocks, extrinsics and as a sane default | | ||
| | [[H256]] | Hash containing 256 bits (32 bytes), typically used in blocks, extrinsics and as a sane default | | ||
| | [[H512]] | Hash containing 512 bits (64 bytes), typically used for signatures | | ||
|
|
@@ -57,6 +60,8 @@ These primitive types are available: | |
| | [[Null]] | Implements a type that does not contain anything (apart from `null`) | | ||
| | [[Origin]] | Where Origin occurs, it should be ignored as an internal-only value | | ||
| | [[Signature]] | The default signature that is used accross the system | | ||
| | [[SignaturePayload]] | A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based on the contents included | | ||
| | [[SignaturePayloadRaw]] | A version of the [[SignaturePayload]] where it doesn't rely on [[Method]] with metadata, rather it treats the values as a raw byte stream | | ||
| | [[StorageData]] | Data retrieved via Storage queries and data for key-value pairs | | ||
| | [[StorageKey]] | A representation of a storage key (typically hashed) in the system | | ||
| | [[Text]] | This is a string wrapper, along with the length. | | ||
|
|
@@ -97,10 +102,7 @@ These custom types implement specific types that are found as part of the Substr | |
| | [[ContractStorageKey]] | A representation of a storage key for contracts | | ||
| | [[EraIndex]] | A representation for the era count | | ||
| | [[Exposure]] | A snapshot of the stake backing a single validator in the system | | ||
| | [[Extrinsic]] | Representation of an Extrinsic in the system | | ||
| | [[ExtrinsicEra]] | The era for an extrinsic, indicating either a mortal or immortal extrinsic | | ||
| | [[Extrinsics]] | A [[Vector]] of [[Extrinsic]] | | ||
| | [[ExtrinsicSignature]] | A container for the [[Signature]] associated with a specific [[Extrinsic]] | | ||
| | [[Gas]] | A gas number type for Substrate, extending [[U64]] | | ||
| | [[IndividualExposure]] | The Substrate IndividualExposure for staking | | ||
| | [[InherentOfflineReport]] | Describes the offline-reporting extrinsic | | ||
|
|
@@ -135,8 +137,6 @@ These custom types implement specific types that are found as part of the Substr | |
| | [[SessionKey]] | Wrapper for a SessionKey. Same as an normal [[AuthorityId]], i.e. a wrapper around publicKey | | ||
| | [[SessionKeys]] | Wrapper for the session and authority ids | | ||
| | [[SetIndex]] | Set index, implemented as a [[U32]] | | ||
| | [[SignaturePayload]] | A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based on the contents included | | ||
| | [[SignaturePayloadRaw]] | A version of the [[SignaturePayload]] where it doesn't rely on [[Method]] with metadata, rather it treats the values as a raw byte stream | | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The raw variant disappeared. The normal payload is now the same as the raw variant. We cannot continue to add special cases - this has a very limited impact, since it is just signers that actually use it. (extensions & internally to sumittable) |
||
| | [[StakingLedger]] | The ledger of a (bonded) stash | | ||
| | [[StoredPendingChange]] | Stored pending change for a Grandpa events | | ||
| | [[TreasuryProposal]] | A Proposal made for Treasury | | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,73 @@ | |
| // This software may be modified and distributed under the terms | ||
| // of the Apache-2.0 license. See the LICENSE file for details. | ||
|
|
||
| import { AnyJson, Codec } from '../types'; | ||
|
|
||
| /** | ||
| * @name Base | ||
| * @description A type extends the Base class, when it holds a value | ||
| */ | ||
| export default class Base<T> { | ||
| export default abstract class Base<T extends Codec> implements Codec { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make extension less noisy, the base defaults have been included into Base. Anything that extends it have been adjusted to remove duplication. (This happened since I really didn't want to add more boilerplate to the new classes, so rather cleaned up all) |
||
| protected raw: T; | ||
|
|
||
| public constructor (value?: any) { | ||
| this.raw = value; | ||
| } | ||
|
|
||
| /** | ||
| * @description The length of the value when encoded as a Uint8Array | ||
| */ | ||
| public get encodedLength (): number { | ||
| return this.toU8a().length; | ||
| } | ||
|
|
||
| /** | ||
| * @description Checks if the value is an empty value | ||
| */ | ||
| public get isEmpty (): boolean { | ||
| return this.raw.isEmpty; | ||
| } | ||
|
|
||
| /** | ||
| * @description Compares the value of the input to see if there is a match | ||
| */ | ||
| public eq (other?: any): boolean { | ||
| return this.raw.eq(other); | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation | ||
| */ | ||
| public toHex (isLe?: boolean): string { | ||
| return this.raw.toHex(isLe); | ||
| } | ||
|
|
||
| /** | ||
| * @description Converts the Object to JSON, typically used for RPC transfers | ||
| */ | ||
| public toJSON (): AnyJson { | ||
| return this.raw.toJSON(); | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns the string representation of the value | ||
| */ | ||
| public toString (): string { | ||
| return this.raw.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * @description Encodes the value as a Uint8Array as per the SCALE specifications | ||
| * @param isBare true when the value has none of the type-specific prefixes (internal) | ||
| */ | ||
| public toU8a (isBare?: boolean): Uint8Array { | ||
| return this.raw.toU8a(isBare); | ||
| } | ||
|
|
||
| /** | ||
| * @description Returns the base runtime type name for this instance | ||
| */ | ||
| public toRawType (): string { | ||
| return 'Base'; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I whacked this, technically a backwards incompatible change, however we have not had the
nobodyaccount in the system since poc-2 - so actually the test is bullsh*t. (inherents are now just unsigned, not with a nobody sig)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just remove it, then? Are we still supporting POC-2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which is why the test was removed. (It relied on an unsupported all-0 sig)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry, right, monday morning comment...