Skip to content

Commit a6cfea9

Browse files
committed
wip/included head not present in relay storage proof
1 parent 03993a1 commit a6cfea9

File tree

6 files changed

+474
-15
lines changed

6 files changed

+474
-15
lines changed

crates/anvil-polkadot/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,21 @@ polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch
6767
"substrate-frame-rpc-system",
6868
"substrate-rpc-client",
6969
"substrate-wasm-builder",
70+
71+
"sc-consensus-aura",
72+
"polkadot-primitives",
73+
"cumulus-client-parachain-inherent",
74+
"sp-arithmetic",
75+
"cumulus-client-service",
76+
"cumulus-primitives-aura",
77+
78+
"cumulus-primitives-core",
79+
"sp-inherents",
7080
] }
81+
hex = "0.4"
82+
indicatif.workspace = true
83+
84+
7185
anvil.workspace = true
7286
anvil-core.workspace = true
7387
anvil-server = { workspace = true, features = ["clap"] }

crates/anvil-polkadot/src/cmd.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ pub struct NodeArgs {
9797

9898
#[command(flatten)]
9999
pub server_config: ServerConfig,
100+
101+
#[command(flatten)]
102+
pub fork: ForkArgs,
100103
}
101104

102105
/// The default IPC endpoint
@@ -133,7 +136,9 @@ impl NodeArgs {
133136
.with_code_size_limit(self.evm.code_size_limit)
134137
.disable_code_size_limit(self.evm.disable_code_size_limit)
135138
.with_disable_default_create2_deployer(self.evm.disable_default_create2_deployer)
136-
.with_memory_limit(self.evm.memory_limit);
139+
.with_memory_limit(self.evm.memory_limit)
140+
.with_fork_url(self.fork.fork_url)
141+
.with_fork_block_hash(self.fork.fork_block_hash);
137142

138143
let substrate_node_config = SubstrateNodeConfig::new(&anvil_config);
139144

@@ -253,6 +258,22 @@ fn duration_from_secs_f64(s: &str) -> Result<Duration, String> {
253258
Duration::try_from_secs_f64(s).map_err(|e| e.to_string())
254259
}
255260

261+
#[derive(Clone, Debug, Parser)]
262+
#[command(next_help_heading = "Fork options")]
263+
pub struct ForkArgs {
264+
/// Fetch state over a remote endpoint instead of starting from an empty state.
265+
#[arg(
266+
long = "fork-url",
267+
short = 'f',
268+
value_name = "URL",
269+
)]
270+
pub fork_url: Option<String>,
271+
272+
/// Fetch state from a specific block hash over a remote endpoint.
273+
#[arg(long, value_name = "BLOCK")]
274+
pub fork_block_hash: Option<String>,
275+
}
276+
256277
#[cfg(test)]
257278
mod tests {
258279
use super::*;

crates/anvil-polkadot/src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ pub struct AnvilNodeConfig {
324324
pub memory_limit: Option<u64>,
325325
/// Do not print log messages.
326326
pub silent: bool,
327+
/// Fetch state over a remote endpoint instead of starting from an empty state.
328+
pub fork_url: Option<String>,
329+
/// Fetch state from a specific block hash over a remote endpoint.
330+
pub fork_block_hash: Option<String>,
327331
}
328332

329333
impl AnvilNodeConfig {
@@ -545,6 +549,8 @@ impl Default for AnvilNodeConfig {
545549
disable_default_create2_deployer: false,
546550
memory_limit: None,
547551
silent: false,
552+
fork_url: None,
553+
fork_block_hash: None,
548554
}
549555
}
550556
}
@@ -842,6 +848,20 @@ impl AnvilNodeConfig {
842848
self.silent = silent;
843849
self
844850
}
851+
852+
/// Sets the fork url
853+
#[must_use]
854+
pub fn with_fork_url(mut self, fork_url: Option<String>) -> Self {
855+
self.fork_url = fork_url;
856+
self
857+
}
858+
859+
/// Sets the fork block
860+
#[must_use]
861+
pub fn with_fork_block_hash(mut self, fork_block_hash: Option<String>) -> Self {
862+
self.fork_block_hash = fork_block_hash;
863+
self
864+
}
845865
}
846866

847867
/// Can create dev accounts

