Skip to content

Commit 36c4855

Browse files
committed
[Fluid] Refactor fluid code and remove index database
1 parent 65717d5 commit 36c4855

18 files changed

+532
-223
lines changed

src/Makefile.am

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ DYNAMIC_CORE_H = \
104104
dynodeman.h \
105105
dynodeconfig.h \
106106
flat-database.h \
107-
fluid.h \
107+
fluid/fluid.h \
108+
fluid/fluiddynode.h \
108109
governance.h \
109110
governance-classes.h \
110111
governance-exceptions.h \
@@ -225,7 +226,9 @@ libdynamic_server_a_SOURCES = \
225226
dynode-sync.cpp \
226227
dynodeconfig.cpp \
227228
dynodeman.cpp \
228-
fluid.cpp \
229+
fluid/fluid.cpp \
230+
fluid/fluiddynode.cpp \
231+
fluid/rpcfluid.cpp \
229232
governance.cpp \
230233
governance-classes.cpp \
231234
governance-object.cpp \
@@ -260,7 +263,6 @@ libdynamic_server_a_SOURCES = \
260263
rpcnet.cpp \
261264
rpcrawtransaction.cpp \
262265
rpcserver.cpp \
263-
rpcfluid.cpp \
264266
script/sigcache.cpp \
265267
sendalert.cpp \
266268
spork.cpp \
@@ -510,6 +512,7 @@ CLEANFILES += *.gcda *.gcno
510512
CLEANFILES += compat/*.gcda compat/*.gcno
511513
CLEANFILES += consensus/*.gcda consensus/*.gcno
512514
CLEANFILES += crypto/*.gcda crypto/*.gcno
515+
CLEANFILES += fluid/*.gcda fluid/*.gcno
513516
CLEANFILES += policy/*.gcda policy/*.gcno
514517
CLEANFILES += primitives/*.gcda primitives/*.gcno
515518
CLEANFILES += script/*.gcda script/*.gcno

src/chain.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
77

88
#include "chain.h"
9-
#include "fluid.h"
109

1110
/**
1211
* CChain implementation
@@ -120,11 +119,6 @@ void CBlockIndex::BuildSkip()
120119
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
121120
}
122121

123-
std::vector<std::string> InitialiseAddresses() {
124-
CFluidParameters params;
125-
return params.InitialiseAddresses();
126-
}
127-
128122
arith_uint256 GetBlockProof(const CBlockIndex& block)
129123
{
130124
arith_uint256 bnTarget;

src/chain.h

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include <vector>
1818

19-
std::vector<std::string> InitialiseAddresses();
20-
2119
class CBlockFileInfo
2220
{
2321
public:
@@ -150,71 +148,6 @@ enum BlockStatus: uint32_t {
150148
BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD,
151149
};
152150

153-
/** Fluid Protocol Entry Corresponding to Each Block */
154-
class CFluidEntry
155-
{
156-
public:
157-
CAmount blockReward;
158-
CAmount dynodeReward;
159-
unsigned int dynodeRecipientCount;
160-
std::vector<std::string> fluidHistory;
161-
std::vector<std::string> fluidSovereigns;
162-
std::vector<std::string> fluidFrozenAccounts;
163-
164-
CFluidEntry() {
165-
SetNull();
166-
}
167-
168-
ADD_SERIALIZE_METHODS;
169-
template <typename Stream, typename Operation>
170-
inline void SerializationOp(Stream& s, Operation ser_action) {
171-
READWRITE(VARINT(blockReward));
172-
READWRITE(VARINT(dynodeReward));
173-
READWRITE(VARINT(dynodeRecipientCount));
174-
READWRITE(fluidHistory);
175-
READWRITE(fluidSovereigns);
176-
READWRITE(fluidFrozenAccounts);
177-
}
178-
179-
inline friend bool operator==(const CFluidEntry &a, const CFluidEntry &b) {
180-
return (
181-
a.fluidHistory == b.fluidHistory
182-
&& a.fluidSovereigns == b.fluidSovereigns
183-
&& a.blockReward == b.blockReward
184-
&& a.dynodeReward == b.dynodeReward
185-
&& a.dynodeRecipientCount == b.dynodeRecipientCount
186-
&& a.fluidFrozenAccounts == b.fluidFrozenAccounts
187-
);
188-
}
189-
190-
inline CFluidEntry operator=(const CFluidEntry &b) {
191-
fluidHistory = b.fluidHistory;
192-
fluidSovereigns = b.fluidSovereigns;
193-
blockReward = b.blockReward;
194-
dynodeReward = b.dynodeReward;
195-
dynodeRecipientCount = b.dynodeRecipientCount;
196-
fluidFrozenAccounts = b.fluidFrozenAccounts;
197-
return *this;
198-
}
199-
200-
inline friend bool operator!=(const CFluidEntry &a, const CFluidEntry &b) {
201-
return !(a == b);
202-
}
203-
204-
inline void SetNull() {
205-
fluidHistory.clear();
206-
fluidSovereigns = InitialiseAddresses();
207-
fluidFrozenAccounts.clear();
208-
blockReward = 0;
209-
dynodeReward = 0;
210-
dynodeRecipientCount = 1;
211-
}
212-
213-
inline bool IsNull() const {
214-
return (fluidHistory.empty() && fluidSovereigns == InitialiseAddresses() && fluidFrozenAccounts.empty() && blockReward == 0 && dynodeReward == 0 && dynodeRecipientCount == 1);
215-
}
216-
};
217-
218151
/** The block chain is a tree shaped structure starting with the
219152
* genesis block at the root, with each block potentially having multiple
220153
* candidates to be the next block. A blockindex may have multiple pprev pointing
@@ -269,9 +202,6 @@ class CBlockIndex
269202
//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
270203
int32_t nSequenceId;
271204

272-
//! Fluid Entry
273-
CFluidEntry fluidParams;
274-
275205
void SetNull()
276206
{
277207
phashBlock = NULL;
@@ -286,7 +216,6 @@ class CBlockIndex
286216
nChainTx = 0;
287217
nStatus = 0;
288218
nSequenceId = 0;
289-
fluidParams.SetNull();
290219

291220
nVersion = 0;
292221
hashMerkleRoot = uint256();
@@ -447,8 +376,6 @@ class CDiskBlockIndex : public CBlockIndex
447376
READWRITE(VARINT(nDataPos));
448377
if (nStatus & BLOCK_HAVE_UNDO)
449378
READWRITE(VARINT(nUndoPos));
450-
451-
READWRITE(fluidParams);
452379

453380
// block hash
454381
READWRITE(hash);

src/dbwrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ bool CDBWrapper::IsEmpty()
202202
CDBIterator::~CDBIterator() { delete piter; }
203203
bool CDBIterator::Valid() { return piter->Valid(); }
204204
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
205+
void CDBIterator::SeekToLast() { piter->SeekToLast(); }
205206
void CDBIterator::Next() { piter->Next(); }
206207

207208
namespace dbwrapper_private {

src/dbwrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class CDBIterator
133133
bool Valid();
134134

135135
void SeekToFirst();
136+
void SeekToLast();
136137

137138
template<typename K> void Seek(const K& key) {
138139
CDataStream ssKey(SER_DISK, CLIENT_VERSION);

src/dynode-payments.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "dynode-sync.h"
1111
#include "dynodeman.h"
1212
#include "policy/fees.h"
13-
#include "fluid.h"
13+
#include "fluid/fluid.h"
1414
#include "governance-classes.h"
1515
#include "messagesigner.h"
1616
#include "netfulfilledman.h"
@@ -315,8 +315,8 @@ void CDynodePayments::FillBlockPayee(CMutableTransaction& txNew, int nBlockHeigh
315315

316316
// make sure it's not filled yet
317317
txoutDynodeRet = CTxOut();
318-
319-
CAmount dynodePayment = getDynodeSubsidyWithOverride(pindexPrev->fluidParams.dynodeReward);
318+
//TODO fluid
319+
CAmount dynodePayment = GetDynodePayment(true);//getDynodeSubsidyWithOverride(pindexPrev->fluidParams.dynodeReward);
320320

321321
txoutDynodeRet = CTxOut(dynodePayment, payee);
322322
txNew.vout.push_back(txoutDynodeRet);
@@ -585,7 +585,8 @@ bool CDynodeBlockPayees::IsTransactionValid(const CTransaction& txNew)
585585
std::string strPayeesPossible = "";
586586

587587
/* Dirtiest trick in the book */
588-
CAmount nDynodePayment = getDynodeSubsidyWithOverride(chainActive.Tip()->fluidParams.dynodeReward);
588+
//TODO fluid
589+
CAmount nDynodePayment = GetDynodePayment(true); //getDynodeSubsidyWithOverride(chainActive.Tip()->fluidParams.dynodeReward);
589590

