From 38c5c75cf5c8837c3e4b90a00dc04800e922fbcf Mon Sep 17 00:00:00 2001 From: "nebojsa.urosevic" Date: Mon, 9 Nov 2020 10:51:35 +0100 Subject: [PATCH 1/2] Test storage slot creation gas cost when forking. --- test/local/forking/debug.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/local/forking/debug.js b/test/local/forking/debug.js index 880d895d05..b5feef0827 100644 --- a/test/local/forking/debug.js +++ b/test/local/forking/debug.js @@ -2,6 +2,7 @@ const assert = require("assert"); const bootstrap = require("../../helpers/contract/bootstrap"); const generateSend = require("../../helpers/utils/rpc"); const initializeTestProvider = require("../../helpers/web3/initializeTestProvider"); +const Common = require("ethereumjs-common").default; /** * NOTE: Naming in these tests is a bit confusing. Here, the "main chain" @@ -15,6 +16,7 @@ describe("Forking Debugging", () => { let forkedContext; let mainContext; let mainAccounts; + let common; const logger = { log: function(msg) {} }; @@ -42,6 +44,10 @@ describe("Forking Debugging", () => { logger, seed: "forked provider" }); + + common = Common.forCustomChain("mainnet", { + name: "ganache" + }); mainAccounts = await mainContext.web3.eth.getAccounts(); }); @@ -70,4 +76,27 @@ describe("Forking Debugging", () => { const txReturnValue = parseInt(txMemory[txMemory.length - 1], 16); assert.strictEqual(txReturnValue, 2); }); + + it("successfully manages storage slot creation gas consumption", async() => { + const { bytecode } = forkedContext; + const { web3: mainWeb3 } = mainContext; + + const deployedContract = await mainWeb3.eth.sendTransaction({ + from: mainAccounts[0], + data: bytecode, + gas: 3141592 + }); + + const send = generateSend(mainWeb3.currentProvider); + + const result = await send("debug_traceTransaction", deployedContract.transactionHash, {}); + + for (let i = 0; i < result.result.structLogs.length; i++) { + if (result.result.structLogs[i].op === "SSTORE") { + // ensure that every SSTORE in contract creation triggers slot creation + const gasCost = common.param("gasPrices", "sstoreInitGasEIP2200", "istanbul"); + assert.strictEqual(result.result.structLogs[i].gasCost, gasCost); + } + } + }); }); From 40f6faa12ea2802b01a0b8315757a72648f5574f Mon Sep 17 00:00:00 2001 From: "nebojsa.urosevic" Date: Mon, 9 Nov 2020 11:00:03 +0100 Subject: [PATCH 2/2] Fix value encoding when fetching storage. --- lib/forking/forked_storage_trie.js | 2 +- lib/utils/to.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/forking/forked_storage_trie.js b/lib/forking/forked_storage_trie.js index dadf6eb8b7..acdde00036 100644 --- a/lib/forking/forked_storage_trie.js +++ b/lib/forking/forked_storage_trie.js @@ -62,7 +62,7 @@ ForkedStorageBaseTrie.prototype.get = function(key, callback) { return callback(err); } - value = utils.rlp.encode(value); + value = to.rpcQuantityBuffer(value); callback(null, value); } diff --git a/lib/utils/to.js b/lib/utils/to.js index 0094e91e99..fa64f20f34 100644 --- a/lib/utils/to.js +++ b/lib/utils/to.js @@ -64,6 +64,16 @@ module.exports = { return val; }, + rpcQuantityBuffer: function(val) { + val = this._rpcQuantityHexString(val); + + if (val === "0x0") { + val = "0x"; + } + + return utils.rlp.encode(val); + }, + rpcDataHexString: function(val, length) { if (typeof length === "number") { val = this.hex(val).replace("0x", "");