From 715bff43ef500c5273773d4ca4ac0e2f43de0728 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 16 Dec 2011 21:51:05 -0800 Subject: [PATCH] Micro-optimizations. --- LedgerHistory.cpp | 58 +--------------------------- LedgerMaster.cpp | 96 +---------------------------------------------- 2 files changed, 3 insertions(+), 151 deletions(-) diff --git a/LedgerHistory.cpp b/LedgerHistory.cpp index a676d16d6..0e86e05b1 100644 --- a/LedgerHistory.cpp +++ b/LedgerHistory.cpp @@ -11,7 +11,7 @@ void LedgerHistory::addLedger(Ledger::pointer ledger) { uint256 h(ledger->getHash()); boost::recursive_mutex::scoped_lock sl(mLock); - if(!mLedgersByHash.count(h)) mLedgersByHash.insert(std::make_pair(h, ledger)); + mLedgersByHash.insert(std::make_pair(h, ledger)); } void LedgerHistory::addAcceptedLedger(Ledger::pointer ledger) @@ -69,59 +69,3 @@ Ledger::pointer LedgerHistory::canonicalizeLedger(Ledger::pointer ledger, bool s if(ledger->isAccepted()) mLedgersByIndex.insert(std::make_pair(ledger->getLedgerSeq(), ledger)); return ledger; } - -#if 0 -bool LedgerHistory::loadLedger(const uint256& hash) -{ - Ledger::pointer ledger=Ledger::pointer(new Ledger()); - if(ledger->load(hash)) - { - mAllLedgers[hash]=ledger; - return(true); - } - return(false); -} - -bool LedgerHistory::loadAcceptedLedger(uint32 index) -{ - // TODO: LedgerHistory::loadAcceptedLedger(uint32 index) - /* - Ledger::pointer ledger=theApp->getSerializer()->loadAcceptedLedger(index); - if(ledger) - { - mAcceptedLedgers[index]=ledger; - return(true); - }*/ - return(false); -} - -void LedgerHistory::addAcceptedLedger(Ledger::pointer ledger) -{ - mAcceptedLedgers[ledger->getIndex()]=ledger; -} - -// this will see if the ledger is in memory -// if not it will check disk and load it -// if not it will return NULL -Ledger::pointer LedgerHistory::getAcceptedLedger(uint32 index) -{ - if(mAcceptedLedgers.count(index)) - return(mAcceptedLedgers[index]); - if(loadAcceptedLedger(index)) return(mAcceptedLedgers[index]); - return(Ledger::pointer()); -} - -void LedgerHistory::addLedger(Ledger::pointer ledger) -{ - mAcceptedLedgers[ledger->getIndex()]=ledger; - ledger->save(); -} - -Ledger::pointer LedgerHistory::getLedger(const uint256& hash) -{ - if(mAllLedgers.count(hash)) - return(mAllLedgers[hash]); - if(loadLedger(hash)) return(mAllLedgers[hash]); - return(Ledger::pointer()); -} -#endif diff --git a/LedgerMaster.cpp b/LedgerMaster.cpp index bf723f2a9..d8958180b 100644 --- a/LedgerMaster.cpp +++ b/LedgerMaster.cpp @@ -25,14 +25,9 @@ uint64 LedgerMaster::getBalance(std::string& addr) } bool LedgerMaster::addHeldTransaction(Transaction::pointer transaction) -{ +{ // returns true if transaction was added boost::recursive_mutex::scoped_lock ml(mLock); - if(mHeldTransactionsByID.count(transaction->getID())==0) - { - mHeldTransactionsByID.insert(std::make_pair(transaction->getID(), transaction)); - return true; - } - return false; + return mHeldTransactionsByID.insert(std::make_pair(transaction->getID(), transaction)).second; } void LedgerMaster::pushLedger(Ledger::pointer newLedger) @@ -50,93 +45,6 @@ void LedgerMaster::pushLedger(Ledger::pointer newLedger) #if 0 -bool LedgerMaster::isTransactionOnFutureList(Transaction::pointer needle) -{ - BOOST_FOREACH(Transaction::pointer straw,mFutureTransactions) - { - if(Transaction::isEqual(straw,needle)) - { - return(true); - } - } - return(false); -} - - -// make sure the signature is valid -// 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(Transaction::pointer trans) -{ - if(trans->from()==trans->dest()) return(false); - if(trans->amount()==0) return(false); - if(!Transaction::isSigValid(trans)) return(false); - Ledger::Account* account=mCurrentLedger->getAccount( protobufTo160(trans->from()) ); - if(!account) return(false); - if(trans->seqnum() != (account->second+1) ) return(false); // TODO: do we need to save these? - - return(true); -} - - -// returns true if we should relay it -bool LedgerMaster::addTransaction(Transaction::pointer trans) -{ - if(mFinalizingLedger && (trans->ledgerindex()==mFinalizingLedger->getIndex())) - { - if(mFinalizingLedger->hasTransaction(trans)) return(false); - if(!isValidTransaction(trans)) return(false); - - if(mFinalizingLedger->addTransaction(trans,false)) - { - // TODO: we shouldn't really sendProposal right here - // TODO: since maybe we are adding a whole bunch at once. we should send at the end of the batch - // TODO: do we ever really need to re-propose? - //if(mAfterProposed) sendProposal(); - theApp->getWallet().transactionChanged(trans); - return(true); - }else return(false); - }else if(trans->ledgerindex()==mCurrentLedger->getIndex()) - { - if(mCurrentLedger->hasTransaction(trans)) return(false); - if(!isValidTransaction(trans)) return(false); - if(!mCurrentLedger->addTransaction(trans,false)) return(false); - theApp->getWallet().transactionChanged(trans); - return( true ); - }else if(trans->ledgerindex()>mCurrentLedger->getIndex()) - { // in the future - - if(isTransactionOnFutureList(trans)) return(false); - if(!isValidTransaction(trans)) return(false); - mFutureTransactions.push_back(trans); // broadcast once we get to that ledger. - return(false); - }else - { // transaction is old but we don't have it. Add it to the current ledger - cout << "Old Transaction" << endl; - - // distant past - // This is odd make sure the transaction is valid before proceeding since checking all the past is expensive - if(! isValidTransaction(trans)) return(false); - - uint32 checkIndex=trans->ledgerindex(); - while(checkIndex <= mCurrentLedger->getIndex()) - { - Ledger::pointer ledger=mLedgerHistory.getAcceptedLedger(checkIndex); - if(ledger) - { - if(ledger->hasTransaction(trans)) return(false); - } - checkIndex++; - } - if(!mCurrentLedger->addTransaction(trans,false)) return(false); - theApp->getWallet().transactionChanged(trans); - return(true); - } -} - - - void LedgerMaster::startFinalization() { mFinalizingLedger=mCurrentLedger;