Rework the way the results of ledger and TX map acquisition is passed up and down the

call chain so that the peer logic will know how helpful peers are being, not just whether
they're sending invalid data.
This commit is contained in:
JoelKatz
2012-11-21 09:28:09 -08:00
parent f98741af3c
commit 89d54999c9
11 changed files with 155 additions and 84 deletions

View File

@@ -396,7 +396,7 @@ bool LedgerAcquire::takeBase(const std::string& data)
} }
bool LedgerAcquire::takeTxNode(const std::list<SHAMapNode>& nodeIDs, bool LedgerAcquire::takeTxNode(const std::list<SHAMapNode>& nodeIDs,
const std::list< std::vector<unsigned char> >& data) const std::list< std::vector<unsigned char> >& data, SMAddNode& san)
{ {
if (!mHaveBase) return false; if (!mHaveBase) return false;
std::list<SHAMapNode>::const_iterator nodeIDit = nodeIDs.begin(); std::list<SHAMapNode>::const_iterator nodeIDit = nodeIDs.begin();
@@ -406,11 +406,15 @@ bool LedgerAcquire::takeTxNode(const std::list<SHAMapNode>& nodeIDs,
{ {
if (nodeIDit->isRoot()) if (nodeIDit->isRoot())
{ {
if (!mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), *nodeDatait, snfWIRE, &tFilter)) if (!san.combine(mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), *nodeDatait,
snfWIRE, &tFilter)))
return false;
}
else
{
if (!san.combine(mLedger->peekTransactionMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter)))
return false; return false;
} }
else if (!mLedger->peekTransactionMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter))
return false;
++nodeIDit; ++nodeIDit;
++nodeDatait; ++nodeDatait;
} }
@@ -428,7 +432,7 @@ bool LedgerAcquire::takeTxNode(const std::list<SHAMapNode>& nodeIDs,
} }
bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs, bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs,
const std::list< std::vector<unsigned char> >& data) const std::list< std::vector<unsigned char> >& data, SMAddNode& san)
{ {
cLog(lsTRACE) << "got ASdata (" << nodeIDs.size() <<") acquiring ledger " << mHash; cLog(lsTRACE) << "got ASdata (" << nodeIDs.size() <<") acquiring ledger " << mHash;
tLog(nodeIDs.size() == 1, lsTRACE) << "got AS node: " << nodeIDs.front(); tLog(nodeIDs.size() == 1, lsTRACE) << "got AS node: " << nodeIDs.front();
@@ -446,14 +450,14 @@ bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs,
{ {
if (nodeIDit->isRoot()) if (nodeIDit->isRoot())
{ {
if (!mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), if (!san.combine(mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(),
*nodeDatait, snfWIRE, &tFilter)) *nodeDatait, snfWIRE, &tFilter)))
{ {
cLog(lsWARNING) << "Bad ledger base"; cLog(lsWARNING) << "Bad ledger base";
return false; return false;
} }
} }
else if (!mLedger->peekAccountStateMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter)) else if (!san.combine(mLedger->peekAccountStateMap()->addKnownNode(*nodeIDit, *nodeDatait, &tFilter)))
{ {
cLog(lsWARNING) << "Unable to add AS node"; cLog(lsWARNING) << "Unable to add AS node";
return false; return false;
@@ -474,24 +478,22 @@ bool LedgerAcquire::takeAsNode(const std::list<SHAMapNode>& nodeIDs,
return true; return true;
} }
bool LedgerAcquire::takeAsRootNode(const std::vector<unsigned char>& data) bool LedgerAcquire::takeAsRootNode(const std::vector<unsigned char>& data, SMAddNode& san)
{ {
if (!mHaveBase) if (!mHaveBase)
return false; return false;
AccountStateSF tFilter(mLedger->getHash(), mLedger->getLedgerSeq()); AccountStateSF tFilter(mLedger->getHash(), mLedger->getLedgerSeq());
if (!mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), data, snfWIRE, &tFilter)) return san.combine(
return false; mLedger->peekAccountStateMap()->addRootNode(mLedger->getAccountHash(), data, snfWIRE, &tFilter));
return true;
} }
bool LedgerAcquire::takeTxRootNode(const std::vector<unsigned char>& data) bool LedgerAcquire::takeTxRootNode(const std::vector<unsigned char>& data, SMAddNode& san)
{ {
if (!mHaveBase) if (!mHaveBase)
return false; return false;
TransactionStateSF tFilter(mLedger->getHash(), mLedger->getLedgerSeq()); TransactionStateSF tFilter(mLedger->getHash(), mLedger->getLedgerSeq());
if (!mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), data, snfWIRE, &tFilter)) return san.combine(
return false; mLedger->peekTransactionMap()->addRootNode(mLedger->getTransHash(), data, snfWIRE, &tFilter));
return true;
} }
LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash) LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash)
@@ -532,13 +534,13 @@ void LedgerAcquireMaster::dropLedger(const uint256& hash)
mLedgers.erase(hash); mLedgers.erase(hash);
} }
bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref peer) SMAddNode LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref peer)
{ {
uint256 hash; uint256 hash;
if (packet.ledgerhash().size() != 32) if (packet.ledgerhash().size() != 32)
{ {
std::cerr << "Acquire error" << std::endl; std::cerr << "Acquire error" << std::endl;
return false; return SMAddNode::invalid();
} }
memcpy(hash.begin(), packet.ledgerhash().data(), 32); memcpy(hash.begin(), packet.ledgerhash().data(), 32);
cLog(lsTRACE) << "Got data (" << packet.nodes().size() << ") for acquiring ledger: " << hash; cLog(lsTRACE) << "Got data (" << packet.nodes().size() << ") for acquiring ledger: " << hash;
@@ -547,7 +549,7 @@ bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref
if (!ledger) if (!ledger)
{ {
cLog(lsINFO) << "Got data for ledger we're not acquiring"; cLog(lsINFO) << "Got data for ledger we're not acquiring";
return false; return SMAddNode();
} }
if (packet.type() == ripple::liBASE) if (packet.type() == ripple::liBASE)
@@ -555,23 +557,24 @@ bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref
if (packet.nodes_size() < 1) if (packet.nodes_size() < 1)
{ {
cLog(lsWARNING) << "Got empty base data"; cLog(lsWARNING) << "Got empty base data";
return false; return SMAddNode::invalid();
} }
if (!ledger->takeBase(packet.nodes(0).nodedata())) if (!ledger->takeBase(packet.nodes(0).nodedata()))
{ {
cLog(lsWARNING) << "Got invalid base data"; cLog(lsWARNING) << "Got invalid base data";
return false; return SMAddNode::invalid();
} }
if ((packet.nodes().size() > 1) && !ledger->takeAsRootNode(strCopy(packet.nodes(1).nodedata()))) SMAddNode san = SMAddNode::useful();
if ((packet.nodes().size() > 1) && !ledger->takeAsRootNode(strCopy(packet.nodes(1).nodedata()), san))
{ {
cLog(lsWARNING) << "Included ASbase invalid"; cLog(lsWARNING) << "Included ASbase invalid";
} }
if ((packet.nodes().size() > 2) && !ledger->takeTxRootNode(strCopy(packet.nodes(2).nodedata()))) if ((packet.nodes().size() > 2) && !ledger->takeTxRootNode(strCopy(packet.nodes(2).nodedata()), san))
{ {
cLog(lsWARNING) << "Included TXbase invalid"; cLog(lsWARNING) << "Included TXbase invalid";
} }
ledger->trigger(peer, false); ledger->trigger(peer, false);
return true; return san;
} }
if ((packet.type() == ripple::liTX_NODE) || (packet.type() == ripple::liAS_NODE)) if ((packet.type() == ripple::liTX_NODE) || (packet.type() == ripple::liAS_NODE))
@@ -581,8 +584,8 @@ bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref
if (packet.nodes().size() <= 0) if (packet.nodes().size() <= 0)
{ {
cLog(lsINFO) << "Got request for no nodes"; cLog(lsINFO) << "Got response with no nodes";
return false; return SMAddNode::invalid();
} }
for (int i = 0; i < packet.nodes().size(); ++i) for (int i = 0; i < packet.nodes().size(); ++i)
{ {
@@ -590,24 +593,24 @@ bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref
if (!node.has_nodeid() || !node.has_nodedata()) if (!node.has_nodeid() || !node.has_nodedata())
{ {
cLog(lsWARNING) << "Got bad node"; cLog(lsWARNING) << "Got bad node";
return false; return SMAddNode::invalid();
} }
nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size())); nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size()));
nodeData.push_back(std::vector<unsigned char>(node.nodedata().begin(), node.nodedata().end())); nodeData.push_back(std::vector<unsigned char>(node.nodedata().begin(), node.nodedata().end()));
} }
bool ret; SMAddNode ret;
if (packet.type() == ripple::liTX_NODE) if (packet.type() == ripple::liTX_NODE)
ret = ledger->takeTxNode(nodeIDs, nodeData); ledger->takeTxNode(nodeIDs, nodeData, ret);
else else
ret = ledger->takeAsNode(nodeIDs, nodeData); ledger->takeAsNode(nodeIDs, nodeData, ret);
if (ret) if (!ret.isInvalid())
ledger->trigger(peer, false); ledger->trigger(peer, false);
return ret; return ret;
} }
cLog(lsWARNING) << "Not sure what ledger data we got"; cLog(lsWARNING) << "Not sure what ledger data we got";
return false; return SMAddNode::invalid();
} }
// vim:ts=4 // vim:ts=4

