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

Testnet: error handling and add .testnet to dev_generate #362

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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};
Expand Down Expand Up @@ -75,6 +76,11 @@ impl TopLevelAccountCreator for Testnet {
sk: SecretKey,
// TODO: return Account only, but then you don't get metadata info for it...
) -> Result<Execution<Account>> {
let mut id = id;
if self.info().name.eq("testnet") {
id = AccountId::from_str(format!("{}.testnet", id.as_str()).as_str())
.map_err(|err| ErrorKind::DataConversion.custom(err))?;
}
let url = Url::parse(HELPER_URL).unwrap();
tool::url_create_account(url, id.clone(), sk.public_key()).await?;
let signer = InMemorySigner::from_secret_key(id, sk);
Expand Down
29 changes: 28 additions & 1 deletion workspaces/src/rpc/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) async fn url_create_account(
let helper_url = helper_url.join("account").unwrap();

// TODO(maybe): need this in near-jsonrpc-client as well:
let _resp = reqwest::Client::new()
let resp = reqwest::Client::new()
.post(helper_url)
.header("Content-Type", "application/json")
.body(
Expand All @@ -57,6 +57,33 @@ pub(crate) async fn url_create_account(
.await
.map_err(|e| RpcErrorCode::HelperAccountCreationFailure.custom(e))?;

if resp.status() >= reqwest::StatusCode::BAD_REQUEST {
return Err(ErrorKind::Other.message(format!(
"The faucet (helper service) server failed with status code <{}>",
resp.status()
)));
}

let account_creation_transaction = resp
.json::<near_jsonrpc_client::methods::tx::RpcTransactionStatusResponse>()
.await
.map_err(|err| ErrorKind::DataConversion.custom(err))?;

match account_creation_transaction.status {
near_primitives::views::FinalExecutionStatus::SuccessValue(ref value) => {
if value == b"false" {
return Err(ErrorKind::Other.message(format!(
"The new account <{}> could not be created successfully.",
&account_id
)));
}
}
near_primitives::views::FinalExecutionStatus::Failure(err) => {
return Err(ErrorKind::Execution.custom(err));
}
_ => unreachable!(),
}

Ok(())
}

Expand Down
24 changes: 24 additions & 0 deletions workspaces/tests/account.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
#![recursion_limit = "256"]
use near_sdk::AccountId;
use near_token::NearToken;
use near_workspaces::error::ErrorKind;
use near_workspaces::network::{Sandbox, Testnet};
use near_workspaces::Worker;
use serde_json::{Map, Value};
use test_log::test;

use std::fs::File;
use std::path::Path;
use std::str::FromStr;

#[test(tokio::test)]
async fn test_dev_account_creation() -> anyhow::Result<()> {
let worker: Worker<Sandbox> = near_workspaces::sandbox().await?;
worker.dev_create_account().await?;

let worker: Worker<Testnet> = near_workspaces::testnet().await?;
let (id, sk) = worker.dev_generate().await;
worker.create_tla(id, sk).await?.into_result()?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should not pass. TLA stands for top-level account (account name without ., e.g. near, testnet). create_tla on testnet cannot succeed.


worker.dev_create_account().await?;

let (id, sk) = worker.dev_generate().await;
let id = AccountId::from_str(format!("{}.testnet", id.as_str()).as_str())?;
let err = worker.create_tla(id, sk).await;
assert!(err.is_err_and(|e| e.kind() == &ErrorKind::Execution));

Ok(())
}

#[test(tokio::test)]
async fn test_subaccount_creation() -> anyhow::Result<()> {
Expand Down
Loading