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

More fixes #348

Merged
merged 9 commits into from
Dec 10, 2024
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
25 changes: 13 additions & 12 deletions Cargo.lock

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

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,26 @@ grandpa-prover = { path = "./modules/consensus/grandpa/prover" }

# consensus clients
ismp-bsc = { path = "./modules/ismp/clients/bsc", default-features = false }
ismp-grandpa = { version = "1.15.1", path = "./modules/ismp/clients/grandpa", default-features = false }
ismp-parachain = { version = "1.15.1", path = "./modules/ismp/clients/parachain/client", default-features = false }
ismp-parachain-inherent = { version = "1.15.1", path = "./modules/ismp/clients/parachain/inherent" }
ismp-grandpa = { version = "1.15.3", path = "./modules/ismp/clients/grandpa", default-features = false }
ismp-parachain = { version = "1.15.3", path = "./modules/ismp/clients/parachain/client", default-features = false }
ismp-parachain-inherent = { version = "1.15.3", path = "./modules/ismp/clients/parachain/inherent" }
ismp-parachain-runtime-api = { version = "1.15.1", path = "./modules/ismp/clients/parachain/runtime-api", default-features = false }
ismp-sync-committee = { path = "./modules/ismp/clients/sync-committee", default-features = false }
arbitrum-verifier = { path = "./modules/ismp/clients/arbitrum", default-features = false }
op-verifier = { path = "./modules/ismp/clients/optimism", default-features = false }

# state machine clients
evm-state-machine = { path = "./modules/ismp/state-machines/evm", default-features = false }
substrate-state-machine = { version = "1.15.1", path = "modules/ismp/state-machines/substrate", default-features = false }
substrate-state-machine = { version = "1.15.3", path = "modules/ismp/state-machines/substrate", default-features = false }
hyperbridge-client-machine = { path = "modules/ismp/state-machines/hyperbridge", default-features = false }

# published pallets
pallet-ismp = { version = "1.15.2", path = "modules/ismp/pallets/pallet", default-features = false }
pallet-ismp-rpc = { version = "1.15.2", path = "modules/ismp/pallets/rpc" }
pallet-ismp-runtime-api = { version = "1.15.2", path = "modules/ismp/pallets/runtime-api", default-features = false }
pallet-hyperbridge = { version = "1.15.2", path = "modules/ismp/pallets/hyperbridge", default-features = false }
pallet-token-gateway = { version = "1.15.2", path = "modules/ismp/pallets/token-gateway", default-features = false }
token-gateway-primitives = { version = "1.15.2", path = "modules/ismp/pallets/token-gateway/primitives", default-features = false }
pallet-ismp = { version = "1.15.3", path = "modules/ismp/pallets/pallet", default-features = false }
pallet-ismp-rpc = { version = "1.15.3", path = "modules/ismp/pallets/rpc" }
pallet-ismp-runtime-api = { version = "1.15.3", path = "modules/ismp/pallets/runtime-api", default-features = false }
pallet-hyperbridge = { version = "1.15.3", path = "modules/ismp/pallets/hyperbridge", default-features = false }
pallet-token-gateway = { version = "1.15.3", path = "modules/ismp/pallets/token-gateway", default-features = false }
token-gateway-primitives = { version = "1.15.3", path = "modules/ismp/pallets/token-gateway/primitives", default-features = false }

# ismp modules
pallet-fishermen = { path = "modules/ismp/pallets/fishermen", default-features = false }
Expand All @@ -321,10 +321,10 @@ pallet-mmr = { path = "modules/trees/mmr/pallet", default-features = false }
pallet-token-gateway-inspector = { path = "modules/ismp/pallets/token-gateway-inspector", default-features = false }

# merkle trees
ethereum-triedb = { version = "0.1.1", path = "./modules/trees/ethereum", default-features = false }
mmr-primitives = { path = "modules/trees/mmr/primitives", default-features = false }
pallet-mmr-runtime-api = { path = "modules/trees/mmr/pallet/runtime-api", default-features = false }
mmr-gadget = { path = "modules/trees/mmr/gadget" }
ethereum-triedb = { version = "0.1.1", path = "./modules/trees/ethereum", default-features = false }
mmr-primitives = { version = "1.15.1", path = "modules/trees/mmr/primitives", default-features = false }

