Skip to content

Commit bf0011d

Browse files
committedDec 27, 2017
Don't create Warp capability if snapshot was not imported
1 parent 2d7eadd commit bf0011d

7 files changed

+55
-54
lines changed
 

‎libethereum/Client.cpp

+28-21
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
*/
2121

2222
#include "Client.h"
23-
#include <chrono>
24-
#include <memory>
25-
#include <thread>
26-
#include <boost/filesystem.hpp>
27-
#include <libdevcore/Log.h>
28-
#include <libp2p/Host.h>
23+
#include "Block.h"
2924
#include "Defaults.h"
30-
#include "Executive.h"
3125
#include "EthereumHost.h"
32-
#include "Block.h"
26+
#include "Executive.h"
27+
#include "SnapshotStorage.h"
3328
#include "TransactionQueue.h"
29+
#include <libdevcore/Log.h>
30+
#include <libp2p/Host.h>
31+
#include <boost/filesystem.hpp>
32+
#include <chrono>
33+
#include <memory>
34+
#include <thread>
35+
3436
using namespace std;
3537
using namespace dev;
3638
using namespace dev::eth;
@@ -87,19 +89,19 @@ Client::~Client()
8789

8890
void Client::init(p2p::Host* _extNet, fs::path const& _dbPath, WithExisting _forceAction, u256 _networkId)
8991
{
90-
DEV_TIMED_FUNCTION_ABOVE(500);
92+
DEV_TIMED_FUNCTION_ABOVE(500);
9193

92-
// Cannot be opened until after blockchain is open, since BlockChain may upgrade the database.
94+
// Cannot be opened until after blockchain is open, since BlockChain may upgrade the database.
9395
// TODO: consider returning the upgrade mechanism here. will delaying the opening of the blockchain database
9496
// until after the construction.
95-
m_stateDB = State::openDB(_dbPath, bc().genesisHash(), _forceAction);
97+
m_stateDB = State::openDB(_dbPath, bc().genesisHash(), _forceAction);
9698
// LAZY. TODO: move genesis state construction/commiting to stateDB openning and have this just take the root from the genesis block.
97-
m_preSeal = bc().genesisBlock(m_stateDB);
98-
m_postSeal = m_preSeal;
99+
m_preSeal = bc().genesisBlock(m_stateDB);
100+
m_postSeal = m_preSeal;
99101

100-
m_bq.setChain(bc());
102+
m_bq.setChain(bc());
101103

102-
m_lastGetWork = std::chrono::system_clock::now() - chrono::seconds(30);
104+
m_lastGetWork = std::chrono::system_clock::now() - chrono::seconds(30);
103105
m_tqReady = m_tq.onReady([=]() {
104106
this->onTransactionQueueReady();
105107
}); // TODO: should read m_tq->onReady(thisThread, syncTransactionQueue);
@@ -111,21 +113,26 @@ void Client::init(p2p::Host* _extNet, fs::path const& _dbPath, WithExisting _for
111113
bc().setOnBad([=](Exception& ex) { this->onBadBlock(ex); });
112114
bc().setOnBlockImport([=](BlockHeader const& _info) {
113115
if (auto h = m_host.lock())
114-
h->onBlockImported(_info);
116+
h->onBlockImported(_info);
115117
});
116118

117119
if (_forceAction == WithExisting::Rescue)
118-
bc().rescue(m_stateDB);
120+
bc().rescue(m_stateDB);
119121

120-
m_gp->update(bc());
122+
m_gp->update(bc());
121123

122124
auto host = _extNet->registerCapability(make_shared<EthereumHost>(bc(), m_stateDB, m_tq, m_bq, _networkId));
123-
m_host = host;
125+
m_host = host;
124126

125127
_extNet->addCapability(host, EthereumHost::staticName(), EthereumHost::c_oldProtocolVersion); //TODO: remove this once v61+ protocol is common
126128

127-
m_warpHost =
128-
_extNet->registerCapability(make_shared<WarpHostCapability>(bc(), _networkId, _dbPath));
129+
auto const snapshotPath = importedSnapshotPath(_dbPath, bc().genesisHash());
130+
if (fs::exists(snapshotPath))
131+
{
132+
std::shared_ptr<SnapshotStorageFace> snapshotStorage = createSnapshotStorage(snapshotPath);
133+
m_warpHost = _extNet->registerCapability(
134+
make_shared<WarpHostCapability>(bc(), _networkId, snapshotStorage));
135+
}
129136

130137
if (_dbPath.size())
131138
Defaults::setDBPath(_dbPath);

‎libethereum/SnapshotImporter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void SnapshotImporter::import(SnapshotStorageFace const& _snapshotStorage, h256
6868
importBlockChunks(_snapshotStorage, blockChunkHashes);
6969

7070
clog(SnapshotImportLog) << "Copying snapshot...";
71-
_snapshotStorage.copyTo(getDataDir() / toHex(_genesisHash.ref().cropped(0, 4)) / "snapshot");
71+
_snapshotStorage.copyTo(importedSnapshotPath(getDataDir(), _genesisHash));
7272
}
7373

7474
void SnapshotImporter::importStateChunks(SnapshotStorageFace const& _snapshotStorage, h256s const& _stateChunkHashes, h256 const& _stateRoot)

‎libethereum/SnapshotStorage.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <snappy.h>
2323

24+
namespace fs = boost::filesystem;
25+
2426
namespace dev
2527
{
2628
namespace eth
@@ -51,9 +53,7 @@ std::string snappyUncompress(std::string const& _compressed)
5153
class SnapshotStorage: public SnapshotStorageFace
5254
{
5355
public:
54-
explicit SnapshotStorage(boost::filesystem::path const& _snapshotDir)
55-
: m_snapshotDir(_snapshotDir)
56-
{}
56+
explicit SnapshotStorage(fs::path const& _snapshotDir) : m_snapshotDir(_snapshotDir) {}
5757

5858
bytes readManifest() const override
5959
{
@@ -87,22 +87,22 @@ class SnapshotStorage: public SnapshotStorageFace
8787
return chunkUncompressed;
8888
}
8989

90-
void copyTo(boost::filesystem::path const& _path) const override
91-
{
92-
copyDirectory(m_snapshotDir, _path);
93-
}
90+
void copyTo(fs::path const& _path) const override { copyDirectory(m_snapshotDir, _path); }
9491

9592
private:
96-
boost::filesystem::path const m_snapshotDir;
93+
fs::path const m_snapshotDir;
9794
};
9895

9996
}
10097

101-
std::unique_ptr<SnapshotStorageFace> createSnapshotStorage(
102-
boost::filesystem::path const& _snapshotDirPath)
98+
std::unique_ptr<SnapshotStorageFace> createSnapshotStorage(fs::path const& _snapshotDirPath)
10399
{
104100
return std::unique_ptr<SnapshotStorageFace>(new SnapshotStorage(_snapshotDirPath));
105101
}
106102

103+
fs::path importedSnapshotPath(fs::path const& _dataDir, h256 const& _genesisHash)
104+
{
105+
return _dataDir / toHex(_genesisHash.ref().cropped(0, 4)) / "snapshot";
106+
}
107107
}
108108
}

