Make substrate generic (#169)

* Some initial work on RPC and client

* Rephrase as params

* More work on traitifying substrate.

* Traitify in_mem.rs

* traitify client.rs

* Make new primitives (mainly traits) build again.

* Many (superficial) build fixes throughout.

* Fix remaining build issues up to bft interface.

* Make bft primitives be generic.

* Switch out MisBehaviorReport for generic version.

* Merge Hashing into Header.

* Update runtime for new generics (with Hashing).

* Update demo runtime.

* Make runtime compile.

* Build fixes for runtime

* Remove old modules.

* port substrate-bft to use generic substrate types

* port client

* port substrate-test-runtime

* mostly port test-runtime to get compiling for std

* Ensure `AccountId` has a `Default`.

* Fix type deps.

* finish porting

* initialize test_runtime from genesis correctly

* remove commented code

* maybe unsigned signatures

* runtimes compile

* port over most of network

* serialization for generic types

* fix comment

* remove some unnecessary trait bounds

* network compiles

* tests compile for sync

* fix deserialization

* temporarily remove deserialize derives

* workarounds for serde issues for deriving deserialization

* get demo-runtime compiling on std

* port extrinsic-pool

* primitives reshuffling

* get network compiling again

* remove debugging file

* runtime tests now passing

* port client-db

* start to port over substrate-rpc

* mostly port over PolkadotApi

* test_runtime follows normal conventions

* substrate runtime tests pass

* deal with inherent extrinsics correctly in polkadot-api

* port transaction-pool

* port polkadot-consensus

* port substrate-rpc

* everything compiles

* tests compile

* fix grumbles

* test-runtime uses its own transfer type

* switch to master branch of jsonrpc

* fix network tests and some warnings

* all tests pass locally

* [ci-skip] add another comment about issue

* remove some curlies
This commit is contained in:
Gav Wood
2018-06-06 17:58:45 +02:00
committed by Robert Habermeier
parent 4e844760a3
commit b94cf078af
132 changed files with 4695 additions and 4303 deletions
@@ -8,5 +8,4 @@ error-chain = "0.11"
futures = "0.1"
log = "0.3"
parking_lot = "0.4"
substrate-primitives = { path = "../primitives" }
transaction-pool = "1.12"
+8 -10
View File
@@ -16,12 +16,9 @@
//! External API for extrinsic pool.
use std::fmt;
use std::ops::Deref;
use txpool::{self, VerifiedTransaction};
use primitives::{
Hash,
block::{Extrinsic, ExtrinsicHash},
};
/// Extrinsic pool error.
pub trait Error: ::std::error::Error + Send + Sized {
@@ -38,18 +35,19 @@ impl Error for txpool::Error {
}
/// Extrinsic pool.
pub trait ExtrinsicPool: Send + Sync + 'static {
pub trait ExtrinsicPool<Ex, Hash>: Send + Sync + 'static {
/// Error type
type Error: Error;
/// Submit a collection of extrinsics to the pool.
fn submit(&self, xt: Vec<Extrinsic>) -> Result<Vec<ExtrinsicHash>, Self::Error>;
fn submit(&self, xt: Vec<Ex>) -> Result<Vec<Hash>, Self::Error>;
}
// Blanket implementation for anything that `Derefs` to the pool.
impl<V, S, E, T> ExtrinsicPool for T where
T: Deref<Target=super::Pool<V, S, E>> + Send + Sync + 'static,
V: txpool::Verifier<Extrinsic>,
impl<Ex, Hash, V, S, E, T> ExtrinsicPool<Ex, Hash> for T where
Hash: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default,
T: Deref<Target=super::Pool<Ex, Hash, V, S, E>> + Send + Sync + 'static,
V: txpool::Verifier<Ex>,
S: txpool::Scoring<V::VerifiedTransaction>,
V::VerifiedTransaction: txpool::VerifiedTransaction<Hash=Hash>,
E: From<V::Error>,
@@ -58,7 +56,7 @@ impl<V, S, E, T> ExtrinsicPool for T where
{
type Error = E;
fn submit(&self, xt: Vec<Extrinsic>) -> Result<Vec<ExtrinsicHash>, Self::Error> {
fn submit(&self, xt: Vec<Ex>) -> Result<Vec<Hash>, Self::Error> {
self.deref().submit(xt).map(|result| result.into_iter().map(|xt| *xt.hash()).collect())
}
}
@@ -20,7 +20,6 @@
extern crate futures;
extern crate parking_lot;
extern crate substrate_primitives as primitives;
#[macro_use]
extern crate log;
@@ -16,29 +16,29 @@
use std::{
sync::Arc,
fmt,
collections::HashMap,
};
use primitives::Hash;
use txpool;
use watcher;
#[derive(Default)]
pub struct Listener {
watchers: HashMap<Hash, watcher::Sender>
pub struct Listener<H: ::std::hash::Hash + Eq> {
watchers: HashMap<H, watcher::Sender<H>>
}
impl Listener {
pub fn create_watcher<T: txpool::VerifiedTransaction<Hash=Hash>>(&mut self, xt: Arc<T>) -> watcher::Watcher {
impl<H: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default> Listener<H> {
pub fn create_watcher<T: txpool::VerifiedTransaction<Hash=H>>(&mut self, xt: Arc<T>) -> watcher::Watcher<H> {
let sender = self.watchers.entry(*xt.hash()).or_insert_with(watcher::Sender::default);
sender.new_watcher()
}
pub fn broadcasted(&mut self, hash: &Hash, peers: Vec<String>) {
pub fn broadcasted(&mut self, hash: &H, peers: Vec<String>) {
self.fire(hash, |watcher| watcher.broadcast(peers));
}
fn fire<F: FnOnce(&mut watcher::Sender)>(&mut self, hash: &Hash, fun: F) {
fn fire<F>(&mut self, hash: &H, fun: F) where F: FnOnce(&mut watcher::Sender<H>) {
let clean = if let Some(h) = self.watchers.get_mut(hash) {
fun(h);
h.is_done()
@@ -52,7 +52,10 @@ impl Listener {
}
}
impl<T: txpool::VerifiedTransaction<Hash=Hash>> txpool::Listener<T> for Listener {
impl<H, T> txpool::Listener<T> for Listener<H> where
H: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default,
T: txpool::VerifiedTransaction<Hash=H>,
{
fn added(&mut self, tx: &Arc<T>, old: Option<&Arc<T>>) {
if let Some(old) = old {
let hash = tx.hash();
@@ -81,9 +84,7 @@ impl<T: txpool::VerifiedTransaction<Hash=Hash>> txpool::Listener<T> for Listener
fn mined(&mut self, tx: &Arc<T>) {
// TODO [ToDr] latest block number?
let header_hash = 1.into();
let header_hash = Default::default();
self.fire(tx.hash(), |watcher| watcher.finalised(header_hash))
}
}
+11 -9
View File
@@ -16,6 +16,7 @@
use std::{
collections::HashMap,
fmt,
marker::PhantomData,
sync::{Arc, Weak},
};
@@ -23,28 +24,29 @@ use std::{
use futures::sync::mpsc;
use parking_lot::{RwLock, Mutex};
use txpool;
use primitives::{Hash, block::Extrinsic};
use listener::Listener;
use watcher::Watcher;
/// Extrinsics pool.
pub struct Pool<V, S, E> where
V: txpool::Verifier<Extrinsic>,
pub struct Pool<Ex, Hash, V, S, E> where
Hash: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex,
V: txpool::Verifier<Ex>,
S: txpool::Scoring<V::VerifiedTransaction>,
{
_error: Mutex<PhantomData<E>>,
pool: RwLock<txpool::Pool<
V::VerifiedTransaction,
S,
Listener,
Listener<Hash>,
>>,
verifier: V,
import_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<Weak<V::VerifiedTransaction>>>>,
}
impl<V, S, E> Pool<V, S, E> where
V: txpool::Verifier<Extrinsic>,
impl<Ex, Hash, V, S, E> Pool<Ex, Hash, V, S, E> where
Hash: ::std::hash::Hash + Eq + Copy + fmt::Debug + fmt::LowerHex + Default,
V: txpool::Verifier<Ex>,
S: txpool::Scoring<V::VerifiedTransaction>,
V::VerifiedTransaction: txpool::VerifiedTransaction<Hash=Hash>,
E: From<V::Error>,
@@ -86,7 +88,7 @@ impl<V, S, E> Pool<V, S, E> where
}
/// Imports a bunch of extrinsics to the pool
pub fn submit(&self, xts: Vec<Extrinsic>) -> Result<Vec<Arc<V::VerifiedTransaction>>, E> {
pub fn submit(&self, xts: Vec<Ex>) -> Result<Vec<Arc<V::VerifiedTransaction>>, E> {
xts
.into_iter()
.map(|xt| self.verifier.verify_transaction(xt))
@@ -97,7 +99,7 @@ impl<V, S, E> Pool<V, S, E> where
}
/// Import a single extrinsic and starts to watch their progress in the pool.
pub fn submit_and_watch(&self, xt: Extrinsic) -> Result<Watcher, E> {
pub fn submit_and_watch(&self, xt: Ex) -> Result<Watcher<Hash>, E> {
let xt = self.submit(vec![xt])?.pop().expect("One extrinsic passed; one result returned; qed");
Ok(self.pool.write().listener_mut().create_watcher(xt))
}
@@ -122,7 +124,7 @@ impl<V, S, E> Pool<V, S, E> where
/// Cull transactions from the queue and then compute the pending set.
pub fn cull_and_get_pending<R, F, T>(&self, ready: R, f: F) -> T where
R: txpool::Ready<V::VerifiedTransaction> + Clone,
F: FnOnce(txpool::PendingIterator<V::VerifiedTransaction, R, S, Listener>) -> T,
F: FnOnce(txpool::PendingIterator<V::VerifiedTransaction, R, S, Listener<Hash>>) -> T,
{
let mut pool = self.pool.write();
pool.cull(None, ready.clone());
@@ -15,17 +15,14 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use futures::sync::mpsc;
use primitives::{
block::{HeaderHash, ExtrinsicHash}
};
/// Possible extrinsic status events
#[derive(Debug, Clone)]
pub enum Status {
pub enum Status<H> {
/// Extrinsic has been finalised in block with given hash.
Finalised(HeaderHash),
Finalised(H),
/// Some state change (perhaps another extrinsic was included) rendered this extrinsic invalid.
Usurped(ExtrinsicHash),
Usurped(H),
/// The extrinsic has been broadcast to the given peers.
Broadcast(Vec<String>),
/// Extrinsic has been dropped from the pool because of the limit.
@@ -36,19 +33,19 @@ pub enum Status {
///
/// Represents a stream of status updates for particular extrinsic.
#[derive(Debug)]
pub struct Watcher {
receiver: mpsc::UnboundedReceiver<Status>,
pub struct Watcher<H> {
receiver: mpsc::UnboundedReceiver<Status<H>>,
}
#[derive(Debug, Default)]
pub(crate) struct Sender {
receivers: Vec<mpsc::UnboundedSender<Status>>,
pub(crate) struct Sender<H> {
receivers: Vec<mpsc::UnboundedSender<Status<H>>>,
finalised: bool,
}
impl Sender {
impl<H: Clone> Sender<H> {
/// Add a new watcher to this sender object.
pub fn new_watcher(&mut self) -> Watcher {
pub fn new_watcher(&mut self) -> Watcher<H> {
let (tx, receiver) = mpsc::unbounded();
self.receivers.push(tx);
Watcher {
@@ -57,12 +54,12 @@ impl Sender {
}
/// Some state change (perhaps another extrinsic was included) rendered this extrinsic invalid.
pub fn usurped(&mut self, hash: ExtrinsicHash) {
pub fn usurped(&mut self, hash: H) {
self.send(Status::Usurped(hash))
}
/// Extrinsic has been finalised in block with given hash.
pub fn finalised(&mut self, hash: HeaderHash) {
pub fn finalised(&mut self, hash: H) {
self.send(Status::Finalised(hash));
self.finalised = true;
}
@@ -82,7 +79,7 @@ impl Sender {
self.finalised || self.receivers.is_empty()
}
fn send(&mut self, status: Status) {
fn send(&mut self, status: Status<H>) {
self.receivers.retain(|sender| sender.unbounded_send(status.clone()).is_ok())
}
}