From d26577ffd7991ce7220cfccbaf95763bf5aa7416 Mon Sep 17 00:00:00 2001 From: jed Date: Wed, 26 Oct 2011 20:49:04 -0700 Subject: [PATCH] . --- Ledger.cpp | 58 ++++++++++++++++++++++++++++++-- Ledger.h | 3 +- UniqueNodeList.h | 2 +- database/SqliteDatabase.cpp | 66 ++++++++++++++++++++++++++++++++++++- database/SqliteDatabase.h | 23 +++++++++++++ database/database.cpp | 26 ++++++++++++--- database/database.h | 21 +++++------- types.h | 2 ++ 8 files changed, 179 insertions(+), 22 deletions(-) diff --git a/Ledger.cpp b/Ledger.cpp index 702e26ae5..264444341 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -96,6 +96,8 @@ Ledger::pointer Ledger::getParent() return(mParent); } +// TODO: we can optimize so the ledgers only hold the delta from the accepted ledger +// TODO: check to make sure the ledger is consistent after we load it bool Ledger::load(uint256& hash) { Database* db=theApp->getDB(); @@ -107,8 +109,60 @@ bool Ledger::load(uint256& hash) if(db->executeSQL(sql.c_str())) { - db->getNextRow(); - sql="SELECT * from Transactions where " + if(db->getNextRow()) + { + mIndex=db->getInt("LedgerIndex"); + mHash=hash; + mValidSig=false; + mAccounts.clear(); + mTransactions.clear(); + mDiscardedTransactions.clear(); + + db->getBinary("ParentHash",mParentHash.begin(),mParentHash.GetSerializeSize()); + mFeeHeld=db->getBigInt("FeeHeld"); + + + char buf[100]; + sql="SELECT Transactions.* from Transactions,LedgerTransactionMap where Transactions.TransactionID=LedgerTransactionMap.TransactionID and LedgerTransactionMap.LedgerID="; + sql.append(itoa( db->getInt(0),buf,10) ); + if(db->executeSQL(sql.c_str())) + { + unsigned char tbuf[1000]; + while(db->getNextRow()) + { + TransactionPtr trans=TransactionPtr(new newcoin::Transaction()); + trans->set_amount( db->getBigInt("Amount")); + trans->set_seqnum( db->getInt("seqnum")); + trans->set_ledgerindex( db->getInt("ledgerIndex")); + db->getBinary("from",tbuf,1000); + trans->set_from(tbuf,20); + db->getBinary("dest",tbuf,1000); + trans->set_dest(tbuf,20); + db->getBinary("pubkey",tbuf,1000); + trans->set_pubkey(tbuf,128); + db->getBinary("sig",tbuf,1000); + trans->set_sig(tbuf,32); + + mTransactions.push_back(trans); + } + } + + sql="SELECT Accounts.* from Acconts,LedgerAcountMap where Accounts.AccountID=LedgerAccountMap.AccountID and LedgerAccountMap.LedgerID="; + sql.append(buf); + if(db->executeSQL(sql.c_str())) + { + while(db->getNextRow()) + { + uint160 address; + db->getBinary("Address",address.begin(),address.GetSerializeSize()); + + mAccounts[address].first=db->getBigInt("Amount"); + mAccounts[address].second=db->getInt("SeqNum"); + + } + } + return(true); + } } return(false); } diff --git a/Ledger.h b/Ledger.h index 9b5b75a5d..96868ba19 100644 --- a/Ledger.h +++ b/Ledger.h @@ -26,8 +26,7 @@ private: uint256 mSignature; uint256 mParentHash; uint32 mValidationSeqNum; - - + uint64 mFeeHeld; std::map mAccounts; std::list mTransactions; diff --git a/UniqueNodeList.h b/UniqueNodeList.h index 91cd2d3e5..4fd22dd31 100644 --- a/UniqueNodeList.h +++ b/UniqueNodeList.h @@ -10,7 +10,7 @@ public: //void load(); //void save(); - void addNode(uint160& hanko,uint1024& publicKey); + void addNode(uint160& hanko,std::vector& publicKey); void removeNode(uint160& hanko); // 0- we don't care, 1- we care and is valid, 2-invalid signature diff --git a/database/SqliteDatabase.cpp b/database/SqliteDatabase.cpp index 8849faa9e..5bbdf215f 100644 --- a/database/SqliteDatabase.cpp +++ b/database/SqliteDatabase.cpp @@ -1,10 +1,74 @@ #include "SqliteDatabase.h" -SqliteDatabase::SqliteDatabase() +SqliteDatabase::SqliteDatabase() : Database { } +void SqliteDatabase::connect() +{ + +} +void SqliteDatabase::disconnect() +{ + +} + +// returns true if the query went ok +bool SqliteDatabase::executeSQL(const char* sql) +{ + +} + +// tells you how many rows were changed by an update or insert +int SqliteDatabase::getNumRowsAffected() +{ + +} + +// returns false if there are no results +bool SqliteDatabase::startIterRows() +{ + +} +void SqliteDatabase::endIterRows() +{ + +} + +// call this after you executeSQL +// will return false if there are no more rows +bool SqliteDatabase::getNextRow() +{ + +} + +char* SqliteDatabase::getStr(int colIndex,std::string& retStr) +{ + +} +int32 SqliteDatabase::getInt(int colIndex) +{ + +} +float SqliteDatabase::getFloat(int colIndex) +{ + +} +bool SqliteDatabase::getBool(int colIndex) +{ + +} +bool SqliteDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize) +{ + +} +uint64 SqliteDatabase::getBigInt(int colIndex) +{ + +} + + /* http://www.sqlite.org/lang_expr.html BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character. For example: X'53514C697465' diff --git a/database/SqliteDatabase.h b/database/SqliteDatabase.h index a009e50f8..069dd06ad 100644 --- a/database/SqliteDatabase.h +++ b/database/SqliteDatabase.h @@ -5,6 +5,29 @@ class SqliteDatabase : public Database public: SqliteDatabase(); + void connect(); + void disconnect(); + + // returns true if the query went ok + bool executeSQL(const char* sql); + + // tells you how many rows were changed by an update or insert + int getNumRowsAffected(); + + // returns false if there are no results + bool startIterRows(); + void endIterRows(); + + // call this after you executeSQL + // will return false if there are no more rows + bool getNextRow(); + + char* getStr(int colIndex,std::string& retStr); + int32 getInt(int colIndex); + float getFloat(int colIndex); + bool getBool(int colIndex); + bool getBinary(int colIndex,unsigned char* buf,int maxSize); + uint64 getBigInt(int colIndex); void escape(unsigned char* start,int size,std::string& retStr); diff --git a/database/database.cpp b/database/database.cpp index 999afdfef..f968422fa 100644 --- a/database/database.cpp +++ b/database/database.cpp @@ -1,6 +1,4 @@ #include "database.h" -#include "../utillib/reportingmechanism.h" -#include "string/i4string.h" #include @@ -26,7 +24,7 @@ char* Database::getStr(const char* colName,std::string& retStr) return(NULL); } -w32 Database::getInt(const char* colName) +int32 Database::getInt(const char* colName) { int index; if(getColNumber(colName,&index)) @@ -56,6 +54,26 @@ bool Database::getBool(const char* colName) return(0); } +bool Database::getBinary(const char* colName,unsigned char* buf,int maxSize) +{ + int index; + if(getColNumber(colName,&index)) + { + return(getBinary(index,buf,maxSize)); + } + return(0); +} + +uint64 Database::getBigInt(const char* colName) +{ + int index; + if(getColNumber(colName,&index)) + { + return(getBigInt(index)); + } + return(0); +} + // returns false if can't find col @@ -63,7 +81,7 @@ bool Database::getColNumber(const char* colName,int* retIndex) { for(int n=0; n +#include "../types.h" /* this maintains the connection to the database @@ -10,10 +11,10 @@ class Database { protected: int mNumCol; - std:string mUser; - std:string mHost; - std:string mDBPass; - std:string* mColNameTable; + std::string mUser; + std::string mHost; + std::string mDBPass; + std::string* mColNameTable; bool getColNumber(const char* colName, int* retIndex); @@ -48,13 +49,15 @@ public: int32 getInt(const char* colName); float getFloat(const char* colName); bool getBool(const char* colName); - bool getBinary(const char* colName,char* buf,int maxSize); + bool getBinary(const char* colName,unsigned char* buf,int maxSize); + uint64 getBigInt(const char* colName); virtual char* getStr(int colIndex,std::string& retStr)=0; virtual int32 getInt(int colIndex)=0; virtual float getFloat(int colIndex)=0; virtual bool getBool(int colIndex)=0; - virtual bool getBinary(int colIndex,char* buf,int maxSize)=0; + virtual bool getBinary(int colIndex,unsigned char* buf,int maxSize)=0; + virtual uint64 getBigInt(int colIndex)=0; int getSingleDBValueInt(const char* sql); float getSingleDBValueFloat(const char* sql); @@ -63,11 +66,5 @@ public: }; -class MsqlDatabase -{ - -}; - - #endif diff --git a/types.h b/types.h index 52fe37e46..52ea48e14 100644 --- a/types.h +++ b/types.h @@ -3,12 +3,14 @@ typedef __int64 int64; typedef unsigned __int64 uint64; typedef unsigned int uint32; +typedef int int32; #else typedef long long int64; typedef unsigned long long uint64; typedef unsigned int uint32; +typedef int int32; #endif #if defined(_MSC_VER) && _MSC_VER < 1300