Skip to content

Commit

Permalink
Lazy rewards redistribution (#279)
Browse files Browse the repository at this point in the history
* Split custom tx processing into parsing and applying

Signed-off-by: Anthony Fieroni <[email protected]>

* Prevent copies in serialize

Signed-off-by: Anthony Fieroni <[email protected]>

* Adopt pool tables to have historical records
Per account rewards calculation

Signed-off-by: Anthony Fieroni <[email protected]>

* Do not redistribute rewards per block

Signed-off-by: Anthony Fieroni <[email protected]>

* Fix lint circular dependency

Signed-off-by: Anthony Fieroni <[email protected]>

* Introduce db version check at runtime

Signed-off-by: Anthony Fieroni <[email protected]>

* Use bidirectional storage iterators

Signed-off-by: Anthony Fieroni <[email protected]>

* Fix pool id serialization in update poolpair tx

Signed-off-by: Anthony Fieroni <[email protected]>

* Update rewards on every block as drop-in replacement

Signed-off-by: Anthony Fieroni <[email protected]>

* Split pool historical records

* Added EunosHeight

* Fork out rewards redistribution per block

Signed-off-by: Anthony Fieroni <[email protected]>

* Extend pool swap functional test to Eunos hieght

Signed-off-by: Anthony Fieroni <[email protected]>

* Minimize undo data size

Signed-off-by: Anthony Fieroni <[email protected]>

* Prune undo data prior last checkpoint

Signed-off-by: Anthony Fieroni <[email protected]>

* Prevent database fragmentation

Signed-off-by: Anthony Fieroni <[email protected]>

* Make account history own database (#323)

Signed-off-by: Anthony Fieroni <[email protected]>

Co-authored-by: surangap <[email protected]>
Co-authored-by: Ahmed Hilali <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2021
1 parent 776a3a8 commit 9a0e59c
Show file tree
Hide file tree
Showing 63 changed files with 2,917 additions and 2,486 deletions.
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ DEFI_CORE_H = \
masternodes/mn_checks.h \
masternodes/mn_rpc.h \
masternodes/res.h \
masternodes/rewardhistoryold.h \
masternodes/tokens.h \
masternodes/poolpairs.h \
masternodes/undo.h \
Expand Down Expand Up @@ -374,8 +373,9 @@ libdefi_server_a_SOURCES = \
masternodes/masternodes.cpp \
masternodes/mn_checks.cpp \
masternodes/mn_rpc.cpp \
masternodes/rpc_masternodes.cpp \
masternodes/rpc_accounts.cpp \
masternodes/rpc_customtx.cpp \
masternodes/rpc_masternodes.cpp \
masternodes/rpc_tokens.cpp \
masternodes/rpc_poolpair.cpp \
masternodes/tokens.cpp \
Expand Down
6 changes: 3 additions & 3 deletions src/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct DCT_ID {

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(v);
READWRITE(VARINT(v));
}
};

Expand Down Expand Up @@ -125,7 +125,7 @@ struct CTokenAmount { // simple std::pair is less informative
// add
auto sumRes = SafeAdd(this->nValue, amount);
if (!sumRes.ok) {
return sumRes.res();
return std::move(sumRes);
}
this->nValue = *sumRes.val;
return Res::Ok();
Expand Down Expand Up @@ -162,7 +162,7 @@ struct CTokenAmount { // simple std::pair is less informative

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT(nTokenId.v));
READWRITE(nTokenId);
READWRITE(nValue);
}

Expand Down
11 changes: 11 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,17 @@ void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
consensus.DakotaCrescentHeight = static_cast<int>(height);
}

if (gArgs.IsArgSet("-eunosheight")) {
int64_t height = gArgs.GetArg("-eunosheight", consensus.EunosHeight);
if (height < -1 || height >= std::numeric_limits<int>::max()) {
throw std::runtime_error(strprintf("Activation height %ld for Eunos is out of valid range. Use -1 to disable Eunos features.", height));
} else if (height == -1) {
LogPrintf("Eunos disabled for testing\n");
height = std::numeric_limits<int>::max();
}
consensus.EunosHeight = static_cast<int>(height);
}

if (!args.IsArgSet("-vbparams")) return;

for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
Expand Down
4 changes: 4 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ struct Params {
};
PoS pos;

