From 5e20d074502d91132d1ff24d6ab155e79caef4fb Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 2 Feb 2018 00:12:15 +0000 Subject: [PATCH] Restrict trie/blockchain variables to be accessed via stateManager only --- lib/index.js | 9 ++------- lib/runBlock.js | 10 +++++----- lib/runBlockchain.js | 10 ++++------ 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6ef81658f76..d5422e525a2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -45,11 +45,6 @@ function VM (opts = {}) { }) } - // temporary - // this is here for a gradual transition to StateManager - this.blockchain = this.stateManager.blockchain - this.trie = this.stateManager.trie - // precompiled contracts this._precompiled = {} this._precompiled['0000000000000000000000000000000000000001'] = num01 @@ -63,7 +58,7 @@ function VM (opts = {}) { if (this.opts.activatePrecompiles) { for (var i = 1; i <= 7; i++) { - this.trie.put(new BN(i).toArrayLike(Buffer, 'be', 20), new Account().serialize()) + this.stateManager.trie.put(new BN(i).toArrayLike(Buffer, 'be', 20), new Account().serialize()) } } @@ -87,7 +82,7 @@ VM.prototype.copy = function () { * Loads precompiled contracts into the state */ VM.prototype.loadCompiled = function (address, src, cb) { - this.trie.db.put(address, src, cb) + this.stateManager.trie.db.put(address, src, cb) } VM.prototype.populateCache = function (addresses, cb) { diff --git a/lib/runBlock.js b/lib/runBlock.js index 37383bdbff3..ebc5a75b811 100644 --- a/lib/runBlock.js +++ b/lib/runBlock.js @@ -37,7 +37,7 @@ module.exports = function (opts, cb) { self.stateManager.trie.root = opts.root } - this.trie.checkpoint() + self.stateManager.trie.checkpoint() // run everything async.series([ @@ -139,7 +139,7 @@ module.exports = function (opts, cb) { // handle results or error from block run function parseBlockResults (err) { if (err) { - self.trie.revert() + self.stateManager.trie.revert() cb(err) return } @@ -149,10 +149,10 @@ module.exports = function (opts, cb) { // credit all block rewards if (generateStateRoot) { - block.header.stateRoot = self.trie.root + block.header.stateRoot = self.stateManager.trie.root } - self.trie.commit(function (err) { + self.stateManager.trie.commit(function (err) { self.stateManager.cache.flush(function () { if (validateStateRoot) { if (receiptTrie.root && receiptTrie.root.toString('hex') !== block.header.receiptTrie.toString('hex')) { @@ -164,7 +164,7 @@ module.exports = function (opts, cb) { if (ethUtil.bufferToInt(block.header.gasUsed) !== Number(gasUsed)) { err = new Error((err || '') + 'invalid gasUsed ') } - if (self.trie.root.toString('hex') !== block.header.stateRoot.toString('hex')) { + if (self.stateManager.trie.root.toString('hex') !== block.header.stateRoot.toString('hex')) { err = new Error((err || '') + 'invalid block stateRoot ') } } diff --git a/lib/runBlockchain.js b/lib/runBlockchain.js index ae6010bc591..a629aa551d2 100644 --- a/lib/runBlockchain.js +++ b/lib/runBlockchain.js @@ -9,17 +9,15 @@ module.exports = function (blockchain, cb) { var self = this var headBlock, parentState - self.blockchain = self.stateManager.blockchain - // parse arguments if (typeof blockchain === 'function') { cb = blockchain } else if (blockchain) { - self.blockchain = blockchain + self.stateManager.blockchain = blockchain } // setup blockchain iterator - self.blockchain.iterator('vm', processBlock, cb) + self.stateManager.blockchain.iterator('vm', processBlock, cb) function processBlock (block, reorg, cb) { async.series([ getStartingState, @@ -30,7 +28,7 @@ module.exports = function (blockchain, cb) { function getStartingState (cb) { // if we are just starting or if a chain re-org has happened if (!headBlock || reorg) { - self.blockchain.getBlock(block.header.parentHash, function (err, parentBlock) { + self.stateManager.blockchain.getBlock(block.header.parentHash, function (err, parentBlock) { parentState = parentBlock.header.stateRoot // generate genesis state if we are at the genesis block // we don't have the genesis state @@ -55,7 +53,7 @@ module.exports = function (blockchain, cb) { if (err) { // remove invalid block console.log('Invalid block error:', err) - self.blockchain.delBlock(block.header.hash(), cb) + self.stateManager.blockchain.delBlock(block.header.hash(), cb) } else { // set as new head block headBlock = block