View File

@@ -97,10 +97,12 @@ public:
void addOnComplete(boost::function<void (LedgerAcquire::pointer)>); void addOnComplete(boost::function<void (LedgerAcquire::pointer)>);
bool takeBase(const std::string& data); bool takeBase(const std::string& data);
bool takeTxNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data); bool takeTxNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data,
bool takeTxRootNode(const std::vector<unsigned char>& data); SMAddNode&);
bool takeAsNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data); bool takeTxRootNode(const std::vector<unsigned char>& data, SMAddNode&);
bool takeAsRootNode(const std::vector<unsigned char>& data); bool takeAsNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data,
SMAddNode&);
bool takeAsRootNode(const std::vector<unsigned char>& data, SMAddNode&);
void trigger(Peer::ref, bool timer); void trigger(Peer::ref, bool timer);
bool tryLocal(); bool tryLocal();
void addPeers(); void addPeers();
@@ -119,7 +121,7 @@ public:
LedgerAcquire::pointer find(const uint256& hash); LedgerAcquire::pointer find(const uint256& hash);
bool hasLedger(const uint256& ledgerHash); bool hasLedger(const uint256& ledgerHash);
void dropLedger(const uint256& ledgerHash); void dropLedger(const uint256& ledgerHash);
bool gotLedgerData(ripple::TMLedgerData& packet, Peer::ref); SMAddNode gotLedgerData(ripple::TMLedgerData& packet, Peer::ref);
}; };
#endif #endif

