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

feat!: worker.dev_create_account, worker.dev_deploy create subaccounts of root account instead of tla (sandbox, testnet) #369

Merged
merged 45 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f1b7d44
departed from tla on testnet
Aug 8, 2024
e1d0355
delete the accounts in test
Aug 15, 2024
eb64adc
avoid hardcodings
Aug 16, 2024
b49652c
Merge branch 'main' into remove-using-tla-on-testnet
AlexKushnir1 Aug 16, 2024
2e9c233
add new trait SponsoreAccountCreator
Aug 19, 2024
c0835a4
fmt
Aug 19, 2024
d95e45b
released new methods of traits and renamed tla functions
Aug 29, 2024
522632d
fix
Aug 30, 2024
7ab94b2
println result of deleting acc, isolate macos
Aug 30, 2024
47bf6a6
test only on macos
Aug 30, 2024
5df26ba
test test_batch_tx
Aug 30, 2024
6a92844
unused variables
Aug 30, 2024
50dda2f
find an error state
Aug 30, 2024
8adbcaa
isolate second call function
Aug 30, 2024
5ebdfe5
skip test_batch_tx()
Aug 30, 2024
680c8f7
testing batch_tx
Aug 30, 2024
b1cf6cf
add deposit to second call
Sep 2, 2024
7abadd6
separate two calls
Sep 2, 2024
3126db7
the code that was intended by merge is returned
Sep 3, 2024
a25019d
Merge branch 'main' into remove-using-tla-on-testnet
AlexKushnir1 Sep 5, 2024
dcff640
Merge branch 'near:main' into remove-using-tla-on-testnet
AlexKushnir1 Sep 10, 2024
a4bdc7d
Resolve issues after review
Sep 10, 2024
10d6b28
clippy
Sep 10, 2024
0e63022
remove unused import
Sep 10, 2024
48ca462
unused imports
Sep 10, 2024
adec38f
Merge branch 'main' into remove-using-tla-on-testnet
AlexKushnir1 Sep 26, 2024
d10894a
Merge branch 'main' into remove-using-tla-on-testnet
AlexKushnir1 Nov 1, 2024
679d18e
Merge branch 'main' into remove-using-tla-on-testnet
AlexKushnir1 Nov 22, 2024
1b61afe
Merge branch 'near:main' into remove-using-tla-on-testnet
AlexKushnir1 Dec 19, 2024
c2e4b43
clippy formating
Dec 19, 2024
981d65e
fmt formatting
Dec 19, 2024
2fc538d
formated names after review
Dec 20, 2024
1f11dea
chore: add Worker::<Testnet>::root_account_id method
Dec 25, 2024
69fb351
chore: fixes #398
Dec 25, 2024
b14b60e
doc: move `root_account` doc snippet
Dec 25, 2024
1455779
doc: add comments around `tla` methods
Dec 25, 2024
3c867de
chore: deduplicate into single `generate_dev_account_credentials` method
Dec 25, 2024
1a54933
chore: add `root_account_id` trait method and rename trait
Dec 25, 2024
adaf976
chore: make generate_dev_account_credentials sync (clippy)
Dec 25, 2024
20897d1
fix: dev_deploy on testnet
Dec 26, 2024
5b3000e
chore: use `root_account_id` method in `create_root_account_subaccoun…
Dec 26, 2024
66e424a
ci: add `fail-fast: false` to test job
Dec 26, 2024
1388747
chore: move `subaccount_prefix` validation to trait methods
Dec 26, 2024
55e538f
test: add test_dev_create_account{testnet, sandbox}
Dec 26, 2024
dea6e6b
chore: extract a repeating thingy as a default implementation of new …
Dec 26, 2024
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
4 changes: 1 addition & 3 deletions workspaces/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@ pub use self::mainnet::Mainnet;
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, TopLevelAccountCreator,
};
pub use self::variants::{AllowDevAccountCreation, DevAccountCreator, NetworkClient, NetworkInfo};
pub use config::set_sandbox_genesis;
8 changes: 4 additions & 4 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use near_sandbox_utils as sandbox;

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

#[async_trait]
impl TopLevelAccountCreator for Sandbox {
async fn create_tla(
impl DevAccountCreator for Sandbox {
async fn create_dev_account(
&self,
worker: Worker<dyn Network>,
id: AccountId,
Expand All @@ -151,7 +151,7 @@ impl TopLevelAccountCreator for Sandbox {
})
}

async fn create_tla_and_deploy(
async fn create_dev_account_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
Expand Down
23 changes: 17 additions & 6 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use url::Url;

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, TopLevelAccountCreator};
use crate::network::{AllowDevAccountCreation, DevAccountCreator, NetworkClient, NetworkInfo};
use crate::result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result};
use crate::rpc::{client::Client, tool};
use crate::types::{AccountId, InMemorySigner, NearToken, SecretKey};
Expand All @@ -18,7 +19,7 @@ use crate::{Account, Contract, CryptoHash, Network, Worker};
/// URL to the testnet RPC node provided by near.org.
pub const RPC_URL: &str = "https://rpc.testnet.near.org";

/// URL to the helper contract used to create top-level-accounts (TLA) provided by near.org.
/// URL to the helper contract used to create named accounts provided by near.org.
pub const HELPER_URL: &str = "https://helper.testnet.near.org";

/// URL to the testnet archival RPC node provided by near.org.
Expand Down Expand Up @@ -67,15 +68,25 @@ impl std::fmt::Debug for Testnet {
impl AllowDevAccountCreation for Testnet {}

#[async_trait]
impl TopLevelAccountCreator for Testnet {
async fn create_tla(
impl DevAccountCreator for Testnet {
async fn create_dev_account(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
// TODO: return Account only, but then you don't get metadata info for it...
) -> Result<Execution<Account>> {
let url = Url::parse(HELPER_URL).unwrap();
//only registrar can create tla on testnet, so must concatenate random created id with .testnet
let id = if self.info().name.eq("testnet") {
AccountId::from_str(format!("{}.testnet", id.as_str()).as_str())
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|e| ErrorKind::DataConversion.custom(e))?
} else {
return Err(ErrorKind::Other.message(format!(
"Invalid network. Expected testnet, but got {}",
self.info().name
)));
};
tool::url_create_account(url, id.clone(), sk.public_key()).await?;
let signer = InMemorySigner::from_secret_key(id, sk);

Expand Down Expand Up @@ -104,15 +115,15 @@ impl TopLevelAccountCreator for Testnet {
})
}

async fn create_tla_and_deploy(
async fn create_dev_account_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
let signer = InMemorySigner::from_secret_key(id.clone(), sk.clone());
let account = self.create_tla(worker, id.clone(), sk).await?;
let account = self.create_dev_account(worker, id.clone(), sk).await?;

let outcome = self.client().deploy(&signer, &id, wasm.into()).await?;

Expand Down
33 changes: 18 additions & 15 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub trait NetworkInfo {
}

#[async_trait]
pub trait TopLevelAccountCreator {
async fn create_tla(
pub trait DevAccountCreator {
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved
async fn create_dev_account(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>>;

async fn create_tla_and_deploy(
async fn create_dev_account_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
Expand All @@ -39,12 +39,16 @@ pub trait AllowDevAccountCreation {}

impl<T> Worker<T>
where
T: DevNetwork + TopLevelAccountCreator + 'static,
T: DevNetwork + DevAccountCreator + 'static,
{
pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result<Execution<Account>> {
pub async fn create_dev_account(
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved
&self,
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>> {
let res = self
.workspace
.create_tla(self.clone().coerce(), id, sk)
.create_dev_account(self.clone().coerce(), id, sk)
.await?;

for callback in self.tx_callbacks.iter() {
Expand All @@ -54,15 +58,15 @@ where
Ok(res)
}

pub async fn create_tla_and_deploy(
pub async fn create_dev_account_and_deploy(
&self,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
let res = self
.workspace
.create_tla_and_deploy(self.clone().coerce(), id, sk, wasm)
.create_dev_account_and_deploy(self.clone().coerce(), id, sk, wasm)
.await?;

for callback in self.tx_callbacks.iter() {
Expand Down Expand Up @@ -92,13 +96,15 @@ where
///
pub async fn dev_create_account(&self) -> Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_tla(id.clone(), sk).await?;
let account = self.create_dev_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_tla_and_deploy(id.clone(), sk, wasm).await?;
let contract = self
.create_dev_account_and_deploy(id.clone(), sk, wasm)
.await?;
Ok(contract.into_result()?)
}
}
Expand All @@ -110,9 +116,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: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static {}
pub trait DevNetwork: DevAccountCreator + AllowDevAccountCreation + Network + 'static {}

impl<T> DevNetwork for T where
T: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static
{
}
impl<T> DevNetwork for T where T: DevAccountCreator + AllowDevAccountCreation + Network + 'static {}
2 changes: 1 addition & 1 deletion workspaces/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! All traits that are essential to the ease of use of workspaces.

pub use crate::network::TopLevelAccountCreator;
pub use crate::network::DevAccountCreator;
2 changes: 1 addition & 1 deletion workspaces/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::Path;

#[test(tokio::test)]
async fn test_subaccount_creation() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved
let worker = near_workspaces::testnet().await?;
let account = worker.dev_create_account().await?;
AlexKushnir1 marked this conversation as resolved.
Show resolved Hide resolved

let sub = account
Expand Down
20 changes: 10 additions & 10 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_tla_and_deploy(
.create_dev_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_tla(id.clone(), sk).await?;
let account = worker.create_dev_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_tla_and_deploy(
.create_dev_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_tla(id.clone(), sk).await?;
let account = worker.create_dev_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_tla_and_deploy(
.create_dev_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_tla(id.clone(), sk).await?;
let account = worker.create_dev_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_tla_and_deploy(
.create_dev_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_tla(id.clone(), sk).await?;
let account = worker.create_dev_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_tla_and_deploy(
.create_dev_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_tla(id.clone(), sk).await?;
let account = worker.create_dev_account(id.clone(), sk).await?;
total_gas += account.details.total_gas_burnt.as_gas();

account.into_result()?
Expand Down
Loading