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

New Feature: "Contracts" => enable/disable contract integration tests #80

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion contract-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ edition = "2021"
[dependencies]
ethers = "2.0.10"
lightclient-circuits = { workspace = true }
contracts = { path = "../contracts", optional = true }

[features]
contracts = ["dep:contracts"]


[dev-dependencies]
rstest = "0.18.2"
Expand All @@ -19,4 +24,3 @@ eth-types = { workspace = true }
ssz_rs = { workspace = true }
halo2-base = { workspace = true }
snark-verifier-sdk = { workspace = true }
contracts = { workspace = true }
38 changes: 22 additions & 16 deletions contract-tests/tests/spectre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@
* These are the highest level integration tests for the Spectre protocol
* They treat the Spectre contract as an ethereum light-client and test against the spec
*/
use std::path::PathBuf;
use std::sync::Arc;

use contract_tests::make_client;
use contracts::{MockVerifier, Spectre};
use eth_types::{Minimal, LIMB_BITS};
use ethers::core::types::U256;
use ethers::providers::Middleware;
use halo2_base::halo2_proofs::halo2curves::bn256::Fr;
use lightclient_circuits::sync_step_circuit::StepCircuit;
use rstest::rstest;
use test_utils::{get_initial_sync_committee_poseidon, read_test_files_and_gen_witness};
#[cfg(feature = "contracts")]
mod contract_integration {
use std::path::PathBuf;
use std::sync::Arc;

const SLOTS_PER_EPOCH: usize = 8;
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: usize = 8;
const SLOTS_PER_SYNC_COMMITTEE_PERIOD: usize = EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH;
const FINALITY_THRESHOLD: usize = 20; // ~ 2/3 of 32
use contract_tests::make_client;
use contracts::{MockVerifier, Spectre};
use eth_types::{Minimal, LIMB_BITS};
use ethers::core::types::U256;
use ethers::providers::Middleware;
use halo2_base::halo2_proofs::halo2curves::bn256::Fr;
use lightclient_circuits::sync_step_circuit::StepCircuit;
use rstest::rstest;
use test_utils::{get_initial_sync_committee_poseidon, read_test_files_and_gen_witness};
const SLOTS_PER_EPOCH: usize = 8;
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: usize = 8;
const SLOTS_PER_SYNC_COMMITTEE_PERIOD: usize =
EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH;
const FINALITY_THRESHOLD: usize = 20; // ~ 2/3 of 32
}

#[tokio::test]
#[cfg(feature = "contracts")]
async fn test_deploy_spectre() -> anyhow::Result<()> {
let (_anvil_instance, ethclient) = make_client();
let _contract = deploy_spectre_mock_verifiers(ethclient, 0, U256::zero(), 0).await?;
Expand All @@ -33,6 +38,7 @@ async fn test_deploy_spectre() -> anyhow::Result<()> {

#[rstest]
#[tokio::test]
#[cfg(feature = "contracts")]
async fn test_contract_initialization_and_first_step(
#[files("../consensus-spec-tests/tests/minimal/capella/light_client/sync/pyspec_tests/**")]
#[exclude("deneb*")]
Expand Down Expand Up @@ -77,7 +83,6 @@ async fn test_contract_initialization_and_first_step(
contract.execution_payload_roots(head).call().await?,
step_input.execution_payload_root
);

Ok(())
}

Expand All @@ -86,6 +91,7 @@ async fn test_contract_initialization_and_first_step(
/// Deploy the Spectre contract using the given ethclient
/// Also deploys the step verifier and the update verifier contracts
/// and passes their addresses along with the other params to the constructor
#[cfg(feature = "contracts")]
async fn deploy_spectre_mock_verifiers<M: Middleware + 'static>(
ethclient: Arc<M>,
initial_sync_period: usize,
Expand Down
26 changes: 16 additions & 10 deletions contract-tests/tests/step_input_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
// Code: https://github.com/ChainSafe/Spectre
// SPDX-License-Identifier: LGPL-3.0-only

use std::ops::Deref;
use std::path::PathBuf;
#[cfg(feature = "contracts")]
mod contract_integration {
use std::ops::Deref;
use std::path::PathBuf;

use contract_tests::make_client;
use eth_types::{Minimal, LIMB_BITS};
use ethers::contract::abigen;
use lightclient_circuits::halo2_proofs::halo2curves::bn256;
use lightclient_circuits::witness::SyncStepArgs;
use rstest::rstest;
use ssz_rs::Merkleized;
use test_utils::read_test_files_and_gen_witness;
use contract_tests::make_client;
use eth_types::{Minimal, LIMB_BITS};
use ethers::contract::abigen;
use lightclient_circuits::halo2_proofs::halo2curves::bn256;
use lightclient_circuits::witness::SyncStepArgs;
use rstest::rstest;
use ssz_rs::Merkleized;
use test_utils::read_test_files_and_gen_witness;
}

#[cfg(feature = "contracts")]
abigen!(
StepExternal,
"../contracts/out/StepExternal.sol/StepExternal.json";
);

#[cfg(feature = "contracts")]
// SyncStepInput type produced by abigen macro matches the solidity struct type
impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for StepInput {
fn from(args: SyncStepArgs<Spec>) -> Self {
Expand Down Expand Up @@ -51,6 +56,7 @@ impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for StepInput {

#[rstest]
#[tokio::test]
#[cfg(feature = "contracts")]
async fn test_step_instance_commitment_evm_equivalence(
#[files("../consensus-spec-tests/tests/minimal/capella/light_client/sync/pyspec_tests/**")]
#[exclude("deneb*")]
Expand Down
1 change: 0 additions & 1 deletion contracts
Submodule contracts deleted from a430ca
8 changes: 8 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "contracts"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
4 changes: 4 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Running smart contract integration tests
When running the Spectre smart contract integration tests, replace this crate with [this crate](https://github.com/ChainSafe/spectre-contracts/tree/a430caeb3678582b73e3ee73b6f001bd9d1e75ca)

Additionally make sure to enable the `contracts` feature when running tests against the real contracts crate.
11 changes: 11 additions & 0 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
As a user looking to run contract integration tests it is recommended
that you delete this crate (the 'contracts' crate) and replace it with
the contracts crate found here:

https://github.com/ChainSafe/spectre-contracts/tree/a430caeb3678582b73e3ee73b6f001bd9d1e75ca

To run the contract integration tests afterwards, run:

cargo test -- --features contracts
*/
3 changes: 2 additions & 1 deletion lightclient-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pse-poseidon = { git = "https://github.com/axiom-crypto/pse-poseidon.git" }

# ethereum
ssz_rs = { workspace = true, features = ["serde"] }
ethereum-consensus-types ={ workspace = true, features = ["serde"] }
ethereum-consensus-types = { workspace = true, features = ["serde"] }

# local
eth-types.workspace = true
Expand All @@ -46,6 +46,7 @@ rand = "0.8"
lazy_static = "1.4"
getset = "0.1.2"
rand_chacha = "0.3.0"
bincode = "1.3.3"

[dev-dependencies]
rstest = "0.18.2"
Expand Down
2 changes: 1 addition & 1 deletion lightclient-circuits/config/committee_update_20.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"num_fixed": 1,
"num_lookup_advice_per_phase": [
1,
0,
0,
0
],
Expand Down
Loading