Skip to content

Commit

Permalink
feat-issues-354: dev_deploy after 1.37.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Jun 7, 2024
1 parent 97d6449 commit 7646b15
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
4 changes: 2 additions & 2 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 11 additions & 3 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -123,10 +123,14 @@ impl TopLevelAccountCreator for Sandbox {
async fn create_tla(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
sk: SecretKey,
) -> Result<Execution<Account>> {
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)
Expand All @@ -142,11 +146,15 @@ impl TopLevelAccountCreator for Sandbox {
async fn create_tla_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
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(
Expand Down
15 changes: 12 additions & 3 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryInto;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -71,11 +72,15 @@ impl TopLevelAccountCreator for Testnet {
async fn create_tla(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
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 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);

Expand Down Expand Up @@ -107,12 +112,16 @@ impl TopLevelAccountCreator for Testnet {
async fn create_tla_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
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?;

Expand Down
32 changes: 19 additions & 13 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,14 +20,14 @@ pub trait TopLevelAccountCreator {
async fn create_tla(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
sk: SecretKey,
) -> Result<Execution<Account>>;

async fn create_tla_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
account_prefix: String,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>>;
Expand All @@ -41,10 +41,14 @@ impl<T> Worker<T>
where
T: DevNetwork + TopLevelAccountCreator + 'static,
{
pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result<Execution<Account>> {
pub async fn create_tla(
&self,
account_prefix: String,
sk: SecretKey,
) -> Result<Execution<Account>> {
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() {
Expand All @@ -56,13 +60,13 @@ where

pub async fn create_tla_and_deploy(
&self,
id: AccountId,
account_prefix: String,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
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() {
Expand All @@ -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<Account> {
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<Contract> {
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()?)
}
}
Expand Down
6 changes: 1 addition & 5 deletions workspaces/src/rpc/tool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::File;
use std::io::Write;
use std::path::Path;
Expand All @@ -24,13 +23,10 @@ pub(crate) fn into_state_map(state_items: Vec<StateItem>) -> HashMap<Vec<u8>, 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
}
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 @@ -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"),
)
Expand Down Expand Up @@ -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"),
)
Expand Down Expand Up @@ -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"),
)
Expand Down Expand Up @@ -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"),
)
Expand Down Expand Up @@ -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"),
)
Expand Down

0 comments on commit 7646b15

Please sign in to comment.