Skip to content

Commit

Permalink
Beefy Prover: Remove generated subxt types (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege authored Sep 17, 2024
1 parent c1c436d commit 3170047
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 48,311 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion evm/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ismp = { workspace = true, default-features = true }
pallet-ismp = { workspace = true, default-features = true }
pallet-ismp-host-executive = { workspace = true, default-features = true }
pallet-mmr = { workspace = true, default-features = true }
beefy-prover = { workspace = true, default-features = true }
beefy-prover = { workspace = true, default-features = true, features = ["local"]}
beefy-verifier-primitives = { workspace = true, default-features = true }
pallet-ismp-relayer = { workspace = true, default-features = true }

Expand Down
6 changes: 3 additions & 3 deletions evm/integration-tests/src/tests/beefy_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ impl subxt::Config for HyperbridgeConfig {
}

fn default_para_id() -> u32 {
4009
2000
}
fn activation_block() -> u32 {
0
}
fn default_relay_ws_url() -> String {
"ws://127.0.0.1:9944".to_string()
"ws://127.0.0.1:9922".to_string()
}
fn default_para_ws_url() -> String {
"ws://127.0.0.1:9988".to_string()
"ws://127.0.0.1:9990".to_string()
}
#[derive(Deserialize, Debug)]
struct Config {
Expand Down
3 changes: 3 additions & 0 deletions modules/consensus/beefy/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ primitive-types = { workspace = true, features = ["codec"] }
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"] }
derive_more = { version = "0.99.17", features = ["from"] }
rs_merkle = { git = "https://github.com/polytope-labs/rs-merkle", branch = "seun/2d-merkle-proofs" }
hex-literal = "0.4.1"

# substrate
sp-runtime = { workspace = true, features = ["default"] }
Expand All @@ -35,3 +36,5 @@ serde_json = { version = "1.0.74" }
hex = { version = "0.4.3" }
beefy-verifier-primitives = { path = "../primitives" }

[features]
local = []
87 changes: 51 additions & 36 deletions modules/consensus/beefy/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

/// Methods for querying the relay chain
pub mod relay;
/// Metadata generated code for interacting with the relay chain
pub mod runtime;
/// Helper functions and types
pub mod util;

Expand All @@ -37,8 +35,9 @@ use beefy_verifier_primitives::{
ConsensusMessage, ConsensusState, MmrProof, ParachainHeader, ParachainProof, SignedCommitment,
};
use codec::{Decode, Encode};
use hex_literal::hex;
use primitive_types::H256;
use relay::{fetch_latest_beefy_justification, fetch_mmr_proof};
use relay::{fetch_latest_beefy_justification, fetch_mmr_proof, parachain_header_storage_key};
use sp_consensus_beefy::{
ecdsa_crypto::Signature,
known_payloads::MMR_ROOT_ID,
Expand All @@ -62,6 +61,32 @@ pub struct Prover<R: Config, P: Config> {
pub para_ids: Vec<u32>,
}

#[cfg(not(feature = "local"))]
/// Relay chain storage key for beefMmrLeaf.beefyNextAuthorites()
pub const BEEFY_MMR_LEAF_BEEFY_NEXT_AUTHORITIES: [u8; 32] =
hex!("2ecf93be7260df120a495bd3855c0e600c98535b82c72faf3c64974094af4643");
#[cfg(not(feature = "local"))]
/// Relay chain storage key for beefMmrLeaf.beefyAuthorites()
pub const BEEFY_MMR_LEAF_BEEFY_AUTHORITIES: [u8; 32] =
hex!("2ecf93be7260df120a495bd3855c0e60c52aa943bf0908860a3eea0fad707cdc");
#[cfg(feature = "local")]
/// Relay chain storage key for beefMmrLeaf.beefyNextAuthorites()
pub const BEEFY_MMR_LEAF_BEEFY_NEXT_AUTHORITIES: [u8; 32] =
hex!("da7d4185f8093e80caceb64da45219e30c98535b82c72faf3c64974094af4643");
#[cfg(feature = "local")]
/// Relay chain storage key for beefMmrLeaf.beefyAuthorites()
pub const BEEFY_MMR_LEAF_BEEFY_AUTHORITIES: [u8; 32] =
hex!("da7d4185f8093e80caceb64da45219e3c52aa943bf0908860a3eea0fad707cdc");
/// Relay chain storage key for beefy.authorities()
pub const BEEFY_AUTHORITIES: [u8; 32] =
hex!("08c41974a97dbf15cfbec28365bea2da5e0621c4869aa60c02be9adcc98a0d1d");
/// Relay chain storage key for beefy.validatorSetId()
pub const BEEFY_VALIDATOR_SET_ID: [u8; 32] =
hex!("08c41974a97dbf15cfbec28365bea2da8f05bccc2f70ec66a32999c5761156be");
/// Relay chain storage key for paras.parachains()
pub const PARAS_PARACHAINS: [u8; 32] =
hex!("cd710b30bd2eab0352ddcc26417aa1940b76934f4cc08dee01012d059e1b83ee");

impl<R: Config, P: Config> Prover<R, P> {
/// Construct a beefy client state to be submitted to the counterparty chain
pub async fn get_initial_consensus_state(&self) -> Result<ConsensusState, anyhow::Error> {
Expand All @@ -72,35 +97,28 @@ impl<R: Config, P: Config> Prover<R, P> {

// Encoding and decoding to fix dependency version conflicts
let next_authority_set = {
let key = runtime::storage().beefy_mmr_leaf().beefy_next_authorities();
let next_authority_set = self
.relay
.storage()
.at(latest_beefy_finalized)
.fetch(&key)
.rpc()
.storage(
BEEFY_MMR_LEAF_BEEFY_NEXT_AUTHORITIES.as_slice(),
Some(latest_beefy_finalized),
)
.await?
.expect("Should retrieve next authority set")
.encode();
.0;
BeefyNextAuthoritySet::decode(&mut &*next_authority_set)
.expect("Should decode next authority set correctly")
};

let current_authority_set = {
let key: subxt::storage::Address<
subxt::utils::Static<subxt::utils::Encoded>,
runtime::runtime_types::sp_consensus_beefy::mmr::BeefyAuthoritySet<H256>,
subxt::storage::address::Yes,
subxt::storage::address::Yes,
(),
> = runtime::storage().beefy_mmr_leaf().beefy_authorities();
let authority_set = self
.relay
.storage()
.at(latest_beefy_finalized)
.fetch(&key)
.rpc()
.storage(BEEFY_MMR_LEAF_BEEFY_AUTHORITIES.as_slice(), Some(latest_beefy_finalized))
.await?
.expect("Should retrieve next authority set")
.encode();
.0;
BeefyNextAuthoritySet::decode(&mut &*authority_set)
.expect("Should decode next authority set correctly")
};
Expand Down Expand Up @@ -138,14 +156,13 @@ impl<R: Config, P: Config> Prover<R, P> {
.ok_or_else(|| anyhow!("Failed to query blockhash for blocknumber"))?;

let current_authorities = {
let key = runtime::storage().beefy().authorities();
self.relay
.storage()
.at(block_hash)
.fetch(&key)
.rpc()
.storage(BEEFY_AUTHORITIES.as_slice(), Some(block_hash))
.await?
.map(|data| Vec::<[u8; 33]>::decode(&mut data.as_ref()))
.transpose()?
.ok_or_else(|| anyhow!("No beefy authorities found!"))?
.0
};

// Current LeafIndex
Expand Down Expand Up @@ -173,29 +190,27 @@ impl<R: Config, P: Config> Prover<R, P> {
};

let heads = {
let key = runtime::storage().paras().parachains();
let ids = self
.relay
.storage()
.at(block_hash)
.fetch(&key)
.rpc()
.storage(PARAS_PARACHAINS.as_slice(), Some(block_hash))
.await?
.map(|data| Vec::<u32>::decode(&mut data.as_ref()))
.transpose()?
.ok_or_else(|| anyhow!("No beefy authorities found!"))?;

let mut heads = vec![];
for id in ids {
let key = runtime::storage().paras().heads(&id);
let head = self
.relay
.storage()
.at(block_hash)
.fetch(&key)
.rpc()
.storage(parachain_header_storage_key(id).as_ref(), Some(block_hash))
.await?
.ok_or_else(|| anyhow!("No beefy authorities found!"))?
.0;
heads.push((id.0, head));
.map(|data| Vec::<u8>::decode(&mut data.as_ref()))
.transpose()?
.ok_or_else(|| anyhow!("No beefy authorities found!"))?;
heads.push((id, head));
}

heads.sort();

heads
Expand Down
9 changes: 6 additions & 3 deletions modules/consensus/beefy/prover/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use subxt::{
Config, OnlineClient,
};

use crate::BEEFY_VALIDATOR_SET_ID;

/// This contains the leaf indices of the relay chain blocks and a map of relay chain heights to a
/// map of all parachain headers at those heights Used for generating [`ParaHeadsProof`]
pub struct FinalizedParaHeads {
Expand Down Expand Up @@ -81,10 +83,11 @@ pub async fn fetch_next_beefy_justification<T: Config>(

let (signed_commitment, latest_beefy_finalized) = loop {
let set_id = client
.storage()
.at(block_hash)
.fetch(&crate::runtime::storage().beefy().validator_set_id())
.rpc()
.storage(BEEFY_VALIDATOR_SET_ID.as_slice(), Some(block_hash))
.await?
.map(|data| u64::decode(&mut data.as_ref()))
.transpose()?
.ok_or_else(|| anyhow!("Couldn't fetch latest beefy authority set"))?;

let block = client
Expand Down
2 changes: 0 additions & 2 deletions modules/consensus/beefy/prover/src/runtime.rs

This file was deleted.

Loading

0 comments on commit 3170047

Please sign in to comment.