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

refactor(minor-axelarnet-gateway): simplify the initial implementation #583

Merged
merged 28 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
56ffd0d
refactor(axelarnet-gateway-minor): simplify the initial implementation
cgorenflo Aug 13, 2024
6e98c71
Merge branch 'refs/heads/main' into refactor-axelarnet-gateway
cgorenflo Aug 13, 2024
ecd5724
clippy
cgorenflo Aug 13, 2024
a5a674c
improvements and panic
cgorenflo Aug 13, 2024
d4d6696
refactor(axelarnet-gateeway): improve contract structure
cgorenflo Aug 22, 2024
d1a4c6b
improve naming
cgorenflo Aug 22, 2024
3054ae5
Merge remote-tracking branch 'refs/remotes/origin/main' into refactor…
cgorenflo Aug 22, 2024
42953ac
merge conflicts
cgorenflo Aug 22, 2024
65d4dbb
finish tests
cgorenflo Aug 23, 2024
020d0eb
remove commented out lines
cgorenflo Aug 23, 2024
1ed4353
fix tests
cgorenflo Aug 23, 2024
61da7ea
fix tests
cgorenflo Aug 23, 2024
0827d36
lint
cgorenflo Aug 23, 2024
8eef118
revert gateway changes
cgorenflo Aug 23, 2024
14b135e
Merge branch 'main' into refactor-axelarnet-gateway
cgorenflo Aug 23, 2024
d258975
Merge branch 'main' into refactor-axelarnet-gateway
cgorenflo Aug 23, 2024
8ad0661
Merge remote-tracking branch 'refs/remotes/origin/main' into refactor…
cgorenflo Aug 29, 2024
31d21bb
fixes from comments
cgorenflo Aug 29, 2024
b965022
lint
cgorenflo Aug 29, 2024
b0867a7
clippy
cgorenflo Aug 29, 2024
8b91e4c
remove goldie
cgorenflo Aug 29, 2024
af5c11c
readd goldie
cgorenflo Aug 29, 2024
101961a
remove goldie
cgorenflo Aug 29, 2024
095cd76
remove goldie
cgorenflo Aug 29, 2024
cfbd7a3
panic if config is missing
cgorenflo Aug 29, 2024
c2f0ad9
fix bug
cgorenflo Aug 29, 2024
2880411
address validation function comment
cgorenflo Aug 29, 2024
1619e05
fix bug
cgorenflo Aug 29, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axelar_wasm_std::address;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{HexBinary, WasmMsg};
use cosmwasm_std::{Deps, HexBinary, WasmMsg};
use router_api::{Address, CrossChainId};

/// `AxelarExecutableMsg` is a struct containing the args used by the axelarnet gateway to execute a destination contract on Axelar.
Expand All @@ -11,36 +12,35 @@
pub payload: HexBinary,
}

/// Crate-specific `ExecuteMsg` type wraps the `AxelarExecutableMsg` for the AxelarExecutable client.
#[cw_serde]
pub(crate) enum AxelarExecutableExecuteMsg {
/// Execute the message at the destination contract with the corresponding payload.
Execute(AxelarExecutableMsg),
pub struct PayloadExecutor<'a> {
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
client: client::Client<'a, AxelarExecutableMsg, ()>,
}

impl<'a> From<client::Client<'a, AxelarExecutableExecuteMsg, ()>> for AxelarExecutableClient<'a> {
fn from(client: client::Client<'a, AxelarExecutableExecuteMsg, ()>) -> Self {
AxelarExecutableClient { client }
impl<'a> From<client::Client<'a, AxelarExecutableMsg, ()>> for PayloadExecutor<'a> {
fn from(client: client::Client<'a, AxelarExecutableMsg, ()>) -> Self {
PayloadExecutor { client }
}
}

