Skip to content

Commit c591df9

Browse files
authored
fc-babe (#20)
* fc-babe * reset * update * propagate error * update psdk branch * reset cargo.toml * update cargo.lock * taplo format
1 parent 8d00fcb commit c591df9

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919
"frame/hotfix-sufficients",
2020
"client/api",
2121
"client/aura",
22+
"client/babe",
2223
"client/consensus",
2324
"client/rpc-core",
2425
"client/rpc",
@@ -91,6 +92,7 @@ sc-client-api = { git = "https://github.com/paritytech/polkadot-sdk", tag = "pol
9192
sc-client-db = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
9293
sc-consensus = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
9394
sc-consensus-aura = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
95+
sc-consensus-babe = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
9496
sc-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
9597
sc-consensus-manual-seal = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
9698
sc-executor = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6" }
@@ -112,6 +114,7 @@ sp-block-builder = { git = "https://github.com/paritytech/polkadot-sdk", tag = "
112114
sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
113115
sp-consensus = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
114116
sp-consensus-aura = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
117+
sp-consensus-babe = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
115118
sp-consensus-grandpa = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
116119
sp-core = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
117120
sp-crypto-hashing = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2503-6", default-features = false }
@@ -176,6 +179,7 @@ ark-std = { version = "0.4.0", default-features = false }
176179
# Frontier Client
177180
fc-api = { path = "client/api" }
178181
fc-aura = { path = "client/aura" }
182+
fc-babe = { path = "client/babe" }
179183
fc-cli = { path = "client/cli", default-features = false }
180184
fc-consensus = { path = "client/consensus" }
181185
fc-db = { path = "client/db", default-features = false }

client/babe/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "fc-babe"
3+
version = "1.0.0-dev"
4+
authors = { workspace = true }
5+
edition = { workspace = true }
6+
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
7+
repository = { workspace = true }
8+
description = "Babe consensus data provider for pending runtime calls"
9+
10+
[dependencies]
11+
# Substrate
12+
sc-client-api = { workspace = true }
13+
sc-consensus-babe = { workspace = true }
14+
sp-api = { workspace = true }
15+
sp-blockchain = { workspace = true }
16+
sp-consensus-babe = { workspace = true }
17+
sp-inherents = { workspace = true }
18+
sp-runtime = { workspace = true }
19+
sp-timestamp = { workspace = true }
20+
# Frontier
21+
fc-rpc = { workspace = true }

client/babe/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::{marker::PhantomData, sync::Arc};
2+
3+
use fc_rpc::pending::ConsensusDataProvider;
4+
use sc_client_api::{AuxStore, UsageProvider};
5+
use sp_api::ProvideRuntimeApi;
6+
use sp_blockchain::Error;
7+
use sp_consensus_babe::{digests::CompatibleDigestItem, BabeApi, BabeConfiguration, Slot};
8+
use sp_inherents::InherentData;
9+
use sp_runtime::{traits::Block as BlockT, Digest, DigestItem};
10+
use sp_timestamp::TimestampInherentData;
11+
12+
pub struct BabeConsensusDataProvider<B, C> {
13+
babe_config: BabeConfiguration,
14+
_phantom: PhantomData<(B, C)>,
15+
}
16+
17+
impl<B, C> BabeConsensusDataProvider<B, C>
18+
where
19+
B: BlockT,
20+
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
21+
C::Api: BabeApi<B>,
22+
{
23+
/// Creates a new instance of the [`BabeConsensusDataProvider`], requires that `client`
24+
/// implements [`sp_consensus_babe::BabeApi`]
25+
pub fn new(client: Arc<C>) -> Result<Self, Error> {
26+
let babe_config = sc_consensus_babe::configuration(&*client)?;
27+
Ok(Self {
28+
babe_config,
29+
_phantom: PhantomData,
30+
})
31+
}
32+
}
33+
34+
impl<B: BlockT, C: Send + Sync> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C> {
35+
fn create_digest(
36+
&self,
37+
_parent: &B::Header,
38+
data: &InherentData,
39+
) -> Result<Digest, sp_inherents::Error> {
40+
let timestamp = data
41+
.timestamp_inherent_data()?
42+
.expect("Timestamp is always present; qed");
43+
44+
let slot_duration = self.babe_config.slot_duration();
45+
let slot = Slot::from_timestamp(timestamp, slot_duration);
46+
47+
let digest_item = <DigestItem as CompatibleDigestItem>::babe_pre_digest(
48+
sp_consensus_babe::digests::PreDigest::SecondaryPlain(
49+
sp_consensus_babe::digests::SecondaryPlainPreDigest {
50+
slot,
51+
authority_index: 0, // Use first authority for pending blocks
52+
},
53+
),
54+
);
55+
56+
Ok(Digest {
57+
logs: vec![digest_item],
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)