View File

@@ -126,21 +126,23 @@ void TransactionAcquire::trigger(Peer::ref peer, bool timer)
resetTimer(); resetTimer();
} }
bool TransactionAcquire::takeNodes(const std::list<SHAMapNode>& nodeIDs, SMAddNode TransactionAcquire::takeNodes(const std::list<SHAMapNode>& nodeIDs,
const std::list< std::vector<unsigned char> >& data, Peer::ref peer) const std::list< std::vector<unsigned char> >& data, Peer::ref peer)
{ {
if (mComplete) if (mComplete)
{ {
cLog(lsTRACE) << "TX set complete"; cLog(lsTRACE) << "TX set complete";
return true; return SMAddNode();
} }
if (mFailed) if (mFailed)
{ {
cLog(lsTRACE) << "TX set failed"; cLog(lsTRACE) << "TX set failed";
return false; return SMAddNode();
} }
try try
{ {
if (nodeIDs.empty())
return SMAddNode::invalid();
std::list<SHAMapNode>::const_iterator nodeIDit = nodeIDs.begin(); std::list<SHAMapNode>::const_iterator nodeIDit = nodeIDs.begin();
std::list< std::vector<unsigned char> >::const_iterator nodeDatait = data.begin(); std::list< std::vector<unsigned char> >::const_iterator nodeDatait = data.begin();
ConsensusTransSetSF sf; ConsensusTransSetSF sf;
@@ -151,12 +153,12 @@ bool TransactionAcquire::takeNodes(const std::list<SHAMapNode>& nodeIDs,
if (mHaveRoot) if (mHaveRoot)
{ {
cLog(lsWARNING) << "Got root TXS node, already have it"; cLog(lsWARNING) << "Got root TXS node, already have it";
return false; return SMAddNode();
} }
if (!mMap->addRootNode(getHash(), *nodeDatait, snfWIRE, NULL)) if (!mMap->addRootNode(getHash(), *nodeDatait, snfWIRE, NULL))
{ {
cLog(lsWARNING) << "TX acquire got bad root node"; cLog(lsWARNING) << "TX acquire got bad root node";
return false; return SMAddNode::invalid();
} }
else else
mHaveRoot = true; mHaveRoot = true;
@@ -164,19 +166,19 @@ bool TransactionAcquire::takeNodes(const std::list<SHAMapNode>& nodeIDs,
else if (!mMap->addKnownNode(*nodeIDit, *nodeDatait, &sf)) else if (!mMap->addKnownNode(*nodeIDit, *nodeDatait, &sf))
{ {
cLog(lsWARNING) << "TX acquire got bad non-root node"; cLog(lsWARNING) << "TX acquire got bad non-root node";
return false; return SMAddNode::invalid();
} }
++nodeIDit; ++nodeIDit;
++nodeDatait; ++nodeDatait;
} }
trigger(peer, false); trigger(peer, false);
progress(); progress();
return true; return SMAddNode::useful();
} }
catch (...) catch (...)
{ {
cLog(lsERROR) << "Peer sends us junky transaction node data"; cLog(lsERROR) << "Peer sends us junky transaction node data";
return false; return SMAddNode::invalid();
} }
} }
@@ -996,14 +998,14 @@ bool LedgerConsensus::peerHasSet(Peer::ref peer, const uint256& hashSet, ripple:
return true; return true;
} }
bool LedgerConsensus::peerGaveNodes(Peer::ref peer, const uint256& setHash, SMAddNode LedgerConsensus::peerGaveNodes(Peer::ref peer, const uint256& setHash,
const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData) const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData)
{ {
boost::unordered_map<uint256, TransactionAcquire::pointer>::iterator acq = mAcquiring.find(setHash); boost::unordered_map<uint256, TransactionAcquire::pointer>::iterator acq = mAcquiring.find(setHash);
if (acq == mAcquiring.end()) if (acq == mAcquiring.end())
{ {
cLog(lsINFO) << "Got TX data for set not acquiring: " << setHash; cLog(lsINFO) << "Got TX data for set not acquiring: " << setHash;
return false; return SMAddNode();
} }
TransactionAcquire::pointer set = acq->second; // We must keep the set around during the function TransactionAcquire::pointer set = acq->second; // We must keep the set around during the function
return set->takeNodes(nodeIDs, nodeData, peer); return set->takeNodes(nodeIDs, nodeData, peer);

View File

@@ -45,7 +45,8 @@ public:
SHAMap::pointer getMap() { return mMap; } SHAMap::pointer getMap() { return mMap; }
bool takeNodes(const std::list<SHAMapNode>& IDs, const std::list< std::vector<unsigned char> >& data, Peer::ref); SMAddNode takeNodes(const std::list<SHAMapNode>& IDs,
const std::list< std::vector<unsigned char> >& data, Peer::ref);
}; };
class LCTransaction class LCTransaction
@@ -184,7 +185,7 @@ public:
bool peerHasSet(Peer::ref peer, const uint256& set, ripple::TxSetStatus status); bool peerHasSet(Peer::ref peer, const uint256& set, ripple::TxSetStatus status);
bool peerGaveNodes(Peer::ref peer, const uint256& setHash, SMAddNode peerGaveNodes(Peer::ref peer, const uint256& setHash,
const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData); const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData);
bool isOurPubKey(const RippleAddress &k) { return k == mValPublic; } bool isOurPubKey(const RippleAddress &k) { return k == mValPublic; }

