From a2be7ab180614be71eb346286bfec4d2d4dd78f6 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 30 May 2012 22:15:20 -0700 Subject: [PATCH] Complete ledger consensus logic. Now to test it. --- src/LedgerConsensus.cpp | 43 +++++++++++++++++++++++++---------------- src/LedgerConsensus.h | 3 +++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 4ffac1f2dc..40dbceb652 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -6,6 +6,7 @@ #include "Application.h" #include "NetworkOPs.h" #include "LedgerTiming.h" +#include "SerializedValidation.h" TransactionAcquire::TransactionAcquire(const uint256& hash) : PeerSet(hash, 1), mHaveRoot(false) { @@ -543,13 +544,10 @@ void LedgerConsensus::Saccept(boost::shared_ptr This, SHAMap::p This->accept(txSet); } -void LedgerConsensus::accept(SHAMap::pointer set) +void LedgerConsensus::applyTransactions(SHAMap::pointer set, Ledger::pointer ledger, + std::deque& failedTransactions) { - std::cerr << "Computing new LCL based on network consensus" << std::endl; - Ledger::pointer newLCL = boost::make_shared(mPreviousLedger); - - std::deque failedTransactions; - TransactionEngine engine(newLCL); + TransactionEngine engine(ledger); SHAMapItem::pointer item = set->peekFirstItem(); while (item) @@ -563,18 +561,18 @@ void LedgerConsensus::accept(SHAMap::pointer set) if (result > 0) { std::cerr << " retry" << std::endl; - assert(!newLCL->hasTransaction(item->getTag())); + assert(!ledger->hasTransaction(item->getTag())); failedTransactions.push_back(txn); } else if (result == 0) { std::cerr << " success" << std::endl; - assert(newLCL->hasTransaction(item->getTag())); + assert(ledger->hasTransaction(item->getTag())); } else { std::cerr << " hard fail" << std::endl; - assert(!newLCL->hasTransaction(item->getTag())); + assert(!ledger->hasTransaction(item->getTag())); } } catch (...) @@ -611,24 +609,35 @@ void LedgerConsensus::accept(SHAMap::pointer set) } } } while (successes > 0); +} +void LedgerConsensus::accept(SHAMap::pointer set) +{ + std::cerr << "Computing new LCL based on network consensus" << std::endl; + Ledger::pointer newLCL = boost::make_shared(mPreviousLedger); + + std::deque failedTransactions; + applyTransactions(set, newLCL, failedTransactions); newLCL->setAccepted(); + newLCL->updateHash(); + uint256 newLCLHash = newLCL->getHash(); + Ledger::pointer newOL = boost::make_shared(newLCL); ScopedLock sl = theApp->getMasterLedger().getLock(); - // take transactions from new open ledger, try to process them WRITEME - - // try one last time to process held transactions WRITEME - + applyTransactions(theApp->getMasterLedger().getCurrentLedger()->peekTransactionMap(), + newOL, failedTransactions); theApp->getMasterLedger().pushLedger(newLCL, newOL); - - // Send a network state change WRITEME - // Change the consensus state to lcsACCEPTED + mState = lcsACCEPTED; sl.unlock(); - // make and send validations WRITEME + SerializedValidation v(newLCLHash, mOurPosition->peekKey(), true); + std::vector validation = v.getSigned(); + newcoin::TMValidation val; + val.set_validation(&validation[0], validation.size()); + theApp->getConnectionPool().relayMessage(NULL, boost::make_shared(val, newcoin::mtVALIDATION)); } void LedgerConsensus::endConsensus() diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index b90e7b3ee5..3463149870 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -2,6 +2,7 @@ #define __LEDGER_CONSENSUS__ #include +#include #include #include @@ -112,6 +113,8 @@ protected: void removePosition(LedgerProposal&, bool ours); void sendHaveTxSet(const std::vector& txSetHashes); void closeLedger(); + void applyTransactions(SHAMap::pointer transactionSet, Ledger::pointer targetLedger, + std::deque& failedTransactions); // manipulating our own position void takeInitialPosition(Ledger::pointer initialLedger);