Apply hashCache on Extrinsic only (no duplication) (#4176)

This commit is contained in:
Jaco
2021-11-06 10:01:05 +02:00
committed by GitHub
parent 936d14ffea
commit 0eb995c9a1
2 changed files with 28 additions and 44 deletions
+28 -27
View File
@@ -2,18 +2,17 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Compact } from '../codec/Compact';
import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics';
import type { FunctionMetadataLatest } from '../interfaces/metadata';
import type { Address, Balance, Call, Index } from '../interfaces/runtime';
import type { Address, Balance, Call, CodecHash, Index } from '../interfaces/runtime';
import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, CallBase, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, IMethod, Registry, SignatureOptions } from '../types';
import type { GenericExtrinsicEra } from './ExtrinsicEra';
import type { ExtrinsicValueV4 } from './v4/Extrinsic';
import { assert, compactAddLength, compactFromU8a, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, compactAddLength, compactFromU8a, isHex, isU8a, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { Base } from '../codec/Base';
import { Compact } from '../codec/Compact';
import { CodecHash } from '../interfaces/runtime';
import { BIT_SIGNED, BIT_UNSIGNED, DEFAULT_VERSION, UNMASK_VERSION } from './constants';
interface CreateOptions {
@@ -37,8 +36,6 @@ const VERSIONS = [
export { EXTRINSIC_VERSION as LATEST_EXTRINSIC_VERSION } from './v4/Extrinsic';
abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | ExtrinsicUnknown> {
protected hashCache?: CodecHash;
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Call]]
*/
@@ -81,17 +78,6 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
return this.toU8a().length;
}
/**
* @description returns a hash of the contents
*/
public override get hash (): CodecHash {
if (!this.hashCache) {
this.hashCache = super.hash;
}
return this.hashCache;
}
/**
* @description `true` id the extrinsic is signed
*/
@@ -183,6 +169,8 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
* - left as is, to create an inherent
*/
export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBase<A> implements IExtrinsic<A> {
#hashCache?: CodecHash;
constructor (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { version }: CreateOptions = {}) {
super(registry, GenericExtrinsic._decodeExtrinsic(registry, value, version));
}
@@ -228,12 +216,23 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
return GenericExtrinsic._newFromValue(registry, data.subarray(1), data[0]);
}
/**
* @description returns a hash of the contents
*/
public override get hash (): CodecHash {
if (!this.#hashCache) {
this.#hashCache = super.hash;
}
return this.#hashCache;
}
/**
* @description Injects an already-generated signature into the extrinsic
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsic<A> {
(this._raw as ExtrinsicVx).addSignature(signer, signature, payload);
this.hashCache = undefined;
this.#hashCache = undefined;
return this;
}
@@ -243,7 +242,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
*/
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsic<A> {
(this._raw as ExtrinsicVx).sign(account, options);
this.hashCache = undefined;
this.#hashCache = undefined;
return this;
}
@@ -253,7 +252,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
*/
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsic<A> {
(this._raw as ExtrinsicVx).signFake(signer, options);
this.hashCache = undefined;
this.#hashCache = undefined;
return this;
}
@@ -269,10 +268,13 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (isExpanded?: boolean): AnyJson {
return {
isSigned: this.isSigned,
method: this.method.toHuman(isExpanded),
...(this.isSigned
return objectSpread<Record<string, AnyJson>>(
{},
{
isSigned: this.isSigned,
method: this.method.toHuman(isExpanded)
},
this.isSigned
? {
era: this.era.toHuman(isExpanded),
nonce: this.nonce.toHuman(isExpanded),
@@ -280,9 +282,8 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
signer: this.signer.toHuman(isExpanded),
tip: this.tip.toHuman(isExpanded)
}
: {}
)
};
: null
);
}
/**
@@ -10,7 +10,6 @@ import type { ExtrinsicOptions } from '../types';
import { isU8a } from '@polkadot/util';
import { Struct } from '../../codec/Struct';
import { CodecHash } from '../../interfaces/runtime';
export const EXTRINSIC_VERSION = 4;
@@ -25,8 +24,6 @@ export interface ExtrinsicValueV4 {
* The third generation of compact extrinsics
*/
export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
#hashCache?: CodecHash;
constructor (registry: Registry, value?: Uint8Array | ExtrinsicValueV4 | Call, { isSigned }: Partial<ExtrinsicOptions> = {}) {
super(registry, {
signature: 'ExtrinsicSignatureV4',
@@ -62,17 +59,6 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
return this.toU8a().length;
}
/**
* @description returns a hash of the contents
*/
public override get hash (): CodecHash {
if (!this.#hashCache) {
this.#hashCache = super.hash;
}
return this.#hashCache;
}
/**
* @description The [[Call]] this extrinsic wraps
*/
@@ -99,7 +85,6 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV4 {
this.signature.addSignature(signer, signature, payload);
this.#hashCache = undefined;
return this;
}
@@ -109,7 +94,6 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
*/
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsicV4 {
this.signature.sign(this.method, account, options);
this.#hashCache = undefined;
return this;
}
@@ -119,7 +103,6 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
*/
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsicV4 {
this.signature.signFake(this.method, signer, options);
this.#hashCache = undefined;
return this;
}