forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorrent.h
169 lines (134 loc) · 4.95 KB
/
Torrent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "MerkleTree.h"
#include "TorrentMeta.h"
#include "td/utils/buffer.h"
#include "td/db/utils/BlobView.h"
#include <map>
namespace ton {
class Torrent {
public:
class Creator;
friend class Creator;
using Info = TorrentInfo;
struct Options {
std::string root_dir;
bool in_memory{false};
bool validate{false};
};
// creation
static td::Result<Torrent> open(Options options, TorrentMeta meta);
static td::Result<Torrent> open(Options options, td::Slice meta_str);
void validate();
std::string get_stats_str() const;
const Info &get_info() const;
// get piece and proof
td::Result<std::string> get_piece_data(td::uint64 piece_i);
td::Result<td::Ref<vm::Cell>> get_piece_proof(td::uint64 piece_i);
// add piece (with an optional proof)
td::Status add_piece(td::uint64 piece_i, td::Slice data, td::Ref<vm::Cell> proof);
//TODO: add multiple chunks? Merkle tree supports much more general interface
bool is_completed() const;
// Checks that file is ready and returns its content.
// Intened mostly for in-memory usage and for tests
td::Result<td::BufferSlice> read_file(td::Slice name);
struct GetMetaOptions {
GetMetaOptions();
size_t proof_depth_limit{std::numeric_limits<size_t>::max()};
bool with_header{true};
bool with_proof{true};
GetMetaOptions &without_header() {
with_header = false;
return *this;
}
GetMetaOptions &without_proof() {
with_proof = false;
return *this;
}
GetMetaOptions &with_proof_depth_limit(size_t limit) {
proof_depth_limit = limit;
return *this;
}
};
std::string get_meta_str(const GetMetaOptions &options = {}) const;
TorrentMeta get_meta(const GetMetaOptions &options = {}) const;
// Some api for inspection of a current state
bool is_piece_ready(td::uint64 piece_i) const;
td::optional<size_t> get_files_count() const;
td::CSlice get_file_name(size_t i) const;
td::uint64 get_file_size(size_t i) const;
td::uint64 get_file_ready_size(size_t i) const;
struct PartsRange {
td::uint64 begin{0};
td::uint64 end{0};
};
PartsRange get_file_parts_range(size_t i);
PartsRange get_header_parts_range();
size_t get_ready_parts_count() const;
std::vector<size_t> chunks_by_piece(td::uint64 piece_id);
private:
Info info_;
td::optional<std::string> root_dir_;
// While header is not completly available all pieces are stored in memory
td::BufferSlice header_str_;
td::optional<TorrentHeader> header_;
size_t not_ready_pending_piece_count_{0};
size_t header_pieces_count_{0};
std::map<td::uint64, td::string> pending_pieces_;
ton::MerkleTree merkle_tree_;
std::vector<bool> piece_is_ready_;
size_t not_ready_piece_count_{0};
size_t ready_parts_count_{0};
struct ChunkState {
std::string name;
td::uint64 offset{0};
td::uint64 size{0};
td::uint64 ready_size{0};
td::BlobView data;
struct Cache {
td::uint64 offset{0};
td::uint64 size{0};
td::BufferSlice slice;
};
bool is_ready() const {
return ready_size == size;
}
TD_WARN_UNUSED_RESULT td::Status add_piece(td::Slice piece, td::uint64 offset) {
TRY_RESULT(written, data.write(piece, offset));
CHECK(written == piece.size());
ready_size += written;
return td::Status::OK();
}
bool has_piece(td::uint64 offset, td::uint64 size) {
return data.size() >= offset + size;
}
TD_WARN_UNUSED_RESULT td::Status get_piece(td::MutableSlice dest, td::uint64 offset, Cache *cache = nullptr);
};
std::vector<ChunkState> chunks_;
explicit Torrent(Info info, td::optional<TorrentHeader> header, ton::MerkleTree tree, std::vector<ChunkState> chunk);
explicit Torrent(TorrentMeta meta);
void set_root_dir(std::string root_dir) {
root_dir_ = std::move(root_dir);
}
std::string get_chunk_path(td::Slice name);
td::Status init_chunk_data(ChunkState &chunk);
template <class F>
td::Status iterate_piece(Info::PieceInfo piece, F &&f);
td::Status add_header_piece(td::uint64 piece_i, td::Slice data);
td::Status add_validated_piece(td::uint64 piece_i, td::Slice data);
void set_header(const TorrentHeader &header);
};
} // namespace ton