#![allow(missing_docs)]
use std::sync::Arc;
use crate::{
BlockBuilderExt, ClientBlockImportExt, Sr25519Keyring, TestClientBuilder, TestClientBuilderExt,
};
use futures::executor::block_on;
use sc_block_builder::BlockBuilderBuilder;
use sc_client_api::{
backend,
blockchain::{Backend as BlockChainBackendT, HeaderBackend},
};
use sp_consensus::BlockOrigin;
use sp_runtime::traits::Block as BlockT;
use substrate_test_runtime::Transfer;
pub fn test_leaves_for_backend<B: 'static>(backend: Arc<B>)
where
B: backend::Backend<substrate_test_runtime::Block>,
{
let client = TestClientBuilder::with_backend(backend.clone()).build();
let blockchain = backend.blockchain();
let genesis_hash = client.chain_info().genesis_hash;
assert_eq!(blockchain.leaves().unwrap(), vec![genesis_hash]);
let a1 = BlockBuilderBuilder::new(&client)
.on_parent_block(genesis_hash)
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a1.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a1.hash()]);
let a2 = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a2.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a2.hash()]);
let a3 = BlockBuilderBuilder::new(&client)
.on_parent_block(a2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a3.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a3.hash()]);
let a4 = BlockBuilderBuilder::new(&client)
.on_parent_block(a3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a4.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a4.hash()]);
let a5 = BlockBuilderBuilder::new(&client)
.on_parent_block(a4.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a5.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash()]);
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 41,
nonce: 0,
})
.unwrap();
let b2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, b2.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash(), b2.hash()]);
let b3 = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b3.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash(), b3.hash()]);
let b4 = BlockBuilderBuilder::new(&client)
.on_parent_block(b3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b4.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash(), b4.hash()]);
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 1,
})
.unwrap();
let c3 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, c3.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash(), b4.hash(), c3.hash()]);
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 0,
})
.unwrap();
let d2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, d2.clone())).unwrap();
assert_eq!(blockchain.leaves().unwrap(), vec![a5.hash(), b4.hash(), c3.hash(), d2.hash()]);
}
pub fn test_children_for_backend<B: 'static>(backend: Arc<B>)
where
B: backend::LocalBackend<substrate_test_runtime::Block>,
{
let client = TestClientBuilder::with_backend(backend.clone()).build();
let blockchain = backend.blockchain();
let genesis_hash = client.chain_info().genesis_hash;
let a1 = BlockBuilderBuilder::new(&client)
.on_parent_block(genesis_hash)
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a1.clone())).unwrap();
let a2 = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a2.clone())).unwrap();
let a3 = BlockBuilderBuilder::new(&client)
.on_parent_block(a2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a3.clone())).unwrap();
let a4 = BlockBuilderBuilder::new(&client)
.on_parent_block(a3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a4.clone())).unwrap();
let a5 = BlockBuilderBuilder::new(&client)
.on_parent_block(a4.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a5.clone())).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 41,
nonce: 0,
})
.unwrap();
let b2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, b2.clone())).unwrap();
let b3 = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b3.clone())).unwrap();
let b4 = BlockBuilderBuilder::new(&client)
.on_parent_block(b3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b4)).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 1,
})
.unwrap();
let c3 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, c3.clone())).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 0,
})
.unwrap();
let d2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, d2.clone())).unwrap();
let genesis_hash = client.chain_info().genesis_hash;
let children1 = blockchain.children(a4.hash()).unwrap();
assert_eq!(vec![a5.hash()], children1);
let children2 = blockchain.children(a1.hash()).unwrap();
assert_eq!(vec![a2.hash(), b2.hash(), d2.hash()], children2);
let children3 = blockchain.children(genesis_hash).unwrap();
assert_eq!(vec![a1.hash()], children3);
let children4 = blockchain.children(b2.hash()).unwrap();
assert_eq!(vec![b3.hash(), c3.hash()], children4);
}
pub fn test_blockchain_query_by_number_gets_canonical<B: 'static>(backend: Arc<B>)
where
B: backend::LocalBackend<substrate_test_runtime::Block>,
{
let client = TestClientBuilder::with_backend(backend.clone()).build();
let blockchain = backend.blockchain();
let genesis_hash = client.chain_info().genesis_hash;
let a1 = BlockBuilderBuilder::new(&client)
.on_parent_block(genesis_hash)
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a1.clone())).unwrap();
let a2 = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a2.clone())).unwrap();
let a3 = BlockBuilderBuilder::new(&client)
.on_parent_block(a2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a3.clone())).unwrap();
let a4 = BlockBuilderBuilder::new(&client)
.on_parent_block(a3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a4.clone())).unwrap();
let a5 = BlockBuilderBuilder::new(&client)
.on_parent_block(a4.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, a5.clone())).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 41,
nonce: 0,
})
.unwrap();
let b2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, b2.clone())).unwrap();
let b3 = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b3.clone())).unwrap();
let b4 = BlockBuilderBuilder::new(&client)
.on_parent_block(b3.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap()
.build()
.unwrap()
.block;
block_on(client.import(BlockOrigin::Own, b4)).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(b2.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 1,
})
.unwrap();
let c3 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, c3)).unwrap();
let mut builder = BlockBuilderBuilder::new(&client)
.on_parent_block(a1.hash())
.fetch_parent_block_number(&client)
.unwrap()
.build()
.unwrap();
builder
.push_transfer(Transfer {
from: Sr25519Keyring::Alice.into(),
to: Sr25519Keyring::Ferdie.into(),
amount: 1,
nonce: 0,
})
.unwrap();
let d2 = builder.build().unwrap().block;
block_on(client.import(BlockOrigin::Own, d2)).unwrap();
let genesis_hash = client.chain_info().genesis_hash;
assert_eq!(blockchain.hash(0).unwrap().unwrap(), genesis_hash);
assert_eq!(blockchain.hash(1).unwrap().unwrap(), a1.hash());
assert_eq!(blockchain.hash(2).unwrap().unwrap(), a2.hash());
assert_eq!(blockchain.hash(3).unwrap().unwrap(), a3.hash());
assert_eq!(blockchain.hash(4).unwrap().unwrap(), a4.hash());
assert_eq!(blockchain.hash(5).unwrap().unwrap(), a5.hash());
}