mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-30 03:47:25 +00:00
ca5f10567a
The `BlockBuilderProvider` was a trait that was defined in `sc-block-builder`. The trait was implemented for `Client`. This basically meant that you needed to import `sc-block-builder` any way to have access to the block builder. So, this trait was not providing any real value. This pull request is removing the said trait. Instead of the trait it introduces a builder for creating a `BlockBuilder`. The builder currently has the quite fabulous name `BlockBuilderBuilder` (I'm open to any better name 😅). The rest of the pull request is about replacing the old trait with the new builder. # Downstream code changes If you used `new_block` or `new_block_at` before you now need to switch it over to the new `BlockBuilderBuilder` pattern: ```rust // `new` requires a type that implements `CallApiAt`. let mut block_builder = BlockBuilderBuilder::new(client) // Then you need to specify the hash of the parent block the block will be build on top of .on_parent_block(at) // The block builder also needs the block number of the parent block. // Here it is fetched from the given `client` using the `HeaderBackend` // However, there also exists `with_parent_block_number` for directly passing the number .fetch_parent_block_number(client) .unwrap() // Enable proof recording if required. This call is optional. .enable_proof_recording() // Pass the digests. This call is optional. .with_inherent_digests(digests) .build() .expect("Creates new block builder"); ``` --------- Co-authored-by: Sebastian Kunert <skunert49@gmail.com> Co-authored-by: command-bot <>
276 lines
7.9 KiB
Rust
276 lines
7.9 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use super::*;
|
|
use crate::testing::{test_executor, timeout_secs};
|
|
use assert_matches::assert_matches;
|
|
use jsonrpsee::types::EmptyServerParams as EmptyParams;
|
|
use sc_block_builder::BlockBuilderBuilder;
|
|
use sp_consensus::BlockOrigin;
|
|
use sp_rpc::list::ListOrValue;
|
|
use substrate_test_runtime_client::{
|
|
prelude::*,
|
|
runtime::{Block, Header, H256},
|
|
};
|
|
|
|
#[tokio::test]
|
|
async fn should_return_header() {
|
|
let client = Arc::new(substrate_test_runtime_client::new());
|
|
let api = new_full(client.clone(), test_executor()).into_rpc();
|
|
|
|
let res: Header =
|
|
api.call("chain_getHeader", [H256::from(client.genesis_hash())]).await.unwrap();
|
|
assert_eq!(
|
|
res,
|
|
Header {
|
|
parent_hash: H256::from_low_u64_be(0),
|
|
number: 0,
|
|
state_root: res.state_root,
|
|
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
|
|
.parse()
|
|
.unwrap(),
|
|
digest: Default::default(),
|
|
}
|
|
);
|
|
|
|
let res: Header = api.call("chain_getHeader", EmptyParams::new()).await.unwrap();
|
|
assert_eq!(
|
|
res,
|
|
Header {
|
|
parent_hash: H256::from_low_u64_be(0),
|
|
number: 0,
|
|
state_root: res.state_root,
|
|
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
|
|
.parse()
|
|
.unwrap(),
|
|
digest: Default::default(),
|
|
}
|
|
);
|
|
|
|
assert_matches!(
|
|
api.call::<_, Option<Header>>("chain_getHeader", [H256::from_low_u64_be(5)])
|
|
.await
|
|
.unwrap(),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_return_a_block() {
|
|
let mut client = Arc::new(substrate_test_runtime_client::new());
|
|
let api = new_full(client.clone(), test_executor()).into_rpc();
|
|
|
|
let block = BlockBuilderBuilder::new(&*client)
|
|
.on_parent_block(client.chain_info().best_hash)
|
|
.with_parent_block_number(client.chain_info().best_number)
|
|
.build()
|
|
.unwrap()
|
|
.build()
|
|
.unwrap()
|
|
.block;
|
|
let block_hash = block.hash();
|
|
client.import(BlockOrigin::Own, block).await.unwrap();
|
|
|
|
let res: SignedBlock<Block> =
|
|
api.call("chain_getBlock", [H256::from(client.genesis_hash())]).await.unwrap();
|
|
|
|
// Genesis block is not justified
|
|
assert!(res.justifications.is_none());
|
|
|
|
let res: SignedBlock<Block> =
|
|
api.call("chain_getBlock", [H256::from(block_hash)]).await.unwrap();
|
|
assert_eq!(
|
|
res.block,
|
|
Block {
|
|
header: Header {
|
|
parent_hash: client.genesis_hash(),
|
|
number: 1,
|
|
state_root: res.block.header.state_root,
|
|
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
|
|
.parse()
|
|
.unwrap(),
|
|
digest: Default::default(),
|
|
},
|
|
extrinsics: vec![],
|
|
}
|
|
);
|
|
|
|
let res: SignedBlock<Block> = api.call("chain_getBlock", Vec::<H256>::new()).await.unwrap();
|
|
assert_eq!(
|
|
res.block,
|
|
Block {
|
|
header: Header {
|
|
parent_hash: client.genesis_hash(),
|
|
number: 1,
|
|
state_root: res.block.header.state_root,
|
|
extrinsics_root: "03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314"
|
|
.parse()
|
|
.unwrap(),
|
|
digest: Default::default(),
|
|
},
|
|
extrinsics: vec![],
|
|
}
|
|
);
|
|
|
|
assert_matches!(
|
|
api.call::<_, Option<Header>>("chain_getBlock", [H256::from_low_u64_be(5)])
|
|
.await
|
|
.unwrap(),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_return_block_hash() {
|
|
let mut client = Arc::new(substrate_test_runtime_client::new());
|
|
let api = new_full(client.clone(), test_executor()).into_rpc();
|
|
|
|
let res: ListOrValue<Option<H256>> =
|
|
api.call("chain_getBlockHash", EmptyParams::new()).await.unwrap();
|
|
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
|
|
);
|
|
|
|
let res: ListOrValue<Option<H256>> =
|
|
api.call("chain_getBlockHash", [ListOrValue::from(0_u64)]).await.unwrap();
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
|
|
);
|
|
|
|
let res: Option<ListOrValue<Option<H256>>> =
|
|
api.call("chain_getBlockHash", [ListOrValue::from(1_u64)]).await.unwrap();
|
|
assert_matches!(res, None);
|
|
|
|
let block = BlockBuilderBuilder::new(&*client)
|
|
.on_parent_block(client.chain_info().best_hash)
|
|
.with_parent_block_number(client.chain_info().best_number)
|
|
.build()
|
|
.unwrap()
|
|
.build()
|
|
.unwrap()
|
|
.block;
|
|
client.import(BlockOrigin::Own, block.clone()).await.unwrap();
|
|
|
|
let res: ListOrValue<Option<H256>> =
|
|
api.call("chain_getBlockHash", [ListOrValue::from(0_u64)]).await.unwrap();
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::Value(Some(ref x)) if x == &client.genesis_hash()
|
|
);
|
|
|
|
let res: ListOrValue<Option<H256>> =
|
|
api.call("chain_getBlockHash", [ListOrValue::from(1_u64)]).await.unwrap();
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::Value(Some(ref x)) if x == &block.hash()
|
|
);
|
|
|
|
let res: ListOrValue<Option<H256>> = api
|
|
.call("chain_getBlockHash", [ListOrValue::Value(sp_core::U256::from(1_u64))])
|
|
.await
|
|
.unwrap();
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::Value(Some(ref x)) if x == &block.hash()
|
|
);
|
|
|
|
let res: ListOrValue<Option<H256>> = api
|
|
.call("chain_getBlockHash", [ListOrValue::List(vec![0_u64, 1_u64, 2_u64])])
|
|
.await
|
|
.unwrap();
|
|
assert_matches!(
|
|
res,
|
|
ListOrValue::List(list) if list == &[client.genesis_hash().into(), block.hash().into(), None]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_return_finalized_hash() {
|
|
let mut client = Arc::new(substrate_test_runtime_client::new());
|
|
let api = new_full(client.clone(), test_executor()).into_rpc();
|
|
|
|
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
|
|
assert_eq!(res, client.genesis_hash());
|
|
|
|
// import new block
|
|
let block = BlockBuilderBuilder::new(&*client)
|
|
.on_parent_block(client.chain_info().best_hash)
|
|
.with_parent_block_number(client.chain_info().best_number)
|
|
.build()
|
|
.unwrap()
|
|
.build()
|
|
.unwrap()
|
|
.block;
|
|
let block_hash = block.hash();
|
|
client.import(BlockOrigin::Own, block).await.unwrap();
|
|
|
|
// no finalization yet
|
|
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
|
|
assert_eq!(res, client.genesis_hash());
|
|
|
|
// finalize
|
|
client.finalize_block(block_hash, None).unwrap();
|
|
let res: H256 = api.call("chain_getFinalizedHead", EmptyParams::new()).await.unwrap();
|
|
assert_eq!(res, block_hash);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_notify_about_latest_block() {
|
|
test_head_subscription("chain_subscribeAllHeads").await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_notify_about_best_block() {
|
|
test_head_subscription("chain_subscribeNewHeads").await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn should_notify_about_finalized_block() {
|
|
test_head_subscription("chain_subscribeFinalizedHeads").await;
|
|
}
|
|
|
|
async fn test_head_subscription(method: &str) {
|
|
let mut client = Arc::new(substrate_test_runtime_client::new());
|
|
|
|
let mut sub = {
|
|
let api = new_full(client.clone(), test_executor()).into_rpc();
|
|
let sub = api.subscribe(method, EmptyParams::new()).await.unwrap();
|
|
let block = BlockBuilderBuilder::new(&*client)
|
|
.on_parent_block(client.chain_info().best_hash)
|
|
.with_parent_block_number(client.chain_info().best_number)
|
|
.build()
|
|
.unwrap()
|
|
.build()
|
|
.unwrap()
|
|
.block;
|
|
let block_hash = block.hash();
|
|
client.import(BlockOrigin::Own, block).await.unwrap();
|
|
client.finalize_block(block_hash, None).unwrap();
|
|
sub
|
|
};
|
|
|
|
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(Some(_)));
|
|
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(Some(_)));
|
|
|
|
sub.close();
|
|
assert_matches!(timeout_secs(10, sub.next::<Header>()).await, Ok(None));
|
|
}
|