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

Nicolas/orc 68 avoid loading node test instances when not doing node tests #316

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
07f4bae
Added cw-multi-test 0.20.0
Kayanski Jan 12, 2024
1c6d09b
Removed secp, removed ibc-relayer-types
Kayanski Jan 12, 2024
9c3bfdc
Removed ibc-relayer-types completely
Kayanski Jan 12, 2024
a56bb35
updated prost typesé
Kayanski Jan 12, 2024
c7ca93c
Start testing authz
Kayanski Jan 12, 2024
049d2e9
Merge branch 'update/cw-multi-test-20' into feature/add-authz-wrapping
Kayanski Jan 12, 2024
4d02ed5
Added authz test
Kayanski Jan 12, 2024
533f1af
Merge remote-tracking branch 'origin/main' into feature/add-authz-wra…
Kayanski Jan 12, 2024
da502a0
add authz querier
Buckram123 Jan 17, 2024
5595d47
Merge branch 'main' into feature/add-authz-wrapping
Kayanski Jan 17, 2024
6630b4d
Merge branch 'feature/add-authz-wrapping' of github.com:AbstractSDK/c…
Kayanski Jan 17, 2024
d23d0f7
tx handler error with anyhow test
Buckram123 Jan 17, 2024
e07abdc
Merge remote-tracking branch 'origin/main' into feature/add-authz-wra…
Buckram123 Jan 17, 2024
5b7ebb2
format
Buckram123 Jan 17, 2024
6da5a3d
Merge branch 'feature/add-authz-wrapping' of github.com:AbstractSDK/c…
Kayanski Jan 18, 2024
814066f
Added authz builder method for options
Kayanski Jan 18, 2024
05e321b
Some nits
Kayanski Jan 18, 2024
6e70d1f
Revert with_authz, added sender options setter
Kayanski Jan 18, 2024
10b52a8
Check rest of authz queries
Buckram123 Jan 18, 2024
52d29ae
Added fee grant in daemon builder and interface
Kayanski Jan 19, 2024
2c19a89
Merge branch 'feature/add-authz-wrapping' of github.com:AbstractSDK/c…
Kayanski Jan 19, 2024
1ab922a
Removed node-test default feature in cw-orch-daemon
Kayanski Jan 23, 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
2 changes: 1 addition & 1 deletion cw-orch-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude = [".env"]
all-features = true

[features]
default = ["node-tests"]
default = []
# enable node-backed tests (ensure Docker is running)
# run with `cargo test --jobs 1 --features node-tests`
node-tests = []
Expand Down
22 changes: 19 additions & 3 deletions cw-orch-daemon/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{log::print_if_log_disabled, DaemonAsync, DaemonBuilder};
use crate::{log::print_if_log_disabled, sender::SenderOptions, DaemonAsync, DaemonBuilder};
use std::rc::Rc;

use ibc_chain_registry::chain::ChainData;
Expand Down Expand Up @@ -28,6 +28,8 @@ pub struct DaemonAsyncBuilder {
pub(crate) deployment_id: Option<String>,
/// Wallet mnemonic
pub(crate) mnemonic: Option<String>,
/// Specify Daemon Sender Options
pub(crate) sender_options: SenderOptions,
}