View File

@@ -813,13 +813,13 @@ SHAMap::pointer NetworkOPs::getTXMap(const uint256& hash)
return mConsensus->getTransactionTree(hash, false); return mConsensus->getTransactionTree(hash, false);
} }
bool NetworkOPs::gotTXData(const boost::shared_ptr<Peer>& peer, const uint256& hash, SMAddNode NetworkOPs::gotTXData(const boost::shared_ptr<Peer>& peer, const uint256& hash,
const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData) const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData)
{ {
if (!haveConsensusObject()) if (!haveConsensusObject())
{ {
cLog(lsWARNING) << "Got TX data with no consensus object"; cLog(lsWARNING) << "Got TX data with no consensus object";
return false; return SMAddNode();
} }
return mConsensus->peerGaveNodes(peer, hash, nodeIDs, nodeData); return mConsensus->peerGaveNodes(peer, hash, nodeIDs, nodeData);
} }

View File

@@ -199,7 +199,7 @@ public:
// ledger proposal/close functions // ledger proposal/close functions
void processTrustedProposal(LedgerProposal::pointer proposal, boost::shared_ptr<ripple::TMProposeSet> set, void processTrustedProposal(LedgerProposal::pointer proposal, boost::shared_ptr<ripple::TMProposeSet> set,
RippleAddress nodePublic, uint256 checkLedger, bool sigGood); RippleAddress nodePublic, uint256 checkLedger, bool sigGood);
bool gotTXData(const boost::shared_ptr<Peer>& peer, const uint256& hash, SMAddNode gotTXData(const boost::shared_ptr<Peer>& peer, const uint256& hash,
const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData); const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData);
bool recvValidation(const SerializedValidation::pointer& val); bool recvValidation(const SerializedValidation::pointer& val);
SHAMap::pointer getTXMap(const uint256& hash); SHAMap::pointer getTXMap(const uint256& hash);

