mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Run offchain workers at hash, not number. (#4878)
* Run offchain workers at particular hash, not number. * Don't run if not new best. * Don't run if not new best. * Update client/service/src/builder.rs Co-Authored-By: Nikolay Volf <nikvolf@gmail.com> * Update client/service/src/builder.rs Co-Authored-By: Nikolay Volf <nikvolf@gmail.com> * Update client/service/src/builder.rs Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
@@ -320,7 +320,9 @@ pub fn require_header<Block: BlockT>(
|
||||
id: BlockId<Block>,
|
||||
) -> sp_blockchain::Result<Block::Header> {
|
||||
read_header(db, col_index, col, id)
|
||||
.and_then(|header| header.ok_or_else(|| sp_blockchain::Error::UnknownBlock(format!("{}", id))))
|
||||
.and_then(|header| header.ok_or_else(||
|
||||
sp_blockchain::Error::UnknownBlock(format!("Require header: {}", id))
|
||||
))
|
||||
}
|
||||
|
||||
/// Read meta from the database.
|
||||
|
||||
@@ -97,7 +97,7 @@ impl<Client, Storage, Block> OffchainWorkers<
|
||||
is_validator: bool,
|
||||
) -> impl Future<Output = ()> {
|
||||
let runtime = self.client.runtime_api();
|
||||
let at = BlockId::number(*header.number());
|
||||
let at = BlockId::hash(header.hash());
|
||||
let has_api_v1 = runtime.has_api_with::<dyn OffchainWorkerApi<Block, Error = ()>, _>(
|
||||
&at, |v| v == 1
|
||||
);
|
||||
@@ -169,7 +169,6 @@ 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();
|
||||
|
||||
@@ -210,13 +209,7 @@ 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(),
|
||||
);
|
||||
let header = client.header(&BlockId::number(0)).unwrap().unwrap();
|
||||
|
||||
// when
|
||||
let offchain = OffchainWorkers::new(client, db);
|
||||
|
||||
@@ -898,16 +898,24 @@ ServiceBuilder<
|
||||
}
|
||||
|
||||
let offchain = offchain.as_ref().and_then(|o| o.upgrade());
|
||||
if let Some(offchain) = offchain {
|
||||
let future = offchain.on_block_imported(
|
||||
¬ification.header,
|
||||
network_state_info.clone(),
|
||||
is_validator
|
||||
);
|
||||
let _ = to_spawn_tx_.unbounded_send((
|
||||
Box::pin(future),
|
||||
From::from("offchain-on-block")
|
||||
));
|
||||
match offchain {
|
||||
Some(offchain) if notification.is_new_best => {
|
||||
let future = offchain.on_block_imported(
|
||||
¬ification.header,
|
||||
network_state_info.clone(),
|
||||
is_validator,
|
||||
);
|
||||
let _ = to_spawn_tx_.unbounded_send((
|
||||
Box::pin(future),
|
||||
From::from("offchain-on-block"),
|
||||
));
|
||||
},
|
||||
Some(_) => log::debug!(
|
||||
target: "sc_offchain",
|
||||
"Skipping offchain workers for non-canon block: {:?}",
|
||||
notification.header,
|
||||
),
|
||||
_ => {},
|
||||
}
|
||||
|
||||
ready(())
|
||||
|
||||
@@ -59,19 +59,23 @@ pub trait HeaderBackend<Block: BlockT>: Send + Sync {
|
||||
|
||||
/// Get block header. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_header(&self, id: BlockId<Block>) -> Result<Block::Header> {
|
||||
self.header(id)?.ok_or_else(|| Error::UnknownBlock(format!("{}", id)))
|
||||
self.header(id)?.ok_or_else(|| Error::UnknownBlock(format!("Expect header: {}", id)))
|
||||
}
|
||||
|
||||
/// Convert an arbitrary block ID into a block number. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_block_number_from_id(&self, id: &BlockId<Block>) -> Result<NumberFor<Block>> {
|
||||
self.block_number_from_id(id)
|
||||
.and_then(|n| n.ok_or_else(|| Error::UnknownBlock(format!("{}", id))))
|
||||
.and_then(|n| n.ok_or_else(||
|
||||
Error::UnknownBlock(format!("Expect block number from id: {}", id))
|
||||
))
|
||||
}
|
||||
|
||||
/// Convert an arbitrary block ID into a block hash. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_block_hash_from_id(&self, id: &BlockId<Block>) -> Result<Block::Hash> {
|
||||
self.block_hash_from_id(id)
|
||||
.and_then(|n| n.ok_or_else(|| Error::UnknownBlock(format!("{}", id))))
|
||||
.and_then(|n| n.ok_or_else(||
|
||||
Error::UnknownBlock(format!("Expect block hash from id: {}", id))
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user