Cleanups to the sha map node format code.

This commit is contained in:
JoelKatz
2012-08-07 19:52:09 -07:00
parent b4e63c3025
commit 8e89335e2b
7 changed files with 46 additions and 41 deletions

View File

@@ -311,8 +311,10 @@ bool LedgerAcquire::takeBase(const std::string& data)
theApp->getHashedObjectStore().store(LEDGER, mLedger->getLedgerSeq(), s.peekData(), mHash);
progress();
if (!mLedger->getTransHash()) mHaveTransactions = true;
if (!mLedger->getAccountHash()) mHaveState = true;
if (!mLedger->getTransHash())
mHaveTransactions = true;
if (!mLedger->getAccountHash())
mHaveState = true;
mLedger->setAcquiring();
return true;
}
@@ -328,7 +330,7 @@ bool LedgerAcquire::takeTxNode(const std::list<SHAMapNode>& nodeIDs,
{
if (nodeIDit->isRoot())
{
if (!mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), *nodeDatait, STN_ARF_WIRE))
if (!mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), *nodeDatait, snfWIRE))
return false;
}
else if (!mLedger->peekTransactionMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter))
@@ -363,7 +365,7 @@ bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs,
{
if (nodeIDit->isRoot())
{
if (!mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), *nodeDatait, STN_ARF_WIRE))
if (!mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), *nodeDatait, snfWIRE))
return false;
}
else if (!mLedger->peekAccountStateMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter))
@@ -387,13 +389,13 @@ bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs,
bool LedgerAcquire::takeAsRootNode(const std::vector<unsigned char>& data)
{
if (!mHaveBase) return false;
return mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), data, STN_ARF_WIRE);
return mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), data, snfWIRE);
}
bool LedgerAcquire::takeTxRootNode(const std::vector<unsigned char>& data)
{
if (!mHaveBase) return false;
return mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), data, STN_ARF_WIRE);
return mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), data, snfWIRE);
}
LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash)

View File

@@ -101,7 +101,7 @@ bool TransactionAcquire::takeNodes(const std::list<SHAMapNode>& nodeIDs,
Log(lsWARNING) << "Got root TXS node, already have it";
return false;
}
if (!mMap->addRootNode(getHash(), *nodeDatait, STN_ARF_WIRE))
if (!mMap->addRootNode(getHash(), *nodeDatait, snfWIRE))
return false;
else mHaveRoot = true;
}

View File

