Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
imp(operator): allow custom unbonding period (#69)
Browse files Browse the repository at this point in the history
* refactor

* imp(operator): allow custom unbonding period

* imp: updated e2es
  • Loading branch information
srdtrk authored Jul 18, 2024
1 parent 18b36e9 commit 603c3e4
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 34 deletions.
36 changes: 32 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ sp1-ics07-tendermint-utils = { path = "./packages/utils/" }
tendermint = { version = "0.36.0", default-features = false }
tendermint-rpc = { version = "0.36.0", features = ["http-client"] }
ibc-core-client-types = { version = "0.53.0", default-features = false }
cosmos-sdk-proto = { version = "0.22.0", default-features = false }

reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
Expand Down
14 changes: 10 additions & 4 deletions e2e/interchaintestv8/sp1_ics07_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ func (s *SP1ICS07TendermintTestSuite) TestDeploy() {
clientState, err := s.contract.GetClientState(nil)
s.Require().NoError(err)

stakingParams, err := simd.StakingQueryParams(ctx)
s.Require().NoError(err)

s.Require().Equal(simd.Config().ChainID, clientState.ChainId)
s.Require().Equal(uint8(testvalues.DefaultTrustLevel.Numerator), clientState.TrustLevel.Numerator)
s.Require().Equal(uint8(testvalues.DefaultTrustLevel.Denominator), clientState.TrustLevel.Denominator)
s.Require().Equal(uint32(1_209_600), clientState.TrustingPeriod)
s.Require().Equal(uint32(1_209_600), clientState.UnbondingPeriod)
s.Require().Equal(uint32(stakingParams.UnbondingTime.Seconds()), clientState.TrustingPeriod)
s.Require().Equal(uint32(stakingParams.UnbondingTime.Seconds()), clientState.UnbondingPeriod)
s.Require().False(clientState.IsFrozen)
s.Require().Equal(uint32(1), clientState.LatestHeight.RevisionNumber)
s.Require().Greater(clientState.LatestHeight.RevisionHeight, uint32(0))
Expand All @@ -134,11 +137,14 @@ func (s *SP1ICS07TendermintTestSuite) TestUpdateClient() {
clientState, err := s.contract.GetClientState(nil)
s.Require().NoError(err)

stakingParams, err := simd.StakingQueryParams(ctx)
s.Require().NoError(err)

s.Require().Equal(simd.Config().ChainID, clientState.ChainId)
s.Require().Equal(uint8(testvalues.DefaultTrustLevel.Numerator), clientState.TrustLevel.Numerator)
s.Require().Equal(uint8(testvalues.DefaultTrustLevel.Denominator), clientState.TrustLevel.Denominator)
s.Require().Equal(uint32(1_209_600), clientState.TrustingPeriod)
s.Require().Equal(uint32(1_209_600), clientState.UnbondingPeriod)
s.Require().Equal(uint32(stakingParams.UnbondingTime.Seconds()), clientState.TrustingPeriod)
s.Require().Equal(uint32(stakingParams.UnbondingTime.Seconds()), clientState.UnbondingPeriod)
s.Require().False(clientState.IsFrozen)
s.Require().Equal(uint32(1), clientState.LatestHeight.RevisionNumber)
s.Require().Greater(clientState.LatestHeight.RevisionHeight, s.latestHeight)
Expand Down
23 changes: 14 additions & 9 deletions operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@ license = { workspace = true }

[dependencies]
sp1-sdk = { workspace = true }
reqwest = { workspace = true }
tokio = { workspace = true }
futures = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }

tendermint = { workspace = true }
tendermint-rpc = { workspace = true }
tendermint-light-client-verifier = { workspace = true }
cosmos-sdk-proto = { workspace = true }
ibc-client-tendermint = { workspace = true }
ibc-core-client-types = { workspace = true }
ibc-core-commitment-types = { workspace = true }
ibc-core-host-types = { workspace = true, features = ["std"] }
ibc-proto = { workspace = true }

sp1-ics07-tendermint-update-client = { workspace = true }
sp1-ics07-tendermint-solidity = { workspace = true, features = ["rpc"] }
sp1-ics07-tendermint-utils = { workspace = true }

alloy-sol-types = { workspace = true }
alloy-primitives = { workspace = true }
alloy = { workspace = true, features = ["full", "node-bindings"] }

reqwest = { workspace = true }
tokio = { workspace = true }
futures = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
bincode = { workspace = true }
serde_cbor = { workspace = true }
sha2 = { workspace = true }
Expand All @@ -33,7 +42,3 @@ clap = { workspace = true }
log = { workspace = true }
async-trait = { workspace = true }
hex = { workspace = true }
sp1-ics07-tendermint-update-client = { workspace = true }
sp1-ics07-tendermint-solidity = { workspace = true, features = ["rpc"] }
sp1-ics07-tendermint-utils = { workspace = true }
ibc-proto = { workspace = true }
17 changes: 12 additions & 5 deletions operator/src/helpers/light_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub trait LightBlockExt {
///
/// # Errors
/// Returns an error if the chain identifier or height cannot be parsed.
fn to_sol_client_state(&self, trust_level: TrustThreshold) -> anyhow::Result<ClientState>;
fn to_sol_client_state(
&self,
trust_level: TrustThreshold,
unbonding_period: u32,
) -> anyhow::Result<ClientState>;
/// Convert the [`LightBlock`] to a new [`ConsensusState`].
#[must_use]
fn to_consensus_state(&self) -> ConsensusState;
Expand All @@ -34,9 +38,12 @@ pub trait LightBlockExt {
}

impl LightBlockExt for LightBlock {
fn to_sol_client_state(&self, trust_level: TrustThreshold) -> anyhow::Result<ClientState> {
fn to_sol_client_state(
&self,
trust_level: TrustThreshold,
unbonding_period: u32,
) -> anyhow::Result<ClientState> {
let chain_id = ChainId::from_str(self.signed_header.header.chain_id.as_str())?;
let two_weeks_in_seconds = 14 * 24 * 60 * 60;
Ok(ClientState {
chain_id: chain_id.to_string(),
trust_level,
Expand All @@ -45,8 +52,8 @@ impl LightBlockExt for LightBlock {
revision_height: self.height().value().try_into()?,
},
is_frozen: false,
trusting_period: two_weeks_in_seconds,
unbonding_period: two_weeks_in_seconds,
unbonding_period,
trusting_period: unbonding_period,
})
}

Expand Down
23 changes: 22 additions & 1 deletion operator/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use std::{collections::HashMap, env};

use anyhow::Result;

use cosmos_sdk_proto::{
cosmos::staking::v1beta1::{Params, QueryParamsRequest, QueryParamsResponse},
prost::Message,
traits::MessageExt,
};
use tendermint::{block::signed_header::SignedHeader, validator::Set};
use tendermint_light_client_verifier::types::{LightBlock, ValidatorSet};
use tendermint_rpc::{Client, HttpClient, Paging, Url};
Expand All @@ -25,11 +30,13 @@ pub trait TendermintRpcExt {
/// # Errors
/// Returns an error if the RPC request fails or if the response cannot be parsed.
async fn get_light_block(&self, block_height: Option<u32>) -> Result<LightBlock>;
/// Queries the Cosmos SDK for staking parameters.
async fn sdk_staking_params(&self) -> Result<Params>;
}

impl TendermintRpcExt for HttpClient {
fn from_env() -> Self {
Self::new::<Url>(
Self::new(
Url::from_str(&env::var("TENDERMINT_RPC_URL").expect("TENDERMINT_RPC_URL not set"))
.expect("Failed to parse URL"),
)
Expand Down Expand Up @@ -68,6 +75,20 @@ impl TendermintRpcExt for HttpClient {
peer_id,
))
}

async fn sdk_staking_params(&self) -> Result<Params> {
let abci_resp = self
.abci_query(
Some("/cosmos.staking.v1beta1.Query/Params".to_string()),
QueryParamsRequest::default().to_bytes()?,
None,
false,
)
.await?;
QueryParamsResponse::decode(abci_resp.value.as_slice())?
.params
.ok_or_else(|| anyhow::anyhow!("No staking params found"))
}
}

/// Sorts the signatures in the signed header based on the descending order of validators' power.
Expand Down
10 changes: 9 additions & 1 deletion operator/src/runners/fixtures/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ pub async fn run(args: MembershipCmd) -> anyhow::Result<()> {
.get_light_block(Some(args.trusted_block))
.await?;

let unbonding_period = tm_rpc_client
.sdk_staking_params()
.await?
.unbonding_time
.ok_or_else(|| anyhow::anyhow!("No unbonding time found"))?
.seconds
.try_into()?;

let trusted_client_state =
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?)?;
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?, unbonding_period)?;
let trusted_consensus_state = trusted_light_block.to_consensus_state();
let commitment_root_bytes = trusted_consensus_state.root.as_bytes().to_vec();

Expand Down
10 changes: 9 additions & 1 deletion operator/src/runners/fixtures/uc_and_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ pub async fn run(args: UpdateClientAndMembershipCmd) -> anyhow::Result<()> {
.get_light_block(Some(args.target_block))
.await?;

let unbonding_period = tm_rpc_client
.sdk_staking_params()
.await?
.unbonding_time
.ok_or_else(|| anyhow::anyhow!("No unbonding time found"))?
.seconds
.try_into()?;

let trusted_client_state =
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?)?;
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?, unbonding_period)?;
let trusted_consensus_state = trusted_light_block.to_consensus_state().into();
let proposed_header = target_light_block.into_header(&trusted_light_block);
let contract_env = Env {
Expand Down
16 changes: 12 additions & 4 deletions operator/src/runners/fixtures/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,26 @@ pub async fn run(args: UpdateClientCmd) -> anyhow::Result<()> {
"The target block must be greater than the trusted block"
);

let tendermint_rpc_client = HttpClient::from_env();
let tm_rpc_client = HttpClient::from_env();
let uc_prover = SP1ICS07TendermintProver::<UpdateClientProgram>::default();

let trusted_light_block = tendermint_rpc_client
let trusted_light_block = tm_rpc_client
.get_light_block(Some(args.trusted_block))
.await?;
let target_light_block = tendermint_rpc_client
let target_light_block = tm_rpc_client
.get_light_block(Some(args.target_block))
.await?;

let unbonding_period = tm_rpc_client
.sdk_staking_params()
.await?
.unbonding_time
.ok_or_else(|| anyhow::anyhow!("No unbonding time found"))?
.seconds
.try_into()?;

let trusted_client_state =
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?)?;
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?, unbonding_period)?;
let trusted_consensus_state = trusted_light_block.to_consensus_state().into();
let proposed_header = target_light_block.into_header(&trusted_light_block);
let contract_env = Env {
Expand Down
16 changes: 11 additions & 5 deletions operator/src/runners/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@ pub async fn run(args: Args) -> anyhow::Result<()> {
log::warn!("No .env file found");
}

let tendermint_rpc_client = HttpClient::from_env();
let tm_rpc_client = HttpClient::from_env();

let trusted_light_block = tendermint_rpc_client
.get_light_block(args.trusted_block)
.await?;
let trusted_light_block = tm_rpc_client.get_light_block(args.trusted_block).await?;
if args.trusted_block.is_none() {
log::info!(
"Latest block height: {}",
trusted_light_block.height().value()
);
}

let unbonding_period = tm_rpc_client
.sdk_staking_params()
.await?
.unbonding_time
.ok_or_else(|| anyhow::anyhow!("No unbonding time found"))?
.seconds
.try_into()?;

let trusted_client_state =
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?)?;
trusted_light_block.to_sol_client_state(args.trust_level.try_into()?, unbonding_period)?;
let trusted_consensus_state = trusted_light_block.to_consensus_state();
let genesis = SP1ICS07TendermintGenesis {
trusted_consensus_state: hex::encode(
Expand Down

0 comments on commit 603c3e4

Please sign in to comment.