Local accounts now have their balances and sequences managed from the active

ledger, not from the wallet.
This commit is contained in:
JoelKatz
2012-04-24 04:24:14 -07:00
parent f36d1fdc08
commit 0392d8ddcb
6 changed files with 42 additions and 70 deletions

View File

@@ -17,12 +17,12 @@
class AccountState
{
public:
typedef boost::shared_ptr<AccountState> pointer;
typedef boost::shared_ptr<AccountState> pointer;
private:
NewcoinAddress mAccountID;
SerializedLedgerEntry::pointer mLedgerEntry;
bool mValid;
NewcoinAddress mAccountID;
SerializedLedgerEntry::pointer mLedgerEntry;
bool mValid;
public:
AccountState(const NewcoinAddress& AccountID); // For new accounts

View File

@@ -117,7 +117,7 @@ void Application::run()
// temporary
mWallet.load();
mWallet.syncToLedger(true, &(*secondLedger));
// mWallet.syncToLedger(true, &(*secondLedger));
// temporary
mIOService.run(); // This blocks

View File

@@ -182,18 +182,6 @@ Ledger::pointer Ledger::closeLedger(uint64 timeStamp)
return Ledger::pointer(new Ledger(*this, timeStamp)); // can't use make_shared, constructor is protected
}
void LocalAccount::syncLedger()
{
AccountState::pointer as = theApp->getMasterLedger().getCurrentLedger()->getAccountState(getAddress());
if(!as) mLgrBalance = 0;
else
{
mLgrBalance = as->getBalance();
if ( (mLgrBalance != 0) && (mTxnSeq == 0) ) mTxnSeq = 1;
if (mTxnSeq < as->getSeq()) mTxnSeq = as->getSeq();
}
}
bool Ledger::unitTest()
{
#if 0

View File

@@ -4,6 +4,8 @@
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include "AccountState.h"
class LocalAccountFamily;
class LocalAccount
@@ -21,11 +23,6 @@ protected:
boost::shared_ptr<LocalAccountFamily> mFamily;
int mAccountFSeq;
// local usage tracking
uint64 mLgrBalance; // The balance, from the last ledger
int64 mTxnDelta; // The balance changes from local/pending transactions
uint32 mTxnSeq; // The sequence number of the next transaction
public:
LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq);
@@ -48,16 +45,8 @@ public:
CKey::pointer getPrivateKey();
Json::Value getJson() const;
void update(uint64 balance, uint32 seq);
uint32 getTxnSeq() const { return mTxnSeq; }
uint32 incTxnSeq() { return mTxnSeq++; }
int64 getEffectiveBalance() const { return static_cast<int64_t>(mLgrBalance) + mTxnDelta; }
void credit(uint64 amount) { mTxnDelta += amount; }
void debit(uint64 amount) { mTxnDelta -= amount; }
void setLedgerBalance(uint64_t lb) { mLgrBalance = lb; if (mTxnSeq == 0) mTxnSeq = 1; }
void syncLedger();
AccountState::pointer getAccountState() const;
uint64 getEffectiveBalance() const;
};
class LocalAccountFamily : public boost::enable_shared_from_this<LocalAccountFamily>

View File

