Skip to content

Commit

Permalink
Merge pull request ethereumjs#29 from fgimenez/data-dir-test
Browse files Browse the repository at this point in the history
Initial test structure and data dir creation test
  • Loading branch information
holgerd77 authored Jun 25, 2018
2 parents dfd8b2a + f578440 commit beedc72
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 56 deletions.
44 changes: 2 additions & 42 deletions lib/chain/ChainManager.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
const fs = require('fs')
const path = require('path')
const levelup = require('levelup')
const leveldown = require('leveldown')
const Blockchain = require('ethereumjs-blockchain')
const ethUtil = require('ethereumjs-util')

class ChainManager {
constructor (config, nm) {
constructor (config, nm, bc) {
var self = this
this._config = config
this._logger = config.logger
this._nm = nm

this._initializeDatadir()

// TODO: Store chain data in an appropriate sub folder (e.g. 'chaindata/ropsten/')
this._blockchainDB = levelup(this._config.datadir, { db: leveldown })

this._blockchain = new Blockchain({
db: this._blockchainDB,
validate: false
})
this._blockchain = bc

this._blockchain.getLatestHeader(function (err, header) {
if (err) {
Expand All @@ -46,33 +33,6 @@ class ChainManager {
}
}

// From: https://stackoverflow.com/a/40686853
// replace if there is an easier solution
_mkDirByPathSync (targetDir, {isRelativeToScript = false} = {}) {
const sep = path.sep
const initDir = path.isAbsolute(targetDir) ? sep : ''
const baseDir = isRelativeToScript ? __dirname : '.'

targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir)
try {
fs.mkdirSync(curDir)
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
return curDir
}, initDir)
}

_initializeDatadir () {
if (!fs.existsSync(this._config.datadir)) {
this._mkDirByPathSync(this._config.datadir)
}
this._logger.info(`Initialized data directory: ${this._config.datadir}`)
}

_saveNewBlocks (peer, blocks) {
var self = this
if (blocks.length > 0) {
Expand Down
49 changes: 49 additions & 0 deletions lib/chain/DBManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require('fs')
const path = require('path')
const levelup = require('levelup')
const leveldown = require('leveldown')

class DBManager {
constructor (config) {
this._config = config
this._logger = config.logger

this._initializeDatadir()

// TODO: Store chain data in an appropriate sub folder (e.g. 'chaindata/ropsten/')
this._blockchainDB = levelup(this._config.datadir, { db: leveldown })
}

blockchainDB () {
return this._blockchainDB
}

// From: https://stackoverflow.com/a/40686853
// replace if there is an easier solution
_mkDirByPathSync (targetDir, {isRelativeToScript = false} = {}) {
const sep = path.sep
const initDir = path.isAbsolute(targetDir) ? sep : ''
const baseDir = isRelativeToScript ? __dirname : '.'

targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir)
if (!fs.existsSync(curDir)) {
try {
fs.mkdirSync(curDir)
} catch (err) {
throw err
}
}
return curDir
}, initDir)
}

_initializeDatadir () {
if (!fs.existsSync(this._config.datadir)) {
this._mkDirByPathSync(this._config.datadir)
}
this._logger.info(`Initialized data directory: ${this._config.datadir}`)
}
}

module.exports = DBManager
11 changes: 10 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { randomBytes } = require('crypto')
const Blockchain = require('ethereumjs-blockchain')
const Common = require('ethereumjs-common')
const Logger = require('./logging.js')
const EthNetworkManager = require('./net/EthNetworkManager.js')
const ChainManager = require('./chain/ChainManager.js')
const DBManager = require('./chain/DBManager.js')

function runClient () {
const cliParser = require('./cliParser.js')
Expand All @@ -21,7 +23,14 @@ function runClient () {
const PRIVATE_KEY = randomBytes(32)
var nm = new EthNetworkManager(PRIVATE_KEY, config)

var cm = new ChainManager(config, nm) // eslint-disable-line
let dbm = new DBManager(config)

let bc = new Blockchain({
db: dbm.blockchainDB(),
validate: false
})

var cm = new ChainManager(config, nm, bc) // eslint-disable-line
}

runClient()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"coveralls": "^3.0.0",
"nyc": "~11.6.0",
"standard": "~11.0.1",
"tape": "~4.9.0"
"tape": "~4.9.0",
"tmp": "~0.0.33"
}
}
21 changes: 21 additions & 0 deletions tests/db-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')
const tape = require('tape')
const tmp = require('tmp')
const DBManager = require('../lib/chain/DBManager.js')
const Logger = require('./mocks/Logger.js')

tape('[DB]: Database functions', function (t) {
const config = {}
config.logger = Logger

t.test('should test data dir creation', function (st) {
const tmpdir = tmp.dirSync()
config.datadir = `${tmpdir.name}/chaindb`

new DBManager(config) // eslint-disable-line no-new

st.ok(fs.existsSync(config.datadir), 'data dir exists')

st.end()
})
})
13 changes: 1 addition & 12 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
// Remove this test once first test is established,
// just for test setup, linting test,...
// Replace with references to separate test files
// see other ethereumjs repos for reference
const tape = require('tape')

tape('[CL]: Test --debug command line option', function (t) {
t.test('should test empty command line option', function (st) {
st.equal(true, true, 'output should default to INFO log level')
st.end()
})
})
require('./db-manager.js')
5 changes: 5 additions & 0 deletions tests/mocks/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let Logger = {}

Logger.info = function () {}

module.exports = Logger
5 changes: 5 additions & 0 deletions tests/mocks/NetworkManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let NetworkManager = {}

NetworkManager.startNetworking = function () {}

module.exports = NetworkManager

0 comments on commit beedc72

Please sign in to comment.