Skip to content

Commit

Permalink
feat(gnode): introduce node wrapper (#4003)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Jun 12, 2024
1 parent 5d3db1f commit 1799873
Show file tree
Hide file tree
Showing 24 changed files with 342 additions and 228 deletions.
18 changes: 15 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ gbuiltin-bls381 = { path = "gbuiltins/bls381", default-features = false }
gcore = { path = "gcore" }
gcli = { path = "gcli" }
gclient = { path = "gclient" }
gnode = { path = "utils/gnode" }
gsdk = { path = "gsdk" }
gsdk-codegen = { path = "gsdk/codegen" }
gstd = { path = "gstd", features = ["nightly"] }
Expand Down Expand Up @@ -484,6 +485,7 @@ scale-decode = "0.9.0" # gsdk
directories = "5.0.1" # utils/key-finder
num-traits = { version = "0.2", default-features = false } # gear-core
glob = "0.3.1" # cargo-gbuild
smallvec = "1.13.2" # utils/gnode

# TODO: remove after wasmer bug is fixed:
# `misaligned pointer dereference: address must be a multiple of 0x8 but is...`
Expand Down
2 changes: 1 addition & 1 deletion gcli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ rand.workspace = true
demo-messenger.workspace = true
demo-new-meta.workspace = true
demo-waiter.workspace = true
gsdk = { workspace = true, features = ["testing"] }
gnode.workspace = true

[build-dependencies]
which.workspace = true
Expand Down
6 changes: 1 addition & 5 deletions gcli/tests/common/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::common::{
self, env,
node::{Convert, NodeExec},
Args,
};
use crate::common::{self, env, node::Convert, Args};
use anyhow::{anyhow, Context, Result};
use std::{
path::PathBuf,
Expand Down
28 changes: 8 additions & 20 deletions gcli/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
//! Common utils for integration tests
pub use self::{args::Args, node::NodeExec, result::Result};
use gear_core::ids::{prelude::*, CodeId, ProgramId};
use gsdk::{
ext::{sp_core::crypto::Ss58Codec, sp_runtime::AccountId32},
testing::Node,
};
use gnode::{Node, NodeInstance};
use gsdk::ext::{sp_core::crypto::Ss58Codec, sp_runtime::AccountId32};
use std::{
iter::IntoIterator,
process::{Command, Output},
Expand All @@ -37,11 +35,7 @@ mod result;
pub const ALICE_SS58_ADDRESS: &str = "kGkLEU3e3XXkJp2WK4eNpVmSab5xUNL9QtmLPh8QfCL2EgotW";
pub const RENT_POOL_SS58_ADDRESS: &str = "kGkkENXuYL4Xw6H1ymWm6VwHLi66s56Ywt45pf9hEx1hmx5MV";

impl NodeExec for Node {
fn ws(&self) -> String {
"ws://".to_string() + &self.address().to_string()
}

impl NodeExec for NodeInstance {
/// Run binary `gcli`
fn run(&self, args: Args) -> Result<Output> {
gcli(Vec::<String>::from(args.endpoint(self.ws())))
Expand All @@ -61,17 +55,11 @@ pub fn gcli<T: ToString>(args: impl IntoIterator<Item = T>) -> Result<Output> {
}

/// Run the dev node
pub fn dev() -> Result<Node> {
pub fn dev() -> Result<NodeInstance> {
login_as_alice()?;

let args = vec!["--tmp", "--dev"];
let mut node = Node::try_from_path(env::node_bin(), args)?;

// TODO: use [`Node::wait_while_initialized`] instead,
// it currently presents infinite loop even after capturing
// the specified log #3304.
node.wait_for_log_record("Imported #1")?;
Ok(node)
Node::from_path(env::node_bin())?
.spawn()
.map_err(Into::into)
}

/// Init env logger
Expand All @@ -98,7 +86,7 @@ pub fn alice_account_id() -> AccountId32 {
}

/// Create program messager
pub async fn create_messager() -> Result<Node> {
pub async fn create_messager() -> Result<NodeInstance> {
let node = dev()?;

let args = Args::new("upload").program(env::wasm_bin("demo_messenger.opt.wasm"));
Expand Down
6 changes: 0 additions & 6 deletions gcli/tests/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,4 @@ pub trait NodeExec {

Ok(output.stdout.convert())
}

/// Formats websocket address to string.
///
/// This interface is used for constructing the `endpoint`
/// argument of gcli as the command line input.
fn ws(&self) -> String;
}
2 changes: 0 additions & 2 deletions gcli/tests/common/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub enum Error {
Hex(#[from] hex::FromHexError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Node(#[from] gsdk::testing::Error),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
1 change: 1 addition & 0 deletions gclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
[dependencies]
gear-utils.workspace = true
gsdk = { workspace = true, features = ["testing"] }
gnode.workspace = true
gear-core.workspace = true
gear-core-errors.workspace = true

Expand Down
5 changes: 1 addition & 4 deletions gclient/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use anyhow::Error as AError;
use gsdk::{testing::Error as NodeError, Error as GearSDKError};
use gsdk::Error as GearSDKError;
use std::{io::Error as IOError, result::Result as StdResult};
use subxt::error::{DispatchError, Error as SubxtError};

Expand Down Expand Up @@ -86,9 +86,6 @@ pub enum Error {
/// Occurs when being migrated program already exists in destination node.
#[error("Program {0} already exists")]
ProgramAlreadyExists(String),
/// Occurs when node spawning failed.
#[error(transparent)]
Node(#[from] NodeError),
/// Occurs when parsing websocket domain failed.
#[error("Failed to parse WebSocket domain.")]
WSDomainInvalid,
Expand Down
15 changes: 8 additions & 7 deletions gclient/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ pub mod storage;

use crate::{ws::WSAddress, EventListener};
use error::*;
use gnode::{Node, NodeInstance};
use gsdk::{
ext::{sp_core::sr25519, sp_runtime::AccountId32},
signer::Signer,
testing::Node,
Api,
};
use std::{ffi::OsStr, sync::Arc};

/// The API instance contains methods to access the node.
#[derive(Clone)]
pub struct GearApi(Signer, Option<Arc<Node>>);
pub struct GearApi(Signer, Option<Arc<NodeInstance>>);

impl GearApi {
/// Create and init a new `GearApi` specified by its `address` on behalf of
Expand Down Expand Up @@ -85,8 +85,8 @@ impl GearApi {
/// random port number. The node process uses a binary specified by the
/// `path` param. Ideally, the binary should be downloaded by means of CI pipeline from <https://get.gear.rs>.
pub async fn dev_from_path(path: impl AsRef<OsStr>) -> Result<Self> {
let node = Node::try_from_path(path, vec!["--dev"])?;
let api = Self::init(node.address().into()).await?;
let node = Node::from_path(path.as_ref())?.spawn()?;
let api = Self::init(node.address.into()).await?;
Ok(Self(api.0, Some(Arc::new(node))))
}

Expand All @@ -96,8 +96,8 @@ impl GearApi {
/// process uses a binary specified by the `path` param. Ideally, the
/// binary should be downloaded by means of CI pipeline from <https://get.gear.rs>.
pub async fn vara_dev_from_path(path: impl AsRef<OsStr>) -> Result<Self> {
let node = Node::try_from_path(path, vec!["--chain=vara-dev", "--validator", "--tmp"])?;
let api = Self::init(node.address().into()).await?;
let node = Node::from_path(path.as_ref())?.arg("--validator").spawn()?;
let api = Self::init(node.address.into()).await?;
Ok(Self(api.0, Some(Arc::new(node))))
}

Expand All @@ -106,7 +106,8 @@ impl GearApi {
if let Some(node) = self.1.as_mut() {
Arc::get_mut(node)
.expect("Unable to mutate `Node`")
.print_logs();
.logs()
.expect("Unable to read logs");
}
}

Expand Down
2 changes: 1 addition & 1 deletion gsdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ parity-scale-codec.workspace = true
rand = { workspace = true, optional = true }

[dev-dependencies]
gsdk = { path = ".", features = ["testing"] }
gnode.workspace = true
tokio = { workspace = true, features = [ "full" ] }
demo-messenger.workspace = true
demo-new-meta.workspace = true
Expand Down
1 change: 0 additions & 1 deletion gsdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ mod rpc;
pub mod signer;
mod storage;
pub mod subscription;
pub mod testing;
mod utils;

pub mod ext {
Expand Down
107 changes: 0 additions & 107 deletions gsdk/src/testing/node.rs

This file was deleted.

Loading

0 comments on commit 1799873

Please sign in to comment.