referrerpolicy=no-referrer-when-downgrade
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
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

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

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

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

use gum::CandidateHash;
use polkadot_node_network_protocol::{
	request_response::{incoming::OutgoingResponseSender, v1::DisputeRequest},
	PeerId,
};
use polkadot_node_primitives::SignedDisputeStatement;
use polkadot_primitives::{vstaging::CandidateReceiptV2 as CandidateReceipt, ValidatorIndex};

use crate::receiver::{BATCH_COLLECTING_INTERVAL, MIN_KEEP_BATCH_ALIVE_VOTES};

use super::MAX_BATCH_LIFETIME;

/// A batch of votes to be imported into the `dispute-coordinator`.
///
/// Vote imports are way more efficient when performed in batches, hence we batch together incoming
/// votes until the rate of incoming votes falls below a threshold, then we import into the dispute
/// coordinator.
///
/// A `Batch` keeps track of the votes to be imported and the current incoming rate, on rate update
/// it will "flush" in case the incoming rate dropped too low, preparing the import.
pub struct Batch {
	/// The actual candidate this batch is concerned with.
	candidate_receipt: CandidateReceipt,

	/// Cache of `CandidateHash` (candidate_receipt.hash()).
	candidate_hash: CandidateHash,

	/// All valid votes received in this batch so far.
	///
	/// We differentiate between valid and invalid votes, so we can detect (and drop) duplicates,
	/// while still allowing validators to equivocate.
	///
	/// Detecting and rejecting duplicates is crucial in order to effectively enforce
	/// `MIN_KEEP_BATCH_ALIVE_VOTES` per `BATCH_COLLECTING_INTERVAL`. If we would count duplicates
	/// here, the mechanism would be broken.
	valid_votes: HashMap<ValidatorIndex, SignedDisputeStatement>,

	/// All invalid votes received in this batch so far.
	invalid_votes: HashMap<ValidatorIndex, SignedDisputeStatement>,

	/// How many votes have been batched since the last tick/creation.
	votes_batched_since_last_tick: u32,

	/// Expiry time for the batch.
	///
	/// By this time the latest this batch will get flushed.
	best_before: Instant,

	/// Requesters waiting for a response.
	requesters: Vec<(PeerId, OutgoingResponseSender<DisputeRequest>)>,
}

/// Result of checking a batch every `BATCH_COLLECTING_INTERVAL`.
pub(super) enum TickResult {
	/// Batch is still alive, please call `tick` again at the given `Instant`.
	Alive(Batch, Instant),
	/// Batch is done, ready for import!
	Done(PreparedImport),
}

/// Ready for import.
pub struct PreparedImport {
	pub candidate_receipt: CandidateReceipt,
	pub statements: Vec<(SignedDisputeStatement, ValidatorIndex)>,
	/// Information about original requesters.
	pub requesters: Vec<(PeerId, OutgoingResponseSender<DisputeRequest>)>,
}

impl From<Batch> for PreparedImport {
	fn from(batch: Batch) -> Self {
		let Batch {
			candidate_receipt,
			valid_votes,
			invalid_votes,
			requesters: pending_responses,
			..
		} = batch;

		let statements = valid_votes
			.into_iter()
			.chain(invalid_votes.into_iter())
			.map(|(index, statement)| (statement, index))
			.collect();

		Self { candidate_receipt, statements, requesters: pending_responses }
	}
}

impl Batch {
	/// Create a new empty batch based on the given `CandidateReceipt`.
	///
	/// To create a `Batch` use Batches::find_batch`.
	///
	/// Arguments:
	///
	/// * `candidate_receipt` - The candidate this batch is meant to track votes for.
	/// * `now` - current time stamp for calculating the first tick.
	///
	/// Returns: A batch and the first `Instant` you are supposed to call `tick`.
	pub(super) fn new(candidate_receipt: CandidateReceipt, now: Instant) -> (Self, Instant) {
		let s = Self {
			candidate_hash: candidate_receipt.hash(),
			candidate_receipt,
			valid_votes: HashMap::new(),
			invalid_votes: HashMap::new(),
			votes_batched_since_last_tick: 0,
			best_before: Instant::now() + MAX_BATCH_LIFETIME,
			requesters: Vec::new(),
		};
		let next_tick = s.calculate_next_tick(now);
		(s, next_tick)
	}

	/// Receipt of the candidate this batch is batching votes for.
	pub fn candidate_receipt(&self) -> &CandidateReceipt {
		&self.candidate_receipt
	}

	/// Add votes from a validator into the batch.
	///
	/// The statements are supposed to be the valid and invalid statements received in a
	/// `DisputeRequest`.
	///
	/// The given `pending_response` is the corresponding response sender for responding to `peer`.
	/// If at least one of the votes is new as far as this batch is concerned we record the
	/// pending_response, for later use. In case both votes are known already, we return the
	/// response sender as an `Err` value.
	pub fn add_votes(
		&mut self,
		valid_vote: (SignedDisputeStatement, ValidatorIndex),
		invalid_vote: (SignedDisputeStatement, ValidatorIndex),
		peer: PeerId,
		pending_response: OutgoingResponseSender<DisputeRequest>,
	) -> Result<(), OutgoingResponseSender<DisputeRequest>> {
		debug_assert!(valid_vote.0.candidate_hash() == invalid_vote.0.candidate_hash());
		debug_assert!(valid_vote.0.candidate_hash() == &self.candidate_hash);

		let mut duplicate = true;

		if self.valid_votes.insert(valid_vote.1, valid_vote.0).is_none() {
			self.votes_batched_since_last_tick += 1;
			duplicate = false;
		}
		if self.invalid_votes.insert(invalid_vote.1, invalid_vote.0).is_none() {
			self.votes_batched_since_last_tick += 1;
			duplicate = false;
		}

		if duplicate {
			Err(pending_response)
		} else {
			self.requesters.push((peer, pending_response));
			Ok(())
		}
	}

	/// Check batch for liveness.
	///
	/// This function is supposed to be called at instants given at construction and as returned as
	/// part of `TickResult`.
	pub(super) fn tick(mut self, now: Instant) -> TickResult {
		if self.votes_batched_since_last_tick >= MIN_KEEP_BATCH_ALIVE_VOTES &&
			now < self.best_before
		{
			// Still good:
			let next_tick = self.calculate_next_tick(now);
			// Reset counter:
			self.votes_batched_since_last_tick = 0;
			TickResult::Alive(self, next_tick)
		} else {
			TickResult::Done(PreparedImport::from(self))
		}
	}

	/// Calculate when the next tick should happen.
	///
	/// This will usually return `now + BATCH_COLLECTING_INTERVAL`, except if the lifetime of this
	/// batch would exceed `MAX_BATCH_LIFETIME`.
	///
	/// # Arguments
	///
	/// * `now` - The current time.
	fn calculate_next_tick(&self, now: Instant) -> Instant {
		let next_tick = now + BATCH_COLLECTING_INTERVAL;
		if next_tick < self.best_before {
			next_tick
		} else {
			self.best_before
		}
	}
}