From 5d118b3aefe65d78f0c4aea2945674b035e6c587 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 12 Jun 2019 20:32:21 -0400 Subject: [PATCH] reconcile working for integrals, strs, and bools using enable_if --- src/daemon/MoneroDaemon.cpp | 20 +++++++++++++++-- src/utils/MoneroUtils.h | 44 ++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/daemon/MoneroDaemon.cpp b/src/daemon/MoneroDaemon.cpp index 73c70c7..2edc048 100644 --- a/src/daemon/MoneroDaemon.cpp +++ b/src/daemon/MoneroDaemon.cpp @@ -101,8 +101,24 @@ namespace monero { void MoneroBlockHeader::merge(const MoneroBlockHeader& header) { cout << "MoneroBlockHeader::merge()" << endl; if (this == &header) return; - id = MoneroUtils::reconcile(id, header.id, boost::none, boost::none, boost::none); - throw runtime_error("Not implemented"); + id = MoneroUtils::reconcile(id, header.id); + height = MoneroUtils::reconcile(height, header.height, boost::none, boost::none, true); // height can increase + timestamp = MoneroUtils::reconcile(timestamp, header.timestamp, boost::none, boost::none, true); // timestamp can increase + size = MoneroUtils::reconcile(size, header.size); + weight = MoneroUtils::reconcile(weight, header.weight); + longTermWeight = MoneroUtils::reconcile(longTermWeight, header.longTermWeight); + depth = MoneroUtils::reconcile(depth, header.depth); + difficulty = MoneroUtils::reconcile(difficulty, header.difficulty); + cumulativeDifficulty = MoneroUtils::reconcile(cumulativeDifficulty, header.cumulativeDifficulty); + majorVersion = MoneroUtils::reconcile(majorVersion, header.majorVersion); + minorVersion = MoneroUtils::reconcile(minorVersion, header.minorVersion); + nonce = MoneroUtils::reconcile(nonce, header.nonce); + coinbaseTxId = MoneroUtils::reconcile(coinbaseTxId, header.coinbaseTxId); + numTxs = MoneroUtils::reconcile(numTxs, header.numTxs); + orphanStatus = MoneroUtils::reconcile(orphanStatus, header.orphanStatus); + prevId = MoneroUtils::reconcile(prevId, header.prevId); + reward = MoneroUtils::reconcile(reward, header.reward); + powHash = MoneroUtils::reconcile(powHash, header.powHash); } void MoneroBlock::merge(const MoneroBlock& block) { diff --git a/src/utils/MoneroUtils.h b/src/utils/MoneroUtils.h index fa1caeb..e4715c1 100644 --- a/src/utils/MoneroUtils.h +++ b/src/utils/MoneroUtils.h @@ -95,7 +95,30 @@ namespace MoneroUtils return ss.str(); } - template + // TODO: refactor common template code + + template ::value, T>::type* = nullptr> + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { + cout << "reconcile(" << val1 << ", " << val2 << ")" << endl; + + // check for equality + if (val1 == val2) return val1; + + // resolve one value none + if (val1 == boost::none || val2 == boost::none) { + if (resolveDefined == false) return boost::none; // TODO: boost::optional equality comparitor wokrs like this? + else return val1 == boost::none ? val2 : val1; + } + + throw runtime_error("Cannot reconcile strings"); + } + + template ::value, T>::type* = nullptr> + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { + return reconcile(val1, val2, boost::none, boost::none, boost::none); + } + + template ::value, T>::type* = nullptr> boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { cout << "reconcile(" << val1 << ", " << val2 << ")" << endl; @@ -114,16 +137,16 @@ namespace MoneroUtils } // throw runtime_error("Cannot reconcile values " + val1 + " and " + val2 + " with config: [" + resolveDefined + ", " + resolveTrue + ", " + resolveMax + "]", val1, val2); - throw runtime_error("Cannot reconcile non-booleans"); + throw runtime_error("Cannot reconcile integrals"); } - template + template ::value, T>::type* = nullptr> boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { return reconcile(val1, val2, boost::none, boost::none, boost::none); } - template ::value, T>::type* = nullptr> - boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { + template ::value, T>::type> + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2, boost::optional resolveDefined, boost::optional resolveTrue, boost::optional resolveMax) { cout << "reconcile(" << val1 << ", " << val2 << ")" << endl; // check for equality @@ -139,15 +162,20 @@ namespace MoneroUtils if (resolveTrue != boost::none) { return val1 == resolveTrue ? val1 : val2; // if resolve true, return true, else return false } else { - throw runtime_error("Cannot reconcile boolean values"); + throw runtime_error("Cannot reconcile booleans"); } throw runtime_error("Cannot reconcile booleans"); } - template ::value, T>::type* = nullptr> - boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { + template ::value, T>::type> + boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { return reconcile(val1, val2, boost::none, boost::none, boost::none); } + +// template +// boost::optional reconcile(const boost::optional& val1, const boost::optional& val2) { +// return reconcile(val1, val2, boost::none, boost::none, boost::none); +// } } #endif /* MoneroUtils_h */