Skip to content
This repository was archived by the owner on Dec 10, 2020. It is now read-only.

Commit 085d314

Browse files
holgerd77ryanio
authored andcommitted
Added datadir to Config, new getSyncDataDir() Config method
1 parent e0d7e25 commit 085d314

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

bin/cli.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Config } from '../lib/config'
1010
import Common from '@ethereumjs/common'
1111
const RPCManager = require('../lib/rpc')
1212
const level = require('level')
13-
const os = require('os')
1413
const path = require('path')
1514
const fs = require('fs-extra')
1615

@@ -39,7 +38,7 @@ const args = require('yargs')
3938
},
4039
datadir: {
4140
describe: 'Data directory for the blockchain',
42-
default: `${os.homedir()}/Library/Ethereum`,
41+
default: Config.DATADIR_DEFAULT,
4342
},
4443
transports: {
4544
describe: 'Network transports',
@@ -115,15 +114,13 @@ function runRpcServer(node: any, options: any) {
115114
}
116115

117116
async function run() {
118-
const syncDirName = args.syncmode === 'light' ? 'lightchaindata' : 'chaindata'
119117
// give network id precedence over network name
120118
if (args.networkId) {
121119
const network = networks.find((n) => n[0] === `${args.networkId}`)
122120
if (network) {
123121
args.network = network[1]
124122
}
125123
}
126-
const networkDirName = args.network === 'mainnet' ? '' : `${args.network}/`
127124

128125
const common = new Common({ chain: args.network, hardfork: 'chainstart' })
129126
const config = new Config({
@@ -146,15 +143,16 @@ async function run() {
146143
}
147144
return new Server({ config, ...t.options })
148145
})
149-
const dataDir = `${args.datadir}/${networkDirName}ethereumjs/${syncDirName}`
146+
150147
config.servers = servers
151148

152-
fs.ensureDirSync(dataDir)
153-
logger.info(`Data directory: ${dataDir}`)
149+
const syncDataDir = config.getSyncDataDirectory()
150+
fs.ensureDirSync(syncDataDir)
151+
logger.info(`Sync data directory: ${syncDataDir}`)
154152

155153
const options = {
156154
config,
157-
db: level(dataDir),
155+
db: level(syncDataDir),
158156
rpcport: args.rpcport,
159157
rpcaddr: args.rpcaddr,
160158
}

lib/config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const os = require('os')
12
import Common from '@ethereumjs/common'
23
import { Logger } from 'winston'
34
import { defaultLogger } from './logging'
@@ -34,6 +35,10 @@ export interface Options {
3435
* Default: false
3536
*/
3637
lightserv?: boolean
38+
/**
39+
* Root data directory for the blockchain
40+
*/
41+
datadir?: string
3742
/**
3843
* Number of peers needed before syncing
3944
*
@@ -54,6 +59,7 @@ export class Config {
5459
public servers: (RlpxServer | Libp2pServer)[]
5560
public syncmode: string
5661
public lightserv: boolean
62+
public datadir: string
5763
public minPeers: number
5864
public maxPeers: number
5965

@@ -62,6 +68,7 @@ export class Config {
6268
public static readonly SERVERS_DEFAULT = []
6369
public static readonly SYNCMODE_DEFAULT = 'fast'
6470
public static readonly LIGHTSERV_DEFAULT = false
71+
public static readonly DATADIR_DEFAULT = `${os.homedir()}/Library/Ethereum`
6572
public static readonly MINPEERS_DEFAULT = 2
6673
public static readonly MAXPEERS_DEFAULT = 25
6774

@@ -76,7 +83,21 @@ export class Config {
7683
this.servers = options.servers ?? Config.SERVERS_DEFAULT
7784
this.syncmode = options.syncmode ?? Config.SYNCMODE_DEFAULT
7885
this.lightserv = options.lightserv ?? Config.LIGHTSERV_DEFAULT
86+
this.datadir = options.datadir ?? Config.DATADIR_DEFAULT
7987
this.minPeers = options.minPeers ?? Config.MINPEERS_DEFAULT
8088
this.maxPeers = options.maxPeers ?? Config.MAXPEERS_DEFAULT
8189
}
90+
91+
/**
92+
* Returns the directory for storing the client sync data
93+
* based on syncmode and selected chain (subdirectory of 'datadir')
94+
*/
95+
getSyncDataDirectory(): string {
96+
const syncDirName = this.syncmode === 'light' ? 'lightchaindata' : 'chaindata'
97+
const chain = this.common.chainName()
98+
const networkDirName = chain === 'mainnet' ? '' : `${chain}/`
99+
100+
const dataDir = `${this.datadir}/${networkDirName}ethereumjs/${syncDirName}`
101+
return dataDir
102+
}
82103
}

0 commit comments

Comments
 (0)