mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Add ledger interval. Various updates.
This commit is contained in:
@@ -16,7 +16,8 @@
|
|||||||
#include "BinaryFormats.h"
|
#include "BinaryFormats.h"
|
||||||
|
|
||||||
Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mTotCoins(startAmount),
|
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>();
|
mTransactionMap = boost::make_shared<SHAMap>();
|
||||||
mAccountStateMap = 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,
|
Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash,
|
||||||
uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq)
|
uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq)
|
||||||
: mParentHash(parentHash), mTransHash(transHash), mAccountHash(accountHash),
|
: 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)
|
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false)
|
||||||
{
|
{
|
||||||
updateHash();
|
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),
|
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false),
|
||||||
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap)
|
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger->mAccountStateMap)
|
||||||
{
|
{
|
||||||
mParentHash = prevLedger.getHash();
|
prevLedger->setClosed();
|
||||||
mLedgerSeq = prevLedger.mLedgerSeq+1;
|
prevLedger->updateHash();
|
||||||
mAccountStateMap->setSeq(mLedgerSeq);
|
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),
|
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(mTransHash, BLgPTxT)) return;
|
||||||
if (!s.get256(mAccountHash, BLgPAcT)) return;
|
if (!s.get256(mAccountHash, BLgPAcT)) return;
|
||||||
if (!s.get64(mTimeStamp, BLgPClTs)) return;
|
if (!s.get64(mTimeStamp, BLgPClTs)) return;
|
||||||
|
if (!s.get16(mLedgerInterval, BLgPNlIn)) return;
|
||||||
updateHash();
|
updateHash();
|
||||||
if(mValidHash)
|
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(mTransHash, BLgPTxT)) return;
|
||||||
if (!s.get256(mAccountHash, BLgPAcT)) return;
|
if (!s.get256(mAccountHash, BLgPAcT)) return;
|
||||||
if (!s.get64(mTimeStamp, BLgPClTs)) return;
|
if (!s.get64(mTimeStamp, BLgPClTs)) return;
|
||||||
|
if (!s.get16(mLedgerInterval, BLgPNlIn)) return;
|
||||||
updateHash();
|
updateHash();
|
||||||
if(mValidHash)
|
if(mValidHash)
|
||||||
{
|
{
|
||||||
@@ -112,6 +119,7 @@ void Ledger::addRaw(Serializer &s)
|
|||||||
s.add256(mTransHash);
|
s.add256(mTransHash);
|
||||||
s.add256(mAccountHash);
|
s.add256(mAccountHash);
|
||||||
s.add64(mTimeStamp);
|
s.add64(mTimeStamp);
|
||||||
|
s.add16(mLedgerInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
|
AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
|
||||||
@@ -175,14 +183,6 @@ Transaction::pointer Ledger::getTransaction(const uint256& transID) const
|
|||||||
return txn;
|
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()
|
bool Ledger::unitTest()
|
||||||
{
|
{
|
||||||
#if 0
|
#if 0
|
||||||
@@ -339,42 +339,42 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
|
|||||||
int count;
|
int count;
|
||||||
|
|
||||||
// 1) Validate sequences and make sure the specified ledger is a valid prior ledger
|
// 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.
|
// 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
|
// 3) For any transactions in our previous ledger but not in the new previous ledger, add them to the set
|
||||||
SHAMap::SHAMapDiff mapDifferences;
|
SHAMap::SHAMapDiff mapDifferences;
|
||||||
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> > TxnDiff;
|
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> > TxnDiff;
|
||||||
if(!newPrevious->mTransactionMap->compare(oldPrevious->mTransactionMap, mapDifferences, limit))
|
if (!newPrevious->mTransactionMap->compare(oldPrevious->mTransactionMap, mapDifferences, limit))
|
||||||
return Ledger::pointer();
|
return Ledger::pointer();
|
||||||
if(!Transaction::convertToTransactions(oldPrevious->getLedgerSeq(), newPrevious->getLedgerSeq(),
|
if (!Transaction::convertToTransactions(oldPrevious->getLedgerSeq(), newPrevious->getLedgerSeq(),
|
||||||
false, true, mapDifferences, TxnDiff))
|
false, true, mapDifferences, TxnDiff))
|
||||||
return Ledger::pointer(); // new previous ledger contains invalid transactions
|
return Ledger::pointer(); // new previous ledger contains invalid transactions
|
||||||
|
|
||||||
// 4) Try to add those transactions to the new ledger.
|
// 4) Try to add those transactions to the new ledger.
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
count=0;
|
count = 0;
|
||||||
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it = TxnDiff.begin();
|
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it = TxnDiff.begin();
|
||||||
while (it != TxnDiff.end())
|
while (it != TxnDiff.end())
|
||||||
{
|
{
|
||||||
Transaction::pointer& tx = it->second.second;
|
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++);
|
TxnDiff.erase(it++);
|
||||||
}
|
}
|
||||||
else ++it;
|
else ++it;
|
||||||
}
|
}
|
||||||
} while (count!=0);
|
} while (count != 0);
|
||||||
|
|
||||||
// WRITEME: Handle rejected transactions left in TxnDiff
|
// WRITEME: Handle rejected transactions left in TxnDiff
|
||||||
|
|
||||||
// 5) Try to add transactions from this ledger to the new ledger.
|
// 5) Try to add transactions from this ledger to the new ledger.
|
||||||
std::map<uint256, Transaction::pointer> txnMap;
|
std::map<uint256, Transaction::pointer> txnMap;
|
||||||
for(SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem();
|
for (SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem();
|
||||||
!!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag()))
|
!!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag()))
|
||||||
{
|
{
|
||||||
uint256 txnID = mit->getTag();
|
uint256 txnID = mit->getTag();
|
||||||
@@ -385,18 +385,18 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
count=0;
|
count = 0;
|
||||||
std::map<uint256, Transaction::pointer>::iterator it = txnMap.begin();
|
std::map<uint256, Transaction::pointer>::iterator it = txnMap.begin();
|
||||||
while (it != txnMap.end())
|
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++);
|
txnMap.erase(it++);
|
||||||
}
|
}
|
||||||
else ++it;
|
else ++it;
|
||||||
}
|
}
|
||||||
} while(count!=0);
|
} while(count != 0);
|
||||||
|
|
||||||
|
|
||||||
// WRITEME: Handle rejected transactions left in txnMap
|
// WRITEME: Handle rejected transactions left in txnMap
|
||||||
@@ -406,7 +406,7 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
|
|||||||
|
|
||||||
void Ledger::setAcquiring(void)
|
void Ledger::setAcquiring(void)
|
||||||
{
|
{
|
||||||
if(!mTransactionMap || !mAccountStateMap) throw SHAMapException(InvalidMap);
|
if (!mTransactionMap || !mAccountStateMap) throw SHAMapException(InvalidMap);
|
||||||
mTransactionMap->setSynching();
|
mTransactionMap->setSynching();
|
||||||
mAccountStateMap->setSynching();
|
mAccountStateMap->setSynching();
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/Ledger.h
27
src/Ledger.h
@@ -58,6 +58,7 @@ private:
|
|||||||
uint256 mHash, mParentHash, mTransHash, mAccountHash;
|
uint256 mHash, mParentHash, mTransHash, mAccountHash;
|
||||||
uint64 mTotCoins, mTimeStamp;
|
uint64 mTotCoins, mTimeStamp;
|
||||||
uint32 mLedgerSeq;
|
uint32 mLedgerSeq;
|
||||||
|
uint16 mLedgerInterval;
|
||||||
bool mClosed, mValidHash, mAccepted, mImmutable;
|
bool mClosed, mValidHash, mAccepted, mImmutable;
|
||||||
|
|
||||||
SHAMap::pointer mTransactionMap, mAccountStateMap;
|
SHAMap::pointer mTransactionMap, mAccountStateMap;
|
||||||
@@ -68,8 +69,6 @@ private:
|
|||||||
Ledger& operator=(const Ledger&); // no implementation
|
Ledger& operator=(const Ledger&); // no implementation
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Ledger(Ledger& previous, uint64 timestamp); // ledger after this one
|
|
||||||
void updateHash();
|
|
||||||
|
|
||||||
bool addTransaction(Transaction::pointer);
|
bool addTransaction(Transaction::pointer);
|
||||||
bool addTransaction(const uint256& id, const Serializer& txn, uint64_t fee);
|
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
|
uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers
|
||||||
Ledger(const std::vector<unsigned char>& rawLedger);
|
Ledger(const std::vector<unsigned char>& rawLedger);
|
||||||
Ledger(const std::string& rawLedger);
|
Ledger(const std::string& rawLedger);
|
||||||
|
Ledger(Ledger::pointer previous); // ledger after this one
|
||||||
|
|
||||||
void setClosed() { mClosed=true; }
|
void updateHash();
|
||||||
void setAccepted() { mAccepted=true; }
|
void setClosed() { mClosed = true; }
|
||||||
bool isClosed() { return mClosed; }
|
void setAccepted() { mAccepted = true; }
|
||||||
bool isAccepted() { return mAccepted; }
|
bool isClosed() { return mClosed; }
|
||||||
|
bool isAccepted() { return mAccepted; }
|
||||||
|
|
||||||
// ledger signature operations
|
// ledger signature operations
|
||||||
void addRaw(Serializer &s);
|
void addRaw(Serializer &s);
|
||||||
|
|
||||||
uint256 getHash();
|
uint256 getHash();
|
||||||
const uint256& getParentHash() const { return mParentHash; }
|
const uint256& getParentHash() const { return mParentHash; }
|
||||||
const uint256& getTransHash() const { return mTransHash; }
|
const uint256& getTransHash() const { return mTransHash; }
|
||||||
const uint256& getAccountHash() const { return mAccountHash; }
|
const uint256& getAccountHash() const { return mAccountHash; }
|
||||||
uint64 getTotalCoins() const { return mTotCoins; }
|
uint64 getTotalCoins() const { return mTotCoins; }
|
||||||
uint64 getTimeStamp() const { return mTimeStamp; }
|
uint64 getTimeStamp() const { return mTimeStamp; }
|
||||||
uint32 getLedgerSeq() const { return mLedgerSeq; }
|
uint32 getLedgerSeq() const { return mLedgerSeq; }
|
||||||
|
uint16 getInterval() const { return mLedgerInterval; }
|
||||||
|
|
||||||
// low level functions
|
// low level functions
|
||||||
SHAMap::pointer peekTransactionMap() { return mTransactionMap; }
|
SHAMap::pointer peekTransactionMap() { return mTransactionMap; }
|
||||||
@@ -144,7 +146,6 @@ public:
|
|||||||
const uint160& currency)
|
const uint160& currency)
|
||||||
{ return getRippleIndex(account.getAccountID(), extendTo.getAccountID(), currency); }
|
{ return getRippleIndex(account.getAccountID(), extendTo.getAccountID(), currency); }
|
||||||
|
|
||||||
Ledger::pointer closeLedger(uint64 timestamp);
|
|
||||||
bool isCompatible(boost::shared_ptr<Ledger> other);
|
bool isCompatible(boost::shared_ptr<Ledger> other);
|
||||||
bool signLedger(std::vector<unsigned char> &signature, const LocalHanko &hanko);
|
bool signLedger(std::vector<unsigned char> &signature, const LocalHanko &hanko);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user