annotate src/db.cpp @ 3603:a3ef3e3e0ee2 draft

Handle incompatible BDB environments Before, opening a -datadir that was created with a new version of Berkeley DB would result in an un-caught DB_RUNRECOVERY exception. After these changes, the error is caught and the user is told that there is a problem and is told how to try to recover from it.
author Gavin Andresen <gavinandresen@gmail.com>
date Mon, 08 Oct 2012 15:18:04 -0400
parents ea8bd30bedcb
children fbf468eac50a
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
2607
98dbb2aa4f5a Update License in File Headers
Fordy <fanquake@gmail.com>
parents: 2598
diff changeset
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
575
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
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
29 CDBEnv bitdb;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
30
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
31 void CDBEnv::EnvShutdown()
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
32 {
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
33 if (!fDbEnvInit)
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
34 return;
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
35
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
36 fDbEnvInit = false;
3603
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
37 int ret = dbenv.close(0);
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
38 if (ret != 0)
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
39 printf("EnvShutdown exception: %s (%d)\n", DbEnv::strerror(ret), ret);
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
40 if (!fMockDb)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
41 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
42 }
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
43
3603
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
44 CDBEnv::CDBEnv() : dbenv(DB_CXX_NO_EXCEPTIONS)
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
45 {
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
46 }
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
47
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
48 CDBEnv::~CDBEnv()
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
49 {
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
50 EnvShutdown();
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
51 }
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
52
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
53 void CDBEnv::Close()
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
54 {
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
55 EnvShutdown();
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
56 }
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
57
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
58 bool CDBEnv::Open(boost::filesystem::path pathEnv_)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
59 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
60 if (fDbEnvInit)
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
61 return true;
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
62
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
63 if (fShutdown)
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
64 return false;
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
65
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
66 pathEnv = pathEnv_;
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
67 filesystem::path pathDataDir = pathEnv;
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
68 filesystem::path pathLogDir = pathDataDir / "database";
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
69 filesystem::create_directory(pathLogDir);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
70 filesystem::path pathErrorFile = pathDataDir / "db.log";
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
71 printf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str());
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
72
2669
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
73 unsigned int nEnvFlags = 0;
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
74 if (GetBoolArg("-privdb", true))
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
75 nEnvFlags |= DB_PRIVATE;
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
76
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
77 int nDbCache = GetArg("-dbcache", 25);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
78 dbenv.set_lg_dir(pathLogDir.string().c_str());
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
79 dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
80 dbenv.set_lg_bsize(1048576);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
81 dbenv.set_lg_max(10485760);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
82 dbenv.set_lk_max_locks(10000);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
83 dbenv.set_lk_max_objects(10000);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
84 dbenv.set_errfile(fopen(pathErrorFile.string().c_str(), "a")); /// debug
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
85 dbenv.set_flags(DB_AUTO_COMMIT, 1);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
86 dbenv.set_flags(DB_TXN_WRITE_NOSYNC, 1);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
87 dbenv.log_set_config(DB_LOG_AUTO_REMOVE, 1);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
88 int ret = dbenv.open(pathDataDir.string().c_str(),
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
89 DB_CREATE |
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
90 DB_INIT_LOCK |
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
91 DB_INIT_LOG |
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
92 DB_INIT_MPOOL |
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
93 DB_INIT_TXN |
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
94 DB_THREAD |
2669
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
95 DB_RECOVER |
967a37af0b50 Merge branch 'dbenv' into tmp
Jeff Garzik <jeff@garzik.org>
parents: 2664 2635
diff changeset
96 nEnvFlags,
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
97 S_IRUSR | S_IWUSR);
3603
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
98 if (ret != 0)
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
99 return error("CDB() : error %s (%d) opening database environment", DbEnv::strerror(ret), ret);
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
100
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
101 fDbEnvInit = true;
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
102 fMockDb = false;
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
103 return true;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
104 }
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
105
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
106 void CDBEnv::MakeMock()
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
107 {
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
108 if (fDbEnvInit)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
109 throw runtime_error("CDBEnv::MakeMock(): already initialized");
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
110
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
111 if (fShutdown)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
112 throw runtime_error("CDBEnv::MakeMock(): during shutdown");
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
113
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
114 printf("CDBEnv::MakeMock()\n");
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
115
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
116 dbenv.set_cachesize(1, 0, 1);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
117 dbenv.set_lg_bsize(10485760*4);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
118 dbenv.set_lg_max(10485760);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
119 dbenv.set_lk_max_locks(10000);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
120 dbenv.set_lk_max_objects(10000);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
121 dbenv.set_flags(DB_AUTO_COMMIT, 1);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
122 dbenv.log_set_config(DB_LOG_IN_MEMORY, 1);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
123 int ret = dbenv.open(NULL,
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
124 DB_CREATE |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
125 DB_INIT_LOCK |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
126 DB_INIT_LOG |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
127 DB_INIT_MPOOL |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
128 DB_INIT_TXN |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
129 DB_THREAD |
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
130 DB_PRIVATE,
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
131 S_IRUSR | S_IWUSR);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
132 if (ret > 0)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
133 throw runtime_error(strprintf("CDBEnv::MakeMock(): error %d opening database environment", ret));
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
134
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
135 fDbEnvInit = true;
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
136 fMockDb = true;
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
137 }
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
138
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
139 void CDBEnv::CheckpointLSN(std::string strFile)
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
140 {
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
141 dbenv.txn_checkpoint(0, 0, 0);
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
142 if (fMockDb)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
143 return;
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
144 dbenv.lsn_reset(strFile.c_str(), 0);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
145 }
575
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
2634
a558beaa5c57 CDB::CDB: properly initialize activeTxn to NULL
Jeff Garzik <jgarzik@exmulti.com>
parents: 2633
diff changeset
148 CDB::CDB(const char *pszFile, const char* pszMode) :
a558beaa5c57 CDB::CDB: properly initialize activeTxn to NULL
Jeff Garzik <jgarzik@exmulti.com>
parents: 2633
diff changeset
149 pdb(NULL), activeTxn(NULL)
575
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 int ret;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
152 if (pszFile == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
153 return;
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 fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
156 bool fCreate = strchr(pszMode, 'c');
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
157 unsigned int nFlags = DB_THREAD;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
158 if (fCreate)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
159 nFlags |= DB_CREATE;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
160
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
161 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
162 LOCK(bitdb.cs_db);
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
163 if (!bitdb.Open(GetDataDir()))
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
164 throw runtime_error("env open failed");
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
165
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
166 strFile = pszFile;
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
167 ++bitdb.mapFileUseCount[strFile];
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
168 pdb = bitdb.mapDb[strFile];
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
169 if (pdb == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
170 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
171 pdb = new Db(&bitdb.dbenv, 0);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
172
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
173 bool fMockDb = bitdb.IsMock();
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
174 if (fMockDb)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
175 {
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
176 DbMpoolFile*mpf = pdb->get_mpf();
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
177 ret = mpf->set_flags(DB_MPOOL_NOFILE, 1);
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
178 if (ret != 0)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
179 throw runtime_error(strprintf("CDB() : failed to configure for no temp file backing for database %s", pszFile));
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
180 }
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
181
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
182 ret = pdb->open(NULL, // Txn pointer
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
183 fMockDb ? NULL : pszFile, // Filename
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
184 "main", // Logical db name
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
185 DB_BTREE, // Database type
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
186 nFlags, // Flags
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
187 0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
188
3603
a3ef3e3e0ee2 Handle incompatible BDB environments
Gavin Andresen <gavinandresen@gmail.com>
parents: 3516
diff changeset
189 if (ret != 0)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
190 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
191 delete pdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
192 pdb = NULL;
3391
3bb9018628ee Removed duplicated lock
xanatos <xanatos@geocities.com>
parents: 3194
diff changeset
193 --bitdb.mapFileUseCount[strFile];
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
194 strFile = "";
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
195 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
196 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
197
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
198 if (fCreate && !Exists(string("version")))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
199 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
200 bool fTmp = fReadOnly;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
201 fReadOnly = false;
1584
a67f13278e2c Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents: 1511
diff changeset
202 WriteVersion(CLIENT_VERSION);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
203 fReadOnly = fTmp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
204 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
205
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
206 bitdb.mapDb[strFile] = pdb;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
207 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
208 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
209 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
210
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
211 static bool IsChainFile(std::string strFile)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
212 {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
213 if (strFile == "blkindex.dat")
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
214 return true;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
215
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
216 return false;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
217 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
218
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
219 void CDB::Close()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
220 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
221 if (!pdb)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
222 return;
2633
a7a1bf716c1a Remove unused nested BDB transaction support
Jeff Garzik <jgarzik@exmulti.com>
parents: 2631
diff changeset
223 if (activeTxn)
a7a1bf716c1a Remove unused nested BDB transaction support
Jeff Garzik <jgarzik@exmulti.com>
parents: 2631
diff changeset
224 activeTxn->abort();
a7a1bf716c1a Remove unused nested BDB transaction support
Jeff Garzik <jgarzik@exmulti.com>
parents: 2631
diff changeset
225 activeTxn = NULL;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
226 pdb = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
227
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
228 // 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
229 unsigned int nMinutes = 0;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
230 if (fReadOnly)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
231 nMinutes = 1;
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
232 if (IsChainFile(strFile))
2263
c8bc276d36eb Reduce sync frequency for blkindex.dat
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2246
diff changeset
233 nMinutes = 2;
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
234 if (IsChainFile(strFile) && IsInitialBlockDownload())
2116
94844608a7d7 Use transactions-updated as flush condition
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2114
diff changeset
235 nMinutes = 5;
94844608a7d7 Use transactions-updated as flush condition
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2114
diff changeset
236
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
237 bitdb.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
238
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
239 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
240 LOCK(bitdb.cs_db);
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
241 --bitdb.mapFileUseCount[strFile];
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
242 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
243 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
244
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
245 void CDBEnv::CloseDb(const string& strFile)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
246 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
247 {
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
248 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
249 if (mapDb[strFile] != NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
250 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
251 // Close the database handle
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
252 Db* pdb = mapDb[strFile];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
253 pdb->close(0);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
254 delete pdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
255 mapDb[strFile] = NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
256 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
257 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
258 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
259
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
260 bool CDB::Rewrite(const string& strFile, const char* pszSkip)
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
261 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
262 while (!fShutdown)
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
263 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
264 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
265 LOCK(bitdb.cs_db);
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
266 if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0)
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
267 {
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
268 // Flush log data to the dat file
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
269 bitdb.CloseDb(strFile);
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
270 bitdb.CheckpointLSN(strFile);
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
271 bitdb.mapFileUseCount.erase(strFile);
1500
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 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
274 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
275 string strFileRes = strFile + ".rewrite";
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
276 { // surround usage of db with extra {}
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
277 CDB db(strFile.c_str(), "r");
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
278 Db* pdbCopy = new Db(&bitdb.dbenv, 0);
3516
ea8bd30bedcb Trim trailing whitespace for src/*.{h,cpp}
Jeff Garzik <jgarzik@exmulti.com>
parents: 3391
diff changeset
279
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
280 int ret = pdbCopy->open(NULL, // Txn pointer
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
281 strFileRes.c_str(), // Filename
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
282 "main", // Logical db name
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
283 DB_BTREE, // Database type
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
284 DB_CREATE, // Flags
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
285 0);
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
286 if (ret > 0)
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
287 {
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
288 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
289 fSuccess = false;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
290 }
3516
ea8bd30bedcb Trim trailing whitespace for src/*.{h,cpp}
Jeff Garzik <jgarzik@exmulti.com>
parents: 3391
diff changeset
291
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
292 Dbc* pcursor = db.GetCursor();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
293 if (pcursor)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
294 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
295 {
2277
0dcf927bc6b2 Further reduce header dependencies
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2275
diff changeset
296 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
0dcf927bc6b2 Further reduce header dependencies
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2275
diff changeset
297 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
298 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
299 if (ret == DB_NOTFOUND)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
300 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
301 pcursor->close();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
302 break;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
303 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
304 else if (ret != 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
305 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
306 pcursor->close();
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
307 fSuccess = false;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
308 break;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
309 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
310 if (pszSkip &&
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
311 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
312 continue;
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
313 if (strncmp(&ssKey[0], "\x07version", 8) == 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
314 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
315 // Update version:
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
316 ssValue.clear();
1584
a67f13278e2c Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents: 1511
diff changeset
317 ssValue << CLIENT_VERSION;
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
318 }
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
319 Dbt datKey(&ssKey[0], ssKey.size());
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
320 Dbt datValue(&ssValue[0], ssValue.size());
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
321 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
322 if (ret2 > 0)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
323 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
324 }
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
325 if (fSuccess)
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
326 {
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
327 db.Close();
2635
c8269e1811c5 Further CDBEnv encapsulation work.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2634
diff changeset
328 bitdb.CloseDb(strFile);
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
329 if (pdbCopy->close(0))
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
330 fSuccess = false;
1511
bdff5864eff3 close old db when rewriting
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1510
diff changeset
331 delete pdbCopy;
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
332 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
333 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
334 if (fSuccess)
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
335 {
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
336 Db dbA(&bitdb.dbenv, 0);
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
337 if (dbA.remove(strFile.c_str(), NULL, 0))
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
338 fSuccess = false;
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
339 Db dbB(&bitdb.dbenv, 0);
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
340 if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
341 fSuccess = false;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
342 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
343 if (!fSuccess)
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
344 printf("Rewriting of %s FAILED!\n", strFileRes.c_str());
1500
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
345 return fSuccess;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
346 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
347 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
348 Sleep(100);
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
349 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
350 return false;
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
351 }
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
352
7d93eeefa5ea Resilvering
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1390
diff changeset
353
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
354 void CDBEnv::Flush(bool fShutdown)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
355 {
2598
d1152dd63333 Report how long DBFlush took.
R E Broadley <rebroad+github@gmail.com>
parents: 2581
diff changeset
356 int64 nStart = GetTimeMillis();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
357 // Flush log data to the actual data file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
358 // on all files that are not in use
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
359 printf("Flush(%s)%s\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started");
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
360 if (!fDbEnvInit)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
361 return;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
362 {
2190
60fe81a4849b Use scoped locks instead of CRITICAL_BLOCK
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2176
diff changeset
363 LOCK(cs_db);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
364 map<string, int>::iterator mi = mapFileUseCount.begin();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
365 while (mi != mapFileUseCount.end())
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 string strFile = (*mi).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
368 int nRefCount = (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
369 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
370 if (nRefCount == 0)
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 // Move log data to the dat file
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
373 CloseDb(strFile);
2317
2d4c7d77f691 Make lsn_reset ("detach databases") optional and off by default.
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2313
diff changeset
374 printf("%s checkpoint\n", strFile.c_str());
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
375 dbenv.txn_checkpoint(0, 0, 0);
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
376 if (!IsChainFile(strFile) || fDetachDB) {
2317
2d4c7d77f691 Make lsn_reset ("detach databases") optional and off by default.
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2313
diff changeset
377 printf("%s detach\n", strFile.c_str());
3194
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
378 if (!fMockDb)
bba71ed03345 Run BDB disk-less for test_bitcoin
Luke Dashjr <luke-jr+git@utopios.org>
parents: 3159
diff changeset
379 dbenv.lsn_reset(strFile.c_str(), 0);
2317
2d4c7d77f691 Make lsn_reset ("detach databases") optional and off by default.
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2313
diff changeset
380 }
2d4c7d77f691 Make lsn_reset ("detach databases") optional and off by default.
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2313
diff changeset
381 printf("%s closed\n", strFile.c_str());
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
382 mapFileUseCount.erase(mi++);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
383 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
384 else
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
385 mi++;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
386 }
2598
d1152dd63333 Report how long DBFlush took.
R E Broadley <rebroad+github@gmail.com>
parents: 2581
diff changeset
387 printf("DBFlush(%s)%s ended %15"PRI64d"ms\n", fShutdown ? "true" : "false", fDbEnvInit ? "" : " db not started", GetTimeMillis() - nStart);
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
388 if (fShutdown)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
389 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
390 char** listp;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
391 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
392 {
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
393 dbenv.log_archive(&listp, DB_ARCH_REMOVE);
2631
04775a4029eb Encapsulate BDB environment inside new CDBEnv class
Jeff Garzik <jgarzik@exmulti.com>
parents: 2623
diff changeset
394 Close();
1501
26d0f350347a Obsolete keypool and make sure database removes log files on shutdown.
Gavin Andresen <gavinandresen@gmail.com>
parents: 1500
diff changeset
395 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
396 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
397 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
398 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
399
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
400
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
401
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
402
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
403
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
404
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
405 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
406 // CTxDB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
407 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
408
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
409 bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
410 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
411 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
412 txindex.SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
413 return Read(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
414 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
415
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
416 bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
417 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
418 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
419 return Write(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
420 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
421
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
422 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
423 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
424 assert(!fClient);
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 // Add to tx index
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
427 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
428 CTxIndex txindex(pos, tx.vout.size());
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
429 return Write(make_pair(string("tx"), hash), txindex);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
430 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
431
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
432 bool CTxDB::EraseTxIndex(const CTransaction& tx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
433 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
434 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
435 uint256 hash = tx.GetHash();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
436
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
437 return Erase(make_pair(string("tx"), hash));
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
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
440 bool CTxDB::ContainsTx(uint256 hash)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
441 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
442 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
443 return Exists(make_pair(string("tx"), hash));
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
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
446 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
447 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
448 assert(!fClient);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
449 tx.SetNull();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
450 if (!ReadTxIndex(hash, txindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
451 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
452 return (tx.ReadFromDisk(txindex.pos));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
453 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
454
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
455 bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
456 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
457 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
458 return ReadDiskTx(hash, tx, txindex);
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::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
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 ReadDiskTx(outpoint.hash, tx, txindex);
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::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
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 CTxIndex txindex;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
469 return ReadDiskTx(outpoint.hash, tx, txindex);
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
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
472 bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
473 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
474 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
475 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
476
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
477 bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
478 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
479 return Read(string("hashBestChain"), hashBestChain);
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
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
482 bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
483 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
484 return Write(string("hashBestChain"), hashBestChain);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
485 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
486
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
487 bool CTxDB::ReadBestInvalidWork(CBigNum& bnBestInvalidWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
488 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
489 return Read(string("bnBestInvalidWork"), bnBestInvalidWork);
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
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
492 bool CTxDB::WriteBestInvalidWork(CBigNum bnBestInvalidWork)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
493 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
494 return Write(string("bnBestInvalidWork"), bnBestInvalidWork);
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
690
389b559a397f CWallet class
Pieter Wuille <pieter.wuille@gmail.com>
parents: 689
diff changeset
497 CBlockIndex static * InsertBlockIndex(uint256 hash)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
498 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
499 if (hash == 0)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
500 return NULL;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
501
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
502 // Return existing
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
503 map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
504 if (mi != mapBlockIndex.end())
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
505 return (*mi).second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
506
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
507 // Create new
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
508 CBlockIndex* pindexNew = new CBlockIndex();
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
509 if (!pindexNew)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
510 throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
511 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
512 pindexNew->phashBlock = &((*mi).first);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
513
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
514 return pindexNew;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
515 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
516
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
517 bool CTxDB::LoadBlockIndex()
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
518 {
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
519 if (!LoadBlockIndexGuts())
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
520 return false;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
521
2287
ccef6e0f4b0a Added ability to respond to signals during Block Loading stage.
R E Broadley <rebroad+github@gmail.com>
parents: 2282
diff changeset
522 if (fRequestShutdown)
ccef6e0f4b0a Added ability to respond to signals during Block Loading stage.
R E Broadley <rebroad+github@gmail.com>
parents: 2282
diff changeset
523 return true;
ccef6e0f4b0a Added ability to respond to signals during Block Loading stage.
R E Broadley <rebroad+github@gmail.com>
parents: 2282
diff changeset
524
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
525 // Calculate bnChainWork
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
526 vector<pair<int, CBlockIndex*> > vSortedByHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
527 vSortedByHeight.reserve(mapBlockIndex.size());
595
e630bbb11629 make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 576
diff changeset
528 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
529 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
530 CBlockIndex* pindex = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
531 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
532 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
533 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
534 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
535 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
536 CBlockIndex* pindex = item.second;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
537 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
538 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
539
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
540 // Load hashBestChain pointer to end of best chain
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
541 if (!ReadHashBestChain(hashBestChain))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
542 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
543 if (pindexGenesisBlock == NULL)
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
544 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
545 return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
546 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
547 if (!mapBlockIndex.count(hashBestChain))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
548 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
549 pindexBest = mapBlockIndex[hashBestChain];
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
550 nBestHeight = pindexBest->nHeight;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
551 bnBestChainWork = pindexBest->bnChainWork;
2702
874987f5df7b Show the timestamp for the block.
R E Broadley <rebroad+github@gmail.com>
parents: 2680
diff changeset
552 printf("LoadBlockIndex(): hashBestChain=%s height=%d date=%s\n",
874987f5df7b Show the timestamp for the block.
R E Broadley <rebroad+github@gmail.com>
parents: 2680
diff changeset
553 hashBestChain.ToString().substr(0,20).c_str(), nBestHeight,
874987f5df7b Show the timestamp for the block.
R E Broadley <rebroad+github@gmail.com>
parents: 2680
diff changeset
554 DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
555
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
556 // Load bnBestInvalidWork, OK if it doesn't exist
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
557 ReadBestInvalidWork(bnBestInvalidWork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
558
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
559 // Verify blocks in the best chain
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
560 int nCheckLevel = GetArg("-checklevel", 1);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
561 int nCheckDepth = GetArg( "-checkblocks", 2500);
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
562 if (nCheckDepth == 0)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
563 nCheckDepth = 1000000000; // suffices until the year 19000
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
564 if (nCheckDepth > nBestHeight)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
565 nCheckDepth = nBestHeight;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
566 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
567 CBlockIndex* pindexFork = NULL;
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
568 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
569 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
570 {
2304
2c9b9d05a987 -loadblock to load from an external blk000?.dat file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2287
diff changeset
571 if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
572 break;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
573 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
574 if (!block.ReadFromDisk(pindex))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
575 return error("LoadBlockIndex() : block.ReadFromDisk failed");
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
576 // check level 1: verify block validity
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
577 if (nCheckLevel>0 && !block.CheckBlock())
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
578 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
579 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
580 pindexFork = pindex->pprev;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
581 }
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
582 // check level 2: verify transaction index validity
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
583 if (nCheckLevel>1)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
584 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
585 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
586 mapBlockPos[pos] = pindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
587 BOOST_FOREACH(const CTransaction &tx, block.vtx)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
588 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
589 uint256 hashTx = tx.GetHash();
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
590 CTxIndex txindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
591 if (ReadTxIndex(hashTx, txindex))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
592 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
593 // check level 3: checker transaction hashes
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
594 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
595 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
596 // either an error or a duplicate transaction
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
597 CTransaction txFound;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
598 if (!txFound.ReadFromDisk(txindex.pos))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
599 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
600 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
601 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
602 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
603 else
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
604 if (txFound.GetHash() != hashTx) // not a duplicate tx
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
605 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
606 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
607 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
608 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
609 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
610 // check level 4: check whether spent txouts were spent within the main chain
2313
3e48c4d044d5 Change signed->unsigned at 3 code sites
Jeff Garzik <jgarzik@exmulti.com>
parents: 2287
diff changeset
611 unsigned int nOutput = 0;
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
612 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
613 {
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
614 BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
615 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
616 if (!txpos.IsNull())
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
617 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
618 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
619 if (!mapBlockPos.count(posFind))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
620 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
621 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
622 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
623 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
624 // 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
625 if (nCheckLevel>5)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
626 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
627 CTransaction txSpend;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
628 if (!txSpend.ReadFromDisk(txpos))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
629 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
630 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
631 pindexFork = pindex->pprev;
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 else if (!txSpend.CheckTransaction())
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
634 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
635 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
636 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
637 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
638 else
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 bool fFound = false;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
641 BOOST_FOREACH(const CTxIn &txin, txSpend.vin)
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
642 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
643 fFound = true;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
644 if (!fFound)
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(): *** 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
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 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
651 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
652 nOutput++;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
653 }
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
654 }
2076
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 // 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
657 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
658 {
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
659 BOOST_FOREACH(const CTxIn &txin, tx.vin)
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 CTxIndex txindex;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
662 if (ReadTxIndex(txin.prevout.hash, txindex))
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
663 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
664 {
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
665 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
666 pindexFork = pindex->pprev;
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
667 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
668 }
2246
b26677f778d1 fix warnings: suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]
Wladimir J. van der Laan <laanwj@gmail.com>
parents: 2207
diff changeset
669 }
2076
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
670 }
17f721892510 Add -checklevel and improve -checkblocks
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2033
diff changeset
671 }
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
672 }
2304
2c9b9d05a987 -loadblock to load from an external blk000?.dat file
Pieter Wuille <pieter.wuille@gmail.com>
parents: 2287
diff changeset
673 if (pindexFork && !fRequestShutdown)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
674 {
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
675 // Reorg back to the fork
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
676 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
677 CBlock block;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
678 if (!block.ReadFromDisk(pindexFork))
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
679 return error("LoadBlockIndex() : block.ReadFromDisk failed");
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
680 CTxDB txdb;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
681 block.SetBestChain(txdb, pindexFork);
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
682 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
683
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
684 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
685 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
686
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
687
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
688
2680
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
689 bool CTxDB::LoadBlockIndexGuts()
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
690 {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
691 // Get database cursor
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
692 Dbc* pcursor = GetCursor();
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
693 if (!pcursor)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
694 return false;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
695
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
696 // Load mapBlockIndex
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
697 unsigned int fFlags = DB_SET_RANGE;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
698 loop
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
699 {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
700 // Read next record
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
701 CDataStream ssKey(SER_DISK, CLIENT_VERSION);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
702 if (fFlags == DB_SET_RANGE)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
703 ssKey << make_pair(string("blockindex"), uint256(0));
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
704 CDataStream ssValue(SER_DISK, CLIENT_VERSION);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
705 int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
706 fFlags = DB_NEXT;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
707 if (ret == DB_NOTFOUND)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
708 break;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
709 else if (ret != 0)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
710 return false;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
711
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
712 // Unserialize
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
713
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
714 try {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
715 string strType;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
716 ssKey >> strType;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
717 if (strType == "blockindex" && !fRequestShutdown)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
718 {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
719 CDiskBlockIndex diskindex;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
720 ssValue >> diskindex;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
721
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
722 // Construct block index object
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
723 CBlockIndex* pindexNew = InsertBlockIndex(diskindex.GetBlockHash());
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
724 pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
725 pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
726 pindexNew->nFile = diskindex.nFile;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
727 pindexNew->nBlockPos = diskindex.nBlockPos;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
728 pindexNew->nHeight = diskindex.nHeight;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
729 pindexNew->nVersion = diskindex.nVersion;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
730 pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
731 pindexNew->nTime = diskindex.nTime;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
732 pindexNew->nBits = diskindex.nBits;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
733 pindexNew->nNonce = diskindex.nNonce;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
734
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
735 // Watch for genesis block
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
736 if (pindexGenesisBlock == NULL && diskindex.GetBlockHash() == hashGenesisBlock)
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
737 pindexGenesisBlock = pindexNew;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
738
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
739 if (!pindexNew->CheckIndex())
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
740 return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
741 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
742 else
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
743 {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
744 break; // if shutdown requested or finished loading block index
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
745 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
746 } // try
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
747 catch (std::exception &e) {
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
748 return error("%s() : deserialize error", __PRETTY_FUNCTION__);
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
749 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
750 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
751 pcursor->close();
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
752
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
753 return true;
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
754 }
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
755
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
756
6f9e2902c50d Refactor: add IsChainFile helper. LoadBlockIndex() code movement.
Jeff Garzik <jgarzik@exmulti.com>
parents: 2669
diff changeset
757
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
758
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
759
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
760 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
761 // CAddrDB
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
762 //
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
763
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
764
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
765 CAddrDB::CAddrDB()
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
766 {
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
767 pathAddr = GetDataDir() / "peers.dat";
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
768 }
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
769
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
770 bool CAddrDB::Write(const CAddrMan& addr)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
771 {
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
772 // Generate random temporary filename
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
773 unsigned short randv = 0;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
774 RAND_bytes((unsigned char *)&randv, sizeof(randv));
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
775 std::string tmpfn = strprintf("peers.dat.%04x", randv);
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
776
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
777 // serialize addresses, checksum data up to that point, then append csum
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
778 CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
779 ssPeers << FLATDATA(pchMessageStart);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
780 ssPeers << addr;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
781 uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
782 ssPeers << hash;
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
783
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
784 // open temp output file, and associate with CAutoFile
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
785 boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
786 FILE *file = fopen(pathTmp.string().c_str(), "wb");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
787 CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
788 if (!fileout)
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
789 return error("CAddrman::Write() : open failed");
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
790
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
791 // Write and commit header, data
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
792 try {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
793 fileout << ssPeers;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
794 }
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
795 catch (std::exception &e) {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
796 return error("CAddrman::Write() : I/O error");
1911
a9190d020909 CAddrMan: stochastic address manager
Pieter Wuille <pieter.wuille@gmail.com>
parents: 1873
diff changeset
797 }
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
798 FileCommit(fileout);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
799 fileout.fclose();
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
800
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
801 // replace existing peers.dat, if any, with new peers.dat.XXXX
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
802 if (!RenameOver(pathTmp, pathAddr))
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
803 return error("CAddrman::Write() : Rename-into-place failed");
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
804
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
805 return true;
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
806 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
807
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
808 bool CAddrDB::Read(CAddrMan& addr)
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
809 {
2578
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
810 // open input file, and associate with CAutoFile
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
811 FILE *file = fopen(pathAddr.string().c_str(), "rb");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
812 CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
813 if (!filein)
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
814 return error("CAddrman::Read() : open failed");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
815
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
816 // use file size to size memory buffer
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
817 int fileSize = GetFilesize(filein);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
818 int dataSize = fileSize - sizeof(uint256);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
819 vector<unsigned char> vchData;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
820 vchData.resize(dataSize);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
821 uint256 hashIn;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
822
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
823 // read data and checksum from file
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
824 try {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
825 filein.read((char *)&vchData[0], dataSize);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
826 filein >> hashIn;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
827 }
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
828 catch (std::exception &e) {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
829 return error("CAddrman::Read() 2 : I/O error or stream data corrupted");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
830 }
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
831 filein.fclose();
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
832
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
833 CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
834
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
835 // verify stored checksum matches input data
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
836 uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end());
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
837 if (hashIn != hashTmp)
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
838 return error("CAddrman::Read() : checksum mismatch; data corrupted");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
839
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
840 // de-serialize address data
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
841 unsigned char pchMsgTmp[4];
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
842 try {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
843 ssPeers >> FLATDATA(pchMsgTmp);
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
844 ssPeers >> addr;
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
845 }
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
846 catch (std::exception &e) {
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
847 return error("CAddrman::Read() : I/O error or stream data corrupted");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
848 }
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
849
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
850 // finally, verify the network matches ours
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
851 if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
852 return error("CAddrman::Read() : invalid network magic number");
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
853
038e24060817 CAddrDB: Replace BDB-managed addr.dat with internally managed peers.dat
Jeff Garzik <jgarzik@exmulti.com>
parents: 2404
diff changeset
854 return true;
575
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
855 }
1a0476fe825f directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff changeset
856