@@ -14,6 +14,9 @@
Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAddress& toAccount, uint64 amount,
uint32 ident, uint32 ledger) : mInLedger(0), mStatus(NEW)
{
AccountState::pointer accountState = fromLocalAccount->getAccountState();
if (!accountState) throw std::runtime_error("transaction on non-existent account");
mAccountFrom = fromLocalAccount->getAddress();
mTransaction = boost::make_shared<SerializedTransaction>(ttMAKE_PAYMENT);
@@ -22,8 +25,9 @@ Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAd
assert(mFromPubKey);
mTransaction->setSigningAccount(mFromPubKey->GetPubKey());
mTransaction->setSequence(fromLocalAccount->getTxnSeq());
mTransaction->setSequence(accountState->getSeq());
assert(mTransaction->getSequence() != 0);
mTransaction->setTransactionFee(100); // for now
mTransaction->setITFieldAccount(sfDestination, toAccount);

View File

@@ -24,12 +24,10 @@
//
LocalAccount::LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int familySeq) :
mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq),
mLgrBalance(0), mTxnDelta(0), mTxnSeq(0)
mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq)
{
mAcctID.setAccountPublic(mPublicKey->GetPubKey());
if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey);
if(theApp!=NULL) mPublicKey = theApp->getPubKeyCache().store(mAcctID, mPublicKey);
}
std::string LocalAccount::getFullName() const
@@ -37,7 +35,6 @@ std::string LocalAccount::getFullName() const
std::string ret(mFamily->getFamily().humanFamilyGenerator());
ret.append(":");
ret.append(boost::lexical_cast<std::string>(mAccountFSeq));
return ret;
}
@@ -51,21 +48,36 @@ std::string LocalAccount::getFamilyName() const
return mFamily->getFamily().humanFamilyGenerator();
}
AccountState::pointer LocalAccount::getAccountState() const
{
return theApp->getOPs().getAccountState(mAcctID);
}
uint64 LocalAccount::getEffectiveBalance() const
{
AccountState::pointer as = getAccountState();
if (!as) return 0;
return as->getBalance();
}
Json::Value LocalAccount::getJson() const
{
Json::Value ret(Json::objectValue);
ret["Family"]=getFamilyName();
ret["AccountID"]=getAddress().humanAccountID();
ret["AccountPublic"]=getAddress().humanAccountPublic();
ret["FullName"]=getFullName();
ret["Issued"]=Json::Value(isIssued());
ret["IsLocked"]=mFamily->isLocked();
ret["Family"] = getFamilyName();
ret["AccountID"] = getAddress().humanAccountID();
ret["AccountPublic"] = getAddress().humanAccountPublic();
ret["FullName"] = getFullName();
ret["Issued"] = Json::Value(isIssued());
ret["IsLocked"] = mFamily->isLocked();
uint64 eb=getEffectiveBalance();
if(eb!=0) ret["Balance"]=boost::lexical_cast<std::string>(eb);
uint32 sq=getTxnSeq();
if(sq!=0) ret["TxnSeq"]=boost::lexical_cast<std::string>(sq);
AccountState::pointer as = getAccountState();
if (as) ret["Account"] = "None";
else
{
Json::Value acct(Json::objectValue);
as->addJson(acct);
ret["Account"] = acct;
}
return ret;
}
@@ -475,7 +487,6 @@ LocalAccount::pointer Wallet::getNewLocalAccount(const NewcoinAddress& family)
mAccounts.insert(std::make_pair(acct, lac));
sl.unlock();
lac->syncLedger();
return lac;
}
@@ -495,7 +506,6 @@ LocalAccount::pointer Wallet::getLocalAccount(const NewcoinAddress& family, int
mAccounts.insert(std::make_pair(acct, lac));
sl.unlock();
lac->syncLedger();
return lac;
}
@@ -743,25 +753,6 @@ bool Wallet::unitTest()
return true;
}
void Wallet::syncToLedger(bool force, Ledger* ledger)
{
boost::recursive_mutex::scoped_lock sl(mLock);
if(!force && (mLedger>=ledger->getLedgerSeq())) return;
for(std::map<uint256, LocalTransaction::pointer>::iterator xit=mTransactions.begin();
xit!=mTransactions.end(); ++xit)
{ // check each transaction, see if it's in the ledger or allowed in the ledger
// WRITEME
}
for(std::map<NewcoinAddress, LocalAccount::pointer>::iterator ait=mAccounts.begin(); ait!=mAccounts.end(); ++ait)
{ // check each account, see if our ledger balance matches
LocalAccount::pointer& lac=ait->second;
AccountState::pointer acs=ledger->getAccountState(ait->first);
if(!acs) lac->setLedgerBalance(0);
else lac->setLedgerBalance(acs->getBalance());
}
if(mLedger<ledger->getLedgerSeq()) mLedger=ledger->getLedgerSeq();
}
#if 0
// We can't replicate the transaction logic in the wallet