# runtimes
gargantua-runtime = { path = "./parachain/runtimes/gargantua", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/developers/polkadot/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ cargo add pallet-token-gateway

## Versioning

The versioning of these crates track the LTS releases of Polkadot SDK and are only updated when new LTS versions are released. For instance the `stable2407` version is available for all crates as **v1.15.x**. Subsequent LTS releases will see a minor version bump. eg `stable2409` will be supportted by **v1.16.x** and so on.
The versioning of these crates track the LTS releases of Polkadot SDK and are only updated when new LTS versions are released. For instance the `stable2407` version is available for all crates as **v1.15.x**. Subsequent LTS releases will see a minor version bump. eg `stable2409` will be supported by **v1.16.x** and so on.
6 changes: 4 additions & 2 deletions docs/pages/developers/polkadot/pallet-ismp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ impl pallet_ismp::Config for Runtime {
ismp_parachain::ParachainConsensusClient<Runtime, IsmpParachain>,
);
// Offchain database implementation. Outgoing requests and responses are
// inserted in this database, while their commitments are stored onchain.
type OffchainDB = TransparentOffchainDB;
// inserted in this database, while their commitments are stored onchain.
//
// The default implementation for `()` should suffice
type OffchainDB = ();
// Weight provider for local modules
type WeightProvider = ();
}
Expand Down
58 changes: 37 additions & 21 deletions modules/consensus/beefy/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ pub mod relay;
/// Helper functions and types
pub mod util;

/// Some consensus related constants
pub mod constants {

/// Block at which BEEFY was activated on rococo
pub const ROCOCO_BEEFY_ACTIVATION_BLOCK: u32 = 3_804_028;
}

