mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Fix ledger acquire timer logic.
This commit is contained in:
@@ -1,12 +1,17 @@
|
|||||||
|
|
||||||
#include "boost/foreach.hpp"
|
|
||||||
#include "boost/make_shared.hpp"
|
|
||||||
|
|
||||||
#include "Application.h"
|
|
||||||
#include "LedgerAcquire.h"
|
#include "LedgerAcquire.h"
|
||||||
|
|
||||||
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/make_shared.hpp>
|
||||||
|
#include <boost/bind.hpp>
|
||||||
|
|
||||||
|
#include "Application.h"
|
||||||
|
|
||||||
|
#define LEDGER_ACQUIRE_TIMEOUT 2
|
||||||
|
|
||||||
LedgerAcquire::LedgerAcquire(const uint256& hash) : mHash(hash),
|
LedgerAcquire::LedgerAcquire(const uint256& hash) : mHash(hash),
|
||||||
mComplete(false), mFailed(false), mHaveBase(false), mHaveState(false), mHaveTransactions(false)
|
mComplete(false), mFailed(false), mHaveBase(false), mHaveState(false), mHaveTransactions(false),
|
||||||
|
mTimer(theApp->getIOService())
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cerr << "Acquiring ledger " << mHash.GetHex() << std::endl;
|
std::cerr << "Acquiring ledger " << mHash.GetHex() << std::endl;
|
||||||
@@ -29,15 +34,18 @@ void LedgerAcquire::done()
|
|||||||
triggers[i](shared_from_this());
|
triggers[i](shared_from_this());
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedgerAcquire::setTimer()
|
void LedgerAcquire::resetTimer()
|
||||||
{
|
{
|
||||||
// WRITEME
|
mTimer.expires_from_now(boost::posix_time::seconds(LEDGER_ACQUIRE_TIMEOUT));
|
||||||
|
mTimer.async_wait(boost::bind(&LedgerAcquire::timerEntry,
|
||||||
|
boost::weak_ptr<LedgerAcquire>(shared_from_this()), boost::asio::placeholders::error));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedgerAcquire::timerEntry(boost::weak_ptr<LedgerAcquire> wptr)
|
void LedgerAcquire::timerEntry(boost::weak_ptr<LedgerAcquire> wptr, const boost::system::error_code& result)
|
||||||
{
|
{
|
||||||
|
if (result == boost::asio::error::operation_aborted) return;
|
||||||
LedgerAcquire::pointer ptr = wptr.lock();
|
LedgerAcquire::pointer ptr = wptr.lock();
|
||||||
if (ptr) ptr->trigger(true);
|
if (!!ptr) ptr->trigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedgerAcquire::addOnComplete(boost::function<void (LedgerAcquire::pointer)> trigger)
|
void LedgerAcquire::addOnComplete(boost::function<void (LedgerAcquire::pointer)> trigger)
|
||||||
@@ -47,7 +55,7 @@ void LedgerAcquire::addOnComplete(boost::function<void (LedgerAcquire::pointer)>
|
|||||||
mLock.unlock();
|
mLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedgerAcquire::trigger(bool timer)
|
void LedgerAcquire::trigger()
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
std::cerr << "Trigger acquiring ledger " << mHash.GetHex() << std::endl;
|
std::cerr << "Trigger acquiring ledger " << mHash.GetHex() << std::endl;
|
||||||
@@ -151,8 +159,8 @@ void LedgerAcquire::trigger(bool timer)
|
|||||||
|
|
||||||
if (mComplete || mFailed)
|
if (mComplete || mFailed)
|
||||||
done();
|
done();
|
||||||
else if (timer)
|
else
|
||||||
setTimer();
|
resetTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LedgerAcquire::sendRequest(boost::shared_ptr<newcoin::TMGetLedger> tmGL)
|
void LedgerAcquire::sendRequest(boost::shared_ptr<newcoin::TMGetLedger> tmGL)
|
||||||
@@ -286,12 +294,10 @@ LedgerAcquire::pointer LedgerAcquireMaster::findCreate(const uint256& hash)
|
|||||||
{
|
{
|
||||||
boost::mutex::scoped_lock sl(mLock);
|
boost::mutex::scoped_lock sl(mLock);
|
||||||
LedgerAcquire::pointer& ptr = mLedgers[hash];
|
LedgerAcquire::pointer& ptr = mLedgers[hash];
|
||||||
if (ptr)
|
if (ptr) return ptr;
|
||||||
{
|
|
||||||
ptr->trigger();
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
ptr = boost::make_shared<LedgerAcquire>(hash);
|
ptr = boost::make_shared<LedgerAcquire>(hash);
|
||||||
|
assert(mLedgers[hash] == ptr);
|
||||||
|
ptr->resetTimer(); // Cannot call in constructor
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,10 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
#include "boost/enable_shared_from_this.hpp"
|
#include <boost/enable_shared_from_this.hpp>
|
||||||
#include "boost/function.hpp"
|
#include <boost/function.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
|
|
||||||
#include "Ledger.h"
|
#include "Ledger.h"
|
||||||
#include "Peer.h"
|
#include "Peer.h"
|
||||||
@@ -21,16 +23,18 @@ protected:
|
|||||||
Ledger::pointer mLedger;
|
Ledger::pointer mLedger;
|
||||||
uint256 mHash;
|
uint256 mHash;
|
||||||
bool mComplete, mFailed, mHaveBase, mHaveState, mHaveTransactions;
|
bool mComplete, mFailed, mHaveBase, mHaveState, mHaveTransactions;
|
||||||
|
|
||||||
|
boost::asio::deadline_timer mTimer;
|
||||||
|
|
||||||
std::vector< boost::function<void (LedgerAcquire::pointer)> > mOnComplete;
|
std::vector< boost::function<void (LedgerAcquire::pointer)> > mOnComplete;
|
||||||
|
|
||||||
std::list<boost::weak_ptr<Peer> > mPeers; // peers known to have this ledger
|
std::list<boost::weak_ptr<Peer> > mPeers; // peers known to have this ledger
|
||||||
|
|
||||||
void done();
|
void done();
|
||||||
void trigger(bool timer);
|
void trigger();
|
||||||
|
|
||||||
static void timerEntry(boost::weak_ptr<LedgerAcquire>);
|
static void timerEntry(boost::weak_ptr<LedgerAcquire>, const boost::system::error_code&);
|
||||||
void sendRequest(boost::shared_ptr<newcoin::TMGetLedger> message);
|
void sendRequest(boost::shared_ptr<newcoin::TMGetLedger> message);
|
||||||
void setTimer();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LedgerAcquire(const uint256& hash);
|
LedgerAcquire(const uint256& hash);
|
||||||
@@ -42,7 +46,6 @@ public:
|
|||||||
bool isAcctStComplete() const { return mHaveState; }
|
bool isAcctStComplete() const { return mHaveState; }
|
||||||
bool isTransComplete() const { return mHaveTransactions; }
|
bool isTransComplete() const { return mHaveTransactions; }
|
||||||
Ledger::pointer getLedger() { return mLedger; }
|
Ledger::pointer getLedger() { return mLedger; }
|
||||||
void trigger() { trigger(false); }
|
|
||||||
|
|
||||||
void addOnComplete(boost::function<void (LedgerAcquire::pointer)>);
|
void addOnComplete(boost::function<void (LedgerAcquire::pointer)>);
|
||||||
|
|
||||||
@@ -51,6 +54,7 @@ public:
|
|||||||
bool takeBase(const std::string& data);
|
bool takeBase(const std::string& data);
|
||||||
bool takeTxNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data);
|
bool takeTxNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data);
|
||||||
bool takeAsNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data);
|
bool takeAsNode(const std::list<SHAMapNode>& IDs, const std::list<std::vector<unsigned char> >& data);
|
||||||
|
void resetTimer();
|
||||||
};
|
};
|
||||||
|
|
||||||
class LedgerAcquireMaster
|
class LedgerAcquireMaster
|
||||||
|
|||||||
Reference in New Issue
Block a user