Begin merging this code.

This commit is contained in:
JoelKatz
2011-12-13 12:17:51 -08:00
parent 5db7ba9d62
commit 263fe6f515
4 changed files with 55 additions and 109 deletions

View File

@@ -2,19 +2,8 @@
#include "Config.h"
#include "Application.h"
#include <string>
/*
Soon we should support saving the ledger in a real DB
For now save them all in
For all the various ledgers we can save just the delta from the combined ledger for that index.
*/
void LedgerHistory::load()
{
}
#if 0
bool LedgerHistory::loadLedger(const uint256& hash)
{
Ledger::pointer ledger=Ledger::pointer(new Ledger());
@@ -68,3 +57,4 @@ Ledger::pointer LedgerHistory::getLedger(const uint256& hash)
if(loadLedger(hash)) return(mAllLedgers[hash]);
return(Ledger::pointer());
}
#endif

View File

@@ -3,31 +3,22 @@
#include "Ledger.h"
/*
This is a collection of all the ledgers you know about.
One thread of them is the thread you validate.
But you keep others that you have heard about.
Why do you need to keep them all?
In case people ask.
To make sure that there isn't a conflict between the validated ledgers
*/
class LedgerHistory
{
std::map<uint32, Ledger::pointer> mAcceptedLedgers;
std::map<uint256, Ledger::pointer> mAllLedgers;
std::map<uint32, Ledger::pointer> mLedgersByIndex;
std::map<uint256, Ledger::pointer> mLedgersByHash;
bool loadAcceptedLedger(uint32 index);
bool loadClosedLedger(uint32 index);
bool loadLedger(const uint256& hash);
public:
void load();
LedgerHistory() { ; }
void addLedger(Ledger::pointer ledger);
void addAcceptedLedger(Ledger::pointer ledger);
Ledger::pointer getAcceptedLedger(uint32 index);
Ledger::pointer getLedger(const uint256& hash);
Ledger::pointer getLedgerBySeq(uint32 index);
Ledger::pointer getLedgerByHash(const uint256& hash);
};
#endif

View File

