annotate src/db.cpp @ 2275:fd09fd11692a draft

Move CWalletDB code to new walletdb module. In addition to standard code separation, this change opens the door to fixing several include inter-dependencies.
author Jeff Garzik <jeff@garzik.org>
date Sun, 15 Apr 2012 17:39:49 -0400
parents f2eba0cb504a
children 0dcf927bc6b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
1818
20667468f95b Update all copyrights to 2012
Gavin Andresen <gavinandresen@gmail.com>
parents: 1813
diff changeset
2 // Copyright (c) 2009-2012 The Bitcoin developers
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
3 // Distributed under the MIT/X11 software license, see the accompanying
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
5
605
1ae81ad9b0e5 Only include db.h when we have to.
Jordan Lewis <jordanthelewis@gmail.com>
parents: 595
diff changeset
6 #include "db.h"
2274
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
7 #include "util.h"
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
8 #include "main.h"
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
9 #include "wallet.h"
1840
0760e07f7938 Added 'Backup Wallet' menu option
sje397 <sje397@gmail.com>
parents: 1818
diff changeset
10 #include <boost/version.hpp>
695
9609c7af94ba Fix missing includes needed for Boost 1.46.
Shane Wegner <shane-github@csy.ca>
parents: 690
diff changeset
11 #include <boost/filesystem.hpp>
611
503d51554676 Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents: 607
diff changeset
12 #include <boost/filesystem/fstream.hpp>
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
13
2274
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
14 #ifndef WIN32
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
15 #include "sys/stat.h"
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
16 #endif
f2eba0cb504a Remove headers.h
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
17
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
18 using namespace std;
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
19 using namespace boost;
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
20
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
21
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
22 unsigned int nWalletDBUpdated;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
23
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
24
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
25
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
26 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
27 // CDB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
28 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
29
2275
fd09fd11692a Move CWalletDB code to new walletdb module.
Jeff Garzik <jeff@garzik.org>
parents: 2274
diff changeset
30 CCriticalSection cs_db;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
31 static bool fDbEnvInit = false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
32 DbEnv dbenv(0);
2275
fd09fd11692a Move CWalletDB code to new walletdb module.
Jeff Garzik <jeff@garzik.org>
parents: 2274
diff changeset
33 map<string, int> mapFileUseCount;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
34 static map<string, Db*> mapDb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
35
1509
e1415e140a58 Only remove database log files on shutdown after wallet encryption/rewrite
Gavin Andresen <gavinandresen@gmail.com>
parents: 1506
diff changeset
36 static void EnvShutdown()
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
37 {
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
38 if (!fDbEnvInit)
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
39 return;
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
40
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
41 fDbEnvInit = false;
1505
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
42 try
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
43 {
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
44 dbenv.close(0);
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
45 }
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
46 catch (const DbException& e)
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
47 {
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
48 printf("EnvShutdown exception: %s (%d)\n", e.what(), e.get_errno());
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
49 }
2207
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
50 DbEnv(0).remove(GetDataDir().string().c_str(), 0);
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
51 }
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
52
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
53 class CDBInit
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
54 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
55 public:
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
56 CDBInit()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
57 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
58 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
59 ~CDBInit()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
60 {
1509
e1415e140a58 Only remove database log files on shutdown after wallet encryption/rewrite
Gavin Andresen <gavinandresen@gmail.com>
parents: 1506
diff changeset
61 EnvShutdown();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
62 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
63 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
64 instance_of_cdbinit;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
65
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
66
2207
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
67 CDB::CDB(const char *pszFile, const char* pszMode) : pdb(NULL)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
68 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
69 int ret;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
70 if (pszFile == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
71 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
72
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
73 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
74 bool fCreate = strchr(pszMode, 'c');
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
75 unsigned int nFlags = DB_THREAD;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
76 if (fCreate)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
77 nFlags |= DB_CREATE;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
78
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
79 {
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
80 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
81 if (!fDbEnvInit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
82 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
83 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
84 return;
2207
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
85 filesystem::path pathDataDir = GetDataDir();
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
86 filesystem::path pathLogDir = pathDataDir / "database";
2183
e50d5fc913ea updated db.cpp to use make_preferred()
Philip Kaufmann <phil.kaufmann@t-online.de>
parents: 2176
diff changeset
87 filesystem::create_directory(pathLogDir);
2207
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
88 filesystem::path pathErrorFile = pathDataDir / "db.log";
2183
e50d5fc913ea updated db.cpp to use make_preferred()
Philip Kaufmann <phil.kaufmann@t-online.de>
parents: 2176
diff changeset
89 printf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str());
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
90
2062
d6479f2e1371 Speed up block downloading
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
91 int nDbCache = GetArg("-dbcache", 25);
2183
e50d5fc913ea updated db.cpp to use make_preferred()
Philip Kaufmann <phil.kaufmann@t-online.de>
parents: 2176
diff changeset
92 dbenv.set_lg_dir(pathLogDir.string().c_str());
2062
d6479f2e1371 Speed up block downloading
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
93 dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
2114
8c6353ab7e73 Auto-remove logs, and decrease logfile size to 10M
Gavin Andresen <gavinandresen@gmail.com>
parents: 2097
diff changeset
94 dbenv.set_lg_bsize(1048576);
8c6353ab7e73 Auto-remove logs, and decrease logfile size to 10M
Gavin Andresen <gavinandresen@gmail.com>
parents: 2097
diff changeset
95 dbenv.set_lg_max(10485760);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
96 dbenv.set_lk_max_locks(10000);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
97 dbenv.set_lk_max_objects(10000);
2183
e50d5fc913ea updated db.cpp to use make_preferred()
Philip Kaufmann <phil.kaufmann@t-online.de>
parents: 2176
diff changeset
98 dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
99 dbenv.set_flags(DB_AUTO_COMMIT, 1);
2114
8c6353ab7e73 Auto-remove logs, and decrease logfile size to 10M
Gavin Andresen <gavinandresen@gmail.com>
parents: 2097
diff changeset
100 dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
2207
34d7dc611b26 Use filesystem::path instead of manual string tinkering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2205
diff changeset
101 ret = dbenv.open(pathDataDir.string().c_str(),
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
102 DB_CREATE |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
103 DB_INIT_LOCK |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
104 DB_INIT_LOG |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
105 DB_INIT_MPOOL |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
106 DB_INIT_TXN |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
107 DB_THREAD |
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
108 DB_RECOVER,
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
109 S_IRUSR | S_IWUSR);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
110 if (ret > 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
111 throw runtime_error(strprintf("CDB() : error %d opening database environment", ret));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
112 fDbEnvInit = true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
113 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
114
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
115 strFile = pszFile;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
116 ++mapFileUseCount[strFile];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
117 pdb = mapDb[strFile];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
118 if (pdb == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
119 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
120 pdb = new Db(&dbenv, 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
121
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
122 ret = pdb->open(NULL, // Txn pointer
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
123 pszFile, // Filename
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
124 "main", // Logical db name
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
125 DB_BTREE, // Database type
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
126 nFlags, // Flags
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
127 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
128
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
129 if (ret > 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
130 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
131 delete pdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
132 pdb = NULL;
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
133 {
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
134 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
135 --mapFileUseCount[strFile];
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
136 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
137 strFile = "";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
138 throw runtime_error(strprintf("CDB() : can't open database file %s, error %d", pszFile, ret));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
139 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
140
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
141 if (fCreate && !Exists(string("version")))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
142 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
143 bool fTmp = fReadOnly;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
144 fReadOnly = false;
1584
a67f13278e2c Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents: 1511
diff changeset
145 WriteVersion(CLIENT_VERSION);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
146 fReadOnly = fTmp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
147 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
148
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
149 mapDb[strFile] = pdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
150 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
151 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
152 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
153
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
154 void CDB::Close()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
155 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
156 if (!pdb)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
157 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
158 if (!vTxn.empty())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
159 vTxn.front()->abort();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
160 vTxn.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
161 pdb = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
162
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
163 // Flush database activity from memory pool to disk log
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
164 unsigned int nMinutes = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
165 if (fReadOnly)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
166 nMinutes = 1;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
167 if (strFile == "addr.dat")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
168 nMinutes = 2;
2116
94844608a7d7 Use transactions-updated as flush condition
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2114
diff changeset
169 if (strFile == "blkindex.dat" && IsInitialBlockDownload())
94844608a7d7 Use transactions-updated as flush condition
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2114
diff changeset
170 nMinutes = 5;
94844608a7d7 Use transactions-updated as flush condition
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2114
diff changeset
171
2176
ad3ef2ebaef0 Flush on log size instead of transaction count
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2120
diff changeset
172 dbenv.txn_checkpoint(nMinutes ? GetArg("-dblogsize", 100)*1024 : 0, nMinutes, 0);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
173
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
174 {
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
175 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
176 --mapFileUseCount[strFile];
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
177 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
178 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
179
2275
fd09fd11692a Move CWalletDB code to new walletdb module.
Jeff Garzik <jeff@garzik.org>
parents: 2274
diff changeset
180 void CloseDb(const string& strFile)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
181 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
182 {
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
183 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
184 if (mapDb[strFile] != NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
185 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
186 // Close the database handle
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
187 Db* pdb = mapDb[strFile];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
188 pdb->close(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
189 delete pdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
190 mapDb[strFile] = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
191 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
192 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
193 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
194
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
195 bool CDB::Rewrite(const string& strFile, const char* pszSkip)
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
196 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
197 while (!fShutdown)
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
198 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
199 {
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
200 LOCK(cs_db);
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
201 if (!mapFileUseCount.count(strFile) || mapFileUseCount[strFile] == 0)
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
202 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
203 // Flush log data to the dat file
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
204 CloseDb(strFile);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
205 dbenv.txn_checkpoint(0, 0, 0);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
206 dbenv.lsn_reset(strFile.c_str(), 0);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
207 mapFileUseCount.erase(strFile);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
208
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
209 bool fSuccess = true;
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
210 printf("Rewriting %s...\n", strFile.c_str());
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
211 string strFileRes = strFile + ".rewrite";
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
212 { // surround usage of db with extra {}
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
213 CDB db(strFile.c_str(), "r");
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
214 Db* pdbCopy = new Db(&dbenv, 0);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
215
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
216 int ret = pdbCopy->open(NULL, // Txn pointer
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
217 strFileRes.c_str(), // Filename
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
218 "main", // Logical db name
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
219 DB_BTREE, // Database type
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
220 DB_CREATE, // Flags
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
221 0);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
222 if (ret > 0)
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
223 {
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
224 printf("Cannot create database file %s\n", strFileRes.c_str());
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
225 fSuccess = false;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
226 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
227
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
228 Dbc* pcursor = db.GetCursor();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
229 if (pcursor)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
230 while (fSuccess)
1505
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
231 {
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
232 CDataStream ssKey;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
233 CDataStream ssValue;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
234 int ret = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
235 if (ret == DB_NOTFOUND)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
236 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
237 pcursor->close();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
238 break;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
239 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
240 else if (ret != 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
241 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
242 pcursor->close();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
243 fSuccess = false;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
244 break;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
245 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
246 if (pszSkip &&
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
247 strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
248 continue;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
249 if (strncmp(&ssKey[0], "\x07version", 8) == 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
250 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
251 // Update version:
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
252 ssValue.clear();
1584
a67f13278e2c Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents: 1511
diff changeset
253 ssValue << CLIENT_VERSION;
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
254 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
255 Dbt datKey(&ssKey[0], ssKey.size());
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
256 Dbt datValue(&ssValue[0], ssValue.size());
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
257 int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
258 if (ret2 > 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
259 fSuccess = false;
1505
d54fc7452031 Fix crash-on-osx-on-shutdown bug. And cleanup CDB handling in Rewrite.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1504
diff changeset
260 }
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
261 if (fSuccess)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
262 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
263 db.Close();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
264 CloseDb(strFile);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
265 if (pdbCopy->close(0))
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
266 fSuccess = false;
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
267 delete pdbCopy;
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
268 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
269 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
270 if (fSuccess)
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
271 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
272 Db dbA(&dbenv, 0);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
273 if (dbA.remove(strFile.c_str(), NULL, 0))
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
274 fSuccess = false;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
275 Db dbB(&dbenv, 0);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
276 if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
277 fSuccess = false;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
278 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
279 if (!fSuccess)
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
280 printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
281 return fSuccess;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
282 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
283 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
284 Sleep(100);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
285 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
286 return false;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
287 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
288
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
289
1509
e1415e140a58 Only remove database log files on shutdown after wallet encryption/rewrite
Gavin Andresen <gavinandresen@gmail.com>
parents: 1506
diff changeset
290 void DBFlush(bool fShutdown)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
291 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
292 // Flush log data to the actual data file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
293 // on all files that are not in use
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
294 printf("DBFlush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
295 if (!fDbEnvInit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
296 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
297 {
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
298 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
299 map<string, int>::iterator mi = mapFileUseCount.begin();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
300 while (mi != mapFileUseCount.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
301 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
302 string strFile = (*mi).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
303 int nRefCount = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
304 printf("%s refcount=%d\n", strFile.c_str(), nRefCount);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
305 if (nRefCount == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
306 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
307 // Move log data to the dat file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
308 CloseDb(strFile);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
309 dbenv.txn_checkpoint(0, 0, 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
310 printf("%s flush\n", strFile.c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
311 dbenv.lsn_reset(strFile.c_str(), 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
312 mapFileUseCount.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
313 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
314 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
315 mi++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
316 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
317 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
318 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
319 char** listp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
320 if (mapFileUseCount.empty())
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
321 {
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
322 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
1509
e1415e140a58 Only remove database log files on shutdown after wallet encryption/rewrite
Gavin Andresen <gavinandresen@gmail.com>
parents: 1506
diff changeset
323 EnvShutdown();
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
324 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
325 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
326 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
327 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
328
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
329
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
330
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
331
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
332
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
333
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
334 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
335 // CTxDB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
336 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
337
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
338 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
339 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
340 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
341 txindex.SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
342 return Read(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
343 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
344
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
345 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
346 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
347 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
348 return Write(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
349 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
350
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
351 bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
352 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
353 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
354
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
355 // Add to tx index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
356 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
357 CTxIndex txindex(pos, tx.vout.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
358 return Write(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
359 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
360
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
361 bool CTxDB::EraseTxIndex(const CTransaction& tx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
362 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
363 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
364 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
365
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
366 return Erase(make_pair(string("tx"), hash));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
367 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
368
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
369 bool CTxDB::ContainsTx(uint256 hash)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
370 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
371 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
372 return Exists(make_pair(string("tx"), hash));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
373 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
374
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
375 bool CTxDB::ReadOwnerTxes(uint160 hash160, int nMinHeight, vector<CTransaction>& vtx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
376 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
377 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
378 vtx.clear();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
379
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
380 // Get cursor
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
381 Dbc* pcursor = GetCursor();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
382 if (!pcursor)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
383 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
384
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
385 unsigned int fFlags = DB_SET_RANGE;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
386 loop
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
387 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
388 // Read next record
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
389 CDataStream ssKey;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
390 if (fFlags == DB_SET_RANGE)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
391 ssKey << string("owner") << hash160 << CDiskTxPos(0, 0, 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
392 CDataStream ssValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
393 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
394 fFlags = DB_NEXT;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
395 if (ret == DB_NOTFOUND)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
396 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
397 else if (ret != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
398 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
399 pcursor->close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
400 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
401 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
402
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
403 // Unserialize
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
404 string strType;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
405 uint160 hashItem;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
406 CDiskTxPos pos;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
407 ssKey >> strType >> hashItem >> pos;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
408 int nItemHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
409 ssValue >> nItemHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
410
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
411 // Read transaction
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
412 if (strType != "owner" || hashItem != hash160)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
413 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
414 if (nItemHeight >= nMinHeight)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
415 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
416 vtx.resize(vtx.size()+1);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
417 if (!vtx.back().ReadFromDisk(pos))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
418 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
419 pcursor->close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
420 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
421 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
422 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
423 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
424
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
425 pcursor->close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
426 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
427 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
428
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
429 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
430 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
431 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
432 tx.SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
433 if (!ReadTxIndex(hash, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
434 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
435 return (tx.ReadFromDisk(txindex.pos));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
436 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
437
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
438 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
439 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
440 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
441 return ReadDiskTx(hash, tx, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
442 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
443
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
444 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
445 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
446 return ReadDiskTx(outpoint.hash, tx, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
447 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
448
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
449 bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
450 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
451 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
452 return ReadDiskTx(outpoint.hash, tx, txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
453 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
454
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
455 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
456 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
457 return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
458 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
459
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
460 bool CTxDB::EraseBlockIndex(uint256 hash)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
461 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
462 return Erase(make_pair(string("blockindex"), hash));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
463 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
464
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
465 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
466 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
467 return Read(string("hashBestChain"), hashBestChain);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
468 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
469
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
470 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
471 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
472 return Write(string("hashBestChain"), hashBestChain);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
473 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
474
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
475 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
476 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
477 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
478 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
479
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
480 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
481 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
482 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
483 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
484
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
485 CBlockIndex static * InsertBlockIndex(uint256 hash)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
486 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
487 if (hash == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
488 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
489
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
490 // Return existing
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
491 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
492 if (mi != mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
493 return (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
494
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
495 // Create new
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
496 CBlockIndex* pindexNew = new CBlockIndex();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
497 if (!pindexNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
498 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
499 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
500 pindexNew->phashBlock = &((*mi).first);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
501
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
502 return pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
503 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
504
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
505 bool CTxDB::LoadBlockIndex()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
506 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
507 // Get database cursor
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
508 Dbc* pcursor = GetCursor();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
509 if (!pcursor)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
510 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
511
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
512 // Load mapBlockIndex
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
513 unsigned int fFlags = DB_SET_RANGE;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
514 loop
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
515 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
516 // Read next record
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
517 CDataStream ssKey;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
518 if (fFlags == DB_SET_RANGE)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
519 ssKey << make_pair(string("blockindex"), uint256(0));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
520 CDataStream ssValue;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
521 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
522 fFlags = DB_NEXT;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
523 if (ret == DB_NOTFOUND)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
524 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
525 else if (ret != 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
526 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
527
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
528 // Unserialize
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
529 string strType;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
530 ssKey >> strType;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
531 if (strType == "blockindex")
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
532 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
533 CDiskBlockIndex diskindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
534 ssValue >> diskindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
535
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
536 // Construct block index object
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
537 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
538 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
539 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
540 pindexNew->nFile = diskindex.nFile;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
541 pindexNew->nBlockPos = diskindex.nBlockPos;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
542 pindexNew->nHeight = diskindex.nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
543 pindexNew->nVersion = diskindex.nVersion;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
544 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
545 pindexNew->nTime = diskindex.nTime;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
546 pindexNew->nBits = diskindex.nBits;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
547 pindexNew->nNonce = diskindex.nNonce;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
548
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
549 // Watch for genesis block
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
550 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
551 pindexGenesisBlock = pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
552
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
553 if (!pindexNew->CheckIndex())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
554 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
555 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
556 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
557 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
558 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
559 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
560 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
561 pcursor->close();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
562
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
563 // Calculate bnChainWork
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
564 vector<pair<int, CBlockIndex*> > vSortedByHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
565 vSortedByHeight.reserve(mapBlockIndex.size());
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
566 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
567 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
568 CBlockIndex* pindex = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
569 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
570 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
571 sort(vSortedByHeight.begin(), vSortedByHeight.end());
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
572 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
573 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
574 CBlockIndex* pindex = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
575 pindex->bnChainWork = (pindex->pprev ? pindex->pprev->bnChainWork : 0) + pindex->GetBlockWork();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
576 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
577
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
578 // Load hashBestChain pointer to end of best chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
579 if (!ReadHashBestChain(hashBestChain))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
580 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
581 if (pindexGenesisBlock == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
582 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
583 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
584 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
585 if (!mapBlockIndex.count(hashBestChain))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
586 return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
587 pindexBest = mapBlockIndex[hashBestChain];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
588 nBestHeight = pindexBest->nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
589 bnBestChainWork = pindexBest->bnChainWork;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
590 printf("LoadBlockIndex(): hashBestChain=%s height=%d\n", hashBestChain.ToString().substr(0,20).c_str(), nBestHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
591
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
592 // Load bnBestInvalidWork, OK if it doesn't exist
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
593 ReadBestInvalidWork(bnBestInvalidWork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
594
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
595 // Verify blocks in the best chain
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
596 int nCheckLevel = GetArg("-checklevel", 1);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
597 int nCheckDepth = GetArg( "-checkblocks", 2500);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
598 if (nCheckDepth == 0)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
599 nCheckDepth = 1000000000; // suffices until the year 19000
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
600 if (nCheckDepth > nBestHeight)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
601 nCheckDepth = nBestHeight;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
602 printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
603 CBlockIndex* pindexFork = NULL;
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
604 map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
605 for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
606 {
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
607 if (pindex->nHeight < nBestHeight-nCheckDepth)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
608 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
609 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
610 if (!block.ReadFromDisk(pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
611 return error("LoadBlockIndex() : block.ReadFromDisk failed");
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
612 // check level 1: verify block validity
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
613 if (nCheckLevel>0 && !block.CheckBlock())
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
614 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
615 printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
616 pindexFork = pindex->pprev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
617 }
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
618 // check level 2: verify transaction index validity
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
619 if (nCheckLevel>1)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
620 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
621 pair<unsigned int, unsigned int> pos = make_pair(pindex->nFile, pindex->nBlockPos);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
622 mapBlockPos[pos] = pindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
623 BOOST_FOREACH(const CTransaction &tx, block.vtx)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
624 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
625 uint256 hashTx = tx.GetHash();
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
626 CTxIndex txindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
627 if (ReadTxIndex(hashTx, txindex))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
628 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
629 // check level 3: checker transaction hashes
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
630 if (nCheckLevel>2 || pindex->nFile != txindex.pos.nFile || pindex->nBlockPos != txindex.pos.nBlockPos)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
631 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
632 // either an error or a duplicate transaction
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
633 CTransaction txFound;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
634 if (!txFound.ReadFromDisk(txindex.pos))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
635 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
636 printf("LoadBlockIndex() : *** cannot read mislocated transaction %s\n", hashTx.ToString().c_str());
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
637 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
638 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
639 else
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
640 if (txFound.GetHash() != hashTx) // not a duplicate tx
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
641 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
642 printf("LoadBlockIndex(): *** invalid tx position for %s\n", hashTx.ToString().c_str());
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
643 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
644 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
645 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
646 // check level 4: check whether spent txouts were spent within the main chain
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
647 int nOutput = 0;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
648 if (nCheckLevel>3)
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
649 {
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
650 BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
651 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
652 if (!txpos.IsNull())
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
653 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
654 pair<unsigned int, unsigned int> posFind = make_pair(txpos.nFile, txpos.nBlockPos);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
655 if (!mapBlockPos.count(posFind))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
656 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
657 printf("LoadBlockIndex(): *** found bad spend at %d, hashBlock=%s, hashTx=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str(), hashTx.ToString().c_str());
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
658 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
659 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
660 // check level 6: check whether spent txouts were spent by a valid transaction that consume them
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
661 if (nCheckLevel>5)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
662 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
663 CTransaction txSpend;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
664 if (!txSpend.ReadFromDisk(txpos))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
665 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
666 printf("LoadBlockIndex(): *** cannot read spending transaction of %s:%i from disk\n", hashTx.ToString().c_str(), nOutput);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
667 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
668 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
669 else if (!txSpend.CheckTransaction())
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
670 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
671 printf("LoadBlockIndex(): *** spending transaction of %s:%i is invalid\n", hashTx.ToString().c_str(), nOutput);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
672 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
673 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
674 else
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
675 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
676 bool fFound = false;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
677 BOOST_FOREACH(const CTxIn &txin, txSpend.vin)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
678 if (txin.prevout.hash == hashTx && txin.prevout.n == nOutput)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
679 fFound = true;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
680 if (!fFound)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
681 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
682 printf("LoadBlockIndex(): *** spending transaction of %s:%i does not spend it\n", hashTx.ToString().c_str(), nOutput);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
683 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
684 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
685 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
686 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
687 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
688 nOutput++;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
689 }
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
690 }
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
691 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
692 // check level 5: check whether all prevouts are marked spent
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
693 if (nCheckLevel>4)
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
694 {
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
695 BOOST_FOREACH(const CTxIn &txin, tx.vin)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
696 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
697 CTxIndex txindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
698 if (ReadTxIndex(txin.prevout.hash, txindex))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
699 if (txindex.vSpent.size()-1 < txin.prevout.n || txindex.vSpent[txin.prevout.n].IsNull())
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
700 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
701 printf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString().c_str(), txin.prevout.n, hashTx.ToString().c_str());
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
702 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
703 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
704 }
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
705 }
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
706 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
707 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
708 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
709 if (pindexFork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
710 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
711 // Reorg back to the fork
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
712 printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
713 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
714 if (!block.ReadFromDisk(pindexFork))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
715 return error("LoadBlockIndex() : block.ReadFromDisk failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
716 CTxDB txdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
717 block.SetBestChain(txdb, pindexFork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
718 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
719
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
720 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
721 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
722
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
723
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
724
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
725
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
726
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
727 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
728 // CAddrDB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
729 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
730
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
731 bool CAddrDB::WriteAddrman(const CAddrMan& addrman)
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
732 {
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
733 return Write(string("addrman"), addrman);
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
734 }
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
735
2097
3811124fc664 Use erase instead of rewrite to remove old addr entries
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2084
diff changeset
736 bool CAddrDB::LoadAddresses()
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
737 {
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
738 if (Read(string("addrman"), addrman))
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
739 {
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
740 printf("Loaded %i addresses\n", addrman.size());
2117
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
741 return true;
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
742 }
2117
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
743
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
744 // Read pre-0.6 addr records
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
745
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
746 vector<CAddress> vAddr;
2097
3811124fc664 Use erase instead of rewrite to remove old addr entries
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2084
diff changeset
747 vector<vector<unsigned char> > vDelete;
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
748
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
749 // Get cursor
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
750 Dbc* pcursor = GetCursor();
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
751 if (!pcursor)
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
752 return false;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
753
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
754 loop
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
755 {
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
756 // Read next record
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
757 CDataStream ssKey;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
758 CDataStream ssValue;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
759 int ret = ReadAtCursor(pcursor, ssKey, ssValue);
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
760 if (ret == DB_NOTFOUND)
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
761 break;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
762 else if (ret != 0)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
763 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
764
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
765 // Unserialize
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
766 string strType;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
767 ssKey >> strType;
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
768 if (strType == "addr")
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
769 {
2117
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
770 CAddress addr;
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
771 ssValue >> addr;
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
772 vAddr.push_back(addr);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
773 }
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
774 }
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
775 pcursor->close();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
776
2117
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
777 addrman.Add(vAddr, CNetAddr("0.0.0.0"));
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
778 printf("Loaded %i addresses\n", addrman.size());
2097
3811124fc664 Use erase instead of rewrite to remove old addr entries
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2084
diff changeset
779
2117
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
780 // Note: old records left; we ran into hangs-on-startup
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
781 // bugs for some users who (we think) were running after
84a09265db06 Workaround hangs when upgrading old addr.dat files
Gavin Andresen <gavinandresen@gmail.com>
parents: 2114
diff changeset
782 // an unclean shutdown.
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
783
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
784 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
785 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
786
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
787 bool LoadAddresses()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
788 {
2097
3811124fc664 Use erase instead of rewrite to remove old addr entries
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2084
diff changeset
789 return CAddrDB("cr+").LoadAddresses();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
790 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
791
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
792