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
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
WIP
Forked at: 8f02e23
Parent branch: origin/master
  • Loading branch information
cecton committed May 27, 2020
commit bf53b31eab848f06eb5acad0ab193009fd83254c
7 changes: 5 additions & 2 deletions collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ where
+ Sync
+ 'static,
Backend: sc_client_api::Backend<Block> + 'static,
Client: Finalizer<Block, Backend> + UsageProvider<Block> + Send + Sync + 'static,
Client: Finalizer<Block, Backend> + UsageProvider<Block> + HeaderBackend<Block>
+ Send
+ Sync
+ 'static,
{
type ParachainContext = Collator<Block, PF, BI>;

Expand All @@ -359,7 +362,7 @@ where
Extrinsic: codec::Codec + Send + Sync + 'static,
{
self.delayed_block_announce_validator.set(
Box::new(JustifiedBlockAnnounceValidator::new(polkadot_client.clone())),
Box::new(JustifiedBlockAnnounceValidator::new(polkadot_client.clone(), self.client.clone())),
);

let follow =
Expand Down
26 changes: 21 additions & 5 deletions network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod tests;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as ClientError, HeaderBackend};
use sp_consensus::block_validation::{BlockAnnounceValidator, Validation};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_runtime::{generic::BlockId, traits::{Block as BlockT, Header as HeaderT}};

use polkadot_collator::Network as CollatorNetwork;
use polkadot_network::legacy::gossip::{GossipMessage, GossipStatement};
Expand All @@ -51,24 +51,27 @@ use parking_lot::Mutex;
/// the justification.
///
/// Note: if no justification is provided the annouce is considered valid.
pub struct JustifiedBlockAnnounceValidator<B, P> {
pub struct JustifiedBlockAnnounceValidator<B, P, C> {
phantom: PhantomData<B>,
polkadot_client: Arc<P>,
parachain_client: Arc<C>,
}

impl<B, P> JustifiedBlockAnnounceValidator<B, P> {
pub fn new(polkadot_client: Arc<P>) -> Self {
impl<B, P, C> JustifiedBlockAnnounceValidator<B, P, C> {
pub fn new(polkadot_client: Arc<P>, parachain_client: Arc<C>) -> Self {
Self {
phantom: Default::default(),
polkadot_client,
parachain_client,
}
}
}

impl<B: BlockT, P> BlockAnnounceValidator<B> for JustifiedBlockAnnounceValidator<B, P>
impl<B: BlockT, P, C> BlockAnnounceValidator<B> for JustifiedBlockAnnounceValidator<B, P, C>
where
P: ProvideRuntimeApi<PBlock> + HeaderBackend<PBlock>,
P::Api: ParachainHost<PBlock>,
C: HeaderBackend<B>,
{
fn validate(
&mut self,
Expand All @@ -80,6 +83,19 @@ where
return Ok(Validation::Success);
}

// Check if block is one higher than best
let best_number = self.parachain_client.info().best_number;
let block_number = header.number();

if !(block_number == best_number + 1 || block_number == best_number) {
trace!(
target: "cumulus-network",
"validation failed because the block number is not the best block number or one higher",
);

return Ok(Validation::Failure);
}

// Check data is a gossip message.
let gossip_message = GossipMessage::decode(&mut data).map_err(|_| {
Box::new(ClientError::BadJustification(
Expand Down
62 changes: 58 additions & 4 deletions network/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use super::*;
use cumulus_test_runtime::{Block, Header};
use cumulus_test_runtime::{Block, Header, Hash};
use polkadot_primitives::{
parachain::{
AbridgedCandidateReceipt, Chain, CollatorId, DutyRoster, GlobalValidationSchedule,
Expand All @@ -35,20 +35,22 @@ use sp_core::H256;
use sp_keyring::Sr25519Keyring;
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero};

fn make_validator() -> JustifiedBlockAnnounceValidator<Block, TestApi> {
fn make_validator() -> JustifiedBlockAnnounceValidator<Block, TestApi, TestApi> {
let (validator, _client) = make_validator_and_client();

validator
}

fn make_validator_and_client() -> (
JustifiedBlockAnnounceValidator<Block, TestApi>,
JustifiedBlockAnnounceValidator<Block, TestApi, TestApi>,
Arc<TestApi>,
) {
let builder = TestClientBuilder::new();
let client = Arc::new(TestApi::new(Arc::new(builder.build())));
//let builderc = TestClientBuilderC::new();
//let clientc = Arc::new(TestApiC::new(Arc::new(builderc.build())));

(JustifiedBlockAnnounceValidator::new(client.clone()), client)
(JustifiedBlockAnnounceValidator::new(client.clone(), client.clone()), client)
}

fn default_header() -> Header {
Expand Down Expand Up @@ -466,3 +468,55 @@ impl HeaderBackend<PBlock> for TestApi {
Ok(None)
}
}

/// Blockchain database header backend. Does not perform any validation.
impl HeaderBackend<Block> for TestApi {
fn header(
&self,
_id: BlockId<Block>,
) -> std::result::Result<Option<Header>, sp_blockchain::Error> {
Ok(None)
}

fn info(&self) -> sc_client_api::blockchain::Info<Block> {
let best_hash = H256::from_low_u64_be(1);

sc_client_api::blockchain::Info {
best_hash,
best_number: 1,
finalized_hash: Default::default(),
finalized_number: Zero::zero(),
genesis_hash: Default::default(),
number_leaves: Default::default(),
}
}

fn status(
&self,
_id: BlockId<Block>,
) -> std::result::Result<sc_client_api::blockchain::BlockStatus, sp_blockchain::Error> {
Ok(sc_client_api::blockchain::BlockStatus::Unknown)
}

fn number(
&self,
hash: Hash,
) -> std::result::Result<Option<NumberFor<Block>>, sp_blockchain::Error> {
if hash == H256::zero() {
Ok(Some(0))
} else if hash == H256::from_low_u64_be(1) {
Ok(Some(1))
} else if hash == H256::from_low_u64_be(0xdead) {
Err(sp_blockchain::Error::Backend("dead".to_string()))
} else {
Ok(None)
}
}

fn hash(
&self,
_number: NumberFor<Block>,
) -> std::result::Result<Option<Hash>, sp_blockchain::Error> {
Ok(None)
}
}