diff --git a/Ledger.cpp b/Ledger.cpp index e0b2bff888..a9b171ebc3 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -322,12 +322,56 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger) theApp->getDB()->executeSQL(sql.c_str()); } +Ledger::pointer Ledger::getSQL(const std::string& sql) +{ + uint256 ledgerHash, prevHash, accountHash, transHash; + uint64 feeHeld, closingTime; + uint32 ledgerSeq; + std::string hash; + + if(1) + { + ScopedLock sl(theApp->getDBLock()); + Database *db=theApp->getDB(); + if(!db->executeSQL(sql.c_str()) || !db->getNextRow()) + return Ledger::pointer(); + + db->getStr("LedgerHash", hash); + ledgerHash.SetHex(hash); + db->getStr("PrevHash", hash); + prevHash.SetHex(hash); + db->getStr("AccountSetHash", hash); + accountHash.SetHex(hash); + db->getStr("TransSetHash", hash); + transHash.SetHex(hash); + feeHeld=db->getBigInt("FeeHeld"); + closingTime=db->getBigInt("ClosingTime"); + ledgerSeq=db->getBigInt("LedgerSeq"); + } + + Ledger::pointer ret(new Ledger(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq)); + if(ret->getHash()!=ledgerHash) + { + assert(false); + return Ledger::pointer(); + } + return ret; +} + Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex) { + std::string sql="SELECT * from Ledgers WHERE LedgerSeq='"; + sql.append(boost::lexical_cast(ledgerIndex)); + sql.append("';"); + return getSQL(sql); } Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash) { + std::string sql="SELECT * from Ledgers WHERE LedgerHash='"; + sql.append(ledgerHash.GetHex()); + sql.append("';"); + return getSQL(sql); } #if 0 diff --git a/Ledger.h b/Ledger.h index b2bd82a78d..d4c72b1ed0 100644 --- a/Ledger.h +++ b/Ledger.h @@ -58,6 +58,8 @@ protected: bool updateAccountState(AccountState::pointer); bool addTransaction(Transaction::pointer); bool delTransaction(const uint256& id); + + static Ledger::pointer getSQL(const std::string& sqlStatement); public: Ledger(const uint160& masterID, uint64 startAmount); // used for the starting bootstrap ledger