Skip to content
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
15 changes: 8 additions & 7 deletions code/xcvm/cosmwasm/contracts/gateway/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
auth,
batch::BatchResponse,
error::{ContractError, Result},
events::make_event,
prelude::*,
Expand All @@ -8,17 +9,17 @@ use crate::{
assets::{ASSETS, LOCAL_ASSETS},
},
};
use cosmwasm_std::{Deps, DepsMut, Response};
use cosmwasm_std::{Deps, DepsMut};
use xc_core::{AssetId, NetworkId};

/// Adds a new asset to the registry; errors out if asset already exists.
pub(crate) fn force_asset(_: auth::Admin, deps: DepsMut, msg: AssetItem) -> Result {
pub(crate) fn force_asset(_: auth::Admin, deps: DepsMut, msg: AssetItem) -> Result<BatchResponse> {
let config = crate::state::load(deps.storage)?;
ASSETS.save(deps.storage, msg.asset_id, &msg)?;
if msg.network_id == config.network_id {
LOCAL_ASSETS.save(deps.storage, msg.local.clone(), &msg)?;
}
Ok(Response::new().add_event(
Ok(BatchResponse::new().add_event(
make_event("assets.forced")
.add_attribute("asset_id", msg.asset_id.to_string())
.add_attribute("denom", msg.denom()),
Expand Down Expand Up @@ -46,14 +47,14 @@ pub(crate) fn force_remove_asset(
_: auth::Auth<auth::policy::Admin>,
deps: DepsMut<'_>,
asset_id: AssetId,
) -> std::result::Result<Response, ContractError> {
) -> std::result::Result<BatchResponse, ContractError> {
let config = crate::state::load(deps.storage)?;
let asset = ASSETS.load(deps.storage, asset_id)?;
ASSETS.remove(deps.storage, asset_id);
if asset.network_id == config.network_id {
LOCAL_ASSETS.remove(deps.storage, asset.local);
}
Ok(Response::new()
Ok(BatchResponse::new()
.add_event(make_event("assets.removed").add_attribute("asset_id", asset_id.to_string())))
}

Expand All @@ -63,9 +64,9 @@ pub(crate) fn force_asset_to_network_map(
this_asset: AssetId,
other_network: NetworkId,
other_asset: AssetId,
) -> Result {
) -> Result<BatchResponse> {
state::assets::NETWORK_ASSET.save(deps.storage, (this_asset, other_network), &other_asset)?;
Ok(Response::new().add_event(
Ok(BatchResponse::new().add_event(
make_event("assets.forced_asset_to_network_map")
.add_attribute("this_asset", this_asset.to_string())
.add_attribute("other_asset", other_asset.to_string()),
Expand Down
3 changes: 3 additions & 0 deletions code/xcvm/cosmwasm/contracts/gateway/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use xc_core::{gateway::OtherNetworkItem, NetworkId};
///
/// For convenience, type aliases are provided for the different
/// authorisation levels: [`Contract`], [`Interpreter`] and [`Admin`].
#[derive(Clone, Copy)]
pub(crate) struct Auth<T>(core::marker::PhantomData<T>);

/// Authorisation token for messages which can only be sent from the
Expand Down Expand Up @@ -113,8 +114,10 @@ impl<T> Auth<T> {
}

pub(crate) mod policy {
#[derive(Clone, Copy)]
pub(crate) enum Contract {}
pub(crate) enum Interpreter {}
#[derive(Clone, Copy)]
pub(crate) enum Admin {}
pub(crate) enum WasmHook {}
}
41 changes: 41 additions & 0 deletions code/xcvm/cosmwasm/contracts/gateway/src/batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use cosmwasm_std::{CosmosMsg, Event, Response, SubMsg};

#[derive(Debug, Clone, Default)]
pub struct BatchResponse {
pub messages: Vec<SubMsg>,
pub events: Vec<Event>,
}

impl BatchResponse {
pub fn new() -> Self {
<_>::default()
}
pub fn add_message(mut self, msg: impl Into<CosmosMsg>) -> Self {
self.messages.push(SubMsg::new(msg));
self
}

pub fn add_submessage(mut self, msg: SubMsg) -> Self {
self.messages.push(msg);
self
}

pub fn add_event(mut self, event: Event) -> Self {
self.events.push(event);
self
}

pub fn merge(&mut self, mut other: Self) {
self.messages.append(&mut other.messages);
self.events.append(&mut other.events);
}
}

impl From<BatchResponse> for Response {
fn from(resp: BatchResponse) -> Self {
let mut result = Self::new();
result.messages = resp.messages;
result.events = resp.events;
result
}
}
27 changes: 23 additions & 4 deletions code/xcvm/cosmwasm/contracts/gateway/src/contract/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
assets, auth,
batch::BatchResponse,
error::{ContractError, Result},
events::make_event,
interpreter, msg,
Expand Down Expand Up @@ -28,7 +29,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: msg::ExecuteMsg)
match msg {
ExecuteMsg::Config(msg) => {
let auth = auth::Admin::authorise(deps.as_ref(), &info)?;
handle_config_msg(auth, deps, msg, env)
handle_config_msg(auth, deps, msg, &env).map(Into::into)
},

msg::ExecuteMsg::ExecuteProgram { execute_program, tip } =>
Expand Down Expand Up @@ -60,7 +61,12 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: msg::ExecuteMsg)
}
}

fn handle_config_msg(auth: auth::Admin, deps: DepsMut, msg: ConfigSubMsg, env: Env) -> Result {
fn handle_config_msg(
auth: auth::Admin,
mut deps: DepsMut,
msg: ConfigSubMsg,
env: &Env,
) -> Result<BatchResponse> {
deps.api.debug(serde_json_wasm::to_string(&msg)?.as_str());
match msg {
ConfigSubMsg::ForceNetworkToNetwork(msg) =>
Expand All @@ -71,8 +77,21 @@ fn handle_config_msg(auth: auth::Admin, deps: DepsMut, msg: ConfigSubMsg, env: E
ConfigSubMsg::ForceAssetToNetworkMap { this_asset, other_network, other_asset } =>
assets::force_asset_to_network_map(auth, deps, this_asset, other_network, other_asset),
ConfigSubMsg::ForceNetwork(msg) => network::force_network(auth, deps, msg),
ConfigSubMsg::ForceInstantiate { user_origin, salt } =>
interpreter::force_instantiate(auth, env.contract.address, deps, user_origin, salt),
ConfigSubMsg::ForceInstantiate { user_origin, salt } => interpreter::force_instantiate(
auth,
env.contract.address.clone(),
deps,
user_origin,
salt,
),
ConfigSubMsg::Force(msgs) => {
let mut aggregated = BatchResponse::new();
for msg in msgs {
let response = handle_config_msg(auth, deps.branch(), msg, env)?;
aggregated.merge(response);
}
Ok(aggregated)
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions code/xcvm/cosmwasm/contracts/gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub enum ContractError {
GatewayForNetworkNotFound(NetworkId),
#[error("Anonymous calls can do only limitet set of actions")]
AnonymousCallsCanDoOnlyLimitedSetOfActions,
/// use events attributes to return data
#[error("Batched calls cannot return data")]
BatchedCallsCannotReturnData,
Comment thread
dzmitry-lahoda marked this conversation as resolved.
}

impl From<bech32_no_std::Error> for ContractError {
Expand Down
8 changes: 6 additions & 2 deletions code/xcvm/cosmwasm/contracts/gateway/src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
batch::BatchResponse,
contract::INSTANTIATE_INTERPRETER_REPLY_ID,
error::{ContractError, Result},
events::make_event,
Expand All @@ -21,7 +22,7 @@ pub(crate) fn force_instantiate(
deps: DepsMut,
user_origin: Addr,
salt: String,
) -> Result {
) -> Result<BatchResponse> {
let config = load_this(deps.storage)?;
let interpreter_code_id = match config.gateway.expect("expected setup") {
GatewayId::CosmWasm { interpreter_code_id, .. } => interpreter_code_id,
Expand All @@ -32,7 +33,10 @@ pub(crate) fn force_instantiate(
let interpreter_origin =
InterpreterOrigin { user_origin: call_origin.user(config.network_id), salt: salt.clone() };
let msg = instantiate(deps.as_ref(), gateway, interpreter_code_id, &interpreter_origin, salt)?;
Ok(Response::new().add_submessage(msg).add_event(make_event("interpreter.forced")))
Ok(BatchResponse::new().add_submessage(msg).add_event(
make_event("interpreter.forced")
.add_attribute("interpreter_origin", interpreter_origin.to_string()),
))
}

pub fn instantiate(
Expand Down
1 change: 1 addition & 0 deletions code/xcvm/cosmwasm/contracts/gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use xc_core::gateway as msg;

pub mod assets;
pub mod auth;
pub mod batch;
pub mod contract;
pub mod error;
mod events;
Expand Down
25 changes: 15 additions & 10 deletions code/xcvm/cosmwasm/contracts/gateway/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{events::make_event, prelude::*, state::xcvm::IBC_CHANNEL_NETWORK};
use cosmwasm_std::{Response, Storage};
use crate::{
batch::BatchResponse, events::make_event, prelude::*, state::xcvm::IBC_CHANNEL_NETWORK,
};
use cosmwasm_std::{DepsMut, Storage};
use xc_core::{gateway::NetworkItem, NetworkId};

use crate::state::{self, NETWORK, NETWORK_TO_NETWORK};
Expand Down Expand Up @@ -28,25 +30,28 @@ pub fn load_other(storage: &dyn Storage, other: NetworkId) -> Result<OtherNetwor

pub(crate) fn force_network_to_network(
_: crate::auth::Auth<crate::auth::policy::Admin>,
deps: cosmwasm_std::DepsMut,
deps: DepsMut,
msg: xc_core::gateway::ForceNetworkToNetworkMsg,
) -> std::result::Result<cosmwasm_std::Response, crate::error::ContractError> {
) -> std::result::Result<BatchResponse, crate::error::ContractError> {
NETWORK_TO_NETWORK.save(deps.storage, (msg.from, msg.to), &msg.other)?;
if let Some(ibc) = msg.other.xcvm_channel {
IBC_CHANNEL_NETWORK.save(deps.storage, ibc.id.to_string(), &msg.to)?;
}
Ok(Response::new()
.add_event(make_event("network_to_network.forced").add_attribute("to", msg.to.to_string()))
.add_attribute("from", msg.from.to_string()))
Ok(BatchResponse::new().add_event(
make_event("network_to_network.forced")
.add_attribute("to", msg.to.to_string())
.add_attribute("from", msg.from.to_string())
.add_attribute("ics_20", msg.other.ics_20.is_some().to_string()),
))
}

pub(crate) fn force_network(
_auth: crate::auth::Auth<crate::auth::policy::Admin>,
deps: cosmwasm_std::DepsMut,
deps: DepsMut,
msg: NetworkItem,
) -> std::result::Result<cosmwasm_std::Response, crate::error::ContractError> {
) -> crate::error::Result<BatchResponse> {
NETWORK.save(deps.storage, msg.network_id, &msg)?;
Ok(Response::new().add_event(
Ok(BatchResponse::new().add_event(
make_event("network.forced").add_attribute("network_id", msg.network_id.to_string()),
))
}
38 changes: 29 additions & 9 deletions code/xcvm/lib/core/schema/raw/execute.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@
"type": "object",
"required": [
"asset_id",
"from_network_id",
"local"
"local",
"network_id"
],
"properties": {
"asset_id": {
"$ref": "#/definitions/AssetId"
},
"bridged": {
"description": "if asset was bridged, it would have way to identify bridge/source/channel",
"anyOf": [
{
"$ref": "#/definitions/BridgeAsset"
Expand All @@ -173,11 +174,16 @@
}
]
},
"from_network_id": {
"$ref": "#/definitions/NetworkId"
},
"local": {
"$ref": "#/definitions/AssetReference"
},
"network_id": {
"description": "network id on which this asset id can be used locally",
"allOf": [
{
"$ref": "#/definitions/NetworkId"
}
]
}
}
},
Expand Down Expand Up @@ -513,6 +519,22 @@
},
"additionalProperties": false
},
{
"description": "short cut to rollout config faster",
"type": "object",
"required": [
"force"
],
"properties": {
"force": {
"type": "array",
"items": {
"$ref": "#/definitions/ConfigSubMsg"
}
}
},
"additionalProperties": false
},
{
"description": "instantiates default interpreter on behalf of user `salt` - human string, converted to hex or base64 depending on implementation",
"type": "object",
Expand All @@ -527,10 +549,7 @@
],
"properties": {
"salt": {
"type": [
"string",
"null"
]
"type": "string"
},
"user_origin": {
"$ref": "#/definitions/Addr"
Expand Down Expand Up @@ -1243,6 +1262,7 @@
]
},
"RelativeTimeout": {
"description": "relative timeout to CW/IBC-rs time. very small, assumed messages are arriving fast enough, like less than hours",
"oneOf": [
{
"description": "Timeout is relative to the current block timestamp of counter party",
Expand Down
6 changes: 3 additions & 3 deletions code/xcvm/lib/core/schema/raw/instantiate.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "object",
"required": [
"admin",
"here_id"
"network_id"
],
"properties": {
"admin": {
Expand All @@ -26,8 +26,8 @@
}
]
},
"here_id": {
"description": "Network ID of this network",
"network_id": {
"description": "Network ID of this network where contract is deployed",
"allOf": [
{
"$ref": "#/definitions/NetworkId"
Expand Down
Loading