diff --git a/src/LedgerEntrySet.cpp b/src/LedgerEntrySet.cpp index 3ec82c343c..b07c9ec0ee 100644 --- a/src/LedgerEntrySet.cpp +++ b/src/LedgerEntrySet.cpp @@ -1,5 +1,7 @@ #include "LedgerEntrySet.h" +#include + LedgerEntrySet LedgerEntrySet::duplicate() { return LedgerEntrySet(mEntries, mSeq + 1); @@ -17,5 +19,73 @@ void LedgerEntrySet::swapWith(LedgerEntrySet& e) mEntries.swap(e.mEntries); } +SLE::pointer LedgerEntrySet::getEntry(const uint256& index, LedgerEntryAction& action) +{ + boost::unordered_map::iterator it = mEntries.find(index); + if (it == mEntries.end()) + { + action = taaNONE; + return SLE::pointer(); + } + if (it->second.mSeq != mSeq) + { + it->second.mEntry = boost::make_shared(*it->second.mEntry); + it->second.mSeq = mSeq; + } + action = it->second.mAction; + return it->second.mEntry; +} +LedgerEntryAction LedgerEntrySet::hasEntry(const uint256& index) const +{ + boost::unordered_map::const_iterator it = mEntries.find(index); + if (it == mEntries.end()) + return taaNONE; + return it->second.mAction; +} +void LedgerEntrySet::entryCache(SLE::pointer sle) +{ + boost::unordered_map::iterator it = mEntries.find(sle->getIndex()); + if (it == mEntries.end()) + mEntries.insert(std::make_pair(sle->getIndex(), LedgerEntrySetEntry(sle, taaCACHED, mSeq))); + else if (it->second.mAction == taaCACHED) + { + it->second.mSeq = mSeq; + it->second.mEntry = sle; + } + else + throw std::runtime_error("Cache after modify/delete"); +} + +void LedgerEntrySet::entryCreate(SLE::pointer sle) +{ + boost::unordered_map::iterator it = mEntries.find(sle->getIndex()); + if (it == mEntries.end()) + mEntries.insert(std::make_pair(sle->getIndex(), LedgerEntrySetEntry(sle, taaDELETE, mSeq))); + else if (it->second.mAction == taaDELETE) + throw std::runtime_error("Create after delete"); + else if (it->second.mAction == taaMODIFY) + throw std::runtime_error("Create after modify"); + else + { + it->second.mSeq = mSeq; + it->second.mEntry = sle; + it->second.mAction = taaDELETE; + } +} + +void LedgerEntrySet::entryModify(SLE::pointer sle) +{ + boost::unordered_map::iterator it = mEntries.find(sle->getIndex()); + if (it == mEntries.end()) + mEntries.insert(std::make_pair(sle->getIndex(), LedgerEntrySetEntry(sle, taaMODIFY, mSeq))); + else if (it->second.mAction == taaDELETE) + throw std::runtime_error("Modify after delete"); + else + { + it->second.mSeq = mSeq; + it->second.mEntry = sle; + it->second.mAction = (it->second.mAction == taaCREATE) ? taaCREATE : taaDELETE; + } +} diff --git a/src/LedgerEntrySet.h b/src/LedgerEntrySet.h index d38603b5a7..04ec8d70dd 100644 --- a/src/LedgerEntrySet.h +++ b/src/LedgerEntrySet.h @@ -21,6 +21,8 @@ public: SLE::pointer mEntry; LedgerEntryAction mAction; int mSeq; + + LedgerEntrySetEntry(SLE::pointer e, LedgerEntryAction a, int s) : mEntry(e), mAction(a), mSeq(s) { ; } };