1
+ // Copyright (c) 2017 Duality Blockchain Solutions Developers
2
+
3
+
4
+ #include " fluidmining.h"
5
+
6
+ #include " core_io.h"
7
+ #include " fluid.h"
8
+ #include " operations.h"
9
+ #include " script/script.h"
10
+
11
+ CFluidMiningDB *pFluidMiningDB = NULL ;
12
+
13
+ bool GetFluidMiningData (const CScript& scriptPubKey, CFluidMining& entry)
14
+ {
15
+ std::string fluidOperationString = ScriptToAsmStr (scriptPubKey);
16
+ std::string strOperationCode = GetRidOfScriptStatement (fluidOperationString, 0 );
17
+ std::string verificationWithoutOpCode = GetRidOfScriptStatement (fluidOperationString);
18
+ std::vector<std::string> splitString;
19
+ HexFunctions hexConvert;
20
+ hexConvert.ConvertToString (verificationWithoutOpCode);
21
+ SeparateString (verificationWithoutOpCode, splitString, false );
22
+ std::string messageTokenKey = splitString.at (0 );
23
+ std::vector<std::string> vecSplitScript;
24
+ SeparateFluidOpString (verificationWithoutOpCode, vecSplitScript);
25
+
26
+ if (vecSplitScript.size () == 5 && strOperationCode == " OP_REWARD_MINING" ) {
27
+ std::vector<unsigned char > vchFluidOperation = CharVectorFromString (fluidOperationString);
28
+ entry.FluidScript .insert (entry.FluidScript .end (), vchFluidOperation.begin (), vchFluidOperation.end ());
29
+ std::string strAmount = vecSplitScript[0 ];
30
+ CAmount fluidAmount;
31
+ if (ParseFixedPoint (strAmount, 8 , &fluidAmount)) {
32
+ entry.MiningReward = fluidAmount;
33
+ }
34
+ std::string strTimeStamp = vecSplitScript[1 ];
35
+ int64_t tokenTimeStamp;
36
+ if (ParseInt64 (strTimeStamp, &tokenTimeStamp)) {
37
+ entry.nTimeStamp = tokenTimeStamp;
38
+ }
39
+ entry.SovereignAddresses .clear ();
40
+ entry.SovereignAddresses .push_back (CharVectorFromString (fluid.GetAddressFromDigestSignature (vecSplitScript[2 ], messageTokenKey).ToString ()));
41
+ entry.SovereignAddresses .push_back (CharVectorFromString (fluid.GetAddressFromDigestSignature (vecSplitScript[3 ], messageTokenKey).ToString ()));
42
+ entry.SovereignAddresses .push_back (CharVectorFromString (fluid.GetAddressFromDigestSignature (vecSplitScript[4 ], messageTokenKey).ToString ()));
43
+
44
+ LogPrintf (" GetFluidMiningData: strAmount = %s, strTimeStamp = %d, Addresses1 = %s, Addresses2 = %s, Addresses3 = %s \n " ,
45
+ strAmount, entry.nTimeStamp , StringFromCharVector (entry.SovereignAddresses [0 ]),
46
+ StringFromCharVector (entry.SovereignAddresses [1 ]), StringFromCharVector (entry.SovereignAddresses [2 ]));
47
+
48
+ return true ;
49
+ }
50
+ return false ;
51
+ }
52
+
53
+ bool GetFluidMiningData (const CTransaction& tx, CFluidMining& entry, int & nOut)
54
+ {
55
+ int n = 0 ;
56
+ for (const CTxOut& txout : tx.vout ) {
57
+ CScript txOut = txout.scriptPubKey ;
58
+ if (IsTransactionFluid (txOut)) {
59
+ nOut = n;
60
+ return GetFluidMiningData (txOut, entry);
61
+ }
62
+ n++;
63
+ }
64
+ return false ;
65
+ }
66
+
67
+ bool CFluidMining::UnserializeFromTx (const CTransaction& tx) {
68
+ std::vector<unsigned char > vchData;
69
+ std::vector<unsigned char > vchHash;
70
+ int nOut;
71
+ if (!GetFluidMiningData (tx, *this , nOut))
72
+ {
73
+ SetNull ();
74
+ return false ;
75
+ }
76
+ return true ;
77
+ }
78
+
79
+ bool CFluidMining::UnserializeFromScript (const CScript& fluidScript) {
80
+ std::vector<unsigned char > vchData;
81
+ std::vector<unsigned char > vchHash;
82
+ if (!GetFluidMiningData (fluidScript, *this ))
83
+ {
84
+ SetNull ();
85
+ return false ;
86
+ }
87
+ return true ;
88
+ }
89
+
90
+ void CFluidMining::Serialize (std::vector<unsigned char >& vchData) {
91
+ CDataStream dsFluidOp (SER_NETWORK, PROTOCOL_VERSION);
92
+ dsFluidOp << *this ;
93
+ vchData = std::vector<unsigned char >(dsFluidOp.begin (), dsFluidOp.end ());
94
+ }
95
+
96
+ CFluidMiningDB::CFluidMiningDB (size_t nCacheSize, bool fMemory , bool fWipe , bool obfuscate) : CDBWrapper(GetDataDir() / "fluid-mining", nCacheSize, fMemory, fWipe, obfuscate)
97
+ {
98
+ }
99
+
100
+ bool CFluidMiningDB::AddFluidMiningEntry (const CFluidMining& entry, const int op)
101
+ {
102
+ bool writeState = false ;
103
+ {
104
+ LOCK (cs_fluid_mining);
105
+ writeState = Write (make_pair (std::string (" script" ), entry.FluidScript ), entry)
106
+ && Write (make_pair (std::string (" txid" ), entry.txHash ), entry.FluidScript );
107
+ }
108
+
109
+ return writeState;
110
+ }
111
+
112
+ bool CFluidMiningDB::GetLastFluidMiningRecord (CFluidMining& entry)
113
+ {
114
+ LOCK (cs_fluid_mining);
115
+ std::unique_ptr<CDBIterator> pcursor (NewIterator ());
116
+ pcursor->SeekToLast ();
117
+ pcursor->GetValue (entry);
118
+ return true ;
119
+ }
120
+
121
+ bool CFluidMiningDB::GetAllFluidMiningRecords (std::vector<CFluidMining>& entries)
122
+ {
123
+ LOCK (cs_fluid_mining);
124
+ std::pair<std::string, std::vector<unsigned char > > key;
125
+ std::unique_ptr<CDBIterator> pcursor (NewIterator ());
126
+ pcursor->SeekToFirst ();
127
+ while (pcursor->Valid ()) {
128
+ boost::this_thread::interruption_point ();
129
+ CFluidMining entry;
130
+ try {
131
+ if (pcursor->GetKey (key) && key.first == " script" ) {
132
+ pcursor->GetValue (entry);
133
+ if (!entry.IsNull ())
134
+ {
135
+ entries.push_back (entry);
136
+ }
137
+ }
138
+ pcursor->Next ();
139
+ }
140
+ catch (std::exception& e) {
141
+ return error (" %s() : deserialize error" , __PRETTY_FUNCTION__);
142
+ }
143
+ }
144
+ return true ;
145
+ }
146
+
147
+ bool CheckFluidMiningDB ()
148
+ {
149
+ if (!pFluidMiningDB)
150
+ return false ;
151
+
152
+ return true ;
153
+ }
0 commit comments