impl DaemonAsyncBuilder {
Expand All @@ -53,6 +55,18 @@ impl DaemonAsyncBuilder {
self
}

/// Specifies whether authz should be used with this daemon
pub fn authz_granter(&mut self, granter: impl ToString) -> &mut Self {
self.sender_options.set_authz_granter(granter);
self
}

/// Specifies whether a fee grant should be used with this daemon
pub fn fee_granter(&mut self, granter: impl ToString) -> &mut Self {
self.sender_options.set_fee_granter(granter);
self
}

/// Build a daemon
pub async fn build(&self) -> Result<DaemonAsync, DaemonError> {
let chain = self
Expand All @@ -65,10 +79,11 @@ impl DaemonAsyncBuilder {
.unwrap_or(DEFAULT_DEPLOYMENT.to_string());
let state = Rc::new(DaemonState::new(chain, deployment_id, false).await?);
// if mnemonic provided, use it. Else use env variables to retrieve mnemonic
let sender_options = self.sender_options.clone();
let sender = if let Some(mnemonic) = &self.mnemonic {
Sender::from_mnemonic(&state, mnemonic)?
Sender::from_mnemonic_with_options(&state, mnemonic, sender_options)?
} else {
Sender::new(&state)?
Sender::new_with_options(&state, sender_options)?
};
let daemon = DaemonAsync {
state,
Expand All @@ -85,6 +100,7 @@ impl From<DaemonBuilder> for DaemonAsyncBuilder {
chain: value.chain,
deployment_id: value.deployment_id,
mnemonic: value.mnemonic,
sender_options: value.sender_options,
}
}
}
8 changes: 4 additions & 4 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl DaemonAsync {
contract_address: &Addr,
) -> Result<CosmTxResponse, DaemonError> {
let exec_msg: MsgExecuteContract = MsgExecuteContract {
sender: self.sender.pub_addr()?,
sender: self.sender.msg_sender()?,
contract: AccountId::from_str(contract_address.as_str())?,
msg: serde_json::to_vec(&exec_msg)?,
funds: parse_cw_coins(coins)?,
Expand All @@ -132,7 +132,7 @@ impl DaemonAsync {
code_id,
label: Some(label.unwrap_or("instantiate_contract").to_string()),
admin: admin.map(|a| FromStr::from_str(a.as_str()).unwrap()),
sender: sender.pub_addr()?,
sender: self.sender.msg_sender()?,
msg: serde_json::to_vec(&init_msg)?,
funds: parse_cw_coins(coins)?,
};
Expand Down Expand Up @@ -169,7 +169,7 @@ impl DaemonAsync {
contract_address: &Addr,
) -> Result<CosmTxResponse, DaemonError> {
let exec_msg: MsgMigrateContract = MsgMigrateContract {
sender: self.sender.pub_addr()?,
sender: self.sender.msg_sender()?,
contract: AccountId::from_str(contract_address.as_str())?,
msg: serde_json::to_vec(&migrate_msg)?,
code_id: new_code_id,
Expand Down Expand Up @@ -243,7 +243,7 @@ impl DaemonAsync {
e.write_all(&file_contents)?;
let wasm_byte_code = e.finish()?;
let store_msg = cosmrs::cosmwasm::MsgStoreCode {
sender: sender.pub_addr()?,
sender: self.sender.msg_sender()?,
wasm_byte_code,
instantiate_permission: None,
};
Expand Down
1 change: 1 addition & 0 deletions cw-orch-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub(crate) mod cosmos_modules {
pub use cosmrs::proto::{
cosmos::{
auth::v1beta1 as auth,
authz::v1beta1 as authz,
bank::v1beta1 as bank,
base::{abci::v1beta1 as abci, tendermint::v1beta1 as tendermint},
feegrant::v1beta1 as feegrant,
Expand Down
2 changes: 1 addition & 1 deletion cw-orch-daemon/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static LOGS_DISABLED: Once = Once::new();

// Prints a warning if log is disabled for the application
pub fn print_if_log_disabled() -> Result<(), DaemonError> {
LOGS_DISABLED.call_once(|| {
LOGS_DISABLED.call_once(|| {
// Here we check for logging capabilities.
if !log::log_enabled!(log::Level::Info) && !CwOrchEnvVars::load().map(|env|env.disable_logs_message).unwrap_or(false){
println!(
Expand Down
2 changes: 2 additions & 0 deletions cw-orch-daemon/src/queriers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ macro_rules! cosmos_query {
};
}

mod authz;
mod bank;
mod cosmwasm;
mod feegrant;
Expand All @@ -52,6 +53,7 @@ mod ibc;
mod node;
mod staking;

pub use authz::Authz;
pub use bank::{cosmrs_to_cosmwasm_coins, Bank};
pub use cosmwasm::CosmWasm;
pub use feegrant::Feegrant;
Expand Down
79 changes: 79 additions & 0 deletions cw-orch-daemon/src/queriers/authz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use cosmrs::proto::cosmos::base::query::v1beta1::PageRequest;
use tonic::transport::Channel;

use crate::{cosmos_modules, error::DaemonError};

use super::DaemonQuerier;

/// Queries for Cosmos AuthZ Module
pub struct Authz {
channel: Channel,
}

impl DaemonQuerier for Authz {
fn new(channel: Channel) -> Self {
Self { channel }
}
}

impl Authz {
/// Query Authz Grants from grantee to granter
pub async fn grants(
&self,
granter: String,
grantee: String,
msg_type_url: String,
pagination: Option<PageRequest>,
) -> Result<cosmrs::proto::cosmos::authz::v1beta1::QueryGrantsResponse, DaemonError> {
use cosmos_modules::authz::{query_client::QueryClient, QueryGrantsRequest};
let mut client: QueryClient<Channel> = QueryClient::new(self.channel.clone());
let grants = client
.grants(QueryGrantsRequest {
granter,
grantee,
msg_type_url,
pagination,
})
.await?
.into_inner();
Ok(grants)
}

/// Query Authz Grants of grantee
pub async fn grantee_grants(
&self,
grantee: String,
pagination: Option<PageRequest>,
) -> Result<cosmrs::proto::cosmos::authz::v1beta1::QueryGranteeGrantsResponse, DaemonError>
{
use cosmos_modules::authz::{query_client::QueryClient, QueryGranteeGrantsRequest};
let mut client: QueryClient<Channel> = QueryClient::new(self.channel.clone());
let grants = client
.grantee_grants(QueryGranteeGrantsRequest {
grantee,
pagination,
})
.await?
.into_inner();
Ok(grants)
}

/// Query Authz Grants for granter
pub async fn granter_grants(
&self,
granter: String,
pagination: Option<PageRequest>,
) -> Result<cosmrs::proto::cosmos::authz::v1beta1::QueryGranterGrantsResponse, DaemonError>
{
use cosmos_modules::authz::{query_client::QueryClient, QueryGranterGrantsRequest};
let mut client: QueryClient<Channel> = QueryClient::new(self.channel.clone());
let grants = client
.granter_grants(QueryGranterGrantsRequest {
granter,
pagination,
})
.await?
.into_inner();
Ok(grants)
}
}
85 changes: 81 additions & 4 deletions cw-orch-daemon/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{core::parse_cw_coins, keys::private::PrivateKey};
use cosmrs::{
bank::MsgSend,
crypto::secp256k1::SigningKey,
proto::traits::Message,
proto::{cosmos::authz::v1beta1::MsgExec, traits::Message},
tendermint::chain::Id,
tx::{self, ModeInfo, Msg, Raw, SignDoc, SignMode, SignerInfo},
AccountId, Any,
Expand All @@ -48,14 +48,47 @@ pub type Wallet = Rc<Sender<All>>;

/// Signer of the transactions and helper for address derivation
/// This is the main interface for simulating and signing transactions
#[derive(Clone)]
pub struct Sender<C: Signing + Context> {
pub private_key: PrivateKey,
pub secp: Secp256k1<C>,
pub(crate) daemon_state: Rc<DaemonState>,
pub(crate) options: SenderOptions,
}

#[derive(Default, Clone)]
#[non_exhaustive]
pub struct SenderOptions {
pub authz_granter: Option<String>,
pub fee_granter: Option<String>,
}

impl SenderOptions {
pub fn authz_granter(mut self, granter: impl ToString) -> Self {
self.authz_granter = Some(granter.to_string());
self
}
pub fn fee_granter(mut self, granter: impl ToString) -> Self {
self.fee_granter = Some(granter.to_string());
self
}
pub fn set_authz_granter(&mut self, granter: impl ToString) {
self.authz_granter = Some(granter.to_string());
}
pub fn set_fee_granter(&mut self, granter: impl ToString) {
self.fee_granter = Some(granter.to_string());
}
}

impl Sender<All> {
pub fn new(daemon_state: &Rc<DaemonState>) -> Result<Sender<All>, DaemonError> {
Self::new_with_options(daemon_state, SenderOptions::default())
}

pub fn new_with_options(
daemon_state: &Rc<DaemonState>,
options: SenderOptions,
) -> Result<Sender<All>, DaemonError> {
let kind = ChainKind::from(daemon_state.chain_data.network_type.clone());
// NETWORK_MNEMONIC_GROUP
let env_variable_name = kind.mnemonic_env_variable_name();
Expand All @@ -66,13 +99,22 @@ impl Sender<All> {
)
});

Self::from_mnemonic(daemon_state, &mnemonic)
Self::from_mnemonic_with_options(daemon_state, &mnemonic, options)
}

/// Construct a new Sender from a mnemonic
/// Construct a new Sender from a mnemonic with additional options
pub fn from_mnemonic(
daemon_state: &Rc<DaemonState>,
mnemonic: &str,
) -> Result<Sender<All>, DaemonError> {
Self::from_mnemonic_with_options(daemon_state, mnemonic, SenderOptions::default())
}

/// Construct a new Sender from a mnemonic with additional options
pub fn from_mnemonic_with_options(
daemon_state: &Rc<DaemonState>,
mnemonic: &str,
options: SenderOptions,
) -> Result<Sender<All>, DaemonError> {
let secp = Secp256k1::new();
let p_key: PrivateKey =
Expand All @@ -82,6 +124,7 @@ impl Sender<All> {
daemon_state: daemon_state.clone(),
private_key: p_key,
secp,
options,
};
log::info!(
target: &local_target(),
Expand All @@ -92,6 +135,14 @@ impl Sender<All> {
Ok(sender)
}

pub fn authz_granter(&mut self, granter: impl Into<String>) {
self.options.authz_granter = Some(granter.into());
}

pub fn fee_granter(&mut self, granter: impl Into<String>) {
self.options.fee_granter = Some(granter.into());
}

fn cosmos_private_key(&self) -> SigningKey {
SigningKey::from_slice(&self.private_key.raw_key()).unwrap()
}
Expand All @@ -115,13 +166,24 @@ impl Sender<All> {
Ok(self.pub_addr()?.to_string())
}

/// Returns the actual sender of every message sent.
/// If an authz granter is set, returns the authz granter
/// Else, returns the address associated with the current private key
pub fn msg_sender(&self) -> Result<AccountId, DaemonError> {
if let Some(sender) = &self.options.authz_granter {
Ok(sender.parse()?)
} else {
self.pub_addr()
}
}

pub async fn bank_send(
&self,
recipient: &str,
coins: Vec<cosmwasm_std::Coin>,
) -> Result<CosmTxResponse, DaemonError> {
let msg_send = MsgSend {
from_address: self.pub_addr()?,
from_address: self.msg_sender()?,
to_address: AccountId::from_str(recipient)?,
amount: parse_cw_coins(&coins)?,
};
Expand Down Expand Up @@ -169,6 +231,7 @@ impl Sender<All> {
0u8,
&self.daemon_state.chain_data.fees.fee_tokens[0].denom,
0,
self.options.clone(),
)?;

let auth_info = SignerInfo {
Expand Down Expand Up @@ -239,6 +302,20 @@ impl Sender<All> {
) -> Result<CosmTxResponse, DaemonError> {
let timeout_height = Node::new(self.channel()).block_height().await? + 10u64;

let msgs = if self.options.authz_granter.is_some() {
// We wrap authz messages
vec![Any {
type_url: "/cosmos.authz.v1beta1.MsgExec".to_string(),
value: MsgExec {
grantee: self.pub_addr_str()?,
msgs,
}
.encode_to_vec(),
}]
} else {
msgs
};

let tx_body = TxBuilder::build_body(msgs, memo, timeout_height);

let tx_builder = TxBuilder::new(tx_body);
Expand Down
Loading
Loading