Tie the SHAMap code to the HashedObject code in a much simpler way.

Write out dirty nodes when we accept a ledger.
This commit is contained in:
JoelKatz
2011-12-13 20:15:36 -08:00
parent 854df9d920
commit d1b2d12a04
3 changed files with 45 additions and 86 deletions

View File

@@ -320,6 +320,13 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger)
ScopedLock sl(theApp->getDBLock()); ScopedLock sl(theApp->getDBLock());
theApp->getDB()->executeSQL(sql.c_str()); 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) Ledger::pointer Ledger::getSQL(const std::string& sql)
@@ -457,61 +464,6 @@ bool Ledger::load(const uint256& hash)
return(false); 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() uint256& Ledger::getSignature()
{ {
if(!mValidSig) sign(); if(!mValidSig) sign();
@@ -524,17 +476,6 @@ void Ledger::publishValidation()
theApp->getConnectionPool().relayMessage(NULL,packet); theApp->getConnectionPool().relayMessage(NULL,packet);
} }
void Ledger::sign()
{
// TODO: Ledger::sign()
}
void Ledger::hash()
{
// TODO: Ledger::hash()
}
/* /*
uint64 Ledger::getAmount(std::string address) uint64 Ledger::getAmount(std::string address)
{ {

View File

@@ -126,7 +126,7 @@ SHAMapLeafNode::pointer SHAMap::getLeaf(const SHAMapNode& id, const uint256& has
if(leaf) return returnLeaf(leaf, modify); if(leaf) return returnLeaf(leaf, modify);
std::vector<unsigned char> leafData; std::vector<unsigned char> leafData;
if(!fetchLeafNode(hash, id, leafData)) throw SHAMapException(MissingNode); if(!fetchNode(hash, leafData)) throw SHAMapException(MissingNode);
leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id, leafData, mSeq)); leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id, leafData, mSeq));
if(leaf->getNodeHash()!=hash) throw SHAMapException(InvalidNode); if(leaf->getNodeHash()!=hash) throw SHAMapException(InvalidNode);
mLeafByID[id]=leaf; mLeafByID[id]=leaf;
@@ -139,7 +139,7 @@ SHAMapInnerNode::pointer SHAMap::getInner(const SHAMapNode& id, const uint256& h
if(node) return returnNode(node, modify); if(node) return returnNode(node, modify);
std::vector<unsigned char> rawNode; std::vector<unsigned char> rawNode;
if(!fetchInnerNode(hash, id, rawNode)) throw SHAMapException(MissingNode); if(!fetchNode(hash, rawNode)) throw SHAMapException(MissingNode);
node=SHAMapInnerNode::pointer(new SHAMapInnerNode(id, rawNode, mSeq)); node=SHAMapInnerNode::pointer(new SHAMapInnerNode(id, rawNode, mSeq));
if(node->getNodeHash()!=hash) throw SHAMapException(InvalidNode); if(node->getNodeHash()!=hash) throw SHAMapException(InvalidNode);
@@ -473,25 +473,45 @@ void SHAMapItem::dump()
std::cerr << "SHAMapItem(" << mTag.GetHex() << ") " << mData.size() << "bytes" << std::endl; std::cerr << "SHAMapItem(" << mTag.GetHex() << ") " << mData.size() << "bytes" << std::endl;
} }
// overloads for backed maps bool SHAMap::fetchNode(const uint256& hash, std::vector<unsigned char>& data)
bool SHAMap::fetchInnerNode(const uint256&, const SHAMapNode&, std::vector<unsigned char>&)
{ {
return false; HashedObject::pointer obj(HashedObject::retrieve(hash));
if(!obj) return false;
data=obj->getData();
} }
bool SHAMap::fetchLeafNode(const uint256&, const SHAMapNode&, std::vector<unsigned char>&) 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<unsigned char>&) if(mDirtyLeafNodes)
{ {
return true; 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<unsigned char>&) if(mDirtyInnerNodes)
{ {
return true; 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() void SHAMap::dump()

View File

@@ -12,6 +12,7 @@
#include "uint256.h" #include "uint256.h"
#include "ScopedLock.h" #include "ScopedLock.h"
#include "Serializer.h" #include "Serializer.h"
#include "HashedObject.h"
class SHAMap; class SHAMap;
@@ -291,13 +292,10 @@ public:
// return value: true=successfully completed, false=too different // return value: true=successfully completed, false=too different
bool compare(SHAMap::pointer otherMap, SHAMapDiff& differences, int maxCount); 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 // overloads for backed maps
virtual bool fetchInnerNode(const uint256& hash, const SHAMapNode& id, std::vector<unsigned char>& rawNode); bool fetchNode(const uint256& hash, std::vector<unsigned char>& rawNode);
virtual bool fetchLeafNode(const uint256& hash, const SHAMapNode& id, std::vector<unsigned char>& rawNode);
virtual bool writeInnerNode(const uint256& hash, const SHAMapNode& id, const std::vector<unsigned char>& rawNode);
virtual bool writeLeafNode(const uint256& hash, const SHAMapNode& id, const std::vector<unsigned char>& rawNode);
static bool TestSHAMap(); static bool TestSHAMap();
virtual void dump(); virtual void dump();