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

BEEFY: Replace barretenberg with SP1 #319

Merged
merged 13 commits into from
Oct 3, 2024
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "evm/lib/solidity-stringutils"]
path = evm/lib/solidity-stringutils
url = https://github.com/Arachnid/solidity-stringutils
[submodule "evm/lib/sp1-contracts"]
path = evm/lib/sp1-contracts
url = https://github.com/succinctlabs/sp1-contracts
23 changes: 14 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,11 @@ memory-db = { version = "0.32.0", default-features = false }
codec = { version = "3.1.3", package = "parity-scale-codec", default-features = false }
log = { version = "0.4.21", default-features = false }
anyhow = { version = "1.0.71", default-features = false }
alloy-rlp = { version = "0.3.2", default-features = false }
alloy-rlp-derive = "0.3.2"
alloy-sol-macro = "0.7.4"
alloy-sol-types = { version = "0.7.4", default-features = false }
ethabi = { version = "18.0.0", features = [
"rlp",
"parity-codec",
], default-features = false }
alloy-rlp = { version = "0.3.7", default-features = false }
alloy-rlp-derive = "0.3.7"
alloy-sol-macro = "0.7.7"
alloy-sol-types = { version = "0.7.7", default-features = false }

orml-xcm-support = { version = "=1.0.0", default-features = false }
orml-traits = { version = "=1.0.0", default-features = false }
primitive-types = { version = "0.12.1", default-features = false }
Expand Down Expand Up @@ -329,6 +326,14 @@ telemetry-server = { path = "tesseract/telemetry" }
tesseract-config = { path = "tesseract/config" }
cumulus-pallet-parachain-system = { version = "0.16.0", default-features = false }

[workspace.dependencies.ethabi]
version = "18.0.0"
default-features = false
features = [
"rlp",
"parity-codec",
]

[workspace.dependencies.ethers]
git = "https://github.com/polytope-labs/ethers-rs"
rev = "45239225c50247e049892125d281442c084a2a92"
Expand All @@ -353,7 +358,7 @@ version = "0.5.2"
default-features = false

[workspace.dependencies.alloy-primitives]
version = "0.7.4"
version = "0.7.7"
default-features = false
features = ["rlp"]

