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

Pin specific version of near-sandbox #311

Merged
merged 9 commits into from
Oct 4, 2023
Merged
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
5 changes: 3 additions & 2 deletions workspaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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,
};
44 changes: 27 additions & 17 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;
use near_jsonrpc_client::methods::sandbox_fast_forward::RpcSandboxFastForwardRequest;
use near_jsonrpc_client::methods::sandbox_patch_state::RpcSandboxPatchStateRequest;
use near_primitives::state_record::StateRecord;
use near_sandbox_utils as sandbox;

use super::builder::{FromNetworkBuilder, NetworkBuilder};
use super::server::ValidatorKey;
Expand All @@ -31,6 +32,7 @@ pub struct Sandbox {
pub(crate) server: SandboxServer,
client: Client,
info: Info,
version: Option<String>,
}

impl Sandbox {
Expand All @@ -46,22 +48,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 +60,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 +95,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, sandbox::DEFAULT_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 @@ -56,10 +56,15 @@ async fn acquire_unused_port() -> Result<(u16, File)> {
}
}

#[allow(dead_code)]
async fn init_home_dir() -> Result<TempDir> {
init_home_dir_with_version(sandbox::DEFAULT_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(&home_dir)
let output = sandbox::init_with_version(&home_dir, version)
.map_err(|e| SandboxErrorCode::InitFailure.custom(e))?
.wait_with_output()
.await
Expand Down Expand Up @@ -104,11 +109,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(sandbox::DEFAULT_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 @@ -136,7 +146,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
7 changes: 7 additions & 0 deletions workspaces/src/worker/mod.rs
Original file line number Diff line number Diff line change
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
Loading