Fix the account state bug. Rewrite the relationship between families and

accounts. Remove the LocalAccountEntry class and make some huge
simplifications.
 Ledger.cpp     |   14 +++++--
 LocalAccount.h |   80 +++++++++++++++------------------------------
 Wallet.cpp     |  101 +++++++++++++++++++--------------------------------------
This commit is contained in:
JoelKatz
2012-01-18 11:43:25 -08:00
parent 2e0d47f782
commit b2b8caf6b9
3 changed files with 72 additions and 123 deletions

View File

@@ -95,6 +95,7 @@ bool Ledger::updateAccountState(AccountState::pointer state)
bool Ledger::addAccountState(AccountState::pointer state)
{
assert(!mAccepted);
assert( (state->getBalance()==0) || (state->getSeq()>0) );
SHAMapItem::pointer item(new SHAMapItem(state->getAccountID(), state->getRaw()));
return mAccountStateMap->addGiveItem(item);
}
@@ -269,11 +270,16 @@ Ledger::pointer Ledger::closeLedger(uint64 timeStamp)
return Ledger::pointer(new Ledger(*this, timeStamp));
}
void LocalAccount::syncLedger(const uint160& acctID)
void LocalAccount::syncLedger()
{
AccountState::pointer as=theApp->getMasterLedger().getAccountState(acctID);
if(!as) setLedgerBalance(0);
else syncLedger(as->getBalance(), as->getSeq());
AccountState::pointer as=theApp->getMasterLedger().getAccountState(getAddress());
if(!as) mLgrBalance=0;
else
{
mLgrBalance=as->getBalance();
if( (mLgrBalance!=0) && (mAccountSeq==0) ) mAccountSeq=1;
if(mAccountSeq<as->getSeq()) mAccountSeq=as->getSeq();
}
}
bool Ledger::unitTest()

View File

@@ -1,10 +1,15 @@
#ifndef __LOCALACCOUNT__
#define __LOCALACCOUNT__
class LocalAccountEntry
#include "boost/enable_shared_from_this.hpp"
#include "boost/shared_ptr.hpp"
class LocalAccountFamily;
class LocalAccount
{ // tracks keys for local accounts
public:
typedef boost::shared_ptr<LocalAccountEntry> pointer;
typedef boost::shared_ptr<LocalAccount> pointer;
protected:
// core account information
@@ -13,7 +18,7 @@ protected:
std::string mName, mComment;
// family information
uint160 mAccountFamily;
boost::shared_ptr<LocalAccountFamily> mFamily;
int mAccountSeq;
// local usage tracking
@@ -22,7 +27,7 @@ protected:
uint32 mTxnSeq; // The sequence number of the next transaction
public:
LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey);
LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq);
// Database operations
bool read(); // reads any existing data
@@ -30,12 +35,21 @@ public:
bool updateName(); // writes changed name/comment
bool updateBalance(); // writes changed balance/seq
const uint160& getAccountID() const { return mAcctID; }
int getAccountSeq() const { return mAccountSeq; }
const uint160& getAddress() const { return mAcctID; }
int getAcctSeq() const { return mAccountSeq; }
std::string getLocalAccountName() const; // The name used locally to identify this account
std::string getAccountName() const; // The normal account name used to send to this account
std::string getFullName() const;
std::string getShortName() const;
std::string getFamilyName() const;
CKey::pointer getPubKey() { return mPublicKey; }
bool isLocked() const;
bool isIssued() const;
CKey::pointer getPublicKey() { return mPublicKey; }
CKey::pointer getPrivateKey();
Json::Value getJson() const;
void update(uint64 balance, uint32 seq);
uint32 getTxnSeq() const { return mTxnSeq; }
@@ -45,20 +59,17 @@ public:
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(uint64_t lb, uint32 sq)
{
mLgrBalance=lb;
if(mTxnSeq<sq) mTxnSeq=sq;
}
void syncLedger();
};
class LocalAccountFamily
class LocalAccountFamily : public boost::enable_shared_from_this<LocalAccountFamily>
{ // tracks families of local accounts
public:
typedef boost::shared_ptr<LocalAccountFamily> pointer;
protected:
std::map<int, LocalAccountEntry::pointer> mAccounts;
std::map<int, LocalAccount::pointer> mAccounts;
uint160 mFamily; // the name for this account family
EC_POINT* mRootPubKey;
@@ -84,10 +95,11 @@ public:
void setName(const std::string& n) { mName=n; }
void setComment(const std::string& c) { mComment=c; }
std::map<int, LocalAccountEntry::pointer>& getAcctMap() { return mAccounts; }
LocalAccountEntry::pointer get(int seq);
std::map<int, LocalAccount::pointer>& getAcctMap() { return mAccounts; }
LocalAccount::pointer get(int seq);
uint160 getAccount(int seq, bool keep);
CKey::pointer getPrivateKey(int seq);
CKey::pointer getPublicKey(int seq);
std::string getPubGenHex() const; // The text name of the public key
std::string getShortName() const { return mName; }
@@ -100,40 +112,4 @@ public:
void write(bool is_new);
};
class LocalAccount
{ // tracks a single local account in a form that can be passed to other code.
public:
typedef boost::shared_ptr<LocalAccount> pointer;
protected:
LocalAccountFamily::pointer mFamily;
int mSeq;
public:
LocalAccount(LocalAccountFamily::pointer fam, int seq) : mFamily(fam), mSeq(seq) { ; }
uint160 getAddress() const;
bool isLocked() const;
std::string getShortName() const;
std::string getFullName() const;
std::string getFamilyName() const;
bool isIssued() const;
bool signRaw(Serializer::pointer);
bool signRaw(Serializer::pointer, std::vector<unsigned char>& signature);
bool checkSignRaw(Serializer::pointer data, std::vector<unsigned char>& signature);
uint32 getAcctSeq() const;
uint64 getBalance() const;
void setLedgerBalance(uint64_t ledgerBalance);
void syncLedger(uint64_t ledgerBalance, uint32_t ledgerSeq);
void incAcctSeq(uint32 transAcctSeq);
void syncLedger(const uint160& acctID);
CKey::pointer getPublicKey();
CKey::pointer getPrivateKey();
Json::Value getJson() const;
};
#endif

