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

feat: Automatic consumer registration #34

Merged
merged 22 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 18 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
213 changes: 0 additions & 213 deletions .circleci/config.yml

This file was deleted.

4 changes: 3 additions & 1 deletion contracts/babylon/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub fn setup_instance() -> Instance<MockApi, MockStorage, MockQuerier> {
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,
Expand Down Expand Up @@ -172,4 +174,4 @@ criterion_group!(
config = make_config();
targets = bench_btc_light_client
);
criterion_main!(btc_light_client);
criterion_main!(btc_light_client);
4 changes: 4 additions & 0 deletions contracts/babylon/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down Expand Up @@ -214,6 +216,8 @@ mod tests {
btc_staking_code_id: None,
btc_staking_msg: None,
admin: None,
consumer_name: "MyConsumer".to_string(),
gusin13 marked this conversation as resolved.
Show resolved Hide resolved
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();
Expand Down
4 changes: 3 additions & 1 deletion contracts/babylon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -127,4 +129,4 @@ pub enum BabylonEpochChainError {
EmptyTxKey {},
#[error("The BTC header cannot be decoded")]
BTCHeaderDecodeError {},
}
}
53 changes: 46 additions & 7 deletions contracts/babylon/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ 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,
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;
use crate::state::config::CONFIG;

pub const IBC_VERSION: &str = "zoneconcierge-1";
pub const IBC_ORDERING: IbcOrder = IbcOrder::Ordered;
Expand Down Expand Up @@ -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<IbcBasicResponse, ContractError> {
// Ensure we have no channel yet
Expand All @@ -69,11 +69,45 @@ 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)?;

// 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(),
});
}
gusin13 marked this conversation as resolved.
Show resolved Hide resolved

// 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: Binary::new(packet_data_bytes),
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_event(Event::new("ibc").add_attribute("channel", "connect")))
.add_event(Event::new("ibc").add_attribute("channel", "connect"))
gusin13 marked this conversation as resolved.
Show resolved Hide resolved
)
}

/// This is invoked on the IBC Channel Close message
Expand Down Expand Up @@ -123,7 +157,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| {
Expand Down Expand Up @@ -381,6 +418,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();
Expand Down
2 changes: 1 addition & 1 deletion contracts/babylon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn ibc_channel_connect(
env: Env,
msg: IbcChannelConnectMsg,
) -> Result<IbcBasicResponse, ContractError> {
ibc::ibc_channel_connect(deps, env, msg)
ibc::ibc_channel_connect(deps, &env, msg)
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
Loading