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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum 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 General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

use ethcore::snapshot::{ManifestData, SnapshotService};
use ethereum_types::H256;
use hash::keccak;

use std::{collections::HashSet, iter::FromIterator};

#[derive(PartialEq, Eq, Debug)]
pub enum ChunkType {
    State(H256),
    Block(H256),
}

pub struct Snapshot {
    pending_state_chunks: Vec<H256>,
    pending_block_chunks: Vec<H256>,
    downloading_chunks: HashSet<H256>,
    completed_chunks: HashSet<H256>,
    snapshot_hash: Option<H256>,
    bad_hashes: HashSet<H256>,
    initialized: bool,
}

impl Snapshot {
    /// Create a new instance.
    pub fn new() -> Snapshot {
        Snapshot {
            pending_state_chunks: Vec::new(),
            pending_block_chunks: Vec::new(),
            downloading_chunks: HashSet::new(),
            completed_chunks: HashSet::new(),
            snapshot_hash: None,
            bad_hashes: HashSet::new(),
            initialized: false,
        }
    }

    /// Sync the Snapshot completed chunks with the Snapshot Service
    pub fn initialize(&mut self, snapshot_service: &dyn SnapshotService) {
        if self.initialized {
            return;
        }

        if let Some(completed_chunks) = snapshot_service.completed_chunks() {
            self.completed_chunks = HashSet::from_iter(completed_chunks);
        }

        trace!(
            target: "snapshot",
            "Snapshot is now initialized with {} completed chunks.",
            self.completed_chunks.len(),
        );

        self.initialized = true;
    }

    /// Clear everything.
    pub fn clear(&mut self) {
        self.pending_state_chunks.clear();
        self.pending_block_chunks.clear();
        self.downloading_chunks.clear();
        self.completed_chunks.clear();
        self.snapshot_hash = None;
        self.initialized = false;
    }

    /// Check if currently downloading a snapshot.
    pub fn have_manifest(&self) -> bool {
        self.snapshot_hash.is_some()
    }

    /// Reset collection for a manifest RLP
    pub fn reset_to(&mut self, manifest: &ManifestData, hash: &H256) {
        self.clear();
        self.pending_state_chunks = manifest.state_hashes.clone();
        self.pending_block_chunks = manifest.block_hashes.clone();
        self.snapshot_hash = Some(hash.clone());
    }

    /// Validate chunk and mark it as downloaded
    pub fn validate_chunk(&mut self, chunk: &[u8]) -> Result<ChunkType, ()> {
        let hash = keccak(chunk);
        if self.completed_chunks.contains(&hash) {
            trace!(target: "sync", "Ignored proccessed chunk: {:x}", hash);
            return Err(());
        }
        self.downloading_chunks.remove(&hash);
        if self.pending_block_chunks.iter().any(|h| h == &hash) {
            self.completed_chunks.insert(hash.clone());
            return Ok(ChunkType::Block(hash));
        }
        if self.pending_state_chunks.iter().any(|h| h == &hash) {
            self.completed_chunks.insert(hash.clone());
            return Ok(ChunkType::State(hash));
        }
        trace!(target: "sync", "Ignored unknown chunk: {:x}", hash);
        Err(())
    }

    /// Find a chunk to download
    pub fn needed_chunk(&mut self) -> Option<H256> {
        // Find next needed chunk: first block, then state chunks
        let chunk = {
            let chunk_filter =
                |h| !self.downloading_chunks.contains(h) && !self.completed_chunks.contains(h);

            let needed_block_chunk = self
                .pending_block_chunks
                .iter()
                .filter(|&h| chunk_filter(h))
                .map(|h| *h)
                .next();

            // If no block chunks to download, get the state chunks
            if needed_block_chunk.is_none() {
                self.pending_state_chunks
                    .iter()
                    .filter(|&h| chunk_filter(h))
                    .map(|h| *h)
                    .next()
            } else {
                needed_block_chunk
            }
        };

        if let Some(hash) = chunk {
            self.downloading_chunks.insert(hash.clone());
        }
        chunk
    }

    pub fn clear_chunk_download(&mut self, hash: &H256) {
        self.downloading_chunks.remove(hash);
    }

    // note snapshot hash as bad.
    pub fn note_bad(&mut self, hash: H256) {
        self.bad_hashes.insert(hash);
    }

    // whether snapshot hash is known to be bad.
    pub fn is_known_bad(&self, hash: &H256) -> bool {
        self.bad_hashes.contains(hash)
    }

