Major updates to the ledger tracking system.

This commit is contained in:
JoelKatz
2012-10-25 01:50:07 -07:00
parent 390da86192
commit ef40099649
9 changed files with 98 additions and 203 deletions

View File

@@ -580,111 +580,4 @@ bool LedgerAcquireMaster::gotLedgerData(ripple::TMLedgerData& packet, Peer::ref
return false;
}
LedgerAcquireSet::LedgerAcquireSet(Ledger::ref targetLedger) :
mTargetLedger(targetLedger), mCheckComplete(true)
{
}
void LedgerAcquireSet::updateCurrentLedger(Ledger::pointer currentLedger)
{ // We have 'currentLedger', what do we need to acquire next
while (1)
{
if (mTargetLedger)
{
if ((currentLedger->getHash() == mTargetLedger->getHash()) ||
(currentLedger->getParentHash() == mTargetLedger->getHash()))
{ // We have completed acquiring the set
done();
return;
}
while (mTargetLedger->getLedgerSeq() >= currentLedger->getLedgerSeq())
{ // We need to back up our target
mTargetLedger = theApp->getMasterLedger().getLedgerByHash(mTargetLedger->getParentHash());
if (!mTargetLedger)
{
cLog(lsWARNING) << "LedgerAcquireSet encountered a non-present target ledger";
done();
return;
}
}
}
Ledger::pointer nextLedger =
theApp->getMasterLedger().getLedgerByHash(currentLedger->getParentHash());
if (nextLedger && mCheckComplete && !nextLedger->walkLedger())
{
cLog(lsINFO) << "Acquire set has encountered a ledger that is missing nodes";
nextLedger = Ledger::pointer();
}
if (!nextLedger)
{ // the next ledger we need is missing or missing nodes
LedgerAcquire::pointer nextAcquire =
theApp->getMasterLedgerAcquire().findCreate(currentLedger->getParentHash());
nextAcquire->setAccept();
if (mCurrentLedger)
nextAcquire->takePeerSetFrom(*mCurrentLedger);
mCurrentLedger = nextAcquire;
if (!mCurrentLedger->getPeerCount())
addPeers();
mCurrentLedger->addOnComplete(boost::bind(
&LedgerAcquireSet::onComplete, boost::weak_ptr<LedgerAcquireSet>(shared_from_this()), _1));
return;
}
currentLedger = nextLedger;
}
}
void LedgerAcquireSet::onComplete(boost::weak_ptr<LedgerAcquireSet> set, LedgerAcquire::pointer acquired)
{
LedgerAcquireSet::pointer lSet = set.lock();
if (!lSet)
return;
if (acquired->isComplete())
{
Ledger::pointer ledger = acquired->getLedger();
assert(ledger);
cLog(lsDEBUG) << "LedgerAcquireSet::onComplete " << ledger->getLedgerSeq();
ledger->setAccepted();
theApp->getMasterLedger().checkLedgerGap(ledger);
lSet->updateCurrentLedger(ledger);
}
else
{
cLog(lsWARNING) << "Bailing on LedgerAcquireSet due to failure to acquire a ledger";
lSet->done();
}
}
void LedgerAcquireSet::done()
{
theApp->getMasterLedgerAcquire().killSet(*this);
}
void LedgerAcquireSet::addPeers()
{
std::vector<Peer::pointer> peerList = theApp->getConnectionPool().getPeerVector();
const uint256& hash = mCurrentLedger->getHash();
bool found = false;
BOOST_FOREACH(Peer::ref peer, peerList)
{
if (peer->hasLedger(hash))
{
found = true;
mCurrentLedger->peerHas(peer);
}
}
if (!found)
{
BOOST_FOREACH(Peer::ref peer, peerList)
mCurrentLedger->peerHas(peer);
}
}
// vim:ts=4