Expand Down
1 change: 1 addition & 0 deletions evm/abi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() -> anyhow::Result<()> {
("EvmHost", format!("{base_dir}/out/EvmHost.sol/EvmHost.json")),
("Handler", format!("{base_dir}/out/HandlerV1.sol/HandlerV1.json")),
("Beefy", format!("{base_dir}/out/BeefyV1.sol/BeefyV1.json")),
("SP1Beefy", format!("{base_dir}/out/SP1Beefy.sol/SP1Beefy.json")),
("PingModule", format!("{base_dir}/out/PingModule.sol/PingModule.json")),
("HostManager", format!("{base_dir}/out/HostManager.sol/HostManager.json")),
("ERC20", format!("{base_dir}/out/ERC20.sol/ERC20.json")),
Expand Down
67 changes: 45 additions & 22 deletions evm/abi/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ use std::str::FromStr;

#[cfg(feature = "beefy")]
mod beefy {
use crate::beefy::{
AuthoritySetCommitment, BeefyConsensusProof, BeefyConsensusState, BeefyMmrLeaf, Commitment,
Node, Parachain, ParachainProof, Payload, RelayChainProof, SignedCommitment, Vote,
use crate::{
beefy::{
AuthoritySetCommitment, BeefyConsensusProof, BeefyConsensusState, BeefyMmrLeaf,
Commitment, Node, Parachain, ParachainProof, Payload, RelayChainProof,
SignedCommitment, Vote,
},
sp1_beefy::ParachainHeader,
};
use beefy_verifier_primitives::{ConsensusMessage, ConsensusState, MmrProof};
use merkle_mountain_range::{leaf_index_to_mmr_size, leaf_index_to_pos};
Expand Down Expand Up @@ -81,6 +85,43 @@ mod beefy {
}
}

type SpCommitment = sp_consensus_beefy::Commitment<u32>;
impl From<SpCommitment> for Commitment {
fn from(value: SpCommitment) -> Self {
Commitment {
payload: vec![Payload {
id: b"mh".clone(),
data: value.payload.get_raw(b"mh").unwrap().clone().into(),
}],
block_number: value.block_number.into(),
validator_set_id: value.validator_set_id.into(),
}
}
}

type SpMmrLeaf = sp_consensus_beefy::mmr::MmrLeaf<u32, H256, H256, H256>;

impl From<beefy_verifier_primitives::ParachainHeader> for ParachainHeader {
fn from(value: beefy_verifier_primitives::ParachainHeader) -> Self {
ParachainHeader { header: value.header.into(), id: value.para_id.into() }
}
}

// useful for Sp1Beefy verifier
impl From<SpMmrLeaf> for BeefyMmrLeaf {
fn from(value: SpMmrLeaf) -> Self {
BeefyMmrLeaf {
version: 0.into(),
parent_number: value.parent_number_and_hash.0.into(),
parent_hash: value.parent_number_and_hash.1.into(),
next_authority_set: value.beefy_next_authority_set.into(),
extra: value.leaf_extra.into(),
k_index: 0.into(),
leaf_index: 0.into(),
}
}
}

impl From<MmrProof> for RelayChainProof {
fn from(value: MmrProof) -> Self {
let leaf_index = value.mmr_proof.leaf_indices[0];
Expand All @@ -92,25 +133,7 @@ mod beefy {

RelayChainProof {
signed_commitment: SignedCommitment {
commitment: Commitment {
payload: vec![Payload {
id: b"mh".clone(),
data: value
.signed_commitment
.commitment
.payload
.get_raw(b"mh")
.unwrap()
.clone()
.into(),
}],
block_number: value.signed_commitment.commitment.block_number.into(),
validator_set_id: value
.signed_commitment
.commitment
.validator_set_id
.into(),
},
commitment: value.signed_commitment.commitment.into(),
votes: value
.signed_commitment
.signatures
Expand Down
112 changes: 19 additions & 93 deletions evm/abi/src/generated/beefy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,27 @@ pub mod beefy {
},
::ethers::core::abi::ethabi::Param {
name: ::std::string::String::new(),
kind: ::ethers::core::abi::ethabi::ParamType::Tuple(
::std::vec![
::ethers::core::abi::ethabi::ParamType::Uint(256usize),
::ethers::core::abi::ethabi::ParamType::Uint(256usize),
kind: ::ethers::core::abi::ethabi::ParamType::Array(
::std::boxed::Box::new(
::ethers::core::abi::ethabi::ParamType::Tuple(
::std::vec![
::ethers::core::abi::ethabi::ParamType::Uint(256usize),
::ethers::core::abi::ethabi::ParamType::FixedBytes(32usize),
::ethers::core::abi::ethabi::ParamType::FixedBytes(32usize),
::ethers::core::abi::ethabi::ParamType::Uint(256usize),
::ethers::core::abi::ethabi::ParamType::Tuple(
::std::vec![
::ethers::core::abi::ethabi::ParamType::Uint(256usize),
::ethers::core::abi::ethabi::ParamType::FixedBytes(32usize),
::ethers::core::abi::ethabi::ParamType::FixedBytes(32usize),
],
),
],
),
],
),
),
internal_type: ::core::option::Option::Some(
::std::borrow::ToOwned::to_owned("struct IntermediateState"),
::std::borrow::ToOwned::to_owned(
"struct IntermediateState[]",
),
),
},
],
Expand Down Expand Up @@ -516,7 +522,7 @@ pub mod beefy {
encoded_proof: ::ethers::core::types::Bytes,
) -> ::ethers::contract::builders::ContractCall<
M,
(::ethers::core::types::Bytes, IntermediateState),
(::ethers::core::types::Bytes, ::std::vec::Vec<IntermediateState>),
> {
self.0
.method_hash([125, 117, 85, 152], (encoded_state, encoded_proof))
Expand Down Expand Up @@ -1114,23 +1120,10 @@ pub mod beefy {
Eq,
Hash,
)]
pub struct VerifyConsensusReturn(pub ::ethers::core::types::Bytes, pub IntermediateState);
///`AuthoritySetCommitment(uint256,uint256,bytes32)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct AuthoritySetCommitment {
pub id: ::ethers::core::types::U256,
pub len: ::ethers::core::types::U256,
pub root: [u8; 32],
}
pub struct VerifyConsensusReturn(
pub ::ethers::core::types::Bytes,
pub ::std::vec::Vec<IntermediateState>,
);
///`BeefyConsensusProof(((((bytes2,bytes)[],uint256,uint256),(bytes,uint256)[]),(uint256,
/// uint256,bytes32,(uint256,uint256,bytes32),bytes32,uint256,uint256),bytes32[],(uint256,
/// bytes32)[]),((uint256,uint256,bytes),(uint256,bytes32)[]))`
Expand Down Expand Up @@ -1165,58 +1158,6 @@ pub mod beefy {
pub current_authority_set: AuthoritySetCommitment,
pub next_authority_set: AuthoritySetCommitment,
}
///`BeefyMmrLeaf(uint256,uint256,bytes32,(uint256,uint256,bytes32),bytes32,uint256,uint256)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct BeefyMmrLeaf {
pub version: ::ethers::core::types::U256,
pub parent_number: ::ethers::core::types::U256,
pub parent_hash: [u8; 32],
pub next_authority_set: AuthoritySetCommitment,
pub extra: [u8; 32],
pub k_index: ::ethers::core::types::U256,
pub leaf_index: ::ethers::core::types::U256,
}
///`Commitment((bytes2,bytes)[],uint256,uint256)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct Commitment {
pub payload: ::std::vec::Vec<Payload>,
pub block_number: ::ethers::core::types::U256,
pub validator_set_id: ::ethers::core::types::U256,
}
///`IntermediateState(uint256,uint256,(uint256,bytes32,bytes32))`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct IntermediateState {
pub state_machine_id: ::ethers::core::types::U256,
pub height: ::ethers::core::types::U256,
pub commitment: StateCommitment,
}
///`Node(uint256,bytes32)`
#[derive(
Clone,
Expand Down Expand Up @@ -1263,21 +1204,6 @@ pub mod beefy {
pub parachain: Parachain,
pub proof: ::std::vec::Vec<::std::vec::Vec<Node>>,
}
///`Payload(bytes2,bytes)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
::ethers::contract::EthAbiCodec,
Default,
Debug,
PartialEq,
Eq,
Hash,
)]
pub struct Payload {
pub id: [u8; 2],
pub data: ::ethers::core::types::Bytes,
}
///`RelayChainProof((((bytes2,bytes)[],uint256,uint256),(bytes,uint256)[]),(uint256,uint256,
/// bytes32,(uint256,uint256,bytes32),bytes32,uint256,uint256),bytes32[],(uint256,bytes32)[])`
#[derive(
Expand Down
Loading
Loading