From c1c02c461d3912da502d05c338b8f8b6502dfc6f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 3 Mar 2014 18:54:31 +0000 Subject: [PATCH] Remove Database when protocol version changes. --- libethereum/Client.cpp | 30 +++++++++++++++++++++++++++--- libethereum/Client.h | 15 +++++++++++++++ libethereum/Defaults.h | 1 + libethereum/PeerNetwork.cpp | 10 ++++++++++ libethereum/PeerNetwork.h | 3 +++ libethereum/State.cpp | 2 -- 6 files changed, 56 insertions(+), 5 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index c30de604f..1752c2c0e 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -23,20 +23,44 @@ #include #include +#include #include "Common.h" #include "Defaults.h" using namespace std; using namespace eth; +VersionChecker::VersionChecker(string const& _dbPath, unsigned _protocolVersion): + m_path(_dbPath.size() ? _dbPath : Defaults::dbPath()), + m_protocolVersion(_protocolVersion) +{ + m_ok = RLP(contents(m_path + "/protocol")).toInt(RLP::LaisezFaire) == _protocolVersion; +} + +void VersionChecker::setOk() +{ + if (!m_ok) + { + try + { + boost::filesystem::create_directory(m_path); + } + catch (...) {} + writeFile(m_path + "/protocol", rlp(m_protocolVersion)); + } +} + Client::Client(std::string const& _clientVersion, Address _us, std::string const& _dbPath): m_clientVersion(_clientVersion), - m_bc(_dbPath), - m_stateDB(State::openDB(_dbPath)), + m_vc(_dbPath, PeerSession::protocolVersion()), + m_bc(_dbPath, !m_vc.ok()), + m_stateDB(State::openDB(_dbPath, !m_vc.ok())), m_preMine(_us, m_stateDB), m_postMine(_us, m_stateDB), m_workState(Active) { - Defaults::setDBPath(_dbPath); + if (_dbPath.size()) + Defaults::setDBPath(_dbPath); + m_vc.setOk(); // Synchronise the state according to the head of the block chain. // TODO: currently it contains keys for *all* blocks. Make it remove old ones. diff --git a/libethereum/Client.h b/libethereum/Client.h index bf1964fe0..5bcfe10fa 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -60,6 +60,20 @@ enum ClientWorkState Deleted }; +class VersionChecker +{ +public: + VersionChecker(std::string const& _dbPath, unsigned _protocolVersion); + + void setOk(); + bool ok() const { return m_ok; } + +private: + bool m_ok; + std::string m_path; + unsigned m_protocolVersion; +}; + class Client { public: @@ -141,6 +155,7 @@ private: void work(); std::string m_clientVersion; ///< Our end-application client's name/version. + VersionChecker m_vc; ///< Dummy object to check & update the protocol version. BlockChain m_bc; ///< Maintains block database. TransactionQueue m_tq; ///< Maintains list of incoming transactions not yet on the block chain. Overlay m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it. diff --git a/libethereum/Defaults.h b/libethereum/Defaults.h index ce0c27a42..0d8e84515 100644 --- a/libethereum/Defaults.h +++ b/libethereum/Defaults.h @@ -36,6 +36,7 @@ public: static Defaults* get() { if (!s_this) s_this = new Defaults; return s_this; } static void setDBPath(std::string const& _dbPath) { get()->m_dbPath = _dbPath; } + static std::string const& dbPath() { return get()->m_dbPath; } private: std::string m_dbPath; diff --git a/libethereum/PeerNetwork.cpp b/libethereum/PeerNetwork.cpp index 713441de4..a4e3c6317 100644 --- a/libethereum/PeerNetwork.cpp +++ b/libethereum/PeerNetwork.cpp @@ -97,6 +97,16 @@ PeerSession::~PeerSession() m_socket.close(); } +int PeerSession::protocolVersion() +{ + return c_protocolVersion; +} + +int PeerSession::networkId() +{ + return 0; +} + bi::tcp::endpoint PeerSession::endpoint() const { if (m_socket.is_open()) diff --git a/libethereum/PeerNetwork.h b/libethereum/PeerNetwork.h index 80c6b054c..aa5f42da7 100644 --- a/libethereum/PeerNetwork.h +++ b/libethereum/PeerNetwork.h @@ -101,6 +101,9 @@ public: bool isOpen() const { return m_socket.is_open(); } + static int protocolVersion(); + static int networkId(); + bi::tcp::endpoint endpoint() const; ///< for other peers to connect to. private: diff --git a/libethereum/State.cpp b/libethereum/State.cpp index f14c8c18c..c06453c6f 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -55,8 +55,6 @@ Overlay State::openDB(std::string _path, bool _killExisting) if (_path.empty()) _path = Defaults::get()->m_dbPath; boost::filesystem::create_directory(_path); - if (_killExisting) - boost::filesystem::remove_all(_path + "/state"); ldb::Options o; o.create_if_missing = true;