Add ledger interval. Various updates.

This commit is contained in:
JoelKatz
2012-05-05 13:50:23 -07:00
parent b3c16fe0f6
commit 038144d5aa
2 changed files with 42 additions and 41 deletions

View File

@@ -16,7 +16,8 @@
#include "BinaryFormats.h"
Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mTotCoins(startAmount),
mTimeStamp(0), mLedgerSeq(0), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false)
mTimeStamp(0), mLedgerSeq(0), mLedgerInterval(60), mClosed(false), mValidHash(false),
mAccepted(false), mImmutable(false)
{
mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap = boost::make_shared<SHAMap>();
@@ -35,19 +36,23 @@ Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mTotCoins(s
Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash,
uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq)
: mParentHash(parentHash), mTransHash(transHash), mAccountHash(accountHash),
mTotCoins(totCoins), mTimeStamp(timeStamp), mLedgerSeq(ledgerSeq),
mTotCoins(totCoins), mTimeStamp(timeStamp), mLedgerSeq(ledgerSeq), mLedgerInterval(60),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false)
{
updateHash();
}
Ledger::Ledger(Ledger &prevLedger, uint64 ts) : mTimeStamp(ts),
Ledger::Ledger(Ledger::pointer prevLedger) : mParentHash(prevLedger->getHash()), mTotCoins(prevLedger->mTotCoins),
mLedgerSeq(prevLedger->mLedgerSeq + 1), mLedgerInterval(prevLedger->mLedgerInterval),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false),
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap)
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger->mAccountStateMap)
{
mParentHash = prevLedger.getHash();
mLedgerSeq = prevLedger.mLedgerSeq+1;
prevLedger->setClosed();
prevLedger->updateHash();
mAccountStateMap->setSeq(mLedgerSeq);
if (prevLedger->mTimeStamp == 0)
mTimeStamp = (theApp->getOPs().getNetworkTime() % mLedgerInterval) + mLedgerInterval;
else mTimeStamp = prevLedger->mTimeStamp + prevLedger->mLedgerInterval;
}
Ledger::Ledger(const std::vector<unsigned char>& rawLedger) : mTotCoins(0), mTimeStamp(0),
@@ -61,6 +66,7 @@ Ledger::Ledger(const std::vector<unsigned char>& rawLedger) : mTotCoins(0), mTim
if (!s.get256(mTransHash, BLgPTxT)) return;
if (!s.get256(mAccountHash, BLgPAcT)) return;
if (!s.get64(mTimeStamp, BLgPClTs)) return;
if (!s.get16(mLedgerInterval, BLgPNlIn)) return;
updateHash();
if(mValidHash)
{
@@ -80,6 +86,7 @@ Ledger::Ledger(const std::string& rawLedger) : mTotCoins(0), mTimeStamp(0),
if (!s.get256(mTransHash, BLgPTxT)) return;
if (!s.get256(mAccountHash, BLgPAcT)) return;
if (!s.get64(mTimeStamp, BLgPClTs)) return;
if (!s.get16(mLedgerInterval, BLgPNlIn)) return;
updateHash();
if(mValidHash)
{
@@ -112,6 +119,7 @@ void Ledger::addRaw(Serializer &s)
s.add256(mTransHash);
s.add256(mAccountHash);
s.add64(mTimeStamp);
s.add16(mLedgerInterval);
}
AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
@@ -175,14 +183,6 @@ Transaction::pointer Ledger::getTransaction(const uint256& transID) const
return txn;
}
Ledger::pointer Ledger::closeLedger(uint64 timeStamp)
{ // close this ledger, return a pointer to the next ledger
// CAUTION: New ledger needs its SHAMap's connected to storage
updateHash();
setClosed();
return Ledger::pointer(new Ledger(*this, timeStamp)); // can't use make_shared, constructor is protected
}
bool Ledger::unitTest()
{
#if 0
@@ -342,7 +342,7 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
if (newPrevious->getLedgerSeq() != oldPrevious->getLedgerSeq()) return Ledger::pointer();
// 2) Begin building a new ledger with the specified ledger as previous.
Ledger* newLedger=new Ledger(*newPrevious, mTimeStamp);
Ledger::pointer newLedger = boost::make_shared<Ledger>(newPrevious);
// 3) For any transactions in our previous ledger but not in the new previous ledger, add them to the set
SHAMap::SHAMapDiff mapDifferences;
@@ -361,9 +361,9 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
while (it != TxnDiff.end())
{
Transaction::pointer& tx = it->second.second;
if (!tx || newLedger->addTransaction(tx))
if (!tx || newLedger->addTransaction(tx)) // FIXME: addTransaction doesn't do checks
{
count++;
++count;
TxnDiff.erase(it++);
}
else ++it;
@@ -389,9 +389,9 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
std::map<uint256, Transaction::pointer>::iterator it = txnMap.begin();
while (it != txnMap.end())
{
if(newLedger->addTransaction(it->second))
if (newLedger->addTransaction(it->second)) // FIXME: addTransaction doesn't do checks
{
count++;
++count;
txnMap.erase(it++);
}
else ++it;

View File

@@ -58,6 +58,7 @@ private:
uint256 mHash, mParentHash, mTransHash, mAccountHash;
uint64 mTotCoins, mTimeStamp;
uint32 mLedgerSeq;
uint16 mLedgerInterval;
bool mClosed, mValidHash, mAccepted, mImmutable;
SHAMap::pointer mTransactionMap, mAccountStateMap;
@@ -68,8 +69,6 @@ private:
Ledger& operator=(const Ledger&); // no implementation
protected:
Ledger(Ledger& previous, uint64 timestamp); // ledger after this one
void updateHash();
bool addTransaction(Transaction::pointer);
bool addTransaction(const uint256& id, const Serializer& txn, uint64_t fee);
@@ -85,7 +84,9 @@ public:
uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers
Ledger(const std::vector<unsigned char>& rawLedger);
Ledger(const std::string& rawLedger);
Ledger(Ledger::pointer previous); // ledger after this one
void updateHash();
void setClosed() { mClosed = true; }
void setAccepted() { mAccepted = true; }
bool isClosed() { return mClosed; }
@@ -101,6 +102,7 @@ public:
uint64 getTotalCoins() const { return mTotCoins; }
uint64 getTimeStamp() const { return mTimeStamp; }
uint32 getLedgerSeq() const { return mLedgerSeq; }
uint16 getInterval() const { return mLedgerInterval; }
// low level functions
SHAMap::pointer peekTransactionMap() { return mTransactionMap; }
@@ -144,7 +146,6 @@ public:
const uint160& currency)
{ return getRippleIndex(account.getAccountID(), extendTo.getAccountID(), currency); }
Ledger::pointer closeLedger(uint64 timestamp);
bool isCompatible(boost::shared_ptr<Ledger> other);
bool signLedger(std::vector<unsigned char> &signature, const LocalHanko &hanko);