-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcandidate_descriptor_v2.hpp
169 lines (157 loc) · 6.3 KB
/
candidate_descriptor_v2.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/endian/conversion.hpp>
#include "crypto/sr25519_provider.hpp"
#include "parachain/pvf/pvf_error.hpp"
#include "parachain/transpose_claim_queue.hpp"
#include "parachain/types.hpp"
#include "parachain/ump_signal.hpp"
namespace kagome::network {
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L44
/// The default claim queue offset to be used if it's not
/// configured/accessible in the parachain
/// runtime
constexpr uint8_t kDefaultClaimQueueOffset = 0;
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L532-L534
inline bool isV1(const CandidateDescriptor &descriptor) {
constexpr auto is_zero = [](BufferView xs) {
return static_cast<size_t>(std::ranges::count(xs, 0)) == xs.size();
};
return not is_zero(std::span{descriptor.reserved_1}.subspan(7))
or not is_zero(descriptor.reserved_2);
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L532-L537
inline bool isV2(const CandidateDescriptor &descriptor) {
return not isV1(descriptor) and descriptor.reserved_1[0] == 0;
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L179
/// Check the signature of the collator within this descriptor.
inline outcome::result<void> checkSignature(
const crypto::Sr25519Provider &sr25519,
const CandidateDescriptor &descriptor) {
if (not isV1(descriptor)) {
return outcome::success();
}
OUTCOME_TRY(
r,
sr25519.verify(crypto::Sr25519Signature{descriptor.reserved_2},
descriptor.signable(),
crypto::Sr25519PublicKey{descriptor.reserved_1}));
if (not r) {
return PvfError::SIGNATURE;
}
return outcome::success();
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L580
/// Returns the `core_index` of `V2` candidate descriptors, `None` otherwise.
inline std::optional<parachain::CoreIndex> coreIndex(
const CandidateDescriptor &descriptor) {
if (isV1(descriptor)) {
return std::nullopt;
}
return boost::endian::load_little_u16(&descriptor.reserved_1[1]);
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L589
/// Returns the `core_index` of `V2` candidate descriptors, `None` otherwise.
inline std::optional<parachain::SessionIndex> sessionIndex(
const CandidateDescriptor &descriptor) {
if (isV1(descriptor)) {
return std::nullopt;
}
return boost::endian::load_little_u32(&descriptor.reserved_1[3]);
}
enum class CheckCoreIndexError : uint8_t {
InvalidCoreIndex,
NoAssignment,
UnknownVersion,
InvalidSession,
};
Q_ENUM_ERROR_CODE(CheckCoreIndexError) {
using E = decltype(e);
switch (e) {
case E::InvalidCoreIndex:
return "The specified core index is invalid";
case E::NoAssignment:
return "The parachain is not assigned to any core at specified claim "
"queue offset";
case E::UnknownVersion:
return "Unknown internal version";
case E::InvalidSession:
return "Invalid session";
}
abort();
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/primitives/src/vstaging/mod.rs#L602
/// Checks if descriptor core index is equal to the committed core index.
/// Input `cores_per_para` is a claim queue snapshot at the candidate's relay
/// parent, stored as a mapping between `ParaId` and the cores assigned per
/// depth.
inline outcome::result<void> checkCoreIndex(
const CommittedCandidateReceipt &receipt,
const TransposedClaimQueue &claims) {
if (isV1(receipt.descriptor)) {
return outcome::success();
}
if (not isV2(receipt.descriptor)) {
return CheckCoreIndexError::UnknownVersion;
}
OUTCOME_TRY(selector, coreSelector(receipt.commitments));
auto offset =
selector ? selector->claim_queue_offset : kDefaultClaimQueueOffset;
auto it1 = claims.find(receipt.descriptor.para_id);
if (it1 == claims.end()) {
return CheckCoreIndexError::NoAssignment;
}
auto it2 = it1->second.find(offset);
if (it2 == it1->second.end()) {
return CheckCoreIndexError::NoAssignment;
}
auto &assigned_cores = it2->second;
if (assigned_cores.empty()) {
return CheckCoreIndexError::NoAssignment;
}
auto core = coreIndex(receipt.descriptor).value();
if (not selector and assigned_cores.size() > 1) {
if (not assigned_cores.contains(core)) {
return CheckCoreIndexError::InvalidCoreIndex;
}
return outcome::success();
}
auto expected_core =
*std::next(assigned_cores.begin(),
selector ? static_cast<ptrdiff_t>(selector->core_selector
% assigned_cores.size())
: 0);
if (core != expected_core) {
return CheckCoreIndexError::InvalidCoreIndex;
}
return outcome::success();
}
// https://github.com/paritytech/polkadot-sdk/blob/1e3b8e1639c1cf784eabf0a9afcab1f3987e0ca4/polkadot/node/network/collator-protocol/src/validator_side/mod.rs#L2114
inline outcome::result<void> descriptorVersionSanityCheck(
const CandidateDescriptor &descriptor,
bool v2_receipts,
CoreIndex expected_core,
SessionIndex expected_session) {
if (isV1(descriptor)) {
return outcome::success();
}
if (not isV2(descriptor)) {
return CheckCoreIndexError::UnknownVersion;
}
if (not v2_receipts) {
return CheckCoreIndexError::UnknownVersion;
}
if (coreIndex(descriptor) != expected_core) {
return CheckCoreIndexError::InvalidCoreIndex;
}
if (sessionIndex(descriptor) != expected_session) {
return CheckCoreIndexError::InvalidSession;
}
return outcome::success();
}
} // namespace kagome::network