Raven Core  3.0.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
restricteddb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 The Raven Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "restricteddb.h"
6 #include "validation.h"
7 
8 #include <boost/thread.hpp>
9 
10 static const char DB_FLAG = 'D';
11 static const char VERIFIER_FLAG = 'V';
12 static const char ADDRESS_QULAIFIER_FLAG = 'T';
13 static const char QULAIFIER_ADDRESS_FLAG = 'Q';
14 static const char RESTRICTED_ADDRESS_FLAG = 'R';
15 static const char GLOBAL_RESTRICTION_FLAG = 'G';
16 
17 
18 
19 CRestrictedDB::CRestrictedDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "assets" / "restricted", nCacheSize, fMemory, fWipe) {
20 }
21 
22 // Restricted Verifier Strings
23 bool CRestrictedDB::WriteVerifier(const std::string& assetName, const std::string& verifier)
24 {
25  return Write(std::make_pair(VERIFIER_FLAG, assetName), verifier);
26 }
27 
28 bool CRestrictedDB::ReadVerifier(const std::string& assetName, std::string& verifier)
29 {
30  return Read(std::make_pair(VERIFIER_FLAG, assetName), verifier);
31 }
32 
33 bool CRestrictedDB::EraseVerifier(const std::string& assetName)
34 {
35  return Erase(std::make_pair(VERIFIER_FLAG, assetName));
36 }
37 
38 // Address Tags
39 bool CRestrictedDB::WriteAddressQualifier(const std::string &address, const std::string &tag)
40 {
41  int8_t i = 1;
42  return Write(std::make_pair(ADDRESS_QULAIFIER_FLAG, std::make_pair(address, tag)), i);
43 }
44 
45 bool CRestrictedDB::ReadAddressQualifier(const std::string &address, const std::string &tag)
46 {
47  int8_t i;
48  return Read(std::make_pair(ADDRESS_QULAIFIER_FLAG, std::make_pair(address, tag)), i);
49 }
50 
51 bool CRestrictedDB::EraseAddressQualifier(const std::string &address, const std::string &tag)
52 {
53  return Erase(std::make_pair(ADDRESS_QULAIFIER_FLAG, std::make_pair(address, tag)));
54 }
55 
56 // Address Tags
57 bool CRestrictedDB::WriteQualifierAddress(const std::string &address, const std::string &tag)
58 {
59  int8_t i = 1;
60  return Write(std::make_pair(QULAIFIER_ADDRESS_FLAG, std::make_pair(tag, address)), i);
61 }
62 
63 bool CRestrictedDB::ReadQualifierAddress(const std::string &address, const std::string &tag)
64 {
65  int8_t i;
66  return Read(std::make_pair(QULAIFIER_ADDRESS_FLAG, std::make_pair(tag, address)), i);
67 }
68 
69 bool CRestrictedDB::EraseQualifierAddress(const std::string &address, const std::string &tag)
70 {
71  return Erase(std::make_pair(QULAIFIER_ADDRESS_FLAG, std::make_pair(tag, address)));
72 }
73 
74 
75 // Address Restriction
76 bool CRestrictedDB::WriteRestrictedAddress(const std::string& address, const std::string& assetName)
77 {
78  int8_t i = 1;
79  return Write(std::make_pair(RESTRICTED_ADDRESS_FLAG, std::make_pair(address, assetName)), i);
80 }
81 
82 bool CRestrictedDB::ReadRestrictedAddress(const std::string& address, const std::string& assetName)
83 {
84  int8_t i;
85  return Read(std::make_pair(RESTRICTED_ADDRESS_FLAG, std::make_pair(address, assetName)), i);
86 }
87 
88 bool CRestrictedDB::EraseRestrictedAddress(const std::string& address, const std::string& assetName)
89 {
90  return Erase(std::make_pair(RESTRICTED_ADDRESS_FLAG, std::make_pair(address, assetName)));
91 }
92 
93 // Global Restriction
94 bool CRestrictedDB::WriteGlobalRestriction(const std::string& assetName)
95 {
96  int8_t i = 1;
97  return Write(std::make_pair(GLOBAL_RESTRICTION_FLAG, assetName), i);
98 }
99 
100 bool CRestrictedDB::ReadGlobalRestriction(const std::string& assetName)
101 {
102  int8_t i;
103  return Read(std::make_pair(GLOBAL_RESTRICTION_FLAG, assetName), i);
104 }
105 
106 bool CRestrictedDB::EraseGlobalRestriction(const std::string& assetName)
107 {
108  return Erase(std::make_pair(GLOBAL_RESTRICTION_FLAG, assetName));
109 }
110 
111 bool CRestrictedDB::WriteFlag(const std::string &name, bool fValue)
112 {
113  return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
114 }
115 
116 bool CRestrictedDB::ReadFlag(const std::string &name, bool &fValue)
117 {
118  char ch;
119  if (!Read(std::make_pair(DB_FLAG, name), ch))
120  return false;
121  fValue = ch == '1';
122  return true;
123 }
124 
125 bool CRestrictedDB::GetQualifierAddresses(std::string& qualifier, std::vector<std::string>& addresses)
126 {
128 
129  std::unique_ptr<CDBIterator> pcursor(NewIterator());
130 
131  pcursor->Seek(std::make_pair(QULAIFIER_ADDRESS_FLAG, std::make_pair(qualifier, std::string())));
132 
133  // Load all qualifiers related to that given address
134  while (pcursor->Valid()) {
135  boost::this_thread::interruption_point();
136  std::pair<char, std::pair<std::string, std::string> > key;
137  if (pcursor->GetKey(key) && key.first == QULAIFIER_ADDRESS_FLAG && key.second.first == qualifier) {
138  addresses.emplace_back(key.second.second);
139  pcursor->Next();
140  } else {
141  break;
142  }
143  }
144 
145  return true;
146 }
147 
148 bool CRestrictedDB::CheckForAddressRootQualifier(const std::string& address, const std::string& qualifier)
149 {
150  std::unique_ptr<CDBIterator> pcursor(NewIterator());
151 
152  pcursor->Seek(std::make_pair(ADDRESS_QULAIFIER_FLAG, std::make_pair(address, qualifier)));
153 
154  // Load all qualifiers related to that given address
155  while (pcursor->Valid()) {
156  boost::this_thread::interruption_point();
157  std::pair<char, std::pair<std::string, std::string> > key;
158  if (pcursor->GetKey(key) && key.first == ADDRESS_QULAIFIER_FLAG && key.second.first == address) {
159  if (key.second.second == qualifier || key.second.second.rfind(std::string(qualifier + "/"), 0) == 0) {
160  return true;
161  }
162  pcursor->Next();
163  } else {
164  break;
165  }
166  }
167 
168  return false;
169 }
170 
171 bool CRestrictedDB::GetAddressQualifiers(std::string& address, std::vector<std::string>& qualifiers)
172 {
174 
175  std::unique_ptr<CDBIterator> pcursor(NewIterator());
176 
177  pcursor->Seek(std::make_pair(ADDRESS_QULAIFIER_FLAG, std::make_pair(address, std::string())));
178 
179  // Load all qualifiers related to that given address
180  while (pcursor->Valid()) {
181  boost::this_thread::interruption_point();
182  std::pair<char, std::pair<std::string, std::string> > key;
183  if (pcursor->GetKey(key) && key.first == ADDRESS_QULAIFIER_FLAG && key.second.first == address) {
184  qualifiers.emplace_back(key.second.second);
185  pcursor->Next();
186  } else {
187  break;
188  }
189  }
190 
191  return true;
192 }
193 
194 bool CRestrictedDB::GetAddressRestrictions(std::string& address, std::vector<std::string>& restrictions)
195 {
197 
198  std::unique_ptr<CDBIterator> pcursor(NewIterator());
199 
200  pcursor->Seek(std::make_pair(RESTRICTED_ADDRESS_FLAG, std::make_pair(address, std::string())));
201 
202  // Load all restrictions related to the given address
203  while (pcursor->Valid()) {
204  boost::this_thread::interruption_point();
205  std::pair<char, std::pair<std::string, std::string> > key;
206  if (pcursor->GetKey(key) && key.first == RESTRICTED_ADDRESS_FLAG && key.second.first == address) {
207  restrictions.emplace_back(key.second.second);
208  pcursor->Next();
209  } else {
210  break;
211  }
212  }
213 
214  return true;
215 }
216 
217 bool CRestrictedDB::GetGlobalRestrictions(std::vector<std::string>& restrictions)
218 {
220 
221  std::unique_ptr<CDBIterator> pcursor(NewIterator());
222 
223  pcursor->Seek(std::make_pair(GLOBAL_RESTRICTION_FLAG, std::string()));
224 
225  // Load all restrictions related to the given address
226  while (pcursor->Valid()) {
227  boost::this_thread::interruption_point();
228  std::pair<char, std::string> key;
229  if (pcursor->GetKey(key) && key.first == GLOBAL_RESTRICTION_FLAG) {
230  restrictions.emplace_back(key.second);
231  pcursor->Next();
232  } else {
233  break;
234  }
235  }
236 
237  return true;
238 }
bool WriteQualifierAddress(const std::string &address, const std::string &tag)
CRestrictedDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
bool WriteAddressQualifier(const std::string &address, const std::string &tag)
bool WriteFlag(const std::string &name, bool fValue)
bool EraseQualifierAddress(const std::string &address, const std::string &tag)
bool EraseAddressQualifier(const std::string &address, const std::string &tag)
bool GetAddressQualifiers(std::string &address, std::vector< std::string > &qualifiers)
bool ReadGlobalRestriction(const std::string &assetName)
CDBIterator * NewIterator()
Definition: dbwrapper.h:300
bool Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:279
bool EraseGlobalRestriction(const std::string &assetName)
bool ReadVerifier(const std::string &assetName, std::string &verifier)
bool EraseVerifier(const std::string &assetName)
bool CheckForAddressRootQualifier(const std::string &address, const std::string &qualifier)
bool EraseRestrictedAddress(const std::string &address, const std::string &assetName)
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:226
void FlushStateToDisk()
Flush all state, indexes and buffers to disk.
bool GetQualifierAddresses(std::string &qualifier, std::vector< std::string > &addresses)
bool ReadAddressQualifier(const std::string &address, const std::string &tag)
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:252
bool WriteRestrictedAddress(const std::string &address, const std::string &assetName)
bool ReadFlag(const std::string &name, bool &fValue)
bool ReadQualifierAddress(const std::string &address, const std::string &tag)
bool GetGlobalRestrictions(std::vector< std::string > &restrictions)
bool GetAddressRestrictions(std::string &address, std::vector< std::string > &restrictions)
bool WriteGlobalRestriction(const std::string &assetName)
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:572
bool WriteVerifier(const std::string &assetName, const std::string &verifier)
bool ReadRestrictedAddress(const std::string &address, const std::string &assetName)