@@ -1007,13 +1007,13 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
reply.add_nodes()->set_nodedata(nData.getDataPtr(), nData.getLength());
if (packet.nodeids().size() != 0)
{
{ // new-style root request
Log(lsINFO) << "Ledger root w/map roots request";
SHAMap::pointer map = ledger->peekAccountStateMap();
if (map)
{
{ // return account state root node if possible
Serializer rootNode(768);
if (map->getRootNode(rootNode, STN_ARF_WIRE))
if (map->getRootNode(rootNode, snfWIRE))
{
reply.add_nodes()->set_nodedata(rootNode.getDataPtr(), rootNode.getLength());
if (ledger->getTransHash().isNonZero())
@@ -1022,7 +1022,7 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
if (map)
{
rootNode.resize(0);
if (map->getRootNode(rootNode, STN_ARF_WIRE))
if (map->getRootNode(rootNode, snfWIRE))
reply.add_nodes()->set_nodedata(rootNode.getDataPtr(), rootNode.getLength());
}
}

View File

@@ -641,7 +641,7 @@ SHAMapTreeNode::pointer SHAMap::fetchNodeExternal(const SHAMapNode& id, const ui
try
{
SHAMapTreeNode::pointer ret = boost::make_shared<SHAMapTreeNode>(id, obj->getData(), mSeq, STN_ARF_PREFIXED);
SHAMapTreeNode::pointer ret = boost::make_shared<SHAMapTreeNode>(id, obj->getData(), mSeq, snfPREFIX);
#ifdef DEBUG
assert((ret->getNodeHash() == hash) && (id == *ret));
#endif
@@ -672,7 +672,7 @@ int SHAMap::flushDirty(int maxNodes, HashedObjectType t, uint32 seq)
while (it != dirtyNodes.end())
{
s.erase();
it->second->addRaw(s, STN_ARF_PREFIXED);
it->second->addRaw(s, snfPREFIX);
theApp->getHashedObjectStore().store(t, seq, s.peekData(), s.getSHA512Half());
if (flushed++ >= maxNodes)
return flushed;

View File

@@ -122,6 +122,12 @@ public:
virtual void dump();
};
enum SHANodeFormat
{
snfPREFIX = 1, // Form that hashes to its official hash
snfWIRE = 2, // Compressed form used on the wire
};
class SHAMapTreeNode : public SHAMapNode
{
friend class SHAMap;
@@ -156,12 +162,9 @@ public:
SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq); // copy node from older tree
SHAMapTreeNode(const SHAMapNode& nodeID, SHAMapItem::pointer item, TNType type, uint32 seq);
#define STN_ARF_PREFIXED 1
#define STN_ARF_WIRE 2
// raw node functions
SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned char>& contents, uint32 seq, int format);
void addRaw(Serializer &, int format);
SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned char>& data, uint32 seq, SHANodeFormat format);
void addRaw(Serializer &, SHANodeFormat format);
virtual bool isPopulated() const { return true; }
@@ -325,9 +328,9 @@ public:
SHAMapSyncFilter* filter);
bool getNodeFat(const SHAMapNode& node, std::vector<SHAMapNode>& nodeIDs,
std::list<std::vector<unsigned char> >& rawNode, bool fatLeaves);
bool getRootNode(Serializer& s, int format);
bool addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, int format);
bool addRootNode(const std::vector<unsigned char>& rootNode, int format);
bool getRootNode(Serializer& s, SHANodeFormat format);
bool addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format);
bool addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format);
bool addKnownNode(const SHAMapNode& nodeID, const std::vector<unsigned char>& rawNode,
SHAMapSyncFilter* filter);

View File

