chain-api subsystem: implement BlockHeader messsage (#1778)

* chain-api subsystem: implement BlockHeader messsage

* update the guide
This commit is contained in:
Andronik Ordian
2020-10-06 16:46:54 +02:00
committed by GitHub
parent abf76d27dc
commit d00bdfef08
4 changed files with 48 additions and 8 deletions
+32
View File
@@ -22,6 +22,7 @@
//! //!
//! Supported requests: //! Supported requests:
//! * Block hash to number //! * Block hash to number
//! * Block hash to header
//! * Finalized block number to hash //! * Finalized block number to hash
//! * Last finalized block number //! * Last finalized block number
//! * Ancestors //! * Ancestors
@@ -85,6 +86,13 @@ where
subsystem.metrics.on_request(result.is_ok()); subsystem.metrics.on_request(result.is_ok());
let _ = response_channel.send(result); let _ = response_channel.send(result);
}, },
ChainApiMessage::BlockHeader(hash, response_channel) => {
let result = subsystem.client
.header(BlockId::Hash(hash))
.map_err(|e| e.to_string().into());
subsystem.metrics.on_request(result.is_ok());
let _ = response_channel.send(result);
},
ChainApiMessage::FinalizedBlockHash(number, response_channel) => { ChainApiMessage::FinalizedBlockHash(number, response_channel) => {
// Note: we don't verify it's finalized // Note: we don't verify it's finalized
let result = subsystem.client.hash(number).map_err(|e| e.to_string().into()); let result = subsystem.client.hash(number).map_err(|e| e.to_string().into());
@@ -319,6 +327,30 @@ mod tests {
}) })
} }
#[test]
fn request_block_header() {
test_harness(|client, mut sender| {
async move {
const NOT_HERE: Hash = Hash::repeat_byte(0x5);
let test_cases = [
(TWO, client.header(BlockId::Hash(TWO)).unwrap()),
(NOT_HERE, client.header(BlockId::Hash(NOT_HERE)).unwrap()),
];
for (hash, expected) in &test_cases {
let (tx, rx) = oneshot::channel();
sender.send(FromOverseer::Communication {
msg: ChainApiMessage::BlockHeader(*hash, tx),
}).await;
assert_eq!(rx.await.unwrap().unwrap(), *expected);
}
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
}.boxed()
})
}
#[test] #[test]
fn request_finalized_hash() { fn request_finalized_hash() {
test_harness(|client, mut sender| { test_harness(|client, mut sender| {
+12 -8
View File
@@ -31,12 +31,13 @@ use polkadot_node_primitives::{
CollationGenerationConfig, MisbehaviorReport, SignedFullStatement, ValidationResult, CollationGenerationConfig, MisbehaviorReport, SignedFullStatement, ValidationResult,
}; };
use polkadot_primitives::v1::{ use polkadot_primitives::v1::{
AuthorityDiscoveryId, AvailableData, BackedCandidate, BlockNumber, CandidateDescriptor, AuthorityDiscoveryId, AvailableData, BackedCandidate, BlockNumber,
CandidateEvent, CandidateReceipt, CollatorId, CommittedCandidateReceipt, Header as BlockHeader, CandidateDescriptor, CandidateEvent, CandidateReceipt,
CoreState, ErasureChunk, GroupRotationInfo, Hash, Id as ParaId, CollatorId, CommittedCandidateReceipt, CoreState, ErasureChunk,
OccupiedCoreAssumption, PersistedValidationData, PoV, SessionIndex, SignedAvailabilityBitfield, GroupRotationInfo, Hash, Id as ParaId, OccupiedCoreAssumption,
TransientValidationData, ValidationCode, ValidatorId, ValidationData, ValidatorIndex, PersistedValidationData, PoV, SessionIndex, SignedAvailabilityBitfield,
ValidatorSignature, TransientValidationData, ValidationCode, ValidatorId, ValidationData,
ValidatorIndex, ValidatorSignature,
}; };
use std::sync::Arc; use std::sync::Arc;
@@ -336,6 +337,9 @@ pub enum ChainApiMessage {
/// Request the block number by hash. /// Request the block number by hash.
/// Returns `None` if a block with the given hash is not present in the db. /// Returns `None` if a block with the given hash is not present in the db.
BlockNumber(Hash, ChainApiResponseChannel<Option<BlockNumber>>), BlockNumber(Hash, ChainApiResponseChannel<Option<BlockNumber>>),
/// Request the block header by hash.
/// Returns `None` if a block with the given hash is not present in the db.
BlockHeader(Hash, ChainApiResponseChannel<Option<BlockHeader>>),
/// Request the finalized block hash by number. /// Request the finalized block hash by number.
/// Returns `None` if a block with the given number is not present in the db. /// Returns `None` if a block with the given number is not present in the db.
/// Note: the caller must ensure the block is finalized. /// Note: the caller must ensure the block is finalized.
@@ -404,10 +408,10 @@ pub enum RuntimeApiRequest {
/// the block in whose state this request is executed. /// the block in whose state this request is executed.
CandidateEvents(RuntimeApiSender<Vec<CandidateEvent>>), CandidateEvents(RuntimeApiSender<Vec<CandidateEvent>>),
/// Get the `AuthorityDiscoveryId`s corresponding to the given `ValidatorId`s. /// Get the `AuthorityDiscoveryId`s corresponding to the given `ValidatorId`s.
/// Currently this request is limited to validators in the current session. /// Currently this request is limited to validators in the current session.
/// ///
/// Returns `None` for validators not found in the current session. /// Returns `None` for validators not found in the current session.
ValidatorDiscovery(Vec<ValidatorId>, RuntimeApiSender<Vec<Option<AuthorityDiscoveryId>>>), ValidatorDiscovery(Vec<ValidatorId>, RuntimeApiSender<Vec<Option<AuthorityDiscoveryId>>>),
} }
/// A message to the Runtime API subsystem. /// A message to the Runtime API subsystem.
@@ -14,6 +14,7 @@ On receipt of `ChainApiMessage`, answer the request and provide the response to
Currently, the following requests are supported: Currently, the following requests are supported:
* Block hash to number * Block hash to number
* Block hash to header
* Finalized block number to hash * Finalized block number to hash
* Last finalized block number * Last finalized block number
* Ancestors * Ancestors
@@ -140,6 +140,9 @@ enum ChainApiMessage {
/// Get the block number by hash. /// Get the block number by hash.
/// Returns `None` if a block with the given hash is not present in the db. /// Returns `None` if a block with the given hash is not present in the db.
BlockNumber(Hash, ResponseChannel<Result<Option<BlockNumber>, Error>>), BlockNumber(Hash, ResponseChannel<Result<Option<BlockNumber>, Error>>),
/// Request the block header by hash.
/// Returns `None` if a block with the given hash is not present in the db.
BlockHeader(Hash, ResponseChannel<Result<Option<BlockHeader>, Error>>),
/// Get the finalized block hash by number. /// Get the finalized block hash by number.
/// Returns `None` if a block with the given number is not present in the db. /// Returns `None` if a block with the given number is not present in the db.
/// Note: the caller must ensure the block is finalized. /// Note: the caller must ensure the block is finalized.