@Test public void correctlyConstructedHeaderPassesValidation() { final List<Address> validators = Lists.newArrayList( AddressHelpers.ofValue(1), AddressHelpers.ofValue(2), AddressHelpers.ofValue(3)); final VoteTally voteTally = new VoteTally(validators); final ProtocolContext<IbftContext> context = new ProtocolContext<>(null, null, new IbftContext(voteTally, null)); final BlockHeader header = createProposedBlockHeader(validators, emptyList(), false); assertThat(validatorsValidationRule.validate(header, null, context)).isTrue(); }
@Test public void validatorsInNonAscendingOrderFailValidation() { final List<Address> validators = Lists.newArrayList( AddressHelpers.ofValue(3), AddressHelpers.ofValue(2), AddressHelpers.ofValue(1)); final VoteTally voteTally = new VoteTally(validators); final ProtocolContext<IbftContext> context = new ProtocolContext<>(null, null, new IbftContext(voteTally, null)); final BlockHeader header = createProposedBlockHeader(validators, emptyList(), false); assertThat(validatorsValidationRule.validate(header, null, context)).isFalse(); }
@Test public void blendingAddVoteToHeaderResultsInHeaderWithNonceOfMaxLong() { final ValidatorVote vote = new ValidatorVote(ADD, AddressHelpers.ofValue(1), AddressHelpers.ofValue(2)); final BlockHeaderBuilder builderWithVote = CliqueBlockInterface.insertVoteToHeaderBuilder(builder, Optional.of(vote)); final BlockHeader header = builderWithVote.buildBlockHeader(); assertThat(header.getCoinbase()).isEqualTo(vote.getRecipient()); assertThat(header.getNonce()).isEqualTo(0xFFFFFFFFFFFFFFFFL); }
@Test public void nonVoteBlendedIntoHeaderResultsInACoinbaseOfZero() { final BlockHeaderBuilder builderWithVote = CliqueBlockInterface.insertVoteToHeaderBuilder(builder, Optional.empty()); final BlockHeader header = builderWithVote.buildBlockHeader(); assertThat(header.getCoinbase()).isEqualTo(AddressHelpers.ofValue(0)); assertThat(header.getNonce()).isEqualTo(0x0L); }
@Test public void blendingAddVoteToHeaderResultsInHeaderWithNonceOfMaxLong() { final ValidatorVote vote = new ValidatorVote(ADD, AddressHelpers.ofValue(1), AddressHelpers.ofValue(2)); final BlockHeaderBuilder builderWithVote = IbftLegacyBlockInterface.insertVoteToHeaderBuilder(builder, Optional.of(vote)); final BlockHeader header = builderWithVote.buildBlockHeader(); assertThat(header.getCoinbase()).isEqualTo(vote.getRecipient()); assertThat(header.getNonce()).isEqualTo(0xFFFFFFFFFFFFFFFFL); }
@Test public void blendingDropVoteToHeaderResultsInHeaderWithNonceOfZero() { final ValidatorVote vote = new ValidatorVote(DROP, AddressHelpers.ofValue(1), AddressHelpers.ofValue(2)); final BlockHeaderBuilder builderWithVote = IbftLegacyBlockInterface.insertVoteToHeaderBuilder(builder, Optional.of(vote)); final BlockHeader header = builderWithVote.buildBlockHeader(); assertThat(header.getCoinbase()).isEqualTo(vote.getRecipient()); assertThat(header.getNonce()).isEqualTo(0x0L); }
@Test public void blendingDropVoteToHeaderResultsInHeaderWithNonceOfZero() { final ValidatorVote vote = new ValidatorVote(DROP, AddressHelpers.ofValue(1), AddressHelpers.ofValue(2)); final BlockHeaderBuilder builderWithVote = CliqueBlockInterface.insertVoteToHeaderBuilder(builder, Optional.of(vote)); final BlockHeader header = builderWithVote.buildBlockHeader(); assertThat(header.getCoinbase()).isEqualTo(vote.getRecipient()); assertThat(header.getNonce()).isEqualTo(0x0L); }
private SignedData<ProposalPayload> signedProposal() { final Block block = TestHelpers.createProposalBlock(singletonList(AddressHelpers.ofValue(1)), 0); final ProposalPayload proposalPayload = new ProposalPayload(ROUND_IDENTIFIER, block); final Signature signature = Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 0); return SignedData.from(proposalPayload, signature); } }
private SignedData<ProposalPayload> signedProposal() { final Block block = TestHelpers.createProposalBlock(singletonList(AddressHelpers.ofValue(1)), 0); final ProposalPayload proposalPayload = new ProposalPayload(ROUND_IDENTIFIER, block); final Signature signature = Signature.create(BigInteger.ONE, BigInteger.TEN, (byte) 0); return SignedData.from(proposalPayload, signature); } }
public static SignedData<ProposalPayload> createSignedProposalPayloadWithRound( final KeyPair signerKeys, final int round) { final MessageFactory messageFactory = new MessageFactory(signerKeys); final ConsensusRoundIdentifier roundIdentifier = new ConsensusRoundIdentifier(0x1234567890ABCDEFL, round); final Block block = TestHelpers.createProposalBlock(singletonList(AddressHelpers.ofValue(1)), 0); return messageFactory.createSignedProposalPayload(roundIdentifier, block); }
@Test public void roundRobinChangesProposerOnRoundZeroOfNextBlock() { final long PREV_BLOCK_NUMBER = 2; final Address localAddr = AddressHelpers.ofValue(10); // arbitrarily selected final Blockchain blockchain = createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr); final LinkedList<Address> validatorList = createValidatorList(localAddr, 0, 4); final VoteTally voteTally = new VoteTally(validatorList); final ProposerSelector uut = new ProposerSelector(blockchain, voteTally, blockInterface, true); final ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 0); final Address nextProposer = uut.selectProposerForRound(roundId); assertThat(nextProposer).isEqualTo(validatorList.get(1)); }
@Test public void lastValidatorInListValidatedPreviousBlockSoFirstIsNextProposer() { final long PREV_BLOCK_NUMBER = 2; final Address localAddr = AddressHelpers.ofValue(10); // arbitrarily selected final Blockchain blockchain = createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr); final LinkedList<Address> validatorList = createValidatorList(localAddr, 4, 0); final VoteTally voteTally = new VoteTally(validatorList); final ProposerSelector uut = new ProposerSelector(blockchain, voteTally, blockInterface, true); final ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 0); final Address nextProposer = uut.selectProposerForRound(roundId); assertThat(nextProposer).isEqualTo(validatorList.get(0)); }
@Test public void stickyProposerDoesNotChangeOnRoundZeroOfNextBlock() { final long PREV_BLOCK_NUMBER = 2; final ConsensusRoundIdentifier roundId = new ConsensusRoundIdentifier(PREV_BLOCK_NUMBER + 1, 0); final Address localAddr = AddressHelpers.ofValue(10); // arbitrarily selected final Blockchain blockchain = createMockedBlockChainWithHeadOf(PREV_BLOCK_NUMBER, localAddr); final LinkedList<Address> validatorList = createValidatorList(localAddr, 4, 0); final VoteTally voteTally = new VoteTally(validatorList); final ProposerSelector uut = new ProposerSelector(blockchain, voteTally, blockInterface, false); final Address nextProposer = uut.selectProposerForRound(roundId); assertThat(nextProposer).isEqualTo(localAddr); }
@Test public void headerWithZeroCoinbaseReturnsAnEmptyVote() { final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture(); headerBuilder.coinbase(AddressHelpers.ofValue(0)); assertThat(blockInterface.extractVoteFromHeader(headerBuilder.buildHeader())).isEmpty(); }
@Test public void headerWithNonceOfZeroReportsDropVote() { headerBuilder.coinbase(AddressHelpers.ofValue(1)).nonce(0L); final Optional<ValidatorVote> extractedVote = blockInterface.extractVoteFromHeader(header); assertThat(extractedVote) .contains(new ValidatorVote(DROP, proposerAddress, header.getCoinbase())); }
@Test public void headerWithNonceOfMaxLongReportsAddVote() { headerBuilder.nonce(0xFFFFFFFFFFFFFFFFL).coinbase(AddressHelpers.ofValue(2)); final BlockHeader header = TestHelpers.createIbftSignedBlockHeader(headerBuilder, proposerKeys, validatorList); final Optional<ValidatorVote> extractedVote = blockInterface.extractVoteFromHeader(header); assertThat(extractedVote) .contains(new ValidatorVote(ADD, proposerAddress, header.getCoinbase())); }
@Test public void headerWithNonceOfMaxLongReportsAddVote() { headerBuilder.coinbase(AddressHelpers.ofValue(2)).nonce(0xFFFFFFFFFFFFFFFFL); final BlockHeader header = TestHelpers.createCliqueSignedBlockHeader(headerBuilder, proposerKeys, validatorList); final Optional<ValidatorVote> extractedVote = blockInterface.extractVoteFromHeader(header); assertThat(extractedVote) .contains(new ValidatorVote(ADD, proposerAddress, header.getCoinbase())); }
@Test public void headerWithNonceOfZeroReportsDropVote() { headerBuilder.nonce(0x0L).coinbase(AddressHelpers.ofValue(2)); final BlockHeader header = TestHelpers.createIbftSignedBlockHeader(headerBuilder, proposerKeys, validatorList); final Optional<ValidatorVote> extractedVote = blockInterface.extractVoteFromHeader(header); assertThat(extractedVote) .contains(new ValidatorVote(DROP, proposerAddress, header.getCoinbase())); }
private Block createEmptyBlock(final long blockNumber, final Hash parentHash) { headerBuilder.number(blockNumber).parentHash(parentHash).coinbase(AddressHelpers.ofValue(0)); return new Block( headerBuilder.buildHeader(), new BlockBody(Lists.emptyList(), Lists.emptyList())); }