View File

@@ -18,23 +18,22 @@
#define CHECK_NEW_FAMILIES 500
#endif
LocalAccountEntry::LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey) :
mPublicKey(new CKey(accountFamily, rootPubKey, accountSeq)),
mAccountFamily(accountFamily), mAccountSeq(accountSeq),
LocalAccount::LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq) :
mPublicKey(family->getPublicKey(accountSeq)), mFamily(family), mAccountSeq(accountSeq),
mLgrBalance(0), mTxnDelta(0), mTxnSeq(0)
{
mAcctID=mPublicKey->GetAddress().GetHash160();
if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey);
}
std::string LocalAccountEntry::getAccountName() const
std::string LocalAccount::getAccountName() const
{
return mPublicKey->GetAddress().GetString();
}
std::string LocalAccountEntry::getLocalAccountName() const
std::string LocalAccount::getLocalAccountName() const
{
return NewcoinAddress(mAccountFamily).GetString() + ":" + boost::lexical_cast<std::string>(mAccountSeq);
return NewcoinAddress(mFamily->getFamily()).GetString() + ":" + boost::lexical_cast<std::string>(mAccountSeq);
}
LocalAccountFamily::LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey) :
@@ -52,13 +51,13 @@ LocalAccountFamily::~LocalAccountFamily()
uint160 LocalAccountFamily::getAccount(int seq, bool keep)
{
std::map<int, LocalAccountEntry::pointer>::iterator ait=mAccounts.find(seq);
if(ait!=mAccounts.end()) return ait->second->getAccountID();
std::map<int, LocalAccount::pointer>::iterator ait=mAccounts.find(seq);
if(ait!=mAccounts.end()) return ait->second->getAddress();
LocalAccountEntry::pointer lae(new LocalAccountEntry(mFamily, seq, mRootPubKey));
LocalAccount::pointer lae(new LocalAccount(shared_from_this(), seq));
mAccounts.insert(std::make_pair(seq, lae));
return lae->getAccountID();
return lae->getAddress();
}
void LocalAccountFamily::unlock(const BIGNUM* privateKey)
@@ -98,6 +97,11 @@ void LocalAccountFamily::lock()
}
}
CKey::pointer LocalAccountFamily::getPublicKey(int seq)
{
return CKey::pointer(new CKey(mFamily, mRootPubKey, seq));
}
CKey::pointer LocalAccountFamily::getPrivateKey(int seq)
{
if(!mRootPrivateKey)
@@ -293,12 +297,12 @@ std::string LocalAccountFamily::getSQL() const
return ret;
}
LocalAccountEntry::pointer LocalAccountFamily::get(int seq)
LocalAccount::pointer LocalAccountFamily::get(int seq)
{
std::map<int, LocalAccountEntry::pointer>::iterator act=mAccounts.find(seq);
std::map<int, LocalAccount::pointer>::iterator act=mAccounts.find(seq);
if(act!=mAccounts.end()) return act->second;
LocalAccountEntry::pointer ret(new LocalAccountEntry(mFamily, seq, mRootPubKey));
LocalAccount::pointer ret(new LocalAccount(shared_from_this(), seq));
mAccounts.insert(std::make_pair(seq, ret));
return ret;
}
@@ -347,18 +351,16 @@ uint160 Wallet::findFamilyPK(const std::string& pubKey)
return fam->getFamily();
}
uint160 LocalAccount::getAddress() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return uint160();
return la->getAccountID();
}
std::string LocalAccount::getShortName() const
{
std::string ret(mFamily->getShortName());
std::string ret, cmt;
if(!theApp->getWallet().getFamilyInfo(mFamily->getFamily(), ret, cmt))
ret=mFamily->getFamily().GetHex();
ret.append(":");
ret.append(boost::lexical_cast<std::string>(mSeq));
if(mName.empty())
ret.append(boost::lexical_cast<std::string>(mAccountSeq));
else ret.append(mName);
return ret;
}
@@ -366,7 +368,7 @@ std::string LocalAccount::getFullName() const
{
std::string ret(mFamily->getFamily().GetHex());
ret.append(":");
ret.append(boost::lexical_cast<std::string>(mSeq));
ret.append(boost::lexical_cast<std::string>(mAccountSeq));
return ret;
}
@@ -390,7 +392,7 @@ Json::Value LocalAccount::getJson() const
ret["Issued"]=Json::Value(isIssued());
ret["IsLocked"]=mFamily->isLocked();
uint64 eb=getBalance();
uint64 eb=getEffectiveBalance();
if(eb!=0) ret["Balance"]=boost::lexical_cast<std::string>(eb);
uint32 sq=getAcctSeq();
@@ -401,47 +403,12 @@ Json::Value LocalAccount::getJson() const
bool LocalAccount::isIssued() const
{
return mSeq < mFamily->getSeq();
}
uint32 LocalAccount::getAcctSeq() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return 0;
return la->getAccountSeq();
}
uint64 LocalAccount::getBalance() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return 0;
return la->getEffectiveBalance();
}
void LocalAccount::syncLedger(uint64_t lb, uint32_t ls)
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return ;
la->syncLedger(lb, ls);
}
void LocalAccount::setLedgerBalance(uint64_t lb)
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return ;
la->setLedgerBalance(lb);
}
CKey::pointer LocalAccount::getPublicKey()
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return CKey::pointer();
return la->getPubKey();
return mAccountSeq < mFamily->getSeq();
}
CKey::pointer LocalAccount::getPrivateKey()
{
return mFamily->getPrivateKey(mSeq);
return mFamily->getPrivateKey(mAccountSeq);
}
void Wallet::getFamilies(std::vector<uint160>& familyIDs)
@@ -560,7 +527,7 @@ LocalAccount::pointer Wallet::getNewLocalAccount(const uint160& family)
mAccounts.insert(std::make_pair(acct, lac));
sl.unlock();
lac->syncLedger(acct);
lac->syncLedger();
return lac;
}
@@ -578,7 +545,7 @@ LocalAccount::pointer Wallet::getLocalAccount(const uint160& family, int seq)
mAccounts.insert(std::make_pair(acct, lac));
sl.unlock();
lac->syncLedger(acct);
lac->syncLedger();
return lac;
}
@@ -594,7 +561,7 @@ LocalAccount::pointer Wallet::findAccountForTransaction(uint64 amount)
{
boost::recursive_mutex::scoped_lock sl(mLock);
for(std::map<uint160, LocalAccount::pointer>::iterator it=mAccounts.begin(); it!=mAccounts.end(); ++it)
if(!it->second->isLocked() && (it->second->getBalance()>=amount) )
if(!it->second->isLocked() && (it->second->getEffectiveBalance()>=amount) )
return it->second;
return LocalAccount::pointer();
}
@@ -646,9 +613,9 @@ void Wallet::delFamily(const uint160& familyName)
std::map<uint160, LocalAccountFamily::pointer>::iterator fit=mFamilies.find(familyName);
if(fit==mFamilies.end()) return;
std::map<int, LocalAccountEntry::pointer>& acctMap=fit->second->getAcctMap();
for(std::map<int, LocalAccountEntry::pointer>::iterator it=acctMap.begin(); it!=acctMap.end(); ++it)
mAccounts.erase(it->second->getAccountID());
std::map<int, LocalAccount::pointer>& acctMap=fit->second->getAcctMap();
for(std::map<int, LocalAccount::pointer>::iterator it=acctMap.begin(); it!=acctMap.end(); ++it)
mAccounts.erase(it->second->getAddress());
mFamilies.erase(familyName);
}