#include "Ledger.h" #include "newcoin.pb.h" #include "PackedMessage.h" #include "Application.h" #include "Config.h" #include "BitcoinUtil.h" #include #include #include using namespace boost; using namespace std; Ledger::Ledger(uint64 index) { mIndex=index; mValidSig=false; mValidHash=false; } // TODO: we should probably make a shared pointer type for each of these PB types newcoin::FullLedger* Ledger::createFullLedger() { // TODO: do we need to hash and create mone map first? newcoin::FullLedger* ledger=new newcoin::FullLedger(); ledger->set_index(mIndex); ledger->set_hash(mHash); pair& account=pair(); BOOST_FOREACH(account,mMoneyMap) { newcoin::Account* saveAccount=ledger->add_accounts(); saveAccount->set_address(account.first); saveAccount->set_amount(account.second); } mBundle.addTransactionsToPB(ledger); return(ledger); } void Ledger::setTo(newcoin::FullLedger& ledger) { mIndex=ledger.index(); mBundle.clear(); mMoneyMap.clear(); mValidSig=false; mValidHash=false; int numAccounts=ledger.accounts_size(); for(int n=0; nSerializeToOstream(&savefile); savefile.close(); } delete(ledger); } string& Ledger::getHash() { if(!mValidHash) hash(); return(mHash); } string& Ledger::getSignature() { if(!mValidSig) sign(); return(mSignature); } void Ledger::publish() { PackedMessage::pointer packet=Peer::createValidation(shared_from_this()); theApp->getConnectionPool().relayMessage(NULL,packet,mIndex); } void Ledger::finalize() { } void Ledger::sign() { // TODO: } void Ledger::calcMoneyMap() { // start with map from the previous ledger // go through every transaction Ledger::pointer parent=theApp->getLedgerMaster().getLedger(mIndex-1); if(parent) { mMoneyMap.clear(); mMoneyMap=parent->getMoneyMap(); mBundle.updateMap(mMoneyMap); // TODO: strip the 0 ones } } void Ledger::hash() { calcMoneyMap(); // TODO: } uint64 Ledger::getAmount(std::string address) { return(mMoneyMap[address]); } // returns true if the transaction was valid bool Ledger::addTransaction(newcoin::Transaction& trans) { if(mBundle.hasTransaction(trans)) return(false); Ledger::pointer parent=theApp->getLedgerMaster().getLedger(mIndex-1); if(parent) { // check the lineage of the from addresses vector cacheInputLeftOverAmount; int numInputs=trans.inputs_size(); cacheInputLeftOverAmount.resize(numInputs); for(int n=0; ngetAmount(input.from()); // TODO: checkValid could invalidate mValidSig and mValidHash amountHeld = mBundle.checkValid(input.from(),amountHeld,0,trans.seconds()); if(amountHeld