uint32_t blocksPerDay() const {
static const uint32_t blocks = 60 * 60 * 24 / pos.nTargetSpacing;
return blocks;
}
/**
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
* (nTargetTimespan / nTargetSpacing) which is also used for BIP9 deployments.
Expand Down
67 changes: 20 additions & 47 deletions src/consensus/tx_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,72 +65,45 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fChe
return true;
}

bool IsCriminalProofTx(CTransaction const & tx, std::vector<unsigned char> & metadata)
bool ParseScriptByMarker(CScript const & script,
const std::vector<unsigned char> & marker,
std::vector<unsigned char> & metadata)
{
if (!tx.IsCoinBase() || tx.vout.size() != 1 || tx.vout[0].nValue != 0) {
return false;
}
CScript const & memo = tx.vout[0].scriptPubKey;
CScript::const_iterator pc = memo.begin();
opcodetype opcode;
if (!memo.GetOp(pc, opcode) || opcode != OP_RETURN) {
auto pc = script.begin();
if (!script.GetOp(pc, opcode) || opcode != OP_RETURN) {
return false;
}
if (!memo.GetOp(pc, opcode, metadata) ||
(opcode > OP_PUSHDATA1 &&
opcode != OP_PUSHDATA2 &&
opcode != OP_PUSHDATA4) ||
metadata.size() < DfCriminalTxMarker.size() + 1 ||
memcmp(&metadata[0], &DfCriminalTxMarker[0], DfCriminalTxMarker.size()) != 0) {
if (!script.GetOp(pc, opcode, metadata)
|| (opcode > OP_PUSHDATA1 && opcode != OP_PUSHDATA2 && opcode != OP_PUSHDATA4)
|| metadata.size() < marker.size() + 1
|| memcmp(&metadata[0], &marker[0], marker.size()) != 0) {
return false;
}
metadata.erase(metadata.begin(), metadata.begin() + DfCriminalTxMarker.size());
metadata.erase(metadata.begin(), metadata.begin() + marker.size());
return true;
}

bool IsAnchorRewardTx(CTransaction const & tx, std::vector<unsigned char> & metadata)
bool IsCriminalProofTx(CTransaction const & tx, std::vector<unsigned char> & metadata)
{
if (!tx.IsCoinBase() || tx.vout.size() != 2 || tx.vout[0].nValue != 0) {
return false;
}
CScript const & memo = tx.vout[0].scriptPubKey;
CScript::const_iterator pc = memo.begin();
opcodetype opcode;
if (!memo.GetOp(pc, opcode) || opcode != OP_RETURN) {
if (!tx.IsCoinBase() || tx.vout.size() != 1 || tx.vout[0].nValue != 0) {
return false;
}
if (!memo.GetOp(pc, opcode, metadata) ||
(opcode > OP_PUSHDATA1 &&
opcode != OP_PUSHDATA2 &&
opcode != OP_PUSHDATA4) ||
metadata.size() < DfAnchorFinalizeTxMarker.size() + 1 ||
memcmp(&metadata[0], &DfAnchorFinalizeTxMarker[0], DfAnchorFinalizeTxMarker.size()) != 0) {
return ParseScriptByMarker(tx.vout[0].scriptPubKey, DfCriminalTxMarker, metadata);
}

bool IsAnchorRewardTx(CTransaction const & tx, std::vector<unsigned char> & metadata)
{
if (!tx.IsCoinBase() || tx.vout.size() != 2 || tx.vout[0].nValue != 0) {
return false;
}
metadata.erase(metadata.begin(), metadata.begin() + DfAnchorFinalizeTxMarker.size());
return true;
return ParseScriptByMarker(tx.vout[0].scriptPubKey, DfAnchorFinalizeTxMarker, metadata);
}

bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata)
{
if (!tx.IsCoinBase() || tx.vout.size() != 2 || tx.vout[0].nValue != 0) {
return false;
}
CScript const & memo = tx.vout[0].scriptPubKey;
CScript::const_iterator pc = memo.begin();
opcodetype opcode;
if (!memo.GetOp(pc, opcode) || opcode != OP_RETURN) {
return false;
}
if (!memo.GetOp(pc, opcode, metadata) ||
(opcode > OP_PUSHDATA1 &&
opcode != OP_PUSHDATA2 &&
opcode != OP_PUSHDATA4) ||
metadata.size() < DfAnchorFinalizeTxMarkerPlus.size() + 1 ||
memcmp(&metadata[0], &DfAnchorFinalizeTxMarkerPlus[0], DfAnchorFinalizeTxMarkerPlus.size()) != 0) {
return false;
}
metadata.erase(metadata.begin(), metadata.begin() + DfAnchorFinalizeTxMarkerPlus.size());
return true;
return ParseScriptByMarker(tx.vout[0].scriptPubKey, DfAnchorFinalizeTxMarkerPlus, metadata);
}

5 changes: 4 additions & 1 deletion src/consensus/tx_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ extern const std::vector<unsigned char> DfCriminalTxMarker;
extern const std::vector<unsigned char> DfAnchorFinalizeTxMarker;
extern const std::vector<unsigned char> DfAnchorFinalizeTxMarkerPlus;

class CScript;
class CTransaction;
class CValidationState;

bool CheckTransaction(const CTransaction& tx, CValidationState& state, bool fCheckDuplicateInputs=true);

/// moved here (!!) due to strange linker errors under mac/win builds
bool ParseScriptByMarker(CScript const & script,
const std::vector<unsigned char> & marker,
std::vector<unsigned char> & metadata);
bool IsCriminalProofTx(CTransaction const & tx, std::vector<unsigned char> & metadata);
bool IsAnchorRewardTx(CTransaction const & tx, std::vector<unsigned char> & metadata);
bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata);
Expand Down
3 changes: 2 additions & 1 deletion src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
const auto txType = GuessCustomTxType(tx, dummy);

if (NotAllowedToFail(txType, nSpendHeight)) {
auto res = ApplyCustomTx(const_cast<CCustomCSView&>(*mnview), inputs, tx, chainparams.GetConsensus(), nSpendHeight, 0, uint64_t{0}, true); // note for 'isCheck == true' here; 'zero' for txn is dummy value
CCustomCSView discardCache(const_cast<CCustomCSView&>(*mnview));
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg);
}
Expand Down
1 change: 1 addition & 0 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ CDBIterator::~CDBIterator() { delete piter; }
bool CDBIterator::Valid() const { return piter->Valid(); }
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
void CDBIterator::Next() { piter->Next(); }
void CDBIterator::Prev() { piter->Prev(); }

namespace dbwrapper_private {

Expand Down
1 change: 1 addition & 0 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class CDBIterator
}

void Next();
void Prev();

template<typename K> bool GetKey(K& key) {
leveldb::Slice slKey = piter->key();
Expand Down
Loading

0 comments on commit 9a0e59c

Please sign in to comment.