24 namespace fs = boost::filesystem;
34 size_t const c_maxChunkUncomressedSize = 10 * 1024 * 1024;
37 std::string snappyUncompress(std::string
const& _compressed)
39 size_t uncompressedSize = 0;
40 if (!snappy::GetUncompressedLength(_compressed.data(), _compressed.size(), &uncompressedSize))
41 BOOST_THROW_EXCEPTION(FailedToGetUncompressedLength());
43 if (uncompressedSize > c_maxChunkUncomressedSize)
44 BOOST_THROW_EXCEPTION(ChunkIsTooBig());
46 std::string uncompressed;
47 if (!snappy::Uncompress(_compressed.data(), _compressed.size(), &uncompressed))
48 BOOST_THROW_EXCEPTION(FailedToUncompressedSnapshotChunk());
53 class SnapshotStorage:
public SnapshotStorageFace
56 explicit SnapshotStorage(fs::path
const& _snapshotDir) : m_snapshotDir(_snapshotDir) {}
58 bytes readManifest()
const override 61 if (manifestBytes.empty())
62 BOOST_THROW_EXCEPTION(FailedToReadSnapshotManifestFile());
67 std::string readCompressedChunk(
h256 const& _chunkHash)
const override 70 if (chunkCompressed.empty())
71 BOOST_THROW_EXCEPTION(FailedToReadChunkFile() <<
errinfo_hash256(_chunkHash));
73 return chunkCompressed;
76 std::string readChunk(
h256 const& _chunkHash)
const override 78 std::string
const chunkCompressed = readCompressedChunk(_chunkHash);
80 h256 const chunkHash =
sha3(chunkCompressed);
81 if (chunkHash != _chunkHash)
82 BOOST_THROW_EXCEPTION(ChunkDataCorrupted() <<
errinfo_hash256(_chunkHash));
84 std::string
const chunkUncompressed = snappyUncompress(chunkCompressed);
85 assert(!chunkUncompressed.empty());
87 return chunkUncompressed;
90 void copyTo(fs::path
const& _path)
const override {
copyDirectory(m_snapshotDir, _path); }
93 fs::path
const m_snapshotDir;
100 return std::unique_ptr<SnapshotStorageFace>(
new SnapshotStorage(_snapshotDirPath));
105 return _dataDir /
toHex(_genesisHash.
ref().
cropped(0, 4)) /
"snapshot";
std::unique_ptr< SnapshotStorageFace > createSnapshotStorage(fs::path const &_snapshotDirPath)
vector_ref< _T > cropped(size_t _begin, size_t _count) const
string contentsString(boost::filesystem::path const &_file)
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
bytes contents(boost::filesystem::path const &_file)
boost::error_info< struct tag_hash, h256 > errinfo_hash256
std::vector< byte > bytes
fs::path importedSnapshotPath(fs::path const &_dataDir, h256 const &_genesisHash)
void copyDirectory(boost::filesystem::path const &_srcDir, boost::filesystem::path const &_dstDir)
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)