diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index daf6ac24..c08106d3 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -39,11 +39,11 @@ near-crypto = "0.20.0" near-primitives = "0.20.0" near-jsonrpc-primitives = "0.20.0" near-jsonrpc-client = { version = "0.8", features = ["sandbox"] } -near-sandbox-utils = "0.7.0" +near-sandbox-utils = {git = "https://github.com/near/sandbox", tag = "v0.7.1"} near-chain-configs = { version = "0.20.0", optional = true } [build-dependencies] -near-sandbox-utils = "0.7.0" +near-sandbox-utils = {git = "https://github.com/near/sandbox", tag = "v0.7.1"} [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index 1cea0ba1..1c018e84 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -10,7 +10,7 @@ use near_sandbox_utils as sandbox; use super::builder::{FromNetworkBuilder, NetworkBuilder}; use super::server::ValidatorKey; use super::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator}; -use crate::error::SandboxErrorCode; +use crate::error::{ErrorKind, SandboxErrorCode}; use crate::network::server::SandboxServer; use crate::network::Info; use crate::result::{Execution, ExecutionFinalResult, Result}; @@ -123,10 +123,14 @@ impl TopLevelAccountCreator for Sandbox { async fn create_tla( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, ) -> Result> { let root_signer = self.root_signer()?; + let id = AccountId::from_str( + &(account_prefix.as_str().to_string() + "." + root_signer.account_id.as_str()), + ) + .map_err(|err| ErrorKind::DataConversion.custom(err))?; let outcome = self .client() .create_account(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT) @@ -142,11 +146,15 @@ impl TopLevelAccountCreator for Sandbox { async fn create_tla_and_deploy( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, wasm: &[u8], ) -> Result> { let root_signer = self.root_signer()?; + let id = AccountId::from_str( + &(account_prefix.as_str().to_string() + "." + root_signer.account_id.as_str()), + ) + .map_err(|err| ErrorKind::DataConversion.custom(err))?; let outcome = self .client() .create_account_and_deploy( diff --git a/workspaces/src/network/testnet.rs b/workspaces/src/network/testnet.rs index 15ed8fb7..481c506f 100644 --- a/workspaces/src/network/testnet.rs +++ b/workspaces/src/network/testnet.rs @@ -1,3 +1,4 @@ +use std::convert::TryInto; use std::path::PathBuf; use std::str::FromStr; @@ -71,11 +72,15 @@ impl TopLevelAccountCreator for Testnet { async fn create_tla( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, // TODO: return Account only, but then you don't get metadata info for it... ) -> Result> { let url = Url::parse(HELPER_URL).unwrap(); + let id = account_prefix + ".testnet"; + let id: AccountId = id + .try_into() + .expect("could not convert dev account into AccountId"); tool::url_create_account(url, id.clone(), sk.public_key()).await?; let signer = InMemorySigner::from_secret_key(id, sk); @@ -107,12 +112,16 @@ impl TopLevelAccountCreator for Testnet { async fn create_tla_and_deploy( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, wasm: &[u8], ) -> Result> { + let id = account_prefix.clone() + ".testnet"; + let id: AccountId = id + .try_into() + .expect("could not convert dev account into AccountId"); let signer = InMemorySigner::from_secret_key(id.clone(), sk.clone()); - let account = self.create_tla(worker, id.clone(), sk).await?; + let account = self.create_tla(worker, account_prefix.clone(), sk).await?; let outcome = self.client().deploy(&signer, &id, wasm.into()).await?; diff --git a/workspaces/src/network/variants.rs b/workspaces/src/network/variants.rs index eb228cd8..ad700a61 100644 --- a/workspaces/src/network/variants.rs +++ b/workspaces/src/network/variants.rs @@ -1,7 +1,7 @@ use crate::network::Info; use crate::result::{Execution, Result}; use crate::rpc::client::Client; -use crate::types::{AccountId, KeyType, SecretKey}; +use crate::types::{KeyType, SecretKey}; use crate::{Account, Contract, Worker}; use async_trait::async_trait; @@ -20,14 +20,14 @@ pub trait TopLevelAccountCreator { async fn create_tla( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, ) -> Result>; async fn create_tla_and_deploy( &self, worker: Worker, - id: AccountId, + account_prefix: String, sk: SecretKey, wasm: &[u8], ) -> Result>; @@ -41,10 +41,14 @@ impl Worker where T: DevNetwork + TopLevelAccountCreator + 'static, { - pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result> { + pub async fn create_tla( + &self, + account_prefix: String, + sk: SecretKey, + ) -> Result> { let res = self .workspace - .create_tla(self.clone().coerce(), id, sk) + .create_tla(self.clone().coerce(), account_prefix, sk) .await?; for callback in self.tx_callbacks.iter() { @@ -56,13 +60,13 @@ where pub async fn create_tla_and_deploy( &self, - id: AccountId, + account_prefix: String, sk: SecretKey, wasm: &[u8], ) -> Result> { let res = self .workspace - .create_tla_and_deploy(self.clone().coerce(), id, sk, wasm) + .create_tla_and_deploy(self.clone().coerce(), account_prefix, sk, wasm) .await?; for callback in self.tx_callbacks.iter() { @@ -72,21 +76,23 @@ where Ok(res) } - pub async fn dev_generate(&self) -> (AccountId, SecretKey) { - let id = crate::rpc::tool::random_account_id(); + pub async fn dev_generate(&self) -> (String, SecretKey) { + let id = crate::rpc::tool::random_account_id_prefix(); let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED); (id, sk) } pub async fn dev_create_account(&self) -> Result { - let (id, sk) = self.dev_generate().await; - let account = self.create_tla(id.clone(), sk).await?; + let (account_prefix, sk) = self.dev_generate().await; + let account = self.create_tla(account_prefix.clone(), sk).await?; Ok(account.into_result()?) } pub async fn dev_deploy(&self, wasm: &[u8]) -> Result { - let (id, sk) = self.dev_generate().await; - let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?; + let (account_prefix, sk) = self.dev_generate().await; + let contract = self + .create_tla_and_deploy(account_prefix.clone(), sk, wasm) + .await?; Ok(contract.into_result()?) } } diff --git a/workspaces/src/rpc/tool.rs b/workspaces/src/rpc/tool.rs index 5b79d5a4..48121d65 100644 --- a/workspaces/src/rpc/tool.rs +++ b/workspaces/src/rpc/tool.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::convert::TryInto; use std::fs::File; use std::io::Write; use std::path::Path; @@ -24,13 +23,10 @@ pub(crate) fn into_state_map(state_items: Vec) -> HashMap, Ve .collect() } -pub(crate) fn random_account_id() -> AccountId { +pub(crate) fn random_account_id_prefix() -> String { let mut rng = rand::thread_rng(); let random_num = rng.gen_range(10000000000000usize..99999999999999); let account_id = format!("dev-{}-{}", Utc::now().format("%Y%m%d%H%M%S"), random_num); - let account_id: AccountId = account_id - .try_into() - .expect("could not convert dev account into AccountId"); account_id } diff --git a/workspaces/tests/gas_meter.rs b/workspaces/tests/gas_meter.rs index b1738d2d..3a114ea4 100644 --- a/workspaces/tests/gas_meter.rs +++ b/workspaces/tests/gas_meter.rs @@ -13,10 +13,10 @@ async fn test_gas_meter_with_single_transaction() -> anyhow::Result<()> { // analogous to: worker.dev_deploy(include_bytes!("*.wasm")).await?; let status_msg = { - let (id, sk) = worker.dev_generate().await; + let (account_prefix, sk) = worker.dev_generate().await; let contract = worker .create_tla_and_deploy( - id.clone(), + account_prefix.clone(), sk, include_bytes!("../../examples/res/status_message.wasm"), ) @@ -57,10 +57,10 @@ async fn test_gas_meter_with_multiple_transactions() -> anyhow::Result<()> { // analogous to: worker.dev_deploy(include_bytes!("*.wasm")).await?; let status_msg = { - let (id, sk) = worker.dev_generate().await; + let (account_prefix, sk) = worker.dev_generate().await; let contract = worker .create_tla_and_deploy( - id.clone(), + account_prefix.clone(), sk, include_bytes!("../../examples/res/status_message.wasm"), ) @@ -110,10 +110,10 @@ async fn test_gas_meter_with_parallel_transactions() -> anyhow::Result<()> { // analogous to: worker.dev_deploy(include_bytes!("*.wasm")).await?; let status_msg = { - let (id, sk) = worker.dev_generate().await; + let (account_prefix, sk) = worker.dev_generate().await; let contract = worker .create_tla_and_deploy( - id.clone(), + account_prefix.clone(), sk, include_bytes!("../../examples/res/status_message.wasm"), ) @@ -168,10 +168,10 @@ async fn test_gas_meter_with_multiple_transactions_and_view() -> anyhow::Result< // analogous to: worker.dev_deploy(include_bytes!("*.wasm")).await?; let status_msg = { - let (id, sk) = worker.dev_generate().await; + let (account_prefix, sk) = worker.dev_generate().await; let contract = worker .create_tla_and_deploy( - id.clone(), + account_prefix.clone(), sk, include_bytes!("../../examples/res/status_message.wasm"), ) @@ -231,10 +231,10 @@ async fn test_gas_meter_batch_tx() -> anyhow::Result<()> { // analogous to: worker.dev_deploy(include_bytes!("*.wasm")).await?; let contract = { - let (id, sk) = worker.dev_generate().await; + let (account_prefix, sk) = worker.dev_generate().await; let contract = worker .create_tla_and_deploy( - id.clone(), + account_prefix.clone(), sk, include_bytes!("../../examples/res/status_message.wasm"), )