forked from NomicFoundation/hardhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into run-block-as-test
- Loading branch information
Showing
10 changed files
with
295 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/hardhat-core/src/internal/hardhat-network/provider/fork/ForkTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
BufferLike, | ||
PrefixedHexString, | ||
Transaction, | ||
TransactionOptions, | ||
TxData, | ||
} from "ethereumjs-tx"; | ||
import { BN, bufferToInt, ecrecover } from "ethereumjs-util"; | ||
|
||
import { InternalError } from "../errors"; | ||
|
||
// tslint:disable only-hardhat-error | ||
|
||
/** | ||
* Custom Transaction class to avoid EIP155 errors when hardhat is forked | ||
*/ | ||
export class ForkTransaction extends Transaction { | ||
private readonly _chainId: number; | ||
|
||
constructor( | ||
chainId: number, | ||
data: Buffer | PrefixedHexString | BufferLike[] | TxData = {}, | ||
opts: TransactionOptions = {} | ||
) { | ||
super(data, opts); | ||
|
||
this._chainId = chainId; | ||
|
||
const msgHash = this.hash(false); | ||
const v = bufferToInt(this.v); | ||
|
||
const senderPubKey = ecrecover( | ||
msgHash, | ||
v, | ||
this.r, | ||
this.s, | ||
(this as any)._implementsEIP155() ? chainId : undefined | ||
); | ||
|
||
(this as any)._senderPubKey = senderPubKey; | ||
} | ||
|
||
public verifySignature(): boolean { | ||
return true; | ||
} | ||
|
||
public getChainId(): number { | ||
return this._chainId; | ||
} | ||
|
||
public sign() { | ||
throw new InternalError("`sign` is not implemented in ForkTransaction"); | ||
} | ||
public getDataFee(): BN { | ||
throw new InternalError( | ||
"`getDataFee` is not implemented in ForkTransaction" | ||
); | ||
} | ||
public getBaseFee(): BN { | ||
throw new InternalError( | ||
"`getBaseFee` is not implemented in ForkTransaction" | ||
); | ||
} | ||
public getUpfrontCost(): BN { | ||
throw new InternalError( | ||
"`getUpfrontCost` is not implemented in ForkTransaction" | ||
); | ||
} | ||
|
||
public validate(_?: false): boolean; | ||
public validate(_: true): string; | ||
public validate(_: boolean = false): boolean | string { | ||
throw new InternalError("`validate` is not implemented in ForkTransaction"); | ||
} | ||
|
||
public toCreationAddress(): boolean { | ||
throw new InternalError( | ||
"`toCreationAddress` is not implemented in ForkTransaction" | ||
); | ||
} | ||
} | ||
|
||
// override private methods | ||
const ForkTransactionPrototype: any = ForkTransaction.prototype; | ||
|
||
// make _validateV a noop | ||
ForkTransactionPrototype._validateV = function () {}; | ||
|
||
ForkTransactionPrototype._implementsEIP155 = function (): boolean { | ||
const chainId = this.getChainId(); | ||
const v = bufferToInt(this.v); | ||
|
||
return v === chainId * 2 + 35 || v === chainId * 2 + 36; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 11 additions & 9 deletions
20
packages/hardhat-core/src/internal/hardhat-network/provider/utils/makeForkCommon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import Common from "ethereumjs-common"; | ||
import { BN } from "ethereumjs-util"; | ||
|
||
import { JsonRpcClient } from "../../jsonrpc/client"; | ||
import { ForkedNodeConfig } from "../node-types"; | ||
|
||
export async function makeForkCommon( | ||
forkClient: JsonRpcClient, | ||
forkBlockNumber: BN | ||
) { | ||
const common = new Common(forkClient.getNetworkId()); | ||
common.setHardfork(common.activeHardfork(forkBlockNumber.toNumber())); | ||
return common; | ||
export async function makeForkCommon(config: ForkedNodeConfig) { | ||
return Common.forCustomChain( | ||
"mainnet", | ||
{ | ||
chainId: config.chainId, | ||
networkId: config.networkId, | ||
name: config.networkName, | ||
}, | ||
config.hardfork | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.