From 038144d5aa074e9f2d108c31ee19fbe4c23d2953 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sat, 5 May 2012 13:50:23 -0700 Subject: [PATCH] Add ledger interval. Various updates. --- src/Ledger.cpp | 56 +++++++++++++++++++++++++------------------------- src/Ledger.h | 27 ++++++++++++------------ 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/Ledger.cpp b/src/Ledger.cpp index 99df33959..c3c1d8804 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -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(); mAccountStateMap = boost::make_shared(); @@ -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& rawLedger) : mTotCoins(0), mTimeStamp(0), @@ -61,6 +66,7 @@ Ledger::Ledger(const std::vector& 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 @@ -339,42 +339,42 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger int count; // 1) Validate sequences and make sure the specified ledger is a valid prior ledger - if(newPrevious->getLedgerSeq()!=oldPrevious->getLedgerSeq()) return Ledger::pointer(); + 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(newPrevious); // 3) For any transactions in our previous ledger but not in the new previous ledger, add them to the set SHAMap::SHAMapDiff mapDifferences; std::map > TxnDiff; - if(!newPrevious->mTransactionMap->compare(oldPrevious->mTransactionMap, mapDifferences, limit)) + if (!newPrevious->mTransactionMap->compare(oldPrevious->mTransactionMap, mapDifferences, limit)) return Ledger::pointer(); - if(!Transaction::convertToTransactions(oldPrevious->getLedgerSeq(), newPrevious->getLedgerSeq(), + if (!Transaction::convertToTransactions(oldPrevious->getLedgerSeq(), newPrevious->getLedgerSeq(), false, true, mapDifferences, TxnDiff)) return Ledger::pointer(); // new previous ledger contains invalid transactions // 4) Try to add those transactions to the new ledger. do { - count=0; + count = 0; std::map >::iterator it = TxnDiff.begin(); 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; } - } while (count!=0); + } while (count != 0); // WRITEME: Handle rejected transactions left in TxnDiff // 5) Try to add transactions from this ledger to the new ledger. std::map txnMap; - for(SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem(); + for (SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem(); !!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag())) { uint256 txnID = mit->getTag(); @@ -385,18 +385,18 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger do { - count=0; + count = 0; std::map::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; } - } while(count!=0); + } while(count != 0); // WRITEME: Handle rejected transactions left in txnMap @@ -406,7 +406,7 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger void Ledger::setAcquiring(void) { - if(!mTransactionMap || !mAccountStateMap) throw SHAMapException(InvalidMap); + if (!mTransactionMap || !mAccountStateMap) throw SHAMapException(InvalidMap); mTransactionMap->setSynching(); mAccountStateMap->setSynching(); } diff --git a/src/Ledger.h b/src/Ledger.h index cc1379455..16730c4ad 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -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,22 +84,25 @@ public: uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers Ledger(const std::vector& rawLedger); Ledger(const std::string& rawLedger); + Ledger(Ledger::pointer previous); // ledger after this one - void setClosed() { mClosed=true; } - void setAccepted() { mAccepted=true; } - bool isClosed() { return mClosed; } - bool isAccepted() { return mAccepted; } + void updateHash(); + void setClosed() { mClosed = true; } + void setAccepted() { mAccepted = true; } + bool isClosed() { return mClosed; } + bool isAccepted() { return mAccepted; } // ledger signature operations void addRaw(Serializer &s); uint256 getHash(); - const uint256& getParentHash() const { return mParentHash; } - const uint256& getTransHash() const { return mTransHash; } - const uint256& getAccountHash() const { return mAccountHash; } - uint64 getTotalCoins() const { return mTotCoins; } - uint64 getTimeStamp() const { return mTimeStamp; } - uint32 getLedgerSeq() const { return mLedgerSeq; } + const uint256& getParentHash() const { return mParentHash; } + const uint256& getTransHash() const { return mTransHash; } + const uint256& getAccountHash() const { return mAccountHash; } + 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 other); bool signLedger(std::vector &signature, const LocalHanko &hanko);