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

Replaced rc by arc in daemon #308

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions cw-orch-daemon/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{log::print_if_log_disabled, DaemonAsync, DaemonBuilder};
use std::rc::Rc;
use std::sync::Arc;

use ibc_chain_registry::chain::ChainData;

Expand Down Expand Up @@ -63,7 +63,7 @@ impl DaemonAsyncBuilder {
.deployment_id
.clone()
.unwrap_or(DEFAULT_DEPLOYMENT.to_string());
let state = Rc::new(DaemonState::new(chain, deployment_id, false).await?);
let state = Arc::new(DaemonState::new(chain, deployment_id, false).await?);
// if mnemonic provided, use it. Else use env variables to retrieve mnemonic
let sender = if let Some(mnemonic) = &self.mnemonic {
Sender::from_mnemonic(&state, mnemonic)?
Expand All @@ -72,7 +72,7 @@ impl DaemonAsyncBuilder {
};
let daemon = DaemonAsync {
state,
sender: Rc::new(sender),
sender: Arc::new(sender),
};
print_if_log_disabled()?;
Ok(daemon)
Expand Down
6 changes: 3 additions & 3 deletions cw-orch-daemon/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use serde_json::from_str;
use std::{
fmt::Debug,
io::Write,
rc::Rc,
str::{from_utf8, FromStr},
sync::Arc,
time::Duration,
};

Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct DaemonAsync {
/// Sender to send transactions to the chain
pub sender: Wallet,
/// State of the daemon
pub state: Rc<DaemonState>,
pub state: Arc<DaemonState>,
}

impl DaemonAsync {
Expand All @@ -84,7 +84,7 @@ impl DaemonAsync {
}

impl ChainState for DaemonAsync {
type Out = Rc<DaemonState>;
type Out = Arc<DaemonState>;

fn state(&self) -> Self::Out {
self.state.clone()
Expand Down
10 changes: 5 additions & 5 deletions cw-orch-daemon/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use cosmwasm_std::{coin, Addr, Coin};
use cw_orch_core::{log::local_target, CwOrchEnvVars};

use bitcoin::secp256k1::{All, Context, Secp256k1, Signing};
use std::{convert::TryFrom, rc::Rc, str::FromStr};
use std::{convert::TryFrom, str::FromStr, sync::Arc};

use cosmos_modules::vesting::PeriodicVestingAccount;
use tonic::transport::Channel;
Expand All @@ -44,18 +44,18 @@ const BUFFER_THRESHOLD: u64 = 200_000;
const SMALL_GAS_BUFFER: f64 = 1.4;

/// A wallet is a sender of transactions, can be safely cloned and shared within the same thread.
pub type Wallet = Rc<Sender<All>>;
pub type Wallet = Arc<Sender<All>>;

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

impl Sender<All> {
pub fn new(daemon_state: &Rc<DaemonState>) -> Result<Sender<All>, DaemonError> {
pub fn new(daemon_state: &Arc<DaemonState>) -> 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 @@ -71,7 +71,7 @@ impl Sender<All> {

/// Construct a new Sender from a mnemonic
pub fn from_mnemonic(
daemon_state: &Rc<DaemonState>,
daemon_state: &Arc<DaemonState>,
mnemonic: &str,
) -> Result<Sender<All>, DaemonError> {
let secp = Secp256k1::new();
Expand Down
4 changes: 2 additions & 2 deletions cw-orch-daemon/src/sync/core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, rc::Rc, time::Duration};
use std::{fmt::Debug, sync::Arc, time::Duration};

use super::super::{sender::Wallet, DaemonAsync};
use crate::{
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Daemon {
}

impl ChainState for Daemon {
type Out = Rc<DaemonState>;
type Out = Arc<DaemonState>;

fn state(&self) -> Self::Out {
self.daemon.state.clone()
Expand Down
32 changes: 31 additions & 1 deletion packages/cw-orch-core/src/environment/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::error::CwEnvError;
use cosmwasm_std::Addr;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};

/// State accessor trait.
/// Indicates that the type has access to an underlying state.
Expand Down Expand Up @@ -107,3 +107,33 @@
(**self).deploy_details()
}
}

impl<S: StateInterface> StateInterface for Arc<S> {
fn get_address(&self, contract_id: &str) -> Result<Addr, CwEnvError> {
(**self).get_address(contract_id)
}

fn set_address(&mut self, contract_id: &str, address: &Addr) {
(*Arc::make_mut(self)).set_address(contract_id, address)
}

fn get_code_id(&self, contract_id: &str) -> Result<u64, CwEnvError> {
(**self).get_code_id(contract_id)
}

fn set_code_id(&mut self, contract_id: &str, code_id: u64) {
(*Arc::make_mut(self)).set_code_id(contract_id, code_id)
}

fn get_all_addresses(&self) -> Result<HashMap<String, Addr>, CwEnvError> {
(**self).get_all_addresses()
}

Check warning on line 130 in packages/cw-orch-core/src/environment/state.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/state.rs#L128-L130

Added lines #L128 - L130 were not covered by tests

fn get_all_code_ids(&self) -> Result<HashMap<String, u64>, CwEnvError> {
(**self).get_all_code_ids()
}

Check warning on line 134 in packages/cw-orch-core/src/environment/state.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/state.rs#L132-L134

Added lines #L132 - L134 were not covered by tests

fn deploy_details(&self) -> DeployDetails {
(**self).deploy_details()
}

Check warning on line 138 in packages/cw-orch-core/src/environment/state.rs

View check run for this annotation

Codecov / codecov/patch

packages/cw-orch-core/src/environment/state.rs#L136-L138

Added lines #L136 - L138 were not covered by tests
}
Loading