-
Notifications
You must be signed in to change notification settings - Fork 539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for fetching grandpa justifications for arbitrary blocks #658
Open
ToufeeqP
wants to merge
8
commits into
main
Choose a base branch
from
toufeeq/grandpa-just-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d307624
feat: added grandpa_blockJustification RPC
ToufeeqP cd3f776
integrated grandpa RPC with node RPC
ToufeeqP 5fd0434
add CLI parameter to configure justification storage period
ToufeeqP 06452e9
cleaning
ToufeeqP c6f8373
conditionally enabled the grandpa-justifications feature behind the f…
ToufeeqP bebc711
updated justification_period upper bound
ToufeeqP 690062e
cleaning
ToufeeqP 647a399
Merge branch 'main' into toufeeq/grandpa-just-poc
ToufeeqP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,10 +169,7 @@ pub fn create_extrinsic( | |
#[allow(clippy::type_complexity)] | ||
pub fn new_partial( | ||
config: &Configuration, | ||
unsafe_da_sync: bool, | ||
kate_max_cells_size: usize, | ||
kate_rpc_enabled: bool, | ||
kate_rpc_metrics_enabled: bool, | ||
cli: Cli, | ||
) -> Result< | ||
sc_service::PartialComponents< | ||
FullClient, | ||
|
@@ -248,9 +245,14 @@ pub fn new_partial( | |
client.clone(), | ||
); | ||
|
||
#[cfg(not(feature = "grandpa-justifications"))] | ||
let grandpa_justification_period = GRANDPA_JUSTIFICATION_PERIOD; | ||
#[cfg(feature = "grandpa-justifications")] | ||
let grandpa_justification_period = cli.grandpa_justification_period; | ||
|
||
let (grandpa_block_import, grandpa_link) = sc_consensus_grandpa::block_import( | ||
client.clone(), | ||
GRANDPA_JUSTIFICATION_PERIOD, | ||
grandpa_justification_period, | ||
&(client.clone() as Arc<_>), | ||
select_chain.clone(), | ||
telemetry.as_ref().map(|x| x.handle()), | ||
|
@@ -263,7 +265,7 @@ pub fn new_partial( | |
client.clone(), | ||
)?; | ||
|
||
let da_block_import = BlockImport::new(client.clone(), block_import, unsafe_da_sync); | ||
let da_block_import = BlockImport::new(client.clone(), block_import, cli.unsafe_da_sync); | ||
|
||
let slot_duration = babe_link.config().slot_duration(); | ||
let (import_queue, babe_worker_handle) = | ||
|
@@ -330,9 +332,9 @@ pub fn new_partial( | |
subscription_executor, | ||
finality_provider: finality_proof_provider.clone(), | ||
}, | ||
kate_max_cells_size, | ||
kate_rpc_enabled, | ||
kate_rpc_metrics_enabled, | ||
kate_max_cells_size: cli.kate_max_cells_size, | ||
kate_rpc_enabled: cli.kate_rpc_enabled, | ||
kate_rpc_metrics_enabled: cli.kate_rpc_metrics_enabled, | ||
}; | ||
|
||
node_rpc::create_full(deps, rpc_backend.clone()).map_err(Into::into) | ||
|
@@ -375,10 +377,7 @@ pub fn new_full_base( | |
config: Configuration, | ||
disable_hardware_benchmarks: bool, | ||
with_startup_data: impl FnOnce(&BlockImport, &sc_consensus_babe::BabeLink<Block>), | ||
unsafe_da_sync: bool, | ||
kate_max_cells_size: usize, | ||
kate_rpc_enabled: bool, | ||
kate_rpc_metrics_enabled: bool, | ||
cli: Cli, | ||
) -> Result<NewFullBase, ServiceError> { | ||
let hwbench = if !disable_hardware_benchmarks { | ||
config.database.path().map(|database_path| { | ||
|
@@ -389,6 +388,11 @@ pub fn new_full_base( | |
None | ||
}; | ||
|
||
#[cfg(not(feature = "grandpa-justifications"))] | ||
let grandpa_justification_period = GRANDPA_JUSTIFICATION_PERIOD; | ||
#[cfg(feature = "grandpa-justifications")] | ||
let grandpa_justification_period = cli.grandpa_justification_period; | ||
|
||
let sc_service::PartialComponents { | ||
client, | ||
backend, | ||
|
@@ -398,13 +402,7 @@ pub fn new_full_base( | |
select_chain, | ||
transaction_pool, | ||
other: (rpc_builder, import_setup, rpc_setup, mut telemetry), | ||
} = new_partial( | ||
&config, | ||
unsafe_da_sync, | ||
kate_max_cells_size, | ||
kate_rpc_enabled, | ||
kate_rpc_metrics_enabled, | ||
)?; | ||
} = new_partial(&config, cli)?; | ||
|
||
let shared_voter_state = rpc_setup; | ||
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht; | ||
|
@@ -582,7 +580,7 @@ pub fn new_full_base( | |
let grandpa_config = sc_consensus_grandpa::Config { | ||
// Considering our block_time of 20 seconds, we may consider increasing this to 2 seconds without introducing delay in finality. | ||
gossip_duration: std::time::Duration::from_millis(1000), | ||
justification_generation_period: GRANDPA_JUSTIFICATION_PERIOD, | ||
justification_generation_period: grandpa_justification_period, | ||
name: Some(name), | ||
observer_enabled: false, | ||
keystore, | ||
|
@@ -655,20 +653,22 @@ pub fn new_full_base( | |
/// Builds a new service for a full client. | ||
pub fn new_full(config: Configuration, cli: Cli) -> Result<TaskManager, ServiceError> { | ||
let database_path = config.database.path().map(Path::to_path_buf); | ||
let storage_param = cli.storage_monitor.clone(); | ||
let task_manager = new_full_base( | ||
config, | ||
cli.no_hardware_benchmarks, | ||
|_, _| (), | ||
cli.unsafe_da_sync, | ||
cli.kate_max_cells_size, | ||
cli.kate_rpc_enabled, | ||
cli.kate_rpc_metrics_enabled, | ||
cli, | ||
// cli.unsafe_da_sync, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove commented code |
||
// cli.kate_max_cells_size, | ||
// cli.kate_rpc_enabled, | ||
// cli.kate_rpc_metrics_enabled, | ||
) | ||
.map(|NewFullBase { task_manager, .. }| task_manager)?; | ||
|
||
if let Some(database_path) = database_path { | ||
sc_storage_monitor::StorageMonitorService::try_spawn( | ||
cli.storage_monitor, | ||
storage_param, | ||
database_path, | ||
&task_manager.spawn_essential_handle(), | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use jsonrpsee::{ | ||
core::{async_trait, RpcResult}, | ||
proc_macros::rpc, | ||
types::error::ErrorObject, | ||
}; | ||
use sc_client_api::BlockBackend; | ||
use sp_runtime::traits::Block as BlockT; | ||
use std::{marker::PhantomData, sync::Arc}; | ||
|
||
/// GRANDPA consensus engine_id | ||
pub const GRANDPA_ENGINE_ID: [u8; 4] = *b"FRNK"; | ||
|
||
#[rpc(client, server)] | ||
pub trait Grandpa<Block> | ||
where | ||
Block: BlockT, | ||
{ | ||
#[method(name = "grandpa_blockJustification")] | ||
async fn block_justification(&self, block_number: u32) -> RpcResult<Vec<u8>>; | ||
} | ||
|
||
pub struct GrandpaJustifications<Client, Block: BlockT> { | ||
client: Arc<Client>, | ||
_block: PhantomData<Block>, | ||
} | ||
|
||
impl<Client, Block: BlockT> GrandpaJustifications<Client, Block> { | ||
pub fn new(client: Arc<Client>) -> Self { | ||
Self { | ||
client, | ||
_block: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
/// Error type for this RPC API. | ||
pub enum Error { | ||
/// Generic justification error. | ||
JustificationError, | ||
} | ||
|
||
impl From<Error> for i32 { | ||
fn from(e: Error) -> i32 { | ||
match e { | ||
Error::JustificationError => 1, | ||
} | ||
} | ||
} | ||
|
||
macro_rules! internal_err { | ||
($($arg:tt)*) => {{ | ||
ErrorObject::owned( | ||
Error::JustificationError.into(), | ||
format!($($arg)*), | ||
None::<()> | ||
) | ||
}} | ||
} | ||
|
||
#[async_trait] | ||
impl<Client, Block> GrandpaServer<Block> for GrandpaJustifications<Client, Block> | ||
where | ||
Block: BlockT, | ||
Client: Send + Sync + 'static, | ||
Client: BlockBackend<Block>, | ||
{ | ||
async fn block_justification(&self, block_number: u32) -> RpcResult<Vec<u8>> { | ||
// Fetch the block hash | ||
let block_hash = self | ||
.client | ||
.block_hash(block_number.into()) | ||
.map_err(|e| internal_err!("Failed to fetch block hash: {e:?}"))? | ||
.ok_or_else(|| internal_err!("Block hash not found for block #{block_number}"))?; | ||
|
||
// Fetch the justification for the block hash | ||
let justification = self | ||
.client | ||
.justifications(block_hash) | ||
.map_err(|e| internal_err!("Failed to fetch justifications: {e:?}"))? | ||
.and_then(|just| just.into_justification(GRANDPA_ENGINE_ID)); | ||
|
||
justification | ||
.ok_or_else(|| internal_err!("Cannot fetch justification for block #{block_number}")) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we create a
KateDeps
here, in the same way ofnode_rpc::BabeDeps
andnode_rpc::GrandpaDeps
(see some lines avobe).