      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

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity 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.

    // Parity 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 Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! Helpers for decoding and verifying responses for headers.

    use std::fmt;

    use ethcore::header::Header;
    use light::request::{HashOrNumber, Headers as HeadersRequest};
    use rlp::{DecoderError, UntrustedRlp, View};
    use util::{Bytes, H256};

    /// Errors found when decoding headers and verifying with basic constraints.
    #[derive(Debug, PartialEq)]
    pub enum BasicError {
    	/// Wrong skip value: expected, found (if any).
    	WrongSkip(u64, Option<u64>),
    	/// Wrong start number.
    	WrongStartNumber(u64, u64),
    	/// Wrong start hash.
    	WrongStartHash(H256, H256),
    	/// Too many headers.
    	TooManyHeaders(usize, usize),
    	/// Decoder error.
    	Decoder(DecoderError),
    }

    impl From<DecoderError> for BasicError {
    	fn from(err: DecoderError) -> Self {
    		BasicError::Decoder(err)
    	}
    }

    impl fmt::Display for BasicError {
    	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    		try!(write!(f, "Header response verification error: "));

    		match *self {
    			BasicError::WrongSkip(ref exp, ref got)
    				=> write!(f, "wrong skip (expected {}, got {:?})", exp, got),
    			BasicError::WrongStartNumber(ref exp, ref got)
    				=> write!(f, "wrong start number (expected {}, got {})", exp, got),
    			BasicError::WrongStartHash(ref exp, ref got)
    				=> write!(f, "wrong start hash (expected {}, got {})", exp, got),
    			BasicError::TooManyHeaders(ref max, ref got)
    				=> write!(f, "too many headers (max {}, got {})", max, got),
    			BasicError::Decoder(ref err)
    				=> write!(f, "{}", err),
    		}
    	}
    }

    /// Request verification constraint.
    pub trait Constraint {
    	type Error;

    	/// Verify headers against this.
    	fn verify(&self, headers: &[Header], reverse: bool) -> Result<(), Self::Error>;
    }

    /// Decode a response and do basic verification against a request.
    pub fn decode_and_verify(headers: &[Bytes], request: &HeadersRequest) -> Result<Vec<Header>, BasicError> {
    	let headers: Vec<_> = try!(headers.iter().map(|x| UntrustedRlp::new(&x).as_val()).collect());

    	let reverse = request.reverse;

    	try!(Max(request.max).verify(&headers, reverse));
    	match request.start {
    		HashOrNumber::Number(ref num) => try!(StartsAtNumber(*num).verify(&headers, reverse)),
    		HashOrNumber::Hash(ref hash) => try!(StartsAtHash(*hash).verify(&headers, reverse)),
    	}

    	try!(SkipsBetween(request.skip).verify(&headers, reverse));

    	Ok(headers)
    }

    struct StartsAtNumber(u64);
    struct StartsAtHash(H256);
    struct SkipsBetween(u64);
    struct Max(usize);

    impl Constraint for StartsAtNumber {
    	type Error = BasicError;

    	fn verify(&self, headers: &[Header], _reverse: bool) -> Result<(), BasicError> {
    		headers.first().map_or(Ok(()), |h| {
    			if h.number() == self.0 {
    				Ok(())
    			} else {
    				Err(BasicError::WrongStartNumber(self.0, h.number()))
    			}
    		})
    	}
    }

    impl Constraint for StartsAtHash {
    	type Error = BasicError;

    	fn verify(&self, headers: &[Header], _reverse: bool) -> Result<(), BasicError> {
    		headers.first().map_or(Ok(()), |h| {
    			if h.hash() == self.0 {
    				Ok(())
    			} else {
    				Err(BasicError::WrongStartHash(self.0, h.hash()))
    			}
    		})
    	}
    }

    impl Constraint for SkipsBetween {
    	type Error = BasicError;

    	fn verify(&self, headers: &[Header], reverse: bool) -> Result<(), BasicError> {
    		for pair in headers.windows(2) {
    			let (low, high) = if reverse { (&pair[1], &pair[0]) } else { (&pair[0], &pair[1]) };
    			if low.number() >= high.number() { return Err(BasicError::WrongSkip(self.0, None)) }

    			let skip = (high.number() - low.number()) - 1;
    			if skip != self.0 { return Err(BasicError::WrongSkip(self.0, Some(skip))) }
    		}

    		Ok(())
    	}
    }

    impl Constraint for Max {
    	type Error = BasicError;

    	fn verify(&self, headers: &[Header], _reverse: bool) -> Result<(), BasicError> {
    		match headers.len() > self.0 {
    			true => Err(BasicError::TooManyHeaders(self.0, headers.len())),
    			false => Ok(())
    		}
    	}
    }

    #[cfg(test)]
    mod tests {
    	use ethcore::header::Header;
    	use light::request::Headers as HeadersRequest;

    	use super::*;

    	#[test]
    	fn sequential_forward() {
    		let request = HeadersRequest {
    			start: 10.into(),
    			max: 30,
    			skip: 0,
    			reverse: false,
    		};

    		let mut parent_hash = None;
    		let headers: Vec<_> = (0..25).map(|x| x + 10).map(|x| {
    			let mut header = Header::default();
    			header.set_number(x);

    			if let Some(parent_hash) = parent_hash {
    				header.set_parent_hash(parent_hash);
    			}

    			parent_hash = Some(header.hash());

    			::rlp::encode(&header).to_vec()
    		}).collect();

    		assert!(decode_and_verify(&headers, &request).is_ok());
    	}

    	#[test]
    	fn sequential_backward() {
    		let request = HeadersRequest {
    			start: 34.into(),
    			max: 30,
    			skip: 0,
    			reverse: true,
    		};

    		let mut parent_hash = None;
    		let headers: Vec<_> = (0..25).map(|x| x + 10).rev().map(|x| {
    			let mut header = Header::default();
    			header.set_number(x);

    			if let Some(parent_hash) = parent_hash {
    				header.set_parent_hash(parent_hash);
    			}

    			parent_hash = Some(header.hash());

    			::rlp::encode(&header).to_vec()
    		}).collect();

    		assert!(decode_and_verify(&headers, &request).is_ok());
    	}

    	#[test]
    	fn too_many() {
    		let request = HeadersRequest {
    			start: 10.into(),
    			max: 20,
    			skip: 0,
    			reverse: false,
    		};

    		let mut parent_hash = None;
    		let headers: Vec<_> = (0..25).map(|x| x + 10).map(|x| {
    			let mut header = Header::default();
    			header.set_number(x);

    			if let Some(parent_hash) = parent_hash {
    				header.set_parent_hash(parent_hash);
    			}

    			parent_hash = Some(header.hash());

    			::rlp::encode(&header).to_vec()
    		}).collect();

    		assert_eq!(decode_and_verify(&headers, &request), Err(BasicError::TooManyHeaders(20, 25)));
    	}

    	#[test]
    	fn wrong_skip() {
    		let request = HeadersRequest {
    			start: 10.into(),
    			max: 30,
    			skip: 5,
    			reverse: false,
    		};

    		let headers: Vec<_> = (0..25).map(|x| x * 3).map(|x| x + 10).map(|x| {
    			let mut header = Header::default();
    			header.set_number(x);

    			::rlp::encode(&header).to_vec()
    		}).collect();

    		assert_eq!(decode_and_verify(&headers, &request), Err(BasicError::WrongSkip(5, Some(2))));
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)