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 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
bc28c60
Add documentation to signed transactions and actually make them work.
tomusdrw Nov 25, 2019
a645b90
Fix naming and bounds.
tomusdrw Nov 25, 2019
f8c9540
Forgotten import.
tomusdrw Nov 25, 2019
f85b890
Merge branch 'master' into td-signed-transactions
tomusdrw Dec 2, 2019
3f38218
Merge branch 'master' into td-signed-transactions
tomusdrw Dec 3, 2019
d715f64
Remove warning.
tomusdrw Dec 3, 2019
0c4c037
Make accounts optional, fix logic.
tomusdrw Dec 3, 2019
dc8d71c
Split the method to avoid confusing type error message.
tomusdrw Dec 3, 2019
173cf6c
Move executor tests to integration.
tomusdrw Nov 28, 2019
90a00fd
Add submit transactions tests.
tomusdrw Dec 3, 2019
901dbdb
Merge branch 'master' into td-signed-transactions
tomusdrw Dec 10, 2019
d103fa1
Make `submit_transaction` tests compile
HCastano Dec 19, 2019
99c54b7
Remove a file that was accidently committed
HCastano Dec 19, 2019
dbc81dd
Merge branch 'master' into td-signed-transactions
tomusdrw Dec 20, 2019
9eb7a2e
Add can_sign helper function.
tomusdrw Dec 20, 2019
0ccb951
Fix compilation.
tomusdrw Dec 20, 2019
73a936c
Add a key to keystore.
tomusdrw Dec 20, 2019
d776d50
Fix the tests.
tomusdrw Dec 20, 2019
0ac61d7
Remove env_logger.
tomusdrw Dec 20, 2019
8755539
Fix sending multiple transactions.
tomusdrw Dec 20, 2019
fc6b5fe
Remove commented code.
tomusdrw Dec 20, 2019
af1782e
Bring back criterion.
tomusdrw Dec 20, 2019
58673bf
Merge branch 'master' into td-signed-transactions
tomusdrw Dec 23, 2019
bacf48c
Remove stray debug log.
tomusdrw Dec 23, 2019
b3c8726
Merge branch 'master' into td-signed-transactions
gavofyork Jan 3, 2020
da816e2
Apply suggestions from code review
tomusdrw Jan 6, 2020
a2c6d09
Make sure to initialize block correctly.
tomusdrw Jan 8, 2020
af5724c
Initialize block for offchain workers.
tomusdrw Jan 8, 2020
3eda535
Add test for transaction validity.
tomusdrw Jan 8, 2020
765f96f
Merge branch 'master' into td-signed-transactions
tomusdrw Jan 8, 2020
6bafaff
Fix tests.
tomusdrw Jan 8, 2020
021f586
Review suggestions.
tomusdrw Jan 8, 2020
3ac00ff
Remove redundant comment.
tomusdrw Jan 8, 2020
7f38d4c
Make sure to use correct block number of authoring.
tomusdrw Jan 8, 2020
afb99f9
Change the runtime API.
tomusdrw Jan 9, 2020
a945a8c
Support both versions.
tomusdrw Jan 9, 2020
4c66a45
Bump spec version, fix RPC test.
tomusdrw Jan 9, 2020
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
Prev Previous commit
Next Next commit
Change the runtime API.
  • Loading branch information
