-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9014f3a
commit 404ff36
Showing
10 changed files
with
272 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Utilizing custom sandbox node | ||
|
||
This example will show us how to spin up a sandbox node of our own choosing. Follow the guide in https://github.com/near/sandbox to download it. This is mainly needed if a user wants to manage their own node and/or not require each test to spin up a new node each time. | ||
|
||
Then initialize the chain via `init` and run it: | ||
|
||
```sh | ||
near-sandbox --home ${MY_HOME_DIRECTORY} init | ||
near-sandbox --home ${MY_HOME_DIRECTORY} run | ||
``` | ||
|
||
This will launch the chain onto `localhost:3030` by default. The `${MY_HOME_DIRECTORY}` is a path of our choosing here and this will be needed when running the workspaces code later on. In the following example, we had it set to `/home/user/.near-sandbox-home`. | ||
|
||
In workspaces, to connect to our manually launched node, all we have to do is add a few additional parameters to `workspaces::sandbox()`: | ||
|
||
```rs | ||
#[tokio::main] | ||
fn main() { | ||
let worker = workspaces::sandbox() | ||
.rpc_addr("http://localhost:3030") | ||
.home_dir("/home/user/.near-sandbox-home") | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
``` | ||
|
||
Then afterwards, we can continue performing our tests as we normally would if workspaces has spawned its own sandbox process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::future::{Future, IntoFuture}; | ||
use std::marker::PhantomData; | ||
use std::path::PathBuf; | ||
|
||
use crate::network::Sandbox; | ||
use crate::{Network, Worker}; | ||
|
||
pub(crate) type BoxFuture<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
|
||
/// This trait provides a way to construct Networks out of a single builder. Currently | ||
/// not planned to offer this trait outside, since the custom networks can just construct | ||
/// themselves however they want utilizing `Worker::new` like so: | ||
/// ```ignore | ||
/// Worker::new(CustomNetwork { | ||
/// ... // fields | ||
/// }) | ||
/// ``` | ||
#[async_trait::async_trait] | ||
pub(crate) trait FromNetworkBuilder: Sized { | ||
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> crate::result::Result<Self>; | ||
} | ||
|
||
/// Builder for Networks. Only usable with workspaces provided Networks. | ||
// Note, this is currently the aggregated state for all network types you can have since | ||
// I didn't want to add additional reading complexity with another trait that associates the | ||
// Network state. | ||
pub struct NetworkBuilder<'a, T> { | ||
pub(crate) name: &'a str, | ||
pub(crate) rpc_addr: Option<String>, | ||
pub(crate) home_dir: Option<PathBuf>, | ||
_network: PhantomData<T>, | ||
} | ||
|
||
impl<'a, T> IntoFuture for NetworkBuilder<'a, T> | ||
where | ||
T: FromNetworkBuilder + Network + Send + 'a, | ||
{ | ||
type Output = crate::result::Result<Worker<T>>; | ||
type IntoFuture = BoxFuture<'a, Self::Output>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
let fut = async { | ||
let network = FromNetworkBuilder::from_builder(self).await?; | ||
Ok(Worker::new(network)) | ||
}; | ||
Box::pin(fut) | ||
} | ||
} | ||
|
||
impl<'a, T> NetworkBuilder<'a, T> { | ||
pub(crate) fn new(name: &'a str) -> Self { | ||
Self { | ||
name, | ||
rpc_addr: None, | ||
home_dir: None, | ||
_network: PhantomData, | ||
} | ||
} | ||
|
||
/// Sets the RPC addr for this network. Useful for setting the Url to a different RPC | ||
/// node than the default one provided by near.org. This enables certain features that | ||
/// the default node doesn't provide such as getting beyond the data cap when downloading | ||
/// state from the network. | ||
/// Example: | ||
/// ``` | ||
/// async fn connect_custom() -> anyhow::Result<()> { | ||
/// let worker = workspaces::testnet() | ||
/// .rpc_addr("https://rpc.testnet.near.org") | ||
/// .await?; | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
/// | ||
/// Note that, for sandbox, we also are required to specify `home_dir` | ||
pub fn rpc_addr(mut self, addr: &str) -> Self { | ||
self.rpc_addr = Some(addr.into()); | ||
self | ||
} | ||
} | ||
|
||
// So far, only Sandbox makes use of home_dir. | ||
impl NetworkBuilder<'_, Sandbox> { | ||
/// Specify at which location the home_dir of the manually spawned sandbox node is at. | ||
/// We are expected to init our own sandbox before running this builder. To learn more | ||
/// about initalizing and starting our own sandbox, go to [near-sandbox](https://github.com/near/sandbox). | ||
/// Also required to set the home directory where all the chain data lives. This is | ||
/// the `my_home_folder` we passed into `near-sandbox --home {my_home_folder} init`. | ||
pub fn home_dir(mut self, home_dir: impl AsRef<std::path::Path>) -> Self { | ||
self.home_dir = Some(home_dir.as_ref().into()); | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.