-
Notifications
You must be signed in to change notification settings - Fork 16
/
DSCAttachedPassportCounter.cpp
executable file
·75 lines (55 loc) · 2.39 KB
/
DSCAttachedPassportCounter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "DSCAttachedPassportCounter.h"
#include "DB/DB.h"
#include "ChainParams.h"
#include "Serialization/streams.h"
#include "Tools/Log.h"
bool DSCAttachedPassportCounter::increment(std::vector<unsigned char> dscId) {
DB& db = DB::Instance();
std::vector<unsigned char> dscCountVector = db.getFromDB(DB_DSC_ATTACHED_PASSPORTS_COUNTER, dscId);
DSCAttachedPassportCount dscAttachedPassportCount;
if(dscCountVector.size()) {
CDataStream s(SER_DISK, 1);
s.write((char*)dscCountVector.data(), dscCountVector.size());
s >> dscAttachedPassportCount;
dscAttachedPassportCount++;
} else {
dscAttachedPassportCount.count = 1;
}
CDataStream s2(SER_DISK, 1);
s2 << dscAttachedPassportCount;
db.putInDB(DB_DSC_ATTACHED_PASSPORTS_COUNTER, dscId, std::vector<unsigned char> (s2.data(), s2.data() + s2.size()));
return true;
}
bool DSCAttachedPassportCounter::decrement(std::vector<unsigned char> dscId) {
DB& db = DB::Instance();
std::vector<unsigned char> dscCountVector = db.getFromDB(DB_DSC_ATTACHED_PASSPORTS_COUNTER, dscId);
DSCAttachedPassportCount dscAttachedPassportCount;
if(!dscCountVector.size()) {
Log(LOG_LEVEL_ERROR) << "Cannot decrement not existing DB_DSC_ATTACHED_PASSPORTS_COUNTER entry" << dscId;
return false;
}
CDataStream s(SER_DISK, 1);
s.write((char*)dscCountVector.data(), dscCountVector.size());
s >> dscAttachedPassportCount;
if(dscAttachedPassportCount.count == 0) {
Log(LOG_LEVEL_CRITICAL_ERROR) << "Cannot decrement DB_DSC_ATTACHED_PASSPORTS_COUNTER, entry is already 0" << dscId;
return false;
}
dscAttachedPassportCount--;
CDataStream s2(SER_DISK, 1);
s2 << dscAttachedPassportCount;
db.putInDB(DB_DSC_ATTACHED_PASSPORTS_COUNTER, dscId, std::vector<unsigned char> (s2.data(), s2.data() + s2.size()));
return true;
}
uint64_t DSCAttachedPassportCounter::getCount(std::vector<unsigned char> dscId) {
DB& db = DB::Instance();
std::vector<unsigned char> dscCountVector = db.getFromDB(DB_DSC_ATTACHED_PASSPORTS_COUNTER, dscId);
DSCAttachedPassportCount dscAttachedPassportCount;
if(dscCountVector.size()) {
CDataStream s(SER_DISK, 1);
s.write((char*)dscCountVector.data(), dscCountVector.size());
s >> dscAttachedPassportCount;
return dscAttachedPassportCount.count;
}
return 0;
}