Skip to content
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

Make fp-rpc consensus algorithm agnostic #9

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ tokio = { workspace = true, features = ["sync"] }
# Substrate
prometheus-endpoint = { workspace = true }
sc-client-api = { workspace = true }
sc-consensus-aura = { workspace = true }
sc-network = { workspace = true }
sc-network-sync = { workspace = true }
sc-rpc = { workspace = true }
Expand All @@ -43,15 +42,13 @@ sp-api = { workspace = true, features = ["default"] }
sp-block-builder = { workspace = true, features = ["default"] }
sp-blockchain = { workspace = true }
sp-consensus = { workspace = true }
sp-consensus-aura = { workspace = true, features = ["default"] }
sp-core = { workspace = true, features = ["default"] }
sp-externalities = { workspace = true, features = ["default"] }
sp-inherents = { workspace = true, features = ["default"] }
sp-io = { workspace = true, features = ["default"] }
sp-runtime = { workspace = true, features = ["default"] }
sp-state-machine = { workspace = true, features = ["default"] }
sp-storage = { workspace = true, features = ["default"] }
sp-timestamp = { workspace = true, features = ["default"] }
# Frontier
fc-api = { workspace = true }
fc-mapping-sync = { workspace = true }
Expand Down
67 changes: 2 additions & 65 deletions client/rpc/src/eth/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{marker::PhantomData, sync::Arc};

// Substrate
use sc_client_api::{
backend::{AuxStore, Backend, StorageProvider},
UsageProvider,
};
use sc_client_api::backend::{Backend, StorageProvider};
use sc_transaction_pool::ChainApi;
use sc_transaction_pool_api::InPoolTransaction;
use sp_api::{ApiExt, ApiRef, Core, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{ApplyExtrinsicFailed, HeaderBackend};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::{
generic::{Digest, DigestItem},
generic::Digest,
traits::{Block as BlockT, Header as HeaderT, One},
TransactionOutcome,
};
use sp_timestamp::TimestampInherentData;

use crate::eth::Eth;
use fp_rpc::EthereumRuntimeRPCApi;
Expand Down Expand Up @@ -168,60 +162,3 @@ impl<B: BlockT> ConsensusDataProvider<B> for () {
Ok(Default::default())
}
}

pub use self::aura::AuraConsensusDataProvider;
mod aura {
use super::*;
use sp_consensus_aura::{
digests::CompatibleDigestItem,
sr25519::{AuthorityId, AuthoritySignature},
AuraApi, Slot, SlotDuration,
};

/// Consensus data provider for Aura.
pub struct AuraConsensusDataProvider<B, C> {
// slot duration
slot_duration: SlotDuration,
// phantom data for required generics
_phantom: PhantomData<(B, C)>,
}

impl<B, C> AuraConsensusDataProvider<B, C>
where
B: BlockT,
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
C::Api: AuraApi<B, AuthorityId>,
{
/// Creates a new instance of the [`AuraConsensusDataProvider`], requires that `client`
/// implements [`sp_consensus_aura::AuraApi`]
pub fn new(client: Arc<C>) -> Self {
let slot_duration = sc_consensus_aura::slot_duration(&*client)
.expect("slot_duration is always present; qed.");
Self {
slot_duration,
_phantom: PhantomData,
}
}
}

impl<B: BlockT, C: Send + Sync> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C> {
fn create_digest(
&self,
_parent: &B::Header,
data: &InherentData,
) -> Result<Digest, sp_inherents::Error> {
let timestamp = data
.timestamp_inherent_data()?
.expect("Timestamp is always present; qed");

let digest_item =
<DigestItem as CompatibleDigestItem<AuthoritySignature>>::aura_pre_digest(
Slot::from_timestamp(timestamp, self.slot_duration),
);

Ok(Digest {
logs: vec![digest_item],
})
}
}
}
70 changes: 66 additions & 4 deletions template/node/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,67 @@ pub struct EthDeps<B: BlockT, C, P, A: ChainApi, CT, CIDP> {
pub pending_create_inherent_data_providers: CIDP,
}

mod aura {
use super::*;
use fc_rpc::pending::ConsensusDataProvider;
use sp_consensus_aura::{
digests::CompatibleDigestItem,
sr25519::{AuthorityId, AuthoritySignature},
AuraApi, Slot, SlotDuration,
};
use sp_inherents::InherentData;
use sp_runtime::{Digest, DigestItem};
use sp_timestamp::TimestampInherentData;
use std::marker::PhantomData;

/// Consensus data provider for Aura.
pub struct AuraConsensusDataProvider<B, C> {
// slot duration
slot_duration: SlotDuration,
// phantom data for required generics
_phantom: PhantomData<(B, C)>,
}

impl<B, C> AuraConsensusDataProvider<B, C>
where
B: BlockT,
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
C::Api: AuraApi<B, AuthorityId>,
{
/// Creates a new instance of the [`AuraConsensusDataProvider`], requires that `client`
/// implements [`sp_consensus_aura::AuraApi`]
pub fn new(client: Arc<C>) -> Self {
let slot_duration = sc_consensus_aura::slot_duration(&*client)
.expect("slot_duration is always present; qed.");
Self {
slot_duration,
_phantom: PhantomData,
}
}
}

impl<B: BlockT, C: Send + Sync> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, C> {
fn create_digest(
&self,
_parent: &B::Header,
data: &InherentData,
) -> Result<Digest, sp_inherents::Error> {
let timestamp = data
.timestamp_inherent_data()?
.expect("Timestamp is always present; qed");

let digest_item =
<DigestItem as CompatibleDigestItem<AuthoritySignature>>::aura_pre_digest(
Slot::from_timestamp(timestamp, self.slot_duration),
);

Ok(Digest {
logs: vec![digest_item],
})
}
}
}

/// Instantiate Ethereum-compatible RPC extensions.
pub fn create_eth<B, C, BE, P, A, CT, CIDP, EC>(
mut io: RpcModule<()>,
Expand Down Expand Up @@ -94,9 +155,8 @@ where
EC: EthConfig<B, C>,
{
use fc_rpc::{
pending::AuraConsensusDataProvider, Debug, DebugApiServer, Eth, EthApiServer, EthDevSigner,
EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, EthSigner, Net, NetApiServer,
Web3, Web3ApiServer,
Debug, DebugApiServer, Eth, EthApiServer, EthDevSigner, EthFilter, EthFilterApiServer,
EthPubSub, EthPubSubApiServer, EthSigner, Net, NetApiServer, Web3, Web3ApiServer,
};
#[cfg(feature = "txpool")]
use fc_rpc::{TxPool, TxPoolApiServer};
Expand Down Expand Up @@ -144,7 +204,9 @@ where
execute_gas_limit_multiplier,
forced_parent_hashes,
pending_create_inherent_data_providers,
Some(Box::new(AuraConsensusDataProvider::new(client.clone()))),
Some(Box::new(aura::AuraConsensusDataProvider::new(
client.clone(),
))),
)
.replace_config::<EC>()
.into_rpc(),
Expand Down