From 27c140355e9561d08e94f658044227867e6bd1c4 Mon Sep 17 00:00:00 2001 From: Brad Decker Date: Wed, 30 Jun 2021 09:39:00 -0500 Subject: [PATCH] Switch hardfork in getCommonConfiguration when EIP-1559 support is detected (#11385) --- app/scripts/controllers/transactions/index.js | 21 +++++++++++++------ .../controllers/transactions/index.test.js | 1 + app/scripts/metamask-controller.js | 3 +++ shared/constants/network.js | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 01c6a4727..a921ad52b 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -26,6 +26,7 @@ import { import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller'; import { GAS_LIMITS } from '../../../../shared/constants/gas'; import { + HARDFORKS, MAINNET, NETWORK_TYPE_RPC, } from '../../../../shared/constants/network'; @@ -35,8 +36,6 @@ import TxGasUtil from './tx-gas-utils'; import PendingTransactionTracker from './pending-tx-tracker'; import * as txUtils from './lib/util'; -const HARDFORK = 'berlin'; - const hstInterface = new ethers.utils.Interface(abi); const MAX_MEMSTORE_TX_LIST_SIZE = 100; // Number of transactions (by unique nonces) to keep in memory @@ -82,6 +81,7 @@ export default class TransactionController extends EventEmitter { this.networkStore = opts.networkStore || new ObservableStore({}); this._getCurrentChainId = opts.getCurrentChainId; this.getProviderConfig = opts.getProviderConfig; + this.getEIP1559Compatibility = opts.getEIP1559Compatibility; this.preferencesStore = opts.preferencesStore || new ObservableStore({}); this.provider = opts.provider; this.getPermittedAccounts = opts.getPermittedAccounts; @@ -171,14 +171,23 @@ export default class TransactionController extends EventEmitter { * transaction type to use. * @returns {Common} common configuration object */ - getCommonConfiguration() { + async getCommonConfiguration() { const { type, nickname: name } = this.getProviderConfig(); + const supportsEIP1559 = await this.getEIP1559Compatibility(); + + // This logic below will have to be updated each time a hardfork happens + // that carries with it a new Transaction type. It is inconsequential for + // hardforks that do not include new types. + const hardfork = supportsEIP1559 ? HARDFORKS.LONDON : HARDFORKS.BERLIN; // type will be one of our default network names or 'rpc'. the default // network names are sufficient configuration, simply pass the name as the // chain argument in the constructor. if (type !== NETWORK_TYPE_RPC) { - return new Common({ chain: type, hardfork: HARDFORK }); + return new Common({ + chain: type, + hardfork, + }); } // For 'rpc' we need to use the same basic configuration as mainnet, @@ -203,7 +212,7 @@ export default class TransactionController extends EventEmitter { networkId: networkId === 'loading' ? 0 : parseInt(networkId, 10), }; - return Common.forCustomChain(MAINNET, customChainParams, HARDFORK); + return Common.forCustomChain(MAINNET, customChainParams, hardfork); } /** @@ -637,7 +646,7 @@ export default class TransactionController extends EventEmitter { }; // sign tx const fromAddress = txParams.from; - const common = this.getCommonConfiguration(); + const common = await this.getCommonConfiguration(); const unsignedEthTx = TransactionFactory.fromTxData(txParams, { common }); const signedEthTx = await this.signEthTx(unsignedEthTx, fromAddress); diff --git a/app/scripts/controllers/transactions/index.test.js b/app/scripts/controllers/transactions/index.test.js index 1b8172394..26f15ccfb 100644 --- a/app/scripts/controllers/transactions/index.test.js +++ b/app/scripts/controllers/transactions/index.test.js @@ -50,6 +50,7 @@ describe('Transaction Controller', function () { return '0xee6b2800'; }, networkStore: new ObservableStore(currentNetworkId), + getEIP1559Compatibility: () => Promise.resolve(true), txHistoryLimit: 10, blockTracker: blockTrackerStub, signTransaction: (ethTx) => diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index abe6d6c7a..20cb8655e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -336,6 +336,9 @@ export default class MetamaskController extends EventEmitter { getProviderConfig: this.networkController.getProviderConfig.bind( this.networkController, ), + getEIP1559Compatibility: this.networkController.getEIP1559Compatibility.bind( + this.networkController, + ), networkStore: this.networkController.networkStore, getCurrentChainId: this.networkController.getCurrentChainId.bind( this.networkController, diff --git a/shared/constants/network.js b/shared/constants/network.js index 01891d57c..5e3d2b959 100644 --- a/shared/constants/network.js +++ b/shared/constants/network.js @@ -120,3 +120,24 @@ export const NATIVE_CURRENCY_TOKEN_IMAGE_MAP = { }; export const INFURA_BLOCKED_KEY = 'countryBlocked'; + +/** + * Hardforks are points in the chain where logic is changed significantly + * enough where there is a fork and the new fork becomes the active chain. + * These constants are presented in chronological order starting with BERLIN + * because when we first needed to track the hardfork we had launched support + * for EIP-2718 (where transactions can have types and different shapes) and + * EIP-2930 (optional access lists), which were included in BERLIN. + * + * BERLIN - forked at block number 12,244,000, included typed transactions and + * optional access lists + * LONDON - future, upcoming fork that introduces the baseFeePerGas, an amount + * of the ETH transaction fees that will be burned instead of given to the + * miner. This change necessitated the third type of transaction envelope to + * specify maxFeePerGas and maxPriorityFeePerGas moving the fee bidding system + * to a second price auction model. + */ +export const HARDFORKS = { + BERLIN: 'berlin', + LONDON: 'london', +};