From c891df0ef8c6d1f4e733ccb6f218914204cab229 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 4 Dec 2011 19:32:16 -0800 Subject: [PATCH] Complete public key cache logic and tie to application object. --- Application.h | 13 ++++++++----- PubKeyCache.cpp | 8 +++++--- PubKeyCache.h | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Application.h b/Application.h index f1260050dc..b8929e5cf9 100644 --- a/Application.h +++ b/Application.h @@ -5,6 +5,7 @@ #include "ConnectionPool.h" #include "KnownNodeList.h" #include "TimingService.h" +#include "PubKeyCache.h" #include "ScopedLock.h" #include "Wallet.h" #include "database/database.h" @@ -21,6 +22,7 @@ class Application UniqueNodeList mUNL; KnownNodeList mKnownNodes; Wallet mWallet; + PubKeyCache mPKCache; Database* mDatabase; @@ -36,13 +38,14 @@ class Application public: Application(); - ConnectionPool& getConnectionPool(){ return(mConnectionPool); } - UniqueNodeList& getUNL(){ return(mUNL); } - Wallet& getWallet(){ return(mWallet); } - Database* getDB(){ return(mDatabase); } + ConnectionPool& getConnectionPool() { return(mConnectionPool); } + UniqueNodeList& getUNL() { return(mUNL); } + Wallet& getWallet() { return(mWallet); } + PubKeyCache& getPubKeyCache() { return mPKCache; } + Database* getDB() { return(mDatabase); } ScopedLock getDBLock() { return ScopedLock(dbLock); } - void setDB(Database* db){ mDatabase=db; } + void setDB(Database* db) { mDatabase=db; } //Serializer* getSerializer(){ return(mSerializer); } //void setSerializer(Serializer* ser){ mSerializer=ser; } diff --git a/PubKeyCache.cpp b/PubKeyCache.cpp index db9a6df9b4..0350a2aac2 100644 --- a/PubKeyCache.cpp +++ b/PubKeyCache.cpp @@ -43,12 +43,13 @@ CKey::pointer PubKeyCache::locate(const uint160& id) return ckp; } -void PubKeyCache::store(const uint160& id, CKey::pointer key) -{ +CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key) +{ // stored if needed, returns cached copy (possibly the original) if(1) { boost::mutex::scoped_lock sl(mLock); - if(mCache[id]) return; + CKey::pointer cached(mCache[id]); + if(cached) return cached; mCache[id]=key; } std::string sql="INSERT INTO PubKeys (ID, PubKey) VALUES ('"; @@ -62,6 +63,7 @@ void PubKeyCache::store(const uint160& id, CKey::pointer key) sql.append(";"); ScopedLock sl(theApp->getDBLock()); theApp->getDB()->executeSQL(sql.c_str()); + return key; } void PubKeyCache::clear() diff --git a/PubKeyCache.h b/PubKeyCache.h index 79db370cb8..c2ffd82af6 100644 --- a/PubKeyCache.h +++ b/PubKeyCache.h @@ -18,7 +18,7 @@ public: PubKeyCache() { ; } CKey::pointer locate(const uint160& id); - void store(const uint160& id, CKey::pointer key); + CKey::pointer store(const uint160& id, CKey::pointer key); void clear(void); };