This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(operator): light block refactor (#45)
* refactor: genesis refactored * refactor: refactored operator * refactor: update client * refactor: genesis * refactor: operator * refactor: verify membership fixture * rename * refactor: programs * refactor: vm * refactor: uc * refactor: genesis * refactor * imp: regen fixtures
- Loading branch information
Showing
14 changed files
with
240 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! Helpers for interacting with EVM. | ||
use std::env; | ||
|
||
use alloy::{network::EthereumWallet, signers::local::PrivateKeySigner}; | ||
|
||
/// Create an Ethereum wallet from the `PRIVATE_KEY` environment variable. | ||
/// | ||
/// # Panics | ||
/// Panics if the `PRIVATE_KEY` environment variable is not set. | ||
/// Panics if the `PRIVATE_KEY` environment variable is not a valid private key. | ||
#[must_use] | ||
pub fn wallet_from_env() -> EthereumWallet { | ||
let mut private_key = env::var("PRIVATE_KEY").expect("PRIVATE_KEY not set"); | ||
if let Some(stripped) = private_key.strip_prefix("0x") { | ||
private_key = stripped.to_string(); | ||
} | ||
|
||
let signer: PrivateKeySigner = private_key.parse().unwrap(); | ||
EthereumWallet::from(signer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! Provides helpers for deriving other types from `LightBlock`. | ||
use ibc_client_tendermint::types::{ConsensusState, Header}; | ||
use ibc_core_client_types::Height as IbcHeight; | ||
use ibc_core_commitment_types::commitment::CommitmentRoot; | ||
use ibc_core_host_types::{error::IdentifierError, identifiers::ChainId}; | ||
use sp1_ics07_tendermint_solidity::sp1_ics07_tendermint::{ClientState, Height, TrustThreshold}; | ||
use std::str::FromStr; | ||
use tendermint_light_client_verifier::types::LightBlock; | ||
|
||
/// A wrapper around a [`LightBlock`] that provides additional methods. | ||
#[allow(clippy::module_name_repetitions)] | ||
pub struct LightBlockWrapper(LightBlock); | ||
|
||
impl LightBlockWrapper { | ||
/// Create a new instance of the `LightBlockWrapper`. | ||
#[must_use] | ||
pub const fn new(light_block: LightBlock) -> Self { | ||
Self(light_block) | ||
} | ||
|
||
/// Get the inner `LightBlock`. | ||
#[must_use] | ||
pub const fn as_light_block(&self) -> &LightBlock { | ||
&self.0 | ||
} | ||
|
||
/// Convert the [`LightBlockWrapper`] to a new solidity [`ClientState`]. | ||
/// | ||
/// # Errors | ||
/// Returns an error if the chain identifier or height cannot be parsed. | ||
pub fn to_sol_client_state(&self) -> anyhow::Result<ClientState> { | ||
let chain_id = ChainId::from_str(self.0.signed_header.header.chain_id.as_str())?; | ||
let two_weeks_in_nanos = 14 * 24 * 60 * 60 * 1_000_000_000; | ||
Ok(ClientState { | ||
chain_id: chain_id.to_string(), | ||
trust_level: TrustThreshold { | ||
numerator: 1, | ||
denominator: 3, | ||
}, | ||
latest_height: Height { | ||
revision_number: chain_id.revision_number().try_into()?, | ||
revision_height: self.0.height().value().try_into()?, | ||
}, | ||
is_frozen: false, | ||
trusting_period: two_weeks_in_nanos, | ||
unbonding_period: two_weeks_in_nanos, | ||
}) | ||
} | ||
|
||
/// Convert the [`LightBlockWrapper`] to a new [`ConsensusState`]. | ||
#[must_use] | ||
pub fn to_consensus_state(&self) -> ConsensusState { | ||
ConsensusState { | ||
timestamp: self.0.signed_header.header.time, | ||
root: CommitmentRoot::from_bytes(self.0.signed_header.header.app_hash.as_bytes()), | ||
next_validators_hash: self.0.signed_header.header.next_validators_hash, | ||
} | ||
} | ||
|
||
/// Convert the [`LightBlockWrapper`] to a new [`Header`]. | ||
/// | ||
/// # Panics | ||
/// Panics if the `trusted_height` is zero. | ||
#[must_use] | ||
pub fn into_header(self, trusted_light_block: &LightBlock) -> Header { | ||
let trusted_revision_number = | ||
ChainId::from_str(trusted_light_block.signed_header.header.chain_id.as_str()) | ||
.unwrap() | ||
.revision_number(); | ||
let trusted_block_height = trusted_light_block.height().value(); | ||
Header { | ||
signed_header: self.0.signed_header, | ||
validator_set: self.0.validators, | ||
trusted_height: IbcHeight::new(trusted_revision_number, trusted_block_height).unwrap(), | ||
trusted_next_validator_set: trusted_light_block.next_validators.clone(), | ||
} | ||
} | ||
|
||
/// Get the chain identifier from the [`LightBlock`]. | ||
/// | ||
/// # Errors | ||
/// Returns an error if the chain identifier cannot be parsed. | ||
pub fn chain_id(&self) -> Result<ChainId, IdentifierError> { | ||
ChainId::from_str(self.0.signed_header.header.chain_id.as_str()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! Provides helpers for `sp1-ics07-tendermint-operator`. | ||
pub mod eth; | ||
pub mod light_block; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//! Programs for `sp1-ics07-tendermint`. | ||
use sp1_sdk::{MockProver, Prover, SP1VerifyingKey}; | ||
|
||
/// Trait for SP1 ICS07 Tendermint programs. | ||
pub trait SP1Program { | ||
/// The ELF file for the program. | ||
const ELF: &'static [u8]; | ||
|
||
/// Get the verifying key for the program using [`MockProver`]. | ||
#[must_use] | ||
fn get_vkey() -> SP1VerifyingKey { | ||
let mock_prover = MockProver::new(); | ||
let (_, vkey) = mock_prover.setup(Self::ELF); | ||
vkey | ||
} | ||
} | ||
|
||
/// SP1 ICS07 Tendermint update client program. | ||
pub struct UpdateClientProgram; | ||
|
||
/// SP1 ICS07 Tendermint verify membership program. | ||
pub struct VerifyMembershipProgram; | ||
|
||
impl SP1Program for UpdateClientProgram { | ||
const ELF: &'static [u8] = | ||
include_bytes!("../../elf/update-client-riscv32im-succinct-zkvm-elf"); | ||
} | ||
|
||
impl SP1Program for VerifyMembershipProgram { | ||
const ELF: &'static [u8] = | ||
include_bytes!("../../elf/verify-membership-riscv32im-succinct-zkvm-elf"); | ||
} |
Oops, something went wrong.