diff --git a/Ledger.cpp b/Ledger.cpp index a9b171ebc..08cf0c5b4 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -320,6 +320,13 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger) ScopedLock sl(theApp->getDBLock()); theApp->getDB()->executeSQL(sql.c_str()); + + // write out dirty nodes + while(ledger->mTransactionMap->flushDirty(64, TRANSACTION_NODE, ledger->mLedgerSeq)) + { ; } + while(ledger->mAccountStateMap->flushDirty(64, ACCOUNT_NODE, ledger->mLedgerSeq)) + { ; } + } Ledger::pointer Ledger::getSQL(const std::string& sql) @@ -457,61 +464,6 @@ bool Ledger::load(const uint256& hash) return(false); } -void Ledger::save() -{ - Database* db=theApp->getDB(); - - string sql="SELECT ledgerID from Ledgers where hash="; - string hashStr; - db->escape(mHash.begin(),mHash.GetSerializeSize(),hashStr); - sql.append(hashStr); - - if(db->executeSQL(sql.c_str())) - { - db->startIterRows(); - - if(db->getNextRow()) - { // this Ledger is already in the DB. We don't need to do anything since the hashes are the same - db->endIterRows(); - }else - { // this ledger isn't in the DB - char buf[100]; - sql="INSERT INTO Ledgers (LedgerIndex,Hash,ParentHash,FeeHeld) values ("; - sprintf(buf, "%d", mIndex); - sql.append(buf); - sql.append(","); - sql.append(buf); - sql.append(","); - sql.append(buf); - sql.append(","); - sprintf(buf, "%llu", mFeeHeld); - sql.append(buf); - sql.append(")"); - - sql="SELECT LAST_INSERT_ID()"; - - } - } -} - -int64 Ledger::getAmountHeld(const uint160& address) -{ - if(mAccounts.count(address)) - { - return(mAccounts[address].first); - } - return(0); -} - -Ledger::Account* Ledger::getAccount(const uint160& address) -{ - if(mAccounts.count(address)) - { - return(&(mAccounts[address])); - } - return(NULL); -} - uint256& Ledger::getSignature() { if(!mValidSig) sign(); @@ -524,17 +476,6 @@ void Ledger::publishValidation() theApp->getConnectionPool().relayMessage(NULL,packet); } -void Ledger::sign() -{ - // TODO: Ledger::sign() -} - - -void Ledger::hash() -{ - // TODO: Ledger::hash() -} - /* uint64 Ledger::getAmount(std::string address) { diff --git a/SHAMap.cpp b/SHAMap.cpp index 6f60db7f5..24943b8e8 100644 --- a/SHAMap.cpp +++ b/SHAMap.cpp @@ -126,7 +126,7 @@ SHAMapLeafNode::pointer SHAMap::getLeaf(const SHAMapNode& id, const uint256& has if(leaf) return returnLeaf(leaf, modify); std::vector leafData; - if(!fetchLeafNode(hash, id, leafData)) throw SHAMapException(MissingNode); + if(!fetchNode(hash, leafData)) throw SHAMapException(MissingNode); leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id, leafData, mSeq)); if(leaf->getNodeHash()!=hash) throw SHAMapException(InvalidNode); mLeafByID[id]=leaf; @@ -139,7 +139,7 @@ SHAMapInnerNode::pointer SHAMap::getInner(const SHAMapNode& id, const uint256& h if(node) return returnNode(node, modify); std::vector rawNode; - if(!fetchInnerNode(hash, id, rawNode)) throw SHAMapException(MissingNode); + if(!fetchNode(hash, rawNode)) throw SHAMapException(MissingNode); node=SHAMapInnerNode::pointer(new SHAMapInnerNode(id, rawNode, mSeq)); if(node->getNodeHash()!=hash) throw SHAMapException(InvalidNode); @@ -473,25 +473,45 @@ void SHAMapItem::dump() std::cerr << "SHAMapItem(" << mTag.GetHex() << ") " << mData.size() << "bytes" << std::endl; } -// overloads for backed maps -bool SHAMap::fetchInnerNode(const uint256&, const SHAMapNode&, std::vector&) +bool SHAMap::fetchNode(const uint256& hash, std::vector& data) { - return false; + HashedObject::pointer obj(HashedObject::retrieve(hash)); + if(!obj) return false; + data=obj->getData(); } -bool SHAMap::fetchLeafNode(const uint256&, const SHAMapNode&, std::vector&) +int SHAMap::flushDirty(int maxNodes, HashedObjectType t, uint32 seq) { - return false; -} + int flushed=0; + Serializer s; -bool SHAMap::writeInnerNode(const uint256&, const SHAMapNode&, const std::vector&) -{ - return true; -} + if(mDirtyLeafNodes) + { + while(!mDirtyLeafNodes->empty()) + { + SHAMapLeafNode::pointer& dln=mDirtyLeafNodes->begin()->second; + s.erase(); + dln->addRaw(s); + HashedObject::store(t, seq, s.peekData(), s.getSHA512Half()); + mDirtyLeafNodes->erase(mDirtyLeafNodes->begin()); + if(flushed++>=maxNodes) return flushed; + } + } -bool SHAMap::writeLeafNode(const uint256&, const SHAMapNode&, const std::vector&) -{ - return true; + if(mDirtyInnerNodes) + { + while(!mDirtyInnerNodes->empty()) + { + SHAMapInnerNode::pointer& din=mDirtyInnerNodes->begin()->second; + s.erase(); + din->addRaw(s); + HashedObject::store(t, seq, s.peekData(), s.getSHA512Half()); + mDirtyInnerNodes->erase(mDirtyInnerNodes->begin()); + if(flushed++>=maxNodes) return flushed; + } + } + + return flushed; } void SHAMap::dump() diff --git a/SHAMap.h b/SHAMap.h index 61e6588b4..59b0fd20a 100644 --- a/SHAMap.h +++ b/SHAMap.h @@ -12,6 +12,7 @@ #include "uint256.h" #include "ScopedLock.h" #include "Serializer.h" +#include "HashedObject.h" class SHAMap; @@ -291,13 +292,10 @@ public: // return value: true=successfully completed, false=too different bool compare(SHAMap::pointer otherMap, SHAMapDiff& differences, int maxCount); - int flushDirty(int maxNodes); + int flushDirty(int maxNodes, HashedObjectType t, uint32 seq); // overloads for backed maps - virtual bool fetchInnerNode(const uint256& hash, const SHAMapNode& id, std::vector& rawNode); - virtual bool fetchLeafNode(const uint256& hash, const SHAMapNode& id, std::vector& rawNode); - virtual bool writeInnerNode(const uint256& hash, const SHAMapNode& id, const std::vector& rawNode); - virtual bool writeLeafNode(const uint256& hash, const SHAMapNode& id, const std::vector& rawNode); + bool fetchNode(const uint256& hash, std::vector& rawNode); static bool TestSHAMap(); virtual void dump();