Browse Source

Remove Database when protocol version changes.

cl-refactor
Gav Wood 11 years ago
parent
commit
c1c02c461d
  1. 30
      libethereum/Client.cpp
  2. 15
      libethereum/Client.h
  3. 1
      libethereum/Defaults.h
  4. 10
      libethereum/PeerNetwork.cpp
  5. 3
      libethereum/PeerNetwork.h
  6. 2
      libethereum/State.cpp

30
libethereum/Client.cpp

@ -23,20 +23,44 @@
#include <chrono>
#include <thread>
#include <boost/filesystem.hpp>
#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<unsigned>(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.

15
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.

1
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;

10
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())

3
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:

2
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;

Loading…
Cancel
Save