diff --git a/Application.h b/Application.h index 5cbe449210..39cbb762ba 100644 --- a/Application.h +++ b/Application.h @@ -8,6 +8,7 @@ #include "PubKeyCache.h" #include "ScopedLock.h" #include "LedgerMaster.h" +#include "TransactionMaster.h" #include "Wallet.h" #include "Peer.h" #include "NetworkOPs.h" @@ -41,6 +42,7 @@ class Application KnownNodeList mKnownNodes; PubKeyCache mPKCache; LedgerMaster mMasterLedger; + TransactionMaster mMasterTransaction; DatabaseCon *mTxnDB, *mLedgerDB, *mWalletDB, *mHashNodeDB, *mNetNodeDB; @@ -70,6 +72,7 @@ public: boost::asio::io_service& getIOService() { return mIOService; } LedgerMaster& getMasterLedger() { return mMasterLedger; } + TransactionMaster& getMasterTransaction() { return mMasterTransaction; } DatabaseCon* getTxnDB() { return mTxnDB; } DatabaseCon* getLedgerDB() { return mLedgerDB; } diff --git a/Makefile b/Makefile index 12a6081995..2cbea7760e 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ SRCS= keystore.cpp BitcoinUtil.cpp \ Application.cpp TimingService.cpp KnownNodeList.cpp ConnectionPool.cpp Peer.cpp \ PeerDoor.cpp RPCDoor.cpp RPCServer.cpp rpc.cpp Conversion.cpp RequestParser.cpp HashedObject.cpp \ UniqueNodeList.cpp PubKeyCache.cpp SHAMapDiff.cpp DeterministicKeys.cpp LedgerMaster.cpp \ - LedgerHistory.cpp NetworkOPs.cpp CallRPC.cpp DBInit.cpp LocalTransaction.cpp + LedgerHistory.cpp NetworkOPs.cpp CallRPC.cpp DBInit.cpp LocalTransaction.cpp TransactionMaster.cpp DBSRCS= SqliteDatabase.cpp database.cpp DBSRCC= sqlite3.c diff --git a/TaggedCache.h b/TaggedCache.h index a77709b811..aff1e06954 100644 --- a/TaggedCache.h +++ b/TaggedCache.h @@ -31,7 +31,7 @@ protected: std::map mMap; // Track stored objects public: - TaggedCache(int size, int age) : mTargetSize(size), mTargetAge(age), mLastSweep(0) { ; } + TaggedCache(int size, int age) : mTargetSize(size), mTargetAge(age), mLastSweep(time(NULL)) { ; } int getTargetSize() const; int getTargetAge() const; diff --git a/Transaction.cpp b/Transaction.cpp index 52c98e3d4d..fee140833e 100644 --- a/Transaction.cpp +++ b/Transaction.cpp @@ -159,6 +159,11 @@ void Transaction::setStatus(TransStatus ts, uint32 lseq) mInLedger=lseq; } +void Transaction::saveTransaction(Transaction::pointer txn) +{ + txn->save(); +} + bool Transaction::save() const { if((mStatus==INVALID)||(mStatus==REMOVED)) return false; diff --git a/Transaction.h b/Transaction.h index d11121b04f..04a72b2be9 100644 --- a/Transaction.h +++ b/Transaction.h @@ -82,6 +82,7 @@ public: void setStatus(TransStatus status) { mStatus=status; } // database functions + static void saveTransaction(Transaction::pointer); bool save() const; static Transaction::pointer load(const uint256& id); static Transaction::pointer findFrom(const uint160& fromID, uint32 seq); diff --git a/TransactionMaster.cpp b/TransactionMaster.cpp new file mode 100644 index 0000000000..1b74621597 --- /dev/null +++ b/TransactionMaster.cpp @@ -0,0 +1,40 @@ + +#include "boost/bind.hpp" + +#include "Application.h" +#include "TransactionMaster.h" + +#ifndef CACHED_TRANSACTION_NUM +#define CACHED_TRANSACTION_NUM 65536 +#endif + +#ifndef CACHED_TRANSACTION_AGE +#define CACHED_TRANSACTION_AGE 1800 +#endif + +TransactionMaster::TransactionMaster() : mCache(CACHED_TRANSACTION_NUM, CACHED_TRANSACTION_AGE) +{ + ; +} + +Transaction::pointer TransactionMaster::fetch(const uint256& txnID, bool checkDisk) +{ + Transaction::pointer txn=mCache.fetch(txnID); + if(!checkDisk || txn) return txn; + + txn=Transaction::load(txnID); + if(!txn) return txn; + + mCache.canonicalize(txnID, txn); + return txn; +} + +bool TransactionMaster::canonicalize(Transaction::pointer& txn, bool may_be_new) +{ + uint256 tid=txn->getID(); + if(!tid) return false; + if(mCache.canonicalize(tid, txn)) return true; + if(may_be_new) + theApp->getIOService().post(boost::bind(&Transaction::saveTransaction, txn)); + return false; +}