PIVX Core  5.6.99
P2P Digital Currency
sapling_txdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 The PIVX Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "txdb.h"
6 
7 // Db keys
8 static const char DB_SAPLING_ANCHOR = 'Z';
9 static const char DB_SAPLING_NULLIFIER = 'S';
10 static const char DB_BEST_SAPLING_ANCHOR = 'z';
11 
12 // Sapling
14  if (rt == SaplingMerkleTree::empty_root()) {
15  SaplingMerkleTree new_tree;
16  tree = new_tree;
17  return true;
18  }
19 
20  bool read = db.Read(std::make_pair(DB_SAPLING_ANCHOR, rt), tree);
21 
22  return read;
23 }
24 
25 bool CCoinsViewDB::GetNullifier(const uint256 &nf) const {
26  bool spent = false;
27  return db.Read(std::make_pair(DB_SAPLING_NULLIFIER, nf), spent);
28 }
29 
31  uint256 hashBestAnchor;
32  if (!db.Read(DB_BEST_SAPLING_ANCHOR, hashBestAnchor))
34  return hashBestAnchor;
35 }
36 
37 void BatchWriteNullifiers(CDBBatch& batch, CNullifiersMap& mapToUse, const char& dbChar)
38 {
39  size_t count = 0;
40  size_t changed = 0;
41  for (CNullifiersMap::iterator it = mapToUse.begin(); it != mapToUse.end();) {
42  if (it->second.flags & CNullifiersCacheEntry::DIRTY) {
43  if (!it->second.entered)
44  batch.Erase(std::make_pair(dbChar, it->first));
45  else
46  batch.Write(std::make_pair(dbChar, it->first), true);
47  changed++;
48  }
49  count++;
50  CNullifiersMap::iterator itOld = it++;
51  mapToUse.erase(itOld);
52  }
53  LogPrint(BCLog::COINDB, "Committed %u changed nullifiers (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
54 }
55 
56 template<typename Map, typename MapIterator, typename MapEntry, typename Tree>
57 void BatchWriteAnchors(CDBBatch& batch, Map& mapToUse, const char& dbChar)
58 {
59  size_t count = 0;
60  size_t changed = 0;
61  for (MapIterator it = mapToUse.begin(); it != mapToUse.end();) {
62  if (it->second.flags & MapEntry::DIRTY) {
63  if (!it->second.entered)
64  batch.Erase(std::make_pair(dbChar, it->first));
65  else {
66  if (it->first != Tree::empty_root()) {
67  batch.Write(std::make_pair(dbChar, it->first), it->second.tree);
68  }
69  }
70  changed++;
71  }
72  count++;
73  MapIterator itOld = it++;
74  mapToUse.erase(itOld);
75  }
76  LogPrint(BCLog::COINDB, "Committed %u changed sapling anchors (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
77 }
78 
79 bool CCoinsViewDB::BatchWriteSapling(const uint256& hashSaplingAnchor,
80  CAnchorsSaplingMap& mapSaplingAnchors,
81  CNullifiersMap& mapSaplingNullifiers,
82  CDBBatch& batch) {
83 
84  ::BatchWriteAnchors<CAnchorsSaplingMap, CAnchorsSaplingMap::iterator, CAnchorsSaplingCacheEntry, SaplingMerkleTree>(batch, mapSaplingAnchors, DB_SAPLING_ANCHOR);
85  ::BatchWriteNullifiers(batch, mapSaplingNullifiers, DB_SAPLING_NULLIFIER);
86  if (!hashSaplingAnchor.IsNull())
87  batch.Write(DB_BEST_SAPLING_ANCHOR, hashSaplingAnchor);
88  return true;
89 }
uint256 GetBestAnchor() const override
Get the current "tip" or the latest anchored tree root in the chain.
bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const override
Retrieve the tree (Sapling) at a particular anchored root in the chain.
CDBWrapper db
Definition: txdb.h:73
bool GetNullifier(const uint256 &nf) const override
Determine whether a nullifier is spent or not.
bool BatchWriteSapling(const uint256 &hashSaplingAnchor, CAnchorsSaplingMap &mapSaplingAnchors, CNullifiersMap &mapSaplingNullifiers, CDBBatch &batch)
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:46
void Erase(const K &key)
Definition: dbwrapper.h:100
void Write(const K &key, const V &value)
Definition: dbwrapper.h:69
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:260
bool IsNull() const
Definition: uint256.h:36
256-bit opaque blob.
Definition: uint256.h:138
std::unordered_map< uint256, CAnchorsSaplingCacheEntry, SaltedIdHasher > CAnchorsSaplingMap
Definition: coins.h:174
std::unordered_map< uint256, CNullifiersCacheEntry, SaltedIdHasher > CNullifiersMap
Definition: coins.h:175
#define LogPrint(category,...)
Definition: logging.h:163
@ COINDB
Definition: logging.h:55
void BatchWriteNullifiers(CDBBatch &batch, CNullifiersMap &mapToUse, const char &dbChar)
void BatchWriteAnchors(CDBBatch &batch, Map &mapToUse, const char &dbChar)
@ DIRTY
Definition: coins.h:168