From f6b86153a0cf7623e2871b1413b09f41dffe2e5a Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Thu, 10 Jan 2019 11:55:18 +0100 Subject: [PATCH 1/2] Fixed an issue where trie passed as opts.state is overwritten if opts.activePrecomplies is set to true --- lib/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index e6b8f66d86..724dd61e1e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -57,7 +57,6 @@ function VM (opts = {}) { } else { var trie = opts.state || new Trie() if (opts.activatePrecompiles) { - trie = new Trie() for (var i = 1; i <= 8; i++) { trie.put(new BN(i).toArrayLike(Buffer, 'be', 20), new Account().serialize()) } From 240dab62a89ba14d5eb4bd5272564a91e759f542 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Thu, 10 Jan 2019 13:50:13 +0100 Subject: [PATCH 2/2] Added API test case to check if state manager instance works with trie provided on VM instantiation (opts.state) --- package.json | 2 +- tests/api/index.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0a10158400..92fde19f9a 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "ethereumjs-util": "^6.0.0", "fake-merkle-patricia-tree": "^1.0.1", "functional-red-black-tree": "^1.0.1", - "merkle-patricia-tree": "^2.1.2", + "merkle-patricia-tree": "^2.3.2", "rustbn.js": "~0.2.0", "safe-buffer": "^5.1.1" }, diff --git a/tests/api/index.js b/tests/api/index.js index 1f569f234a..aa9a01e096 100644 --- a/tests/api/index.js +++ b/tests/api/index.js @@ -2,13 +2,14 @@ const { promisify } = require('util') const tape = require('tape') const util = require('ethereumjs-util') const Block = require('ethereumjs-block') +const Trie = require('merkle-patricia-tree/secure') const VM = require('../../lib/index') const { setupVM } = require('./utils') const { setupPreConditions } = require('../util') const testData = require('./testdata.json') tape('VM with fake blockchain', (t) => { - t.test('should insantiate without params', (st) => { + t.test('should instantiate without params', (st) => { const vm = new VM() st.ok(vm.stateManager) st.deepEqual(vm.stateManager._trie.root, util.KECCAK256_RLP, 'it has default trie') @@ -22,6 +23,15 @@ tape('VM with fake blockchain', (t) => { st.end() }) + t.test('should work with trie (state) provided', (st) => { + let trie = new Trie() + trie.isTestTrie = true + let vm = new VM({ state: trie, activatePrecompiles: true }) + st.notEqual(vm.stateManager._trie.root, util.KECCAK256_RLP, 'it has different root') + st.ok(vm.stateManager._trie.isTestTrie, 'it works on trie provided') + st.end() + }) + t.test('should only accept valid chain and fork', (st) => { let vm = new VM({ chain: 'ropsten', hardfork: 'byzantium' }) st.equal(vm.stateManager._common.param('gasPrices', 'ecAdd'), 500)