@@ -25,11 +25,13 @@ const (
25
25
defaultMaxPeers = 8
26
26
defaultBanDuration = time .Hour * 24
27
27
defaultVerifyEnabled = false
28
+ defaultDbType = "sqlite"
28
29
)
29
30
30
31
var (
31
32
defaultConfigFile = filepath .Join (btcdHomeDir (), defaultConfigFilename )
32
- defaultDataDir = filepath .Join (btcdHomeDir (), "db" )
33
+ defaultDataDir = filepath .Join (btcdHomeDir (), "data" )
34
+ knownDbTypes = []string {"leveldb" , "sqlite" }
33
35
)
34
36
35
37
// config defines the configuration options for btcd.
@@ -58,7 +60,7 @@ type config struct {
58
60
UseTor bool `long:"tor" description:"Specifies the proxy server used is a Tor node"`
59
61
TestNet3 bool `long:"testnet" description:"Use the test network"`
60
62
RegressionTest bool `long:"regtest" description:"Use the regression test network"`
61
- DbType string `long:"dbtype" description:"DB backend to use for Block Chain"`
63
+ DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
62
64
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
63
65
}
64
66
@@ -99,6 +101,19 @@ func validLogLevel(logLevel string) bool {
99
101
return false
100
102
}
101
103
104
+ // validDbType returns whether or not dbType is a supported database type.
105
+ func validDbType (dbType string ) bool {
106
+ // Would be interesting if btcdb had a 'SupportedDBs() []string'
107
+ // API to populate this field.
108
+ for _ , knownType := range knownDbTypes {
109
+ if dbType == knownType {
110
+ return true
111
+ }
112
+ }
113
+
114
+ return false
115
+ }
116
+
102
117
// normalizePeerAddress returns addr with the default peer port appended if
103
118
// there is not already a port specified.
104
119
func normalizePeerAddress (addr string ) string {
@@ -180,6 +195,7 @@ func loadConfig() (*config, []string, error) {
180
195
BanDuration : defaultBanDuration ,
181
196
ConfigFile : defaultConfigFile ,
182
197
DataDir : defaultDataDir ,
198
+ DbType : defaultDbType ,
183
199
}
184
200
185
201
// Pre-parse the command line options to see if an alternative config
@@ -249,13 +265,32 @@ func loadConfig() (*config, []string, error) {
249
265
250
266
// Validate debug log level.
251
267
if ! validLogLevel (cfg .DebugLevel ) {
252
- str := "%s: The specified debug level is invalid -- parsed [%v]"
268
+ str := "%s: The specified debug level [%v] is invalid "
253
269
err := errors .New (fmt .Sprintf (str , "loadConfig" , cfg .DebugLevel ))
254
270
fmt .Fprintln (os .Stderr , err )
255
271
parser .WriteHelp (os .Stderr )
256
272
return nil , nil , err
257
273
}
258
274
275
+ // Validate database type.
276
+ if ! validDbType (cfg .DbType ) {
277
+ str := "%s: The specified database type [%v] is invalid -- " +
278
+ "supported types %v"
279
+ err := errors .New (fmt .Sprintf (str , "loadConfig" , cfg .DbType ,
280
+ knownDbTypes ))
281
+ fmt .Fprintln (os .Stderr , err )
282
+ parser .WriteHelp (os .Stderr )
283
+ return nil , nil , err
284
+ }
285
+
286
+ // Append the network type to the data directory so it is "namespaced"
287
+ // per network. In addition to the block database, there are other
288
+ // pieces of data that are saved to disk such as address manager state.
289
+ // All data is specific to a network, so namespacing the data directory
290
+ // means each individual piece of serialized data does not have to
291
+ // worry about changing names per network and such.
292
+ cfg .DataDir = filepath .Join (cfg .DataDir , activeNetParams .netName )
293
+
259
294
// Don't allow ban durations that are too short.
260
295
if cfg .BanDuration < time .Duration (time .Second ) {
261
296
str := "%s: The banduration option may not be less than 1s -- parsed [%v]"
@@ -303,45 +338,7 @@ func loadConfig() (*config, []string, error) {
303
338
// Add default port to all added peer addresses if needed and remove
304
339
// duplicate addresses.
305
340
cfg .AddPeers = normalizeAndRemoveDuplicateAddresses (cfg .AddPeers )
306
- cfg .ConnectPeers =
307
- normalizeAndRemoveDuplicateAddresses (cfg .ConnectPeers )
341
+ cfg .ConnectPeers = normalizeAndRemoveDuplicateAddresses (cfg .ConnectPeers )
308
342
309
- // determine which database backend to use
310
- // would be interesting of btcdb had a 'supportedDBs() []string'
311
- // API to populate this field.
312
- knownDbs := []string {"leveldb" , "sqlite" }
313
- defaultDb := "sqlite"
314
-
315
- if len (cfg .DbType ) == 0 {
316
- // if db was not specified, use heuristic to see what
317
- // type the database is (if the specified database is a
318
- // file then it is sqlite3, if it is a directory assume
319
- // it is leveldb, if not found default to type _____
320
- dbPath := filepath .Join (cfg .DataDir , activeNetParams .dbName )
321
-
322
- fi , err := os .Stat (dbPath )
323
- if err == nil {
324
- if fi .IsDir () {
325
- cfg .DbType = "leveldb"
326
- } else {
327
- cfg .DbType = "sqlite"
328
- }
329
- } else {
330
- cfg .DbType = defaultDb
331
- }
332
- } else {
333
- // does checking this here really make sense ?
334
- typeVerified := false
335
- for _ , dbtype := range knownDbs {
336
- if cfg .DbType == dbtype {
337
- typeVerified = true
338
- }
339
- }
340
- if typeVerified == false {
341
- err := fmt .Errorf ("Specified database type [%v] not in list of supported databases: %v" , cfg .DbType , knownDbs )
342
- fmt .Fprintln (os .Stderr , err )
343
- return nil , nil , err
344
- }
345
- }
346
343
return & cfg , remainingArgs , nil
347
344
}
0 commit comments