Skip to content

Commit

Permalink
Switch from Rc to Arc
Browse files Browse the repository at this point in the history
The point here is to make all these types (Session in particular) Send,
so that they can be used within multithreaded code.
  • Loading branch information
obrok committed Mar 26, 2024
1 parent 4e93b94 commit dccd678
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 46 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.15.0"
version = "0.16.0"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand Down Expand Up @@ -51,5 +51,5 @@ sp-runtime-interface = { version = "26.0.0" }

# Local dependencies

drink = { version = "=0.15.0", path = "drink" }
drink-test-macro = { version = "=0.15.0", path = "drink/test-macro" }
drink = { version = "=0.16.0", path = "drink" }
drink-test-macro = { version = "=0.16.0", path = "drink/test-macro" }
4 changes: 2 additions & 2 deletions drink-cli/src/app_state/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, rc::Rc};
use std::{path::PathBuf, sync::Arc};

use contract_transcode::ContractMessageTranscoder;
use drink::AccountId32;
Expand All @@ -10,7 +10,7 @@ pub struct Contract {
pub name: String,
pub address: AccountId32,
pub base_path: PathBuf,
pub transcoder: Rc<ContractMessageTranscoder>,
pub transcoder: Arc<ContractMessageTranscoder>,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
Expand Down
4 changes: 2 additions & 2 deletions drink-cli/src/executor/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
fs,
path::{Path, PathBuf},
rc::Rc,
sync::Arc,
};

use contract_build::{BuildMode, ExecuteArgs, ManifestPath, OptimizationPasses, Verbosity};
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec<String>,
app_state.print_error("Failed to create transcoder from metadata file.");
return;
};
let transcoder = Rc::new(transcoder);
let transcoder = Arc::new(transcoder);

