Skip to content

Commit

Permalink
Add sandbox_with_version
Browse files Browse the repository at this point in the history
  • Loading branch information
iho committed Sep 25, 2023
1 parent 027c98a commit b633688
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
5 changes: 3 additions & 2 deletions workspaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use types::block::Block;
pub use types::chunk::Chunk;
pub use types::{AccessKey, AccountId, BlockHeight, CryptoHash, InMemorySigner};
pub use worker::{
betanet, mainnet, mainnet_archival, sandbox, testnet, testnet_archival, with_betanet,
with_mainnet, with_mainnet_archival, with_sandbox, with_testnet, with_testnet_archival, Worker,
betanet, mainnet, mainnet_archival, sandbox, sandbox_with_version, testnet, testnet_archival,
with_betanet, with_mainnet, with_mainnet_archival, with_sandbox, with_testnet,
with_testnet_archival, Worker,
};
43 changes: 26 additions & 17 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Sandbox {
pub(crate) server: SandboxServer,
client: Client,
info: Info,
version: Option<String>,
}

impl Sandbox {
Expand All @@ -46,22 +47,10 @@ impl Sandbox {
)),
}
}
}

impl std::fmt::Debug for Sandbox {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Sandbox")
.field("root_id", &self.info.root_id)
.field("rpc_url", &self.info.rpc_url)
.field("rpc_port", &self.server.rpc_port())
.field("net_port", &self.server.net_port())
.finish()
}
}

#[async_trait]
impl FromNetworkBuilder for Sandbox {
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result<Self> {
pub(crate) async fn from_builder_with_version<'a>(
build: NetworkBuilder<'a, Self>,
version: &str,
) -> Result<Self> {
// Check the conditions of the provided rpc_url and validator_key
let mut server = match (build.rpc_addr, build.validator_key) {
// Connect to a provided sandbox:
Expand All @@ -70,7 +59,7 @@ impl FromNetworkBuilder for Sandbox {
}

// Spawn a new sandbox since rpc_url and home_dir weren't specified:
(None, None) => SandboxServer::run_new().await?,
(None, None) => SandboxServer::run_new_with_version(version).await?,

// Missing inputted parameters for sandbox:
(Some(rpc_url), None) => {
Expand Down Expand Up @@ -105,10 +94,30 @@ impl FromNetworkBuilder for Sandbox {
server,
client,
info,
version: Some(version.to_string()),
})
}
}

impl std::fmt::Debug for Sandbox {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Sandbox")
.field("root_id", &self.info.root_id)
.field("rpc_url", &self.info.rpc_url)
.field("rpc_port", &self.server.rpc_port())
.field("net_port", &self.server.net_port())
.field("version", &self.version)
.finish()
}
}

#[async_trait]
impl FromNetworkBuilder for Sandbox {
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result<Self> {
Self::from_builder_with_version(build, crate::version::NEAR_SANDBOX_VERSION).await
}
}

impl AllowDevAccountCreation for Sandbox {}

#[async_trait]
Expand Down
16 changes: 13 additions & 3 deletions workspaces/src/network/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ async fn acquire_unused_port() -> Result<(u16, File)> {
}
}

#[allow(dead_code)]
async fn init_home_dir() -> Result<TempDir> {
init_home_dir_with_version(crate::version::NEAR_SANDBOX_VERSION).await
}

async fn init_home_dir_with_version(version: &str) -> Result<TempDir> {
let home_dir = tempfile::tempdir().map_err(|e| ErrorKind::Io.custom(e))?;
let output = sandbox::init_with_version(&home_dir, crate::version::NEAR_SANDBOX_VERSION)
let output = sandbox::init_with_version(&home_dir, version)
.map_err(|e| SandboxErrorCode::InitFailure.custom(e))?
.output()
.await
Expand Down Expand Up @@ -101,11 +106,16 @@ impl SandboxServer {
}

/// Run a new SandboxServer, spawning the sandbox node in the process.
#[allow(dead_code)]
pub(crate) async fn run_new() -> Result<Self> {
Self::run_new_with_version(crate::version::NEAR_SANDBOX_VERSION).await
}

pub(crate) async fn run_new_with_version(version: &str) -> Result<Self> {
// Suppress logs for the sandbox binary by default:
suppress_sandbox_logs_if_required();

let home_dir = init_home_dir().await?.into_path();
let home_dir = init_home_dir_with_version(version).await?.into_path();
// Configure `$home_dir/config.json` to our liking. Sandbox requires extra settings
// for the best user experience, and being able to offer patching large state payloads.
crate::network::config::set_sandbox_configs(&home_dir)?;
Expand Down Expand Up @@ -133,7 +143,7 @@ impl SandboxServer {
&net_addr,
];

let child = sandbox::run_with_options(options)
let child = sandbox::run_with_options_with_version(options, version)
.map_err(|e| SandboxErrorCode::RunFailure.custom(e))?;

info!(target: "workspaces", "Started up sandbox at localhost:{} with pid={:?}", rpc_port, child.id());
Expand Down
9 changes: 8 additions & 1 deletion workspaces/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use crate::network::builder::NetworkBuilder;
use crate::network::{Betanet, Mainnet, Sandbox, Testnet};
use crate::{Network, Result};
use crate::{version, Network, Result};

/// The `Worker` type allows us to interact with any NEAR related networks, such
/// as mainnet and testnet. This controls where the environment the worker is
Expand Down Expand Up @@ -47,6 +47,13 @@ pub fn sandbox<'a>() -> NetworkBuilder<'a, Sandbox> {
NetworkBuilder::new("sandbox")
}

/// Spin up a new sandbox instance, and grab a [`Worker`] that interacts with it.
pub async fn sandbox_with_version<'a>(version: &str) -> Result<Worker<Sandbox>> {
let network_builder = NetworkBuilder::new("sandbox");
let network = Sandbox::from_builder_with_version(network_builder, version).await?;
Ok(Worker::new(network))
}

/// Connect to the [testnet](https://explorer.testnet.near.org/) network, and grab
/// a [`Worker`] that can interact with it.
pub fn testnet<'a>() -> NetworkBuilder<'a, Testnet> {
Expand Down

0 comments on commit b633688

Please sign in to comment.