|
| 1 | +import assert from "assert"; |
| 2 | +import { join } from "path"; |
| 3 | +import { EthereumProvider } from "../src/provider"; |
| 4 | +import compile from "./helpers/compile"; |
| 5 | +import getProvider from "./helpers/getProvider"; |
| 6 | + |
| 7 | +describe("merge", () => { |
| 8 | + let provider: EthereumProvider; |
| 9 | + let accounts: string[]; |
| 10 | + beforeEach("get provider", async () => { |
| 11 | + provider = await getProvider({ |
| 12 | + wallet: { seed: "temet nosce" }, |
| 13 | + chain: { hardfork: "merge" } |
| 14 | + }); |
| 15 | + accounts = await provider.send("eth_accounts"); |
| 16 | + }); |
| 17 | + describe("safe", () => { |
| 18 | + it("returns the safe block", async () => { |
| 19 | + const safe = await provider.send("eth_getBlockByNumber", ["safe", true]); |
| 20 | + const latest = await provider.send("eth_getBlockByNumber", [ |
| 21 | + "latest", |
| 22 | + true |
| 23 | + ]); |
| 24 | + assert.deepStrictEqual(safe, latest); |
| 25 | + }); |
| 26 | + }); |
| 27 | + describe("finalized", () => { |
| 28 | + it("returns the finalized block", async () => { |
| 29 | + const finalized = await provider.send("eth_getBlockByNumber", [ |
| 30 | + "finalized", |
| 31 | + true |
| 32 | + ]); |
| 33 | + const latest = await provider.send("eth_getBlockByNumber", [ |
| 34 | + "latest", |
| 35 | + true |
| 36 | + ]); |
| 37 | + assert.deepStrictEqual(finalized, latest); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("difficulty", () => { |
| 42 | + let contract: ReturnType<typeof compile>; |
| 43 | + let contractAddress: string; |
| 44 | + beforeEach("deploy contract", async () => { |
| 45 | + contract = compile(join(__dirname, "./contracts/Merge.sol")); |
| 46 | + const transaction = { |
| 47 | + from: accounts[0], |
| 48 | + data: contract.code, |
| 49 | + gasLimit: "0x2fefd8" |
| 50 | + }; |
| 51 | + const txHash = await provider.send("eth_sendTransaction", [transaction]); |
| 52 | + const receipt = await provider.send("eth_getTransactionReceipt", [ |
| 53 | + txHash |
| 54 | + ]); |
| 55 | + contractAddress = receipt.contractAddress; |
| 56 | + }); |
| 57 | + it("uses the block's mixhash value as the PREVRANDAO opcode", async () => { |
| 58 | + const block = await provider.send("eth_getBlockByNumber", [ |
| 59 | + "latest", |
| 60 | + false |
| 61 | + ]); |
| 62 | + assert.strictEqual(block.difficulty, "0x0"); |
| 63 | + |
| 64 | + const constructorResult = await provider.send("eth_call", [ |
| 65 | + { |
| 66 | + from: accounts[0], |
| 67 | + to: contractAddress, |
| 68 | + data: `0x${contract.contract.evm.methodIdentifiers["difficulty()"]}` |
| 69 | + }, |
| 70 | + block.number |
| 71 | + ]); |
| 72 | + |
| 73 | + const txHash = await provider.send("eth_sendTransaction", [ |
| 74 | + { |
| 75 | + from: accounts[0], |
| 76 | + to: contractAddress, |
| 77 | + gasLimit: "0x2fefd8", |
| 78 | + data: `0x${contract.contract.evm.methodIdentifiers["getCurrentDifficulty()"]}` |
| 79 | + } |
| 80 | + ]); |
| 81 | + const receipt = await provider.send("eth_getTransactionReceipt", [ |
| 82 | + txHash |
| 83 | + ]); |
| 84 | + // make sure it didn't revert, which is will do if `difficulty` is not > `1` |
| 85 | + assert.strictEqual(receipt.status, "0x1"); |
| 86 | + |
| 87 | + const viewResult1 = await provider.send("eth_call", [ |
| 88 | + { |
| 89 | + from: accounts[0], |
| 90 | + to: contractAddress, |
| 91 | + data: `0x${contract.contract.evm.methodIdentifiers["getCurrentDifficulty()"]}` |
| 92 | + }, |
| 93 | + block.number |
| 94 | + ]); |
| 95 | + |
| 96 | + // it shouldn't be all 0s for the merge: |
| 97 | + assert.notStrictEqual( |
| 98 | + block.mixHash, |
| 99 | + "0x0000000000000000000000000000000000000000000000000000000000000000" |
| 100 | + ); |
| 101 | + |
| 102 | + // the results should match |
| 103 | + assert.strictEqual(constructorResult, block.mixHash); |
| 104 | + assert.strictEqual(viewResult1, block.mixHash); |
| 105 | + |
| 106 | + const lastblock = await provider.send("eth_getBlockByNumber", [ |
| 107 | + receipt.blockNumber, |
| 108 | + false |
| 109 | + ]); |
| 110 | + assert.strictEqual(lastblock.difficulty, "0x0"); |
| 111 | + const viewResult2 = await provider.send("eth_call", [ |
| 112 | + { |
| 113 | + from: accounts[0], |
| 114 | + to: contractAddress, |
| 115 | + data: `0x${contract.contract.evm.methodIdentifiers["getCurrentDifficulty()"]}` |
| 116 | + }, |
| 117 | + "latest" |
| 118 | + ]); |
| 119 | + // it changed |
| 120 | + assert.notStrictEqual(viewResult1, viewResult2); |
| 121 | + assert.strictEqual(viewResult2, lastblock.mixHash); |
| 122 | + }).timeout(0); |
| 123 | + }); |
| 124 | +}); |
0 commit comments