mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-10 05:09:22 +00:00
Implement proper checking of inherents
This commit is contained in:
@@ -7,27 +7,39 @@ use std::{
|
||||
|
||||
use sp_runtime::traits::{Header, Block};
|
||||
|
||||
use sp_consensus::Error;
|
||||
use sc_consensus::{BlockImportStatus, BlockImportError, Link};
|
||||
|
||||
use sc_service::ImportQueue;
|
||||
|
||||
use tendermint_machine::ext::BlockError;
|
||||
|
||||
use crate::TendermintImportQueue;
|
||||
|
||||
// Custom helpers for ImportQueue in order to obtain the result of a block's importing
|
||||
struct ValidateLink<B: Block>(Option<(B::Hash, bool)>);
|
||||
struct ValidateLink<B: Block>(Option<(B::Hash, Result<(), BlockError>)>);
|
||||
impl<B: Block> Link<B> for ValidateLink<B> {
|
||||
fn blocks_processed(
|
||||
&mut self,
|
||||
imported: usize,
|
||||
count: usize,
|
||||
results: Vec<(
|
||||
mut results: Vec<(
|
||||
Result<BlockImportStatus<<B::Header as Header>::Number>, BlockImportError>,
|
||||
B::Hash,
|
||||
)>,
|
||||
) {
|
||||
assert_eq!(imported, 1);
|
||||
assert_eq!(count, 1);
|
||||
self.0 = Some((results[0].1, results[0].0.is_ok()));
|
||||
self.0 = Some((
|
||||
results[0].1,
|
||||
match results.swap_remove(0).0 {
|
||||
Ok(_) => Ok(()),
|
||||
Err(BlockImportError::Other(Error::Other(err))) => Err(
|
||||
err.downcast::<BlockError>().map(|boxed| *boxed.as_ref()).unwrap_or(BlockError::Fatal),
|
||||
),
|
||||
_ => Err(BlockError::Fatal),
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +57,7 @@ impl<'a, B: Block, T: Send> ImportFuture<'a, B, T> {
|
||||
}
|
||||
|
||||
impl<'a, B: Block, T: Send> Future for ImportFuture<'a, B, T> {
|
||||
type Output = bool;
|
||||
type Output = Result<(), BlockError>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let mut link = ValidateLink(None);
|
||||
|
||||
@@ -10,7 +10,6 @@ use log::warn;
|
||||
use tokio::task::yield_now;
|
||||
|
||||
use sp_core::{Encode, Decode};
|
||||
use sp_inherents::{InherentData, InherentDataProvider, CreateInherentDataProviders};
|
||||
use sp_runtime::{
|
||||
traits::{Header, Block},
|
||||
Digest,
|
||||
@@ -105,29 +104,7 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
||||
}
|
||||
|
||||
pub(crate) async fn get_proposal(&mut self, header: &<T::Block as Block>::Header) -> T::Block {
|
||||
let inherent_data = match self
|
||||
.import
|
||||
.providers
|
||||
.read()
|
||||
.await
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.create_inherent_data_providers(header.hash(), ())
|
||||
.await
|
||||
{
|
||||
Ok(providers) => match providers.create_inherent_data() {
|
||||
Ok(data) => Some(data),
|
||||
Err(err) => {
|
||||
warn!(target: "tendermint", "Failed to create inherent data: {}", err);
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
warn!(target: "tendermint", "Failed to create inherent data providers: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
.unwrap_or_else(InherentData::new);
|
||||
let parent = *header.parent_hash();
|
||||
|
||||
let proposer = self
|
||||
.active
|
||||
@@ -137,9 +114,15 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
||||
.init(header)
|
||||
.await
|
||||
.expect("Failed to create a proposer for the new block");
|
||||
// TODO: Production time, size limit
|
||||
|
||||
proposer
|
||||
.propose(inherent_data, Digest::default(), Duration::from_secs(1), None)
|
||||
.propose(
|
||||
self.import.inherent_data(parent).await,
|
||||
Digest::default(),
|
||||
// TODO: Production time, size limit
|
||||
Duration::from_secs(1),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to crate a new block proposal")
|
||||
.block
|
||||
@@ -316,9 +299,7 @@ impl<T: TendermintValidator> Network for TendermintAuthority<T> {
|
||||
}],
|
||||
);
|
||||
|
||||
if !ImportFuture::new(hash, queue_write.as_mut().unwrap()).await {
|
||||
todo!()
|
||||
}
|
||||
ImportFuture::new(hash, queue_write.as_mut().unwrap()).await?;
|
||||
|
||||
// Sanity checks that a child block can have less work than its parent
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user