diff --git a/src/BinaryFormats.h b/src/BinaryFormats.h index 0c06b96fb..be1de2ba2 100644 --- a/src/BinaryFormats.h +++ b/src/BinaryFormats.h @@ -14,7 +14,7 @@ const int BTxPSig=73, BTxLSig=72; // signature // ledger (note: fields after the timestamp are not part of the hash) const int BLgSize=192; const int BLgPIndex=0, BLgLIndex=4; // ledger index -const int BLgPFeeHeld=4, BLgLFeeHeld=8; // transaction fees held +const int BLgPTotCoins=4, BLgLTotCoins=8; // total native coins const int BLgPPrevLg=12, BLgLPrevLg=32; // previous ledger hash const int BLgPTxT=44, BLgLTxT=32; // transaction tree hash const int BLgPAcT=76, BLgLPAct=32; // account state hash diff --git a/src/DBInit.cpp b/src/DBInit.cpp index 27304efd7..76d6b96e1 100644 --- a/src/DBInit.cpp +++ b/src/DBInit.cpp @@ -28,7 +28,7 @@ const char *LedgerDBInit[] = { LedgerHash CHARACTER(64) PRIMARY KEY, \ LedgerSeq BIGINT UNSIGNED, \ PrevHash CHARACTER(64), \ - FeeHeld BIGINT UNSIGNED, \ + TotalCoins BIGINT UNSIGNED, \ ClosingTime BIGINT UNSINGED, \ AccountSetHash CHARACTER(64), \ TransSetHash CHARACTER(64) \ diff --git a/src/Ledger.cpp b/src/Ledger.cpp index e6c174d5f..99df33959 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -15,8 +15,8 @@ #include "Wallet.h" #include "BinaryFormats.h" -Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mFeeHeld(0), mTimeStamp(0), mLedgerSeq(0), - mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) +Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mTotCoins(startAmount), + mTimeStamp(0), mLedgerSeq(0), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) { mTransactionMap = boost::make_shared(); mAccountStateMap = boost::make_shared(); @@ -33,9 +33,9 @@ Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mFeeHeld(0) } Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, - uint64 feeHeld, uint64 timeStamp, uint32 ledgerSeq) + uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq) : mParentHash(parentHash), mTransHash(transHash), mAccountHash(accountHash), - mFeeHeld(feeHeld), mTimeStamp(timeStamp), mLedgerSeq(ledgerSeq), + mTotCoins(totCoins), mTimeStamp(timeStamp), mLedgerSeq(ledgerSeq), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) { updateHash(); @@ -50,13 +50,13 @@ Ledger::Ledger(Ledger &prevLedger, uint64 ts) : mTimeStamp(ts), mAccountStateMap->setSeq(mLedgerSeq); } -Ledger::Ledger(const std::vector& rawLedger) : mFeeHeld(0), mTimeStamp(0), +Ledger::Ledger(const std::vector& rawLedger) : mTotCoins(0), mTimeStamp(0), mLedgerSeq(0), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(true) { Serializer s(rawLedger); // 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts if (!s.get32(mLedgerSeq, BLgPIndex)) return; - if (!s.get64(mFeeHeld, BLgPFeeHeld)) return; + if (!s.get64(mTotCoins, BLgPTotCoins)) return; if (!s.get256(mParentHash, BLgPPrevLg)) return; if (!s.get256(mTransHash, BLgPTxT)) return; if (!s.get256(mAccountHash, BLgPAcT)) return; @@ -69,13 +69,13 @@ Ledger::Ledger(const std::vector& rawLedger) : mFeeHeld(0), mTime } } -Ledger::Ledger(const std::string& rawLedger) : mFeeHeld(0), mTimeStamp(0), +Ledger::Ledger(const std::string& rawLedger) : mTotCoins(0), mTimeStamp(0), mLedgerSeq(0), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(true) { Serializer s(rawLedger); // 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts if (!s.get32(mLedgerSeq, BLgPIndex)) return; - if (!s.get64(mFeeHeld, BLgPFeeHeld)) return; + if (!s.get64(mTotCoins, BLgPTotCoins)) return; if (!s.get256(mParentHash, BLgPPrevLg)) return; if (!s.get256(mTransHash, BLgPTxT)) return; if (!s.get256(mAccountHash, BLgPAcT)) return; @@ -107,7 +107,7 @@ void Ledger::updateHash() void Ledger::addRaw(Serializer &s) { s.add32(mLedgerSeq); - s.add64(mFeeHeld); + s.add64(mTotCoins); s.add256(mParentHash); s.add256(mTransHash); s.add256(mAccountHash); @@ -135,25 +135,23 @@ AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID) } bool Ledger::addTransaction(Transaction::pointer trans) -{ // low-level - just add to table +{ // low-level - just add to table, debit fee assert(!mAccepted); assert(!!trans->getID()); Serializer s; trans->getSTransaction()->getTransaction(s, false); SHAMapItem::pointer item = boost::make_shared(trans->getID(), s.peekData()); - return mTransactionMap->addGiveItem(item, true); + if (!mTransactionMap->addGiveItem(item, true)) return false; + mTotCoins -= trans->getFee(); + return true; } -bool Ledger::addTransaction(const uint256& txID, const Serializer& txn) +bool Ledger::addTransaction(const uint256& txID, const Serializer& txn, uint64_t fee) { // low-level - just add to table SHAMapItem::pointer item = boost::make_shared(txID, txn.peekData()); - return mTransactionMap->addGiveItem(item, true); -} - -bool Ledger::delTransaction(const uint256& transID) -{ - assert(!mAccepted); - return mTransactionMap->delItem(transID); + if (!mTransactionMap->addGiveItem(item, true)) return false; + mTotCoins -= fee; + return true; } bool Ledger::hasTransaction(const uint256& transID) const @@ -233,14 +231,14 @@ uint256 Ledger::getHash() void Ledger::saveAcceptedLedger(Ledger::pointer ledger) { std::string sql="INSERT INTO Ledgers " - "(LedgerHash,LedgerSeq,PrevHash,FeeHeld,ClosingTime,AccountSetHash,TransSetHash) VALUES ('"; + "(LedgerHash,LedgerSeq,TotalCoins,,ClosingTime,AccountSetHash,TransSetHash) VALUES ('"; sql.append(ledger->getHash().GetHex()); sql.append("','"); sql.append(boost::lexical_cast(ledger->mLedgerSeq)); sql.append("','"); sql.append(ledger->mParentHash.GetHex()); sql.append("','"); - sql.append(boost::lexical_cast(ledger->mFeeHeld)); + sql.append(boost::lexical_cast(ledger->mTotCoins)); sql.append("','"); sql.append(boost::lexical_cast(ledger->mTimeStamp)); sql.append("','"); @@ -263,15 +261,15 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger) Ledger::pointer Ledger::getSQL(const std::string& sql) { uint256 ledgerHash, prevHash, accountHash, transHash; - uint64 feeHeld, closingTime; + uint64 totCoins, closingTime; uint32 ledgerSeq; std::string hash; if(1) { ScopedLock sl(theApp->getLedgerDB()->getDBLock()); - Database *db=theApp->getLedgerDB()->getDB(); - if(!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow()) + Database *db = theApp->getLedgerDB()->getDB(); + if (!db->executeSQL(sql.c_str()) || !db->startIterRows() || !db->getNextRow()) return Ledger::pointer(); db->getStr("LedgerHash", hash); @@ -282,14 +280,14 @@ Ledger::pointer Ledger::getSQL(const std::string& sql) accountHash.SetHex(hash); db->getStr("TransSetHash", hash); transHash.SetHex(hash); - feeHeld=db->getBigInt("FeeHeld"); - closingTime=db->getBigInt("ClosingTime"); - ledgerSeq=db->getBigInt("LedgerSeq"); + totCoins = db->getBigInt("TotalCoins"); + closingTime = db->getBigInt("ClosingTime"); + ledgerSeq = db->getBigInt("LedgerSeq"); db->endIterRows(); } - Ledger::pointer ret=boost::make_shared(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq); - if(ret->getHash()!=ledgerHash) + Ledger::pointer ret=boost::make_shared(prevHash, transHash, accountHash, totCoins, closingTime, ledgerSeq); + if (ret->getHash() != ledgerHash) { assert(false); return Ledger::pointer(); diff --git a/src/Ledger.h b/src/Ledger.h index 2cca9b3b1..cc1379455 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -56,7 +56,7 @@ public: private: uint256 mHash, mParentHash, mTransHash, mAccountHash; - uint64 mFeeHeld, mTimeStamp; + uint64 mTotCoins, mTimeStamp; uint32 mLedgerSeq; bool mClosed, mValidHash, mAccepted, mImmutable; @@ -72,8 +72,7 @@ protected: void updateHash(); bool addTransaction(Transaction::pointer); - bool addTransaction(const uint256& id, const Serializer& txn); - bool delTransaction(const uint256& id); + bool addTransaction(const uint256& id, const Serializer& txn, uint64_t fee); static Ledger::pointer getSQL(const std::string& sqlStatement); @@ -83,7 +82,7 @@ protected: public: Ledger(const NewcoinAddress& masterID, uint64 startAmount); // used for the starting bootstrap ledger Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, - uint64 feeHeld, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers + uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers Ledger(const std::vector& rawLedger); Ledger(const std::string& rawLedger); @@ -99,7 +98,7 @@ public: const uint256& getParentHash() const { return mParentHash; } const uint256& getTransHash() const { return mTransHash; } const uint256& getAccountHash() const { return mAccountHash; } - uint64 getFeeHeld() const { return mFeeHeld; } + uint64 getTotalCoins() const { return mTotCoins; } uint64 getTimeStamp() const { return mTimeStamp; } uint32 getLedgerSeq() const { return mLedgerSeq; } diff --git a/src/TransactionEngine.cpp b/src/TransactionEngine.cpp index 52a3a1abc..82ab4837c 100644 --- a/src/TransactionEngine.cpp +++ b/src/TransactionEngine.cpp @@ -100,7 +100,7 @@ TransactionEngineResult TransactionEngine::applyTransaction(const SerializedTran Serializer s; txn.add(s); - mLedger->addTransaction(txID, s); + mLedger->addTransaction(txID, s, txnFee); } return result;