crates/anvil-polkadot/src/substrate_node/service/consensus.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
2+
use crate::{
3+
substrate_node::{
4+
service::{AuxStore, ProvideRuntimeApi, UsageProvider, AuraApi, AuthorityId, TIMESTAMP}
5+
},
6+
};
17
use polkadot_sdk::{
28
sc_consensus::BlockImportParams,
3-
sc_consensus_aura::CompatibleDigestItem,
9+
sc_consensus_aura::{self,CompatibleDigestItem},
410
sc_consensus_manual_seal::{ConsensusDataProvider, Error},
511
sp_consensus_aura::ed25519::AuthoritySignature,
612
sp_consensus_babe::Slot,
713
sp_inherents::InherentData,
814
sp_runtime::{Digest, DigestItem, traits::Block as BlockT},
15+
sp_timestamp::TimestampInherentData,
916
};
1017
use std::marker::PhantomData;
18+
use std::sync::atomic::{ Ordering};
19+
use std::sync::Arc;
1120

1221
/// Consensus data provider for Aura. This will always use slot 0 (used to determine the
1322
/// index of the AURA authority from the authorities set by AURA runtimes) for the aura
@@ -55,3 +64,74 @@ where
5564
Ok(())
5665
}
5766
}
67+
68+
// Mine /// Consensus data provider for Aura. This allows to use manual-seal driven nodes to author valid
69+
/// AURA blocks. It will inspect incoming [`InherentData`] and look for included timestamps. Based
70+
/// on these timestamps, the [`AuraConsensusDataProvider`] will emit fitting digest items.
71+
pub struct AuraConsensusDataProvider<B, P> {
72+
// slot duration
73+
slot_duration: sc_consensus_aura::SlotDuration,
74+
// phantom data for required generics
75+
_phantom: PhantomData<(B, P)>,
76+
}
77+
78+
impl<B, P> AuraConsensusDataProvider<B, P>
79+
where
80+
B: BlockT,
81+
{
82+
/// Creates a new instance of the [`AuraConsensusDataProvider`], requires that `client`
83+
/// implements [`sp_consensus_aura::AuraApi`]
84+
pub fn new<C>(client: Arc<C>) -> Self
85+
where
86+
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
87+
C::Api: AuraApi<B, AuthorityId>,
88+
{
89+
let slot_duration = sc_consensus_aura::slot_duration(&*client)
90+
.expect("slot_duration is always present; qed.");
91+
92+
Self { slot_duration, _phantom: PhantomData }
93+
}
94+
95+
/// Creates a new instance of the [`AuraConsensusDataProvider`]
96+
pub fn new_with_slot_duration(slot_duration: sc_consensus_aura::SlotDuration) -> Self {
97+
Self { slot_duration, _phantom: PhantomData }
98+
}
99+
}
100+
101+
impl<B, P> ConsensusDataProvider<B> for AuraConsensusDataProvider<B, P>
102+
where
103+
B: BlockT,
104+
P: Send + Sync,
105+
{
106+
type Proof = P;
107+
108+
fn create_digest(
109+
&self,
110+
_parent: &B::Header,
111+
inherents: &InherentData,
112+
) -> Result<Digest, Error> {
113+
let timestamp =
114+
inherents.timestamp_inherent_data()?.expect("Timestamp is always present; qed");
115+
116+
print!("time da {}", timestamp);
117+
print!("time db {}", TIMESTAMP.load(Ordering::SeqCst));
118+
119+
// we always calculate the new slot number based on the current time-stamp and the slot
120+
// duration.
121+
let digest_item = <DigestItem as CompatibleDigestItem<AuthoritySignature>>::aura_pre_digest(
122+
Slot::from_timestamp(timestamp, self.slot_duration),
123+
);
124+
125+
Ok(Digest { logs: vec![digest_item] })
126+
}
127+
128+
fn append_block_import(
129+
&self,
130+
_parent: &B::Header,
131+
_params: &mut BlockImportParams<B>,
132+
_inherents: &InherentData,
133+
_proof: Self::Proof,
134+
) -> Result<(), Error> {
135+
Ok(())
136+
}
137+
}

crates/anvil-polkadot/src/substrate_node/service/executor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ use polkadot_sdk::{
1616
sp_storage::ChildInfo,
1717
sp_version,
1818
sp_wasm_interface::ExtendedHostFunctions,
19+
cumulus_client_service::ParachainHostFunctions,
1920
};
2021
use std::{cell::RefCell, sync::Arc};
2122

2223
/// Wasm executor which overrides the signature checking host functions for impersonation.
2324
pub type WasmExecutor = sc_executor::WasmExecutor<
2425
ExtendedHostFunctions<
25-
ExtendedHostFunctions<sp_io::SubstrateHostFunctions, SenderAddressRecoveryOverride>,
26+
ExtendedHostFunctions<ParachainHostFunctions, SenderAddressRecoveryOverride>,
2627
PublicKeyToHashOverride,
2728
>,
2829
>;

0 commit comments

Comments
 (0)