Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d8d5da2

Browse files
authored
Improve error message on can_author_with failure (#4262)
1 parent a5739a9 commit d8d5da2

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

client/consensus/pow/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,12 @@ fn mine_loop<B: BlockT<Hash=H256>, C, Algorithm, E, SO, S, CAW>(
467467
},
468468
};
469469

470-
if can_author_with.can_author_with(&BlockId::Hash(best_hash)) {
471-
debug!(
470+
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(best_hash)) {
471+
warn!(
472472
target: "pow",
473-
"Skipping proposal `can_author_with` returned `false`. \
474-
Probably a node update is required!"
473+
"Skipping proposal `can_author_with` returned: {} \
474+
Probably a node update is required!",
475+
err,
475476
);
476477
std::thread::sleep(std::time::Duration::from_secs(1));
477478
continue 'outer

client/consensus/slots/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,23 @@ where
348348
}
349349
};
350350

351-
if can_author_with.can_author_with(&BlockId::Hash(chain_head.hash())) {
351+
if let Err(err) = can_author_with.can_author_with(&BlockId::Hash(chain_head.hash())) {
352+
warn!(
353+
target: "slots",
354+
"Unable to author block in slot {},. `can_author_with` returned: {} \
355+
Probably a node update is required!",
356+
slot_num,
357+
err,
358+
);
359+
Either::Right(future::ready(Ok(())))
360+
} else {
352361
Either::Left(
353362
worker.on_slot(chain_head, slot_info)
354363
.map_err(|e| {
355364
warn!(target: "slots", "Encountered consensus error: {:?}", e);
356365
})
357366
.or_else(|_| future::ready(Ok(())))
358367
)
359-
} else {
360-
warn!(
361-
target: "slots",
362-
"Unable to author block in slot {}. `can_author_with` returned `false`. \
363-
Probably a node update is required!",
364-
slot_num,
365-
);
366-
Either::Right(future::ready(Ok(())))
367368
}
368369
}).then(|res| {
369370
if let Err(err) = res {

primitives/consensus/common/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ impl<T> SyncOracle for Arc<T> where T: ?Sized, for<'r> &'r T: SyncOracle {
137137
/// at the given block.
138138
pub trait CanAuthorWith<Block: BlockT> {
139139
/// See trait docs for more information.
140-
fn can_author_with(&self, at: &BlockId<Block>) -> bool;
140+
///
141+
/// # Return
142+
///
143+
/// - Returns `Ok(())` when authoring is supported.
144+
/// - Returns `Err(_)` when authoring is not supported.
145+
fn can_author_with(&self, at: &BlockId<Block>) -> Result<(), String>;
141146
}
142147

143148
/// Checks if the node can author blocks by using
@@ -154,18 +159,15 @@ impl<T> CanAuthorWithNativeVersion<T> {
154159
impl<T: runtime_version::GetRuntimeVersion<Block>, Block: BlockT> CanAuthorWith<Block>
155160
for CanAuthorWithNativeVersion<T>
156161
{
157-
fn can_author_with(&self, at: &BlockId<Block>) -> bool {
162+
fn can_author_with(&self, at: &BlockId<Block>) -> Result<(), String> {
158163
match self.0.runtime_version(at) {
159164
Ok(version) => self.0.native_version().can_author_with(&version),
160165
Err(e) => {
161-
error!(
162-
target: "CanAuthorWithNativeVersion",
166+
Err(format!(
163167
"Failed to get runtime version at `{}` and will disable authoring. Error: {}",
164168
at,
165169
e,
166-
);
167-
168-
false
170+
))
169171
}
170172
}
171173
}
@@ -175,7 +177,7 @@ impl<T: runtime_version::GetRuntimeVersion<Block>, Block: BlockT> CanAuthorWith<
175177
pub struct AlwaysCanAuthor;
176178

177179
impl<Block: BlockT> CanAuthorWith<Block> for AlwaysCanAuthor {
178-
fn can_author_with(&self, _: &BlockId<Block>) -> bool {
179-
true
180+
fn can_author_with(&self, _: &BlockId<Block>) -> Result<(), String> {
181+
Ok(())
180182
}
181183
}

primitives/sr-version/src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,30 @@ pub struct NativeVersion {
161161
#[cfg(feature = "std")]
162162
impl NativeVersion {
163163
/// Check if this version matches other version for authoring blocks.
164-
pub fn can_author_with(&self, other: &RuntimeVersion) -> bool {
165-
self.runtime_version.spec_name == other.spec_name &&
166-
(self.runtime_version.authoring_version == other.authoring_version ||
167-
self.can_author_with.contains(&other.authoring_version))
164+
///
165+
/// # Return
166+
///
167+
/// - Returns `Ok(())` when authoring is supported.
168+
/// - Returns `Err(_)` with a detailed error when authoring is not supported.
169+
pub fn can_author_with(&self, other: &RuntimeVersion) -> Result<(), String> {
170+
if self.runtime_version.spec_name != other.spec_name {
171+
Err(format!(
172+
"`spec_name` does not match `{}` vs `{}`",
173+
self.runtime_version.spec_name,
174+
other.spec_name,
175+
))
176+
} else if (self.runtime_version.authoring_version != other.authoring_version
177+
&& !self.can_author_with.contains(&other.authoring_version))
178+
{
179+
Err(format!(
180+
"`authoring_version` does not match `{version}` vs `{other_version}` and \
181+
`can_author_with` not contains `{other_version}`",
182+
version = self.runtime_version.authoring_version,
183+
other_version = other.authoring_version,
184+
))
185+
} else {
186+
Ok(())
187+
}
168188
}
169189
}
170190

0 commit comments

Comments
 (0)