From 573865cb23f42b3be576e255aeb39ceeeb3e5799 Mon Sep 17 00:00:00 2001 From: jed Date: Tue, 9 Oct 2012 03:01:32 -0700 Subject: [PATCH] allow you to load old ledger --- newcoind.cfg | 6 ++++ src/Application.cpp | 84 ++++++++++++++++++++++++++++++-------------- src/Application.h | 3 ++ src/Config.cpp | 1 + src/Config.h | 3 ++ src/Ledger.cpp | 11 +++--- src/Ledger.h | 6 ++-- src/LedgerMaster.cpp | 4 ++- src/NetworkOPs.cpp | 6 ++-- src/main.cpp | 5 +++ 10 files changed, 92 insertions(+), 37 deletions(-) diff --git a/newcoind.cfg b/newcoind.cfg index 689b27692..6a3e97437 100644 --- a/newcoind.cfg +++ b/newcoind.cfg @@ -118,6 +118,12 @@ [rpc_allow_remote] 1 +[websocket_ip] +0.0.0.0 + +[websocket_port] +5006 + [debug_logfile] log/debug.log diff --git a/src/Application.cpp b/src/Application.cpp index 2011ca8e7..49bd4836d 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -73,6 +73,7 @@ void Application::run() boost::thread auxThread(boost::bind(&boost::asio::io_service::run, &mAuxService)); auxThread.detach(); + if (!theConfig.RUN_STANDALONE) mSNTPClient.init(theConfig.SNTP_SERVERS); @@ -87,9 +88,24 @@ void Application::run() boost::thread t6(boost::bind(&InitDB, &mNetNodeDB, "netnode.db", NetNodeDBInit, NetNodeDBCount)); t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); t6.join(); + if(theConfig.START_UP==Config::FRESH) + { + Log(lsINFO) << "Starting new Ledger"; + startNewLedger(); + }else if(theConfig.START_UP==Config::LOAD) + { + Log(lsINFO) << "Loading Old Ledger"; + loadOldLedger(); + }else + { // TODO: This should really not validate a ledger until it gets the current one from our peers + // but I'll let david make this change since a lot of code assumes we have a ledger + // for now just do what we always were doing + startNewLedger(); + } + // // Begin validation and ip maintenance. - // - Wallet maintains local information: including identity and network connection persistency information. + // - Wallet maintains local information: including identity and network connection persistence information. // mWallet.start(); @@ -132,32 +148,8 @@ void Application::run() if (!theConfig.RUN_STANDALONE) mConnectionPool.start(); - // New stuff. - NewcoinAddress rootSeedMaster = NewcoinAddress::createSeedGeneric("masterpassphrase"); - NewcoinAddress rootGeneratorMaster = NewcoinAddress::createGeneratorPublic(rootSeedMaster); - NewcoinAddress rootAddress = NewcoinAddress::createAccountPublic(rootGeneratorMaster, 0); - - // Print enough information to be able to claim root account. - Log(lsINFO) << "Root master seed: " << rootSeedMaster.humanSeed(); - Log(lsINFO) << "Root account: " << rootAddress.humanAccountID(); - - { - Ledger::pointer firstLedger = boost::make_shared(rootAddress, SYSTEM_CURRENCY_START); - assert(!!firstLedger->getAccountState(rootAddress)); - firstLedger->updateHash(); - firstLedger->setClosed(); - firstLedger->setAccepted(); - mMasterLedger.pushLedger(firstLedger); - - Ledger::pointer secondLedger = boost::make_shared(true, boost::ref(*firstLedger)); - secondLedger->setClosed(); - secondLedger->setAccepted(); - mMasterLedger.pushLedger(secondLedger, boost::make_shared(true, boost::ref(*secondLedger))); - assert(!!secondLedger->getAccountState(rootAddress)); - mNetOps.setLastCloseTime(secondLedger->getCloseTimeNC()); - } - + if (theConfig.RUN_STANDALONE) { Log(lsWARNING) << "Running in standalone mode"; @@ -182,4 +174,44 @@ Application::~Application() delete mHashNodeDB; delete mNetNodeDB; } +void Application::startNewLedger() +{ + // New stuff. + NewcoinAddress rootSeedMaster = NewcoinAddress::createSeedGeneric("masterpassphrase"); + NewcoinAddress rootGeneratorMaster = NewcoinAddress::createGeneratorPublic(rootSeedMaster); + NewcoinAddress rootAddress = NewcoinAddress::createAccountPublic(rootGeneratorMaster, 0); + + // Print enough information to be able to claim root account. + Log(lsINFO) << "Root master seed: " << rootSeedMaster.humanSeed(); + Log(lsINFO) << "Root account: " << rootAddress.humanAccountID(); + + { + Ledger::pointer firstLedger = boost::make_shared(rootAddress, SYSTEM_CURRENCY_START); + assert(!!firstLedger->getAccountState(rootAddress)); + firstLedger->updateHash(); + firstLedger->setClosed(); + firstLedger->setAccepted(); + mMasterLedger.pushLedger(firstLedger); + + Ledger::pointer secondLedger = boost::make_shared(true, boost::ref(*firstLedger)); + secondLedger->setClosed(); + secondLedger->setAccepted(); + mMasterLedger.pushLedger(secondLedger, boost::make_shared(true, boost::ref(*secondLedger))); + assert(!!secondLedger->getAccountState(rootAddress)); + mNetOps.setLastCloseTime(secondLedger->getCloseTimeNC()); + } +} + +void Application::loadOldLedger() +{ + Ledger::pointer lastLedger = Ledger::getSQL("SELECT * from Ledgers order by LedgerSeq desc limit 1;",true); + + if(!lastLedger) + { + std::cout << "No Ledger found?" << std::endl; + exit(-1); + } + mMasterLedger.pushLedger(lastLedger); + mNetOps.setLastCloseTime(lastLedger->getCloseTimeNC()); +} // vim:ts=4 diff --git a/src/Application.h b/src/Application.h index 8ab7f83ed..9a37333fb 100644 --- a/src/Application.h +++ b/src/Application.h @@ -67,6 +67,9 @@ class Application std::map mPeerMap; boost::recursive_mutex mPeerMapLock; + void startNewLedger(); + void loadOldLedger(); + public: Application(); ~Application(); diff --git a/src/Config.cpp b/src/Config.cpp index 4e4cb8e91..e4fb3f79a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -155,6 +155,7 @@ void Config::setup(const std::string& strConf) VALIDATORS_SITE = DEFAULT_VALIDATORS_SITE; RUN_STANDALONE = false; + START_UP = NORMAL; load(); } diff --git a/src/Config.h b/src/Config.h index 6fc42dca3..cc2b85ed4 100644 --- a/src/Config.h +++ b/src/Config.h @@ -56,6 +56,9 @@ public: std::vector IPS; // Peer IPs from newcoind.cfg. std::vector SNTP_SERVERS; // SNTP servers from newcoind.cfg. + enum StartUpType {FRESH,NORMAL,LOAD}; + StartUpType START_UP; + // Network parameters int NETWORK_START_TIME; // The Unix time we start ledger 0. int TRANSACTION_FEE_BASE; diff --git a/src/Ledger.cpp b/src/Ledger.cpp index 2f3c8cfdf..015a51c30 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -37,11 +37,11 @@ Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) : mTotCoins(s } Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, - uint64 totCoins, uint32 closeTime, uint32 parentCloseTime, int closeFlags, int closeResolution, uint32 ledgerSeq) + uint64 totCoins, uint32 closeTime, uint32 parentCloseTime, int closeFlags, int closeResolution, uint32 ledgerSeq,bool isMutable) : mParentHash(parentHash), mTransHash(transHash), mAccountHash(accountHash), mTotCoins(totCoins), mLedgerSeq(ledgerSeq), mCloseTime(closeTime), mParentCloseTime(parentCloseTime), mCloseResolution(closeResolution), mCloseFlags(closeFlags), - mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) + mClosed(false), mValidHash(false), mAccepted(false), mImmutable(isMutable) { updateHash(); } @@ -390,7 +390,7 @@ void Ledger::saveAcceptedLedger(Ledger::ref ledger) theApp->getOPs().pubLedger(ledger); } -Ledger::pointer Ledger::getSQL(const std::string& sql) +Ledger::pointer Ledger::getSQL(const std::string& sql,bool isMutable) { uint256 ledgerHash, prevHash, accountHash, transHash; uint64 totCoins; @@ -423,9 +423,8 @@ Ledger::pointer Ledger::getSQL(const std::string& sql) db->endIterRows(); } - Ledger::pointer ret = - boost::make_shared(prevHash, transHash, accountHash, totCoins, closingTime, prevClosingTime, - closeFlags, closeResolution, ledgerSeq); + Ledger::pointer ret =Ledger::pointer(new Ledger(prevHash, transHash, accountHash, totCoins, closingTime, prevClosingTime, + closeFlags, closeResolution, ledgerSeq,isMutable)); if (ret->getHash() != ledgerHash) { Json::StyledStreamWriter ssw; diff --git a/src/Ledger.h b/src/Ledger.h index d059f0158..e7a546649 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -84,7 +84,7 @@ private: protected: - static Ledger::pointer getSQL(const std::string& sqlStatement); + SLE::pointer getASNode(LedgerStateParms& parms, const uint256& nodeID, LedgerEntryType let); @@ -93,7 +93,7 @@ public: Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, uint64 totCoins, uint32 closeTime, uint32 parentCloseTime, int closeFlags, int closeResolution, - uint32 ledgerSeq); // used for database ledgers + uint32 ledgerSeq,bool immutable); // used for database ledgers Ledger(const std::vector& rawLedger); @@ -103,6 +103,8 @@ public: Ledger(Ledger& target, bool isMutable); // snapshot + static Ledger::pointer getSQL(const std::string& sqlStatement,bool immutable=false); + void updateHash(); void setClosed() { mClosed = true; } void setAccepted(uint32 closeTime, int closeResolution, bool correctCloseTime); diff --git a/src/LedgerMaster.cpp b/src/LedgerMaster.cpp index e0adb16ad..dd0f6ecca 100644 --- a/src/LedgerMaster.cpp +++ b/src/LedgerMaster.cpp @@ -21,7 +21,7 @@ bool LedgerMaster::addHeldTransaction(const Transaction::pointer& transaction) void LedgerMaster::pushLedger(Ledger::ref newLedger) { // Caller should already have properly assembled this ledger into "ready-to-close" form -- - // all candidate transactions must already be appled + // all candidate transactions must already be applied Log(lsINFO) << "PushLedger: " << newLedger->getHash(); ScopedLock sl(mLock); if (!!mFinalizedLedger) @@ -70,6 +70,8 @@ void LedgerMaster::storeLedger(Ledger::ref ledger) mLedgerHistory.addLedger(ledger); } + + Ledger::pointer LedgerMaster::closeLedger() { boost::recursive_mutex::scoped_lock sl(mLock); diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index 27e28d413..dcaca3800 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -424,7 +424,7 @@ void NetworkOPs::checkState(const boost::system::error_code& result) // If full or tracking, check only at wobble time! uint256 networkClosed; bool ledgerChange = checkLastClosedLedger(peerList, networkClosed); - assert(networkClosed.isNonZero()); + if(networkClosed.isZero())return; // WRITEME: Unless we are in omFULL and in the process of doing a consensus, // we must count how many nodes share our LCL, how many nodes disagree with our LCL, @@ -438,7 +438,7 @@ void NetworkOPs::checkState(const boost::system::error_code& result) setMode(omTRACKING); } - if ((mMode == omTRACKING) && !ledgerChange) + if ((mMode == omTRACKING) && !ledgerChange ) { // check if the ledger is good enough to go to omFULL // Note: Do not go to omFULL if we don't have the previous ledger @@ -470,6 +470,8 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis Log(lsTRACE) << "NetworkOPs::checkLastClosedLedger"; Ledger::pointer ourClosed = mLedgerMaster->getClosedLedger(); + if(!ourClosed) return(false); + uint256 closedLedger = ourClosed->getHash(); uint256 prevClosedLedger = ourClosed->getParentHash(); diff --git a/src/main.cpp b/src/main.cpp index 4a69f9f2d..63a781213 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,6 +98,8 @@ int main(int argc, char* argv[]) ("test,t", "Perform unit tests.") ("parameters", po::value< vector >(), "Specify comma separated parameters.") ("verbose,v", "Increase log level.") + ("load","Load the current ledger from the local DB.") + ("start","Start from a fresh Ledger.") ; // Interpret positional arguments as --parameters. @@ -154,6 +156,9 @@ int main(int argc, char* argv[]) } } + if(vm.count("start")) theConfig.START_UP=Config::FRESH; + else if(vm.count("load")) theConfig.START_UP=Config::LOAD; + if (iResult) { nothing();