From 77dc55af51b405fab839092efb8eac662e65cfdd Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 9 Mar 2021 15:29:35 -0800 Subject: [PATCH] introduce `common.copy()` for deep copy --- packages/client/lib/config.ts | 4 ++-- packages/common/src/index.ts | 7 +++++++ packages/tx/src/baseTransaction.ts | 5 +---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/client/lib/config.ts b/packages/client/lib/config.ts index 639b3595e3..01fd275692 100644 --- a/packages/client/lib/config.ts +++ b/packages/client/lib/config.ts @@ -194,8 +194,8 @@ export class Config { // TODO: map chainParams (and lib/util.parseParams) to new Common format const common = options.common ?? new Common({ chain: Config.CHAIN_DEFAULT, hardfork: 'chainstart' }) - this.chainCommon = Object.assign(Object.create(Object.getPrototypeOf(common)), common) - this.execCommon = Object.assign(Object.create(Object.getPrototypeOf(common)), common) + this.chainCommon = common.copy() + this.execCommon = common.copy() this.discDns = this.getDnsDiscovery(options.discDns) this.discV4 = this.getV4Discovery(options.discV4) diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index e131b494bc..dc462b7653 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -742,4 +742,11 @@ export default class Common extends EventEmitter { consensusConfig(): any { return (this._chainParams)['consensus'][this.consensusAlgorithm()] } + + /** + * Returns a deep copy of this common instance. + */ + copy(): Common { + return Object.assign(Object.create(Object.getPrototypeOf(this)), this) + } } diff --git a/packages/tx/src/baseTransaction.ts b/packages/tx/src/baseTransaction.ts index 9bfd482ff5..36c2f749d5 100644 --- a/packages/tx/src/baseTransaction.ts +++ b/packages/tx/src/baseTransaction.ts @@ -46,10 +46,7 @@ export abstract class BaseTransaction { this._validateExceedsMaxInteger(validateCannotExceedMaxInteger) - this.common = - (txOptions.common && - Object.assign(Object.create(Object.getPrototypeOf(txOptions.common)), txOptions.common)) ?? - new Common({ chain: 'mainnet' }) + this.common = txOptions.common?.copy() ?? new Common({ chain: 'mainnet' }) } /**