diff --git a/src/cpp/ripple/HashedObject.cpp b/src/cpp/ripple/HashedObject.cpp index cfa9dcdac..ef0d17aa4 100644 --- a/src/cpp/ripple/HashedObject.cpp +++ b/src/cpp/ripple/HashedObject.cpp @@ -4,6 +4,8 @@ #include #include +#include "../database/SqliteDatabase.h" + #include "Serializer.h" #include "Application.h" #include "Log.h" @@ -188,4 +190,70 @@ HashedObject::pointer HashedObjectStore::retrieve(const uint256& hash) return obj; } +int HashedObjectStore::import(const std::string& file) +{ + cLog(lsWARNING) << "Hash import from \"" << file << "\"."; + std::auto_ptr importDB(new SqliteDatabase(file.c_str())); + importDB->connect(); + + int countYes = 0, countNo = 0; + + SQL_FOREACH(importDB, "SELECT * FROM CommittedObjects;") + { + uint256 hash; + std::string hashStr; + importDB->getStr("Hash", hashStr); + hash.SetHex(hashStr); + if (hash.isZero()) + { + cLog(lsWARNING) << "zero hash found in import table"; + } + else + { + if (retrieve(hash) != HashedObject::pointer()) + ++countNo; + else + { // we don't have this object + std::vector data; + std::string type; + importDB->getStr("ObjType", type); + uint32 index = importDB->getBigInt("LedgerIndex"); + + int size = importDB->getBinary("Object", NULL, 0); + data.resize(size); + importDB->getBinary("Object", &(data.front()), size); + + assert(Serializer::getSHA512Half(data) == hash); + + HashedObjectType htype = hotUNKNOWN; + switch (type[0]) + { + case 'L': htype = hotLEDGER; break; + case 'T': htype = hotTRANSACTION; break; + case 'A': htype = hotACCOUNT_NODE; break; + case 'N': htype = hotTRANSACTION_NODE; break; + default: + assert(false); + cLog(lsERROR) << "Invalid hashed object"; + } + + if (Serializer::getSHA512Half(data) != hash) + { + cLog(lsWARNING) << "Hash mismatch in import table " << hash + << " " << Serializer::getSHA512Half(data); + } + else + { + store(htype, index, data, hash); + ++countYes; + } + } + } + } + + cLog(lsWARNING) << "Imported " << countYes << " nodes, had " << countNo << " nodes"; + waitWrite(); + return countYes; +} + // vim:ts=4 diff --git a/src/cpp/ripple/HashedObject.h b/src/cpp/ripple/HashedObject.h index 05883c2ca..d96101370 100644 --- a/src/cpp/ripple/HashedObject.h +++ b/src/cpp/ripple/HashedObject.h @@ -68,6 +68,8 @@ public: void bulkWrite(); void waitWrite(); void sweep() { mCache.sweep(); mNegativeCache.sweep(); } + + int import(const std::string&); }; #endif