From 263fe6f515296a5bc7bfeb4722a0d75bc3aa2bd1 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 13 Dec 2011 12:17:51 -0800 Subject: [PATCH] Begin merging this code. --- LedgerHistory.cpp | 14 ++-------- LedgerHistory.h | 23 +++++----------- LedgerMaster.cpp | 60 +++++++++--------------------------------- LedgerMaster.h | 67 ++++++++++++++++++++++++----------------------- 4 files changed, 55 insertions(+), 109 deletions(-) diff --git a/LedgerHistory.cpp b/LedgerHistory.cpp index d3611873b5..82c8b12820 100644 --- a/LedgerHistory.cpp +++ b/LedgerHistory.cpp @@ -2,19 +2,8 @@ #include "Config.h" #include "Application.h" #include -/* -Soon we should support saving the ledger in a real DB -For now save them all in - -For all the various ledgers we can save just the delta from the combined ledger for that index. - -*/ - -void LedgerHistory::load() -{ - -} +#if 0 bool LedgerHistory::loadLedger(const uint256& hash) { Ledger::pointer ledger=Ledger::pointer(new Ledger()); @@ -68,3 +57,4 @@ Ledger::pointer LedgerHistory::getLedger(const uint256& hash) if(loadLedger(hash)) return(mAllLedgers[hash]); return(Ledger::pointer()); } +#endif diff --git a/LedgerHistory.h b/LedgerHistory.h index 374e978b75..44522bec39 100644 --- a/LedgerHistory.h +++ b/LedgerHistory.h @@ -3,31 +3,22 @@ #include "Ledger.h" -/* -This is a collection of all the ledgers you know about. -One thread of them is the thread you validate. -But you keep others that you have heard about. - -Why do you need to keep them all? - In case people ask. - To make sure that there isn't a conflict between the validated ledgers -*/ - class LedgerHistory { - std::map mAcceptedLedgers; - std::map mAllLedgers; + std::map mLedgersByIndex; + std::map mLedgersByHash; - bool loadAcceptedLedger(uint32 index); + bool loadClosedLedger(uint32 index); bool loadLedger(const uint256& hash); + public: - void load(); + LedgerHistory() { ; } void addLedger(Ledger::pointer ledger); void addAcceptedLedger(Ledger::pointer ledger); - Ledger::pointer getAcceptedLedger(uint32 index); - Ledger::pointer getLedger(const uint256& hash); + Ledger::pointer getLedgerBySeq(uint32 index); + Ledger::pointer getLedgerByHash(const uint256& hash); }; #endif \ No newline at end of file diff --git a/LedgerMaster.cpp b/LedgerMaster.cpp index 77ed22dcbf..67cd5557a0 100644 --- a/LedgerMaster.cpp +++ b/LedgerMaster.cpp @@ -5,44 +5,31 @@ #include "Conversion.h" #include -using namespace std; - -LedgerMaster::LedgerMaster() +LedgerMaster::LedgerMaster() : mIsSynced(false) { - mFinalizingLedger=Ledger::pointer(); - mCurrentLedger=Ledger::pointer(new Ledger(TimingService::getCurrentLedgerIndex())); -} - -void LedgerMaster::load() -{ - mLedgerHistory.load(); -} - -void LedgerMaster::save() -{ - } uint32 LedgerMaster::getCurrentLedgerIndex() { - return(mCurrentLedger->getIndex()); + return mCurrentLedger->getLedgerSeq(); } -int64 LedgerMaster::getAmountHeld(uint160& addr) +uint64 LedgerMaster::getBalance(const uint160& addr) { - return(mCurrentLedger->getAmountHeld(addr)); + return mCurrentLedger->getBalance(addr); } -int64 LedgerMaster::getAmountHeld(std::string& addr) +uint64 LedgerMaster::getBalance(std::string& addr) { - return(mCurrentLedger->getAmountHeld(humanTo160(addr))); + return mCurrentLedger->getBalance(humanTo160(addr)); } +#if 0 -bool LedgerMaster::isTransactionOnFutureList(TransactionPtr needle) +bool LedgerMaster::isTransactionOnFutureList(Transaction::pointer needle) { - BOOST_FOREACH(TransactionPtr straw,mFutureTransactions) + BOOST_FOREACH(Transaction::pointer straw,mFutureTransactions) { if(Transaction::isEqual(straw,needle)) { @@ -57,7 +44,7 @@ bool LedgerMaster::isTransactionOnFutureList(TransactionPtr needle) // make sure from address != dest address // make sure not 0 amount unless null dest (this is just a way to make sure your seqnum is incremented) // make sure the sequence number is good (but the ones with a bad seqnum we need to save still?) -bool LedgerMaster::isValidTransaction(TransactionPtr trans) +bool LedgerMaster::isValidTransaction(Transaction::pointer trans) { if(trans->from()==trans->dest()) return(false); if(trans->amount()==0) return(false); @@ -71,7 +58,7 @@ bool LedgerMaster::isValidTransaction(TransactionPtr trans) // returns true if we should relay it -bool LedgerMaster::addTransaction(TransactionPtr trans) +bool LedgerMaster::addTransaction(Transaction::pointer trans) { if(mFinalizingLedger && (trans->ledgerindex()==mFinalizingLedger->getIndex())) { @@ -127,30 +114,6 @@ bool LedgerMaster::addTransaction(TransactionPtr trans) -void LedgerMaster::addFullLedger(newcoin::FullLedger& ledger) -{ - // check if we already have this ledger - uint256 inHash=protobufTo256(ledger.hash()); - Ledger::pointer existingLedger=mLedgerHistory.getLedger( inHash ); - if(existingLedger) return; - - // check that the hash is correct - Ledger::pointer newLedger=Ledger::pointer(new Ledger(ledger)); - if(newLedger->getHash()==inHash) - { - mLedgerHistory.addLedger(newLedger); - - // add all these in case we are missing some - BOOST_FOREACH(TransactionPtr trans, newLedger->getTransactions()) - { - addTransaction(trans); - } - - }else cout << "We were sent a bad ledger hash" << endl; - -} - - void LedgerMaster::startFinalization() { mFinalizingLedger=mCurrentLedger; @@ -299,3 +262,4 @@ theApp->getConnectionPool().relayMessage(NULL,msg); } */ +#endif diff --git a/LedgerMaster.h b/LedgerMaster.h index 81826f6d8a..395eb9432e 100644 --- a/LedgerMaster.h +++ b/LedgerMaster.h @@ -7,56 +7,57 @@ #include "types.h" #include "Transaction.h" -/* -Handles: - collecting the current ledger - finalizing the ledger - validating the past ledger - keeping the ledger history +// Tracks the current ledger and any ledgers in the process of closing +// Tracks ledger history +// Tracks held transactions - There is one ledger we are working on. - -*/ class LedgerMaster { + bool mIsSynced; + Ledger::pointer mCurrentLedger; Ledger::pointer mFinalizingLedger; LedgerHistory mLedgerHistory; - std::list mFutureTransactions; - std::list< std::pair > mFutureProposals; - //bool mAfterProposed; + std::map mHeldTransactionsByID; + std::map mHeldTransactionsByLedger; - - void addFutureProposal(Peer::pointer peer,newcoin::ProposeLedger& packet); - void applyFutureProposals(uint32 ledgerIndex); void applyFutureTransactions(uint32 ledgerIndex); - bool isValidTransaction(TransactionPtr trans); - bool isTransactionOnFutureList(TransactionPtr trans); + bool isValidTransaction(Transaction::pointer trans); + bool isTransactionOnFutureList(Transaction::pointer trans); + public: + LedgerMaster(); - void load(); - void save(); - uint32 getCurrentLedgerIndex(); + bool IsSynced(void) { return mIsSynced; } + void SetSynced(void) { mIsSynced=true; } - Ledger::pointer getAcceptedLedger(uint32 index){ return(mLedgerHistory.getAcceptedLedger(index)); } - Ledger::pointer getLedger(const uint256& hash){ return(mLedgerHistory.getLedger(hash)); } + Ledger::pointer getCurrentLedger() { return mCurrentLedger; } + Ledger::pointer getClosingLedger() { return mFinalizingLedger; } - int64 getAmountHeld(std::string& addr); - int64 getAmountHeld(uint160& addr); - Ledger::Account* getAccount(uint160& addr){ return(mCurrentLedger->getAccount(addr)); } + Ledger::pointer getLedgerBySeq(uint32 index) + { + if(mCurrentLedger && (mCurrentLedger->getLedgerSeq()==index)) return mCurrentLedger; + if(mFinalizingLedger && (mFinalizingLedger->getLedgerSeq()==index)) return mFinalizingLedger; + return mLedgerHistory.getLedgerBySeq(index); + } - bool addTransaction(TransactionPtr trans); - void addFullLedger(newcoin::FullLedger& ledger); + Ledger::pointer getLedgerByHash(const uint256& hash) + { + if(mCurrentLedger && (mCurrentLedger->getHash()==hash)) return mCurrentLedger; + if(mFinalizingLedger && (mFinalizingLedger->getHash()==hash)) return mFinalizingLedger; + return mLedgerHistory.getLedgerByHash(hash); + } - void startFinalization(); - void sendProposal(); - void endFinalization(); - void checkLedgerProposal(Peer::pointer peer,newcoin::ProposeLedger& packet); - void checkConsensus(uint32 ledgerIndex); + uint64 getBalance(std::string& addr); + uint64 getBalance(const uint160& addr); + AccountState::pointer getAccountState(const uint160& addr) + { return mCurrentLedger->getAccountState(addr); } + + bool addTransaction(Transaction::pointer trans); }; -#endif \ No newline at end of file +#endif