    pub fn snapshot_hash(&self) -> Option<H256> {
        self.snapshot_hash
    }

    pub fn total_chunks(&self) -> usize {
        self.pending_block_chunks.len() + self.pending_state_chunks.len()
    }

    pub fn done_chunks(&self) -> usize {
        self.completed_chunks.len()
    }

    pub fn is_complete(&self) -> bool {
        self.total_chunks() == self.completed_chunks.len()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use bytes::Bytes;
    use ethcore::snapshot::ManifestData;
    use hash::keccak;

    fn is_empty(snapshot: &Snapshot) -> bool {
        snapshot.pending_block_chunks.is_empty()
            && snapshot.pending_state_chunks.is_empty()
            && snapshot.completed_chunks.is_empty()
            && snapshot.downloading_chunks.is_empty()
            && snapshot.snapshot_hash.is_none()
    }

    fn test_manifest() -> (ManifestData, H256, Vec<Bytes>, Vec<Bytes>) {
        let state_chunks: Vec<Bytes> = (0..20)
            .map(|_| H256::random().as_bytes().to_vec())
            .collect();
        let block_chunks: Vec<Bytes> = (0..20)
            .map(|_| H256::random().as_bytes().to_vec())
            .collect();
        let manifest = ManifestData {
            version: 2,
            state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(),
            block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(),
            state_root: H256::default(),
            block_number: 42,
            block_hash: H256::default(),
        };
        let mhash = keccak(manifest.clone().into_rlp());
        (manifest, mhash, state_chunks, block_chunks)
    }

    #[test]
    fn create_clear() {
        let mut snapshot = Snapshot::new();
        assert!(is_empty(&snapshot));
        let (manifest, mhash, _, _) = test_manifest();
        snapshot.reset_to(&manifest, &mhash);
        assert!(!is_empty(&snapshot));
        snapshot.clear();
        assert!(is_empty(&snapshot));
    }

    #[test]
    fn validate_chunks() {
        let mut snapshot = Snapshot::new();
        let (manifest, mhash, state_chunks, block_chunks) = test_manifest();
        snapshot.reset_to(&manifest, &mhash);
        assert_eq!(snapshot.done_chunks(), 0);
        assert!(snapshot
            .validate_chunk(&H256::random().as_bytes().to_vec())
            .is_err());

        let requested: Vec<H256> = (0..40).map(|_| snapshot.needed_chunk().unwrap()).collect();
        assert!(snapshot.needed_chunk().is_none());

        let requested_all_block_chunks = manifest
            .block_hashes
            .iter()
            .all(|h| requested.iter().any(|rh| rh == h));
        assert!(requested_all_block_chunks);

        let requested_all_state_chunks = manifest
            .state_hashes
            .iter()
            .all(|h| requested.iter().any(|rh| rh == h));
        assert!(requested_all_state_chunks);

        assert_eq!(snapshot.downloading_chunks.len(), 40);

        assert_eq!(
            snapshot.validate_chunk(&state_chunks[4]),
            Ok(ChunkType::State(manifest.state_hashes[4].clone()))
        );
        assert_eq!(snapshot.completed_chunks.len(), 1);
        assert_eq!(snapshot.downloading_chunks.len(), 39);

        assert_eq!(
            snapshot.validate_chunk(&block_chunks[10]),
            Ok(ChunkType::Block(manifest.block_hashes[10].clone()))
        );
        assert_eq!(snapshot.completed_chunks.len(), 2);
        assert_eq!(snapshot.downloading_chunks.len(), 38);

        for (i, data) in state_chunks.iter().enumerate() {
            if i != 4 {
                assert!(snapshot.validate_chunk(data).is_ok());
            }
        }

        for (i, data) in block_chunks.iter().enumerate() {
            if i != 10 {
                assert!(snapshot.validate_chunk(data).is_ok());
            }
        }

        assert!(snapshot.is_complete());
        assert_eq!(snapshot.done_chunks(), 40);
        assert_eq!(snapshot.done_chunks(), snapshot.total_chunks());
        assert_eq!(snapshot.snapshot_hash(), Some(keccak(manifest.into_rlp())));
    }

    #[test]
    fn tracks_known_bad() {
        let mut snapshot = Snapshot::new();
        let hash = H256::random();

        assert_eq!(snapshot.is_known_bad(&hash), false);
        snapshot.note_bad(hash);
        assert_eq!(snapshot.is_known_bad(&hash), true);
    }
}