From d8fc024a1959293ea1360b9b78d32125bbcbca28 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Wed, 7 Aug 2024 17:36:27 -0400 Subject: [PATCH 01/22] auto register --- contracts/babylon/src/contract.rs | 6 +++++- contracts/babylon/src/msg/contract.rs | 6 +++++- contracts/babylon/src/state/config.rs | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index 137de240..2f8ca972 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -34,6 +34,8 @@ pub fn instantiate( checkpoint_finalization_timeout: msg.checkpoint_finalization_timeout, notify_cosmos_zone: msg.notify_cosmos_zone, btc_staking: None, // Will be set in `reply` if `btc_staking_code_id` is provided + consumer_name: msg.consumer_name, + consumer_description: msg.consumer_description, }; CONFIG.save(deps.storage, &cfg)?; @@ -214,9 +216,11 @@ mod tests { btc_staking_code_id: None, btc_staking_msg: None, admin: None, + consumer_name: "MyConsumer".to_string(), + consumer_description: "This is a description of my consumer".to_string(), }; let info = message_info(&deps.api.addr_make(CREATOR), &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); } -} +} \ No newline at end of file diff --git a/contracts/babylon/src/msg/contract.rs b/contracts/babylon/src/msg/contract.rs index 3b721fe9..cdde29ad 100644 --- a/contracts/babylon/src/msg/contract.rs +++ b/contracts/babylon/src/msg/contract.rs @@ -33,6 +33,10 @@ pub struct InstantiateMsg { pub btc_staking_msg: Option, /// If set, this will be the Wasm migration / upgrade admin of the BTC staking contract pub admin: Option, + /// Name of the consumer + pub consumer_name: String, + /// Description of the consumer + pub consumer_description: String, } impl ContractMsg for InstantiateMsg { @@ -125,4 +129,4 @@ pub enum QueryMsg { /// CzHeader returns the CZ header stored in the contract, by CZ height. #[returns(CzHeaderResponse)] CzHeader { height: u64 }, -} +} \ No newline at end of file diff --git a/contracts/babylon/src/state/config.rs b/contracts/babylon/src/state/config.rs index c52441d0..841ae919 100644 --- a/contracts/babylon/src/state/config.rs +++ b/contracts/babylon/src/state/config.rs @@ -16,4 +16,6 @@ pub struct Config { pub notify_cosmos_zone: bool, /// If set, this stores a BTC staking contract used for BTC re-staking pub btc_staking: Option, + pub consumer_name: String, + pub consumer_description: String, } From 2080b5415a53917d2c45a579c9b207b3fcc661b3 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Wed, 7 Aug 2024 17:42:47 -0400 Subject: [PATCH 02/22] add todo --- contracts/babylon/src/ibc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index f3519fc2..58ab7f90 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -69,6 +69,8 @@ pub fn ibc_channel_connect( // Store the channel IBC_CHANNEL.save(deps.storage, channel)?; + // TODO: send IBC packet to Babylon to register the consumer + let chan_id = &channel.endpoint.channel_id; Ok(IbcBasicResponse::new() .add_attribute("action", "ibc_connect") From 9db8c4502f931146490ca4eccf99a59a1cd38f76 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Wed, 7 Aug 2024 17:45:40 -0400 Subject: [PATCH 03/22] remove space --- contracts/babylon/src/contract.rs | 2 +- contracts/babylon/src/msg/contract.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index 2f8ca972..23fea9a1 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -223,4 +223,4 @@ mod tests { let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); } -} \ No newline at end of file +} diff --git a/contracts/babylon/src/msg/contract.rs b/contracts/babylon/src/msg/contract.rs index cdde29ad..77b5baa2 100644 --- a/contracts/babylon/src/msg/contract.rs +++ b/contracts/babylon/src/msg/contract.rs @@ -129,4 +129,4 @@ pub enum QueryMsg { /// CzHeader returns the CZ header stored in the contract, by CZ height. #[returns(CzHeaderResponse)] CzHeader { height: u64 }, -} \ No newline at end of file +} From f63a09d900724885bd82b387810eec4713fc2324 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Thu, 8 Aug 2024 12:45:31 -0400 Subject: [PATCH 04/22] fix tests --- contracts/babylon/src/ibc.rs | 2 ++ contracts/babylon/src/multitest/suite.rs | 2 ++ contracts/babylon/src/state/btc_light_client.rs | 2 ++ 3 files changed, 6 insertions(+) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 58ab7f90..f340ab05 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -383,6 +383,8 @@ mod tests { btc_staking_code_id: None, btc_staking_msg: None, admin: None, + consumer_name: "TestConsumer".to_string(), + consumer_description: "Test Consumer Description".to_string(), }; let info = message_info(&deps.api.addr_make(CREATOR), &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); diff --git a/contracts/babylon/src/multitest/suite.rs b/contracts/babylon/src/multitest/suite.rs index c9f6102d..87842797 100644 --- a/contracts/babylon/src/multitest/suite.rs +++ b/contracts/babylon/src/multitest/suite.rs @@ -67,6 +67,8 @@ impl SuiteBuilder { btc_staking_code_id: Some(btc_staking_code_id), btc_staking_msg: None, admin: Some(owner.to_string()), + consumer_name: "TestConsumer".to_string(), + consumer_description: "Test Consumer Description".to_string(), }, &[], "babylon", diff --git a/contracts/babylon/src/state/btc_light_client.rs b/contracts/babylon/src/state/btc_light_client.rs index 9cf5dc6a..f12df343 100644 --- a/contracts/babylon/src/state/btc_light_client.rs +++ b/contracts/babylon/src/state/btc_light_client.rs @@ -357,6 +357,8 @@ pub(crate) mod tests { checkpoint_finalization_timeout: w as u64, notify_cosmos_zone: false, btc_staking: None, + consumer_name: "TestConsumer".to_string(), + consumer_description: "Test Consumer Description".to_string(), }; CONFIG.save(storage, &cfg).unwrap(); w From 1ba73052bcc67b89839f4e2a539d0d45168361d4 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Thu, 8 Aug 2024 17:25:30 -0400 Subject: [PATCH 05/22] bump sub mods --- packages/proto/babylon | 2 +- packages/proto/babylon-private | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/proto/babylon b/packages/proto/babylon index d64ddc97..d63c121c 160000 --- a/packages/proto/babylon +++ b/packages/proto/babylon @@ -1 +1 @@ -Subproject commit d64ddc97d1c8b9f695b814b7b1b92ce133f2547b +Subproject commit d63c121c49944885785ca67ab8b7f64165687070 diff --git a/packages/proto/babylon-private b/packages/proto/babylon-private index 3d8f190c..07d73a64 160000 --- a/packages/proto/babylon-private +++ b/packages/proto/babylon-private @@ -1 +1 @@ -Subproject commit 3d8f190c9b0c0795f6546806e3b8582de716cd60 +Subproject commit 07d73a649d70049c87560f22e5c4bade579fe609 From f28d9f343557dea94d8182b256ff0ba35fe0376a Mon Sep 17 00:00:00 2001 From: Gurjot Date: Thu, 8 Aug 2024 17:32:01 -0400 Subject: [PATCH 06/22] fix proto --- .../src/gen/babylon.btcstkconsumer.v1.rs | 58 ++----------------- .../proto/src/gen/babylon.zoneconcierge.v1.rs | 4 +- 2 files changed, 7 insertions(+), 55 deletions(-) diff --git a/packages/proto/src/gen/babylon.btcstkconsumer.v1.rs b/packages/proto/src/gen/babylon.btcstkconsumer.v1.rs index 0b975aef..4b6df3e1 100644 --- a/packages/proto/src/gen/babylon.btcstkconsumer.v1.rs +++ b/packages/proto/src/gen/babylon.btcstkconsumer.v1.rs @@ -1,61 +1,11 @@ // @generated -/// QueryFinalityProviderRequest requests information about a finality provider +/// ConsumerRegisterPacketData defines the packet data for consumer registration #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] -pub struct QueryFinalityProviderRequest { - /// consumer id is the consumer id this finality provider is registered to +pub struct ConsumerRegisterIbcPacket { #[prost(string, tag="1")] - pub consumer_id: ::prost::alloc::string::String, - /// fp_btc_pk_hex is the hex str of Bitcoin secp256k1 PK of the finality provider + pub consumer_name: ::prost::alloc::string::String, #[prost(string, tag="2")] - pub fp_btc_pk_hex: ::prost::alloc::string::String, -} -/// QueryFinalityProviderResponse contains information about a finality provider -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct QueryFinalityProviderResponse { - /// finality_provider contains the FinalityProvider - #[prost(message, optional, tag="1")] - pub finality_provider: ::core::option::Option, -} -/// FinalityProviderResponse defines a finality provider with voting power information. -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct FinalityProviderResponse { - /// description defines the description terms for the finality provider. - #[prost(message, optional, tag="1")] - pub description: ::core::option::Option, - /// commission defines the commission rate of the finality provider. - #[prost(string, tag="2")] - pub commission: ::prost::alloc::string::String, - /// babylon_pk is the Babylon secp256k1 PK of this finality provider - #[prost(message, optional, tag="3")] - pub babylon_pk: ::core::option::Option, - /// btc_pk is the Bitcoin secp256k1 PK of this finality provider - /// the PK follows encoding in BIP-340 spec - #[prost(bytes="bytes", tag="4")] - pub btc_pk: ::prost::bytes::Bytes, - /// pop is the proof of possession of babylon_pk and btc_pk - #[prost(message, optional, tag="5")] - pub pop: ::core::option::Option, - /// slashed_babylon_height indicates the Babylon height when - /// the finality provider is slashed. - /// if it's 0 then the finality provider is not slashed - #[prost(uint64, tag="6")] - pub slashed_babylon_height: u64, - /// slashed_btc_height indicates the BTC height when - /// the finality provider is slashed. - /// if it's 0 then the finality provider is not slashed - #[prost(uint64, tag="7")] - pub slashed_btc_height: u64, - /// height is the queried Babylon height - #[prost(uint64, tag="8")] - pub height: u64, - /// voting_power is the voting power of this finality provider at the given height - #[prost(uint64, tag="9")] - pub voting_power: u64, - /// consumer_id is the consumer id this finality provider is registered to - #[prost(string, tag="10")] - pub consumer_id: ::prost::alloc::string::String, + pub consumer_description: ::prost::alloc::string::String, } // @@protoc_insertion_point(module) diff --git a/packages/proto/src/gen/babylon.zoneconcierge.v1.rs b/packages/proto/src/gen/babylon.zoneconcierge.v1.rs index 23674e39..85d24538 100644 --- a/packages/proto/src/gen/babylon.zoneconcierge.v1.rs +++ b/packages/proto/src/gen/babylon.zoneconcierge.v1.rs @@ -154,7 +154,7 @@ pub struct ProofFinalizedChainInfo { #[derive(Clone, PartialEq, ::prost::Message)] pub struct ZoneconciergePacketData { /// packet is the actual message carried in the IBC packet - #[prost(oneof="zoneconcierge_packet_data::Packet", tags="1, 2")] + #[prost(oneof="zoneconcierge_packet_data::Packet", tags="1, 2, 3")] pub packet: ::core::option::Option, } /// Nested message and enum types in `ZoneconciergePacketData`. @@ -167,6 +167,8 @@ pub mod zoneconcierge_packet_data { BtcTimestamp(super::BtcTimestamp), #[prost(message, tag="2")] BtcStaking(super::super::super::btcstaking::v1::BtcStakingIbcPacket), + #[prost(message, tag="3")] + ConsumerRegister(super::super::super::btcstkconsumer::v1::ConsumerRegisterIbcPacket), } } /// BTCTimestamp is a BTC timestamp that carries information of a BTC-finalised epoch From a410a4e3f313e5610e95e97132fa75d053209842 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Thu, 8 Aug 2024 18:18:59 -0400 Subject: [PATCH 07/22] send ibc packet --- contracts/babylon/src/ibc.rs | 33 ++++++++++++++++++++++++++++++--- packages/proto/src/lib.rs | 7 +++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index f340ab05..1e18d73e 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -3,14 +3,17 @@ use babylon_bindings::BabylonMsg; use babylon_proto::babylon::zoneconcierge::v1::{ zoneconcierge_packet_data::Packet, BtcTimestamp, ZoneconciergePacketData, }; +use babylon_proto::babylon::btcstkconsumer::v1::ConsumerRegisterIbcPacket; + use cosmwasm_std::{ DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout, Never, StdAck, - StdError, StdResult, + StdError, StdResult, IbcMsg, to_json_binary, }; use cw_storage_plus::Item; use prost::Message; +use crate::state::config::{Config, CONFIG}; pub const IBC_VERSION: &str = "zoneconcierge-1"; pub const IBC_ORDERING: IbcOrder = IbcOrder::Ordered; @@ -69,7 +72,28 @@ pub fn ibc_channel_connect( // Store the channel IBC_CHANNEL.save(deps.storage, channel)?; - // TODO: send IBC packet to Babylon to register the consumer + // Load the config + let cfg = CONFIG.load(deps.storage)?; + + // Create the ConsumerRegisterIBCPacket + let consumer_register_packet = ConsumerRegisterIbcPacket { + consumer_name: cfg.consumer_name, + consumer_description: cfg.consumer_description, + }; + + // Create the ZoneconciergePacketData + let packet_data = ZoneconciergePacketData { + packet: Some(Packet::ConsumerRegister(consumer_register_packet)), + }; + + let packet_data_bytes = packet_data.encode_to_vec(); + + // Create the IBC packet message + let ibc_msg = IbcMsg::SendPacket { + channel_id: channel.endpoint.channel_id.clone(), + data: to_json_binary(&packet_data_bytes)?, + timeout: packet_timeout(&env), + }; let chan_id = &channel.endpoint.channel_id; Ok(IbcBasicResponse::new() @@ -125,7 +149,10 @@ pub fn ibc_packet_receive( Packet::BtcTimestamp(btc_ts) => ibc_packet::handle_btc_timestamp(deps, caller, &btc_ts), Packet::BtcStaking(btc_staking) => { ibc_packet::handle_btc_staking(deps, caller, &btc_staking) - } + }, + Packet::ConsumerRegister(_) => { + return Err(StdError::generic_err("ConsumerRegister packet should not be received").into()) + }, } })() .or_else(|e| { diff --git a/packages/proto/src/lib.rs b/packages/proto/src/lib.rs index 1053d6e1..01bf089b 100644 --- a/packages/proto/src/lib.rs +++ b/packages/proto/src/lib.rs @@ -49,6 +49,13 @@ pub mod babylon { // @@protoc_insertion_point(babylon.finality.v1) } } + pub mod btcstkconsumer { + // @@protoc_insertion_point(attribute:babylon.btcstkconsumer.v1) + pub mod v1 { + include!("gen/babylon.btcstkconsumer.v1.rs"); + // @@protoc_insertion_point(babylon.btcstkconsumer.v1) + } + } } #[cfg(test)] From 92021417c9a6e5d9b9eb37b6bc8a4563c4151800 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 10:03:48 -0400 Subject: [PATCH 08/22] add logs --- contracts/babylon/src/ibc.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 1e18d73e..136e879b 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -60,9 +60,11 @@ pub fn ibc_channel_open( /// Second part of the 4-step handshake, i.e. ChannelOpenAck and ChannelOpenConfirm. pub fn ibc_channel_connect( deps: DepsMut, - _env: Env, + env: &Env, msg: IbcChannelConnectMsg, ) -> Result { + deps.api.debug("Entered function: ibc_channel_connect"); + // Ensure we have no channel yet if IBC_CHANNEL.may_load(deps.storage)?.is_some() { return Err(ContractError::IbcChannelAlreadyOpen {}); @@ -72,7 +74,7 @@ pub fn ibc_channel_connect( // Store the channel IBC_CHANNEL.save(deps.storage, channel)?; - // Load the config + // Load the config let cfg = CONFIG.load(deps.storage)?; // Create the ConsumerRegisterIBCPacket @@ -92,9 +94,11 @@ pub fn ibc_channel_connect( let ibc_msg = IbcMsg::SendPacket { channel_id: channel.endpoint.channel_id.clone(), data: to_json_binary(&packet_data_bytes)?, - timeout: packet_timeout(&env), + timeout: packet_timeout(env), }; + deps.api.debug(&format!("Sending IBC message from ibc_channel_connect: {:?}", ibc_msg)); + let chan_id = &channel.endpoint.channel_id; Ok(IbcBasicResponse::new() .add_attribute("action", "ibc_connect") From 3102c09f98b2d586f79ede1e740310daa7300256 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 10:31:11 -0400 Subject: [PATCH 09/22] fix env --- contracts/babylon/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/babylon/src/lib.rs b/contracts/babylon/src/lib.rs index dd7414f3..3aa172eb 100644 --- a/contracts/babylon/src/lib.rs +++ b/contracts/babylon/src/lib.rs @@ -73,7 +73,7 @@ pub fn ibc_channel_connect( env: Env, msg: IbcChannelConnectMsg, ) -> Result { - ibc::ibc_channel_connect(deps, env, msg) + ibc::ibc_channel_connect(deps, &env, msg) } #[cfg_attr(not(feature = "library"), entry_point)] From a60be4cf2d22fedefc75e1d89b2e920cc88c8af7 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 10:48:17 -0400 Subject: [PATCH 10/22] consumer validation checks --- contracts/babylon/src/ibc.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 136e879b..225640fd 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -77,6 +77,13 @@ pub fn ibc_channel_connect( // Load the config let cfg = CONFIG.load(deps.storage)?; + // Check if consumer name or description is empty + if cfg.consumer_name.is_empty() || cfg.consumer_description.is_empty() { + return Err(ContractError::InvalidConfig { + msg: "Consumer name and description must not be empty".to_string(), + }); + } + // Create the ConsumerRegisterIBCPacket let consumer_register_packet = ConsumerRegisterIbcPacket { consumer_name: cfg.consumer_name, From 9c1591b3dcb9fff7f0c72a0e74e04509f8756b6a Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 11:04:08 -0400 Subject: [PATCH 11/22] add todo --- contracts/babylon/src/error.rs | 4 +++- contracts/babylon/src/ibc.rs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/contracts/babylon/src/error.rs b/contracts/babylon/src/error.rs index 61b515e5..7816edc1 100644 --- a/contracts/babylon/src/error.rs +++ b/contracts/babylon/src/error.rs @@ -34,6 +34,8 @@ pub enum ContractError { Unauthorized {}, #[error("The BTC staking contract is not set")] BtcStakingNotSet {}, + #[error("Invalid configuration: {msg}")] + InvalidConfig { msg: String }, } #[derive(Error, Debug, PartialEq)] @@ -127,4 +129,4 @@ pub enum BabylonEpochChainError { EmptyTxKey {}, #[error("The BTC header cannot be decoded")] BTCHeaderDecodeError {}, -} +} \ No newline at end of file diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 225640fd..0a467083 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -74,6 +74,8 @@ pub fn ibc_channel_connect( // Store the channel IBC_CHANNEL.save(deps.storage, channel)?; + // TODO: send IBC packet only on ChannelOpenConfirm? + // Load the config let cfg = CONFIG.load(deps.storage)?; From 4d6e02e9e50eb2a9eb4858f85302db9391bc3cdf Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 11:14:18 -0400 Subject: [PATCH 12/22] fix lint --- contracts/babylon/benches/main.rs | 4 +++- contracts/babylon/tests/integration.rs | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/contracts/babylon/benches/main.rs b/contracts/babylon/benches/main.rs index 3ce8c0ca..16337811 100644 --- a/contracts/babylon/benches/main.rs +++ b/contracts/babylon/benches/main.rs @@ -44,6 +44,8 @@ pub fn setup_instance() -> Instance { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), + consumer_name: "TestConsumer".to_string(), + consumer_description: "Test Consumer Description".to_string(), btc_confirmation_depth: 10, checkpoint_finalization_timeout: 1, notify_cosmos_zone: false, @@ -172,4 +174,4 @@ criterion_group!( config = make_config(); targets = bench_btc_light_client ); -criterion_main!(btc_light_client); +criterion_main!(btc_light_client); \ No newline at end of file diff --git a/contracts/babylon/tests/integration.rs b/contracts/babylon/tests/integration.rs index d1c25543..245b7356 100644 --- a/contracts/babylon/tests/integration.rs +++ b/contracts/babylon/tests/integration.rs @@ -41,6 +41,8 @@ fn setup() -> Instance { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), + consumer_name: "test_consumer".to_string(), + consumer_description: "Test consumer description".to_string(), btc_confirmation_depth: 10, checkpoint_finalization_timeout: 99, notify_cosmos_zone: false, @@ -90,6 +92,8 @@ fn instantiate_works() { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), + consumer_name: "test_consumer".to_string(), + consumer_description: "Test consumer description".to_string(), btc_confirmation_depth: 10, checkpoint_finalization_timeout: 100, notify_cosmos_zone: false, @@ -183,4 +187,4 @@ fn btc_headers_query_works() { test_headers[..10], res.headers.iter().map(Into::into).collect::>() ); -} +} \ No newline at end of file From 4e188dcb6fdf3f0b61f10939aa2a953db8cd4778 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 11:17:11 -0400 Subject: [PATCH 13/22] fix lint --- contracts/babylon/src/ibc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 0a467083..2245af75 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -13,7 +13,7 @@ use cosmwasm_std::{ }; use cw_storage_plus::Item; use prost::Message; -use crate::state::config::{Config, CONFIG}; +use crate::state::config::CONFIG; pub const IBC_VERSION: &str = "zoneconcierge-1"; pub const IBC_ORDERING: IbcOrder = IbcOrder::Ordered; From f5b8c7ebce88de2274c935070ca3dbc4f937a7e7 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Fri, 9 Aug 2024 11:18:44 -0400 Subject: [PATCH 14/22] debug log --- contracts/babylon/src/ibc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 2245af75..4eb3d4e9 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -92,6 +92,8 @@ pub fn ibc_channel_connect( consumer_description: cfg.consumer_description, }; + deps.api.debug(&format!("ConsumerRegisterIBCPacket: {:?}", consumer_register_packet.clone())); + // Create the ZoneconciergePacketData let packet_data = ZoneconciergePacketData { packet: Some(Packet::ConsumerRegister(consumer_register_packet)), From 00c45976ba9bd4d6b3c38d954cdbbb846341c381 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Mon, 12 Aug 2024 16:26:45 -0400 Subject: [PATCH 15/22] remove circleci --- .circleci/config.yml | 213 ------------------------------------------- 1 file changed, 213 deletions(-) delete mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 854fb35e..00000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,213 +0,0 @@ -# Use the latest 2.1 version of CircleCI pipeline process engine. -# See: https://circleci.com/docs/configuration-reference -version: 2.1 - -orbs: - aws-cli: circleci/aws-cli@4.0 - aws-s3: circleci/aws-s3@4.0 - -jobs: - local-build-test: - docker: - - image: cimg/rust:1.78.0 - steps: - - checkout - - run: - name: Set up Rust environment - command: | - rustup component add rustfmt clippy - - run: - name: Build Babylon contracts - command: | - cargo build - - run: - name: Test generating schema - command: | - (cd ./contracts/babylon && cargo run --bin schema) - (cd ./contracts/btc-staking && cargo run --bin btcstaking-schema) - - run: - name: Check formats and run unit tests - command: | - cargo test --lib - - wasm-build-check-integration: - docker: - - image: cimg/rust:1.78.0 - steps: - - checkout - - setup_remote_docker - - run: - name: Build optimised Wasm binaries - command: | - ./scripts/build-optimizer-ci.sh - - run: - name: Install cosmwasm-check - # Uses --debug for compilation speed - command: cargo install --locked --debug --version ^2.0 cosmwasm-check - - run: - name: Check the Wasm binaries' validity - command: | - cosmwasm-check /home/circleci/project/artifacts/*.wasm - - run: - name: Integration tests based on CosmWasm - command: | - cargo test --test integration - - persist_to_workspace: - root: /home/circleci/project/artifacts - paths: - - babylon_contract.wasm - - btc_staking.wasm - - benchmarking: - docker: - - image: cimg/rust:1.78.0 - steps: - - checkout - - setup_remote_docker - - run: - name: Build optimised Wasm binaries - command: | - ./scripts/build-optimizer-ci.sh - - run: - name: Version information (default; stable) - command: rustc --version && cargo --version - - run: - name: Run babylon contract benchmarks - working_directory: ~/project/contracts/babylon - command: | - cargo clean - cargo bench --locked -p babylon-contract -- --color never --save-baseline btc_light_client - - push-to-s3: - machine: - image: default - resource_class: large - steps: - - attach_workspace: - at: /tmp - - aws-cli/setup: - aws_access_key_id: AWS_ACCESS_KEY_ID - aws_secret_access_key: AWS_SECRET_ACCESS_KEY - region: "$AWS_REGION" - - aws-s3/copy: - from: /tmp/babylon_contract.wasm - to: $S3_BUCKET_PREFIX/$CIRCLE_PROJECT_REPONAME-$CIRCLE_SHA1.wasm - - # This job roughly follows the instructions from https://circleci.com/blog/publishing-to-github-releases-via-circleci/ - build_and_upload_contracts: - docker: - - image: cimg/rust:1.78.0 - working_directory: /home/circleci/project - steps: - - checkout: - path: /home/circleci/project - - setup_remote_docker - - run: - name: Make temporary bin - command: mkdir -p /tmp/bin - - run: - name: Install ghr - command: wget https://github.com/tcnksm/ghr/releases/download/v0.14.0/ghr_v0.14.0_linux_amd64.tar.gz -O - | tar -zxvf - -C /tmp/bin --wildcards --strip-components 1 */ghr - - run: - name: Build optimised Wasm binaries - command: | - ./scripts/build-optimizer-ci.sh - - persist_to_workspace: - root: /home/circleci/project/artifacts - paths: - - babylon_contract.wasm - - btc_staking.wasm - - checksums.txt - - run: - name: Show data - command: | - ls -l artifacts - cat artifacts/checksums.txt - - run: - name: Publish artifacts on GitHub - command: | - TAG="$CIRCLE_TAG" - TITLE="$TAG" - BODY="Attached there are some build artifacts generated at this tag. Those are for development purposes only! Please use crates.io to find the packages of this release." - /tmp/bin/ghr -t "$GITHUB_TOKEN" \ - -u "$CIRCLE_PROJECT_USERNAME" -r "$CIRCLE_PROJECT_REPONAME" \ - -c "$CIRCLE_SHA1" \ - -n "$TITLE" -b "$BODY" \ - -replace \ - "$TAG" ./artifacts/ - - build_and_upload_schemas: - docker: - - image: cimg/rust:1.78.0 - working_directory: /home/circleci/project - steps: - - checkout: - path: /home/circleci/project - - run: - name: Make temporary bin - command: mkdir -p /tmp/bin - - run: - name: Install ghr - command: wget https://github.com/tcnksm/ghr/releases/download/v0.14.0/ghr_v0.14.0_linux_amd64.tar.gz -O - | tar -zxvf - -C /tmp/bin --wildcards --strip-components 1 */ghr - - run: - name: Build and run schema generator - command: | - (cd ./contracts/babylon && cargo run --bin schema) - (cd ./contracts/btc-staking && cargo run --bin btcstaking-schema) - - run: - name: Consolidate schemas - command: | - mkdir -p ./schemas - cp -a ./contracts/*/schema/* ./schemas - - run: - name: Show data - command: ls -l ./schemas - - run: - name: Publish schemas on GitHub - command: | - TAG="$CIRCLE_TAG" - TITLE="$TAG" - BODY="Attached there are some schemas and build artifacts generated at this tag. Those are for development purposes only! Please use crates.io to find the packages of this release." - /tmp/bin/ghr -t "$GITHUB_TOKEN" \ - -u "$CIRCLE_PROJECT_USERNAME" -r "$CIRCLE_PROJECT_REPONAME" \ - -c "$CIRCLE_SHA1" \ - -n "$TITLE" -b "$BODY" \ - -replace \ - "$TAG" ./schemas/ - -# Orchestrate jobs using workflows -# See: https://circleci.com/docs/configuration-reference/#workflows -workflows: - local-tests: - jobs: - - local-build-test - wasm-tests: - jobs: - - wasm-build-check-integration - - benchmarking: - requires: - - wasm-build-check-integration - filters: - branches: - only: - # Long living branches - - main-disable - - /^[0-9]+\.[0-9]+-disable$/ - # Add your branch here if benchmarking matters to your work - - push-to-s3: - requires: - - wasm-build-check-integration - deploy: - jobs: - - build_and_upload_contracts: - filters: - tags: - only: /^v[0-9]+\.[0-9]+\.[0-9]+.*/ - branches: - ignore: /.*/ - - build_and_upload_schemas: - filters: - tags: - only: /^v[0-9]+\.[0-9]+\.[0-9]+.*/ - branches: - ignore: /.*/ From ff0953359bd9ff58a81d3dd162d308004925bc4f Mon Sep 17 00:00:00 2001 From: Gurjot Date: Mon, 12 Aug 2024 16:30:30 -0400 Subject: [PATCH 16/22] send ibc --- contracts/babylon/src/contract.rs | 5 ++++ contracts/babylon/src/ibc.rs | 40 +++++++++++++++++++-------- contracts/babylon/src/msg/contract.rs | 13 ++++++++- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index 23fea9a1..466a39cb 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -24,6 +24,11 @@ pub fn instantiate( _info: MessageInfo, msg: InstantiateMsg, ) -> Result, ContractError> { + deps.api.debug("CONTRACT: Entering instantiate function"); + deps.api.debug(&format!("CONTRACT: Consumer Name: {}", msg.consumer_name)); + deps.api.debug(&format!("CONTRACT: Consumer Description: {}", msg.consumer_description)); + + msg.validate()?; // initialise config diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 4eb3d4e9..979ddd34 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -6,10 +6,7 @@ use babylon_proto::babylon::zoneconcierge::v1::{ use babylon_proto::babylon::btcstkconsumer::v1::ConsumerRegisterIbcPacket; use cosmwasm_std::{ - DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, - IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcOrder, IbcPacketAckMsg, - IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout, Never, StdAck, - StdError, StdResult, IbcMsg, to_json_binary, + from_json, Binary, DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout, Never, StdAck, StdError, StdResult }; use cw_storage_plus::Item; use prost::Message; @@ -31,6 +28,7 @@ pub fn ibc_channel_open( _env: Env, msg: IbcChannelOpenMsg, ) -> Result { + deps.api.debug("CONTRACT: Entered function: ibc_channel_open"); // Ensure we have no channel yet if IBC_CHANNEL.may_load(deps.storage)?.is_some() { return Err(ContractError::IbcChannelAlreadyOpen {}); @@ -63,7 +61,7 @@ pub fn ibc_channel_connect( env: &Env, msg: IbcChannelConnectMsg, ) -> Result { - deps.api.debug("Entered function: ibc_channel_connect"); + deps.api.debug("CONTRACT: Entered function: ibc_channel_connect"); // Ensure we have no channel yet if IBC_CHANNEL.may_load(deps.storage)?.is_some() { @@ -92,7 +90,7 @@ pub fn ibc_channel_connect( consumer_description: cfg.consumer_description, }; - deps.api.debug(&format!("ConsumerRegisterIBCPacket: {:?}", consumer_register_packet.clone())); + deps.api.debug(&format!("ConsumerRegisterIBCPacket:\n{:#?}", consumer_register_packet)); // Create the ZoneconciergePacketData let packet_data = ZoneconciergePacketData { @@ -104,7 +102,7 @@ pub fn ibc_channel_connect( // Create the IBC packet message let ibc_msg = IbcMsg::SendPacket { channel_id: channel.endpoint.channel_id.clone(), - data: to_json_binary(&packet_data_bytes)?, + data: Binary::new(packet_data_bytes), timeout: packet_timeout(env), }; @@ -112,9 +110,11 @@ pub fn ibc_channel_connect( let chan_id = &channel.endpoint.channel_id; Ok(IbcBasicResponse::new() + .add_message(ibc_msg) .add_attribute("action", "ibc_connect") .add_attribute("channel_id", chan_id) - .add_event(Event::new("ibc").add_attribute("channel", "connect"))) + .add_event(Event::new("ibc").add_attribute("channel", "connect")) + ) } /// This is invoked on the IBC Channel Close message @@ -382,11 +382,29 @@ pub fn packet_timeout(env: &Env) -> IbcTimeout { } pub fn ibc_packet_ack( - _deps: DepsMut, + deps: DepsMut, _env: Env, - _msg: IbcPacketAckMsg, + msg: IbcPacketAckMsg, ) -> Result { - Ok(IbcBasicResponse::default()) + deps.api.debug(&format!("Received acknowledgement: {:?}", msg.acknowledgement)); + + // Parse the acknowledgement and handle success/error + let ack: StdAck = from_json(&msg.acknowledgement.data)?; + match ack { + StdAck::Success(data) => { + // Handle successful acknowledgement + deps.api.debug(&format!("Packet successfully processed by receiver: {:?}", data)); + // Add any necessary state updates or further actions + } + StdAck::Error(error) => { + // Handle error acknowledgement + deps.api.debug(&format!("Packet processing error: {}", error)); + // Add any necessary error handling or recovery logic + } + } + + Ok(IbcBasicResponse::new() + .add_attribute("action", "acknowledge_packet")) } pub fn ibc_packet_timeout( diff --git a/contracts/babylon/src/msg/contract.rs b/contracts/babylon/src/msg/contract.rs index 77b5baa2..803e956d 100644 --- a/contracts/babylon/src/msg/contract.rs +++ b/contracts/babylon/src/msg/contract.rs @@ -48,6 +48,17 @@ impl ContractMsg for InstantiateMsg { )); } let _ = self.babylon_tag_to_bytes()?; + + // Check if consumer name is empty + if self.consumer_name.trim().is_empty() { + return Err(StdError::generic_err("Consumer name cannot be empty")); + } + + // Check if consumer description is empty + if self.consumer_description.trim().is_empty() { + return Err(StdError::generic_err("Consumer description cannot be empty")); + } + Ok(()) } @@ -129,4 +140,4 @@ pub enum QueryMsg { /// CzHeader returns the CZ header stored in the contract, by CZ height. #[returns(CzHeaderResponse)] CzHeader { height: u64 }, -} +} \ No newline at end of file From 00e00fec067c00a5aa92302b9b663ec4a37ea5b4 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Mon, 12 Aug 2024 17:24:58 -0400 Subject: [PATCH 17/22] remove debug logs --- contracts/babylon/src/contract.rs | 5 ----- contracts/babylon/src/ibc.rs | 31 +++---------------------------- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index 466a39cb..23fea9a1 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -24,11 +24,6 @@ pub fn instantiate( _info: MessageInfo, msg: InstantiateMsg, ) -> Result, ContractError> { - deps.api.debug("CONTRACT: Entering instantiate function"); - deps.api.debug(&format!("CONTRACT: Consumer Name: {}", msg.consumer_name)); - deps.api.debug(&format!("CONTRACT: Consumer Description: {}", msg.consumer_description)); - - msg.validate()?; // initialise config diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 979ddd34..874c9c6b 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -28,7 +28,6 @@ pub fn ibc_channel_open( _env: Env, msg: IbcChannelOpenMsg, ) -> Result { - deps.api.debug("CONTRACT: Entered function: ibc_channel_open"); // Ensure we have no channel yet if IBC_CHANNEL.may_load(deps.storage)?.is_some() { return Err(ContractError::IbcChannelAlreadyOpen {}); @@ -61,8 +60,6 @@ pub fn ibc_channel_connect( env: &Env, msg: IbcChannelConnectMsg, ) -> Result { - deps.api.debug("CONTRACT: Entered function: ibc_channel_connect"); - // Ensure we have no channel yet if IBC_CHANNEL.may_load(deps.storage)?.is_some() { return Err(ContractError::IbcChannelAlreadyOpen {}); @@ -90,8 +87,6 @@ pub fn ibc_channel_connect( consumer_description: cfg.consumer_description, }; - deps.api.debug(&format!("ConsumerRegisterIBCPacket:\n{:#?}", consumer_register_packet)); - // Create the ZoneconciergePacketData let packet_data = ZoneconciergePacketData { packet: Some(Packet::ConsumerRegister(consumer_register_packet)), @@ -106,8 +101,6 @@ pub fn ibc_channel_connect( timeout: packet_timeout(env), }; - deps.api.debug(&format!("Sending IBC message from ibc_channel_connect: {:?}", ibc_msg)); - let chan_id = &channel.endpoint.channel_id; Ok(IbcBasicResponse::new() .add_message(ibc_msg) @@ -382,29 +375,11 @@ pub fn packet_timeout(env: &Env) -> IbcTimeout { } pub fn ibc_packet_ack( - deps: DepsMut, + _deps: DepsMut, _env: Env, - msg: IbcPacketAckMsg, + _msg: IbcPacketAckMsg, ) -> Result { - deps.api.debug(&format!("Received acknowledgement: {:?}", msg.acknowledgement)); - - // Parse the acknowledgement and handle success/error - let ack: StdAck = from_json(&msg.acknowledgement.data)?; - match ack { - StdAck::Success(data) => { - // Handle successful acknowledgement - deps.api.debug(&format!("Packet successfully processed by receiver: {:?}", data)); - // Add any necessary state updates or further actions - } - StdAck::Error(error) => { - // Handle error acknowledgement - deps.api.debug(&format!("Packet processing error: {}", error)); - // Add any necessary error handling or recovery logic - } - } - - Ok(IbcBasicResponse::new() - .add_attribute("action", "acknowledge_packet")) + Ok(IbcBasicResponse::default()) } pub fn ibc_packet_timeout( From d3ccd4d7ef9d2e80a4d0215a4ffb4ef594d4f2ba Mon Sep 17 00:00:00 2001 From: Gurjot Date: Mon, 12 Aug 2024 17:26:30 -0400 Subject: [PATCH 18/22] fix warning --- contracts/babylon/src/ibc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 874c9c6b..21674721 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -6,7 +6,7 @@ use babylon_proto::babylon::zoneconcierge::v1::{ use babylon_proto::babylon::btcstkconsumer::v1::ConsumerRegisterIbcPacket; use cosmwasm_std::{ - from_json, Binary, DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout, Never, StdAck, StdError, StdResult + Binary, DepsMut, Env, Event, Ibc3ChannelOpenResponse, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg, IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, IbcTimeout, Never, StdAck, StdError, StdResult }; use cw_storage_plus::Item; use prost::Message; From 57a1d7823015d90f86fc2a95eed7b9b613b9a680 Mon Sep 17 00:00:00 2001 From: Gurjot Date: Tue, 13 Aug 2024 10:20:18 -0400 Subject: [PATCH 19/22] pr comment: remove redundant checks --- contracts/babylon/src/ibc.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index 21674721..a27ccbde 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -74,13 +74,6 @@ pub fn ibc_channel_connect( // Load the config let cfg = CONFIG.load(deps.storage)?; - // Check if consumer name or description is empty - if cfg.consumer_name.is_empty() || cfg.consumer_description.is_empty() { - return Err(ContractError::InvalidConfig { - msg: "Consumer name and description must not be empty".to_string(), - }); - } - // Create the ConsumerRegisterIBCPacket let consumer_register_packet = ConsumerRegisterIbcPacket { consumer_name: cfg.consumer_name, From 76f7789469cdc0ba9c5686dc66d3f00a6887c55e Mon Sep 17 00:00:00 2001 From: Gurjot Date: Tue, 13 Aug 2024 10:57:32 -0400 Subject: [PATCH 20/22] pr comment: add consumer info in event attributes --- contracts/babylon/src/ibc.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index a27ccbde..f8131025 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -76,8 +76,8 @@ pub fn ibc_channel_connect( // Create the ConsumerRegisterIBCPacket let consumer_register_packet = ConsumerRegisterIbcPacket { - consumer_name: cfg.consumer_name, - consumer_description: cfg.consumer_description, + consumer_name: cfg.consumer_name.clone(), + consumer_description: cfg.consumer_description.clone(), }; // Create the ZoneconciergePacketData @@ -99,6 +99,8 @@ pub fn ibc_channel_connect( .add_message(ibc_msg) .add_attribute("action", "ibc_connect") .add_attribute("channel_id", chan_id) + .add_attribute("consumer_name", cfg.consumer_name.clone()) + .add_attribute("consumer_description", cfg.consumer_description.clone()) .add_event(Event::new("ibc").add_attribute("channel", "connect")) ) } From 09e91492f269e87971afee0f90fbef58ee574fac Mon Sep 17 00:00:00 2001 From: Gurjot Date: Tue, 13 Aug 2024 11:01:57 -0400 Subject: [PATCH 21/22] pr comment: fix strings in consumer tests --- contracts/babylon/src/contract.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index 23fea9a1..fc6af82e 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -216,8 +216,8 @@ mod tests { btc_staking_code_id: None, btc_staking_msg: None, admin: None, - consumer_name: "MyConsumer".to_string(), - consumer_description: "This is a description of my consumer".to_string(), + consumer_name: "TestConsumer".to_string(), + consumer_description: "Test Consumer Description".to_string(), }; let info = message_info(&deps.api.addr_make(CREATOR), &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); From 16a2283ae1cc50fc5bac0b8d18a69f18e663a728 Mon Sep 17 00:00:00 2001 From: Gurjot Singh <111540954+gusin13@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:53:56 -0400 Subject: [PATCH 22/22] babylon contract should be able to instantiate w/o consumer metadata (#42) --- contracts/babylon/benches/main.rs | 4 +- contracts/babylon/src/contract.rs | 23 +++++--- contracts/babylon/src/ibc.rs | 59 +++++++++---------- contracts/babylon/src/lib.rs | 2 +- contracts/babylon/src/msg/contract.rs | 23 ++++---- contracts/babylon/src/multitest/suite.rs | 4 +- .../babylon/src/state/btc_light_client.rs | 4 +- contracts/babylon/src/state/config.rs | 8 ++- contracts/babylon/tests/integration.rs | 8 +-- 9 files changed, 73 insertions(+), 62 deletions(-) diff --git a/contracts/babylon/benches/main.rs b/contracts/babylon/benches/main.rs index 16337811..a682cc9b 100644 --- a/contracts/babylon/benches/main.rs +++ b/contracts/babylon/benches/main.rs @@ -44,8 +44,8 @@ pub fn setup_instance() -> Instance { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), - consumer_name: "TestConsumer".to_string(), - consumer_description: "Test Consumer Description".to_string(), + consumer_name: None, + consumer_description: None, btc_confirmation_depth: 10, checkpoint_finalization_timeout: 1, notify_cosmos_zone: false, diff --git a/contracts/babylon/src/contract.rs b/contracts/babylon/src/contract.rs index fc6af82e..4c557c42 100644 --- a/contracts/babylon/src/contract.rs +++ b/contracts/babylon/src/contract.rs @@ -26,22 +26,25 @@ pub fn instantiate( ) -> Result, ContractError> { msg.validate()?; - // initialise config - let cfg = Config { + // Initialize config with None values for consumer fields + let mut cfg = Config { network: msg.network.clone(), babylon_tag: msg.babylon_tag_to_bytes()?, btc_confirmation_depth: msg.btc_confirmation_depth, checkpoint_finalization_timeout: msg.checkpoint_finalization_timeout, notify_cosmos_zone: msg.notify_cosmos_zone, btc_staking: None, // Will be set in `reply` if `btc_staking_code_id` is provided - consumer_name: msg.consumer_name, - consumer_description: msg.consumer_description, + consumer_name: None, + consumer_description: None, }; - CONFIG.save(deps.storage, &cfg)?; let mut res = Response::new().add_attribute("action", "instantiate"); if let Some(btc_staking_code_id) = msg.btc_staking_code_id { + // Update config with consumer information + cfg.consumer_name = msg.consumer_name; + cfg.consumer_description = msg.consumer_description; + // Instantiate BTC staking contract let init_msg = WasmMsg::Instantiate { admin: msg.admin, @@ -54,6 +57,10 @@ pub fn instantiate( res = res.add_submessage(init_msg); } + + // Save the config after potentially updating it + CONFIG.save(deps.storage, &cfg)?; + Ok(res) } @@ -216,11 +223,11 @@ mod tests { btc_staking_code_id: None, btc_staking_msg: None, admin: None, - consumer_name: "TestConsumer".to_string(), - consumer_description: "Test Consumer Description".to_string(), + consumer_name: None, + consumer_description: None, }; let info = message_info(&deps.api.addr_make(CREATOR), &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); } -} +} \ No newline at end of file diff --git a/contracts/babylon/src/ibc.rs b/contracts/babylon/src/ibc.rs index f8131025..b262412d 100644 --- a/contracts/babylon/src/ibc.rs +++ b/contracts/babylon/src/ibc.rs @@ -57,7 +57,7 @@ pub fn ibc_channel_open( /// Second part of the 4-step handshake, i.e. ChannelOpenAck and ChannelOpenConfirm. pub fn ibc_channel_connect( deps: DepsMut, - env: &Env, + env: Env, msg: IbcChannelConnectMsg, ) -> Result { // Ensure we have no channel yet @@ -69,40 +69,39 @@ pub fn ibc_channel_connect( // Store the channel IBC_CHANNEL.save(deps.storage, channel)?; - // TODO: send IBC packet only on ChannelOpenConfirm? - // Load the config let cfg = CONFIG.load(deps.storage)?; - // Create the ConsumerRegisterIBCPacket - let consumer_register_packet = ConsumerRegisterIbcPacket { - consumer_name: cfg.consumer_name.clone(), - consumer_description: cfg.consumer_description.clone(), - }; + let chan_id = &channel.endpoint.channel_id; + let mut response = IbcBasicResponse::new() + .add_attribute("action", "ibc_connect") + .add_attribute("channel_id", chan_id) + .add_event(Event::new("ibc").add_attribute("channel", "connect")); - // Create the ZoneconciergePacketData - let packet_data = ZoneconciergePacketData { - packet: Some(Packet::ConsumerRegister(consumer_register_packet)), - }; + // If the consumer name and description are set, create and send a ConsumerRegister packet + if let (Some(name), Some(description)) = (&cfg.consumer_name, &cfg.consumer_description) { + let consumer_register_packet = ConsumerRegisterIbcPacket { + consumer_name: name.clone(), + consumer_description: description.clone(), + }; - let packet_data_bytes = packet_data.encode_to_vec(); + let packet_data = ZoneconciergePacketData { + packet: Some(Packet::ConsumerRegister(consumer_register_packet)), + }; - // Create the IBC packet message - let ibc_msg = IbcMsg::SendPacket { - channel_id: channel.endpoint.channel_id.clone(), - data: Binary::new(packet_data_bytes), - timeout: packet_timeout(env), - }; + let ibc_msg = IbcMsg::SendPacket { + channel_id: channel.endpoint.channel_id.clone(), + data: Binary::new(packet_data.encode_to_vec()), + timeout: packet_timeout(&env), + }; - let chan_id = &channel.endpoint.channel_id; - Ok(IbcBasicResponse::new() - .add_message(ibc_msg) - .add_attribute("action", "ibc_connect") - .add_attribute("channel_id", chan_id) - .add_attribute("consumer_name", cfg.consumer_name.clone()) - .add_attribute("consumer_description", cfg.consumer_description.clone()) - .add_event(Event::new("ibc").add_attribute("channel", "connect")) - ) + response = response + .add_message(ibc_msg) + .add_attribute("consumer_name", name) + .add_attribute("consumer_description", description); + } + + Ok(response) } /// This is invoked on the IBC Channel Close message @@ -413,8 +412,8 @@ mod tests { btc_staking_code_id: None, btc_staking_msg: None, admin: None, - consumer_name: "TestConsumer".to_string(), - consumer_description: "Test Consumer Description".to_string(), + consumer_name: None, + consumer_description: None, }; let info = message_info(&deps.api.addr_make(CREATOR), &[]); let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); diff --git a/contracts/babylon/src/lib.rs b/contracts/babylon/src/lib.rs index 3aa172eb..dd7414f3 100644 --- a/contracts/babylon/src/lib.rs +++ b/contracts/babylon/src/lib.rs @@ -73,7 +73,7 @@ pub fn ibc_channel_connect( env: Env, msg: IbcChannelConnectMsg, ) -> Result { - ibc::ibc_channel_connect(deps, &env, msg) + ibc::ibc_channel_connect(deps, env, msg) } #[cfg_attr(not(feature = "library"), entry_point)] diff --git a/contracts/babylon/src/msg/contract.rs b/contracts/babylon/src/msg/contract.rs index 803e956d..13273a05 100644 --- a/contracts/babylon/src/msg/contract.rs +++ b/contracts/babylon/src/msg/contract.rs @@ -34,9 +34,9 @@ pub struct InstantiateMsg { /// If set, this will be the Wasm migration / upgrade admin of the BTC staking contract pub admin: Option, /// Name of the consumer - pub consumer_name: String, + pub consumer_name: Option, /// Description of the consumer - pub consumer_description: String, + pub consumer_description: Option, } impl ContractMsg for InstantiateMsg { @@ -49,14 +49,17 @@ impl ContractMsg for InstantiateMsg { } let _ = self.babylon_tag_to_bytes()?; - // Check if consumer name is empty - if self.consumer_name.trim().is_empty() { - return Err(StdError::generic_err("Consumer name cannot be empty")); - } - - // Check if consumer description is empty - if self.consumer_description.trim().is_empty() { - return Err(StdError::generic_err("Consumer description cannot be empty")); + if self.btc_staking_code_id.is_some() { + if let (Some(consumer_name), Some(consumer_description)) = (&self.consumer_name, &self.consumer_description) { + if consumer_name.trim().is_empty() { + return Err(StdError::generic_err("Consumer name cannot be empty")); + } + if consumer_description.trim().is_empty() { + return Err(StdError::generic_err("Consumer description cannot be empty")); + } + } else { + return Err(StdError::generic_err("Consumer name and description are required when btc_staking_code_id is set")); + } } Ok(()) diff --git a/contracts/babylon/src/multitest/suite.rs b/contracts/babylon/src/multitest/suite.rs index 87842797..b99dae9e 100644 --- a/contracts/babylon/src/multitest/suite.rs +++ b/contracts/babylon/src/multitest/suite.rs @@ -67,8 +67,8 @@ impl SuiteBuilder { btc_staking_code_id: Some(btc_staking_code_id), btc_staking_msg: None, admin: Some(owner.to_string()), - consumer_name: "TestConsumer".to_string(), - consumer_description: "Test Consumer Description".to_string(), + consumer_name: Some("TestConsumer".to_string()), + consumer_description: Some("Test Consumer Description".to_string()), }, &[], "babylon", diff --git a/contracts/babylon/src/state/btc_light_client.rs b/contracts/babylon/src/state/btc_light_client.rs index f12df343..db7e0e95 100644 --- a/contracts/babylon/src/state/btc_light_client.rs +++ b/contracts/babylon/src/state/btc_light_client.rs @@ -357,8 +357,8 @@ pub(crate) mod tests { checkpoint_finalization_timeout: w as u64, notify_cosmos_zone: false, btc_staking: None, - consumer_name: "TestConsumer".to_string(), - consumer_description: "Test Consumer Description".to_string(), + consumer_name: None, + consumer_description: None, }; CONFIG.save(storage, &cfg).unwrap(); w diff --git a/contracts/babylon/src/state/config.rs b/contracts/babylon/src/state/config.rs index 841ae919..8eafcf81 100644 --- a/contracts/babylon/src/state/config.rs +++ b/contracts/babylon/src/state/config.rs @@ -16,6 +16,8 @@ pub struct Config { pub notify_cosmos_zone: bool, /// If set, this stores a BTC staking contract used for BTC re-staking pub btc_staking: Option, - pub consumer_name: String, - pub consumer_description: String, -} + /// Consumer name + pub consumer_name: Option, + /// Consumer description + pub consumer_description: Option, +} \ No newline at end of file diff --git a/contracts/babylon/tests/integration.rs b/contracts/babylon/tests/integration.rs index 245b7356..6858b9a9 100644 --- a/contracts/babylon/tests/integration.rs +++ b/contracts/babylon/tests/integration.rs @@ -41,8 +41,8 @@ fn setup() -> Instance { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), - consumer_name: "test_consumer".to_string(), - consumer_description: "Test consumer description".to_string(), + consumer_name: None, + consumer_description: None, btc_confirmation_depth: 10, checkpoint_finalization_timeout: 99, notify_cosmos_zone: false, @@ -92,8 +92,8 @@ fn instantiate_works() { let msg = InstantiateMsg { network: babylon_bitcoin::chain_params::Network::Regtest, babylon_tag: "01020304".to_string(), - consumer_name: "test_consumer".to_string(), - consumer_description: "Test consumer description".to_string(), + consumer_name: None, + consumer_description: None, btc_confirmation_depth: 10, checkpoint_finalization_timeout: 100, notify_cosmos_zone: false,