Remove substrate-in-the-browser (#9541)

* Comment out browser stuff

* Remove browser stuff

* Remove more wasm transport code

* Remove ExtTransport and rework how telemetry initialises.

* Change (most) wasm-timer using code to use std::time

* Rename CI-job

* Aura does not compile for wasm

* Remove testing in the browser on CI

* Update README

* Leave `StreamSink` be

* fmt
This commit is contained in:
David
2021-08-17 20:06:23 +02:00
committed by GitHub
parent 598c6676ae
commit 2de7e51c2a
61 changed files with 57 additions and 921 deletions
+1 -22
View File
@@ -391,17 +391,11 @@ test-linux-stable-int:
paths:
- ${CI_COMMIT_SHORT_SHA}_int_failure.log
check-web-wasm:
check-tracing:
stage: test
<<: *docker-env
<<: *test-refs
script:
# WASM support is in progress. As more and more crates support WASM, we
# should add entries here. See https://github.com/paritytech/substrate/issues/2416
# Note: we don't need to test crates imported in `bin/node/cli`
- time cargo build --manifest-path=client/consensus/aura/Cargo.toml --target=wasm32-unknown-unknown --features getrandom
# Note: the command below is a bit weird because several Cargo issues prevent us from compiling the node in a more straight-forward way.
- time cargo +nightly build --manifest-path=bin/node/cli/Cargo.toml --no-default-features --features browser --target=wasm32-unknown-unknown
# with-tracing must be explicitly activated, we run a test to ensure this works as expected in both cases
- time cargo +nightly test --manifest-path primitives/tracing/Cargo.toml --no-default-features
- time cargo +nightly test --manifest-path primitives/tracing/Cargo.toml --no-default-features --features=with-tracing
@@ -459,21 +453,6 @@ check-polkadot-companion-build:
- cd polkadot && git rev-parse --abbrev-ref HEAD
allow_failure: true
test-browser-node:
stage: build
<<: *docker-env
<<: *test-refs
needs:
- job: check-web-wasm
artifacts: false
variables:
<<: *default-vars
CHROMEDRIVER_ARGS: "--log-level=INFO --whitelisted-ips=127.0.0.1"
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: "wasm-bindgen-test-runner"
WASM_BINDGEN_TEST_TIMEOUT: 120
script:
- cargo +nightly test --target wasm32-unknown-unknown -p node-browser-testing
build-linux-substrate: &build-binary
stage: build
<<: *collect-artifacts
-103
View File
@@ -861,12 +861,10 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"js-sys",
"libc",
"num-integer",
"num-traits",
"time",
"wasm-bindgen",
"winapi 0.3.9",
]
@@ -943,16 +941,6 @@ dependencies = [
"cache-padded",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211"
dependencies = [
"cfg-if 0.1.10",
"wasm-bindgen",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
@@ -2178,10 +2166,6 @@ name = "futures-timer"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c"
dependencies = [
"gloo-timers",
"send_wrapper",
]
[[package]]
name = "futures-util"
@@ -2237,10 +2221,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
@@ -2684,9 +2666,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
@@ -4212,20 +4191,6 @@ dependencies = [
"tempfile",
]
[[package]]
name = "node-browser-testing"
version = "3.0.0-dev"
dependencies = [
"jsonrpc-core",
"node-cli",
"parking_lot 0.11.1",
"serde",
"serde_json",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test",
]
[[package]]
name = "node-cli"
version = "3.0.0-dev"
@@ -4236,7 +4201,6 @@ dependencies = [
"frame-system",
"futures 0.3.16",
"hex-literal",
"libp2p-wasm-ext",
"log 0.4.14",
"nix",
"node-executor",
@@ -4288,13 +4252,10 @@ dependencies = [
"sp-transaction-storage-proof",
"sp-trie",
"structopt",
"substrate-browser-utils",
"substrate-build-script-utils",
"substrate-frame-cli",
"tempfile",
"try-runtime-cli",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
@@ -7388,7 +7349,6 @@ dependencies = [
"sp-utils",
"substrate-prometheus-endpoint",
"thiserror",
"wasm-timer",
]
[[package]]
@@ -7746,7 +7706,6 @@ dependencies = [
"substrate-test-runtime-client",
"tempfile",
"tokio 0.2.25",
"wasm-timer",
]
[[package]]
@@ -7790,7 +7749,6 @@ dependencies = [
"sc-transaction-pool-api",
"sp-blockchain",
"sp-runtime",
"wasm-timer",
]
[[package]]
@@ -7879,7 +7837,6 @@ dependencies = [
"thiserror",
"unsigned-varint 0.6.0",
"void",
"wasm-timer",
"zeroize",
]
@@ -7899,7 +7856,6 @@ dependencies = [
"substrate-prometheus-endpoint",
"substrate-test-runtime-client",
"tracing",
"wasm-timer",
]
[[package]]
@@ -8139,7 +8095,6 @@ dependencies = [
"tokio 0.2.25",
"tracing",
"tracing-futures",
"wasm-timer",
]
[[package]]
@@ -8302,7 +8257,6 @@ dependencies = [
"substrate-test-runtime-client",
"substrate-test-runtime-transaction-pool",
"thiserror",
"wasm-timer",
]
[[package]]
@@ -8443,12 +8397,6 @@ dependencies = [
"pest",
]
[[package]]
name = "send_wrapper"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0"
[[package]]
name = "serde"
version = "1.0.126"
@@ -9400,7 +9348,6 @@ dependencies = [
"sp-runtime",
"sp-std",
"thiserror",
"wasm-timer",
]
[[package]]
@@ -9608,30 +9555,6 @@ dependencies = [
"zeroize",
]
[[package]]
name = "substrate-browser-utils"
version = "0.10.0-dev"
dependencies = [
"chrono",
"console_error_panic_hook",
"futures 0.3.16",
"futures-timer 3.0.2",
"getrandom 0.2.3",
"js-sys",
"kvdb-memorydb",
"libp2p-wasm-ext",
"log 0.4.14",
"rand 0.7.3",
"sc-chain-spec",
"sc-informant",
"sc-network",
"sc-service",
"sc-tracing",
"sp-database",
"wasm-bindgen",
"wasm-bindgen-futures",
]
[[package]]
name = "substrate-build-script-utils"
version = "3.0.0"
@@ -10944,8 +10867,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9"
dependencies = [
"cfg-if 1.0.0",
"serde",
"serde_json",
"wasm-bindgen-macro",
]
@@ -11005,30 +10926,6 @@ version = "0.2.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0d4da138503a4cf86801b94d95781ee3619faa8feca830569cc6b54997b8b5c"
dependencies = [
"console_error_panic_hook",
"js-sys",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3199c33f06500c731d5544664c24d0c2b742b98debc6b1c6f0c6d6e8fb7c19b"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "wasm-gc-api"
version = "0.1.11"
-2
View File
@@ -6,7 +6,6 @@ members = [
"bin/node-template/pallets/template",
"bin/node-template/runtime",
"bin/node/bench",
"bin/node/browser-testing",
"bin/node/cli",
"bin/node/test-runner-example",
"bin/node/executor",
@@ -192,7 +191,6 @@ members = [
"test-utils/runtime/transaction-pool",
"test-utils/test-runner",
"test-utils/test-crate",
"utils/browser",
"utils/build-script-utils",
"utils/fork-tree",
"utils/frame/benchmarking-cli",
@@ -1,23 +0,0 @@
[package]
name = "node-browser-testing"
version = "3.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Tests for the in-browser light client."
edition = "2018"
license = "Apache-2.0"
[dependencies]
jsonrpc-core = "18.0.0"
serde = "1.0.126"
serde_json = "1.0.48"
wasm-bindgen = { version = "=0.2.73", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.18"
wasm-bindgen-test = "0.3.18"
node-cli = { path = "../cli", default-features = false, features = [
"browser",
], version = "3.0.0-dev" }
# This is a HACK to make browser tests pass.
# enables [`instant/wasm_bindgen`]
parking_lot = { version = "0.11.1", features = ["wasm-bindgen"] }
@@ -1,67 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Running
//! Running this test can be done with
//! ```text
//! wasm-pack test --firefox --release --headless bin/node/browser-testing
//! ```
//! or (without `wasm-pack`)
//! ```text
//! CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner WASM_BINDGEN_TEST_TIMEOUT=60 cargo test --target wasm32-unknown-unknown
//! ```
//! For debug infomation, such as the informant, run without the `--headless`
//! flag and open a browser to the url that `wasm-pack test` outputs.
//! For more infomation see <https://rustwasm.github.io/docs/wasm-pack/>.
use jsonrpc_core::types::{Id, MethodCall, Params, Success, Version};
use serde::de::DeserializeOwned;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
wasm_bindgen_test_configure!(run_in_browser);
fn rpc_call(method: &str) -> String {
serde_json::to_string(&MethodCall {
jsonrpc: Some(Version::V2),
method: method.into(),
params: Params::None,
id: Id::Num(1),
})
.unwrap()
}
fn deserialize_rpc_result<T: DeserializeOwned>(js_value: JsValue) -> T {
let string = js_value.as_string().unwrap();
let value = serde_json::from_str::<Success>(&string).unwrap().result;
// We need to convert a `Value::Object` into a proper type.
let value_string = serde_json::to_string(&value).unwrap();
serde_json::from_str(&value_string).unwrap()
}
#[wasm_bindgen_test]
async fn runs() {
let mut client = node_cli::start_client(None, "info".into()).unwrap();
// Check that the node handles rpc calls.
// TODO: Re-add the code that checks if the node is syncing.
let chain_name: String = deserialize_rpc_result(
JsFuture::from(client.rpc_send(&rpc_call("system_chain"))).await.unwrap(),
);
assert_eq!(chain_name, "Development");
}
@@ -1,7 +0,0 @@
{
"goog:chromeOptions": {
"args": [
"--whitelisted-ips=127.0.0.1"
]
}
}
+1 -13
View File
@@ -92,12 +92,6 @@ frame-benchmarking-cli = { version = "4.0.0-dev", optional = true, path = "../..
node-inspect = { version = "0.9.0-dev", optional = true, path = "../inspect" }
try-runtime-cli = { version = "0.10.0-dev", optional = true, path = "../../../utils/frame/try-runtime/cli" }
# WASM-specific dependencies
wasm-bindgen = { version = "0.2.73", optional = true }
wasm-bindgen-futures = { version = "0.4.18", optional = true }
browser-utils = { package = "substrate-browser-utils", path = "../../../utils/browser", optional = true, version = "0.10.0-dev"}
libp2p-wasm-ext = { version = "0.29", features = ["websocket"], optional = true }
[target.'cfg(target_arch="x86_64")'.dependencies]
node-executor = { version = "3.0.0-dev", path = "../executor", features = [
"wasmtime",
@@ -143,13 +137,7 @@ path = "../../../client/cli"
optional = true
[features]
default = ["cli"]
browser = [
"browser-utils",
"wasm-bindgen",
"wasm-bindgen-futures",
"libp2p-wasm-ext",
]
default = [ "cli" ]
cli = [
"node-executor/wasmi-errno",
"node-inspect",
@@ -1 +0,0 @@
pkg
@@ -1,10 +0,0 @@
# How to run this demo
```sh
# If necessary, install wasm-bindgen
# The version must match that used when building the browser demo.
cargo install --version 0.2.67 wasm-bindgen-cli
# Run the build script
./build.sh
```
@@ -1,5 +0,0 @@
#!/usr/bin/env sh
set -e -x
cargo +nightly build --release -p node-cli --target wasm32-unknown-unknown --no-default-features --features browser -Z features=itarget
wasm-bindgen ../../../../target/wasm32-unknown-unknown/release/node_cli.wasm --out-dir pkg --target web
python -m http.server 8000
Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

@@ -1,39 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>Substrate node</title>
<link rel="shortcut icon" href="/favicon.png" />
<script type="module">
import { start_client, default as init } from './pkg/node_cli.js';
function log(msg) {
document.getElementsByTagName('body')[0].innerHTML += msg + '\n';
}
async function start() {
log('Loading WASM');
await init('./pkg/node_cli_bg.wasm');
log('Fetching chain spec');
const chain_spec_response = await fetch("https://raw.githubusercontent.com/paritytech/substrate/master/bin/node/cli/res/flaming-fir.json");
const chain_spec_text = await chain_spec_response.text();
// Build our client.
let client = await start_client(chain_spec_text, 'info');
log('Client started');
client.rpcSubscribe('{"method":"chain_subscribeNewHead","params":[],"id":1,"jsonrpc":"2.0"}',
(r) => log("New chain head: " + r));
setInterval(() => {
client
.rpcSend('{"method":"system_localPeerId","params":[],"id":1,"jsonrpc":"2.0"}')
.then((r) => log("Local PeerId: " + r));
}, 20000);
}
start();
</script>
</head>
<body style="white-space: pre"></body>
</html>
-57
View File
@@ -1,57 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use crate::chain_spec::ChainSpec;
use browser_utils::{browser_configuration, init_logging, set_console_error_panic_hook, Client};
use log::info;
use wasm_bindgen::prelude::*;
/// Starts the client.
#[wasm_bindgen]
pub fn start_client(chain_spec: Option<String>, log_level: String) -> Result<Client, JsValue> {
start_inner(chain_spec, log_level).map_err(|err| JsValue::from_str(&err.to_string()))
}
fn start_inner(
chain_spec: Option<String>,
log_directives: String,
) -> Result<Client, Box<dyn std::error::Error>> {
set_console_error_panic_hook();
init_logging(&log_directives)?;
let chain_spec = match chain_spec {
Some(chain_spec) => ChainSpec::from_json_bytes(chain_spec.as_bytes().to_vec())
.map_err(|e| format!("{:?}", e))?,
None => crate::chain_spec::development_config(),
};
let config = browser_configuration(chain_spec)?;
info!("Substrate browser node");
info!("✌️ version {}", config.impl_version);
info!("❤️ by Parity Technologies, 2017-2021");
info!("📋 Chain specification: {}", config.chain_spec.name());
info!("🏷 Node name: {}", config.network.node_name);
info!("👤 Role: {:?}", config.role);
// Create the service. This is the most heavy initialization step.
let (task_manager, rpc_handlers) = crate::service::new_light_base(config)
.map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers))
.map_err(|e| format!("{:?}", e))?;
Ok(browser_utils::start_client(task_manager, rpc_handlers))
}
-4
View File
@@ -34,15 +34,11 @@ pub mod chain_spec;
#[macro_use]
mod service;
#[cfg(feature = "browser")]
mod browser;
#[cfg(feature = "cli")]
mod cli;
#[cfg(feature = "cli")]
mod command;
#[cfg(feature = "browser")]
pub use browser::*;
#[cfg(feature = "cli")]
pub use cli::*;
#[cfg(feature = "cli")]
+1 -6
View File
@@ -441,12 +441,7 @@ pub fn new_light_base(
.clone()
.filter(|x| !x.is_empty())
.map(|endpoints| -> Result<_, sc_telemetry::Error> {
#[cfg(feature = "browser")]
let transport = Some(sc_telemetry::ExtTransport::new(libp2p_wasm_ext::ffi::websocket_transport()));
#[cfg(not(feature = "browser"))]
let transport = None;
let worker = TelemetryWorker::with_transport(16, transport)?;
let worker = TelemetryWorker::new(16)?;
let telemetry = worker.handle().new_telemetry(endpoints);
Ok((worker, telemetry))
})
+3 -12
View File
@@ -27,10 +27,9 @@ use names::{Generator, Name};
use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_service::{
config::{
BasePath, Configuration, DatabaseSource, ExtTransport, KeystoreConfig,
NetworkConfiguration, NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode,
Role, RpcMethods, TaskExecutor, TelemetryEndpoints, TransactionPoolOptions,
WasmExecutionMethod,
BasePath, Configuration, DatabaseSource, KeystoreConfig, NetworkConfiguration,
NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode, Role, RpcMethods,
TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod,
},
ChainSpec, KeepBlocks, TracingReceiver, TransactionStorageMode,
};
@@ -380,13 +379,6 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
Ok(chain_spec.telemetry_endpoints().clone())
}
/// Get the telemetry external transport
///
/// By default this is `None`.
fn telemetry_external_transport(&self) -> Result<Option<ExtTransport>> {
Ok(None)
}
/// Get the default value for heap pages
///
/// By default this is `None`.
@@ -526,7 +518,6 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
rpc_max_payload: self.rpc_max_payload()?,
prometheus_config: self.prometheus_config(DCV::prometheus_listen_port())?,
telemetry_endpoints,
telemetry_external_transport: self.telemetry_external_transport()?,
default_heap_pages: self.default_heap_pages()?,
offchain_worker: self.offchain_worker(&role)?,
force_authoring: self.force_authoring()?,
@@ -222,7 +222,6 @@ impl NetworkParams {
transport: TransportConfig::Normal {
enable_mdns: !is_dev && !self.no_mdns,
allow_private_ipv4,
wasm_external_transport: None,
},
max_parallel_downloads: self.max_parallel_downloads,
enable_dht_random_walk: !self.reserved_only,
@@ -29,7 +29,6 @@ sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" }
parking_lot = "0.11.1"
serde = { version = "1.0", features = ["derive"] }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0" }
wasm-timer = "0.2.5"
async-trait = "0.1.42"
[dev-dependencies]
@@ -265,7 +265,7 @@ pub(crate) async fn import_single_block_metered<
r => return Ok(r), // Any other successful result means that the block is already imported.
}
let started = wasm_timer::Instant::now();
let started = std::time::Instant::now();
let mut import_block = BlockImportParams::new(block_origin, header);
import_block.body = block.body;
@@ -289,7 +289,7 @@ impl<B: BlockT> BlockImportWorker<B> {
number: NumberFor<B>,
justification: Justification,
) {
let started = wasm_timer::Instant::now();
let started = std::time::Instant::now();
let success = match self.justification_import.as_mut() {
Some(justification_import) => justification_import
@@ -45,7 +45,6 @@ prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../..
sc-block-builder = { version = "0.10.0-dev", path = "../block-builder" }
finality-grandpa = { version = "0.14.1", features = ["derive-codec"] }
async-trait = "0.1.50"
wasm-timer = "0.2"
[dev-dependencies]
assert_matches = "1.3.0"
@@ -101,9 +101,8 @@ use crate::{environment, CatchUp, CompactCommit, SignedMessage};
use std::{
collections::{HashMap, HashSet, VecDeque},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
const REBROADCAST_AFTER: Duration = Duration::from_secs(60 * 5);
const CATCH_UP_REQUEST_TIMEOUT: Duration = Duration::from_secs(45);
@@ -46,9 +46,8 @@ use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
const LOG_PENDING_INTERVAL: Duration = Duration::from_secs(15);
-1
View File
@@ -23,4 +23,3 @@ sc-network = { version = "0.10.0-dev", path = "../network" }
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }
sc-transaction-pool-api = { version = "4.0.0-dev", path = "../transaction-pool/api" }
wasm-timer = "0.2"
+1 -1
View File
@@ -25,8 +25,8 @@ use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Saturating, Zer
use std::{
convert::{TryFrom, TryInto},
fmt,
time::Instant,
};
use wasm_timer::Instant;
/// State of the informant display system.
///
@@ -23,7 +23,6 @@ lru = "0.6.5"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" }
sc-network = { version = "0.10.0-dev", path = "../network" }
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }
wasm-timer = "0.2"
tracing = "0.1.25"
[dev-dependencies]
@@ -29,8 +29,8 @@ use std::{
iter,
sync::Arc,
time,
time::Instant,
};
use wasm_timer::Instant;
// FIXME: Add additional spam/DoS attack protection: https://github.com/paritytech/substrate/issues/1115
// NOTE: The current value is adjusted based on largest production network deployment (Kusama) and
-1
View File
@@ -63,7 +63,6 @@ unsigned-varint = { version = "0.6.0", features = [
"asynchronous_codec",
] }
void = "1.0.2"
wasm-timer = "0.2"
zeroize = "1.2.0"
[dependencies.libp2p]
+3 -15
View File
@@ -29,7 +29,7 @@ pub use crate::{
},
warp_request_handler::WarpSyncProvider,
};
pub use libp2p::{build_multiaddr, core::PublicKey, identity, wasm_ext::ExtTransport};
pub use libp2p::{build_multiaddr, core::PublicKey, identity};
// Note: this re-export shouldn't be part of the public API of the crate and will be removed in
// the future.
@@ -42,7 +42,7 @@ use core::{fmt, iter};
use futures::future;
use libp2p::{
identity::{ed25519, Keypair},
multiaddr, wasm_ext, Multiaddr, PeerId,
multiaddr, Multiaddr, PeerId,
};
use prometheus_endpoint::Registry;
use sc_consensus::ImportQueue;
@@ -490,11 +490,7 @@ impl NetworkConfiguration {
extra_sets: Vec::new(),
client_version: client_version.into(),
node_name: node_name.into(),
transport: TransportConfig::Normal {
enable_mdns: false,
allow_private_ipv4: true,
wasm_external_transport: None,
},
transport: TransportConfig::Normal { enable_mdns: false, allow_private_ipv4: true },
max_parallel_downloads: 5,
sync_mode: SyncMode::Full,
enable_dht_random_walk: true,
@@ -628,14 +624,6 @@ pub enum TransportConfig {
/// [RFC1918](https://tools.ietf.org/html/rfc1918)). Irrelevant for addresses that have
/// been passed in [`NetworkConfiguration::boot_nodes`].
allow_private_ipv4: bool,
/// Optional external implementation of a libp2p transport. Used in WASM contexts where we
/// need some binding between the networking provided by the operating system or
/// environment and libp2p.
///
/// This parameter exists whatever the target platform is, but it is expected to be set to
/// `Some` only when compiling for WASM.
wasm_external_transport: Option<wasm_ext::ExtTransport>,
},
/// Only allow connections within the same process.
+1 -2
View File
@@ -40,9 +40,8 @@ use std::{
error, io,
pin::Pin,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
/// Time after we disconnect from a node before we purge its information from the cache.
const CACHE_EXPIRE: Duration = Duration::from_secs(10 * 60);
@@ -42,9 +42,8 @@ use std::{
str,
sync::Arc,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
/// Network behaviour that handles opening substreams for custom protocols with other peers.
///
@@ -88,9 +88,8 @@ use std::{
str,
sync::Arc,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
/// Number of pending notifications in asynchronous contexts.
/// See [`NotificationsSink::reserve_notification`] for context.
@@ -24,9 +24,8 @@ use sp_blockchain::Error as ClientError;
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero};
use std::{
collections::{HashMap, HashSet, VecDeque},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
// Time to wait before trying to get the same extra data from the same peer.
const EXTRA_RETRY_WAIT: Duration = Duration::from_secs(10);
@@ -60,9 +60,8 @@ use std::{
io, iter,
pin::Pin,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::Instant;
pub use libp2p::request_response::{InboundFailure, OutboundFailure, RequestId};
+3 -5
View File
@@ -291,10 +291,9 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
};
let (transport, bandwidth) = {
let (config_mem, config_wasm) = match params.network_config.transport {
TransportConfig::MemoryOnly => (true, None),
TransportConfig::Normal { wasm_external_transport, .. } =>
(false, wasm_external_transport),
let config_mem = match params.network_config.transport {
TransportConfig::MemoryOnly => true,
TransportConfig::Normal { .. } => false,
};
// The yamux buffer size limit is configured to be equal to the maximum frame size
@@ -337,7 +336,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
transport::build_transport(
local_identity,
config_mem,
config_wasm,
params.network_config.yamux_window_size,
yamux_maximum_buffer_size,
)
+4 -11
View File
@@ -25,7 +25,7 @@ use libp2p::{
transport::{Boxed, OptionalTransport},
upgrade,
},
identity, mplex, noise, wasm_ext, PeerId, Transport,
identity, mplex, noise, PeerId, Transport,
};
#[cfg(not(target_os = "unknown"))]
use libp2p::{dns, tcp, websocket};
@@ -51,18 +51,11 @@ pub use self::bandwidth::BandwidthSinks;
pub fn build_transport(
keypair: identity::Keypair,
memory_only: bool,
wasm_external_transport: Option<wasm_ext::ExtTransport>,
yamux_window_size: Option<u32>,
yamux_maximum_buffer_size: usize,
) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>) {
// Build the base layer of the transport.
let transport = if let Some(t) = wasm_external_transport {
OptionalTransport::some(t)
} else {
OptionalTransport::none()
};
#[cfg(not(target_os = "unknown"))]
let transport = transport.or_transport(if !memory_only {
let transport = if !memory_only {
let desktop_trans = tcp::TcpConfig::new().nodelay(true);
let desktop_trans =
websocket::WsConfig::new(desktop_trans.clone()).or_transport(desktop_trans);
@@ -73,9 +66,9 @@ pub fn build_transport(
EitherTransport::Right(desktop_trans.map_err(dns::DnsErr::Transport))
})
} else {
// For the in-memory case we set up the transport with an `.or_transport` below.
OptionalTransport::none()
});
};
let transport = transport.or_transport(if memory_only {
OptionalTransport::some(libp2p::core::transport::MemoryTransport::default())
} else {
+2 -2
View File
@@ -42,9 +42,9 @@ use std::{
collections::{HashMap, HashSet, VecDeque},
pin::Pin,
task::{Context, Poll},
time::Duration,
time::{Duration, Instant},
};
use wasm_timer::{Delay, Instant};
use wasm_timer::Delay;
pub use libp2p::PeerId;
+1 -1
View File
@@ -36,8 +36,8 @@ use std::{
hash_map::{Entry, OccupiedEntry},
HashMap, HashSet,
},
time::Instant,
};
use wasm_timer::Instant;
/// State storage behind the peerset.
///
-1
View File
@@ -30,7 +30,6 @@ rand = "0.7.3"
parking_lot = "0.11.1"
log = "0.4.11"
futures-timer = "3.0.1"
wasm-timer = "0.2"
exit-future = "0.2.0"
pin-project = "1.0.4"
hash-db = "0.15.2"
+1 -2
View File
@@ -62,8 +62,7 @@ use sp_runtime::{
BuildStorage,
};
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender};
use std::{str::FromStr, sync::Arc};
use wasm_timer::SystemTime;
use std::{str::FromStr, sync::Arc, time::SystemTime};
/// A utility trait for building an RPC extension given a `DenyUnsafe` instance.
/// This is useful since at service definition time we don't know whether the
+1 -4
View File
@@ -23,7 +23,7 @@ pub use sc_client_db::{Database, DatabaseSource, KeepBlocks, PruningMode, Transa
pub use sc_executor::WasmExecutionMethod;
pub use sc_network::{
config::{
ExtTransport, IncomingRequest, MultiaddrWithPeerId, NetworkConfiguration, NodeKeyConfig,
IncomingRequest, MultiaddrWithPeerId, NetworkConfiguration, NodeKeyConfig,
NonDefaultSetConfig, OutgoingResponse, RequestResponseConfig, Role, SetConfig,
TransportConfig,
},
@@ -107,9 +107,6 @@ pub struct Configuration {
pub prometheus_config: Option<PrometheusConfig>,
/// Telemetry service URL. `None` if disabled.
pub telemetry_endpoints: Option<TelemetryEndpoints>,
/// External WASM transport for the telemetry. If `Some`, when connection to a telemetry
/// endpoint, this transport will be tried in priority before all others.
pub telemetry_external_transport: Option<ExtTransport>,
/// The default number of 64KB pages to allocate for Wasm execution
pub default_heap_pages: Option<u64>,
/// Should offchain workers be executed.
+4 -2
View File
@@ -28,8 +28,10 @@ use sc_transaction_pool_api::{MaintainedTransactionPool, PoolStatus};
use sp_api::ProvideRuntimeApi;
use sp_runtime::traits::{Block, NumberFor, SaturatedConversion, UniqueSaturatedInto};
use sp_utils::metrics::register_globals;
use std::{sync::Arc, time::Duration};
use wasm_timer::Instant;
use std::{
sync::Arc,
time::{Duration, Instant},
};
struct PrometheusMetrics {
// generic info
+2 -6
View File
@@ -221,11 +221,8 @@ fn node_config<
.collect(),
);
network_config.transport = TransportConfig::Normal {
enable_mdns: false,
allow_private_ipv4: true,
wasm_external_transport: None,
};
network_config.transport =
TransportConfig::Normal { enable_mdns: false, allow_private_ipv4: true };
Configuration {
impl_name: String::from("network-test-impl"),
@@ -256,7 +253,6 @@ fn node_config<
rpc_max_payload: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: Default::default(),
force_authoring: false,
+1 -10
View File
@@ -46,7 +46,6 @@ use std::{
sync::{atomic, Arc},
};
pub use libp2p::wasm_ext::ExtTransport;
pub use log;
pub use serde_json;
@@ -124,15 +123,7 @@ impl TelemetryWorker {
///
/// Only one is needed per process.
pub fn new(buffer_size: usize) -> Result<Self> {
Self::with_transport(buffer_size, None)
}
/// Instantiate a new [`TelemetryWorker`] with the given [`ExtTransport`]
/// which can run in background.
///
/// Only one is needed per process.
pub fn with_transport(buffer_size: usize, transport: Option<ExtTransport>) -> Result<Self> {
let transport = initialize_transport(transport)?;
let transport = initialize_transport()?;
let (message_sender, message_receiver) = mpsc::channel(buffer_size);
let (register_sender, register_receiver) = mpsc::unbounded();
+4 -22
View File
@@ -22,30 +22,15 @@ use futures::{
ready,
task::{Context, Poll},
};
use libp2p::{
core::transport::{timeout::TransportTimeout, OptionalTransport},
wasm_ext, Transport,
};
use libp2p::{core::transport::timeout::TransportTimeout, Transport};
use std::{io, pin::Pin, time::Duration};
/// Timeout after which a connection attempt is considered failed. Includes the WebSocket HTTP
/// upgrading.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(20);
pub(crate) fn initialize_transport(
wasm_external_transport: Option<wasm_ext::ExtTransport>,
) -> Result<WsTrans, io::Error> {
let transport = match wasm_external_transport.clone() {
Some(t) => OptionalTransport::some(t),
None => OptionalTransport::none(),
}
.map((|inner, _| StreamSink::from(inner)) as fn(_, _) -> _);
// The main transport is the `wasm_external_transport`, but if we're on desktop we add
// support for TCP+WebSocket+DNS as a fallback. In practice, you're not expected to pass
// an external transport on desktop and the fallback is used all the time.
#[cfg(not(target_os = "unknown"))]
let transport = transport.or_transport({
pub(crate) fn initialize_transport() -> Result<WsTrans, io::Error> {
let transport = {
let inner = block_on(libp2p::dns::DnsConfig::system(libp2p::tcp::TcpConfig::new()))?;
libp2p::websocket::framed::WsConfig::new(inner).and_then(|connec, _| {
let connec = connec
@@ -57,7 +42,7 @@ pub(crate) fn initialize_transport(
.map_ok(|data| data.into_bytes());
future::ready(Ok::<_, io::Error>(connec))
})
});
};
Ok(TransportTimeout::new(
transport.map(|out, _| {
@@ -86,9 +71,6 @@ pub(crate) type WsTrans = libp2p::core::transport::Boxed<
/// Wraps around an `AsyncWrite` and implements `Sink`. Guarantees that each item being sent maps
/// to one call of `write`.
///
/// For some context, we put this object around the `wasm_ext::ExtTransport` in order to make sure
/// that each telemetry message maps to one single call to `write` in the WASM FFI.
#[pin_project::pin_project]
pub(crate) struct StreamSink<T>(#[pin] T, Option<Vec<u8>>);
@@ -1,116 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
use crate::logging::event_format::{CustomFmtContext, EventFormat};
use std::fmt;
use tracing::{Event, Level, Subscriber};
use tracing_subscriber::{
fmt::{
time::{FormatTime, SystemTime},
FormatFields,
},
layer::Context,
registry::LookupSpan,
Layer,
};
use wasm_bindgen::prelude::*;
/// A `Layer` that display logs in the browser's console.
pub struct ConsoleLogLayer<S, N = tracing_subscriber::fmt::format::DefaultFields, T = SystemTime> {
event_format: EventFormat<T>,
fmt_fields: N,
_inner: std::marker::PhantomData<S>,
}
impl<S, T> ConsoleLogLayer<S, tracing_subscriber::fmt::format::DefaultFields, T> {
/// Create a new [`ConsoleLogLayer`] using the `EventFormat` provided in argument.
pub fn new(event_format: EventFormat<T>) -> Self {
Self { event_format, fmt_fields: Default::default(), _inner: std::marker::PhantomData }
}
}
// NOTE: the following code took inspiration from `EventFormat` (in this file)
impl<S, N, T: FormatTime> ConsoleLogLayer<S, N, T>
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'writer> FormatFields<'writer> + 'static,
{
fn format_event(
&self,
ctx: &Context<'_, S>,
writer: &mut dyn fmt::Write,
event: &Event,
) -> fmt::Result {
self.event_format.format_event_custom(
CustomFmtContext::ContextWithFormatFields(ctx, &self.fmt_fields),
writer,
event,
)
}
}
// NOTE: the following code took inspiration from tracing-subscriber
//
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/fmt_layer.rs#L717
impl<S, N, T> Layer<S> for ConsoleLogLayer<S, N, T>
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'writer> FormatFields<'writer> + 'static,
T: FormatTime + 'static,
{
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
thread_local! {
static BUF: std::cell::RefCell<String> = std::cell::RefCell::new(String::new());
}
BUF.with(|buf| {
let borrow = buf.try_borrow_mut();
let mut a;
let mut b;
let mut buf = match borrow {
Ok(buf) => {
a = buf;
&mut *a
},
_ => {
b = String::new();
&mut b
},
};
if self.format_event(&ctx, &mut buf, event).is_ok() {
if !buf.is_empty() {
let meta = event.metadata();
let level = meta.level();
// NOTE: the following code took inspiration from tracing-subscriber
//
// https://github.com/iamcodemaker/console_log/blob/f13b5d6755/src/lib.rs#L149
match *level {
Level::ERROR => web_sys::console::error_1(&JsValue::from(buf.as_str())),
Level::WARN => web_sys::console::warn_1(&JsValue::from(buf.as_str())),
Level::INFO => web_sys::console::info_1(&JsValue::from(buf.as_str())),
Level::DEBUG => web_sys::console::log_1(&JsValue::from(buf.as_str())),
Level::TRACE => web_sys::console::debug_1(&JsValue::from(buf.as_str())),
}
}
}
buf.clear();
});
}
}
@@ -16,10 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#[cfg(target_os = "unknown")]
mod console_log;
mod prefix_layer;
#[cfg(target_os = "unknown")]
pub use console_log::*;
pub use prefix_layer::*;
@@ -30,7 +30,6 @@ sp-transaction-pool = { version = "4.0.0-dev", path = "../../primitives/transact
sc-transaction-pool-api = { version = "4.0.0-dev", path = "./api" }
sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-utils = { version = "4.0.0-dev", path = "../../primitives/utils" }
wasm-timer = "0.2"
serde = { version = "1.0.126", features = ["derive"] }
linked-hash-map = "0.5.4"
retain_mut = "0.1.3"
@@ -19,7 +19,6 @@ futures = "0.3.9"
log = "0.4.8"
parking_lot = "0.11.1"
serde = { version = "1.0.101", features = ["derive"] }
wasm-timer = "0.2"
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
sp-utils = { version = "4.0.0-dev", path = "../../../primitives/utils" }
sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
@@ -24,7 +24,7 @@ use std::{
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::transaction_validity::TransactionTag as Tag;
use wasm_timer::Instant;
use std::time::Instant;
use super::base_pool::Transaction;
@@ -27,7 +27,7 @@ use sp_runtime::{
TransactionSource, TransactionTag as Tag, TransactionValidity, TransactionValidityError,
},
};
use wasm_timer::Instant;
use std::time::Instant;
use super::{
base_pool as base,
@@ -454,9 +454,11 @@ mod tests {
traits::Hash,
transaction_validity::{InvalidTransaction, TransactionSource, ValidTransaction},
};
use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
time::Instant,
};
use substrate_test_runtime::{AccountId, Block, Extrinsic, Hashing, Transfer, H256};
use wasm_timer::Instant;
const INVALID_NONCE: u64 = 254;
const SOURCE: TransactionSource = TransactionSource::External;
@@ -22,8 +22,11 @@
//! Discarded extrinsics are banned so that they don't get re-imported again.
use parking_lot::RwLock;
use std::{collections::HashMap, hash, iter, time::Duration};
use wasm_timer::Instant;
use std::{
collections::HashMap,
hash, iter,
time::{Duration, Instant},
};
use super::base_pool::Transaction;
@@ -32,7 +32,7 @@ use sp_runtime::{
traits::{self, SaturatedConversion},
transaction_validity::{TransactionSource, TransactionTag as Tag, ValidTransaction},
};
use wasm_timer::Instant;
use std::time::Instant;
use super::{
base_pool::{self as base, PruneStatus},
+1 -1
View File
@@ -63,7 +63,7 @@ use sp_runtime::{
generic::BlockId,
traits::{AtLeast32Bit, Block as BlockT, Extrinsic, Header as HeaderT, NumberFor, Zero},
};
use wasm_timer::Instant;
use std::time::Instant;
use crate::metrics::MetricsLink as PrometheusMetrics;
use prometheus_endpoint::Registry as PrometheusRegistry;
-63
View File
@@ -439,69 +439,6 @@ and add the new service:
The telemetry subsystem has seen a few fixes and refactorings to allow for a more flexible handling, in particular in regards to parachains. Most notably `sc_service::spawn_tasks` now returns the `telemetry_connection_notifier` as the second member of the tuple, (`let (_rpc_handlers, telemetry_connection_notifier) = sc_service::spawn_tasks(`), which should be passed to `telemetry_on_connect` of `new_full_base` now: `telemetry_on_connect: telemetry_connection_notifier.map(|x| x.on_connect_stream()),` (see the service-section below for a full diff).
On the browser-side, this complicates setup a tiny bit, yet not terribly. Instead of `init_console_log`, we now use `init_logging_and_telemetry` and need to make sure we spawn the runner for its handle at the end (the other changes are formatting and cosmetics):
```diff
--- a/bin/node/cli/src/browser.rs
+++ b/bin/node/cli/src/browser.rs
@@ -21,9 +21,8 @@ use log::info;
use wasm_bindgen::prelude::*;
use browser_utils::{
Client,
- browser_configuration, set_console_error_panic_hook, init_console_log,
+ browser_configuration, init_logging_and_telemetry, set_console_error_panic_hook,
};
-use std::str::FromStr;
/// Starts the client.
#[wasm_bindgen]
@@ -33,29 +32,38 @@ pub async fn start_client(chain_spec: Option<String>, log_level: String) -> Resu
.map_err(|err| JsValue::from_str(&err.to_string()))
}
-async fn start_inner(chain_spec: Option<String>, log_level: String) -> Result<Client, Box<dyn std::error::Error>> {
+async fn start_inner(
+ chain_spec: Option<String>,
+ log_directives: String,
+) -> Result<Client, Box<dyn std::error::Error>> {
set_console_error_panic_hook();
- init_console_log(log::Level::from_str(&log_level)?)?;
+ let telemetry_worker = init_logging_and_telemetry(&log_directives)?;
let chain_spec = match chain_spec {
Some(chain_spec) => ChainSpec::from_json_bytes(chain_spec.as_bytes().to_vec())
.map_err(|e| format!("{:?}", e))?,
None => crate::chain_spec::development_config(),
};
- let config = browser_configuration(chain_spec).await?;
+ let telemetry_handle = telemetry_worker.handle();
+ let config = browser_configuration(
+ chain_spec,
+ Some(telemetry_handle),
+ ).await?;
info!("Substrate browser node");
info!("✌️ version {}", config.impl_version);
- info!("❤️ by Parity Technologies, 2017-2020");
+ info!("❤️ by Parity Technologies, 2017-2021");
info!("📋 Chain specification: {}", config.chain_spec.name());
- info!("🏷 Node name: {}", config.network.node_name);
+ info!("🏷 Node name: {}", config.network.node_name);
info!("👤 Role: {:?}", config.role);
// Create the service. This is the most heavy initialization step.
let (task_manager, rpc_handlers) =
crate::service::new_light_base(config)
- .map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers))
+ .map(|(components, rpc_handlers, _, _, _, _)| (components, rpc_handlers))
.map_err(|e| format!("{:?}", e))?;
+ task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run());
+
Ok(browser_utils::start_client(task_manager, rpc_handlers))
}
```
##### Async & Remote Keystore support
In order to allow for remote-keystores, the keystore-subsystem has been reworked to support async operations and generally refactored to not provide the keys itself but only sign on request. This allows for remote-keystore to never hand out keys and thus to operate any substrate-based node in a manner without ever having the private keys in the local system memory.
@@ -18,7 +18,6 @@ sp-std = { version = "4.0.0-dev", default-features = false, path = "../std" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../runtime" }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-inherents = { version = "4.0.0-dev", default-features = false, path = "../inherents" }
wasm-timer = { version = "0.2", optional = true }
thiserror = { version = "1.0.21", optional = true }
log = { version = "0.4.8", optional = true }
futures-timer = { version = "3.0.2", optional = true }
@@ -32,7 +31,6 @@ std = [
"sp-runtime/std",
"codec/std",
"sp-inherents/std",
"wasm-timer",
"thiserror",
"log",
"futures-timer",
+1 -1
View File
@@ -164,7 +164,7 @@ impl TimestampInherentData for InherentData {
/// This timestamp is the time since the UNIX epoch.
#[cfg(feature = "std")]
fn current_timestamp() -> std::time::Duration {
use wasm_timer::SystemTime;
use std::time::SystemTime;
let now = SystemTime::now();
now.duration_since(SystemTime::UNIX_EPOCH)
@@ -101,7 +101,6 @@ pub fn default_config(
rpc_max_payload: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: Default::default(),
force_authoring: false,
-35
View File
@@ -1,35 +0,0 @@
[package]
name = "substrate-browser-utils"
version = "0.10.0-dev"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Utilities for creating a browser light-client."
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
readme = "README.md"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
futures = "0.3.16"
log = "0.4.8"
libp2p-wasm-ext = { version = "0.29", features = ["websocket"] }
console_error_panic_hook = "0.1.6"
js-sys = "0.3.34"
wasm-bindgen = "0.2.73"
wasm-bindgen-futures = "0.4.18"
kvdb-memorydb = "0.10.0"
sp-database = { version = "4.0.0-dev", path = "../../primitives/database" }
sc-informant = { version = "0.10.0-dev", path = "../../client/informant" }
sc-service = { version = "0.10.0-dev", path = "../../client/service", default-features = false }
sc-network = { path = "../../client/network", version = "0.10.0-dev"}
sc-chain-spec = { path = "../../client/chain-spec", version = "4.0.0-dev"}
sc-tracing = { path = "../../client/tracing", version = "4.0.0-dev"}
# Imported just for the `wasm-bindgen` feature
getrandom = { version = "0.2", features = ["js"] }
rand = { version = "0.7", features = ["wasm-bindgen"] }
futures-timer = { version = "3.0.1", features = ["wasm-bindgen"]}
chrono = { version = "0.4", features = ["wasmbind"] }
-1
View File
@@ -1 +0,0 @@
License: Apache-2.0
-212
View File
@@ -1,212 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use futures::{
channel::{mpsc, oneshot},
future::{ready, select},
prelude::*,
};
use libp2p_wasm_ext::{ffi, ExtTransport};
use log::{debug, info};
use sc_chain_spec::Extension;
use sc_network::config::TransportConfig;
use sc_service::{
config::{DatabaseSource, KeystoreConfig, NetworkConfiguration},
Configuration, GenericChainSpec, KeepBlocks, Role, RpcHandlers, RpcSession, RuntimeGenesis,
TaskManager, TransactionStorageMode,
};
use sc_tracing::logging::LoggerBuilder;
use std::pin::Pin;
use wasm_bindgen::prelude::*;
pub use console_error_panic_hook::set_once as set_console_error_panic_hook;
/// Initialize the logger and return a `TelemetryWorker` and a wasm `ExtTransport`.
pub fn init_logging(pattern: &str) -> Result<(), sc_tracing::logging::Error> {
LoggerBuilder::new(pattern).init()
}
/// Create a service configuration from a chain spec.
///
/// This configuration contains good defaults for a browser light client.
pub fn browser_configuration<G, E>(
chain_spec: GenericChainSpec<G, E>,
) -> Result<Configuration, Box<dyn std::error::Error>>
where
G: RuntimeGenesis + 'static,
E: Extension + 'static + Send + Sync,
{
let name = chain_spec.name().to_string();
let transport = ExtTransport::new(ffi::websocket_transport());
let mut network = NetworkConfiguration::new(
format!("{} (Browser)", name),
"unknown",
Default::default(),
None,
);
network.boot_nodes = chain_spec.boot_nodes().to_vec();
network.transport = TransportConfig::Normal {
wasm_external_transport: Some(transport.clone()),
allow_private_ipv4: true,
enable_mdns: false,
};
let config = Configuration {
network,
telemetry_endpoints: chain_spec.telemetry_endpoints().clone(),
chain_spec: Box::new(chain_spec),
task_executor: (|fut, _| {
wasm_bindgen_futures::spawn_local(fut);
async {}
})
.into(),
telemetry_external_transport: Some(transport),
role: Role::Light,
database: {
info!("Opening Indexed DB database '{}'...", name);
let db = kvdb_memorydb::create(10);
DatabaseSource::Custom(sp_database::as_database(db))
},
keystore_remote: Default::default(),
keystore: KeystoreConfig::InMemory,
default_heap_pages: Default::default(),
dev_key_seed: Default::default(),
disable_grandpa: Default::default(),
execution_strategies: Default::default(),
force_authoring: Default::default(),
impl_name: String::from("parity-substrate"),
impl_version: String::from("0.0.0"),
offchain_worker: Default::default(),
prometheus_config: Default::default(),
state_pruning: Default::default(),
keep_blocks: KeepBlocks::All,
transaction_storage: TransactionStorageMode::BlockBody,
rpc_cors: Default::default(),
rpc_http: Default::default(),
rpc_ipc: Default::default(),
rpc_ws: Default::default(),
rpc_ws_max_connections: Default::default(),
rpc_http_threads: Default::default(),
rpc_methods: Default::default(),
rpc_max_payload: Default::default(),
state_cache_child_ratio: Default::default(),
state_cache_size: Default::default(),
tracing_receiver: Default::default(),
tracing_targets: Default::default(),
transaction_pool: Default::default(),
wasm_method: Default::default(),
wasm_runtime_overrides: Default::default(),
max_runtime_instances: 8,
announce_block: true,
base_path: None,
informant_output_format: sc_informant::OutputFormat { enable_color: false },
disable_log_reloading: false,
};
Ok(config)
}
/// A running client.
#[wasm_bindgen]
pub struct Client {
rpc_send_tx: mpsc::UnboundedSender<RpcMessage>,
}
struct RpcMessage {
rpc_json: String,
session: RpcSession,
send_back: oneshot::Sender<Pin<Box<dyn futures::Future<Output = Option<String>> + Send>>>,
}
/// Create a Client object that connects to a service.
pub fn start_client(mut task_manager: TaskManager, rpc_handlers: RpcHandlers) -> Client {
// We dispatch a background task responsible for processing the service.
//
// The main action performed by the code below consists in polling the service with
// `service.poll()`.
// The rest consists in handling RPC requests.
let (rpc_send_tx, rpc_send_rx) = mpsc::unbounded::<RpcMessage>();
wasm_bindgen_futures::spawn_local(
select(
rpc_send_rx.for_each(move |message| {
let fut = rpc_handlers.rpc_query(&message.session, &message.rpc_json);
let _ = message.send_back.send(fut);
ready(())
}),
Box::pin(async move {
let _ = task_manager.future().await;
}),
)
.map(drop),
);
Client { rpc_send_tx }
}
#[wasm_bindgen]
impl Client {
/// Allows starting an RPC request. Returns a `Promise` containing the result of that request.
#[wasm_bindgen(js_name = "rpcSend")]
pub fn rpc_send(&mut self, rpc: &str) -> js_sys::Promise {
let rpc_session = RpcSession::new(mpsc::unbounded().0);
let (tx, rx) = oneshot::channel();
let _ = self.rpc_send_tx.unbounded_send(RpcMessage {
rpc_json: rpc.to_owned(),
session: rpc_session,
send_back: tx,
});
wasm_bindgen_futures::future_to_promise(async {
match rx.await {
Ok(fut) => fut.await.map(|s| JsValue::from_str(&s)).ok_or_else(|| JsValue::NULL),
Err(_) => Err(JsValue::NULL),
}
})
}
/// Subscribes to an RPC pubsub endpoint.
#[wasm_bindgen(js_name = "rpcSubscribe")]
pub fn rpc_subscribe(&mut self, rpc: &str, callback: js_sys::Function) {
let (tx, rx) = mpsc::unbounded();
let rpc_session = RpcSession::new(tx);
let (fut_tx, fut_rx) = oneshot::channel();
let _ = self.rpc_send_tx.unbounded_send(RpcMessage {
rpc_json: rpc.to_owned(),
session: rpc_session.clone(),
send_back: fut_tx,
});
wasm_bindgen_futures::spawn_local(async {
if let Ok(fut) = fut_rx.await {
fut.await;
}
});
wasm_bindgen_futures::spawn_local(async move {
let _ = rx
.for_each(|s| {
let _ = callback.call1(&callback, &JsValue::from_str(&s));
ready(())
})
.await;
// We need to keep `rpc_session` alive.
debug!("RPC subscription has ended");
drop(rpc_session);
});
}
}