590591
//require at least DNPAYMENTS_SIGNATURES_REQUIRED signatures
591592

src/dynode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "dynode-payments.h"
1212
#include "dynode-sync.h"
1313
#include "dynodeman.h"
14-
#include "fluid.h"
14+
#include "fluid/fluid.h"
1515
#include "init.h"
1616
#include "messagesigner.h"
1717
#include "netbase.h"
@@ -301,8 +301,8 @@ void CDynode::UpdateLastPaid(const CBlockIndex *pindex, int nMaxBlocksToScanBack
301301
CBlock block;
302302
if(!ReadBlockFromDisk(block, BlockReading, Params().GetConsensus())) // shouldn't really happen
303303
continue;
304-
305-
CAmount nDynodePayment = getDynodeSubsidyWithOverride(BlockReading->fluidParams.dynodeReward);
304+
//TODO fluid.
305+
CAmount nDynodePayment = GetDynodePayment(true); //getDynodeSubsidyWithOverride(BlockReading->fluidParams.dynodeReward);
306306

307307
BOOST_FOREACH(CTxOut txout, block.vtx[0].vout)
308308
if(dnpayee == txout.scriptPubKey && nDynodePayment == txout.nValue) {

src/fluid.cpp renamed to src/fluid/fluid.cpp

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,41 @@ CFluid fluid;
2323
extern CWallet* pwalletMain;
2424
#endif //ENABLE_WALLET
2525

26-
bool IsTransactionFluid(CScript txOut) {
26+
bool IsTransactionFluid(const CScript& txOut) {
2727
return (txOut.IsProtocolInstruction(MINT_TX)
2828
|| txOut.IsProtocolInstruction(DYNODE_MODFIY_TX)
2929
|| txOut.IsProtocolInstruction(MINING_MODIFY_TX)
3030
);
3131
}
3232

33+
bool IsTransactionFluid(const CTransaction& tx, CScript& fluidScript)
34+
{
35+
for (const CTxOut& txout : tx.vout) {
36+
CScript txOut = txout.scriptPubKey;
37+
if (IsTransactionFluid(txOut)) {
38+
fluidScript = txOut;
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
int GetFluidOpCode(const CScript& fluidScript)
46+
{
47+
if (fluidScript.IsProtocolInstruction(MINT_TX)) {
48+
return OP_MINT;
49+
}
50+
else if (fluidScript.IsProtocolInstruction(DYNODE_MODFIY_TX))
51+
{
52+
return OP_REWARD_DYNODE;
53+
}
54+
else if (fluidScript.IsProtocolInstruction(MINING_MODIFY_TX))
55+
{
56+
return OP_REWARD_MINING;
57+
}
58+
return 0;
59+
}
60+
3361
/** Initialise sovereign identities that are able to run fluid commands */
3462
std::vector<std::pair<std::string, CDynamicAddress>> CFluidParameters::InitialiseSovereignIdentities() {
3563
std::vector<std::pair<std::string, CDynamicAddress>> x;
@@ -57,6 +85,11 @@ std::vector<std::pair<std::string, CDynamicAddress>> CFluidParameters::Initialis
5785
return x;
5886
}
5987

88+
std::vector<std::string> InitialiseAddresses() {
89+
CFluidParameters params;
90+
return params.InitialiseAddresses();
91+
}
92+
6093
/** Checks if any given address is a current master key (invoked by RPC) */
6194
bool CFluid::IsGivenKeyMaster(CDynamicAddress inputKey) {
6295
if (!inputKey.IsValid()) {
@@ -67,8 +100,11 @@ bool CFluid::IsGivenKeyMaster(CDynamicAddress inputKey) {
67100
GetLastBlockIndex(chainActive.Tip());
68101
CBlockIndex* pindex = chainActive.Tip();
69102

70-
if (pindex != NULL)
71-
fluidSovereigns = pindex->fluidParams.fluidSovereigns;
103+
if (pindex != NULL) {
104+
//TODO fluid
105+
fluidSovereigns = InitialiseAddresses(); //pindex->fluidParams.fluidSovereigns;
106+
}
107+
72108
else
73109
fluidSovereigns = InitialiseAddresses();
74110

@@ -176,8 +212,10 @@ bool CFluid::CheckIfQuorumExists(const std::string consentToken, std::string &me
176212
GetLastBlockIndex(chainActive.Tip());
177213
CBlockIndex* pindex = chainActive.Tip();
178214

179-
if (pindex != NULL)
180-
fluidSovereigns = pindex->fluidParams.fluidSovereigns;
215+
if (pindex != NULL) {
216+
//TODO fluid
217+
fluidSovereigns = InitialiseAddresses(); //pindex->fluidParams.fluidSovereigns;
218+
}
181219
else
182220
fluidSovereigns = InitialiseAddresses();
183221

@@ -477,19 +515,24 @@ void CFluid::AddFluidTransactionsToRecord(const CBlockIndex* pblockindex, std::v
477515

478516
/* Check if transaction exists in record */
479517
bool CFluid::CheckTransactionInRecord(CScript fluidInstruction, CBlockIndex* pindex) {
480-
std::string verificationString;
481-
CFluidEntry fluidIndex;
482-
if (chainActive.Height() <= fluid.FLUID_ACTIVATE_HEIGHT)
483-
return false;
484-
else if (pindex == nullptr) {
485-
GetLastBlockIndex(chainActive.Tip());
486-
fluidIndex = chainActive.Tip()->fluidParams;
487-
} else
488-
fluidIndex = pindex->fluidParams;
489-
490-
std::vector<std::string> transactionRecord = fluidIndex.fluidHistory;
491-
492518
if (IsTransactionFluid(fluidInstruction)) {
519+
std::string verificationString;
520+
//TODO fluid
521+
/*
522+
CFluidEntry fluidIndex;
523+
if (chainActive.Height() <= fluid.FLUID_ACTIVATE_HEIGHT) {
524+
return false;
525+
}
526+
else if (pindex == nullptr) {
527+
GetLastBlockIndex(chainActive.Tip());
528+
//TODO fluid
529+
//fluidIndex = chainActive.Tip()->fluidParams;
530+
} else {
531+
//TODO fluid
532+
//fluidIndex = pindex->fluidParams;
533+
}
534+
*/
535+
std::vector<std::string> transactionRecord;//fluidIndex.fluidHistory;
493536
verificationString = ScriptToAsmStr(fluidInstruction);
494537
std::string verificationWithoutOpCode = GetRidOfScriptStatement(verificationString);
495538
std::string message;
@@ -646,4 +689,20 @@ bool CFluid::CheckTransactionToBlock(const CTransaction& transaction, const uint
646689
}
647690

648691
return true;
692+
}
693+
694+
std::vector<unsigned char> CharVectorFromString(const std::string& str)
695+
{
696+
return std::vector<unsigned char>(str.begin(), str.end());
697+
}
698+
699+
std::string StringFromCharVector(const std::vector<unsigned char>& vch)
700+
{
701+
std::string strReturn;
702+
std::vector<unsigned char>::const_iterator vi = vch.begin();
703+
while (vi != vch.end()) {
704+
strReturn += (char) (*vi);
705+
vi++;
706+
}
707+
return strReturn;
649708
}

0 commit comments

Comments
 (0)