Ethereum  PoC-8
The C++ Implementation of Ethereum
SnapshotStorage.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "SnapshotStorage.h"
19 #include <libdevcore/CommonIO.h>
20 #include <libdevcore/SHA3.h>
21 
22 #include <snappy.h>
23 
24 namespace fs = boost::filesystem;
25 
26 namespace dev
27 {
28 namespace eth
29 {
30 
31 namespace
32 {
33 
34 size_t const c_maxChunkUncomressedSize = 10 * 1024 * 1024;
35 
36 
37 std::string snappyUncompress(std::string const& _compressed)
38 {
39  size_t uncompressedSize = 0;
40  if (!snappy::GetUncompressedLength(_compressed.data(), _compressed.size(), &uncompressedSize))
41  BOOST_THROW_EXCEPTION(FailedToGetUncompressedLength());
42 
43  if (uncompressedSize > c_maxChunkUncomressedSize)
44  BOOST_THROW_EXCEPTION(ChunkIsTooBig());
45 
46  std::string uncompressed;
47  if (!snappy::Uncompress(_compressed.data(), _compressed.size(), &uncompressed))
48  BOOST_THROW_EXCEPTION(FailedToUncompressedSnapshotChunk());
49 
50  return uncompressed;
51 }
52 
53 class SnapshotStorage: public SnapshotStorageFace
54 {
55 public:
56  explicit SnapshotStorage(fs::path const& _snapshotDir) : m_snapshotDir(_snapshotDir) {}
57 
58  bytes readManifest() const override
59  {
60  bytes const manifestBytes = dev::contents((m_snapshotDir / "MANIFEST").string());
61  if (manifestBytes.empty())
62  BOOST_THROW_EXCEPTION(FailedToReadSnapshotManifestFile());
63 
64  return manifestBytes;
65  }
66 
67  std::string readCompressedChunk(h256 const& _chunkHash) const override
68  {
69  std::string const chunkCompressed = dev::contentsString((m_snapshotDir / toHex(_chunkHash)).string());
70  if (chunkCompressed.empty())
71  BOOST_THROW_EXCEPTION(FailedToReadChunkFile() << errinfo_hash256(_chunkHash));
72 
73  return chunkCompressed;
74  }
75 
76  std::string readChunk(h256 const& _chunkHash) const override
77  {
78  std::string const chunkCompressed = readCompressedChunk(_chunkHash);
79 
80  h256 const chunkHash = sha3(chunkCompressed);
81  if (chunkHash != _chunkHash)
82  BOOST_THROW_EXCEPTION(ChunkDataCorrupted() << errinfo_hash256(_chunkHash));
83 
84  std::string const chunkUncompressed = snappyUncompress(chunkCompressed);
85  assert(!chunkUncompressed.empty());
86 
87  return chunkUncompressed;
88  }
89 
90  void copyTo(fs::path const& _path) const override { copyDirectory(m_snapshotDir, _path); }
91 
92 private:
93  fs::path const m_snapshotDir;
94 };
95 
96 }
97 
98 std::unique_ptr<SnapshotStorageFace> createSnapshotStorage(fs::path const& _snapshotDirPath)
99 {
100  return std::unique_ptr<SnapshotStorageFace>(new SnapshotStorage(_snapshotDirPath));
101 }
102 
103 fs::path importedSnapshotPath(fs::path const& _dataDir, h256 const& _genesisHash)
104 {
105  return _dataDir / toHex(_genesisHash.ref().cropped(0, 4)) / "snapshot";
106 }
107 }
108 }
dev::contents
bytes contents(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:107
SnapshotStorage.h
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
dev::FixedHash::ref
bytesRef ref()
Definition: FixedHash.h:132
dev::errinfo_hash256
boost::error_info< struct tag_hash, h256 > errinfo_hash256
Definition: Exceptions.h:89
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::copyDirectory
void copyDirectory(boost::filesystem::path const &_srcDir, boost::filesystem::path const &_dstDir)
Definition: CommonIO.cpp:146
dev::FixedHash< 32 >
dev::contentsString
string contentsString(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:120
CommonIO.h
dev::vector_ref::cropped
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:60
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
SHA3.h
dev::eth::createSnapshotStorage
std::unique_ptr< SnapshotStorageFace > createSnapshotStorage(fs::path const &_snapshotDirPath)
Definition: SnapshotStorage.cpp:98
dev
Definition: Address.cpp:21
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
dev::eth::importedSnapshotPath
fs::path importedSnapshotPath(fs::path const &_dataDir, h256 const &_genesisHash)
Definition: SnapshotStorage.cpp:103