pub struct AxelarExecutableClient<'a> {
client: client::Client<'a, AxelarExecutableExecuteMsg, ()>,
}
impl<'a> PayloadExecutor<'a> {
pub fn new(deps: Deps<'a>, destination: &str) -> error_stack::Result<Self, address::Error> {
let destination = address::validate_cosmwasm_address(deps.api, destination)?;
Ok(PayloadExecutor {
client: client::Client::new(deps.querier, destination),
})
}

Check warning on line 31 in contracts/axelarnet-gateway/src/clients/external.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/clients/external.rs#L26-L31

Added lines #L26 - L31 were not covered by tests

impl<'a> AxelarExecutableClient<'a> {
pub fn execute(
&self,
cc_id: CrossChainId,
source_address: Address,
payload: HexBinary,
) -> WasmMsg {
self.client
.execute(&AxelarExecutableExecuteMsg::Execute(AxelarExecutableMsg {
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
cc_id,
source_address,
payload,
}))
self.client.execute(&AxelarExecutableMsg {
cc_id,
source_address,
payload,
})
}
}

Expand All @@ -54,7 +54,7 @@
#[test]
fn execute_message() {
let (querier, addr) = setup();
let client: AxelarExecutableClient =
let client: PayloadExecutor =
client::Client::new(QuerierWrapper::new(&querier), addr.clone()).into();

let cc_id = CrossChainId::new("source-chain", "message-id").unwrap();
Expand All @@ -67,11 +67,11 @@
msg,
WasmMsg::Execute {
contract_addr: addr.to_string(),
msg: to_json_binary(&AxelarExecutableExecuteMsg::Execute(AxelarExecutableMsg {
msg: to_json_binary(&AxelarExecutableMsg {
cc_id,
source_address,
payload,
}))
})
.unwrap(),
funds: vec![],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axelar_wasm_std::vec::VecExt;
use cosmwasm_std::{HexBinary, WasmMsg};
use router_api::{Address, ChainName, CrossChainId, Message};

use crate::msg::{ExecuteMsg, QueryMsg};
use crate::msg::{CallContractData, ExecuteMsg, QueryMsg};

impl<'a> From<client::Client<'a, ExecuteMsg, QueryMsg>> for Client<'a> {
fn from(client: client::Client<'a, ExecuteMsg, QueryMsg>) -> Self {
Expand All @@ -21,11 +21,12 @@ impl<'a> Client<'a> {
destination_address: Address,
payload: HexBinary,
) -> WasmMsg {
self.client.execute(&ExecuteMsg::CallContract {
destination_chain,
destination_address,
payload,
})
self.client
.execute(&ExecuteMsg::CallContract(CallContractData {
destination_chain,
destination_address,
payload,
}))
}

pub fn execute(&self, cc_id: CrossChainId, payload: HexBinary) -> WasmMsg {
Expand Down Expand Up @@ -67,11 +68,11 @@ mod test {
msg,
WasmMsg::Execute {
contract_addr: addr.to_string(),
msg: to_json_binary(&ExecuteMsg::CallContract {
msg: to_json_binary(&ExecuteMsg::CallContract(CallContractData {
destination_chain,
destination_address,
payload,
})
}))
.unwrap(),
funds: vec![],
}
Expand Down
5 changes: 5 additions & 0 deletions contracts/axelarnet-gateway/src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod external;
mod gateway;

pub use external::*;
pub use gateway::Client as GatewayClient;
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
117 changes: 35 additions & 82 deletions contracts/axelarnet-gateway/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use axelar_wasm_std::{address, FnExt};
use axelar_wasm_std::error::ContractError;
use axelar_wasm_std::{address, FnExt, IntoContractError};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
use error_stack::ResultExt;
use router_api::client::Router;
use router_api::CrossChainId;

use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{self, Config};
Expand All @@ -15,34 +14,18 @@
const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, Debug, IntoContractError)]

Check warning on line 17 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L17

Added line #L17 was not covered by tests
pub enum Error {
#[error("contract config is missing")]
ConfigMissing,
#[error("invalid store access")]
InvalidStoreAccess,
#[error("failed to serialize the response")]
SerializeResponse,
#[error("failed to serialize wasm message")]
SerializeWasmMsg,
#[error("invalid address {0}")]
InvalidAddress(String),
#[error("invalid message id")]
InvalidMessageId,
#[error("failed to save outgoing message")]
SaveOutgoingMessage,
#[error("message with ID {0} not found")]
MessageNotFound(CrossChainId),
#[error("message with ID {0} is different")]
MessageMismatch(CrossChainId),
#[error("message with ID {0} not in approved status")]
MessageNotApproved(CrossChainId),
#[error("failed to set message with ID {0} as executed")]
SetMessageStatusExecutedFailed(CrossChainId),
#[error("payload hash doesn't match message")]
PayloadHashMismatch,
#[error("failed to route messages")]
RoutingFailed,
#[error("failed to make a cross-chain contract call")]
CallContract,
#[error("failed to route messages on the gateway")]
RouteMessages,
#[error("failed to execute a cross-chain execution payload")]
Execute,
#[error("failed to query cross-chain contract call messages")]
QueryContractCallMessages,
cgorenflo marked this conversation as resolved.
Show resolved Hide resolved
#[error("failed to query executable messages")]
QueryExecutableMessages,
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -64,18 +47,13 @@
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, axelar_wasm_std::error::ContractError> {
) -> Result<Response, ContractError> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let router = address::validate_cosmwasm_address(deps.api, &msg.router_address)?;
let chain_name = msg.chain_name;

let config = Config {
chain_name: msg.chain_name,
router,
};

state::save_config(deps.storage, &config).change_context(Error::InvalidStoreAccess)?;

state::save_config(deps.storage, &Config { chain_name, router })?;
Ok(Response::new())
}

Expand All @@ -85,59 +63,34 @@
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, axelar_wasm_std::error::ContractError> {
let msg = msg.ensure_permissions(deps.storage, &info.sender)?;

let config = state::load_config(deps.storage).change_context(Error::ConfigMissing)?;

let router = Router {
address: config.router,
};
let chain_name = config.chain_name;

match msg {
ExecuteMsg::CallContract {
destination_chain,
destination_address,
payload,
} => execute::call_contract(
deps.storage,
env.block.height,
&router,
chain_name,
info.sender,
destination_chain,
destination_address,
payload,
),
ExecuteMsg::RouteMessages(msgs) => {
if info.sender == router.address {
execute::receive_messages(deps.storage, chain_name, msgs)
} else {
// Messages initiated via call contract can be routed again
execute::send_messages(deps.storage, &router, msgs)
}
) -> Result<Response, ContractError> {
match msg.ensure_permissions(deps.storage, &info.sender)? {
ExecuteMsg::CallContract(data) => {
execute::call_contract(deps.storage, env.block.height, info.sender, data)
.change_context(Error::CallContract)

Check warning on line 70 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L66-L70

Added lines #L66 - L70 were not covered by tests
}
ExecuteMsg::RouteMessages(msgs) => execute::route_messages(deps.storage, info.sender, msgs)
.change_context(Error::RouteMessages),

Check warning on line 73 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L72-L73

Added lines #L72 - L73 were not covered by tests
ExecuteMsg::Execute { cc_id, payload } => {
execute::execute(deps.storage, deps.api, deps.querier, cc_id, payload)
execute::execute(deps, cc_id, payload).change_context(Error::Execute)

Check warning on line 75 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L75

Added line #L75 was not covered by tests
}
}?
.then(Ok)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: QueryMsg,
) -> Result<Binary, axelar_wasm_std::error::ContractError> {
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {

Check warning on line 82 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L82

Added line #L82 was not covered by tests
match msg {
QueryMsg::SentMessages { cc_ids } => to_json_binary(&query::sent_messages(deps, cc_ids)?),
QueryMsg::ReceivedMessages { cc_ids } => {
to_json_binary(&query::received_messages(deps, cc_ids)?)
}
}
.map_err(axelar_wasm_std::error::ContractError::from)
QueryMsg::RoutableMessages { cc_ids } => to_json_binary(
&query::routable_messages(deps, cc_ids)
.change_context(Error::QueryContractCallMessages)?,

Check warning on line 86 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L84-L86

Added lines #L84 - L86 were not covered by tests
),
QueryMsg::ExecutableMessages { cc_ids } => to_json_binary(
&query::executable_messages(deps, cc_ids)
.change_context(Error::QueryExecutableMessages)?,

Check warning on line 90 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L88-L90

Added lines #L88 - L90 were not covered by tests
),
}?
.then(Ok)

Check warning on line 93 in contracts/axelarnet-gateway/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/axelarnet-gateway/src/contract.rs#L92-L93

Added lines #L92 - L93 were not covered by tests
}

#[cfg(test)]
Expand Down
Loading
Loading