Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 107 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions api/src/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<B: LocalBackend<Block, KeccakHasher, RlpCodec>> PolkadotApi for Client<B, L
}

fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>> {
with_runtime!(self, at, || <::runtime::Staking as AuxLookup>::lookup(address).ok())
with_runtime!(self, at, || <::runtime::Balances as AuxLookup>::lookup(address).ok())
}

fn active_parachains(&self, at: &BlockId) -> Result<Vec<ParaId>> {
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<B: LocalBackend<Block, KeccakHasher, RlpCodec>> PolkadotApi for Client<B, L
let runtime_version = self.runtime_version_at(at)?;

with_runtime!(self, at, || {
let extrinsics = ::runtime::inherent_extrinsics(inherent_data, runtime_version);
let extrinsics = ::runtime::inherent_extrinsics(inherent_data, runtime_version.spec_version);
extrinsics.into_iter()
.map(|x| x.encode()) // get encoded representation
.map(|x| Decode::decode(&mut &x[..])) // get byte-vec equivalent to extrinsic
Expand Down Expand Up @@ -209,10 +209,10 @@ mod tests {
authorities: session_keys(),
}),
system: None,
balances: Some(Default::default()),
session: Some(SessionConfig {
validators: validators(),
session_length: 100,
broken_percent_late: 100,
}),
council: Some(Default::default()),
democracy: Some(Default::default()),
Expand Down
4 changes: 2 additions & 2 deletions collator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ description = "Collator node implementation"
[dependencies]
futures = "0.1.17"
substrate-client = { git = "https://github.com/paritytech/substrate" }
substrate-codec = { git = "https://github.com/paritytech/substrate", version = "0.1" }
substrate-primitives = { git = "https://github.com/paritytech/substrate", version = "0.1" }
substrate-codec = { git = "https://github.com/paritytech/substrate" }
substrate-primitives = { git = "https://github.com/paritytech/substrate" }
polkadot-api = { path = "../api" }
polkadot-runtime = { path = "../runtime", version = "0.1" }
polkadot-primitives = { path = "../primitives", version = "0.1" }
Expand Down
19 changes: 13 additions & 6 deletions consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extern crate substrate_keyring;

use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::{self, Duration, Instant};

use codec::{Decode, Encode};
use extrinsic_store::Store as ExtrinsicStore;
Expand Down Expand Up @@ -274,6 +274,9 @@ impl<C, N, P> bft::Environment<Block> for ProposerFactory<C, N, P>
) -> Result<(Self::Proposer, Self::Input, Self::Output), Error> {
use runtime_primitives::traits::{Hash as HashT, BlakeTwo256};

// force delay in evaluation this long.
const FORCE_DELAY: Timestamp = 5;

let parent_hash = parent_header.hash().into();

let id = BlockId::hash(parent_hash);
Expand Down Expand Up @@ -343,6 +346,7 @@ impl<C, N, P> bft::Environment<Block> for ProposerFactory<C, N, P>
transaction_pool: self.transaction_pool.clone(),
offline: self.offline.clone(),
validators,
minimum_timestamp: current_timestamp() + FORCE_DELAY,
_drop_signal: drop_signal,
};

Expand Down Expand Up @@ -422,6 +426,7 @@ pub struct Proposer<C: PolkadotApi + Send + Sync> {
transaction_pool: Arc<TransactionPool<C>>,
offline: SharedOfflineTracker,
validators: Vec<AccountId>,
minimum_timestamp: u64,
_drop_signal: exit_future::Signal,
}

Expand Down Expand Up @@ -473,6 +478,7 @@ impl<C> bft::Proposer<Block> for Proposer<C>
table: self.table.clone(),
offline: self.offline.clone(),
validators: self.validators.clone(),
minimum_timestamp: self.minimum_timestamp,
timing,
})
}
Expand Down Expand Up @@ -525,9 +531,11 @@ impl<C> bft::Proposer<Block> for Proposer<C>
);

