From 2782c2dafe20b4ac921333c1ee2649a94ffc5258 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 8 Jan 2012 23:58:13 -0800 Subject: [PATCH] Begin work to track transactions involvings spends from local accounts. --- Ledger.h | 1 - LocalAccount.h | 130 ++++++++++++++++++++++++++++++++++++++++++++++ Transaction.h | 2 +- Wallet.cpp | 23 ++++++++- Wallet.h | 136 ++++--------------------------------------------- 5 files changed, 163 insertions(+), 129 deletions(-) create mode 100644 LocalAccount.h diff --git a/Ledger.h b/Ledger.h index 430da928d3..56542a766c 100644 --- a/Ledger.h +++ b/Ledger.h @@ -18,7 +18,6 @@ class Ledger : public boost::enable_shared_from_this { // The basic Ledger structure, can be opened, closed, or synching public: - typedef boost::shared_ptr pointer; enum TransResult diff --git a/LocalAccount.h b/LocalAccount.h new file mode 100644 index 0000000000..c56f76d36f --- /dev/null +++ b/LocalAccount.h @@ -0,0 +1,130 @@ +#ifndef __LOCALACCOUNT__ +#define __LOCALACCOUNT__ + +class LocalAccountEntry +{ // tracks keys for local accounts +public: + typedef boost::shared_ptr pointer; + +protected: + // core account information + CKey::pointer mPublicKey; + uint160 mAcctID; + std::string mName, mComment; + + // family information + uint160 mAccountFamily; + int mAccountSeq; + + // local usage tracking + uint64 mBalance; // The balance, from the last ledger + uint64 mBalanceLT; // The balance, after all local transactions + uint32 mTxnSeq; // The sequence number of the next transaction + +public: + LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey); + + // Database operations + bool read(); // reads any existing data + bool write(); // creates the record in the first place + bool updateName(); // writes changed name/comment + bool updateBalance(); // writes changed balance/seq + + const uint160& getAccountID() const { return mAcctID; } + int getAccountSeq() 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 + + CKey::pointer getPubKey() { return mPublicKey; } + + void update(uint64 balance, uint32 seq); + uint32 getTxnSeq() const { return mTxnSeq; } + uint32 incTxnSeq() { return mTxnSeq++; } + + uint64 getBalance() const { return mBalance; } + void credit(uint64 amount) { mBalance+=amount; } + void debit(uint64 amount) { assert(mBalance>=amount); mBalance-=amount; } +}; + +class LocalAccountFamily +{ // tracks families of local accounts +public: + typedef boost::shared_ptr pointer; + +protected: + std::map mAccounts; + + uint160 mFamily; // the name for this account family + EC_POINT* mRootPubKey; + + uint32 mLastSeq; + std::string mName, mComment; + + BIGNUM* mRootPrivateKey; + +public: + + LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey); + ~LocalAccountFamily(); + + const uint160& getFamily() const { return mFamily; } + + void unlock(const BIGNUM* privateKey); + void lock(); + bool isLocked() const { return mRootPrivateKey==NULL; } + + void setSeq(uint32 s) { mLastSeq=s; } + uint32 getSeq() { return mLastSeq; } + void setName(const std::string& n) { mName=n; } + void setComment(const std::string& c) { mComment=c; } + + std::map& getAcctMap() { return mAccounts; } + LocalAccountEntry::pointer get(int seq); + uint160 getAccount(int seq, bool keep); + CKey::pointer getPrivateKey(int seq); + + std::string getPubGenHex() const; // The text name of the public key + std::string getShortName() const { return mName; } + std::string getComment() const { return mComment; } + Json::Value getJson() const; + + static std::string getSQLFields(); + std::string getSQL() const; + static LocalAccountFamily::pointer readFamily(const uint160& family); + 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 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& signature); + bool checkSignRaw(Serializer::pointer data, std::vector& signature); + + uint32 getAcctSeq() const; + uint64 getBalance() const; + void incAcctSeq(uint32 transAcctSeq); + + CKey::pointer getPublicKey(); + CKey::pointer getPrivateKey(); + + Json::Value getJson() const; +}; + +#endif diff --git a/Transaction.h b/Transaction.h index 57664f1dba..2eaa865795 100644 --- a/Transaction.h +++ b/Transaction.h @@ -14,8 +14,8 @@ #include "newcoin.pb.h" #include "Hanko.h" #include "Serializer.h" -#include "Wallet.h" #include "SHAMap.h" +#include "LocalAccount.h" /* We could have made something that inherited from the protobuf transaction but this seemed simpler diff --git a/Wallet.cpp b/Wallet.cpp index 0ebb5f662b..dfb26042be 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -6,6 +6,7 @@ #include "boost/foreach.hpp" #include "boost/lexical_cast.hpp" +#include "boost/interprocess/sync/scoped_lock.hpp" #include "Wallet.h" #include "NewcoinAddress.h" @@ -19,7 +20,7 @@ LocalAccountEntry::LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey) : mPublicKey(new CKey(accountFamily, rootPubKey, accountSeq)), mAccountFamily(accountFamily), mAccountSeq(accountSeq), - mBalance(0), mLedgerSeq(0), mTxnSeq(0) + mBalance(0), mBalanceLT(0), mTxnSeq(0) { mAcctID=mPublicKey->GetAddress().GetHash160(); if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey); @@ -788,3 +789,23 @@ 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::iterator it=mAccounts.begin(); it!=mAccounts.end(); ++it) + { + LocalAccount::pointer& lac=it->second; + AccountState::pointer acs=ledger->getAccountState(it->first); + if(!acs) + { // account is not in the ledger + // WRITEME + } + else + { // account is in the ledger + // WRITEME + } + } + if(mLedgergetLedgerSeq()) mLedger=ledger->getLedgerSeq(); +} diff --git a/Wallet.h b/Wallet.h index fb81f3f3d9..7423231e7e 100644 --- a/Wallet.h +++ b/Wallet.h @@ -14,139 +14,21 @@ #include "uint256.h" #include "Serializer.h" +#include "LocalAccount.h" +#include "LocalTransaction.h" -class LocalAccountEntry -{ // tracks keys for local accounts -public: - typedef boost::shared_ptr pointer; - -protected: - // core account information - CKey::pointer mPublicKey; - uint160 mAcctID; - std::string mName, mComment; - - // family information - uint160 mAccountFamily; - int mAccountSeq; - - // local usage tracking - uint64 mBalance; // The balance, last we checked/updated - uint32 mLedgerSeq; // The ledger seq when we updated the balance - uint32 mTxnSeq; // The sequence number of the next transaction - -public: - LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey); - - // Database operations - bool read(); // reads any existing data - bool write(); // creates the record in the first place - bool updateName(); // writes changed name/comment - bool updateBalance(); // writes changed balance/seq - - const uint160& getAccountID() const { return mAcctID; } - int getAccountSeq() 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 - - CKey::pointer getPubKey() { return mPublicKey; } - - void update(uint64 balance, uint32 seq); - uint32 getTxnSeq() const { return mTxnSeq; } - uint32 incTxnSeq() { return mTxnSeq++; } - - uint64 getBalance() const { return mBalance; } - void credit(uint64 amount) { mBalance+=amount; } - void debit(uint64 amount) { assert(mBalance>=amount); mBalance-=amount; } -}; - -class LocalAccountFamily -{ // tracks families of local accounts -public: - typedef boost::shared_ptr pointer; - -protected: - std::map mAccounts; - - uint160 mFamily; // the name for this account family - EC_POINT* mRootPubKey; - - uint32 mLastSeq; - std::string mName, mComment; - - BIGNUM* mRootPrivateKey; - -public: - - LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey); - ~LocalAccountFamily(); - - const uint160& getFamily() const { return mFamily; } - - void unlock(const BIGNUM* privateKey); - void lock(); - bool isLocked() const { return mRootPrivateKey==NULL; } - - void setSeq(uint32 s) { mLastSeq=s; } - uint32 getSeq() { return mLastSeq; } - void setName(const std::string& n) { mName=n; } - void setComment(const std::string& c) { mComment=c; } - - std::map& getAcctMap() { return mAccounts; } - LocalAccountEntry::pointer get(int seq); - uint160 getAccount(int seq, bool keep); - CKey::pointer getPrivateKey(int seq); - - std::string getPubGenHex() const; // The text name of the public key - std::string getShortName() const { return mName; } - std::string getComment() const { return mComment; } - Json::Value getJson() const; - - static std::string getSQLFields(); - std::string getSQL() const; - static LocalAccountFamily::pointer readFamily(const uint160& family); - 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 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& signature); - bool checkSignRaw(Serializer::pointer data, std::vector& signature); - - uint32 getAcctSeq() const; - uint64 getBalance() const; - void incAcctSeq(uint32 transAcctSeq); - - CKey::pointer getPublicKey(); - CKey::pointer getPrivateKey(); - - Json::Value getJson() const; -}; +class Ledger; class Wallet { protected: boost::recursive_mutex mLock; + std::map mFamilies; std::map mAccounts; + std::map mTransactions; + + uint32 mLedger; // ledger we last synched to LocalAccountFamily::pointer doPrivate(const uint256& passPhrase, bool do_create, bool do_unlock); LocalAccountFamily::pointer doPublic(const std::string& pubKey, bool do_create, bool do_db); @@ -155,7 +37,7 @@ protected: const std::string& name, const std::string& comment); public: - Wallet() { ; } + Wallet() : mLedger(0) { ; } uint160 addFamily(const std::string& passPhrase, bool lock); uint160 addFamily(const uint256& passPhrase, bool lock); @@ -197,6 +79,8 @@ public: static std::string privKeyToText(const uint256& privKey); static uint256 textToPrivKey(const std::string&); + void syncToLedger(bool force, Ledger* ledger); + static bool unitTest(); };