use anyhow::anyhow;
use beefy_verifier_primitives::{
ConsensusMessage, ConsensusState, MmrProof, ParachainHeader, ParachainProof, SignedCommitment,
ConsensusMessage, ConsensusState, MmrProof, ParachainHeader, ParachainProof,
SignatureWithAuthorityIndex, SignedCommitment,
};
use codec::{Decode, Encode};
use hex_literal::hex;
Expand All @@ -46,7 +40,7 @@ use sp_consensus_beefy::{
use sp_io::hashing::keccak_256;
use sp_mmr_primitives::LeafProof;
use subxt::{rpc_params, Config, OnlineClient};
use util::{hash_authority_addresses, prove_authority_set, AuthorityProofWithSignatures};
use util::hash_authority_addresses;

/// This contains methods for fetching BEEFY proofs for parachain headers.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -208,30 +202,49 @@ impl<R: Config, P: Config> Prover<R, P> {
&self,
signed_commitment: sp_consensus_beefy::SignedCommitment<u32, Signature>,
) -> Result<ConsensusMessage, anyhow::Error> {
let subxt_block_number: subxt::rpc::types::BlockNumber =
(signed_commitment.commitment.block_number - 1).into();
let block_number: subxt::rpc::types::NumberOrHex =
signed_commitment.commitment.block_number.into();
let block_hash = self
.relay
.rpc()
.block_hash(Some(subxt_block_number))
.block_hash(Some(block_number.into()))
.await?
.ok_or_else(|| anyhow!("Failed to query blockhash for blocknumber"))?;

let current_authorities = self.beefy_authorities(Some(block_hash)).await?;

// Current LeafIndex
let block_number = signed_commitment.commitment.block_number;
let leaf_proof = fetch_mmr_proof(&self.relay, block_number.into()).await?;
let leaf_proof = fetch_mmr_proof(&self.relay, block_number.try_into()?).await?;
let leaves: Vec<Vec<u8>> = codec::Decode::decode(&mut &*leaf_proof.leaves.0)?;
let latest_leaf: MmrLeaf<u32, H256, H256, H256> = codec::Decode::decode(&mut &*leaves[0])?;
let mmr_proof: LeafProof<H256> = Decode::decode(&mut &*leaf_proof.proof.0)?;

// create authorities proof
let signatures = signed_commitment
.signatures
.iter()
.enumerate()
.map(|(index, x)| {
if let Some(sig) = x {
let mut temp = [0u8; 65];
if sig.len() == 65 {
temp.copy_from_slice(&*sig.encode());
let last = temp.last_mut().unwrap();
*last = *last + 27;
Some(SignatureWithAuthorityIndex { index: index as u32, signature: temp })
} else {
None
}
} else {
None
}
})
.filter_map(|x| x)
.collect::<Vec<_>>();
let current_authorities = self.beefy_authorities(Some(block_hash)).await?;
let authority_address_hashes = hash_authority_addresses(
current_authorities.into_iter().map(|x| x.encode()).collect(),
)?;

let AuthorityProofWithSignatures { authority_proof, signatures } =
prove_authority_set(&signed_commitment, authority_address_hashes)?;
let indices = signatures.iter().map(|x| x.index as usize).collect::<Vec<_>>();
let authority_proof = util::merkle_proof(&authority_address_hashes, &indices);

let mmr = MmrProof {
signed_commitment: SignedCommitment {
Expand All @@ -243,8 +256,11 @@ impl<R: Config, P: Config> Prover<R, P> {
authority_proof,
};

let heads = self.paras_parachains(Some(block_hash)).await?;

let heads = self
.paras_parachains(Some(R::Hash::decode(
&mut &*latest_leaf.parent_number_and_hash.1.encode(),
)?))
.await?;
let (parachains, indices): (Vec<_>, Vec<_>) = self
.para_ids
.iter()
Expand Down
36 changes: 0 additions & 36 deletions modules/consensus/beefy/prover/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,42 +106,6 @@ pub async fn fetch_timestamp_extrinsic_with_proof<T: Config>(
Ok(TimeStampExtWithProof { ext, proof })
}

/// Get the proof for authority set that signed this commitment
pub fn prove_authority_set(
signed_commitment: &sp_consensus_beefy::SignedCommitment<
u32,
sp_consensus_beefy::ecdsa_crypto::Signature,
>,
authority_address_hashes: Vec<Hash>,
) -> Result<AuthorityProofWithSignatures, anyhow::Error> {
let signatures = signed_commitment
.signatures
.iter()
.enumerate()
.map(|(index, x)| {
if let Some(sig) = x {
let mut temp = [0u8; 65];
if sig.len() == 65 {
temp.copy_from_slice(&*sig.encode());
let last = temp.last_mut().unwrap();
*last = *last + 27;
Some(SignatureWithAuthorityIndex { index: index as u32, signature: temp })
} else {
None
}
} else {
None
}
})
.filter_map(|x| x)
.collect::<Vec<_>>();

let signature_indices = signatures.iter().map(|x| x.index as usize).collect::<Vec<_>>();
let authority_proof = merkle_proof(&authority_address_hashes, &signature_indices);

Ok(AuthorityProofWithSignatures { authority_proof, signatures })
}

/// Hash encoded authority public keys
pub fn hash_authority_addresses(
encoded_public_keys: Vec<Vec<u8>>,
Expand Down
28 changes: 14 additions & 14 deletions modules/consensus/sync-committee/prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod middleware;
pub mod middleware;
#[warn(unused_imports)]
#[warn(unused_variables)]
mod responses;
mod routes;
pub mod responses;
pub mod routes;

#[cfg(test)]
mod test;
Expand Down Expand Up @@ -251,8 +251,8 @@ impl<C: Config, const ETH1_DATA_VOTES_BOUND: usize> SyncCommitteeProver<C, ETH1_
finality_checkpoint: Checkpoint,
latest_block_id: Option<&str>,
) -> Result<Option<VerifierStateUpdate>, anyhow::Error> {
if finality_checkpoint.root == Node::default() ||
client_state.latest_finalized_epoch >= finality_checkpoint.epoch
if finality_checkpoint.root == Node::default()
|| client_state.latest_finalized_epoch >= finality_checkpoint.epoch
{
trace!(target: "sync-committee-prover", "No new epoch finalized yet {}", finality_checkpoint.epoch);
return Ok(None);
Expand Down Expand Up @@ -287,9 +287,9 @@ impl<C: Config, const ETH1_DATA_VOTES_BOUND: usize> SyncCommitteeProver<C, ETH1_

let signature_period = compute_sync_committee_period_at_slot::<C>(block.slot);

if num_signatures >= min_signatures &&
(state_period..=state_period + 1).contains(&signature_period) &&
parent_block_finality_checkpoint.epoch > client_state.latest_finalized_epoch
if num_signatures >= min_signatures
&& (state_period..=state_period + 1).contains(&signature_period)
&& parent_block_finality_checkpoint.epoch > client_state.latest_finalized_epoch
{
break;
}
Expand Down Expand Up @@ -354,10 +354,10 @@ impl<C: Config, const ETH1_DATA_VOTES_BOUND: usize> SyncCommitteeProver<C, ETH1_
period: u64,
) -> Result<VerifierStateUpdate, anyhow::Error> {
trace!(target: "sync-committee-prover", "latest_update_for_period {period}");
let mut higest_slot_in_epoch = ((period * C::EPOCHS_PER_SYNC_COMMITTEE_PERIOD) *
C::SLOTS_PER_EPOCH) +
(C::EPOCHS_PER_SYNC_COMMITTEE_PERIOD * C::SLOTS_PER_EPOCH) -
1;
let mut higest_slot_in_epoch = ((period * C::EPOCHS_PER_SYNC_COMMITTEE_PERIOD)
* C::SLOTS_PER_EPOCH)
+ (C::EPOCHS_PER_SYNC_COMMITTEE_PERIOD * C::SLOTS_PER_EPOCH)
- 1;
let mut count = 0;
// Some slots are empty so we'll use a loop to fetch the highest available slot in an epoch
let mut block = loop {
Expand Down Expand Up @@ -494,8 +494,8 @@ pub fn prove_block_roots_proof<C: Config, const ETH1_DATA_VOTES_BOUND: usize>(
let epoch_for_header = compute_epoch_at_slot::<C>(header.slot) as usize;
let epoch_for_state = compute_epoch_at_slot::<C>(state.slot) as usize;

if epoch_for_state.saturating_sub(epoch_for_header) >=
SLOTS_PER_HISTORICAL_ROOT / C::SLOTS_PER_EPOCH as usize
if epoch_for_state.saturating_sub(epoch_for_header)
>= SLOTS_PER_HISTORICAL_ROOT / C::SLOTS_PER_EPOCH as usize
{
// todo: Historical root proofs
unimplemented!()
Expand Down
Loading
Loading