From 0af5d379c2b1a77bfc954e7a8d02bd518cb4faa9 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 20 Jun 2012 04:23:29 -0700 Subject: [PATCH] Fix set: 1) Create validation structure even if not proposing (but if validating) 2) Fix hashes in status change messages, do it only one way 3) Don't spin right at wobble time. 4) Report accept status change on new last-closed ledger, not new open ledger. 5) Make sure validation count is correct even if we first encounter a ledger on ourselves (not a peer) 6) At end of consensus, assume nodes cycled (until they report otherwise). By doing nothing, we effectively assumed they didn't. 7) Miscellaneous cleanups and fixes. --- src/LedgerConsensus.cpp | 39 ++++++++++++++++++++++----------------- src/NetworkOPs.cpp | 38 ++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 93e977019..073723ec0 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -16,6 +16,8 @@ // #define LC_DEBUG +// TODO: If we don't have the previousLCL, check if we got it. If so, change modes + TransactionAcquire::TransactionAcquire(const uint256& hash) : PeerSet(hash, 1), mFilter(&theApp->getNodeCache()), mHaveRoot(false) { @@ -224,7 +226,7 @@ void LedgerConsensus::takeInitialPosition(Ledger::pointer initialLedger) } } - if (mProposing) + if (mValidating) mOurPosition = boost::make_shared (mValSeed, initialLedger->getParentHash(), txSet); else @@ -319,15 +321,22 @@ void LedgerConsensus::adjustCount(SHAMap::pointer map, const std::vectorgetLedgerSeq()); - s.set_networktime(theApp->getOPs().getNetworkTimeNC()); - uint256 plhash = ledger->getParentHash(); - s.set_previousledgerhash(plhash.begin(), plhash.size()); + if (!mHaveCorrectLCL) + s.set_newevent(newcoin::neLOST_SYNC); + else + { + s.set_newevent(event); + s.set_ledgerseq(ledger->getLedgerSeq()); + s.set_networktime(theApp->getOPs().getNetworkTimeNC()); + uint256 hash = ledger->getParentHash(); + s.set_previousledgerhash(hash.begin(), hash.size()); + hash = ledger->getHash(); + s.set_ledgerhash(hash.begin(), hash.size()); + } PackedMessage::pointer packet = boost::make_shared(s, newcoin::mtSTATUS_CHANGE); theApp->getConnectionPool().relayMessage(NULL, packet); + Log(lsINFO) << "send status change to peer"; } void LedgerConsensus::abort() @@ -751,15 +760,6 @@ void LedgerConsensus::accept(SHAMap::pointer set) uint256 newLCLHash = newLCL->getHash(); Log(lsTRACE) << "newLCL " << newLCLHash.GetHex(); -#ifdef DEBUG - if (1) - { - Log(lsTRACE) << "newLCL after transactions"; - Json::Value p; - newLCL->addJson(p, LEDGER_JSON_DUMP_TXNS | LEDGER_JSON_DUMP_STATE); - ssw.write(Log(lsTRACE).ref(), p); - } -#endif Ledger::pointer newOL = boost::make_shared(true, boost::ref(*newLCL)); @@ -815,6 +815,10 @@ void LedgerConsensus::accept(SHAMap::pointer set) Json::Value p; newOL->addJson(p, LEDGER_JSON_DUMP_TXNS | LEDGER_JSON_DUMP_STATE); ssw.write(Log(lsTRACE).ref(), p); + Log(lsINFO) << "newLCL after transactions"; + Json::Value p; + newLCL->addJson(p, LEDGER_JSON_DUMP_TXNS | LEDGER_JSON_DUMP_STATE); + ssw.write(Log(lsTRACE).ref(), p); } #endif @@ -831,7 +835,8 @@ void LedgerConsensus::accept(SHAMap::pointer set) theApp->getConnectionPool().relayMessage(NULL, boost::make_shared(val, newcoin::mtVALIDATION)); Log(lsINFO) << "Validation sent " << newLCL->getHash().GetHex(); } - statusChange(newcoin::neACCEPTED_LEDGER, newOL); + else Log(lsWARNING) << "Not validating"; + statusChange(newcoin::neACCEPTED_LEDGER, newLCL); } void LedgerConsensus::endConsensus() diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index 3fb47feb7..af8b8ad5a 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -251,7 +251,7 @@ void NetworkOPs::setStateTimer(int sec) uint64 consensusTime = mLedgerMaster->getCurrentLedger()->getCloseTimeNC() - LEDGER_WOBBLE_TIME; uint64 now = getNetworkTimeNC(); - if (now >= consensusTime) sec = 0; + if (now >= consensusTime) sec = 1; else if (sec > (consensusTime - now)) sec = (consensusTime - now); } mNetTimer.expires_from_now(boost::posix_time::seconds(sec)); @@ -308,7 +308,6 @@ void NetworkOPs::checkState(const boost::system::error_code& result) return; } - // FIXME: Don't check unless last closed ledger is at least some seconds old // If full or tracking, check only at wobble time! if (checkLastClosedLedger(peerList)) @@ -374,9 +373,8 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis if (!!peerLedger) { // FIXME: If we have this ledger, don't count it if it's too far past its close time - bool isNew = ledgers.find(peerLedger) == ledgers.end(); ValidationCount& vc = ledgers[peerLedger]; - if (isNew) + if (vc.nodesUsing == 0) { theApp->getValidations().getValidationCount(peerLedger, vc.trustedValidations, vc.untrustedValidations); @@ -392,19 +390,27 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis Ledger::pointer currentClosed = mLedgerMaster->getClosedLedger(); uint256 closedLedger = currentClosed->getHash(); - ValidationCount& vc = ledgers[closedLedger]; - if ((vc.nodesUsing == 0) || (theApp->getWallet().getNodePublic() > vc.highNode)) - vc.highNode = theApp->getWallet().getNodePublic(); - ++ledgers[closedLedger].nodesUsing; + ValidationCount& ourVC = ledgers[closedLedger]; + if (ourVC.nodesUsing == 0) + { + ourVC.highNode = theApp->getWallet().getNodePublic(); + theApp->getValidations().getValidationCount(closedLedger, + ourVC.trustedValidations, ourVC.untrustedValidations); + } + ++ourVC.nodesUsing; + ValidationCount bestVC = ourVC; // 3) Is there a network ledger we'd like to switch to? If so, do we have it? bool switchLedgers = false; for(boost::unordered_map::iterator it = ledgers.begin(), end = ledgers.end(); it != end; ++it) { - if (it->second > vc) + Log(lsTRACE) << "L: " << it->first.GetHex() << + " t=" << it->second.trustedValidations << ", u=" << it->second.untrustedValidations << + ", n=" << it->second.nodesUsing; + if (it->second > bestVC) { - vc = it->second; + bestVC = it->second; closedLedger = it->first; switchLedgers = true; } @@ -464,8 +470,10 @@ void NetworkOPs::switchLastClosedLedger(Ledger::pointer newLedger) s.set_newevent(newcoin::neSWITCHED_LEDGER); s.set_ledgerseq(newLedger->getLedgerSeq()); s.set_networktime(theApp->getOPs().getNetworkTimeNC()); - uint256 plhash = newLedger->getParentHash(); - s.set_previousledgerhash(plhash.begin(), plhash.size()); + uint256 hash = newLedger->getParentHash(); + s.set_previousledgerhash(hash.begin(), hash.size()); + hash = newLedger->getHash(); + s.set_ledgerhash(hash.begin(), hash.size()); PackedMessage::pointer packet = boost::make_shared(s, newcoin::mtSTATUS_CHANGE); theApp->getConnectionPool().relayMessage(NULL, packet); } @@ -566,6 +574,12 @@ void NetworkOPs::mapComplete(const uint256& hash, SHAMap::pointer map) void NetworkOPs::endConsensus() { + uint256 deadLedger = theApp->getMasterLedger().getClosedLedger()->getParentHash(); + Log(lsTRACE) << "Ledger " << deadLedger.GetHex() << " is now dead"; + std::vector peerList = theApp->getConnectionPool().getPeerVector(); + for (std::vector::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) + if (*it && ((*it)->getClosedLedgerHash() == deadLedger)) + (*it)->cycleStatus(); mConsensus = boost::shared_ptr(); }