Skip to content

Commit

Permalink
Resolve issues after review
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKushnir1 authored and AlexKushnir1 committed Sep 10, 2024
1 parent dcff640 commit a4bdc7d
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 75 deletions.
2 changes: 1 addition & 1 deletion examples/src/croncat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn main() -> anyhow::Result<()> {
.into_result()?;

// Create a root croncat account with agent subaccounts to schedule tasks.
let croncat = worker.dev_create_account().await?;
let croncat = worker.dev_create().await?;

// This will setup a task to call into the counter contract, with a cadence of 1 hour.
println!("Creating task for `counter.increment`");
Expand Down
2 changes: 1 addition & 1 deletion examples/src/various_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() -> anyhow::Result<()> {
.await?;
println!("Latest Chunk: {chunk:#?}");

let bob = worker.dev_create_account().await?;
let bob = worker.dev_create().await?;
println!("\nCreated bob's account with id {:?}", bob.id());

// Show all the access keys relating to bob:
Expand Down
3 changes: 1 addition & 2 deletions workspaces/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub use self::sandbox::Sandbox;
pub use self::server::{pick_unused_port, ValidatorKey};
pub use self::testnet::Testnet;
pub use self::variants::{
AllowDevAccountCreation, NetworkClient, NetworkInfo, SponsoredAccountCreator,
TopLevelAccountCreator,
NetworkClient, NetworkInfo, SponsoredAccountCreator, TopLevelAccountCreator,
};
pub use config::set_sandbox_genesis;
7 changes: 1 addition & 6 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use near_sandbox_utils as sandbox;

use super::builder::{FromNetworkBuilder, NetworkBuilder};
use super::server::ValidatorKey;
use super::{
AllowDevAccountCreation, NetworkClient, NetworkInfo, SponsoredAccountCreator,
TopLevelAccountCreator,
};
use super::{NetworkClient, NetworkInfo, SponsoredAccountCreator, TopLevelAccountCreator};
use crate::error::{ErrorKind, SandboxErrorCode};
use crate::network::server::SandboxServer;
use crate::network::Info;
Expand Down Expand Up @@ -131,8 +128,6 @@ impl FromNetworkBuilder for Sandbox {
}
}

impl AllowDevAccountCreation for Sandbox {}

#[async_trait]
impl TopLevelAccountCreator for Sandbox {
async fn create_tla(
Expand Down
6 changes: 1 addition & 5 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use near_primitives::views::ExecutionStatusView;
use crate::error::ErrorKind;
use crate::network::builder::{FromNetworkBuilder, NetworkBuilder};
use crate::network::Info;
use crate::network::{
AllowDevAccountCreation, NetworkClient, NetworkInfo, SponsoredAccountCreator,
};
use crate::network::{NetworkClient, NetworkInfo, SponsoredAccountCreator};
use crate::result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result};
use crate::rpc::{client::Client, tool};
use crate::types::{AccountId, InMemorySigner, NearToken, SecretKey};
Expand Down Expand Up @@ -67,8 +65,6 @@ impl std::fmt::Debug for Testnet {
}
}

impl AllowDevAccountCreation for Testnet {}

#[async_trait]
impl SponsoredAccountCreator for Testnet {
async fn create_sponsored_account(
Expand Down
41 changes: 16 additions & 25 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ pub trait TopLevelAccountCreator {
) -> Result<Execution<Contract>>;
}

// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy or dev_deploy_tla.
// This trait acts as segmented boundary for only specific networks such as sandbox and testnet.
pub trait AllowDevAccountCreation {}

impl<T> Worker<T>
where
T: DevNetwork + TopLevelAccountCreator + 'static,
T: Network + TopLevelAccountCreator + 'static,
{
pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result<Execution<Account>> {
let res = self
Expand Down Expand Up @@ -128,9 +124,9 @@ where

impl<T> Worker<T>
where
T: DevNetwork + SponsoredAccountCreator + 'static,
T: DevNetwork + 'static,
{
pub async fn create_dev_account(
pub async fn create_sponsored_account(
&self,
subaccount_prefix: AccountId,
sk: SecretKey,
Expand All @@ -151,12 +147,16 @@ where
Ok(res)
}

pub async fn create_dev_account_and_deploy(
pub async fn create_sponsored_account_and_deploy(
&self,
subaccount_prefix: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
if subaccount_prefix.as_str().contains('.') {
return Err(crate::error::ErrorKind::Io
.custom("Subaccount prefix for sponsored account cannot contain '.'"));
}
let res = self
.workspace
.create_sponsored_account_and_deploy(self.clone().coerce(), subaccount_prefix, sk, wasm)
Expand All @@ -175,28 +175,19 @@ where
(id, sk)
}

/// Creates a top level developement account.
/// On sandbox network it has a balance of 100 Near.
/// If you need more Near for your tests in sandbox consider using `root_account()` method:
///
/// # Examples
/// ```
/// use near_workspaces::{result::Result, Account, network::Sandbox, Worker};
/// fn get_account_with_lots_of_near(worker: &Worker<Sandbox>) -> Result<Account> {
/// worker.root_account()
/// }
/// ```
///
pub async fn dev_create_account(&self) -> Result<Account> {
/// Creates a sub-account of the network root account with
/// random account ID and secret key. By default, balance is around 10 Near.
pub async fn dev_create(&self) -> Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_dev_account(id.clone(), sk).await?;
let account = self.create_sponsored_account(id.clone(), sk).await?;
Ok(account.into_result()?)
}

pub async fn dev_deploy(&self, wasm: &[u8]) -> Result<Contract> {
let (id, sk) = self.dev_generate().await;
let contract = self
.create_dev_account_and_deploy(id.clone(), sk, wasm)
.create_sponsored_account_and_deploy(id.clone(), sk, wasm)
.await?;
Ok(contract.into_result()?)
}
Expand All @@ -209,6 +200,6 @@ pub trait Network: NetworkInfo + NetworkClient + Send + Sync {}
impl<T> Network for T where T: NetworkInfo + NetworkClient + Send + Sync {}

/// DevNetwork is a Network that can call into `dev_create` and `dev_deploy` to create developer accounts.
pub trait DevNetwork: AllowDevAccountCreation + Network + 'static {}
pub trait DevNetwork: Network + SponsoredAccountCreator + 'static {}

impl<T> DevNetwork for T where T: AllowDevAccountCreation + Network + 'static {}
impl<T> DevNetwork for T where T: Network + SponsoredAccountCreator + 'static {}
4 changes: 1 addition & 3 deletions workspaces/src/worker/impls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_primitives::views::StatusResponse;

use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo};
use crate::network::{Info, Sandbox};
use crate::network::{NetworkClient, NetworkInfo};
use crate::operations::{CallTransaction, Function};
use crate::result::{ExecutionFinalResult, Result};
use crate::rpc::client::Client;
Expand Down Expand Up @@ -41,8 +41,6 @@ impl<T: ?Sized> Clone for Worker<T> {
}
}

impl<T> AllowDevAccountCreation for Worker<T> where T: AllowDevAccountCreation {}

impl<T> NetworkInfo for Worker<T>
where
T: NetworkInfo,
Expand Down
20 changes: 3 additions & 17 deletions workspaces/tests/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![recursion_limit = "256"]
use near_token::NearToken;
use near_workspaces::network::NetworkInfo;
use serde_json::{Map, Value};
use test_log::test;

Expand All @@ -9,8 +8,8 @@ use std::path::Path;

#[test(tokio::test)]
async fn test_subaccount_creation() -> anyhow::Result<()> {
let worker = near_workspaces::testnet().await?;
let account = worker.dev_create_account().await?;
let worker = near_workspaces::sandbox().await?;
let account = worker.dev_create().await?;

let sub = account
.create_subaccount("subaccount")
Expand All @@ -33,16 +32,6 @@ async fn test_subaccount_creation() -> anyhow::Result<()> {
Some(&Value::String(sub.id().to_string()))
);

let res = worker
.delete_account(sub.id(), sub.signer(), &worker.info().root_id)
.await?;
assert!(res.is_success());

let res = worker
.delete_account(account.id(), account.signer(), &worker.info().root_id)
.await?;
assert!(res.is_success());

fs::remove_file(savedir.join(format!("{}.json", sub.id())))?;

Ok(())
Expand Down Expand Up @@ -82,10 +71,7 @@ async fn test_transfer_near() -> anyhow::Result<()> {
async fn test_delete_account() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;

let (alice, bob) = (
worker.dev_create_account().await?,
worker.dev_create_account().await?,
);
let (alice, bob) = (worker.dev_create().await?, worker.dev_create().await?);

_ = alice.clone().delete_account(bob.id()).await?;

Expand Down
22 changes: 11 additions & 11 deletions workspaces/tests/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn test_gas_meter_with_single_transaction() -> anyhow::Result<()> {
let status_msg = {
let (id, sk) = worker.dev_generate().await;
let contract = worker
.create_dev_account_and_deploy(
.create_sponsored_account_and_deploy(
id.clone(),
sk,
include_bytes!("../../examples/res/status_message.wasm"),
Expand All @@ -29,7 +29,7 @@ async fn test_gas_meter_with_single_transaction() -> anyhow::Result<()> {
// analogous to: worker.dev_create_account().await?;
let account = {
let (id, sk) = worker.dev_generate().await;
let account = worker.create_dev_account(id.clone(), sk).await?;
let account = worker.create_sponsored_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand Down Expand Up @@ -59,7 +59,7 @@ async fn test_gas_meter_with_multiple_transactions() -> anyhow::Result<()> {
let status_msg = {
let (id, sk) = worker.dev_generate().await;
let contract = worker
.create_dev_account_and_deploy(
.create_sponsored_account_and_deploy(
id.clone(),
sk,
include_bytes!("../../examples/res/status_message.wasm"),
Expand All @@ -73,7 +73,7 @@ async fn test_gas_meter_with_multiple_transactions() -> anyhow::Result<()> {
// analogous to: worker.dev_create_account().await?;
let account = {
let (id, sk) = worker.dev_generate().await;
let account = worker.create_dev_account(id.clone(), sk).await?;
let account = worker.create_sponsored_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand Down Expand Up @@ -112,7 +112,7 @@ async fn test_gas_meter_with_parallel_transactions() -> anyhow::Result<()> {
let status_msg = {
let (id, sk) = worker.dev_generate().await;
let contract = worker
.create_dev_account_and_deploy(
.create_sponsored_account_and_deploy(
id.clone(),
sk,
include_bytes!("../../examples/res/status_message.wasm"),
Expand All @@ -126,7 +126,7 @@ async fn test_gas_meter_with_parallel_transactions() -> anyhow::Result<()> {
// analogous to: worker.dev_create_account().await?;
let account = {
let (id, sk) = worker.dev_generate().await;
let account = worker.create_dev_account(id.clone(), sk).await?;
let account = worker.create_sponsored_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand Down Expand Up @@ -170,7 +170,7 @@ async fn test_gas_meter_with_multiple_transactions_and_view() -> anyhow::Result<
let status_msg = {
let (id, sk) = worker.dev_generate().await;
let contract = worker
.create_dev_account_and_deploy(
.create_sponsored_account_and_deploy(
id.clone(),
sk,
include_bytes!("../../examples/res/status_message.wasm"),
Expand All @@ -184,7 +184,7 @@ async fn test_gas_meter_with_multiple_transactions_and_view() -> anyhow::Result<
// analogous to: worker.dev_create_account().await?;
let account = {
let (id, sk) = worker.dev_generate().await;
let account = worker.create_dev_account(id.clone(), sk).await?;
let account = worker.create_sponsored_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand Down Expand Up @@ -233,7 +233,7 @@ async fn test_gas_meter_batch_tx() -> anyhow::Result<()> {
let contract = {
let (id, sk) = worker.dev_generate().await;
let contract = worker
.create_dev_account_and_deploy(
.create_sponsored_account_and_deploy(
id.clone(),
sk,
include_bytes!("../../examples/res/status_message.wasm"),
Expand Down Expand Up @@ -291,7 +291,7 @@ async fn test_gas_meter_create_account_transaction() -> anyhow::Result<()> {
// analogous to: worker.dev_create_account().await?;
let account = {
let (id, sk) = worker.dev_generate().await;
let account = worker.create_dev_account(id.clone(), sk).await?;
let account = worker.create_sponsored_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand All @@ -311,7 +311,7 @@ async fn test_dropped_gas_meter() -> anyhow::Result<()> {
let gas_meter = GasMeter::now(&mut worker);
drop(gas_meter);

worker.dev_create_account().await?;
worker.dev_create().await?;

Ok(())
}
4 changes: 2 additions & 2 deletions workspaces/tests/parallel_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const STATUS_MSG_CONTRACT: &[u8] = include_bytes!("../../examples/res/status_mes
async fn test_parallel() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let contract = worker.dev_deploy(STATUS_MSG_CONTRACT).await?;
let account = worker.dev_create_account().await?;
let account = worker.dev_create().await?;

let parallel_tasks = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
.iter()
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn test_parallel() -> anyhow::Result<()> {
async fn test_parallel_async() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
let contract = worker.dev_deploy(STATUS_MSG_CONTRACT).await?;
let account = worker.dev_create_account().await?;
let account = worker.dev_create().await?;

// nonce of access key before any transactions occurred.
let nonce_start = worker
Expand Down
4 changes: 2 additions & 2 deletions workspaces/tests/patch_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async fn test_patch_code_hash() -> anyhow::Result<()> {
let status_msg_acc = worker.view_account(&contract_id).await?;
let status_msg_code = worker.view_code(&contract_id).await?;

let bob = worker.dev_create_account().await?;
let bob = worker.dev_create().await?;

// Patching code bytes should also set the code hash, otherwise the node will crash
// when we try to do anything with the contract.
Expand All @@ -191,7 +191,7 @@ async fn test_patch_code_hash() -> anyhow::Result<()> {
async fn test_patch_account_from_current() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;

let bob = worker.dev_create_account().await?;
let bob = worker.dev_create().await?;

const NEW_BALANCE: NearToken = NearToken::from_yoctonear(10_u128.pow(16));

Expand Down

0 comments on commit a4bdc7d

Please sign in to comment.