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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory 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.

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

//! ResponseGuard implementation.
//! It is responsible for the receiving end of `Pending Request` (see `OnDemand` module docs for more information)
//! The major functionality is the following:
//!    1) Register non-successful responses which will reported back if it fails
//!    2) A timeout mechanism that will wait for successful response at most t seconds

use std::time::{Duration, Instant};
use std::collections::HashMap;
use std::fmt;

use super::{ResponseError, ValidityError};

/// Response guard error type
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
	/// No majority, the error reason can't be determined
	NoMajority(usize),
	/// Majority, with the error reason
	Majority(Inner, usize, usize),
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Error::Majority(err, majority, total) => {
				write!(f, "Error cause was {:?}, (majority count: {} / total: {})",
					err, majority, total)
			}
			Error::NoMajority(total) => {
				write!(f, "Error cause couldn't be determined, the total number of responses was {}", total)
			}
		}
	}
}

/// Dummy type to convert a generic type with no trait bounds
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub enum Inner {
	/// Bad execution proof
	BadProof,
	/// RLP decoding
	Decoder,
	/// Empty response
	EmptyResponse,
	/// Wrong header sequence
	HeaderByNumber,
	/// Too few results
	TooFewResults,
	/// Too many results
	TooManyResults,
	/// Trie error
	Trie,
	/// Unresolved header
	UnresolvedHeader,
	/// No responses expected.
	Unexpected,
	/// Wrong hash
	WrongHash,
	/// Wrong Header sequence
	WrongHeaderSequence,
	/// Wrong response kind
	WrongKind,
	/// Wrong number
	WrongNumber,
	/// Wrong Trie Root
	WrongTrieRoot,
}

/// Handle and register responses that can fail
#[derive(Debug)]
pub struct ResponseGuard {
	request_start: Instant,
	time_to_live: Duration,
	responses: HashMap<Inner, usize>,
	number_responses: usize,
}

impl ResponseGuard {
	/// Constructor
	pub fn new(time_to_live: Duration) -> Self {
		Self {
			request_start: Instant::now(),
			time_to_live,
			responses: HashMap::new(),
			number_responses: 0,
		}
	}

	fn into_reason(&self, err: &ResponseError<super::request::Error>) -> Inner {
		match err {
			ResponseError::Unexpected => Inner::Unexpected,
			ResponseError::Validity(ValidityError::BadProof) => Inner::BadProof,
			ResponseError::Validity(ValidityError::Decoder(_)) => Inner::Decoder,
			ResponseError::Validity(ValidityError::Empty) => Inner::EmptyResponse,
			ResponseError::Validity(ValidityError::HeaderByNumber) => Inner::HeaderByNumber,
			ResponseError::Validity(ValidityError::TooFewResults(_, _)) => Inner::TooFewResults,
			ResponseError::Validity(ValidityError::TooManyResults(_, _)) => Inner::TooManyResults,
			ResponseError::Validity(ValidityError::Trie(_)) => Inner::Trie,
			ResponseError::Validity(ValidityError::UnresolvedHeader(_)) => Inner::UnresolvedHeader,
			ResponseError::Validity(ValidityError::WrongHash(_, _)) => Inner::WrongHash,
			ResponseError::Validity(ValidityError::WrongHeaderSequence) => Inner::WrongHeaderSequence,
			ResponseError::Validity(ValidityError::WrongKind) => Inner::WrongKind,
			ResponseError::Validity(ValidityError::WrongNumber(_, _)) => Inner::WrongNumber,
			ResponseError::Validity(ValidityError::WrongTrieRoot(_, _)) => Inner::WrongTrieRoot,
		}
	}

	/// Update the state after a `faulty` call
	pub fn register_error(&mut self, err: &ResponseError<super::request::Error>) -> Result<(), Error> {
		let err = self.into_reason(err);
		*self.responses.entry(err).or_insert(0) += 1;
		self.number_responses = self.number_responses.saturating_add(1);
		trace!(target: "circuit_breaker", "ResponseGuard: {:?}", self.responses);
		// The request has exceeded its timeout
		if self.request_start.elapsed() >= self.time_to_live {
			let (&err, &max_count) = self.responses.iter().max_by_key(|(_k, v)| *v).expect("got at least one element; qed");
			let majority = self.responses.values().filter(|v| **v == max_count).count() == 1;
			if majority {
				Err(Error::Majority(err, max_count, self.number_responses))
			} else {
				Err(Error::NoMajority(self.number_responses))
			}
		} else {
			Ok(())
		}
	}
}

#[cfg(test)]
mod tests {
	use std::thread;
	use super::*;

	#[test]
	fn test_basic_by_majority() {
		let mut guard = ResponseGuard::new(Duration::from_secs(5));
		guard.register_error(&ResponseError::Validity(ValidityError::Empty)).unwrap();
		guard.register_error(&ResponseError::Unexpected).unwrap();
		guard.register_error(&ResponseError::Unexpected).unwrap();
		guard.register_error(&ResponseError::Unexpected).unwrap();
		thread::sleep(Duration::from_secs(5));

		assert_eq!(guard.register_error(&ResponseError::Validity(ValidityError::WrongKind)), Err(Error::Majority(Inner::Unexpected, 3, 5)));
	}

	#[test]
	fn test_no_majority() {
		let mut guard = ResponseGuard::new(Duration::from_secs(5));
		guard.register_error(&ResponseError::Validity(ValidityError::Empty)).unwrap();
		guard.register_error(&ResponseError::Validity(ValidityError::Empty)).unwrap();
		guard.register_error(&ResponseError::Unexpected).unwrap();
		guard.register_error(&ResponseError::Unexpected).unwrap();
		thread::sleep(Duration::from_secs(5));

		assert_eq!(guard.register_error(&ResponseError::Validity(ValidityError::WrongKind)), Err(Error::NoMajority(5)));
	}
}