From 2de990fbb258538791d7af2734523fb375c8d9d2 Mon Sep 17 00:00:00 2001 From: nebojsa94 Date: Fri, 20 Nov 2020 04:20:54 +0100 Subject: [PATCH] fix: storage value encoding in forked trie. (#658) --- lib/forking/forked_storage_trie.js | 2 +- lib/utils/to.js | 10 ++++++++++ test/local/forking/debug.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 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", ""); 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); + } + } + }); });