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

//! A generic candidate backing subsystem mockup suitable to be used in benchmarks.

use crate::{configuration::TestConfiguration, NODE_UNDER_TEST};
use futures::FutureExt;
use polkadot_node_primitives::{SignedFullStatementWithPVD, Statement, StatementWithPVD};
use polkadot_node_subsystem::{
	messages::CandidateBackingMessage, overseer, SpawnedSubsystem, SubsystemError,
};
use polkadot_node_subsystem_types::OverseerSignal;
use polkadot_primitives::{
	CandidateHash, Hash, PersistedValidationData, SigningContext, ValidatorIndex, ValidatorPair,
};
use sp_core::Pair;
use std::collections::HashMap;

const LOG_TARGET: &str = "subsystem-bench::candidate-backing-mock";

struct MockCandidateBackingState {
	pair: ValidatorPair,
	pvd: PersistedValidationData,
	own_backing_group: Vec<ValidatorIndex>,
}

pub struct MockCandidateBacking {
	config: TestConfiguration,
	state: MockCandidateBackingState,
}

impl MockCandidateBacking {
	pub fn new(
		config: TestConfiguration,
		pair: ValidatorPair,
		pvd: PersistedValidationData,
		own_backing_group: Vec<ValidatorIndex>,
	) -> Self {
		Self { config, state: MockCandidateBackingState { pair, pvd, own_backing_group } }
	}

	fn handle_statement(
		&self,
		relay_parent: Hash,
		statement: SignedFullStatementWithPVD,
		statements_tracker: &mut HashMap<CandidateHash, u32>,
	) -> Vec<polkadot_node_subsystem::messages::StatementDistributionMessage> {
		let mut messages = vec![];
		let validator_id = statement.validator_index();
		let is_own_backing_group = self.state.own_backing_group.contains(&validator_id);

		match statement.payload() {
			StatementWithPVD::Seconded(receipt, _pvd) => {
				let candidate_hash = receipt.hash();
				statements_tracker
					.entry(candidate_hash)
					.and_modify(|v| {
						*v += 1;
					})
					.or_insert(1);

				let statements_received_count = *statements_tracker.get(&candidate_hash).unwrap();
				if statements_received_count == (self.config.minimum_backing_votes - 1) &&
					is_own_backing_group
				{
					let statement = Statement::Valid(candidate_hash);
					let context = SigningContext { parent_hash: relay_parent, session_index: 0 };
					let payload = statement.to_compact().signing_payload(&context);
					let message =
						polkadot_node_subsystem::messages::StatementDistributionMessage::Share(
							relay_parent,
							SignedFullStatementWithPVD::new(
								statement.supply_pvd(self.state.pvd.clone()),
								ValidatorIndex(NODE_UNDER_TEST),
								self.state.pair.sign(&payload[..]),
								&context,
								&self.state.pair.public(),
							)
							.unwrap(),
						);
					messages.push(message);
				}

				if statements_received_count == self.config.minimum_backing_votes {
					let message =
						polkadot_node_subsystem::messages::StatementDistributionMessage::Backed(
							candidate_hash,
						);
					messages.push(message);
				}
			},
			StatementWithPVD::Valid(candidate_hash) => {
				statements_tracker
					.entry(*candidate_hash)
					.and_modify(|v| {
						*v += 1;
					})
					.or_insert(1);

				let statements_received_count = *statements_tracker.get(candidate_hash).unwrap();
				if statements_received_count == self.config.minimum_backing_votes {
					let message =
						polkadot_node_subsystem::messages::StatementDistributionMessage::Backed(
							*candidate_hash,
						);
					messages.push(message);
				}
			},
		}

		messages
	}
}

#[overseer::subsystem(CandidateBacking, error=SubsystemError, prefix=self::overseer)]
impl<Context> MockCandidateBacking {
	fn start(self, ctx: Context) -> SpawnedSubsystem {
		let future = self.run(ctx).map(|_| Ok(())).boxed();

		SpawnedSubsystem { name: "test-environment", future }
	}
}

#[overseer::contextbounds(CandidateBacking, prefix = self::overseer)]
impl MockCandidateBacking {
	async fn run<Context>(self, mut ctx: Context) {
		let mut statements_tracker: HashMap<CandidateHash, u32> = Default::default();

		loop {
			let msg = ctx.recv().await.expect("Overseer never fails us");
			match msg {
				orchestra::FromOrchestra::Signal(signal) =>
					if signal == OverseerSignal::Conclude {
						return
					},
				orchestra::FromOrchestra::Communication { msg } => {
					gum::trace!(target: LOG_TARGET, msg=?msg, "recv message");

					match msg {
						CandidateBackingMessage::Statement(relay_parent, statement) => {
							let messages = self.handle_statement(
								relay_parent,
								statement,
								&mut statements_tracker,
							);
							for message in messages {
								ctx.send_message(message).await;
							}
						},
						_ => {
							unimplemented!("Unexpected candidate-backing message")
						},
					}
				},
			}
		}
	}
}