Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into wasmtime-real
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork authored Oct 28, 2019
2 parents 812b6f9 + 7389b73 commit ff45239
Show file tree
Hide file tree
Showing 80 changed files with 2,029 additions and 556 deletions.
139 changes: 91 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ members = [
"srml/aura",
"srml/balances",
"srml/contracts",
"srml/contracts/rpc",
"srml/collective",
"srml/democracy",
"srml/elections",
Expand All @@ -96,6 +97,7 @@ members = [
"srml/indices",
"srml/membership",
"srml/metadata",
"srml/nicks",
"srml/offences",
"srml/randomness-collective-flip",
"srml/scored-pool",
Expand All @@ -108,6 +110,7 @@ members = [
"srml/timestamp",
"srml/treasury",
"srml/transaction-payment",
"srml/transaction-payment/rpc",
"srml/utility",
"node/cli",
"node/executor",
Expand Down
3 changes: 3 additions & 0 deletions core/client/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub use state_machine::OverlayedChanges;
#[cfg(feature = "std")]
pub use primitives::NativeOrEncoded;
#[doc(hidden)]
#[cfg(not(feature = "std"))]
pub use primitives::to_substrate_wasm_fn_return_value;
#[doc(hidden)]
pub use sr_primitives::{
traits::{
Block as BlockT, GetNodeBlockType, GetRuntimeBlockType,
Expand Down
2 changes: 1 addition & 1 deletion core/finality-grandpa/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ impl<B: BlockT, N: Network<B>> Clone for NetworkBridge<B, N> {
}
}

fn localized_payload<E: Encode>(round: RoundNumber, set_id: SetIdNumber, message: &E) -> Vec<u8> {
pub(crate) fn localized_payload<E: Encode>(round: RoundNumber, set_id: SetIdNumber, message: &E) -> Vec<u8> {
(message, round, set_id).encode()
}

Expand Down
14 changes: 6 additions & 8 deletions core/finality-grandpa/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,15 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, SC> BlockImport<Block>
_ => {},
}

if !needs_justification && !enacts_consensus_change {
return Ok(ImportResult::Imported(imported_aux));
}

match justification {
Some(justification) => {
self.import_justification(hash, number, justification, needs_justification).unwrap_or_else(|err| {
debug!(target: "finality", "Imported block #{} that enacts authority set change with \
invalid justification: {:?}, requesting justification from peers.", number, err);
imported_aux.bad_justification = true;
imported_aux.needs_justification = true;
if needs_justification || enacts_consensus_change {
debug!(target: "finality", "Imported block #{} that enacts authority set change with \
invalid justification: {:?}, requesting justification from peers.", number, err);
imported_aux.bad_justification = true;
imported_aux.needs_justification = true;
}
});
},
None => {
Expand Down
2 changes: 1 addition & 1 deletion core/finality-grandpa/src/justification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::communication;
/// This is meant to be stored in the db and passed around the network to other
/// nodes, and are used by syncing nodes to prove authority set handoffs.
#[derive(Encode, Decode)]
pub(crate) struct GrandpaJustification<Block: BlockT> {
pub struct GrandpaJustification<Block: BlockT> {
round: u64,
pub(crate) commit: Commit<Block>,
votes_ancestries: Vec<Block::Header>,
Expand Down
1 change: 1 addition & 0 deletions core/finality-grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ mod voting_rule;

pub use communication::Network;
pub use finality_proof::FinalityProofProvider;
pub use justification::GrandpaJustification;
pub use light_import::light_block_import;
pub use observer::run_grandpa_observer;
pub use voting_rule::{
Expand Down
81 changes: 81 additions & 0 deletions core/finality-grandpa/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,84 @@ fn grandpa_environment_respects_voting_rules() {
19,
);
}

#[test]
fn imports_justification_for_regular_blocks_on_import() {
// NOTE: this is a regression test since initially we would only import
// justifications for authority change blocks, and would discard any
// existing justification otherwise.
let peers = &[Ed25519Keyring::Alice];
let voters = make_ids(peers);
let api = TestApi::new(voters);
let mut net = GrandpaTestNet::new(api.clone(), 1);

let client = net.peer(0).client().clone();
let (mut block_import, ..) = net.make_block_import(client.clone());

let full_client = client.as_full().expect("only full clients are used in test");
let builder = full_client.new_block_at(&BlockId::Number(0), Default::default()).unwrap();
let block = builder.bake().unwrap();

let block_hash = block.hash();

// create a valid justification, with one precommit targeting the block
let justification = {
let round = 1;
let set_id = 0;

let precommit = grandpa::Precommit {
target_hash: block_hash,
target_number: *block.header.number(),
};

let msg = grandpa::Message::Precommit(precommit.clone());
let encoded = communication::localized_payload(round, set_id, &msg);
let signature = peers[0].sign(&encoded[..]).into();

let precommit = grandpa::SignedPrecommit {
precommit,
signature,
id: peers[0].public().into(),
};

let commit = grandpa::Commit {
target_hash: block_hash,
target_number: *block.header.number(),
precommits: vec![precommit],
};

GrandpaJustification::from_commit(
&full_client,
round,
commit,
).unwrap()
};

// we import the block with justification attached
let block = BlockImportParams {
origin: BlockOrigin::File,
header: block.header,
justification: Some(justification.encode()),
post_digests: Vec::new(),
body: Some(block.extrinsics),
finalized: false,
auxiliary: Vec::new(),
fork_choice: ForkChoiceStrategy::LongestChain,
};

assert_eq!(
block_import.import_block(block, HashMap::new()).unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: false,
clear_justification_requests: false,
bad_justification: false,
is_new_best: true,
..Default::default()
}),
);

// the justification should be imported and available from the client
assert!(
client.justification(&BlockId::Hash(block_hash)).unwrap().is_some(),
);
}
4 changes: 2 additions & 2 deletions core/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use libp2p::core::{Multiaddr, PeerId, PublicKey};
use libp2p::kad::record;
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess};
use libp2p::core::{nodes::Substream, muxing::StreamMuxerBox};
use log::warn;
use log::{debug, warn};
use sr_primitives::traits::Block as BlockT;
use std::iter;
use void;
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> NetworkBehaviourEventPr
warn!(target: "sub-libp2p", "Connected to a non-Substrate node: {:?}", info);
}
if info.listen_addrs.len() > 30 {
warn!(target: "sub-libp2p", "Node {:?} has reported more than 30 addresses; \
debug!(target: "sub-libp2p", "Node {:?} has reported more than 30 addresses; \
it is identified by {:?} and {:?}", peer_id, info.protocol_version,
info.agent_version
);
Expand Down
45 changes: 44 additions & 1 deletion core/phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ pub fn elect<AccountId, Balance, FS, C>(
let mut assignment = (n.who.clone(), vec![]);
for e in &mut n.edges {
if let Some(c) = elected_candidates.iter().cloned().find(|(c, _)| *c == e.who) {
if c.0 != n.who {
// if self_vote == false, this branch should always be executed as we want to
// collect all nominations
if c.0 != n.who || !self_vote {
let per_bill_parts =
{
if n.load == e.load {
Expand Down Expand Up @@ -360,6 +362,47 @@ pub fn elect<AccountId, Balance, FS, C>(
})
}

/// Build the support map from the given phragmen result.
pub fn build_support_map<Balance, AccountId, FS, C>(
elected_stashes: &Vec<AccountId>,
assignments: &Vec<(AccountId, Vec<PhragmenAssignment<AccountId>>)>,
stake_of: FS,
assume_self_vote: bool,
) -> SupportMap<AccountId> where
AccountId: Default + Ord + Member,
Balance: Default + Copy + SimpleArithmetic,
C: Convert<Balance, u64> + Convert<u128, Balance>,
for<'r> FS: Fn(&'r AccountId) -> Balance,
{
let to_votes = |b: Balance| <C as Convert<Balance, u64>>::convert(b) as ExtendedBalance;
// Initialize the support of each candidate.
let mut supports = <SupportMap<AccountId>>::new();
elected_stashes
.iter()
.map(|e| (e, if assume_self_vote { to_votes(stake_of(e)) } else { Zero::zero() } ))
.for_each(|(e, s)| {
let item = Support { own: s, total: s, ..Default::default() };
supports.insert(e.clone(), item);
});

// build support struct.
for (n, assignment) in assignments.iter() {
for (c, per_thing) in assignment.iter() {
let nominator_stake = to_votes(stake_of(n));
// AUDIT: it is crucially important for the `Mul` implementation of all
// per-things to be sound.
let other_stake = *per_thing * nominator_stake;
if let Some(support) = supports.get_mut(c) {
// For an astronomically rich validator with more astronomically rich
// set of nominators, this might saturate.
support.total = support.total.saturating_add(other_stake);
support.others.push((n.clone(), other_stake));
}
}
}
supports
}

/// Performs equalize post-processing to the output of the election algorithm. This happens in
/// rounds. The number of rounds and the maximum diff-per-round tolerance can be tuned through input
/// parameters.
Expand Down
22 changes: 22 additions & 0 deletions core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,25 @@ impl From<LogLevel> for log::Level {
}
}
}

/// Encodes the given value into a buffer and returns the pointer and the length as a single `u64`.
///
/// When Substrate calls into Wasm it expects a fixed signature for functions exported
/// from the Wasm blob. The return value of this signature is always a `u64`.
/// This `u64` stores the pointer to the encoded return value and the length of this encoded value.
/// The low `32bits` are reserved for the pointer, followed by `32bit` for the length.
#[cfg(not(feature = "std"))]
pub fn to_substrate_wasm_fn_return_value(value: &impl Encode) -> u64 {
let encoded = value.encode();

let ptr = encoded.as_ptr() as u64;
let length = encoded.len() as u64;
let res = ptr | (length << 32);

// Leak the output vector to avoid it being freed.
// This is fine in a WASM context since the heap
// will be discarded after the call.
rstd::mem::forget(encoded);

res
}
3 changes: 0 additions & 3 deletions core/primitives/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ impl std::fmt::Display for Public {
}
}

#[cfg(not(feature = "std"))]
use core as std;

impl rstd::fmt::Debug for Public {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result {
Expand Down
19 changes: 2 additions & 17 deletions core/primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,7 @@ macro_rules! wasm_export_functions {
$( $fn_impl )*
}

// We need to return *something*
let output = Vec::<u8>::new();
let res = output.as_ptr() as u64 + ((output.len() as u64) << 32);

// Leak the output vector to avoid it being freed.
// This is fine in a WASM context since the heap
// will be discarded after the call.
$crate::rstd::mem::forget(output);
res
$crate::to_substrate_wasm_fn_return_value(&())
}
};
(@IMPL
Expand Down Expand Up @@ -232,14 +224,7 @@ macro_rules! wasm_export_functions {
$( $fn_impl )*
};

let output = $crate::Encode::encode(&output);
let res = output.as_ptr() as u64 + ((output.len() as u64) << 32);

// Leak the output vector to avoid it being freed.
// This is fine in a WASM context since the heap
// will be discarded after the call.
$crate::rstd::mem::forget(output);
res
$crate::to_substrate_wasm_fn_return_value(&output)
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions core/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ pub fn rpc_handler<M: PubSubMetadata>(
mod inner {
use super::*;

/// Type alias for http server
pub type HttpServer = http::Server;
/// Type alias for ws server
pub type WsServer = ws::Server;

/// Start HTTP server listening on given address.
Expand Down
2 changes: 0 additions & 2 deletions core/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ client = { package = "substrate-client", path = "../../core/client" }
client_db = { package = "substrate-client-db", path = "../../core/client/db", features = ["kvdb-rocksdb"] }
codec = { package = "parity-scale-codec", version = "1.0.0" }
substrate-executor = { path = "../../core/executor" }
substrate-authority-discovery = { path = "../../core/authority-discovery"}
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }
rpc-servers = { package = "substrate-rpc-servers", path = "../../core/rpc-servers" }
rpc = { package = "substrate-rpc", path = "../../core/rpc" }
tel = { package = "substrate-telemetry", path = "../../core/telemetry" }
offchain = { package = "substrate-offchain", path = "../../core/offchain" }
parity-multiaddr = { package = "parity-multiaddr", version = "0.5.0" }
authority-discovery-primitives = { package = "substrate-authority-discovery-primitives", path = "../authority-discovery/primitives", default-features = false }

[dev-dependencies]
substrate-test-runtime-client = { path = "../test-runtime/client" }
Expand Down
11 changes: 8 additions & 3 deletions core/sr-api-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ state_machine = { package = "substrate-state-machine", path = "../state-machine"
sr-primitives = { path = "../sr-primitives" }
sr-version = { path = "../sr-version" }
primitives = { package = "substrate-primitives", path = "../primitives" }
criterion = "0.2.11"
criterion = "0.3.0"
consensus_common = { package = "substrate-consensus-common", path = "../consensus/common" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
trybuild = "1.0.14"
rustversion = "0.1.4"
trybuild = "1.0.17"
rustversion = "1.0.0"

[[bench]]
name = "bench"
harness = false

# We actually don't need the `std` feature in this crate, but the tests require it.
[features]
default = [ "std" ]
std = []
14 changes: 4 additions & 10 deletions core/sr-api-macros/src/impl_runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ fn generate_impl_call(
)*

#[allow(deprecated)]
let output = <#runtime as #impl_trait>::#fn_name(#( #pborrow #pnames2 ),*);
#c::runtime_api::Encode::encode(&output)
<#runtime as #impl_trait>::#fn_name(#( #pborrow #pnames2 ),*)
)
)
}
Expand Down Expand Up @@ -175,11 +174,12 @@ fn generate_impl_calls(
/// Generate the dispatch function that is used in native to call into the runtime.
fn generate_dispatch_function(impls: &[ItemImpl]) -> Result<TokenStream> {
let data = Ident::new("data", Span::call_site());
let c = generate_crate_access(HIDDEN_INCLUDES_ID);
let impl_calls = generate_impl_calls(impls, &data)?
.into_iter()
.map(|(trait_, fn_name, impl_)| {
let name = prefix_function_with_trait(&trait_, &fn_name);
quote!( #name => Some({ #impl_ }), )
quote!( #name => Some(#c::runtime_api::Encode::encode(&{ #impl_ })), )
});

Ok(quote!(
Expand Down Expand Up @@ -218,13 +218,7 @@ fn generate_wasm_interface(impls: &[ItemImpl]) -> Result<TokenStream> {
};

let output = { #impl_ };
let res = output.as_ptr() as u64 + ((output.len() as u64) << 32);

// Leak the output vector to avoid it being freed.
// This is fine in a WASM context since the heap
// will be discarded after the call.
#c::runtime_api::mem::forget(output);
res
#c::runtime_api::to_substrate_wasm_fn_return_value(&output)
}
)
});
Expand Down
Loading

0 comments on commit ff45239

Please sign in to comment.