‎libethereum/SnapshotStorage.h

+3
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@ class SnapshotStorageFace
5555

5656
std::unique_ptr<SnapshotStorageFace> createSnapshotStorage(
5757
boost::filesystem::path const& _snapshotDirPath);
58+
59+
boost::filesystem::path importedSnapshotPath(
60+
boost::filesystem::path const& _dataDir, h256 const& _genesisHash);
5861
}
5962
}

‎libethereum/WarpHostCapability.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ namespace dev
2424
namespace eth
2525
{
2626
WarpHostCapability::WarpHostCapability(BlockChain const& _blockChain, u256 const& _networkId,
27-
boost::filesystem::path const& _dataDirPath)
28-
: m_blockChain(_blockChain), m_networkId(_networkId)
27+
std::shared_ptr<SnapshotStorageFace> _snapshotStorage)
28+
: m_blockChain(_blockChain), m_networkId(_networkId), m_snapshot(_snapshotStorage)
2929
{
30-
boost::filesystem::path const snapshotPath =
31-
_dataDirPath / toHex(m_blockChain.genesisHash().ref().cropped(0, 4)) / "snapshot";
32-
if (boost::filesystem::exists(snapshotPath))
33-
m_snapshot = createSnapshotStorage(snapshotPath);
3430
}
3531

3632
std::shared_ptr<p2p::Capability> WarpHostCapability::newPeerCapability(

‎libethereum/WarpHostCapability.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class WarpHostCapability : public p2p::HostCapability<WarpPeerCapability>
2929
{
3030
public:
3131
WarpHostCapability(BlockChain const& _blockChain, u256 const& _networkId,
32-
boost::filesystem::path const& _dataDirPath);
32+
std::shared_ptr<SnapshotStorageFace> _snapshotStorage);
3333

3434
protected:
3535
std::shared_ptr<p2p::Capability> newPeerCapability(std::shared_ptr<p2p::SessionFace> const& _s,

‎libethereum/WarpPeerCapability.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,24 @@ void WarpPeerCapability::init(unsigned _hostProtocolVersion, u256 _hostNetworkId
3333
u256 _chainTotalDifficulty, h256 _chainCurrentHash, h256 _chainGenesisHash,
3434
std::shared_ptr<SnapshotStorageFace const> _snapshot)
3535
{
36-
m_snapshot = _snapshot;
36+
assert(_snapshot);
37+
m_snapshot = std::move(_snapshot);
3738

38-
h256 snapshotBlockHash;
39-
u256 snapshotBlockNumber;
40-
if (m_snapshot)
41-
{
42-
bytes const snapshotManifest(m_snapshot->readManifest());
43-
RLP manifest(snapshotManifest);
44-
if (manifest.itemCount() != 6)
45-
BOOST_THROW_EXCEPTION(InvalidSnapshotManifest());
46-
snapshotBlockNumber = manifest[4].toInt<u256>(RLP::VeryStrict);
47-
snapshotBlockHash = manifest[5].toHash<h256>(RLP::VeryStrict);
48-
}
39+
bytes const snapshotManifest(m_snapshot->readManifest());
40+
RLP manifest(snapshotManifest);
41+
if (manifest.itemCount() != 6)
42+
BOOST_THROW_EXCEPTION(InvalidSnapshotManifest());
43+
u256 const snapshotBlockNumber = manifest[4].toInt<u256>(RLP::VeryStrict);
44+
h256 const snapshotBlockHash = manifest[5].toHash<h256>(RLP::VeryStrict);
4945

5046
requestStatus(_hostProtocolVersion, _hostNetworkId, _chainTotalDifficulty, _chainCurrentHash,
5147
_chainGenesisHash, snapshotBlockHash, snapshotBlockNumber);
5248
}
5349

5450
bool WarpPeerCapability::interpret(unsigned _id, RLP const& _r)
5551
{
56-
if (!m_snapshot)
57-
return false;
58-
52+
assert(m_snapshot);
53+
5954
try
6055
{
6156
switch (_id)

0 commit comments

Comments
 (0)
Please sign in to comment.