// the duration until the given timestamp is current
let proposed_timestamp = proposal.timestamp();
let proposed_timestamp = ::std::cmp::max(self.minimum_timestamp, proposal.timestamp());
let timestamp_delay = if proposed_timestamp > current_timestamp {
Some(now + Duration::from_secs(proposed_timestamp - current_timestamp))
let delay_s = proposed_timestamp - current_timestamp;
debug!(target: "bft", "Delaying evaluation of proposal for {} seconds", delay_s);
Some(now + Duration::from_secs(delay_s))
} else {
None
};
Expand Down Expand Up @@ -677,8 +685,6 @@ impl<C> bft::Proposer<Block> for Proposer<C>
}

fn current_timestamp() -> Timestamp {
use std::time;

time::SystemTime::now().duration_since(time::UNIX_EPOCH)
.expect("now always later than unix epoch; qed")
.as_secs()
Expand Down Expand Up @@ -732,6 +738,7 @@ pub struct CreateProposal<C: PolkadotApi + Send + Sync> {
timing: ProposalTiming,
validators: Vec<AccountId>,
offline: SharedOfflineTracker,
minimum_timestamp: Timestamp,
}

impl<C> CreateProposal<C> where C: PolkadotApi + Send + Sync {
Expand All @@ -743,7 +750,7 @@ impl<C> CreateProposal<C> where C: PolkadotApi + Send + Sync {
const MAX_VOTE_OFFLINE_SECONDS: Duration = Duration::from_secs(60);

// TODO: handle case when current timestamp behind that in state.
let timestamp = current_timestamp();
let timestamp = ::std::cmp::max(self.minimum_timestamp, current_timestamp());

let elapsed_since_start = self.timing.dynamic_inclusion.started_at().elapsed();
let offline_indices = if elapsed_since_start > MAX_VOTE_OFFLINE_SECONDS {
Expand Down
23 changes: 4 additions & 19 deletions consensus/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use extrinsic_store::Store as ExtrinsicStore;
use tokio::executor::current_thread::TaskExecutor as LocalThreadHandle;
use tokio::runtime::TaskExecutor as ThreadPoolHandle;
use tokio::runtime::current_thread::Runtime as LocalRuntime;
use tokio::timer::{Delay, Interval};
use tokio::timer::Interval;

use super::{Network, Collators, ProposerFactory};
use error;
Expand All @@ -59,25 +59,10 @@ fn start_bft<F, C>(
<F::Proposer as bft::Proposer<Block>>::Error: ::std::fmt::Display + Into<error::Error>,
<F as bft::Environment<Block>>::Error: ::std::fmt::Display
{
const DELAY_UNTIL: Duration = Duration::from_millis(5000);

let mut handle = LocalThreadHandle::current();
match bft_service.build_upon(&header) {
Ok(Some(bft_work)) => {
// do not poll work for some amount of time.
let work = Delay::new(Instant::now() + DELAY_UNTIL).then(move |res| {
if let Err(e) = res {
warn!(target: "bft", "Failed to force delay of consensus: {:?}", e);
}

debug!(target: "bft", "Starting agreement. After forced delay for {:?}",
DELAY_UNTIL);

bft_work
});
if let Err(e) = handle.spawn_local(Box::new(work)) {
warn!(target: "bft", "Couldn't initialize BFT agreement: {:?}", e);
}
Ok(Some(bft_work)) => if let Err(e) = handle.spawn_local(Box::new(bft_work)) {
warn!(target: "bft", "Couldn't initialize BFT agreement: {:?}", e);
}
Ok(None) => trace!(target: "bft", "Could not start agreement on top of {}", header.hash()),
Err(e) => warn!(target: "bft", "BFT agreement error: {}", e),
Expand Down Expand Up @@ -207,7 +192,7 @@ impl Service {
let mut prev_best = match client.best_block_header() {
Ok(header) => header.hash(),
Err(e) => {
warn!("Cant's start consensus service. Error reading best block header: {:?}", e);
warn!("Can't start consensus service. Error reading best block header: {:?}", e);
return;
}
};
Expand Down
Binary file modified parachain/tests/res/adder.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ serde_derive = { version = "1.0", optional = true }
safe-mix = { version = "1.0", default_features = false}
polkadot-primitives = { path = "../primitives", default_features = false }
substrate-codec = { git = "https://github.com/paritytech/substrate" }
substrate-codec-derive = { git = "https://github.com/paritytech/substrate" }
substrate-serializer = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-std = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-io = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-support = { git = "https://github.com/paritytech/substrate" }
substrate-primitives = { git = "https://github.com/paritytech/substrate" }
substrate-keyring = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-balances = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-consensus = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-council = { git = "https://github.com/paritytech/substrate" }
substrate-runtime-democracy = { git = "https://github.com/paritytech/substrate" }
Expand All @@ -36,10 +38,12 @@ default = ["std"]
std = [
"polkadot-primitives/std",
"substrate-codec/std",
"substrate-codec-derive/std",
"substrate-primitives/std",
"substrate-runtime-std/std",
"substrate-runtime-io/std",
"substrate-runtime-support/std",
"substrate-runtime-balances/std",
"substrate-runtime-consensus/std",
"substrate-runtime-council/std",
"substrate-runtime-democracy/std",
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/checked_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::{Call, Block, TIMESTAMP_SET_POSITION, PARACHAINS_SET_POSITION, NOTE_OFFLINE_POSITION};
use timestamp::Call as TimestampCall;
use parachains::Call as ParachainsCall;
use session::Call as SessionCall;
use consensus::Call as ConsensusCall;
use primitives::parachain::CandidateReceipt;

/// Provides a type-safe wrapper around a structurally valid block.
Expand Down Expand Up @@ -90,10 +90,10 @@ impl CheckedBlock {
}
}

/// Extract the noted offline validator indices (if any) from the block.
/// Extract the noted missed proposal validator indices (if any) from the block.
pub fn noted_offline(&self) -> &[u32] {
self.inner.extrinsics.get(NOTE_OFFLINE_POSITION as usize).and_then(|xt| match xt.extrinsic.function {
Call::Session(SessionCall::note_offline(ref x)) => Some(&x[..]),
Call::Consensus(ConsensusCall::note_offline(ref x)) => Some(&x[..]),
_ => None,
}).unwrap_or(&[])
}
Expand Down
55 changes: 39 additions & 16 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ extern crate substrate_primitives;

#[macro_use]
extern crate substrate_runtime_std as rstd;
#[macro_use]
extern crate substrate_codec_derive;

extern crate polkadot_primitives as primitives;
extern crate substrate_codec as codec;
extern crate substrate_runtime_balances as balances;
extern crate substrate_runtime_consensus as consensus;
extern crate substrate_runtime_council as council;
extern crate substrate_runtime_democracy as democracy;
Expand All @@ -67,7 +70,7 @@ mod utils;
#[cfg(feature = "std")]
pub use checked_block::CheckedBlock;
pub use utils::{inherent_extrinsics, check_extrinsic};
pub use staking::address::Address as RawAddress;
pub use balances::address::Address as RawAddress;

use primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Log, SessionKey, Signature};
use runtime_primitives::{generic, traits::{HasPublicAux, BlakeTwo256, Convert}};
Expand All @@ -85,11 +88,11 @@ pub use primitives::Header;
pub const TIMESTAMP_SET_POSITION: u32 = 0;
/// The position of the parachains set extrinsic.
pub const PARACHAINS_SET_POSITION: u32 = 1;
/// The position of the offline nodes noting extrinsic.
/// The position of the note_offline in the block, if it exists.
pub const NOTE_OFFLINE_POSITION: u32 = 2;

/// The address format for describing accounts.
pub type Address = staking::Address<Concrete>;
pub type Address = balances::Address<Concrete>;
/// Block Id type for this block.
pub type BlockId = generic::BlockId<Block>;
/// Unchecked extrinsic type as expected by this runtime.
Expand Down Expand Up @@ -128,20 +131,33 @@ impl HasPublicAux for Concrete {
}

impl system::Trait for Concrete {
type PublicAux = <Concrete as HasPublicAux>::PublicAux;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hashing = BlakeTwo256;
type Digest = generic::Digest<Log>;
type AccountId = AccountId;
type Header = Header;
type Event = Event;
}
/// System module for this concrete runtime.
pub type System = system::Module<Concrete>;

impl balances::Trait for Concrete {
type Balance = Balance;
type AccountIndex = AccountIndex;
type OnFreeBalanceZero = Staking;
type EnsureAccountLiquid = Staking;
type Event = Event;
}
/// Staking module for this concrete runtime.
pub type Balances = balances::Module<Concrete>;

impl consensus::Trait for Concrete {
type PublicAux = <Concrete as HasPublicAux>::PublicAux;
const NOTE_OFFLINE_POSITION: u32 = NOTE_OFFLINE_POSITION;
type SessionKey = SessionKey;
type OnOfflineValidator = Staking;
}
/// Consensus module for this concrete runtime.
pub type Consensus = consensus::Module<Concrete>;
Expand All @@ -162,17 +178,15 @@ impl Convert<AccountId, SessionKey> for SessionKeyConversion {
}

impl session::Trait for Concrete {
const NOTE_OFFLINE_POSITION: u32 = NOTE_OFFLINE_POSITION;
type ConvertAccountIdToSessionKey = SessionKeyConversion;
type OnSessionChange = Staking;
type Event = Event;
}
/// Session module for this concrete runtime.
pub type Session = session::Module<Concrete>;

impl staking::Trait for Concrete {
type Balance = Balance;
type AccountIndex = AccountIndex;
type OnAccountKill = ();
type Event = Event;
}
/// Staking module for this concrete runtime.
pub type Staking = staking::Module<Concrete>;
Expand All @@ -196,15 +210,22 @@ impl parachains::Trait for Concrete {
}
pub type Parachains = parachains::Module<Concrete>;

impl_outer_event! {
pub enum Event for Concrete {
balances, session, staking
}
}

impl_outer_dispatch! {
/// Call type for polkadot transactions.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum Call where aux: <Concrete as HasPublicAux>::PublicAux {
Consensus = 0,
Session = 1,
Staking = 2,
Timestamp = 3,
Balances = 1,
Session = 2,
Staking = 3,
Timestamp = 4,
Democracy = 5,
Council = 6,
CouncilVoting = 7,
Expand All @@ -216,8 +237,9 @@ impl_outer_dispatch! {
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
pub enum PrivCall {
Consensus = 0,
Session = 1,
Staking = 2,
Balances = 1,
Session = 2,
Staking = 3,
Democracy = 5,
Council = 6,
CouncilVoting = 7,
Expand All @@ -226,13 +248,14 @@ impl_outer_dispatch! {
}

/// Executive: handles dispatch to the various modules.
pub type Executive = executive::Executive<Concrete, Block, Staking, Staking,
pub type Executive = executive::Executive<Concrete, Block, Balances, Balances,
(((((((), Parachains), Council), Democracy), Staking), Session), Timestamp)>;

impl_outer_config! {
pub struct GenesisConfig for Concrete {
ConsensusConfig => consensus,
SystemConfig => system,
BalancesConfig => balances,
SessionConfig => session,
StakingConfig => staking,
DemocracyConfig => democracy,
Expand All @@ -250,7 +273,7 @@ pub mod api {
apply_extrinsic => |extrinsic| super::Executive::apply_extrinsic(extrinsic),
execute_block => |block| super::Executive::execute_block(block),
finalise_block => |()| super::Executive::finalise_block(),
inherent_extrinsics => |(inherent, version)| super::inherent_extrinsics(inherent, version),
inherent_extrinsics => |(inherent, spec_version)| super::inherent_extrinsics(inherent, spec_version),
validator_count => |()| super::Session::validator_count(),
validators => |()| super::Session::validators()
);
Expand Down Expand Up @@ -380,7 +403,7 @@ mod tests {
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

let v = Encode::encode(&tx);
assert_eq!(&v[..], &hex!["6f000000ff0101010101010101010101010101010101010101010101010101010101010101e70300000300df0f02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"][..]);
assert_eq!(&v[..], &hex!["6f000000ff0101010101010101010101010101010101010101010101010101010101010101e70300000400df0f02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"][..]);
println!("{}", HexDisplay::from(&v));
assert_eq!(UncheckedExtrinsic::decode(&mut &v[..]).unwrap(), tx);
}
Expand Down
Loading