Skip to content

Commit f033503

Browse files
committed
[Fluid] Implement new fluid databases
1 parent f4b8623 commit f033503

16 files changed

+168
-105
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ DYNAMIC_CORE_H = \
105105
dynodeconfig.h \
106106
flat-database.h \
107107
fluid/fluid.h \
108+
fluid/fluiddb.h \
108109
fluid/fluiddynode.h \
109110
fluid/fluidmining.h \
110111
fluid/fluidmint.h \
@@ -229,6 +230,7 @@ libdynamic_server_a_SOURCES = \
229230
dynodeconfig.cpp \
230231
dynodeman.cpp \
231232
fluid/fluid.cpp \
233+
fluid/fluiddb.cpp \
232234
fluid/fluiddynode.cpp \
233235
fluid/fluidmining.cpp \
234236
fluid/fluidmint.cpp \

src/dynode-payments.cpp

Lines changed: 3 additions & 7 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/fluid.h"
13+
#include "fluid/fluiddb.h"
1414
#include "governance-classes.h"
1515
#include "messagesigner.h"
1616
#include "netfulfilledman.h"
@@ -315,8 +315,7 @@ void CDynodePayments::FillBlockPayee(CMutableTransaction& txNew, int nBlockHeigh
315315

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

321320
txoutDynodeRet = CTxOut(dynodePayment, payee);
322321
txNew.vout.push_back(txoutDynodeRet);
@@ -583,10 +582,7 @@ bool CDynodeBlockPayees::IsTransactionValid(const CTransaction& txNew)
583582

584583
int nMaxSignatures = 0;
585584
std::string strPayeesPossible = "";
586-
587-
/* Dirtiest trick in the book */
588-
//TODO fluid
589-
CAmount nDynodePayment = GetDynodePayment(true); //getDynodeSubsidyWithOverride(chainActive.Tip()->fluidParams.dynodeReward);
585+
CAmount nDynodePayment = GetFluidDynodeReward();
590586

591587
//require at least DNPAYMENTS_SIGNATURES_REQUIRED signatures
592588

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/fluid.h"
14+
#include "fluid/fluiddb.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-
//TODO fluid.
305-
CAmount nDynodePayment = GetDynodePayment(true); //getDynodeSubsidyWithOverride(BlockReading->fluidParams.dynodeReward);
304+
305+
CAmount nDynodePayment = GetFluidDynodeReward();
306306

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

src/fluid/fluid.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -575,61 +575,39 @@ bool CFluid::InsertTransactionToRecord(CScript fluidInstruction, std::vector<std
575575
return false;
576576
}
577577

578-
CAmount GetPoWBlockPayment(const int& nHeight)
578+
CAmount GetStandardPoWBlockPayment()
579579
{
580580
if (chainActive.Height() == 0) {
581581
CAmount nSubsidy = INITIAL_SUPERBLOCK_PAYMENT;
582-
LogPrint("superblock creation", "GetPoWBlockPayment() : create=%s nSubsidy=%d\n", FormatMoney(nSubsidy), nSubsidy);
582+
LogPrint("superblock creation", "GetStandardPoWBlockPayment() : create=%s nSubsidy=%d\n", FormatMoney(nSubsidy), nSubsidy);
583583
return nSubsidy;
584584
}
585585
else if (chainActive.Height() >= 1 && chainActive.Height() <= Params().GetConsensus().nRewardsStart) {
586-
LogPrint("zero-reward block creation", "GetPoWBlockPayment() : create=%s nSubsidy=%d\n", FormatMoney(BLOCKCHAIN_INIT_REWARD), BLOCKCHAIN_INIT_REWARD);
586+
LogPrint("zero-reward block creation", "GetStandardPoWBlockPayment() : create=%s nSubsidy=%d\n", FormatMoney(BLOCKCHAIN_INIT_REWARD), BLOCKCHAIN_INIT_REWARD);
587587
return BLOCKCHAIN_INIT_REWARD; // Burn transaction fees
588588
}
589589
else if (chainActive.Height() > Params().GetConsensus().nRewardsStart) {
590-
LogPrint("creation", "GetPoWBlockPayment() : create=%s PoW Reward=%d\n", FormatMoney(PHASE_1_POW_REWARD), PHASE_1_POW_REWARD);
590+
LogPrint("creation", "GetStandardPoWBlockPayment() : create=%s PoW Reward=%d\n", FormatMoney(PHASE_1_POW_REWARD), PHASE_1_POW_REWARD);
591591
return PHASE_1_POW_REWARD; // 1 DYN and burn transaction fees
592592
}
593593
else
594594
return BLOCKCHAIN_INIT_REWARD; // Burn transaction fees
595595
}
596596

597-
CAmount GetDynodePayment(bool fDynode)
597+
CAmount GetStandardDynodePayment()
598598
{
599-
if (fDynode && chainActive.Height() > Params().GetConsensus().nDynodePaymentsStartBlock && chainActive.Height() < Params().GetConsensus().nUpdateDiffAlgoHeight) {
600-
LogPrint("creation", "GetDynodePayment() : create=%s DN Payment=%d\n", FormatMoney(PHASE_1_DYNODE_PAYMENT), PHASE_1_DYNODE_PAYMENT);
599+
if (chainActive.Height() > Params().GetConsensus().nDynodePaymentsStartBlock && chainActive.Height() < Params().GetConsensus().nUpdateDiffAlgoHeight) {
600+
LogPrint("creation", "GetStandardDynodePayment() : create=%s DN Payment=%d\n", FormatMoney(PHASE_1_DYNODE_PAYMENT), PHASE_1_DYNODE_PAYMENT);
601601
return PHASE_1_DYNODE_PAYMENT; // 0.382 DYN
602602
}
603-
else if (fDynode && chainActive.Height() > Params().GetConsensus().nDynodePaymentsStartBlock && chainActive.Height() >= Params().GetConsensus().nUpdateDiffAlgoHeight) {
604-
LogPrint("creation", "GetDynodePayment() : create=%s DN Payment=%d\n", FormatMoney(PHASE_2_DYNODE_PAYMENT), PHASE_2_DYNODE_PAYMENT);
603+
else if (chainActive.Height() > Params().GetConsensus().nDynodePaymentsStartBlock && chainActive.Height() >= Params().GetConsensus().nUpdateDiffAlgoHeight) {
604+
LogPrint("creation", "GetStandardDynodePayment() : create=%s DN Payment=%d\n", FormatMoney(PHASE_2_DYNODE_PAYMENT), PHASE_2_DYNODE_PAYMENT);
605605
return PHASE_2_DYNODE_PAYMENT; // 1.618 DYN
606606
}
607-
else if ((fDynode && !fDynode) && chainActive.Height() <= Params().GetConsensus().nDynodePaymentsStartBlock) {
608-
LogPrint("creation", "GetDynodePayment() : create=%s DN Payment=%d\n", FormatMoney(BLOCKCHAIN_INIT_REWARD), BLOCKCHAIN_INIT_REWARD);
609-
return BLOCKCHAIN_INIT_REWARD;
610-
}
611607
else
612608
return BLOCKCHAIN_INIT_REWARD;
613609
}
614610

615-
/** Passover code that will act as a switch to check if override did occur for Proof of Work Rewards **/
616-
CAmount getBlockSubsidyWithOverride(const int& nHeight, CAmount lastOverrideCommand) {
617-
if (lastOverrideCommand != 0) {
618-
return lastOverrideCommand;
619-
} else {
620-
return GetPoWBlockPayment(nHeight);
621-
}
622-
}
623-
624-
/** Passover code that will act as a switch to check if override did occur for Dynode Rewards **/
625-
CAmount getDynodeSubsidyWithOverride(CAmount lastOverrideCommand, bool fDynode) {
626-
if (lastOverrideCommand != 0) {
627-
return lastOverrideCommand;
628-
} else {
629-
return GetDynodePayment(fDynode);
630-
}
631-
}
632-
633611
bool CFluid::ValidationProcesses(CValidationState& state, CScript txOut, CAmount txValue) {
634612
std::string message;
635613
CAmount mintAmount;

src/fluid/fluid.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2016-2018 Duality Blockchain Solutions Developers
22

33
#ifndef FLUID_PROTOCOL_H
44
#define FLUID_PROTOCOL_H
@@ -77,12 +77,8 @@ class CFluid : public CFluidParameters, public COperations {
7777
};
7878

7979
/** Standard Reward Payment Determination Functions */
80-
CAmount GetPoWBlockPayment(const int& nHeight);
81-
CAmount GetDynodePayment(bool fDynode = true);
82-
83-
/** Override Logic Switch for Reward Payment Determination Functions */
84-
CAmount getBlockSubsidyWithOverride(const int& nHeight, CAmount lastOverrideCommand);
85-
CAmount getDynodeSubsidyWithOverride(CAmount lastOverrideCommand, bool fDynode = true);
80+
CAmount GetStandardPoWBlockPayment();
81+
CAmount GetStandardDynodePayment();
8682

8783
void BuildFluidInformationIndex(CBlockIndex* pindex, CAmount &nExpectedBlockValue, bool fDynodePaid);
8884
bool IsTransactionFluid(const CScript& txOut);

src/fluid/fluiddb.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
2+
3+
#include "fluiddb.h"
4+
5+
#include "fluid.h"
6+
#include "fluiddynode.h"
7+
#include "fluidmining.h"
8+
#include "fluidmint.h"
9+
10+
CAmount GetFluidDynodeReward()
11+
{
12+
if (!CheckFluidDynodeDB())
13+
return GetStandardDynodePayment();
14+
15+
CFluidDynode lastDynodeRecord;
16+
if (!pFluidDynodeDB->GetLastFluidDynodeRecord(lastDynodeRecord)) {
17+
return GetStandardDynodePayment();
18+
}
19+
return lastDynodeRecord.DynodeReward;
20+
}
21+
22+
CAmount GetFluidMiningReward()
23+
{
24+
if (!CheckFluidMiningDB())
25+
return GetStandardPoWBlockPayment();
26+
27+
CFluidMining lastMiningRecord;
28+
if (!pFluidMiningDB->GetLastFluidMiningRecord(lastMiningRecord)) {
29+
return GetStandardPoWBlockPayment();
30+
}
31+
return lastMiningRecord.MiningReward;
32+
}
33+
34+
bool GetMintingInstructions(const int nHeight, CFluidMint& fluidMint)
35+
{
36+
if (!CheckFluidMintDB())
37+
return false;
38+
39+
CFluidMint getFluidMint;
40+
if (!pFluidMintDB->GetLastFluidMintRecord(getFluidMint)) {
41+
return false;
42+
}
43+
44+
if ((int)getFluidMint.nHeight == (nHeight -1))
45+
{
46+
fluidMint = getFluidMint;
47+
return true;
48+
}
49+
return false;
50+
}

src/fluid/fluiddb.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
2+
3+
#ifndef FLUID_DB_H
4+
#define FLUID_DB_H
5+
6+
#include "amount.h"
7+
8+
class CFluidMint;
9+
10+
CAmount GetFluidDynodeReward();
11+
CAmount GetFluidMiningReward();
12+
bool GetMintingInstructions(const int nHeight, CFluidMint& fluidMint);
13+
14+
#endif // FLUID_DYNODE_H

src/fluid/fluiddynode.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
22

33

44
#include "fluiddynode.h"
@@ -114,7 +114,14 @@ bool CFluidDynodeDB::GetLastFluidDynodeRecord(CFluidDynode& entry)
114114
LOCK(cs_fluid_dynode);
115115
std::unique_ptr<CDBIterator> pcursor(NewIterator());
116116
pcursor->SeekToLast();
117-
pcursor->GetValue(entry);
117+
if (pcursor->Valid()) {
118+
try {
119+
pcursor->GetValue(entry);
120+
}
121+
catch (std::exception& e) {
122+
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
123+
}
124+
}
118125
return true;
119126
}
120127

src/fluid/fluiddynode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
22

33
#ifndef FLUID_DYNODE_H
44
#define FLUID_DYNODE_H

src/fluid/fluidmining.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
22

33

44
#include "fluidmining.h"
@@ -114,7 +114,14 @@ bool CFluidMiningDB::GetLastFluidMiningRecord(CFluidMining& entry)
114114
LOCK(cs_fluid_mining);
115115
std::unique_ptr<CDBIterator> pcursor(NewIterator());
116116
pcursor->SeekToLast();
117-
pcursor->GetValue(entry);
117+
if (pcursor->Valid()) {
118+
try {
119+
pcursor->GetValue(entry);
120+
}
121+
catch (std::exception& e) {
122+
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
123+
}
124+
}
118125
return true;
119126
}
120127

src/fluid/fluidmining.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
22

33
#ifndef FLUID_MINER
44
#define FLUID_MINER
@@ -102,6 +102,7 @@ class CFluidMiningDB : public CDBWrapper {
102102
bool GetFluidMiningData(const CScript& scriptPubKey, CFluidMining& entry);
103103
bool GetFluidMiningData(const CTransaction& tx, CFluidMining& entry, int& nOut);
104104
bool CheckFluidMiningDB();
105+
CAmount GetFluidMiningReward();
105106

106107
extern CFluidMiningDB *pFluidMiningDB;
107108

src/fluid/fluidmint.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "fluidmint.h"
55

6+
#include "base58.h"
67
#include "core_io.h"
78
#include "fluid.h"
89
#include "operations.h"
@@ -96,6 +97,11 @@ void CFluidMint::Serialize(std::vector<unsigned char>& vchData) {
9697
vchData = std::vector<unsigned char>(dsFluidOp.begin(), dsFluidOp.end());
9798
}
9899

100+
CDynamicAddress CFluidMint::GetDestinationAddress() const
101+
{
102+
return CDynamicAddress(StringFromCharVector(DestinationAddress));
103+
}
104+
99105
CFluidMintDB::CFluidMintDB(size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate) : CDBWrapper(GetDataDir() / "fluid-mint", nCacheSize, fMemory, fWipe, obfuscate)
100106
{
101107
}
@@ -117,7 +123,15 @@ bool CFluidMintDB::GetLastFluidMintRecord(CFluidMint& entry)
117123
LOCK(cs_fluid_mint);
118124
std::unique_ptr<CDBIterator> pcursor(NewIterator());
119125
pcursor->SeekToLast();
120-
pcursor->GetValue(entry);
126+
if (pcursor->Valid()) {
127+
try {
128+
pcursor->GetValue(entry);
129+
}
130+
catch (std::exception& e) {
131+
return error("%s() : deserialize error", __PRETTY_FUNCTION__);
132+
}
133+
}
134+
LogPrintf("GetLastFluidMintRecord 4\n");
121135
return true;
122136
}
123137

src/fluid/fluidmint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Duality Blockchain Solutions Developers
1+
// Copyright (c) 2018 Duality Blockchain Solutions Developers
22

33
#ifndef FLUID_MINT_H
44
#define FLUID_MINT_H
@@ -10,6 +10,7 @@
1010
#include "sync.h"
1111
#include "uint256.h"
1212

13+
class CDynamicAddress;
1314
class CScript;
1415
class CTransaction;
1516

@@ -91,6 +92,7 @@ class CFluidMint {
9192
bool UnserializeFromTx(const CTransaction& tx);
9293
bool UnserializeFromScript(const CScript& fluidScript);
9394
void Serialize(std::vector<unsigned char>& vchData);
95+
CDynamicAddress GetDestinationAddress() const;
9496
};
9597

9698
static CCriticalSection cs_fluid_mint;

0 commit comments

Comments
 (0)