diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d85ca464..2fedcb0c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,7 +22,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Run clippy - run: cargo clippy --benches -- -D clippy::all + run: cargo clippy --all-targets -- -D clippy::all -D clippy::nursery cargo-fmt: runs-on: ubuntu-20.04 diff --git a/examples/src/croncat.rs b/examples/src/croncat.rs index 9bea218d..aaff0683 100644 --- a/examples/src/croncat.rs +++ b/examples/src/croncat.rs @@ -18,7 +18,7 @@ const COUNTER_CONTRACT: &[u8] = include_bytes!("../res/counter.wasm"); /// `AgentStatus` struct taken from [croncat repo](github.com/CronCats/contracts/) to /// deserialize into after we get the result of a transaction and converting over to /// this particular type. -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Deserialize, PartialEq, Eq)] pub enum AgentStatus { Active, Pending, diff --git a/examples/src/spooning.rs b/examples/src/spooning.rs index 1b0e36f2..0c38346d 100644 --- a/examples/src/spooning.rs +++ b/examples/src/spooning.rs @@ -99,11 +99,7 @@ async fn main() -> anyhow::Result<()> { // Patch our testnet STATE into our local sandbox: worker - .patch_state( - sandbox_contract.id(), - "STATE".as_bytes(), - &status_msg.try_to_vec()?, - ) + .patch_state(sandbox_contract.id(), b"STATE", &status_msg.try_to_vec()?) .await?; // Now grab the state to see that it has indeed been patched: diff --git a/workspaces/src/error/impls.rs b/workspaces/src/error/impls.rs index 3f434ead..3cf7e1f6 100644 --- a/workspaces/src/error/impls.rs +++ b/workspaces/src/error/impls.rs @@ -137,7 +137,7 @@ impl std::error::Error for Error { impl From> for Error { fn from(value: PoisonError) -> Self { - Error::custom(ErrorKind::Other, value.to_string()) + Self::custom(ErrorKind::Other, value.to_string()) } } @@ -167,7 +167,7 @@ impl SandboxErrorCode { impl From for Error { fn from(code: SandboxErrorCode) -> Self { - Error::simple(ErrorKind::Sandbox(code)) + Self::simple(ErrorKind::Sandbox(code)) } } @@ -189,6 +189,6 @@ impl RpcErrorCode { impl From for Error { fn from(code: RpcErrorCode) -> Self { - Error::simple(ErrorKind::Rpc(code)) + Self::simple(ErrorKind::Rpc(code)) } } diff --git a/workspaces/src/network/builder.rs b/workspaces/src/network/builder.rs index 0cea9155..2e48e441 100644 --- a/workspaces/src/network/builder.rs +++ b/workspaces/src/network/builder.rs @@ -8,7 +8,7 @@ use super::server::ValidatorKey; pub(crate) type BoxFuture<'a, T> = std::pin::Pin + Send + 'a>>; -/// This trait provides a way to construct Networks out of a single builder. Currently +/// This trait provides a way to construct Networks out of a single builder. Currently, /// not planned to offer this trait outside, since the custom networks can just construct /// themselves however they want utilizing `Worker::new` like so: /// ```ignore diff --git a/workspaces/src/result.rs b/workspaces/src/result.rs index 12c59999..286c05c1 100644 --- a/workspaces/src/result.rs +++ b/workspaces/src/result.rs @@ -399,7 +399,7 @@ impl ViewResultDetails { impl From for ViewResultDetails { fn from(result: CallResult) -> Self { - ViewResultDetails { + Self { result: result.result, logs: result.logs, } @@ -525,7 +525,7 @@ impl Value { impl From for ExecutionOutcome { fn from(view: ExecutionOutcomeWithIdView) -> Self { - ExecutionOutcome { + Self { transaction_hash: CryptoHash(view.id.0), block_hash: CryptoHash(view.block_hash.0), logs: view.outcome.logs, diff --git a/workspaces/src/rpc/client.rs b/workspaces/src/rpc/client.rs index b2cf3ccb..16ba48bf 100644 --- a/workspaces/src/rpc/client.rs +++ b/workspaces/src/rpc/client.rs @@ -81,10 +81,7 @@ impl Client { pub(crate) async fn query_broadcast_tx( &self, method: &methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest, - ) -> MethodCallResult< - FinalExecutionOutcomeView, - near_jsonrpc_primitives::types::transactions::RpcTransactionError, - > { + ) -> MethodCallResult { retry(|| async { let result = self.rpc_client.call(method).await; match &result { @@ -124,16 +121,16 @@ impl Client { pub(crate) async fn query_nolog(&self, method: M) -> MethodCallResult where - M: methods::RpcMethod, + M: methods::RpcMethod + Send + Sync, { retry(|| async { self.rpc_client.call(&method).await }).await } pub(crate) async fn query(&self, method: M) -> MethodCallResult where - M: methods::RpcMethod + Debug, - M::Response: Debug, - M::Error: Debug, + M: methods::RpcMethod + Debug + Send + Sync, + M::Response: Debug + Send, + M::Error: Debug + Send, { retry(|| async { let result = self.rpc_client.call(&method).await; @@ -456,7 +453,7 @@ impl Client { pub(crate) async fn access_key( client: &Client, - account_id: near_primitives::account::id::AccountId, + account_id: AccountId, public_key: near_crypto::PublicKey, ) -> Result<(AccessKeyView, CryptoHash)> { let query_resp = client @@ -522,8 +519,8 @@ async fn fetch_tx_nonce( pub(crate) async fn retry(task: F) -> T::Output where - F: FnMut() -> T, - T: core::future::Future>, + F: FnMut() -> T + Send, + T: core::future::Future> + Send, { // Exponential backoff starting w/ 5ms for maximum retry of 4 times with the following delays: // 5, 25, 125, 625 ms diff --git a/workspaces/src/rpc/patch.rs b/workspaces/src/rpc/patch.rs index 0956a2ca..789eade5 100644 --- a/workspaces/src/rpc/patch.rs +++ b/workspaces/src/rpc/patch.rs @@ -97,7 +97,7 @@ impl<'a> ImportContractTransaction<'a> { } /// Process the transaction, and return the result of the execution. - pub async fn transact(self) -> crate::result::Result { + pub async fn transact(self) -> Result { let from_account_id = self.account_id; let into_account_id = self.into_account_id.as_ref().unwrap_or(from_account_id); @@ -153,7 +153,7 @@ impl<'a> ImportContractTransaction<'a> { /// or to patch an entire account. enum AccountUpdate { Update(AccountDetailsPatch), - FromCurrent(Box AccountDetailsPatch>), + FromCurrent(Box AccountDetailsPatch + Send>), } pub struct PatchTransaction { @@ -166,7 +166,7 @@ pub struct PatchTransaction { impl PatchTransaction { pub(crate) fn new(worker: &Worker, account_id: AccountId) -> Self { - PatchTransaction { + Self { account_id, records: vec![], worker: worker.clone(), @@ -184,9 +184,9 @@ impl PatchTransaction { /// Patch and overwrite the info contained inside an [`crate::Account`] in sandbox. This /// will allow us to fetch the current details on the chain and allow us to update /// the account details w.r.t to them. - pub fn account_from_current(mut self, f: F) -> Self + pub fn account_from_current(mut self, f: F) -> Self where - F: Fn(AccountDetails) -> AccountDetailsPatch, + F: Fn(AccountDetails) -> AccountDetailsPatch + Send + 'static, { self.account_updates .push(AccountUpdate::FromCurrent(Box::new(f))); diff --git a/workspaces/src/types/account.rs b/workspaces/src/types/account.rs index d742a83f..ff08ff93 100644 --- a/workspaces/src/types/account.rs +++ b/workspaces/src/types/account.rs @@ -33,7 +33,7 @@ impl fmt::Debug for Account { impl Account { /// Create a new account with the given path to the credentials JSON file pub fn from_file( - path: impl AsRef, + path: impl AsRef, worker: &Worker, ) -> Result { let signer = InMemorySigner::from_file(path.as_ref())?; @@ -179,7 +179,7 @@ impl Account { } /// Store the credentials of this account locally in the directory provided. - pub async fn store_credentials(&self, save_dir: impl AsRef) -> Result<()> { + pub async fn store_credentials(&self, save_dir: impl AsRef + Send) -> Result<()> { let savepath = save_dir.as_ref(); std::fs::create_dir_all(&save_dir).map_err(|e| ErrorKind::Io.custom(e))?; let savepath = savepath.join(format!("{}.json", self.id())); @@ -334,7 +334,7 @@ pub struct AccountDetailsPatch { } impl AccountDetailsPatch { - pub fn reduce(&mut self, acc: AccountDetailsPatch) { + pub fn reduce(&mut self, acc: Self) { if let Some(balance) = acc.balance { self.balance = Some(balance); } diff --git a/workspaces/src/types/chunk.rs b/workspaces/src/types/chunk.rs index ff01b4bd..b0e33e9c 100644 --- a/workspaces/src/types/chunk.rs +++ b/workspaces/src/types/chunk.rs @@ -50,7 +50,7 @@ impl From for Chunk { impl From for ChunkHeader { fn from(view: ChunkHeaderView) -> Self { - ChunkHeader { + Self { chunk_hash: view.chunk_hash.into(), prev_block_hash: view.prev_block_hash.into(), height_created: view.height_created, diff --git a/workspaces/src/types/gas_meter.rs b/workspaces/src/types/gas_meter.rs index 7b75f2b6..061c09f4 100644 --- a/workspaces/src/types/gas_meter.rs +++ b/workspaces/src/types/gas_meter.rs @@ -67,8 +67,7 @@ impl GasMeter { /// Reset the gas consumed to 0. pub fn reset(&self) -> Result<()> { - let mut meter = self.gas.lock()?; - *meter = Gas::from_gas(0); + *self.gas.lock()? = Gas::from_gas(0); Ok(()) } } diff --git a/workspaces/src/types/mod.rs b/workspaces/src/types/mod.rs index 33a41e8d..c93d1621 100644 --- a/workspaces/src/types/mod.rs +++ b/workspaces/src/types/mod.rs @@ -103,8 +103,8 @@ impl TryFrom for KeyType { fn try_from(value: u8) -> Result { match value { - 0 => Ok(KeyType::ED25519), - 1 => Ok(KeyType::SECP256K1), + 0 => Ok(Self::ED25519), + 1 => Ok(Self::SECP256K1), unknown_key_type => Err(ErrorKind::DataConversion .custom(format!("Unknown key type provided: {unknown_key_type}"))), } @@ -336,7 +336,7 @@ impl TryFrom<&[u8]> for CryptoHash { } let mut buf = [0; 32]; buf.copy_from_slice(bytes); - Ok(CryptoHash(buf)) + Ok(Self(buf)) } } diff --git a/workspaces/src/worker/impls.rs b/workspaces/src/worker/impls.rs index a1f2e12b..1d660dae 100644 --- a/workspaces/src/worker/impls.rs +++ b/workspaces/src/worker/impls.rs @@ -49,9 +49,9 @@ where } } -impl Worker +impl Worker where - T: NetworkClient, + T: NetworkClient + ?Sized, { pub(crate) fn client(&self) -> &Client { self.workspace.client() @@ -144,8 +144,27 @@ where ) } + /// View account details of a specific account on the network. + pub fn view_account(&self, account_id: &AccountId) -> Query<'_, ViewAccount> { + Query::new( + self.client(), + ViewAccount { + account_id: account_id.clone(), + }, + ) + } + + pub fn gas_price(&self) -> Query<'_, GasPrice> { + Query::new(self.client(), GasPrice) + } +} + +impl Worker +where + T: NetworkClient + Send + Sync + ?Sized, +{ /// Transfer tokens from one account to another. The signer is the account - /// that will be used to to send from. + /// that will be used to send from. pub async fn transfer_near( &self, signer: &InMemorySigner, @@ -173,26 +192,12 @@ where .map(ExecutionFinalResult::from_view) .map_err(crate::error::Error::from) } - - /// View account details of a specific account on the network. - pub fn view_account(&self, account_id: &AccountId) -> Query<'_, ViewAccount> { - Query::new( - self.client(), - ViewAccount { - account_id: account_id.clone(), - }, - ) - } - - pub fn gas_price(&self) -> Query<'_, GasPrice> { - Query::new(self.client(), GasPrice) - } } #[cfg(feature = "experimental")] -impl Worker +impl Worker where - T: NetworkClient, + T: NetworkClient + Send + Sync + ?Sized, { pub async fn changes_in_block( &self, diff --git a/workspaces/src/worker/mod.rs b/workspaces/src/worker/mod.rs index 505050a2..036570f4 100644 --- a/workspaces/src/worker/mod.rs +++ b/workspaces/src/worker/mod.rs @@ -10,7 +10,7 @@ use crate::{Network, Result}; /// The `Worker` type allows us to interact with any NEAR related networks, such /// as mainnet and testnet. This controls where the environment the worker is -/// running on top of is. Refer to this for all network related actions such as +/// running on top of it. Refer to this for all network related actions such as /// deploying a contract, or interacting with transactions. pub struct Worker { pub(crate) workspace: Arc, @@ -97,8 +97,8 @@ pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> { /// Run a locally scoped task where a [`sandbox`] instanced [`Worker`] is supplied. pub async fn with_sandbox(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(sandbox().await?).await) } @@ -106,8 +106,8 @@ where /// Run a locally scoped task where a [`testnet`] instanced [`Worker`] is supplied. pub async fn with_testnet(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(testnet().await?).await) } @@ -115,8 +115,8 @@ where /// Run a locally scoped task where a [`testnet_archival`] instanced [`Worker`] is supplied. pub async fn with_testnet_archival(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(testnet_archival().await?).await) } @@ -124,8 +124,8 @@ where /// Run a locally scoped task where a [`mainnet`] instanced [`Worker`] is supplied. pub async fn with_mainnet(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(mainnet().await?).await) } @@ -133,8 +133,8 @@ where /// Run a locally scoped task where a [`mainnet_archival`] instanced [`Worker`] is supplied. pub async fn with_mainnet_archival(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(mainnet_archival().await?).await) } @@ -142,8 +142,8 @@ where /// Run a locally scoped task where a [`betanet`] instanced [`Worker`] is supplied. pub async fn with_betanet(task: F) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(betanet().await?).await) } @@ -151,8 +151,8 @@ where #[allow(dead_code)] pub async fn with_custom(task: F, rpc_url: &str) -> Result where - F: Fn(Worker) -> T, - T: core::future::Future, + F: Fn(Worker) -> T + Send + Sync, + T: core::future::Future + Send, { Ok(task(custom(rpc_url).await?).await) }