Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
Added MessageValidatorFactory (#425)
Browse files Browse the repository at this point in the history
This has required the MessageValidatorFactory used as part of the
RoundChangeMessageValidator to be renamed, which in turn has had
renaming knock on effects through the code.
  • Loading branch information
rain-on authored Dec 17, 2018
1 parent d793a7e commit 0e0aa98
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorFactory;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorForHeightFactory;
import tech.pegasys.pantheon.ethereum.core.Address;

import java.util.Collection;
Expand Down Expand Up @@ -85,7 +85,7 @@ public RoundChangeCertificate createRoundChangeCertificate() {
public RoundChangeManager(
final long sequenceNumber,
final Collection<Address> validators,
final MessageValidatorFactory messageValidityFactory) {
final MessageValidatorForHeightFactory messageValidityFactory) {
this.quorumSize = IbftHelpers.calculateRequiredValidatorQuorum(validators.size());
this.roundChangeMessageValidator =
new RoundChangeMessageValidator(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.consensus.ibft.validation;

import tech.pegasys.pantheon.consensus.ibft.ConsensusRoundIdentifier;
import tech.pegasys.pantheon.consensus.ibft.IbftContext;
import tech.pegasys.pantheon.consensus.ibft.IbftHelpers;
import tech.pegasys.pantheon.consensus.ibft.blockcreation.ProposerSelector;
import tech.pegasys.pantheon.ethereum.ProtocolContext;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.mainnet.BlockHeaderValidator;

import java.util.Collection;

public class MessageValidatorFactory {

private final ProposerSelector proposerSelector;
private final BlockHeaderValidator<IbftContext> blockHeaderValidator;
private final ProtocolContext<IbftContext> protocolContext;

public MessageValidatorFactory(
final ProposerSelector proposerSelector,
final BlockHeaderValidator<IbftContext> blockHeaderValidator,
final ProtocolContext<IbftContext> protocolContext) {
this.proposerSelector = proposerSelector;
this.blockHeaderValidator = blockHeaderValidator;
this.protocolContext = protocolContext;
}

public MessageValidator createMessageValidator(
final ConsensusRoundIdentifier roundIdentifier, final BlockHeader parentHeader) {
return new MessageValidator(
protocolContext.getConsensusState().getVoteTally().getValidators(),
proposerSelector.selectProposerForRound(roundIdentifier),
roundIdentifier,
blockHeaderValidator,
protocolContext,
parentHeader);
}

public RoundChangeMessageValidator createRoundChangeMessageValidator(
final BlockHeader parentHeader) {
final Collection<Address> validators =
protocolContext.getConsensusState().getVoteTally().getValidators();
return new RoundChangeMessageValidator(
roundIdentifier -> createMessageValidator(roundIdentifier, parentHeader),
validators,
IbftHelpers.calculateRequiredValidatorQuorum(validators.size()),
parentHeader.getNumber() + 1);
}

public NewRoundMessageValidator createNewRoundValidator(final BlockHeader parentHeader) {
final Collection<Address> validators =
protocolContext.getConsensusState().getVoteTally().getValidators();
return new NewRoundMessageValidator(
validators,
proposerSelector,
roundIdentifier -> createMessageValidator(roundIdentifier, parentHeader),
IbftHelpers.calculateRequiredValidatorQuorum(validators.size()),
parentHeader.getNumber() + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangeCertificate;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorFactory;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorForHeightFactory;
import tech.pegasys.pantheon.ethereum.core.Address;

import java.util.Collection;
Expand All @@ -35,14 +35,14 @@ public class NewRoundMessageValidator {

private final Collection<Address> validators;
private final ProposerSelector proposerSelector;
private final MessageValidatorFactory messageValidatorFactory;
private final MessageValidatorForHeightFactory messageValidatorFactory;
private final long quorumSize;
private final long chainHeight;

public NewRoundMessageValidator(
final Collection<Address> validators,
final ProposerSelector proposerSelector,
final MessageValidatorFactory messageValidatorFactory,
final MessageValidatorForHeightFactory messageValidatorFactory,
final long quorumSize,
final long chainHeight) {
this.validators = validators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public class RoundChangeMessageValidator {

private static final Logger LOG = LogManager.getLogger();

private final MessageValidatorFactory messageValidatorFactory;
private final MessageValidatorForHeightFactory messageValidatorFactory;
private final Collection<Address> validators;
private final long minimumPrepareMessages;
private final long chainHeight;

public RoundChangeMessageValidator(
final MessageValidatorFactory messageValidatorFactory,
final MessageValidatorForHeightFactory messageValidatorFactory,
final Collection<Address> validators,
final long minimumPrepareMessages,
final long chainHeight) {
Expand Down Expand Up @@ -129,8 +129,7 @@ private boolean validatePreparedCertificateRound(
}

@FunctionalInterface
public interface MessageValidatorFactory {

public interface MessageValidatorForHeightFactory {
MessageValidator createAt(final ConsensusRoundIdentifier roundIdentifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangeCertificate;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorFactory;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorForHeightFactory;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Block;
Expand Down Expand Up @@ -59,7 +59,8 @@ public class NewRoundMessageValidatorTest {

private final Block proposedBlock = mock(Block.class);
private final ProposerSelector proposerSelector = mock(ProposerSelector.class);
private final MessageValidatorFactory validatorFactory = mock(MessageValidatorFactory.class);
private final MessageValidatorForHeightFactory validatorFactory =
mock(MessageValidatorForHeightFactory.class);
private final MessageValidator messageValidator = mock(MessageValidator.class);

private final SignedData<NewRoundPayload> validMsg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparedCertificate;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorFactory;
import tech.pegasys.pantheon.consensus.ibft.validation.RoundChangeMessageValidator.MessageValidatorForHeightFactory;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Block;
Expand Down Expand Up @@ -60,7 +60,8 @@ public class RoundChangeMessageValidatorTest {
private final MessageValidator basicValidator = mock(MessageValidator.class);
private final List<Address> validators = Lists.newArrayList();

private final MessageValidatorFactory validatorFactory = mock(MessageValidatorFactory.class);
private final MessageValidatorForHeightFactory validatorFactory =
mock(MessageValidatorForHeightFactory.class);
private final RoundChangeMessageValidator validator =
new RoundChangeMessageValidator(validatorFactory, validators, 1, chainHeight);

Expand Down

0 comments on commit 0e0aa98

Please sign in to comment.