mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-12-02 23:37:06 +00:00
Fix folder names in client and test (#4360)
* client/rpc/api -> client/rpc-api * client/util/wasm-builder-runner -> utils/wasm-builder-runner * client/grafana-data-source -> utils/grafana-data-source * test/utils -> test-utils * fix moved path * Update Cargo.lock * Update Cargo.lock
This commit is contained in:
1 parent
8131dc8a66
commit
c5a709a882
75 files changed
+166
-185
No files matched your search
@@ -35,6 +35,6 @@ tracing = "0.1.10"
|
||||
[dev-dependencies]
|
||||
env_logger = "0.7.0"
|
||||
tempfile = "3.1.0"
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../test-utils/runtime/client" }
|
||||
kvdb-memorydb = "0.1.2"
|
||||
panic-handler = { package = "sp-panic-handler", path = "../primitives/panic-handler" }
|
||||
@@ -22,5 +22,5 @@ tokio-executor = { version = "0.2.0-alpha.6", features = ["blocking"] }
|
||||
|
||||
[dev-dependencies]
|
||||
txpool = { package = "sc-transaction-pool", path = "../../client/transaction-pool" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client" }
|
||||
parking_lot = "0.9"
|
||||
@@ -37,7 +37,7 @@ sc-executor = { path = "../../executor" }
|
||||
sc-network = { path = "../../network" }
|
||||
sc-network-test = { path = "../../network/test" }
|
||||
service = { package = "sc-service", path = "../../service" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test-utils/runtime/client" }
|
||||
tokio = "0.1.22"
|
||||
env_logger = "0.7.0"
|
||||
tempfile = "3.1.0"
|
||||
@@ -46,7 +46,7 @@ sc-executor = { path = "../../executor" }
|
||||
sc-network = { path = "../../network" }
|
||||
sc-network-test = { path = "../../network/test" }
|
||||
service = { package = "sc-service", path = "../../service" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test-utils/runtime/client" }
|
||||
block-builder = { package = "sc-block-builder", path = "../../block-builder" }
|
||||
tokio = "0.1.22"
|
||||
env_logger = "0.7.0"
|
||||
|
||||
@@ -21,4 +21,4 @@ parking_lot = "0.9.0"
|
||||
log = "0.4.8"
|
||||
|
||||
[dev-dependencies]
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test-utils/runtime/client" }
|
||||
@@ -26,7 +26,7 @@ sp-blockchain = { path = "../../primitives/blockchain" }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-keyring = { path = "../../primitives/keyring" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client" }
|
||||
env_logger = "0.7.0"
|
||||
quickcheck = "0.9"
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ assert_matches = "1.3.0"
|
||||
wabt = "0.9.2"
|
||||
hex-literal = "0.2.1"
|
||||
runtime-test = { package = "sc-runtime-test", path = "runtime-test" }
|
||||
test-runtime = { package = "substrate-test-runtime", path = "../../test/utils/runtime" }
|
||||
test-runtime = { package = "substrate-test-runtime", path = "../../test-utils/runtime" }
|
||||
runtime-interface = { package = "sp-runtime-interface", path = "../../primitives/runtime-interface" }
|
||||
state_machine = { package = "sp-state-machine", path = "../../primitives/state-machine" }
|
||||
test-case = "0.3.3"
|
||||
|
||||
@@ -13,7 +13,7 @@ primitives = { package = "sp-core", path = "../../../primitives/core", default-
|
||||
sp-runtime = { package = "sp-runtime", path = "../../../primitives/runtime", default-features = false }
|
||||
|
||||
[build-dependencies]
|
||||
wasm-builder-runner = { package = "substrate-wasm-builder-runner", path = "../../../client/utils/wasm-builder-runner", version = "1.0.4" }
|
||||
wasm-builder-runner = { package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner", version = "1.0.4" }
|
||||
|
||||
[features]
|
||||
default = [ "std" ]
|
||||
|
||||
@@ -34,7 +34,7 @@ grandpa = { package = "finality-grandpa", version = "0.10.1", features = ["deriv
|
||||
network = { package = "sc-network", path = "../network" }
|
||||
sc-network-test = { path = "../network/test" }
|
||||
keyring = { package = "sp-keyring", path = "../../primitives/keyring" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client"}
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client"}
|
||||
babe_primitives = { package = "sp-consensus-babe", path = "../../primitives/consensus/babe" }
|
||||
state_machine = { package = "sp-state-machine", path = "../../primitives/state-machine" }
|
||||
env_logger = "0.7.0"
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
[package]
|
||||
description = "Grafana data source server"
|
||||
name = "grafana-data-source"
|
||||
version = "2.0.0"
|
||||
license = "GPL-3.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.8"
|
||||
hyper = { version = "0.13.0-alpha.4", default-features = false, features = ["unstable-stream"] }
|
||||
tokio-io = "0.2.0-alpha.6"
|
||||
tokio-executor = "0.2.0-alpha.6"
|
||||
futures-util = { version = "0.3.1", default-features = false, features = ["io"] }
|
||||
futures-util-alpha = { package = "futures-util-preview", default-features = false, version = "0.3.0-alpha.19" }
|
||||
serde_json = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
lazy_static = "1.4"
|
||||
parking_lot = "0.9"
|
||||
futures-timer = "2.0"
|
||||
derive_more = "0.99"
|
||||
|
||||
[target.'cfg(not(target_os = "unknown"))'.dependencies]
|
||||
async-std = { version = "1.0.1", features = ["unstable"] }
|
||||
@@ -1,170 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use crate::Error;
|
||||
|
||||
pub struct Database {
|
||||
base_timestamp: i64,
|
||||
storage: HashMap<String, Vec<Datapoint>>
|
||||
}
|
||||
|
||||
impl Database {
|
||||
/// Create a new Database.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base_timestamp: now_millis(),
|
||||
storage: HashMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce an iterator for keys starting with a base string.
|
||||
pub fn keys_starting_with<'a>(&'a self, base: &'a str) -> impl Iterator<Item = String> + 'a {
|
||||
self.storage.keys()
|
||||
.filter(move |key| key.starts_with(base))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Select `max_datapoints` datapoints that have been added between `from` and `to`.
|
||||
pub fn datapoints_between(&self, key: &str, from: i64, to: i64, max_datapoints: usize) -> Option<Vec<(f32, i64)>> {
|
||||
self.storage.get(key)
|
||||
.map(|vec| {
|
||||
let from = find_index(vec, self.base_timestamp, from);
|
||||
let to = find_index(vec, self.base_timestamp, to);
|
||||
let slice = &vec[from .. to];
|
||||
|
||||
if max_datapoints == 0 {
|
||||
Vec::new()
|
||||
} else if max_datapoints >= slice.len() {
|
||||
// Just convert the slice as-is
|
||||
slice.iter()
|
||||
.map(|dp| dp.make_absolute(self.base_timestamp))
|
||||
.collect()
|
||||
} else {
|
||||
// We have more datapoints than we need, so we need to skip some
|
||||
(0 .. max_datapoints - 1)
|
||||
.map(|i| &slice[i * slice.len() / (max_datapoints - 1)])
|
||||
.chain(slice.last())
|
||||
.map(|dp| dp.make_absolute(self.base_timestamp))
|
||||
.collect()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Push a new datapoint. Will error if the base timestamp hasn't been updated in `2^32`
|
||||
/// milliseconds (49 days).
|
||||
pub fn push(&mut self, key: &str, value: f32) -> Result<(), Error> {
|
||||
self.storage.entry(key.into())
|
||||
.or_insert_with(Vec::new)
|
||||
.push(Datapoint::new(self.base_timestamp, value)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set a new base timestamp, and remove metrics older than this new timestamp. Errors if the
|
||||
/// difference between timestamps is greater than `2^32` milliseconds (49 days).
|
||||
pub fn truncate(&mut self, new_base_timestamp: i64) -> Result<(), Error> {
|
||||
// Ensure that the new base is older.
|
||||
if self.base_timestamp >= new_base_timestamp {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If the old base timestamp was too long ago, the
|
||||
let delta = u32::try_from(new_base_timestamp - self.base_timestamp)
|
||||
.map_err(Error::Timestamp)?;
|
||||
|
||||
for metric in self.storage.values_mut() {
|
||||
// Find the index of the oldest allowed timestamp and cut out all those before it.
|
||||
let index = find_index(&metric, self.base_timestamp, new_base_timestamp);
|
||||
|
||||
*metric = metric.iter_mut()
|
||||
.skip(index)
|
||||
.map(|dp| {
|
||||
dp.delta_timestamp -= delta;
|
||||
*dp
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
self.base_timestamp = new_base_timestamp;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Datapoint {
|
||||
delta_timestamp: u32,
|
||||
value: f32
|
||||
}
|
||||
|
||||
impl Datapoint {
|
||||
fn new(base_timestamp: i64, value: f32) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
delta_timestamp: u32::try_from(now_millis() - base_timestamp)
|
||||
.map_err(Error::Timestamp)?,
|
||||
value
|
||||
})
|
||||
}
|
||||
|
||||
fn make_absolute(&self, base_timestamp: i64) -> (f32, i64) {
|
||||
(self.value, base_timestamp + self.delta_timestamp as i64)
|
||||
}
|
||||
}
|
||||
|
||||
fn find_index(slice: &[Datapoint], base_timestamp: i64, timestamp: i64) -> usize {
|
||||
slice.binary_search_by_key(×tamp, |datapoint| {
|
||||
base_timestamp + datapoint.delta_timestamp as i64
|
||||
}).unwrap_or_else(|index| index)
|
||||
}
|
||||
|
||||
/// Get the current unix timestamp in milliseconds.
|
||||
fn now_millis() -> i64 {
|
||||
chrono::Utc::now().timestamp_millis()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let mut database = Database::new();
|
||||
let start = now_millis();
|
||||
|
||||
database.push("test", 1.0).unwrap();
|
||||
database.push("test", 2.5).unwrap();
|
||||
database.push("test", 2.0).unwrap();
|
||||
database.push("test 2", 1.0).unwrap();
|
||||
|
||||
let mut keys: Vec<_> = database.keys_starting_with("test").collect();
|
||||
keys.sort();
|
||||
|
||||
assert_eq!(keys, ["test", "test 2"]);
|
||||
assert_eq!(database.keys_starting_with("test ").collect::<Vec<_>>(), ["test 2"]);
|
||||
|
||||
assert_eq!(
|
||||
database.datapoints_between("test", start - 1000, start + 1000, 4),
|
||||
Some(vec![(1.0, start), (2.5, start), (2.0, start)])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
database.datapoints_between("test", start - 1000, start + 1000, 3),
|
||||
Some(vec![(1.0, start), (2.5, start), (2.0, start)])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
database.datapoints_between("test", start - 1000, start + 1000, 2),
|
||||
Some(vec![(1.0, start), (2.0, start)])
|
||||
);
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! [Grafana] data source server
|
||||
//!
|
||||
//! To display node statistics with [Grafana], this module exposes a `run_server` function that
|
||||
//! starts up a HTTP server that conforms to the [`grafana-json-data-source`] API. The
|
||||
//! `record_metrics` macro can be used to pass metrics to this server.
|
||||
//!
|
||||
//! [Grafana]: https://grafana.com/
|
||||
//! [`grafana-json-data-source`]: https://github.com/simPod/grafana-json-datasource
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
mod types;
|
||||
mod server;
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
mod networking;
|
||||
mod database;
|
||||
|
||||
use database::Database;
|
||||
pub use server::run_server;
|
||||
use std::num::TryFromIntError;
|
||||
|
||||
lazy_static! {
|
||||
// The `RwLock` wrapping the metrics database.
|
||||
static ref DATABASE: RwLock<Database> = RwLock::new(Database::new());
|
||||
}
|
||||
|
||||
/// Write metrics to `METRICS`.
|
||||
#[macro_export]
|
||||
macro_rules! record_metrics(
|
||||
($($key:expr => $value:expr,)*) => {
|
||||
if cfg!(not(target_os = "unknown")) {
|
||||
$crate::record_metrics_slice(&[
|
||||
$( ($key, $value as f32), )*
|
||||
])
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/// Write metrics to `METRICS` as a slice. Intended to be only used via `record_metrics!`.
|
||||
pub fn record_metrics_slice(metrics: &[(&str, f32)]) -> Result<(), Error> {
|
||||
let mut database = crate::DATABASE.write();
|
||||
|
||||
for &(key, value) in metrics.iter() {
|
||||
database.push(key, value)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Error type that can be returned by either `record_metrics` or `run_server`.
|
||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||
pub enum Error {
|
||||
Hyper(hyper::Error),
|
||||
Serde(serde_json::Error),
|
||||
Http(hyper::http::Error),
|
||||
Timestamp(TryFromIntError),
|
||||
Io(std::io::Error)
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Error::Hyper(error) => Some(error),
|
||||
Error::Serde(error) => Some(error),
|
||||
Error::Http(error) => Some(error),
|
||||
Error::Timestamp(error) => Some(error),
|
||||
Error::Io(error) => Some(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use async_std::pin::Pin;
|
||||
use std::task::{Poll, Context};
|
||||
use futures_util::{stream::Stream, io::{AsyncRead, AsyncWrite}};
|
||||
|
||||
pub struct Incoming<'a>(pub async_std::net::Incoming<'a>);
|
||||
|
||||
impl hyper::server::accept::Accept for Incoming<'_> {
|
||||
type Conn = TcpStream;
|
||||
type Error = async_std::io::Error;
|
||||
|
||||
fn poll_accept(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
Pin::new(&mut Pin::into_inner(self).0)
|
||||
.poll_next(cx)
|
||||
.map(|opt| opt.map(|res| res.map(TcpStream)))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TcpStream(pub async_std::net::TcpStream);
|
||||
|
||||
impl tokio_io::AsyncRead for TcpStream {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context,
|
||||
buf: &mut [u8]
|
||||
) -> Poll<Result<usize, std::io::Error>> {
|
||||
Pin::new(&mut Pin::into_inner(self).0)
|
||||
.poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl tokio_io::AsyncWrite for TcpStream {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context,
|
||||
buf: &[u8]
|
||||
) -> Poll<Result<usize, std::io::Error>> {
|
||||
Pin::new(&mut Pin::into_inner(self).0)
|
||||
.poll_write(cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), std::io::Error>> {
|
||||
Pin::new(&mut Pin::into_inner(self).0)
|
||||
.poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), std::io::Error>> {
|
||||
Pin::new(&mut Pin::into_inner(self).0)
|
||||
.poll_close(cx)
|
||||
}
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
use hyper::{Body, Request, Response, header, service::{service_fn, make_service_fn}, Server};
|
||||
use chrono::{Duration, Utc};
|
||||
use futures_util::{FutureExt, future::{Future, select, Either}};
|
||||
use futures_timer::Delay;
|
||||
use crate::{DATABASE, Error, types::{Target, Query, TimeseriesData, Range}};
|
||||
|
||||
async fn api_response(req: Request<Body>) -> Result<Response<Body>, Error> {
|
||||
match req.uri().path() {
|
||||
"/search" => {
|
||||
map_request_to_response(req, |target: Target| {
|
||||
// Filter and return metrics relating to the target
|
||||
DATABASE.read()
|
||||
.keys_starting_with(&target.target)
|
||||
.collect::<Vec<_>>()
|
||||
}).await
|
||||
},
|
||||
"/query" => {
|
||||
map_request_to_response(req, |query: Query| {
|
||||
let metrics = DATABASE.read();
|
||||
|
||||
let Query {
|
||||
range: Range { from, to },
|
||||
max_datapoints, ..
|
||||
} = query;
|
||||
|
||||
// Return timeseries data related to the specified metrics
|
||||
query.targets.iter()
|
||||
.map(|target| {
|
||||
let datapoints = metrics.datapoints_between(&target.target, from, to, max_datapoints)
|
||||
.unwrap_or_else(Vec::new);
|
||||
|
||||
TimeseriesData {
|
||||
target: target.target.clone(), datapoints
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}).await
|
||||
},
|
||||
_ => Ok(Response::new(Body::empty())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn map_request_to_response<Req, Res, T>(req: Request<Body>, transformation: T) -> Result<Response<Body>, Error>
|
||||
where
|
||||
Req: DeserializeOwned,
|
||||
Res: Serialize,
|
||||
T: Fn(Req) -> Res + Send + Sync + 'static
|
||||
{
|
||||
use futures_util_alpha::TryStreamExt;
|
||||
|
||||
let body = req.into_body()
|
||||
.try_concat()
|
||||
.await
|
||||
.map_err(Error::Hyper)?;
|
||||
|
||||
let req = serde_json::from_slice(body.as_ref()).map_err(Error::Serde)?;
|
||||
let res = transformation(req);
|
||||
let string = serde_json::to_string(&res).map_err(Error::Serde)?;
|
||||
|
||||
Response::builder()
|
||||
.header(header::CONTENT_TYPE, "application/json")
|
||||
.body(Body::from(string))
|
||||
.map_err(Error::Http)
|
||||
}
|
||||
|
||||
/// Given that we're not using hyper's tokio feature, we need to define out own executor.
|
||||
#[derive(Clone)]
|
||||
pub struct Executor;
|
||||
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
impl<T> tokio_executor::TypedExecutor<T> for Executor
|
||||
where
|
||||
T: Future + Send + 'static,
|
||||
T::Output: Send + 'static,
|
||||
{
|
||||
fn spawn(&mut self, future: T) -> Result<(), tokio_executor::SpawnError> {
|
||||
async_std::task::spawn(future);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the data source server.
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
pub async fn run_server(mut address: std::net::SocketAddr) -> Result<(), Error> {
|
||||
use async_std::{net, io};
|
||||
use crate::networking::Incoming;
|
||||
|
||||
let listener = loop {
|
||||
let listener = net::TcpListener::bind(&address).await;
|
||||
match listener {
|
||||
Ok(listener) => {
|
||||
log::info!("Grafana data source server started at {}", address);
|
||||
break listener
|
||||
},
|
||||
Err(err) => match err.kind() {
|
||||
io::ErrorKind::AddrInUse | io::ErrorKind::PermissionDenied if address.port() != 0 => {
|
||||
log::warn!(
|
||||
"Unable to bind grafana data source server to {}. Trying random port.",
|
||||
address
|
||||
);
|
||||
address.set_port(0);
|
||||
continue;
|
||||
},
|
||||
_ => Err(err)?,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let service = make_service_fn(|_| {
|
||||
async {
|
||||
Ok::<_, Error>(service_fn(api_response))
|
||||
}
|
||||
});
|
||||
|
||||
let server = Server::builder(Incoming(listener.incoming()))
|
||||
.executor(Executor)
|
||||
.serve(service)
|
||||
.boxed();
|
||||
|
||||
let every = std::time::Duration::from_secs(24 * 3600);
|
||||
let clean = clean_up(every, Duration::weeks(1))
|
||||
.boxed();
|
||||
|
||||
let result = match select(server, clean).await {
|
||||
Either::Left((result, _)) => result.map_err(Into::into),
|
||||
Either::Right((result, _)) => result
|
||||
};
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(target_os = "unknown")]
|
||||
pub async fn run_server(_: std::net::SocketAddr) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Periodically remove old metrics.
|
||||
async fn clean_up(every: std::time::Duration, before: Duration) -> Result<(), Error> {
|
||||
loop {
|
||||
Delay::new(every).await;
|
||||
|
||||
let oldest_allowed = (Utc::now() - before).timestamp_millis();
|
||||
DATABASE.write().truncate(oldest_allowed)?;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Target {
|
||||
pub target: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Query {
|
||||
#[serde(rename = "maxDataPoints")]
|
||||
pub max_datapoints: usize,
|
||||
pub targets: Vec<Target>,
|
||||
pub range: Range,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Range {
|
||||
#[serde(deserialize_with = "date_to_timestamp_ms")]
|
||||
pub from: i64,
|
||||
#[serde(deserialize_with = "date_to_timestamp_ms")]
|
||||
pub to: i64,
|
||||
}
|
||||
|
||||
// Deserialize a timestamp via a `DateTime<Utc>`
|
||||
fn date_to_timestamp_ms<'de, D: serde::Deserializer<'de>>(timestamp: D) -> Result<i64, D::Error> {
|
||||
Deserialize::deserialize(timestamp)
|
||||
.map(|date: chrono::DateTime<chrono::Utc>| date.timestamp_millis())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct TimeseriesData {
|
||||
pub target: String,
|
||||
pub datapoints: Vec<(f32, i64)>
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
[package]
|
||||
description = "Grafana data source server test"
|
||||
name = "grafana-data-source-test"
|
||||
version = "2.0.0"
|
||||
license = "GPL-3.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
grafana-data-source = { path = ".." }
|
||||
futures = "0.3"
|
||||
futures-timer = "2.0"
|
||||
rand = "0.7"
|
||||
@@ -1,44 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use grafana_data_source::{run_server, record_metrics};
|
||||
use std::time::Duration;
|
||||
use rand::Rng;
|
||||
use futures::{future::join, executor};
|
||||
|
||||
async fn randomness() {
|
||||
loop {
|
||||
futures_timer::Delay::new(Duration::from_secs(1)).await;
|
||||
|
||||
let random = rand::thread_rng().gen_range(0.0, 1000.0);
|
||||
|
||||
let result = record_metrics!(
|
||||
"random data" => random,
|
||||
"random^2" => random * random,
|
||||
);
|
||||
|
||||
if let Err(error) = result {
|
||||
eprintln!("{}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
executor::block_on(join(
|
||||
run_server("127.0.0.1:9955".parse().unwrap()),
|
||||
randomness()
|
||||
)).0.unwrap();
|
||||
}
|
||||
@@ -43,8 +43,8 @@ tokio-io = "0.1.12"
|
||||
tokio = { version = "0.1.22", optional = true }
|
||||
unsigned-varint = { version = "0.2.2", features = ["codec"] }
|
||||
keyring = { package = "sp-keyring", path = "../../primitives/keyring", optional = true }
|
||||
test_client = { package = "substrate-test-client", path = "../../test/utils/client", optional = true }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client", optional = true }
|
||||
test_client = { package = "substrate-test-client", path = "../../test-utils/client", optional = true }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client", optional = true }
|
||||
erased-serde = "0.3.9"
|
||||
void = "1.0.2"
|
||||
zeroize = "1.0.0"
|
||||
|
||||
@@ -24,7 +24,7 @@ primitives = { package = "sp-core", path = "../../../primitives/core" }
|
||||
block-builder = { package = "sc-block-builder", path = "../../block-builder" }
|
||||
babe-primitives = { package = "sp-consensus-babe", path = "../../../primitives/consensus/babe" }
|
||||
env_logger = "0.7.0"
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test/utils/runtime/client" }
|
||||
test_runtime = { package = "substrate-test-runtime", path = "../../../test/utils/runtime" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../../test-utils/runtime/client" }
|
||||
test_runtime = { package = "substrate-test-runtime", path = "../../../test-utils/runtime" }
|
||||
tempfile = "3.1.0"
|
||||
tokio = "0.1.22"
|
||||
@@ -33,7 +33,7 @@ hyper-rustls = "0.17.1"
|
||||
[dev-dependencies]
|
||||
client-db = { package = "sc-client-db", path = "../db/", default-features = true }
|
||||
env_logger = "0.7.0"
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client" }
|
||||
tokio = "0.1.22"
|
||||
txpool = { package = "sc-transaction-pool", path = "../../client/transaction-pool" }
|
||||
txpool-api = { package = "sp-transaction-pool", path = "../../primitives/transaction-pool" }
|
||||
|
||||
@@ -14,9 +14,9 @@ jsonrpc-derive = "14.0.3"
|
||||
jsonrpc-pubsub = "14.0.3"
|
||||
log = "0.4.8"
|
||||
parking_lot = "0.9.0"
|
||||
primitives = { package = "sp-core", path = "../../../primitives/core" }
|
||||
runtime_version = { package = "sp-version", path = "../../../primitives/version" }
|
||||
primitives = { package = "sp-core", path = "../../primitives/core" }
|
||||
runtime_version = { package = "sp-version", path = "../../primitives/version" }
|
||||
serde = { version = "1.0.101", features = ["derive"] }
|
||||
serde_json = "1.0.41"
|
||||
txpool-api = { package = "sp-transaction-pool", path = "../../../primitives/transaction-pool" }
|
||||
rpc-primitives = { package = "sp-rpc", path = "../../../primitives/rpc" }
|
||||
txpool-api = { package = "sp-transaction-pool", path = "../../primitives/transaction-pool" }
|
||||
rpc-primitives = { package = "sp-rpc", path = "../../primitives/rpc" }
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
api = { package = "sc-rpc-api", path = "./api" }
|
||||
api = { package = "sc-rpc-api", path = "../rpc-api" }
|
||||
client-api = { package = "sc-client-api", path = "../api" }
|
||||
client = { package = "sc-client", path = "../" }
|
||||
sp-api = { path = "../../primitives/api" }
|
||||
@@ -34,6 +34,6 @@ futures01 = { package = "futures", version = "0.1.29" }
|
||||
network = { package = "sc-network", path = "../network" }
|
||||
rustc-hex = "2.0.1"
|
||||
sp-io = { path = "../../primitives/io" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client" }
|
||||
tokio = "0.1.22"
|
||||
txpool = { package = "sc-transaction-pool", path = "../transaction-pool" }
|
||||
@@ -51,12 +51,12 @@ rpc = { package = "sc-rpc", path = "../rpc" }
|
||||
tel = { package = "sc-telemetry", path = "../telemetry" }
|
||||
offchain = { package = "sc-offchain", path = "../offchain" }
|
||||
parity-multiaddr = { package = "parity-multiaddr", version = "0.5.0" }
|
||||
grafana-data-source = { path = "../grafana-data-source" }
|
||||
grafana-data-source = { path = "../../utils/grafana-data-source" }
|
||||
sc-tracing = { package = "sc-tracing", path = "../tracing" }
|
||||
tracing = "0.1.10"
|
||||
|
||||
[dev-dependencies]
|
||||
substrate-test-runtime-client = { path = "../../test/utils/runtime/client" }
|
||||
substrate-test-runtime-client = { path = "../../test-utils/runtime/client" }
|
||||
babe-primitives = { package = "sp-consensus-babe", path = "../../primitives/consensus/babe" }
|
||||
grandpa = { package = "sc-finality-grandpa", path = "../finality-grandpa" }
|
||||
grandpa-primitives = { package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" }
|
||||
|
||||
@@ -15,7 +15,7 @@ slog = { version = "2.5.2", features = ["nested-values"] }
|
||||
tracing-core = "0.1.7"
|
||||
|
||||
sc-telemetry = { path = "../telemetry" }
|
||||
grafana-data-source = { path = "../grafana-data-source" }
|
||||
grafana-data-source = { path = "../../utils/grafana-data-source" }
|
||||
|
||||
[dev-dependencies]
|
||||
tracing = "0.1.10"
|
||||
@@ -20,4 +20,4 @@ sp-blockchain = { path = "../../primitives/blockchain" }
|
||||
|
||||
[dev-dependencies]
|
||||
keyring = { package = "sp-keyring", path = "../../primitives/keyring" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test-utils/runtime/client" }
|
||||
@@ -17,7 +17,7 @@ txpool-api = { package = "sp-transaction-pool", path = "../../../primitives/tran
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.3.0"
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0" }
|
||||
test_runtime = { package = "substrate-test-runtime", path = "../../../test/utils/runtime" }
|
||||
test_runtime = { package = "substrate-test-runtime", path = "../../../test-utils/runtime" }
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
[package]
|
||||
name = "substrate-wasm-builder-runner"
|
||||
version = "1.0.4"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
description = "Runner for substrate-wasm-builder"
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/substrate"
|
||||
license = "GPL-3.0"
|
||||
|
||||
[dependencies]
|
||||
@@ -1,12 +0,0 @@
|
||||
## WASM builder runner
|
||||
|
||||
Since cargo contains many bugs when it comes to correct dependency and feature
|
||||
resolution, we need this little tool. See <https://github.com/rust-lang/cargo/issues/5730> for
|
||||
more information.
|
||||
|
||||
It will create a project that will call `substrate-wasm-builder` to prevent any dependencies
|
||||
from `substrate-wasm-builder` influencing the main project's dependencies.
|
||||
|
||||
For more information see <https://crates.io/substrate-wasm-builder>
|
||||
|
||||
License: GPL-3.0
|
||||
@@ -1,275 +0,0 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! # WASM builder runner
|
||||
//!
|
||||
//! Since cargo contains many bugs when it comes to correct dependency and feature
|
||||
//! resolution, we need this little tool. See <https://github.com/rust-lang/cargo/issues/5730> for
|
||||
//! more information.
|
||||
//!
|
||||
//! It will create a project that will call `substrate-wasm-builder` to prevent any dependencies
|
||||
//! from `substrate-wasm-builder` influencing the main project's dependencies.
|
||||
//!
|
||||
//! For more information see <https://crates.io/substrate-wasm-builder>
|
||||
|
||||
use std::{env, process::{Command, self}, fs, path::{PathBuf, Path}};
|
||||
|
||||
/// Environment variable that tells us to skip building the WASM binary.
|
||||
const SKIP_BUILD_ENV: &str = "SKIP_WASM_BUILD";
|
||||
|
||||
/// Environment variable that tells us to create a dummy WASM binary.
|
||||
///
|
||||
/// This is useful for `cargo check` to speed-up the compilation.
|
||||
///
|
||||
/// # Caution
|
||||
///
|
||||
/// Enabling this option will just provide `&[]` as WASM binary.
|
||||
const DUMMY_WASM_BINARY_ENV: &str = "BUILD_DUMMY_WASM_BINARY";
|
||||
|
||||
/// Environment variable that makes sure the WASM build is triggered.
|
||||
const TRIGGER_WASM_BUILD_ENV: &str = "TRIGGER_WASM_BUILD";
|
||||
|
||||
/// Replace all backslashes with slashes.
|
||||
fn replace_back_slashes<T: ToString>(path: T) -> String {
|
||||
path.to_string().replace("\\", "/")
|
||||
}
|
||||
|
||||
/// The `wasm-builder` dependency source.
|
||||
pub enum WasmBuilderSource {
|
||||
/// The relative path to the source code from the current manifest dir.
|
||||
Path(&'static str),
|
||||
/// The git repository that contains the source code.
|
||||
Git {
|
||||
repo: &'static str,
|
||||
rev: &'static str,
|
||||
},
|
||||
/// Use the given version released on crates.io.
|
||||
Crates(&'static str),
|
||||
/// Use the given version released on crates.io or from the given path.
|
||||
CratesOrPath {
|
||||
version: &'static str,
|
||||
path: &'static str,
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmBuilderSource {
|
||||
/// Convert to a valid cargo source declaration.
|
||||
///
|
||||
/// `absolute_path` - The manifest dir.
|
||||
fn to_cargo_source(&self, manifest_dir: &Path) -> String {
|
||||
match self {
|
||||
WasmBuilderSource::Path(path) => {
|
||||
replace_back_slashes(format!("path = \"{}\"", manifest_dir.join(path).display()))
|
||||
}
|
||||
WasmBuilderSource::Git { repo, rev } => {
|
||||
format!("git = \"{}\", rev=\"{}\"", repo, rev)
|
||||
}
|
||||
WasmBuilderSource::Crates(version) => {
|
||||
format!("version = \"{}\"", version)
|
||||
}
|
||||
WasmBuilderSource::CratesOrPath { version, path } => {
|
||||
replace_back_slashes(
|
||||
format!(
|
||||
"path = \"{}\", version = \"{}\"",
|
||||
manifest_dir.join(path).display(),
|
||||
version
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the currently built project as WASM binary and extend `RUSTFLAGS` with the given rustflags.
|
||||
///
|
||||
/// For more information, see [`build_current_project`].
|
||||
pub fn build_current_project_with_rustflags(
|
||||
file_name: &str,
|
||||
wasm_builder_source: WasmBuilderSource,
|
||||
default_rustflags: &str,
|
||||
) {
|
||||
if check_skip_build() {
|
||||
// If we skip the build, we still want to make sure to be called when an env variable changes
|
||||
generate_rerun_if_changed_instructions();
|
||||
return;
|
||||
}
|
||||
|
||||
let manifest_dir = PathBuf::from(
|
||||
env::var("CARGO_MANIFEST_DIR").expect(
|
||||
"`CARGO_MANIFEST_DIR` is always set for `build.rs` files; qed"
|
||||
)
|
||||
);
|
||||
|
||||
let cargo_toml_path = manifest_dir.join("Cargo.toml");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!"));
|
||||
let file_path = out_dir.join(file_name);
|
||||
let project_name = env::var("CARGO_PKG_NAME").expect("`CARGO_PKG_NAME` is set by cargo!");
|
||||
let project_folder = get_workspace_root().join(project_name);
|
||||
|
||||
if check_provide_dummy_wasm_binary() {
|
||||
provide_dummy_wasm_binary(&file_path);
|
||||
} else {
|
||||
create_project(
|
||||
&project_folder,
|
||||
&file_path,
|
||||
&manifest_dir,
|
||||
wasm_builder_source,
|
||||
&cargo_toml_path,
|
||||
default_rustflags,
|
||||
);
|
||||
run_project(&project_folder);
|
||||
}
|
||||
|
||||
// As last step we need to generate our `rerun-if-changed` stuff. If a build fails, we don't
|
||||
// want to spam the output!
|
||||
generate_rerun_if_changed_instructions();
|
||||
}
|
||||
|
||||
/// Build the currently built project as WASM binary.
|
||||
///
|
||||
/// The current project is determined using the `CARGO_MANIFEST_DIR` environment variable.
|
||||
///
|
||||
/// `file_name` - The name of the file being generated in the `OUT_DIR`. The file contains the
|
||||
/// constant `WASM_BINARY` which contains the build wasm binary.
|
||||
/// `wasm_builder_path` - Path to the wasm-builder project, relative to `CARGO_MANIFEST_DIR`.
|
||||
pub fn build_current_project(file_name: &str, wasm_builder_source: WasmBuilderSource) {
|
||||
build_current_project_with_rustflags(file_name, wasm_builder_source, "");
|
||||
}
|
||||
|
||||
/// Returns the root path of the wasm-builder workspace.
|
||||
///
|
||||
/// The wasm-builder workspace contains all wasm-builder's projects.
|
||||
fn get_workspace_root() -> PathBuf {
|
||||
let out_dir_env = env::var("OUT_DIR").expect("`OUT_DIR` is set by cargo!");
|
||||
let mut out_dir = PathBuf::from(&out_dir_env);
|
||||
|
||||
loop {
|
||||
match out_dir.parent() {
|
||||
Some(parent) if out_dir.ends_with("build") => return parent.join("wbuild-runner"),
|
||||
_ => if !out_dir.pop() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Could not find target dir in: {}", out_dir_env)
|
||||
}
|
||||
|
||||
fn create_project(
|
||||
project_folder: &Path,
|
||||
file_path: &Path,
|
||||
manifest_dir: &Path,
|
||||
wasm_builder_source: WasmBuilderSource,
|
||||
cargo_toml_path: &Path,
|
||||
default_rustflags: &str,
|
||||
) {
|
||||
fs::create_dir_all(project_folder.join("src"))
|
||||
.expect("WASM build runner dir create can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
project_folder.join("Cargo.toml"),
|
||||
format!(
|
||||
r#"
|
||||
[package]
|
||||
name = "wasm-build-runner-impl"
|
||||
version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
substrate-wasm-builder = {{ {wasm_builder_source} }}
|
||||
|
||||
[workspace]
|
||||
"#,
|
||||
wasm_builder_source = wasm_builder_source.to_cargo_source(manifest_dir),
|
||||
)
|
||||
).expect("WASM build runner `Cargo.toml` writing can not fail; qed");
|
||||
|
||||
fs::write(
|
||||
project_folder.join("src/main.rs"),
|
||||
format!(
|
||||
r#"
|
||||
use substrate_wasm_builder::build_project_with_default_rustflags;
|
||||
|
||||
fn main() {{
|
||||
build_project_with_default_rustflags(
|
||||
"{file_path}",
|
||||
"{cargo_toml_path}",
|
||||
"{default_rustflags}",
|
||||
)
|
||||
}}
|
||||
"#,
|
||||
file_path = replace_back_slashes(file_path.display()),
|
||||
cargo_toml_path = replace_back_slashes(cargo_toml_path.display()),
|
||||
default_rustflags = default_rustflags,
|
||||
)
|
||||
).expect("WASM build runner `main.rs` writing can not fail; qed");
|
||||
}
|
||||
|
||||
fn run_project(project_folder: &Path) {
|
||||
let cargo = env::var("CARGO").expect("`CARGO` env variable is always set when executing `build.rs`.");
|
||||
let mut cmd = Command::new(cargo);
|
||||
cmd.arg("run").arg(format!("--manifest-path={}", project_folder.join("Cargo.toml").display()));
|
||||
|
||||
if env::var("DEBUG") != Ok(String::from("true")) {
|
||||
cmd.arg("--release");
|
||||
}
|
||||
|
||||
// Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir exclusive).
|
||||
// The runner project is created in `CARGO_TARGET_DIR` and executing it will create a sub target
|
||||
// directory inside of `CARGO_TARGET_DIR`.
|
||||
cmd.env_remove("CARGO_TARGET_DIR");
|
||||
|
||||
if !cmd.status().map(|s| s.success()).unwrap_or(false) {
|
||||
// Don't spam the output with backtraces when a build failed!
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate the name of the skip build environment variable for the current crate.
|
||||
fn generate_crate_skip_build_env_name() -> String {
|
||||
format!(
|
||||
"SKIP_{}_WASM_BUILD",
|
||||
env::var("CARGO_PKG_NAME").expect("Package name is set").to_uppercase().replace('-', "_"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Checks if the build of the WASM binary should be skipped.
|
||||
fn check_skip_build() -> bool {
|
||||
env::var(SKIP_BUILD_ENV).is_ok() || env::var(generate_crate_skip_build_env_name()).is_ok()
|
||||
}
|
||||
|
||||
/// Check if we should provide a dummy WASM binary.
|
||||
fn check_provide_dummy_wasm_binary() -> bool {
|
||||
env::var(DUMMY_WASM_BINARY_ENV).is_ok()
|
||||
}
|
||||
|
||||
/// Provide the dummy WASM binary
|
||||
fn provide_dummy_wasm_binary(file_path: &Path) {
|
||||
fs::write(
|
||||
file_path,
|
||||
"pub const WASM_BINARY: &[u8] = &[]; pub const WASM_BINARY_BLOATY: &[u8] = &[];",
|
||||
).expect("Writing dummy WASM binary should not fail");
|
||||
}
|
||||
|
||||
/// Generate the `rerun-if-changed` instructions for cargo to make sure that the WASM binary is
|
||||
/// rebuilt when needed.
|
||||
fn generate_rerun_if_changed_instructions() {
|
||||
// Make sure that the `build.rs` is called again if one of the following env variables changes.
|
||||
println!("cargo:rerun-if-env-changed={}", SKIP_BUILD_ENV);
|
||||
println!("cargo:rerun-if-env-changed={}", DUMMY_WASM_BINARY_ENV);
|
||||
println!("cargo:rerun-if-env-changed={}", TRIGGER_WASM_BUILD_ENV);
|
||||
println!("cargo:rerun-if-env-changed={}", generate_crate_skip_build_env_name());
|
||||
}
|
||||
Reference in new issue
Block a user