@@ -5,44 +5,31 @@
#include "Conversion.h"
#include <boost/foreach.hpp>
using namespace std;
LedgerMaster::LedgerMaster()
LedgerMaster::LedgerMaster() : mIsSynced(false)
{
mFinalizingLedger=Ledger::pointer();
mCurrentLedger=Ledger::pointer(new Ledger(TimingService::getCurrentLedgerIndex()));
}
void LedgerMaster::load()
{
mLedgerHistory.load();
}
void LedgerMaster::save()
{
}
uint32 LedgerMaster::getCurrentLedgerIndex()
{
return(mCurrentLedger->getIndex());
return mCurrentLedger->getLedgerSeq();
}
int64 LedgerMaster::getAmountHeld(uint160& addr)
uint64 LedgerMaster::getBalance(const uint160& addr)
{
return(mCurrentLedger->getAmountHeld(addr));
return mCurrentLedger->getBalance(addr);
}
int64 LedgerMaster::getAmountHeld(std::string& addr)
uint64 LedgerMaster::getBalance(std::string& addr)
{
return(mCurrentLedger->getAmountHeld(humanTo160(addr)));
return mCurrentLedger->getBalance(humanTo160(addr));
}
#if 0
bool LedgerMaster::isTransactionOnFutureList(TransactionPtr needle)
bool LedgerMaster::isTransactionOnFutureList(Transaction::pointer needle)
{
BOOST_FOREACH(TransactionPtr straw,mFutureTransactions)
BOOST_FOREACH(Transaction::pointer straw,mFutureTransactions)
{
if(Transaction::isEqual(straw,needle))
{
@@ -57,7 +44,7 @@ bool LedgerMaster::isTransactionOnFutureList(TransactionPtr needle)
// 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(TransactionPtr trans)
bool LedgerMaster::isValidTransaction(Transaction::pointer trans)
{
if(trans->from()==trans->dest()) return(false);
if(trans->amount()==0) return(false);
@@ -71,7 +58,7 @@ bool LedgerMaster::isValidTransaction(TransactionPtr trans)
// returns true if we should relay it
bool LedgerMaster::addTransaction(TransactionPtr trans)
bool LedgerMaster::addTransaction(Transaction::pointer trans)
{
if(mFinalizingLedger && (trans->ledgerindex()==mFinalizingLedger->getIndex()))
{
@@ -127,30 +114,6 @@ bool LedgerMaster::addTransaction(TransactionPtr trans)
void LedgerMaster::addFullLedger(newcoin::FullLedger& ledger)
{
// check if we already have this ledger
uint256 inHash=protobufTo256(ledger.hash());
Ledger::pointer existingLedger=mLedgerHistory.getLedger( inHash );
if(existingLedger) return;
// check that the hash is correct
Ledger::pointer newLedger=Ledger::pointer(new Ledger(ledger));
if(newLedger->getHash()==inHash)
{
mLedgerHistory.addLedger(newLedger);
// add all these in case we are missing some
BOOST_FOREACH(TransactionPtr trans, newLedger->getTransactions())
{
addTransaction(trans);
}
}else cout << "We were sent a bad ledger hash" << endl;
}
void LedgerMaster::startFinalization()
{
mFinalizingLedger=mCurrentLedger;
@@ -299,3 +262,4 @@ theApp->getConnectionPool().relayMessage(NULL,msg);
}
*/
#endif

View File

@@ -7,56 +7,57 @@
#include "types.h"
#include "Transaction.h"
/*
Handles:
collecting the current ledger
finalizing the ledger
validating the past ledger
keeping the ledger history
// Tracks the current ledger and any ledgers in the process of closing
// Tracks ledger history
// Tracks held transactions
There is one ledger we are working on.
*/
class LedgerMaster
{
bool mIsSynced;
Ledger::pointer mCurrentLedger;
Ledger::pointer mFinalizingLedger;
LedgerHistory mLedgerHistory;
std::list<TransactionPtr> mFutureTransactions;
std::list< std::pair<Peer::pointer,newcoin::ProposeLedger> > mFutureProposals;
//bool mAfterProposed;
std::map<uint256, Transaction::pointer> mHeldTransactionsByID;
std::map<uint32, Transaction::pointer> mHeldTransactionsByLedger;
void addFutureProposal(Peer::pointer peer,newcoin::ProposeLedger& packet);
void applyFutureProposals(uint32 ledgerIndex);
void applyFutureTransactions(uint32 ledgerIndex);
bool isValidTransaction(TransactionPtr trans);
bool isTransactionOnFutureList(TransactionPtr trans);
bool isValidTransaction(Transaction::pointer trans);
bool isTransactionOnFutureList(Transaction::pointer trans);
public:
LedgerMaster();
void load();
void save();
uint32 getCurrentLedgerIndex();
bool IsSynced(void) { return mIsSynced; }
void SetSynced(void) { mIsSynced=true; }
Ledger::pointer getAcceptedLedger(uint32 index){ return(mLedgerHistory.getAcceptedLedger(index)); }
Ledger::pointer getLedger(const uint256& hash){ return(mLedgerHistory.getLedger(hash)); }
Ledger::pointer getCurrentLedger() { return mCurrentLedger; }
Ledger::pointer getClosingLedger() { return mFinalizingLedger; }
int64 getAmountHeld(std::string& addr);
int64 getAmountHeld(uint160& addr);
Ledger::Account* getAccount(uint160& addr){ return(mCurrentLedger->getAccount(addr)); }
Ledger::pointer getLedgerBySeq(uint32 index)
{
if(mCurrentLedger && (mCurrentLedger->getLedgerSeq()==index)) return mCurrentLedger;
if(mFinalizingLedger && (mFinalizingLedger->getLedgerSeq()==index)) return mFinalizingLedger;
return mLedgerHistory.getLedgerBySeq(index);
}
bool addTransaction(TransactionPtr trans);
void addFullLedger(newcoin::FullLedger& ledger);
Ledger::pointer getLedgerByHash(const uint256& hash)
{
if(mCurrentLedger && (mCurrentLedger->getHash()==hash)) return mCurrentLedger;
if(mFinalizingLedger && (mFinalizingLedger->getHash()==hash)) return mFinalizingLedger;
return mLedgerHistory.getLedgerByHash(hash);
}
void startFinalization();
void sendProposal();
void endFinalization();
void checkLedgerProposal(Peer::pointer peer,newcoin::ProposeLedger& packet);
void checkConsensus(uint32 ledgerIndex);
uint64 getBalance(std::string& addr);
uint64 getBalance(const uint160& addr);
AccountState::pointer getAccountState(const uint160& addr)
{ return mCurrentLedger->getAccountState(addr); }
bool addTransaction(Transaction::pointer trans);
};
#endif
#endif