View File

@@ -1432,12 +1432,14 @@ void Peer::recvLedger(ripple::TMLedgerData& packet)
nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size())); nodeIDs.push_back(SHAMapNode(node.nodeid().data(), node.nodeid().size()));
nodeData.push_back(std::vector<unsigned char>(node.nodedata().begin(), node.nodedata().end())); nodeData.push_back(std::vector<unsigned char>(node.nodedata().begin(), node.nodedata().end()));
} }
if (!theApp->getOPs().gotTXData(shared_from_this(), hash, nodeIDs, nodeData)) SMAddNode san = theApp->getOPs().gotTXData(shared_from_this(), hash, nodeIDs, nodeData);
if (san.isInvalid())
punishPeer(PP_UNWANTED_DATA); punishPeer(PP_UNWANTED_DATA);
return; return;
} }
if (!theApp->getMasterLedgerAcquire().gotLedgerData(packet, shared_from_this())) SMAddNode san = theApp->getMasterLedgerAcquire().gotLedgerData(packet, shared_from_this());
if (san.isInvalid())
punishPeer(PP_UNWANTED_DATA); punishPeer(PP_UNWANTED_DATA);
} }

View File

@@ -22,6 +22,12 @@ enum PeerPunish
PP_BAD_SIGNATURE = 4, // Object had bad signature PP_BAD_SIGNATURE = 4, // Object had bad signature
}; };
enum PeerReward
{
PR_NEEDED_DATA = 1,
PR_NEW_TRANSACTION = 2,
};
typedef std::pair<std::string,int> ipPort; typedef std::pair<std::string,int> ipPort;
DEFINE_INSTANCE(Peer); DEFINE_INSTANCE(Peer);

