Add callback support.

This commit is contained in:
JoelKatz
2012-12-19 09:37:05 -08:00
parent 07e4da4f41
commit edcd8286d2
2 changed files with 49 additions and 7 deletions

View File

@@ -1,10 +1,30 @@
#include "TransactionQueue.h"
#include <boost/foreach.hpp>
void TXQEntry::addCallbacks(const TXQEntry& otherEntry)
{
BOOST_FOREACH(const stCallback& callback, otherEntry.mCallbacks)
mCallbacks.push_back(callback);
}
void TXQEntry::doCallbacks(TER result)
{
BOOST_FOREACH(const stCallback& callback, mCallbacks)
callback(mTxn, result);
}
bool TXQueue::addEntryForSigCheck(TXQEntry::ref entry)
{ // we always dispatch a thread to check the signature
boost::mutex::scoped_lock sl(mLock);
return mTxMap.insert(valueType(entry->getID(), entry)).second;
if (!mTxMap.insert(valueType(entry->getID(), entry)).second)
{
if (!entry->mCallbacks.empty())
mTxMap.left.find(entry->getID())->second->addCallbacks(*entry);
return false;
}
return true;
}
bool TXQueue::addEntryForExecution(TXQEntry::ref entry)
@@ -12,8 +32,14 @@ bool TXQueue::addEntryForExecution(TXQEntry::ref entry)
boost::mutex::scoped_lock sl(mLock);
entry->mSigChecked = true;
if (!mTxMap.insert(valueType(entry->getID(), entry)).second)
mTxMap.left.find(entry->getID())->second->mSigChecked = true;
std::pair<mapType::iterator, bool> it = mTxMap.insert(valueType(entry->getID(), entry));
if (!it.second)
{ // There was an existing entry
it.first->right->mSigChecked = true;
if (!entry->mCallbacks.empty())
it.first->right->addCallbacks(*entry);
}
if (mRunning)
return false;
@@ -22,11 +48,20 @@ bool TXQueue::addEntryForExecution(TXQEntry::ref entry)
return true; // A thread needs to handle this account
}
void TXQueue::removeEntry(const uint256& id)
TXQEntry::pointer TXQueue::removeEntry(const uint256& id)
{
TXQEntry::pointer ret;
boost::mutex::scoped_lock sl(mLock);
mTxMap.left.erase(id);
mapType::left_map::iterator it = mTxMap.left.find(id);
if (it != mTxMap.left.end())
{
ret = it->second;
mTxMap.left.erase(it);
}
return ret;
}
void TXQueue::getJob(TXQEntry::pointer &job)

View File

@@ -3,6 +3,7 @@
// Allow transactions to be signature checked out of sequence but retired in sequence
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bimap.hpp>
@@ -20,10 +21,14 @@ class TXQEntry
public:
typedef boost::shared_ptr<TXQEntry> pointer;
typedef const boost::shared_ptr<TXQEntry>& ref;
typedef boost::function<void (Transaction::pointer, TER)> stCallback; // must complete immediately
protected:
Transaction::pointer mTxn;
bool mSigChecked;
std::list<stCallback> mCallbacks;
void addCallbacks(const TXQEntry& otherEntry);
public:
TXQEntry(Transaction::ref tx, bool sigChecked) : mTxn(tx), mSigChecked(sigChecked) { ; }
@@ -32,6 +37,8 @@ public:
Transaction::ref getTransaction() const { return mTxn; }
bool getSigChecked() const { return mSigChecked; }
const uint256& getID() const { return mTxn->getID(); }
void doCallbacks(TER);
};
class TXQueue
@@ -56,8 +63,8 @@ public:
// Call only if signature is okay. Returns true if new account, must dispatch
bool addEntryForExecution(TXQEntry::ref);
// Call if signature is bad
void removeEntry(const uint256& txID);
// Call if signature is bad (returns entry so you can run its callbacks)
TXQEntry::pointer removeEntry(const uint256& txID);
// Transaction execution interface
void getJob(TXQEntry::pointer&);