@@ -189,10 +189,10 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& node, SHAMapItem::pointer item,
updateHash();
}
SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned char>& rawNode, uint32 seq, int format)
: SHAMapNode(id), mSeq(seq), mType(tnERROR), mFullBelow(false)
SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned char>& rawNode, uint32 seq,
SHANodeFormat format) : SHAMapNode(id), mSeq(seq), mType(tnERROR), mFullBelow(false)
{
if (format == STN_ARF_WIRE)
if (format == snfWIRE)
{
Serializer s(rawNode);
int type = s.removeLastByte();
@@ -256,7 +256,7 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned
}
}
if (format == STN_ARF_PREFIXED)
if (format == snfPREFIX)
{
if (rawNode.size() < 4)
{
@@ -350,14 +350,14 @@ bool SHAMapTreeNode::updateHash()
return true;
}
void SHAMapTreeNode::addRaw(Serializer& s, int format)
void SHAMapTreeNode::addRaw(Serializer& s, SHANodeFormat format)
{
assert((format == STN_ARF_PREFIXED) || (format == STN_ARF_WIRE));
assert((format == snfPREFIX) || (format == snfWIRE));
if (mType == tnERROR) throw std::runtime_error("invalid I node type");
if (mType == tnINNER)
{
if (format == STN_ARF_PREFIXED)
if (format == snfPREFIX)
{
s.add32(sHP_InnerNode);
for (int i = 0; i < 16; ++i)
@@ -385,7 +385,7 @@ void SHAMapTreeNode::addRaw(Serializer& s, int format)
}
else if (mType == tnACCOUNT_STATE)
{
if (format == STN_ARF_PREFIXED)
if (format == snfPREFIX)
{
s.add32(sHP_LeafNode);
mItem->addRaw(s);
@@ -400,7 +400,7 @@ void SHAMapTreeNode::addRaw(Serializer& s, int format)
}
else if (mType == tnTRANSACTION_NM)
{
if (format == STN_ARF_PREFIXED)
if (format == snfPREFIX)
{
s.add32(sHP_TransactionID);
mItem->addRaw(s);
@@ -413,7 +413,7 @@ void SHAMapTreeNode::addRaw(Serializer& s, int format)
}
else if (mType == tnTRANSACTION_MD)
{
if (format == STN_ARF_PREFIXED)
if (format == snfPREFIX)
{
s.add32(sHP_TransactionNode);
mItem->addRaw(s);

View File

@@ -58,7 +58,7 @@ void SHAMap::getMissingNodes(std::vector<SHAMapNode>& nodeIDs, std::vector<uint2
std::vector<unsigned char> nodeData;
if (filter->haveNode(childID, childHash, nodeData))
{
d = boost::make_shared<SHAMapTreeNode>(childID, nodeData, mSeq, STN_ARF_PREFIXED);
d = boost::make_shared<SHAMapTreeNode>(childID, nodeData, mSeq, snfPREFIX);
if (childHash != d->getNodeHash())
{
Log(lsERROR) << "Wrong hash from cached object";
@@ -99,7 +99,7 @@ bool SHAMap::getNodeFat(const SHAMapNode& wanted, std::vector<SHAMapNode>& nodeI
nodeIDs.push_back(*node);
Serializer s;
node->addRaw(s, STN_ARF_WIRE);
node->addRaw(s, snfWIRE);
rawNodes.push_back(s.peekData());
if (node->isRoot() || node->isLeaf()) // don't get a fat root, can't get a fat leaf
@@ -114,7 +114,7 @@ bool SHAMap::getNodeFat(const SHAMapNode& wanted, std::vector<SHAMapNode>& nodeI
{
nodeIDs.push_back(*nextNode);
Serializer s;
nextNode->addRaw(s, STN_ARF_WIRE);
nextNode->addRaw(s, snfWIRE);
rawNodes.push_back(s.peekData());
}
}
@@ -122,14 +122,14 @@ bool SHAMap::getNodeFat(const SHAMapNode& wanted, std::vector<SHAMapNode>& nodeI
return true;
}
bool SHAMap::getRootNode(Serializer& s, int format)
bool SHAMap::getRootNode(Serializer& s, SHANodeFormat format)
{
boost::recursive_mutex::scoped_lock sl(mLock);
root->addRaw(s, format);
return true;
}
bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, int format)
bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format)
{
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -160,7 +160,7 @@ bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, int format)
return true;
}
bool SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, int format)
bool SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format)
{
boost::recursive_mutex::scoped_lock sl(mLock);
@@ -236,14 +236,14 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
uint256 hash = iNode->getChildHash(branch);
if (!hash) return false;
SHAMapTreeNode::pointer newNode = boost::make_shared<SHAMapTreeNode>(node, rawNode, mSeq, STN_ARF_WIRE);
SHAMapTreeNode::pointer newNode = boost::make_shared<SHAMapTreeNode>(node, rawNode, mSeq, snfWIRE);
if (hash != newNode->getNodeHash()) // these aren't the droids we're looking for
return false;
if (filter)
{
Serializer s;
newNode->addRaw(s, STN_ARF_PREFIXED);
newNode->addRaw(s, snfPREFIX);
filter->gotNode(node, hash, s.peekData(), newNode->isLeaf());
}
@@ -399,7 +399,7 @@ std::list<std::vector<unsigned char> > SHAMap::getTrustedPath(const uint256& ind
Serializer s;
while (!stack.empty())
{
stack.top()->addRaw(s, STN_ARF_WIRE);
stack.top()->addRaw(s, snfWIRE);
path.push_back(s.getData());
s.erase();
stack.pop();
@@ -454,7 +454,7 @@ BOOST_AUTO_TEST_CASE( SHAMapSync_test )
Log(lsFATAL) << "Didn't get root node " << gotNodes.size();
BOOST_FAIL("NodeSize");
}
if (!destination.addRootNode(*gotNodes.begin(), STN_ARF_WIRE))
if (!destination.addRootNode(*gotNodes.begin(), snfWIRE))
{
Log(lsFATAL) << "AddRootNode fails";
BOOST_FAIL("AddRootNode");