txdb-leveldb.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // ECOin - Copyright (c) - 2014/2024 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include <map>
  3. #include <boost/version.hpp>
  4. #include <boost/filesystem.hpp>
  5. #include <boost/filesystem/fstream.hpp>
  6. #include <leveldb/env.h>
  7. #include <leveldb/cache.h>
  8. #include <leveldb/filter_policy.h>
  9. #include <memenv/memenv.h>
  10. #include "kernel.h"
  11. #include "checkpoints.h"
  12. #include "txdb.h"
  13. #include "util.h"
  14. #include "main.h"
  15. using namespace std;
  16. using namespace boost;
  17. leveldb::DB *txdb; // global pointer for LevelDB object instance
  18. static leveldb::Options GetOptions() {
  19. leveldb::Options options;
  20. int nCacheSizeMB = GetArg("-dbcache", 25);
  21. options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
  22. options.filter_policy = leveldb::NewBloomFilterPolicy(10);
  23. return options;
  24. }
  25. void init_blockindex(leveldb::Options& options, bool fRemoveOld = false) {
  26. // First time init.
  27. boost::filesystem::path directory = GetDataDir() / "txleveldb";
  28. if (fRemoveOld) {
  29. boost::filesystem::remove_all(directory); // remove directory
  30. unsigned int nFile = 1;
  31. while (true)
  32. {
  33. boost::filesystem::path strBlockFile = GetDataDir() / strprintf("blk%04u.dat", nFile);
  34. // Break if no such file
  35. if( !boost::filesystem::exists( strBlockFile ) )
  36. break;
  37. boost::filesystem::remove(strBlockFile);
  38. nFile++;
  39. }
  40. }
  41. boost::filesystem::create_directory(directory);
  42. printf("Opening LevelDB in %s\n", directory.string().c_str());
  43. leveldb::Status status = leveldb::DB::Open(options, directory.string(), &txdb);
  44. if (!status.ok()) {
  45. throw runtime_error(strprintf("init_blockindex(): error opening database environment %s", status.ToString().c_str()));
  46. }
  47. }
  48. CTxDB::CTxDB(const char* pszMode)
  49. {
  50. assert(pszMode);
  51. activeBatch = NULL;
  52. fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
  53. if (txdb) {
  54. pdb = txdb;
  55. return;
  56. }
  57. bool fCreate = strchr(pszMode, 'c');
  58. options = GetOptions();
  59. options.create_if_missing = fCreate;
  60. options.filter_policy = leveldb::NewBloomFilterPolicy(10);
  61. init_blockindex(options); // Init directory
  62. pdb = txdb;
  63. if (Exists(string("version")))
  64. {
  65. ReadVersion(nVersion);
  66. printf("Transaction index version is %d\n", nVersion);
  67. if (nVersion < DATABASE_VERSION)
  68. {
  69. printf("Required index version is %d, removing old database\n", DATABASE_VERSION);
  70. // Leveldb instance destruction
  71. delete txdb;
  72. txdb = pdb = NULL;
  73. delete activeBatch;
  74. activeBatch = NULL;
  75. init_blockindex(options, true); // Remove directory and create new database
  76. pdb = txdb;
  77. bool fTmp = fReadOnly;
  78. fReadOnly = false;
  79. WriteVersion(DATABASE_VERSION); // Save transaction index version
  80. fReadOnly = fTmp;
  81. }
  82. }
  83. else if (fCreate)
  84. {
  85. bool fTmp = fReadOnly;
  86. fReadOnly = false;
  87. WriteVersion(DATABASE_VERSION);
  88. fReadOnly = fTmp;
  89. }
  90. printf("Opened LevelDB successfully\n");
  91. }
  92. void CTxDB::Close()
  93. {
  94. delete txdb;
  95. txdb = pdb = NULL;
  96. delete options.filter_policy;
  97. options.filter_policy = NULL;
  98. delete options.block_cache;
  99. options.block_cache = NULL;
  100. delete activeBatch;
  101. activeBatch = NULL;
  102. }
  103. bool CTxDB::TxnBegin()
  104. {
  105. assert(!activeBatch);
  106. activeBatch = new leveldb::WriteBatch();
  107. return true;
  108. }
  109. bool CTxDB::TxnCommit()
  110. {
  111. assert(activeBatch);
  112. leveldb::Status status = pdb->Write(leveldb::WriteOptions(), activeBatch);
  113. delete activeBatch;
  114. activeBatch = NULL;
  115. if (!status.ok()) {
  116. printf("LevelDB batch commit failure: %s\n", status.ToString().c_str());
  117. return false;
  118. }
  119. return true;
  120. }
  121. class CBatchScanner : public leveldb::WriteBatch::Handler {
  122. public:
  123. std::string needle;
  124. bool *deleted;
  125. std::string *foundValue;
  126. bool foundEntry;
  127. CBatchScanner() : foundEntry(false) {}
  128. virtual void Put(const leveldb::Slice& key, const leveldb::Slice& value) {
  129. if (key.ToString() == needle) {
  130. foundEntry = true;
  131. *deleted = false;
  132. *foundValue = value.ToString();
  133. }
  134. }
  135. virtual void Delete(const leveldb::Slice& key) {
  136. if (key.ToString() == needle) {
  137. foundEntry = true;
  138. *deleted = true;
  139. }
  140. }
  141. };
  142. bool CTxDB::ScanBatch(const CDataStream &key, string *value, bool *deleted) const {
  143. assert(activeBatch);
  144. *deleted = false;
  145. CBatchScanner scanner;
  146. scanner.needle = key.str();
  147. scanner.deleted = deleted;
  148. scanner.foundValue = value;
  149. leveldb::Status status = activeBatch->Iterate(&scanner);
  150. if (!status.ok()) {
  151. throw runtime_error(status.ToString());
  152. }
  153. return scanner.foundEntry;
  154. }
  155. bool CTxDB::ReadTxIndex(uint256 hash, CTxIndex& txindex)
  156. {
  157. assert(!fClient);
  158. txindex.SetNull();
  159. return Read(make_pair(string("tx"), hash), txindex);
  160. }
  161. bool CTxDB::UpdateTxIndex(uint256 hash, const CTxIndex& txindex)
  162. {
  163. assert(!fClient);
  164. return Write(make_pair(string("tx"), hash), txindex);
  165. }
  166. bool CTxDB::AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight)
  167. {
  168. assert(!fClient);
  169. // Add to tx index
  170. uint256 hash = tx.GetHash();
  171. CTxIndex txindex(pos, tx.vout.size());
  172. return Write(make_pair(string("tx"), hash), txindex);
  173. }
  174. bool CTxDB::EraseTxIndex(const CTransaction& tx)
  175. {
  176. assert(!fClient);
  177. uint256 hash = tx.GetHash();
  178. return Erase(make_pair(string("tx"), hash));
  179. }
  180. bool CTxDB::ContainsTx(uint256 hash)
  181. {
  182. assert(!fClient);
  183. return Exists(make_pair(string("tx"), hash));
  184. }
  185. bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex)
  186. {
  187. assert(!fClient);
  188. tx.SetNull();
  189. if (!ReadTxIndex(hash, txindex))
  190. return false;
  191. return (tx.ReadFromDisk(txindex.pos));
  192. }
  193. bool CTxDB::ReadDiskTx(uint256 hash, CTransaction& tx)
  194. {
  195. CTxIndex txindex;
  196. return ReadDiskTx(hash, tx, txindex);
  197. }
  198. bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex)
  199. {
  200. return ReadDiskTx(outpoint.hash, tx, txindex);
  201. }
  202. bool CTxDB::ReadDiskTx(COutPoint outpoint, CTransaction& tx)
  203. {
  204. CTxIndex txindex;
  205. return ReadDiskTx(outpoint.hash, tx, txindex);
  206. }
  207. bool CTxDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
  208. {
  209. return Write(make_pair(string("blockindex"), blockindex.GetBlockHash()), blockindex);
  210. }
  211. bool CTxDB::ReadHashBestChain(uint256& hashBestChain)
  212. {
  213. return Read(string("hashBestChain"), hashBestChain);
  214. }
  215. bool CTxDB::WriteHashBestChain(uint256 hashBestChain)
  216. {
  217. return Write(string("hashBestChain"), hashBestChain);
  218. }
  219. bool CTxDB::ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust)
  220. {
  221. return Read(string("bnBestInvalidTrust"), bnBestInvalidTrust);
  222. }
  223. bool CTxDB::WriteBestInvalidTrust(CBigNum bnBestInvalidTrust)
  224. {
  225. return Write(string("bnBestInvalidTrust"), bnBestInvalidTrust);
  226. }
  227. bool CTxDB::ReadSyncCheckpoint(uint256& hashCheckpoint)
  228. {
  229. return Read(string("hashSyncCheckpoint"), hashCheckpoint);
  230. }
  231. bool CTxDB::WriteSyncCheckpoint(uint256 hashCheckpoint)
  232. {
  233. return Write(string("hashSyncCheckpoint"), hashCheckpoint);
  234. }
  235. bool CTxDB::ReadCheckpointPubKey(string& strPubKey)
  236. {
  237. return Read(string("strCheckpointPubKey"), strPubKey);
  238. }
  239. bool CTxDB::WriteCheckpointPubKey(const string& strPubKey)
  240. {
  241. return Write(string("strCheckpointPubKey"), strPubKey);
  242. }
  243. static CBlockIndex *InsertBlockIndex(uint256 hash)
  244. {
  245. if (hash == 0)
  246. return NULL;
  247. // Return existing
  248. map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
  249. if (mi != mapBlockIndex.end())
  250. return (*mi).second;
  251. // Create new
  252. CBlockIndex* pindexNew = new CBlockIndex();
  253. if (!pindexNew)
  254. throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
  255. mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
  256. pindexNew->phashBlock = &((*mi).first);
  257. return pindexNew;
  258. }
  259. bool CTxDB::LoadBlockIndex()
  260. {
  261. if (mapBlockIndex.size() > 0) {
  262. // Already loaded once in this session. It can happen during migration
  263. // from BDB.
  264. return true;
  265. }
  266. leveldb::Iterator *iterator = pdb->NewIterator(leveldb::ReadOptions());
  267. // Seek to start key.
  268. CDataStream ssStartKey(SER_DISK, CLIENT_VERSION);
  269. ssStartKey << make_pair(string("blockindex"), uint256(0));
  270. iterator->Seek(ssStartKey.str());
  271. // Now read each entry.
  272. while (iterator->Valid())
  273. {
  274. // Unpack keys and values.
  275. CDataStream ssKey(SER_DISK, CLIENT_VERSION);
  276. ssKey.write(iterator->key().data(), iterator->key().size());
  277. CDataStream ssValue(SER_DISK, CLIENT_VERSION);
  278. ssValue.write(iterator->value().data(), iterator->value().size());
  279. string strType;
  280. ssKey >> strType;
  281. // Did we reach the end of the data to read?
  282. if (fRequestShutdown || strType != "blockindex")
  283. break;
  284. CDiskBlockIndex diskindex;
  285. ssValue >> diskindex;
  286. uint256 blockHash = diskindex.GetBlockHash();
  287. // Construct block index object
  288. CBlockIndex* pindexNew = InsertBlockIndex(blockHash);
  289. pindexNew->pprev = InsertBlockIndex(diskindex.hashPrev);
  290. pindexNew->pnext = InsertBlockIndex(diskindex.hashNext);
  291. pindexNew->nFile = diskindex.nFile;
  292. pindexNew->nBlockPos = diskindex.nBlockPos;
  293. pindexNew->nHeight = diskindex.nHeight;
  294. pindexNew->nMint = diskindex.nMint;
  295. pindexNew->nMoneySupply = diskindex.nMoneySupply;
  296. pindexNew->nFlags = diskindex.nFlags;
  297. pindexNew->nStakeModifier = diskindex.nStakeModifier;
  298. pindexNew->prevoutStake = diskindex.prevoutStake;
  299. pindexNew->nStakeTime = diskindex.nStakeTime;
  300. pindexNew->hashProofOfStake = diskindex.hashProofOfStake;
  301. pindexNew->nVersion = diskindex.nVersion;
  302. pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
  303. pindexNew->nTime = diskindex.nTime;
  304. pindexNew->nBits = diskindex.nBits;
  305. pindexNew->nNonce = diskindex.nNonce;
  306. // Watch for genesis block
  307. if (pindexGenesisBlock == NULL && blockHash == (!fTestNet ? hashGenesisBlock : hashGenesisBlockTestNet))
  308. pindexGenesisBlock = pindexNew;
  309. if (!pindexNew->CheckIndex()) {
  310. delete iterator;
  311. return error("LoadBlockIndex() : CheckIndex failed at %d", pindexNew->nHeight);
  312. }
  313. // Ecoin: build setStakeSeen
  314. if (pindexNew->IsProofOfStake())
  315. setStakeSeen.insert(make_pair(pindexNew->prevoutStake, pindexNew->nStakeTime));
  316. iterator->Next();
  317. }
  318. delete iterator;
  319. if (fRequestShutdown)
  320. return true;
  321. // Calculate nChainTrust
  322. vector<pair<int, CBlockIndex*> > vSortedByHeight;
  323. vSortedByHeight.reserve(mapBlockIndex.size());
  324. BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
  325. {
  326. CBlockIndex* pindex = item.second;
  327. vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
  328. }
  329. sort(vSortedByHeight.begin(), vSortedByHeight.end());
  330. BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
  331. {
  332. CBlockIndex* pindex = item.second;
  333. pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
  334. // Ecoin: calculate stake modifier checksum
  335. pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
  336. if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
  337. return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016" PRI64x, pindex->nHeight, pindex->nStakeModifier);
  338. }
  339. // Load hashBestChain pointer to end of best chain
  340. if (!ReadHashBestChain(hashBestChain))
  341. {
  342. if (pindexGenesisBlock == NULL)
  343. return true;
  344. return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
  345. }
  346. if (!mapBlockIndex.count(hashBestChain))
  347. return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
  348. pindexBest = mapBlockIndex[hashBestChain];
  349. nBestHeight = pindexBest->nHeight;
  350. nBestChainTrust = pindexBest->nChainTrust;
  351. printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
  352. hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
  353. DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
  354. // Ecoin: load hashSyncCheckpoint
  355. if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
  356. return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
  357. printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
  358. // Load bnBestInvalidTrust, OK if it doesn't exist
  359. CBigNum bnBestInvalidTrust;
  360. ReadBestInvalidTrust(bnBestInvalidTrust);
  361. nBestInvalidTrust = bnBestInvalidTrust.getuint256();
  362. // Verify blocks in the best chain
  363. int nCheckLevel = GetArg("-checklevel", 1);
  364. int nCheckDepth = GetArg( "-checkblocks", 2500);
  365. if (nCheckDepth == 0)
  366. nCheckDepth = 1000000000; // suffices until the year 19000
  367. if (nCheckDepth > nBestHeight)
  368. nCheckDepth = nBestHeight;
  369. printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
  370. CBlockIndex* pindexFork = NULL;
  371. map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
  372. for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
  373. {
  374. if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
  375. break;
  376. CBlock block;
  377. if (!block.ReadFromDisk(pindex))
  378. return error("LoadBlockIndex() : block.ReadFromDisk failed");
  379. // check level 1: verify block validity
  380. // check level 7: verify block signature too
  381. if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6)))
  382. {
  383. printf("LoadBlockIndex() : *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str());
  384. pindexFork = pindex->pprev;
  385. }
  386. // check level 2: verify transaction index validity
  387. if (nCheckLevel>1)
  388. {
  389. pair<unsigned int, unsigned int> pos = make_pair(pindex->nFile, pindex->nBlockPos);
  390. mapBlockPos[pos] = pindex;
  391. BOOST_FOREACH(const CTransaction &tx, block.vtx)
  392. {
  393. uint256 hashTx = tx.GetHash();
  394. CTxIndex txindex;
  395. if (ReadTxIndex(hashTx, txindex))
  396. {
  397. // check level 3: checker transaction hashes
  398. if (nCheckLevel>2 || pindex->nFile != txindex.pos.nFile || pindex->nBlockPos != txindex.pos.nBlockPos)
  399. {
  400. // either an error or a duplicate transaction
  401. CTransaction txFound;
  402. if (!txFound.ReadFromDisk(txindex.pos))
  403. {
  404. printf("LoadBlockIndex() : *** cannot read mislocated transaction %s\n", hashTx.ToString().c_str());
  405. pindexFork = pindex->pprev;
  406. }
  407. else
  408. if (txFound.GetHash() != hashTx) // not a duplicate tx
  409. {
  410. printf("LoadBlockIndex(): *** invalid tx position for %s\n", hashTx.ToString().c_str());
  411. pindexFork = pindex->pprev;
  412. }
  413. }
  414. // check level 4: check whether spent txouts were spent within the main chain
  415. unsigned int nOutput = 0;
  416. if (nCheckLevel>3)
  417. {
  418. BOOST_FOREACH(const CDiskTxPos &txpos, txindex.vSpent)
  419. {
  420. if (!txpos.IsNull())
  421. {
  422. pair<unsigned int, unsigned int> posFind = make_pair(txpos.nFile, txpos.nBlockPos);
  423. if (!mapBlockPos.count(posFind))
  424. {
  425. printf("LoadBlockIndex(): *** found bad spend at %d, hashBlock=%s, hashTx=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString().c_str(), hashTx.ToString().c_str());
  426. pindexFork = pindex->pprev;
  427. }
  428. // check level 6: check whether spent txouts were spent by a valid transaction that consume them
  429. if (nCheckLevel>5)
  430. {
  431. CTransaction txSpend;
  432. if (!txSpend.ReadFromDisk(txpos))
  433. {
  434. printf("LoadBlockIndex(): *** cannot read spending transaction of %s:%i from disk\n", hashTx.ToString().c_str(), nOutput);
  435. pindexFork = pindex->pprev;
  436. }
  437. else if (!txSpend.CheckTransaction())
  438. {
  439. printf("LoadBlockIndex(): *** spending transaction of %s:%i is invalid\n", hashTx.ToString().c_str(), nOutput);
  440. pindexFork = pindex->pprev;
  441. }
  442. else
  443. {
  444. bool fFound = false;
  445. BOOST_FOREACH(const CTxIn &txin, txSpend.vin)
  446. if (txin.prevout.hash == hashTx && txin.prevout.n == nOutput)
  447. fFound = true;
  448. if (!fFound)
  449. {
  450. printf("LoadBlockIndex(): *** spending transaction of %s:%i does not spend it\n", hashTx.ToString().c_str(), nOutput);
  451. pindexFork = pindex->pprev;
  452. }
  453. }
  454. }
  455. }
  456. nOutput++;
  457. }
  458. }
  459. }
  460. // check level 5: check whether all prevouts are marked spent
  461. if (nCheckLevel>4)
  462. {
  463. BOOST_FOREACH(const CTxIn &txin, tx.vin)
  464. {
  465. CTxIndex txindex;
  466. if (ReadTxIndex(txin.prevout.hash, txindex))
  467. if (txindex.vSpent.size()-1 < txin.prevout.n || txindex.vSpent[txin.prevout.n].IsNull())
  468. {
  469. printf("LoadBlockIndex(): *** found unspent prevout %s:%i in %s\n", txin.prevout.hash.ToString().c_str(), txin.prevout.n, hashTx.ToString().c_str());
  470. pindexFork = pindex->pprev;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. if (pindexFork && !fRequestShutdown)
  478. {
  479. // Reorg back to the fork
  480. printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);
  481. CBlock block;
  482. if (!block.ReadFromDisk(pindexFork))
  483. return error("LoadBlockIndex() : block.ReadFromDisk failed");
  484. CTxDB txdb;
  485. block.SetBestChain(txdb, pindexFork);
  486. }
  487. return true;
  488. }