@ -17,23 +17,17 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.when ;
import tech.pegasys.pantheon.consensus.clique.CliqueBlockHashing ;
import tech.pegasys.pantheon.consensus.clique.CliqueContext ;
import tech.pegasys.pantheon.consensus.clique.CliqueExtraData ;
import tech.pegasys.pantheon.consensus.clique.TestHelpers ;
import tech.pegasys.pantheon.consensus.clique.VoteTallyCache ;
import tech.pegasys.pantheon.consensus.common.VoteProposer ;
import tech.pegasys.pantheon.consensus.common.VoteTally ;
import tech.pegasys.pantheon.crypto.SECP256K1 ;
import tech.pegasys.pantheon.crypto.SECP256K1.KeyPair ;
import tech.pegasys.pantheon.crypto.SECP256K1.Signature ;
import tech.pegasys.pantheon.ethereum.ProtocolContext ;
import tech.pegasys.pantheon.ethereum.core.Address ;
import tech.pegasys.pantheon.ethereum.core.AddressHelpers ;
import tech.pegasys.pantheon.ethereum.core.BlockHeader ;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture ;
import tech.pegasys.pantheon.ethereum.core.Hash ;
import tech.pegasys.pantheon.ethereum.core.Util ;
import tech.pegasys.pantheon.util.bytes.BytesValue ;
import tech.pegasys.pantheon.util.uint.UInt256 ;
import java.util.List ;
@ -45,15 +39,15 @@ import org.junit.Test;
public class CliqueDifficultyValidationRuleTest {
private final KeyPair proposerKeyPair = KeyPair . generate ( ) ;
private final KeyPair otherKeyPair = KeyPair . generate ( ) ;
private final List < Address > validatorList = Lists . newArrayList ( ) ;
private ProtocolContext < CliqueContext > cliqueProtocolContext ;
private BlockHeaderTestFixture blockHeaderBuilder ;
@Before
public void setup ( ) {
final Address localAddress = Util . publicKeyToAddress ( proposerKeyPair . getPublicKey ( ) ) ;
validatorList . add ( localAddress ) ;
validatorList . add ( AddressHelpers . calculateAddressWithRespectTo ( localAddress , 1 ) ) ;
validatorList . add ( Util . publicKeyToAddress ( proposerKeyPair . getPublicKey ( ) ) ) ;
validatorList . add ( Util . publicKeyToAddress ( otherKeyPair . getPublicKey ( ) ) ) ;
final VoteTallyCache voteTallyCache = mock ( VoteTallyCache . class ) ;
when ( voteTallyCache . getVoteTallyAtBlock ( any ( ) ) ) . thenReturn ( new VoteTally ( validatorList ) ) ;
@ -62,29 +56,7 @@ public class CliqueDifficultyValidationRuleTest {
final CliqueContext cliqueContext = new CliqueContext ( voteTallyCache , voteProposer , null ) ;
cliqueProtocolContext = new ProtocolContext < > ( null , null , cliqueContext ) ;
blockHeaderBuilder = new BlockHeaderTestFixture ( ) ;
}
private BlockHeader createCliqueSignedBlock ( final BlockHeaderTestFixture blockHeaderBuilder ) {
final CliqueExtraData unsignedExtraData =
new CliqueExtraData ( BytesValue . wrap ( new byte [ 32 ] ) , null , validatorList ) ;
blockHeaderBuilder . extraData ( unsignedExtraData . encode ( ) ) ;
final Hash signingHash =
CliqueBlockHashing . calculateDataHashForProposerSeal (
blockHeaderBuilder . buildHeader ( ) , unsignedExtraData ) ;
final Signature proposerSignature = SECP256K1 . sign ( signingHash , proposerKeyPair ) ;
final CliqueExtraData signedExtraData =
new CliqueExtraData (
unsignedExtraData . getVanityData ( ) ,
proposerSignature ,
unsignedExtraData . getValidators ( ) ) ;
blockHeaderBuilder . extraData ( signedExtraData . encode ( ) ) ;
return blockHeaderBuilder . buildHeader ( ) ;
blockHeaderBuilder . number ( 2 ) ;
}
@Test
@ -92,11 +64,13 @@ public class CliqueDifficultyValidationRuleTest {
final long IN_TURN_BLOCK_NUMBER = validatorList . size ( ) ; // i.e. proposer is 'in turn'
final UInt256 REPORTED_DIFFICULTY = UInt256 . of ( 2 ) ;
blockHeaderBuilder . number ( IN_TURN_BLOCK_NUMBER - 1L ) ;
final BlockHeader parentHeader = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader parentHeader =
TestHelpers . createCliqueSignedBlockHeader ( blockHeaderBuilder , otherKeyPair , validatorList ) ;
blockHeaderBuilder . number ( IN_TURN_BLOCK_NUMBER ) . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader newBlock =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule ( ) ;
assertThat ( diffValidationRule . validate ( newBlock , parentHeader , cliqueProtocolContext ) ) . isTrue ( ) ;
@ -107,11 +81,14 @@ public class CliqueDifficultyValidationRuleTest {
final long OUT_OF_TURN_BLOCK_NUMBER = validatorList . size ( ) - 1L ;
final UInt256 REPORTED_DIFFICULTY = UInt256 . of ( 1 ) ;
blockHeaderBuilder . number ( OUT_OF_TURN_BLOCK_NUMBER - 1L ) ;
final BlockHeader parentHeader = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader parentHeader =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
blockHeaderBuilder . number ( OUT_OF_TURN_BLOCK_NUMBER ) . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader newBlock =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule ( ) ;
assertThat ( diffValidationRule . validate ( newBlock , parentHeader , cliqueProtocolContext ) ) . isTrue ( ) ;
@ -119,14 +96,16 @@ public class CliqueDifficultyValidationRuleTest {
@Test
public void isFalseIfOutTurnValidatorSuppliesDifficultyOfTwo ( ) {
final long OUT_OF_TURN_BLOCK_NUMBER = validatorList . size ( ) - 1L ;
final UInt256 REPORTED_DIFFICULTY = UInt256 . of ( 2 ) ;
blockHeaderBuilder . number ( OUT_OF_TURN_BLOCK_NUMBER - 1L ) ;
final BlockHeader parentHeader = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader parentHeader =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
blockHeaderBuilder . number ( OUT_OF_TURN_BLOCK_NUMBER ) . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock = createCliqueSignedBlock ( blockHeaderBuilder ) ;
blockHeaderBuilder . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule ( ) ;
assertThat ( diffValidationRule . validate ( newBlock , parentHeader , cliqueProtocolContext ) )
@ -135,14 +114,15 @@ public class CliqueDifficultyValidationRuleTest {
@Test
public void isFalseIfInTurnValidatorSuppliesDifficultyOfOne ( ) {
final long IN_TURN_BLOCK_NUMBER = validatorList . size ( ) ;
final UInt256 REPORTED_DIFFICULTY = UInt256 . of ( 1 ) ;
blockHeaderBuilder . number ( IN_TURN_BLOCK_NUMBER - 1L ) ;
final BlockHeader parentHeader = createCliqueSignedBlock ( blockHeaderBuilder ) ;
final BlockHeader parentHeader =
TestHelpers . createCliqueSignedBlockHeader ( blockHeaderBuilder , otherKeyPair , validatorList ) ;
blockHeaderBuilder . number ( IN_TURN_BLOCK_NUMBER ) . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock = createCliqueSignedBlock ( blockHeaderBuilder ) ;
blockHeaderBuilder . difficulty ( REPORTED_DIFFICULTY ) ;
final BlockHeader newBlock =
TestHelpers . createCliqueSignedBlockHeader (
blockHeaderBuilder , proposerKeyPair , validatorList ) ;
final CliqueDifficultyValidationRule diffValidationRule = new CliqueDifficultyValidationRule ( ) ;
assertThat ( diffValidationRule . validate ( newBlock , parentHeader , cliqueProtocolContext ) )