define output stack;

cleanup daemon model impl
This commit is contained in:
woodser
2019-06-21 17:32:51 -04:00
parent 8fadd95f75
commit f3c095ae6a
6 changed files with 132 additions and 86 deletions
+83 -72
View File
@@ -6,8 +6,6 @@
/**
* Public library interface.
*
* TODO: separate per model class like wallet (cleanup)
*/
namespace monero {
@@ -15,12 +13,14 @@ namespace monero {
const string MoneroTx::DEFAULT_PAYMENT_ID = string("0000000000000000");
// -------------------------- MODEL SERIALIZATION ---------------------------
// ------------------------- SERIALIZABLE STRUCT ----------------------------
string SerializableStruct::serialize() const {
return MoneroUtils::serialize(toPropertyTree());
}
// ------------------------- MONERO BLOCK HEADER ----------------------------
boost::property_tree::ptree MoneroBlockHeader::toPropertyTree() const {
//cout << "MoneroBlockHeader::toPropertyTree(block)" << endl;
boost::property_tree::ptree node;
@@ -45,6 +45,31 @@ namespace monero {
return node;
}
void MoneroBlockHeader::merge(const shared_ptr<MoneroBlockHeader>& self, const shared_ptr<MoneroBlockHeader>& other) {
if (this != self.get()) throw runtime_error("this != self");
if (self == other) return;
id = MoneroUtils::reconcile(id, other->id);
height = MoneroUtils::reconcile(height, other->height, boost::none, boost::none, true); // height can increase
timestamp = MoneroUtils::reconcile(timestamp, other->timestamp, boost::none, boost::none, true); // timestamp can increase
size = MoneroUtils::reconcile(size, other->size);
weight = MoneroUtils::reconcile(weight, other->weight);
longTermWeight = MoneroUtils::reconcile(longTermWeight, other->longTermWeight);
depth = MoneroUtils::reconcile(depth, other->depth);
difficulty = MoneroUtils::reconcile(difficulty, other->difficulty);
cumulativeDifficulty = MoneroUtils::reconcile(cumulativeDifficulty, other->cumulativeDifficulty);
majorVersion = MoneroUtils::reconcile(majorVersion, other->majorVersion);
minorVersion = MoneroUtils::reconcile(minorVersion, other->minorVersion);
nonce = MoneroUtils::reconcile(nonce, other->nonce);
coinbaseTxId = MoneroUtils::reconcile(coinbaseTxId, other->coinbaseTxId);
numTxs = MoneroUtils::reconcile(numTxs, other->numTxs);
orphanStatus = MoneroUtils::reconcile(orphanStatus, other->orphanStatus);
prevId = MoneroUtils::reconcile(prevId, other->prevId);
reward = MoneroUtils::reconcile(reward, other->reward);
powHash = MoneroUtils::reconcile(powHash, other->powHash);
}
// ----------------------------- MONERO BLOCK -------------------------------
boost::property_tree::ptree MoneroBlock::toPropertyTree() const {
//cout << "MoneroBlock::toPropertyTree(block)" << endl;
boost::property_tree::ptree node = MoneroBlockHeader::toPropertyTree();
@@ -55,6 +80,48 @@ namespace monero {
return node;
}
void MoneroBlock::merge(const shared_ptr<MoneroBlock>& self, const shared_ptr<MoneroBlock>& other) {
if (this != self.get()) throw runtime_error("this != self");
if (self == other) return;
// merge header fields
MoneroBlockHeader::merge(self, other);
// convert other to MoneroBlock*
MoneroBlock* otherBlock = static_cast<MoneroBlock*>(other.get());
// merge coinbase tx
if (coinbaseTx == boost::none) coinbaseTx = otherBlock->coinbaseTx;
else if (otherBlock->coinbaseTx != boost::none) (**coinbaseTx).merge(*coinbaseTx, *otherBlock->coinbaseTx);
// merge non-coinbase txs
if (txs.empty()) txs = otherBlock->txs;
else if (!otherBlock->txs.empty()) {
for (const shared_ptr<MoneroTx>& thatTx : otherBlock->txs) {
bool found = false;
for (const shared_ptr<MoneroTx>& thisTx : txs) {
if (thatTx->id == thisTx->id) {
thisTx->merge(thisTx, thatTx);
found = true;
break;
}
}
if (!found) txs.push_back(thatTx);
}
}
if (!txs.empty()) {
for (const shared_ptr<MoneroTx>& tx : txs) {
tx->block = static_pointer_cast<MoneroBlock>(self);
}
}
// merge other fields
hex = MoneroUtils::reconcile(hex, otherBlock->hex);
//txIds = MoneroUtils::reconcile(txIds, otherBlock->txIds); // TODO: implement
}
// ------------------------------- MONERO TX --------------------------------
boost::property_tree::ptree MoneroTx::toPropertyTree() const {
//cout << "MoneroTx::txToPropertyTree(tx)" << endl;
boost::property_tree::ptree node;
@@ -98,71 +165,6 @@ namespace monero {
return node;
}
// -------------------------- MODEL IMPLEMENTATION --------------------------
void MoneroBlockHeader::merge(const shared_ptr<MoneroBlockHeader>& self, const shared_ptr<MoneroBlockHeader>& other) {
if (this != self.get()) throw runtime_error("this != self");
if (self == other) return;
id = MoneroUtils::reconcile(id, other->id);
height = MoneroUtils::reconcile(height, other->height, boost::none, boost::none, true); // height can increase
timestamp = MoneroUtils::reconcile(timestamp, other->timestamp, boost::none, boost::none, true); // timestamp can increase
size = MoneroUtils::reconcile(size, other->size);
weight = MoneroUtils::reconcile(weight, other->weight);
longTermWeight = MoneroUtils::reconcile(longTermWeight, other->longTermWeight);
depth = MoneroUtils::reconcile(depth, other->depth);
difficulty = MoneroUtils::reconcile(difficulty, other->difficulty);
cumulativeDifficulty = MoneroUtils::reconcile(cumulativeDifficulty, other->cumulativeDifficulty);
majorVersion = MoneroUtils::reconcile(majorVersion, other->majorVersion);
minorVersion = MoneroUtils::reconcile(minorVersion, other->minorVersion);
nonce = MoneroUtils::reconcile(nonce, other->nonce);
coinbaseTxId = MoneroUtils::reconcile(coinbaseTxId, other->coinbaseTxId);
numTxs = MoneroUtils::reconcile(numTxs, other->numTxs);
orphanStatus = MoneroUtils::reconcile(orphanStatus, other->orphanStatus);
prevId = MoneroUtils::reconcile(prevId, other->prevId);
reward = MoneroUtils::reconcile(reward, other->reward);
powHash = MoneroUtils::reconcile(powHash, other->powHash);
}
void MoneroBlock::merge(const shared_ptr<MoneroBlock>& self, const shared_ptr<MoneroBlock>& other) {
if (this != self.get()) throw runtime_error("this != self");
if (self == other) return;
// merge header fields
MoneroBlockHeader::merge(self, other);
// convert other to MoneroBlock*
MoneroBlock* otherBlock = static_cast<MoneroBlock*>(other.get());
// merge coinbase tx
if (coinbaseTx == boost::none) coinbaseTx = otherBlock->coinbaseTx;
else if (otherBlock->coinbaseTx != boost::none) (**coinbaseTx).merge(*coinbaseTx, *otherBlock->coinbaseTx);
// merge non-coinbase txs
if (txs.empty()) txs = otherBlock->txs;
else if (!otherBlock->txs.empty()) {
for (const shared_ptr<MoneroTx>& thatTx : otherBlock->txs) {
bool found = false;
for (const shared_ptr<MoneroTx>& thisTx : txs) {
if (thatTx->id == thisTx->id) {
thisTx->merge(thisTx, thatTx);
found = true;
break;
}
}
if (!found) txs.push_back(thatTx);
}
}
if (!txs.empty()) {
for (const shared_ptr<MoneroTx>& tx : txs) {
tx->block = static_pointer_cast<MoneroBlock>(self);
}
}
// merge other fields
hex = MoneroUtils::reconcile(hex, otherBlock->hex);
//txIds = MoneroUtils::reconcile(txIds, otherBlock->txIds); // TODO: implement
}
boost::optional<uint64_t> MoneroTx::getHeight() const {
if (block == boost::none) return boost::none;
return *((*block)->height);
@@ -278,9 +280,14 @@ namespace monero {
}
}
void MoneroKeyImage::merge(const shared_ptr<MoneroKeyImage>& self, const shared_ptr<MoneroKeyImage>& other) {
cout << "MoneroKeyImage::merge()" << endl;
throw runtime_error("Not implemented");
// ------------------------------ MONERO OUTPUT -----------------------------
boost::property_tree::ptree MoneroOutput::toPropertyTree() const {
//cout << "MoneroOutput::txToPropertyTree(tx)" << endl;
boost::property_tree::ptree node;
//if (id != boost::none) node.put("id", *id); TODO
throw runtime_error("MoneroOutput toPropertyTree() not implemented");
return node;
}
void MoneroOutput::merge(const shared_ptr<MoneroOutput>& self, const shared_ptr<MoneroOutput>& other) {
@@ -288,6 +295,10 @@ namespace monero {
throw runtime_error("Not implemented");
}
// ---------------------------- PRIVATE HELPERS -----------------------------
// --------------------------- MONERO KEY IMAGE -----------------------------
void MoneroKeyImage::merge(const shared_ptr<MoneroKeyImage>& self, const shared_ptr<MoneroKeyImage>& other) {
cout << "MoneroKeyImage::merge()" << endl;
throw runtime_error("Not implemented");
}
}