annotate src/db.cpp @ 2287:ccef6e0f4b0a draft

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