View File

@@ -213,12 +213,28 @@ void ProofOfWorkGenerator::sweep()
void ProofOfWorkGenerator::loadHigh() void ProofOfWorkGenerator::loadHigh()
{ {
// WRITEME time_t now = time(NULL);
boost::mutex::scoped_lock sl(mLock);
if (mLastDifficultyChange == now)
return;
if (mPowEntry == 30)
return;
++mPowEntry;
mLastDifficultyChange = now;
} }
void ProofOfWorkGenerator::loadLow() void ProofOfWorkGenerator::loadLow()
{ {
// WRITEME time_t now = time(NULL);
boost::mutex::scoped_lock sl(mLock);
if (mLastDifficultyChange == now)
return;
if (mPowEntry == 0)
return;
--mPowEntry;
mLastDifficultyChange = now;
} }
struct PowEntry struct PowEntry

View File

@@ -281,6 +281,44 @@ public:
extern std::ostream& operator<<(std::ostream&, const SHAMapMissingNode&); extern std::ostream& operator<<(std::ostream&, const SHAMapMissingNode&);
class SMAddNode
{ // results of adding nodes
protected:
bool mInvalid, mUseful;
SMAddNode(bool i, bool u) : mInvalid(i), mUseful(u) { ; }
public:
SMAddNode() : mInvalid(false), mUseful(false) { ; }
void setInvalid() { mInvalid = true; }
void setUseful() { mUseful = true; }
void reset() { mInvalid = false; mUseful = false; }
bool isInvalid() const { return mInvalid; }
bool isUseful() const { return mUseful; }
bool combine(const SMAddNode& n)
{
if (n.mInvalid)
{
mInvalid = true;
return false;
}
if (n.mUseful)
mUseful = true;
return true;
}
operator bool() const { return !mInvalid; }
static SMAddNode okay() { return SMAddNode(false, false); }
static SMAddNode useful() { return SMAddNode(false, true); }
static SMAddNode invalid() { return SMAddNode(true, false); }
};
extern bool SMANCombine(SMAddNode& existing, const SMAddNode& additional);
class SHAMap : public IS_INSTANCE(SHAMap) class SHAMap : public IS_INSTANCE(SHAMap)
{ {
public: public:
@@ -374,11 +412,11 @@ public:
bool getNodeFat(const SHAMapNode& node, std::vector<SHAMapNode>& nodeIDs, bool getNodeFat(const SHAMapNode& node, std::vector<SHAMapNode>& nodeIDs,
std::list<std::vector<unsigned char> >& rawNode, bool fatRoot, bool fatLeaves); std::list<std::vector<unsigned char> >& rawNode, bool fatRoot, bool fatLeaves);
bool getRootNode(Serializer& s, SHANodeFormat format); bool getRootNode(Serializer& s, SHANodeFormat format);
bool addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format, SMAddNode addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format,
SHAMapSyncFilter* filter); SHAMapSyncFilter* filter);
bool addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format, SMAddNode addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format,
SHAMapSyncFilter* filter); SHAMapSyncFilter* filter);
bool addKnownNode(const SHAMapNode& nodeID, const std::vector<unsigned char>& rawNode, SMAddNode addKnownNode(const SHAMapNode& nodeID, const std::vector<unsigned char>& rawNode,
SHAMapSyncFilter* filter); SHAMapSyncFilter* filter);
// status functions // status functions

View File

