Skip to content

Commit

Permalink
Restrict trie/blockchain variables to be accessed via stateManager only
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Feb 2, 2018
1 parent 59de43b commit 5e20d07
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
9 changes: 2 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}

Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions lib/runBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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
}
Expand All @@ -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')) {
Expand All @@ -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 ')
}
}
Expand Down
10 changes: 4 additions & 6 deletions lib/runBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5e20d07

Please sign in to comment.