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

imp: add migrate entry_point to eth light client #404

Merged
merged 6 commits into from
Mar 14, 2025
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
27 changes: 27 additions & 0 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 @@ -104,6 +104,7 @@ sp1-helper = { version = "4.1", default-features = false }

cosmwasm-schema = { version = "2.2", default-features = false }
cosmwasm-std = { version = "2.2", default-features = false }
cw2 = { version = "2.0", default-features = false }

# The dependencies below are maintained by Sigma Prime (for use in Lighthouse (and the broader Ethereum ecosystem))
ethereum_ssz = { version = "0.8", default-features = false }
Expand Down
Binary file modified e2e/interchaintestv8/wasm/cw_ics08_wasm_eth.wasm.gz
Binary file not shown.
1 change: 1 addition & 0 deletions programs/cw-ics08-wasm-eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ alloy-primitives = { workspace = true, default-features = false }

cosmwasm-std = { workspace = true, features = ["std"] }
cosmwasm-schema = { workspace = true }
cw2 = { workspace = true }
prost = { workspace = true, features = ["std"] }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
57 changes: 55 additions & 2 deletions programs/cw-ics08-wasm-eth/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ use ibc_proto::ibc::{
},
};

use crate::{custom_query::EthereumCustomQuery, query, state::store_client_state};
use crate::{custom_query::EthereumCustomQuery, msg::MigrateMsg, query, state::store_client_state};
use crate::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg, SudoMsg},
state::store_consensus_state,
};
use crate::{sudo, ContractError};

/// The version of the contracts state.
/// It is used to determine if the state needs to be migrated in the migrate entry point.
const STATE_VERSION: &str = env!("CARGO_PKG_VERSION");
const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");

/// The instantiate entry point for the CosmWasm contract.
/// # Errors
/// Will return an error if the client state or consensus state cannot be deserialized.
Expand All @@ -32,6 +37,8 @@ pub fn instantiate(
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, STATE_VERSION)?;

let client_state_bz: Vec<u8> = msg.client_state.into();
let client_state: EthClientState = serde_json::from_slice(&client_state_bz)
.map_err(ContractError::DeserializeClientStateFailed)?;
Expand Down Expand Up @@ -140,6 +147,22 @@ pub fn query(
}
}

/// The migrate entry point for the CosmWasm contract.
/// # Errors
/// Will return an errror if the state version is not newer than the current one.
#[entry_point]
#[allow(clippy::needless_pass_by_value)]
pub fn migrate(
deps: DepsMut<EthereumCustomQuery>,
_env: Env,
_msg: MigrateMsg,
) -> Result<Response, ContractError> {
// Check if the state version is older than the current one and update it
cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, STATE_VERSION)?;

Ok(Response::default())
}

#[cfg(test)]
mod tests {
mod instantiate_tests {
Expand Down Expand Up @@ -288,7 +311,7 @@ mod tests {
use prost::Message;

use crate::{
contract::{instantiate, query, sudo},
contract::{instantiate, migrate, query, sudo},
msg::{
Height, MerklePath, QueryMsg, SudoMsg, UpdateStateMsg, UpdateStateResult,
VerifyClientMessageMsg, VerifyMembershipMsg,
Expand Down Expand Up @@ -471,5 +494,35 @@ mod tests {
);
}
}

#[test]
fn test_migrate_with_same_state_version() {
let mut deps = mk_deps();
let creator = deps.api.addr_make("creator");
let info = message_info(&creator, &coins(1, "uatom"));

let fixture: StepsFixture =
fixtures::load("TestICS20TransferERC20TokenfromEthereumToCosmosAndBack_Groth16");

let initial_state: InitialState = fixture.get_data_at_step(0);

let client_state = initial_state.client_state;

let consensus_state = initial_state.consensus_state;

let client_state_bz: Vec<u8> = serde_json::to_vec(&client_state).unwrap();
let consensus_state_bz: Vec<u8> = serde_json::to_vec(&consensus_state).unwrap();

let msg = crate::msg::InstantiateMsg {
client_state: Binary::from(client_state_bz),
consensus_state: Binary::from(consensus_state_bz),
checksum: b"checksum".into(),
};

instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// Migrate without any changes (i.e. same state version)
migrate(deps.as_mut(), mock_env(), crate::msg::MigrateMsg {}).unwrap();
}
}
}
46 changes: 25 additions & 21 deletions programs/cw-ics08-wasm-eth/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ pub enum SudoMsg {
MigrateClientStore(MigrateClientStoreMsg),
}

/// The query messages called by `ibc-go`
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// The message to verify the client message
#[returns[()]]
VerifyClientMessage(VerifyClientMessageMsg),

/// The message to check for misbehaviour
#[returns[CheckForMisbehaviourResult]]
CheckForMisbehaviour(CheckForMisbehaviourMsg),

/// The message to get the timestamp at height
#[returns[TimestampAtHeightResult]]
TimestampAtHeight(TimestampAtHeightMsg),

/// The message to get the status
#[returns[StatusResult]]
Status(StatusMsg),
}

/// The message to migrate the contract
#[cw_serde]
pub struct MigrateMsg {}

/// Verify membership message
#[cw_serde]
pub struct VerifyMembershipMsg {
Expand Down Expand Up @@ -114,27 +139,6 @@ pub struct EthereumMisbehaviourMsg {
pub update_2: LightClientUpdate,
}

/// The query messages called by `ibc-go`
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// The message to verify the client message
#[returns[()]]
VerifyClientMessage(VerifyClientMessageMsg),

/// The message to check for misbehaviour
#[returns[CheckForMisbehaviourResult]]
CheckForMisbehaviour(CheckForMisbehaviourMsg),

/// The message to get the timestamp at height
#[returns[TimestampAtHeightResult]]
TimestampAtHeight(TimestampAtHeightMsg),

/// The message to get the status
#[returns[StatusResult]]
Status(StatusMsg),
}

/// The message to verify the client message
#[cw_serde]
pub struct VerifyClientMessageMsg {
Expand Down
Loading