@@ -135,7 +135,8 @@ bool SHAMap::getRootNode(Serializer& s, SHANodeFormat format)
return true; return true;
} }
bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format, SHAMapSyncFilter* filter) SMAddNode SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, SHANodeFormat format,
SHAMapSyncFilter* filter)
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
@@ -143,12 +144,12 @@ bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, SHANodeForm
if (root->getNodeHash().isNonZero()) if (root->getNodeHash().isNonZero())
{ {
cLog(lsTRACE) << "got root node, already have one"; cLog(lsTRACE) << "got root node, already have one";
return true; return SMAddNode::okay();
} }
SHAMapTreeNode::pointer node = boost::make_shared<SHAMapTreeNode>(SHAMapNode(), rootNode, 0, format); SHAMapTreeNode::pointer node = boost::make_shared<SHAMapTreeNode>(SHAMapNode(), rootNode, 0, format);
if (!node) if (!node)
return false; return SMAddNode::invalid();
#ifdef DEBUG #ifdef DEBUG
node->dump(); node->dump();
@@ -170,10 +171,10 @@ bool SHAMap::addRootNode(const std::vector<unsigned char>& rootNode, SHANodeForm
filter->gotNode(*root, root->getNodeHash(), s.peekData(), root->getType()); filter->gotNode(*root, root->getNodeHash(), s.peekData(), root->getType());
} }
return true; return SMAddNode::useful();
} }
bool SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format, SMAddNode SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>& rootNode, SHANodeFormat format,
SHAMapSyncFilter* filter) SHAMapSyncFilter* filter)
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
@@ -183,14 +184,12 @@ bool SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>&
{ {
cLog(lsTRACE) << "got root node, already have one"; cLog(lsTRACE) << "got root node, already have one";
assert(root->getNodeHash() == hash); assert(root->getNodeHash() == hash);
return true; return SMAddNode::okay();
} }
SHAMapTreeNode::pointer node = boost::make_shared<SHAMapTreeNode>(SHAMapNode(), rootNode, 0, format); SHAMapTreeNode::pointer node = boost::make_shared<SHAMapTreeNode>(SHAMapNode(), rootNode, 0, format);
if (!node) if (!node || node->getNodeHash() != hash)
return false; return SMAddNode::invalid();
if (node->getNodeHash() != hash)
return false;
returnNode(root, true); returnNode(root, true);
root = node; root = node;
@@ -207,42 +206,42 @@ bool SHAMap::addRootNode(const uint256& hash, const std::vector<unsigned char>&
filter->gotNode(*root, root->getNodeHash(), s.peekData(), root->getType()); filter->gotNode(*root, root->getNodeHash(), s.peekData(), root->getType());
} }
return true; return SMAddNode::useful();
} }
bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned char>& rawNode, SMAddNode SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned char>& rawNode,
SHAMapSyncFilter* filter) SHAMapSyncFilter* filter)
{ // return value: true=okay, false=error { // return value: true=okay, false=error
assert(!node.isRoot()); assert(!node.isRoot());
if (!isSynching()) if (!isSynching())
{ {
cLog(lsINFO) << "AddKnownNode while not synching"; cLog(lsINFO) << "AddKnownNode while not synching";
return true; return SMAddNode::okay();
} }
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if (checkCacheNode(node)) if (checkCacheNode(node))
return true; return SMAddNode::okay();
std::stack<SHAMapTreeNode::pointer> stack = getStack(node.getNodeID(), true, true); std::stack<SHAMapTreeNode::pointer> stack = getStack(node.getNodeID(), true, true);
if (stack.empty()) if (stack.empty())
{ {
cLog(lsWARNING) << "AddKnownNode with empty stack"; cLog(lsWARNING) << "AddKnownNode with empty stack";
return false; return SMAddNode::invalid();
} }
SHAMapTreeNode::pointer iNode = stack.top(); SHAMapTreeNode::pointer iNode = stack.top();
if (!iNode) if (!iNode)
{ // we should always have a root { // we should always have a root
assert(false); assert(false);
return true; return SMAddNode::invalid();
} }
if (iNode->isLeaf() || (iNode->getDepth() >= node.getDepth())) if (iNode->isLeaf() || (iNode->getDepth() >= node.getDepth()))
{ {
cLog(lsTRACE) << "got inner node, already had it (late)"; cLog(lsTRACE) << "got inner node, already had it (late)";
return true; return SMAddNode::okay();
} }
if (iNode->getDepth() != (node.getDepth() - 1)) if (iNode->getDepth() != (node.getDepth() - 1))
@@ -250,25 +249,25 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
cLog(lsWARNING) << "unable to hook node " << node; cLog(lsWARNING) << "unable to hook node " << node;
cLog(lsINFO) << " stuck at " << *iNode; cLog(lsINFO) << " stuck at " << *iNode;
cLog(lsINFO) << "got depth=" << node.getDepth() << ", walked to= " << iNode->getDepth(); cLog(lsINFO) << "got depth=" << node.getDepth() << ", walked to= " << iNode->getDepth();
return false; return SMAddNode::invalid();
} }
int branch = iNode->selectBranch(node.getNodeID()); int branch = iNode->selectBranch(node.getNodeID());
if (branch < 0) if (branch < 0)
{ {
assert(false); assert(false);
return false; return SMAddNode::invalid();
} }
uint256 hash = iNode->getChildHash(branch); uint256 hash = iNode->getChildHash(branch);
if (!hash) if (!hash)
{ {
cLog(lsWARNING) << "AddKnownNode for empty branch"; cLog(lsWARNING) << "AddKnownNode for empty branch";
return false; return SMAddNode::invalid();
} }
SHAMapTreeNode::pointer newNode = boost::make_shared<SHAMapTreeNode>(node, rawNode, mSeq, snfWIRE); SHAMapTreeNode::pointer newNode = boost::make_shared<SHAMapTreeNode>(node, rawNode, mSeq, snfWIRE);
if (hash != newNode->getNodeHash()) // these aren't the droids we're looking for if (hash != newNode->getNodeHash()) // these aren't the droids we're looking for
return false; return SMAddNode::invalid();
if (filter) if (filter)
{ {
@@ -279,7 +278,7 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
mTNByID[*newNode] = newNode; mTNByID[*newNode] = newNode;
if (!newNode->isLeaf()) if (!newNode->isLeaf())
return true; // only a leaf can fill a branch return SMAddNode::useful(); // only a leaf can fill a branch
// did this new leaf cause its parents to fill up // did this new leaf cause its parents to fill up
do do
@@ -294,11 +293,11 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
{ {
SHAMapTreeNode::pointer nextNode = getNode(iNode->getChildNodeID(i), iNode->getChildHash(i), false); SHAMapTreeNode::pointer nextNode = getNode(iNode->getChildNodeID(i), iNode->getChildHash(i), false);
if (nextNode->isInner() && !nextNode->isFullBelow()) if (nextNode->isInner() && !nextNode->isFullBelow())
return true; return SMAddNode::useful();
} }
catch (SHAMapMissingNode&) catch (SHAMapMissingNode&)
{ {
return true; return SMAddNode::useful();
} }
} }
iNode->setFullBelow(); iNode->setFullBelow();
@@ -306,7 +305,7 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
if (root->isFullBelow()) if (root->isFullBelow())
clearSynching(); clearSynching();
return true; return SMAddNode::useful();
} }
bool SHAMap::deepCompare(SHAMap& other) bool SHAMap::deepCompare(SHAMap& other)
@@ -486,6 +485,8 @@ BOOST_AUTO_TEST_CASE( SHAMapSync_test )
cLog(lsFATAL) << "Didn't get root node " << gotNodes.size(); cLog(lsFATAL) << "Didn't get root node " << gotNodes.size();
BOOST_FAIL("NodeSize"); BOOST_FAIL("NodeSize");
} }
SMAddNode node();
if (!destination.addRootNode(*gotNodes.begin(), snfWIRE, NULL)) if (!destination.addRootNode(*gotNodes.begin(), snfWIRE, NULL))
{ {
cLog(lsFATAL) << "AddRootNode fails"; cLog(lsFATAL) << "AddRootNode fails";