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 39 commits
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
2 changes: 1 addition & 1 deletion workspaces/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +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, TopLevelAccountCreator,
NetworkClient, NetworkInfo, RootAccountSubaccountCreator, TopLevelAccountCreator,
};
pub use config::set_sandbox_genesis;
77 changes: 61 additions & 16 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::path::PathBuf;
use std::str::FromStr;

Expand All @@ -9,8 +10,8 @@ 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 super::{NetworkClient, NetworkInfo, RootAccountSubaccountCreator, TopLevelAccountCreator};
use crate::error::{ErrorKind, SandboxErrorCode};
use crate::network::server::SandboxServer;
use crate::network::Info;
use crate::result::{Execution, ExecutionFinalResult, Result};
Expand All @@ -36,16 +37,7 @@ pub struct Sandbox {

impl Sandbox {
pub(crate) fn root_signer(&self) -> Result<InMemorySigner> {
match &self.server.validator_key {
ValidatorKey::HomeDir(home_dir) => {
let path = home_dir.join("validator_key.json");
InMemorySigner::from_file(&path)
}
ValidatorKey::Known(account_id, secret_key) => Ok(InMemorySigner::from_secret_key(
account_id.clone(),
secret_key.clone(),
)),
}
InMemorySigner::try_from(self.server.validator_key.clone())
}

pub(crate) fn registrar_signer(&self) -> Result<InMemorySigner> {
Expand Down Expand Up @@ -95,9 +87,11 @@ impl Sandbox {
// lockfiles as soon as possible.
server.unlock_lockfiles()?;

let root_id = InMemorySigner::try_from(server.validator_key.clone())?.account_id;

let info = Info {
name: build.name.into(),
root_id: AccountId::from_str("test.near").unwrap(),
root_id,
keystore_path: PathBuf::from(".near-credentials/sandbox/"),
rpc_url: url::Url::parse(&server.rpc_addr()).expect("url is hardcoded"),
};
Expand Down Expand Up @@ -130,8 +124,6 @@ impl FromNetworkBuilder for Sandbox {
}
}

impl AllowDevAccountCreation for Sandbox {}

#[async_trait]
impl TopLevelAccountCreator for Sandbox {
async fn create_tla(
Expand All @@ -145,7 +137,6 @@ impl TopLevelAccountCreator for Sandbox {
.client()
.create_account(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT)
.await?;

let signer = InMemorySigner::from_secret_key(id, sk);
Ok(Execution {
result: Account::new(signer, worker),
Expand All @@ -171,7 +162,61 @@ impl TopLevelAccountCreator for Sandbox {
wasm.into(),
)
.await?;
let signer = InMemorySigner::from_secret_key(id, sk);
Ok(Execution {
result: Contract::new(signer, worker),
details: ExecutionFinalResult::from_view(outcome),
})
}
}

#[async_trait]
impl RootAccountSubaccountCreator for Sandbox {
fn root_account_id(&self) -> Result<AccountId> {
Ok(self.root_signer()?.account_id)
}

async fn create_root_account_subaccount(
&self,
worker: Worker<dyn Network>,
subaccount_prefix: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>> {
let root_id = self.root_account_id()?;
let id = AccountId::from_str(format!("{}.{}", subaccount_prefix, root_id).as_str())
.map_err(|e| ErrorKind::DataConversion.custom(e))?;
let root_signer = self.root_signer()?;
let outcome = self
.client()
.create_account(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT)
.await?;
let signer = InMemorySigner::from_secret_key(id, sk);
Ok(Execution {
result: Account::new(signer, worker),
details: ExecutionFinalResult::from_view(outcome),
})
}
async fn create_root_account_subaccount_and_deploy(
&self,
worker: Worker<dyn Network>,
subaccount_prefix: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
let id =
AccountId::from_str(format!("{}.{}", subaccount_prefix, self.info().root_id).as_str())
.map_err(|e| ErrorKind::DataConversion.custom(e))?;
let root_signer = self.root_signer()?;
let outcome = self
.client()
.create_account_and_deploy(
&root_signer,
&id,
sk.public_key(),
DEFAULT_DEPOSIT,
wasm.into(),
)
.await?;
let signer = InMemorySigner::from_secret_key(id, sk);
Ok(Execution {
result: Contract::new(signer, worker),
Expand Down
2 changes: 1 addition & 1 deletion workspaces/src/network/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn init_home_dir_with_version(version: &str) -> Result<TempDir> {
Ok(home_dir)
}

#[derive(Debug)]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ValidatorKey {
HomeDir(PathBuf),
Expand Down
40 changes: 27 additions & 13 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::{NetworkClient, NetworkInfo, RootAccountSubaccountCreator};
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 @@ -65,18 +66,26 @@ impl std::fmt::Debug for Testnet {
}
}

impl AllowDevAccountCreation for Testnet {}

#[async_trait]
impl TopLevelAccountCreator for Testnet {
async fn create_tla(
impl RootAccountSubaccountCreator for Testnet {
fn root_account_id(&self) -> Result<AccountId> {
Ok(self.info().root_id.clone())
}

async fn create_root_account_subaccount(
&self,
worker: Worker<dyn Network>,
id: AccountId,
subaccount_prefix: 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();
let root_id = self
.root_account_id()
.expect("no source of error expected on testnet");
//only registrar can create tla on testnet, so must concatenate random created id with .testnet
let id = AccountId::from_str(format!("{}.{}", subaccount_prefix, root_id).as_str())
.map_err(|e| ErrorKind::DataConversion.custom(e))?;
tool::url_create_account(url, id.clone(), sk.public_key()).await?;
let signer = InMemorySigner::from_secret_key(id, sk);

Expand Down Expand Up @@ -105,17 +114,22 @@ impl TopLevelAccountCreator for Testnet {
})
}

async fn create_tla_and_deploy(
async fn create_root_account_subaccount_and_deploy(
Copy link
Collaborator

@dj8yfo dj8yfo Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this appears to never have been run (during the lifespan of pr), as it didn't work

&self,
worker: Worker<dyn Network>,
id: AccountId,
subaccount_prefix: 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 outcome = self.client().deploy(&signer, &id, wasm.into()).await?;
let signer = InMemorySigner::from_secret_key(subaccount_prefix.clone(), sk.clone());
let account = self
.create_root_account_subaccount(worker, subaccount_prefix.clone(), sk)
.await?;

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

Ok(Execution {
result: Contract::account(account.into_result()?),
Expand Down
Loading
Loading