match app_state.session.deploy(
contract_bytes,
Expand Down
21 changes: 10 additions & 11 deletions drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{
fmt::Debug,
mem,
rc::Rc,
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -63,7 +62,7 @@ pub const NO_ENDOWMENT: Option<BalanceOf<MinimalSandboxRuntime>> = None;
///
/// `Session` has two APIs: chain-ish and for singular actions. The first one can be used like:
/// ```rust, no_run
/// # use std::rc::Rc;
/// # use std::sync::Arc;
/// # use contract_transcode::ContractMessageTranscoder;
/// # use ink_sandbox::AccountId32;
/// # use drink::{
Expand All @@ -72,8 +71,8 @@ pub const NO_ENDOWMENT: Option<BalanceOf<MinimalSandboxRuntime>> = None;
/// # minimal::MinimalSandbox
/// # };
/// #
/// # fn get_transcoder() -> Rc<ContractMessageTranscoder> {
/// # Rc::new(ContractMessageTranscoder::load("").unwrap())
/// # fn get_transcoder() -> Arc<ContractMessageTranscoder> {
/// # Arc::new(ContractMessageTranscoder::load("").unwrap())
/// # }
/// # fn contract_bytes() -> Vec<u8> { vec![] }
/// # fn bob() -> AccountId32 { AccountId32::new([0; 32]) }
Expand All @@ -90,16 +89,16 @@ pub const NO_ENDOWMENT: Option<BalanceOf<MinimalSandboxRuntime>> = None;
///
/// The second one serves for one-at-a-time actions:
/// ```rust, no_run
/// # use std::rc::Rc;
/// # use std::sync::Arc;
/// # use contract_transcode::ContractMessageTranscoder;
/// # use ink_sandbox::AccountId32;
/// # use drink::{
/// # session::Session,
/// # minimal::MinimalSandbox,
/// # session::{NO_ARGS, NO_ENDOWMENT, NO_SALT}
/// # };
/// # fn get_transcoder() -> Rc<ContractMessageTranscoder> {
/// # Rc::new(ContractMessageTranscoder::load("").unwrap())
/// # fn get_transcoder() -> Arc<ContractMessageTranscoder> {
/// # Arc::new(ContractMessageTranscoder::load("").unwrap())
/// # }
/// # fn contract_bytes() -> Vec<u8> { vec![] }
/// # fn bob() -> AccountId32 { AccountId32::new([0; 32]) }
Expand Down Expand Up @@ -224,7 +223,7 @@ where
pub fn with_transcoder(
mut self,
contract_address: AccountIdFor<T::Runtime>,
transcoder: &Rc<ContractMessageTranscoder>,
transcoder: &Arc<ContractMessageTranscoder>,
) -> Self {
self.set_transcoder(contract_address, transcoder);
self
Expand All @@ -234,7 +233,7 @@ where
pub fn set_transcoder(
&mut self,
contract_address: AccountIdFor<T::Runtime>,
transcoder: &Rc<ContractMessageTranscoder>,
transcoder: &Arc<ContractMessageTranscoder>,
) {
self.transcoders.register(contract_address, transcoder);
}
Expand Down Expand Up @@ -263,7 +262,7 @@ where
args: &[S],
salt: Vec<u8>,
endowment: Option<BalanceOf<T::Runtime>>,
transcoder: &Rc<ContractMessageTranscoder>,
transcoder: &Arc<ContractMessageTranscoder>,
) -> Result<Self, SessionError> {
self.deploy(
contract_bytes,
Expand Down Expand Up @@ -292,7 +291,7 @@ where
args: &[S],
salt: Vec<u8>,
endowment: Option<BalanceOf<T::Runtime>>,
transcoder: &Rc<ContractMessageTranscoder>,
transcoder: &Arc<ContractMessageTranscoder>,
) -> Result<AccountIdFor<T::Runtime>, SessionError> {
let data = transcoder
.encode(constructor, args)
Expand Down
6 changes: 3 additions & 3 deletions drink/src/session/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module provides simple utilities for loading and parsing `.contract` files in context of `drink` tests.
use std::{path::PathBuf, rc::Rc};
use std::{path::PathBuf, sync::Arc};

use contract_metadata::ContractMetadata;
use contract_transcode::ContractMessageTranscoder;
Expand All @@ -19,7 +19,7 @@ pub struct ContractBundle {
/// WASM blob of the contract
pub wasm: Vec<u8>,
/// Transcoder derived from the ABI/metadata
pub transcoder: Rc<ContractMessageTranscoder>,
pub transcoder: Arc<ContractMessageTranscoder>,
}

impl ContractBundle {
Expand All @@ -40,7 +40,7 @@ impl ContractBundle {
))
})?;

let transcoder = Rc::new(ContractMessageTranscoder::new(ink_metadata));
let transcoder = Arc::new(ContractMessageTranscoder::new(ink_metadata));

let wasm = metadata
.source
Expand Down
4 changes: 2 additions & 2 deletions drink/src/session/record.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::sync::Arc;

use contract_transcode::{ContractMessageTranscoder, Value};
use frame_system::Config as SysConfig;
Expand Down Expand Up @@ -176,7 +176,7 @@ impl EventBatch<MinimalSandboxRuntime> {
/// **WARNING 2**: This method will ignore anonymous events.
pub fn contract_events_decoded(
&self,
transcoder: &Rc<ContractMessageTranscoder>,
transcoder: &Arc<ContractMessageTranscoder>,
) -> Vec<Value> {
let signature_topics = transcoder
.metadata()
Expand Down
12 changes: 6 additions & 6 deletions drink/src/session/transcoding.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{collections::BTreeMap, rc::Rc};
use std::{collections::BTreeMap, sync::Arc};

use contract_transcode::ContractMessageTranscoder;

pub struct TranscoderRegistry<Contract: Ord> {
transcoders: BTreeMap<Contract, Rc<ContractMessageTranscoder>>,
transcoders: BTreeMap<Contract, Arc<ContractMessageTranscoder>>,
}

impl<Contract: Ord> TranscoderRegistry<Contract> {
Expand All @@ -13,11 +13,11 @@ impl<Contract: Ord> TranscoderRegistry<Contract> {
}
}

pub fn register(&mut self, contract: Contract, transcoder: &Rc<ContractMessageTranscoder>) {
self.transcoders.insert(contract, Rc::clone(transcoder));
pub fn register(&mut self, contract: Contract, transcoder: &Arc<ContractMessageTranscoder>) {
self.transcoders.insert(contract, Arc::clone(transcoder));
}

pub fn get(&self, contract: &Contract) -> Option<Rc<ContractMessageTranscoder>> {
self.transcoders.get(contract).map(Rc::clone)
pub fn get(&self, contract: &Contract) -> Option<Arc<ContractMessageTranscoder>> {
self.transcoders.get(contract).map(Arc::clone)
}
}
6 changes: 4 additions & 2 deletions examples/chain-extension/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/contract-events/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/flipper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/mocking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/multiple-contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/quick-start-with-drink/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dccd678

Please sign in to comment.