tomusdrw committed Jan 9, 2020
commit afb99f9b528025d5a13cf16639c9088b6098fb5e
6 changes: 3 additions & 3 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sp_runtime::{
impl_opaque_keys, MultiSignature
};
use sp_runtime::traits::{
NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, ConvertInto, IdentifyAccount
BlakeTwo256, Block as BlockT, StaticLookup, Verify, ConvertInto, IdentifyAccount
};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand Down Expand Up @@ -339,8 +339,8 @@ impl_runtime_apis! {
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(number: NumberFor<Block>) {
Executive::offchain_worker(number)
fn offchain_worker(header: &<Block as BlockT>::Header) {
Executive::offchain_worker(header)
}
}

Expand Down
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sp_runtime::{Permill, Perbill, ApplyExtrinsicResult, impl_opaque_keys, gener
use sp_runtime::curve::PiecewiseLinear;
use sp_runtime::transaction_validity::TransactionValidity;
use sp_runtime::traits::{
self, BlakeTwo256, Block as BlockT, NumberFor, StaticLookup, SaturatedConversion,
self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion,
OpaqueKeys,
};
use sp_version::RuntimeVersion;
Expand Down Expand Up @@ -645,8 +645,8 @@ impl_runtime_apis! {
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(number: NumberFor<Block>) {
Executive::offchain_worker(number)
fn offchain_worker(header: &<Block as BlockT>::Header) {
Executive::offchain_worker(header)
}
}

Expand Down
23 changes: 16 additions & 7 deletions client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use futures::future::Future;
use log::{debug, warn};
use sc_network::NetworkStateInfo;
use sp_core::{offchain::{self, OffchainStorage}, ExecutionContext};
use sp_runtime::{generic::BlockId, traits::{self, ProvideRuntimeApi}};
use sp_runtime::{generic::BlockId, traits::{self, ProvideRuntimeApi, Header}};

mod api;

Expand Down Expand Up @@ -92,12 +92,13 @@ impl<Client, Storage, Block> OffchainWorkers<
#[must_use]
pub fn on_block_imported(
&self,
number: &<Block::Header as traits::Header>::Number,
header: &Block::Header,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
is_validator: bool,
) -> impl Future<Output = ()> {
let runtime = self.client.runtime_api();
let at = BlockId::number(*number);
let at = BlockId::number(*header.number());
// TODO [ToDr] Use has_api_with to support old versions too
let has_api = runtime.has_api::<dyn OffchainWorkerApi<Block, Error = ()>>(&at);
debug!("Checking offchain workers at {:?}: {:?}", at, has_api);

Expand All @@ -108,7 +109,7 @@ impl<Client, Storage, Block> OffchainWorkers<
is_validator,
);
debug!("Spawning offchain workers at {:?}", at);
let number = *number;
let header = header.clone();
let client = self.client.clone();
self.spawn_worker(move || {
let runtime = client.runtime_api();
Expand All @@ -117,7 +118,7 @@ impl<Client, Storage, Block> OffchainWorkers<
let run = runtime.offchain_worker_with_context(
&at,
ExecutionContext::OffchainCall(Some((api, offchain::Capabilities::all()))),
number,
&header,
);
if let Err(e) = run {
log::error!("Error running offchain workers at {:?}: {:?}", at, e);
Expand Down Expand Up @@ -150,6 +151,7 @@ mod tests {
use substrate_test_runtime_client::runtime::Block;
use sc_transaction_pool::{BasicPool, FullChainApi};
use sp_transaction_pool::{TransactionPool, InPoolTransaction};
use sp_runtime::{generic::Header, traits::Header as _};

struct MockNetworkStateInfo();

Expand All @@ -169,7 +171,7 @@ mod tests {
fn submit_at(
&self,
at: &BlockId<Block>,
extrinsic: <Block as sp_runtime::traits::Block>::Extrinsic,
extrinsic: <Block as traits::Block>::Extrinsic,
) -> Result<(), ()> {
futures::executor::block_on(self.0.submit_one(&at, extrinsic))
.map(|_| ())
Expand All @@ -187,10 +189,17 @@ mod tests {
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
let db = sc_client_db::offchain::LocalStorage::new_test();
let network_state = Arc::new(MockNetworkStateInfo());
let header = Header::new(
0u64,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
);

// when
let offchain = OffchainWorkers::new(client, db);
futures::executor::block_on(offchain.on_block_imported(&0u64, network_state, false));
futures::executor::block_on(offchain.on_block_imported(&header, network_state, false));

// then
assert_eq!(pool.0.status().ready, 1);
Expand Down
10 changes: 6 additions & 4 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use sc_rpc;
use sp_api::ConstructRuntimeApi;
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{
Block as BlockT, ProvideRuntimeApi, NumberFor, Header, SaturatedConversion,
Block as BlockT, ProvideRuntimeApi, NumberFor, SaturatedConversion,
};
use sc_executor::{NativeExecutor, NativeExecutionDispatch};
use std::{
Expand Down Expand Up @@ -867,7 +867,6 @@ ServiceBuilder<
let events = client.import_notification_stream()
.map(|v| Ok::<_, ()>(v)).compat()
.for_each(move |notification| {
let number = *notification.header.number();
let txpool = txpool.upgrade();

if let Some(txpool) = txpool.as_ref() {
Expand All @@ -880,8 +879,11 @@ ServiceBuilder<

let offchain = offchain.as_ref().and_then(|o| o.upgrade());
if let Some(offchain) = offchain {
let future = offchain.on_block_imported(&number, network_state_info.clone(), is_validator)
.map(|()| Ok(()));
let future = offchain.on_block_imported(
&notification.header,
network_state_info.clone(),
is_validator
).map(|()| Ok(()));
let _ = to_spawn_tx_.unbounded_send(Box::new(Compat::new(future)));
}

Expand Down
16 changes: 8 additions & 8 deletions frame/executive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" }
sp-io ={ path = "../../primitives/io", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" }
frame-support = { version = "2.0.0", default-features = false, path = "../support" }
frame-system = { version = "2.0.0", default-features = false, path = "../system" }
serde = { version = "1.0.101", optional = true }
sp-io ={ path = "../../primitives/io", default-features = false }
sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" }

[dev-dependencies]
hex-literal = "0.2.1"
Expand All @@ -23,11 +23,11 @@ pallet-transaction-payment = { version = "2.0.0", path = "../transaction-payment
[features]
default = ["std"]
std = [
"sp-std/std",
"codec/std",
"frame-support/std",
"frame-system/std",
"serde",
"codec/std",
"sp-runtime/std",
"sp-io/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
]
42 changes: 29 additions & 13 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use sp_runtime::{
generic::Digest, ApplyExtrinsicResult,
traits::{
self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalize, OnInitialize,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable, Saturating,
},
transaction_validity::TransactionValidity,
};
Expand Down Expand Up @@ -154,9 +154,23 @@ where
{
/// Start the execution of a particular block.
pub fn initialize_block(header: &System::Header) {
let mut digests = <DigestOf<System>>::default();
header.digest().logs().iter().for_each(|d| if d.as_pre_runtime().is_some() { digests.push(d.clone()) });
Self::initialize_block_impl(header.number(), header.parent_hash(), header.extrinsics_root(), &digests);
let digests = Self::extract_pre_digest(&header);
Self::initialize_block_impl(
header.number(),
header.parent_hash(),
header.extrinsics_root(),
&digests
);
}

fn extract_pre_digest(header: &System::Header) -> DigestOf<System> {
let mut digest = <DigestOf<System>>::default();
header.digest().logs()
.iter()
.for_each(|d| if d.as_pre_runtime().is_some() {
digest.push(d.clone())
});
digest
}

fn initialize_block_impl(
Expand Down Expand Up @@ -316,22 +330,24 @@ where
}

/// Start an offchain worker and generate extrinsics.
pub fn offchain_worker(n: System::BlockNumber) {
pub fn offchain_worker(header: &System::Header) {
// We need to keep events available for offchain workers,
// hence we initialize the block manually.
// OffchainWorker RuntimeApi should skip initialization.
let parent_hash = Default::default();
let txs_root = Default::default();
let digest = Default::default();
let digests = Self::extract_pre_digest(header);

<frame_system::Module<System>>::initialize(
&(n + 1.into()),
&parent_hash,
&txs_root,
&digest,
header.number(),
header.parent_hash(),
header.extrinsics_root(),
&digests,
frame_system::InitKind::Inspection,
);
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(n)
<AllModules as OffchainWorker<System::BlockNumber>>::offchain_worker(
// to maintain backward compatibility we call module offchain workers
// with parent block number.
header.number().saturating_sub(1.into())
)
}
}

Expand Down
6 changes: 6 additions & 0 deletions primitives/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ pub const STORAGE_PREFIX: &[u8] = b"storage";

sp_api::decl_runtime_apis! {
/// The offchain worker api.
#[api_version(2)]
pub trait OffchainWorkerApi {
/// Starts the off-chain task for given block number.
#[skip_initialize_block]
#[changed_in(2)]
fn offchain_worker(number: NumberFor<Block>);

/// Starts the off-chain task for given block header.
#[skip_initialize_block]
fn offchain_worker(header: &Block::Header);
}
}
24 changes: 20 additions & 4 deletions primitives/transaction-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ pub trait TransactionPool: Send + Sync {
/// Error type.
type Error: From<crate::error::Error> + crate::error::IntoPoolError;

// Networking

/// Returns a future that imports a bunch of unverified transactions to the pool.
fn submit_at(
&self,
Expand All @@ -183,30 +185,44 @@ pub trait TransactionPool: Send + Sync {
Self::Error
>> + Send + Unpin>;

// RPC

/// Returns a future that import a single transaction and starts to watch their progress in the pool.
fn submit_and_watch(
&self,
at: &BlockId<Self::Block>,
xt: TransactionFor<Self>,
) -> Box<dyn Future<Output=Result<Box<TransactionStatusStreamFor<Self>>, Self::Error>> + Send + Unpin>;


// Block production / Networking

/// Get an iterator for ready transactions ordered by priority
fn ready(&self) -> Box<dyn Iterator<Item=Arc<Self::InPoolTransaction>>>;


// Block production

/// Remove transactions identified by given hashes (and dependent transactions) from the pool.
fn remove_invalid(&self, hashes: &[TxHash<Self>]) -> Vec<Arc<Self::InPoolTransaction>>;

// logging

/// Returns pool status.
fn status(&self) -> PoolStatus;

/// Get an iterator for ready transactions ordered by priority
fn ready(&self) -> Box<dyn Iterator<Item=Arc<Self::InPoolTransaction>>>;
// logging / RPC / networking

/// Return an event stream of transactions imported to the pool.
fn import_notification_stream(&self) -> ImportNotificationStream;

/// Returns transaction hash
fn hash_of(&self, xt: &TransactionFor<Self>) -> TxHash<Self>;
// networking

/// Notify the pool about transactions broadcast.
fn on_broadcasted(&self, propagations: HashMap<TxHash<Self>, Vec<String>>);

/// Returns transaction hash
fn hash_of(&self, xt: &TransactionFor<Self>) -> TxHash<Self>;
}

/// An abstraction for transaction pool.
Expand Down
8 changes: 4 additions & 4 deletions test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,8 @@ cfg_if! {
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(block: u64) {
let ex = Extrinsic::IncludeData(block.encode());
fn offchain_worker(header: &<Block as BlockT>::Header) {
let ex = Extrinsic::IncludeData(header.number.encode());
sp_io::offchain::submit_transaction(ex.encode()).unwrap();
}
}
Expand Down Expand Up @@ -839,8 +839,8 @@ cfg_if! {
}

impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
fn offchain_worker(block: u64) {
let ex = Extrinsic::IncludeData(block.encode());
fn offchain_worker(header: &<Block as BlockT>::Header) {
let ex = Extrinsic::IncludeData(header.number.encode());
sp_io::offchain::submit_transaction(ex.encode()).unwrap()
}
}
Expand Down