Search in sources :

Example 1 with Vote

use of org.hyperledger.besu.consensus.common.bft.Vote in project besu by hyperledger.

the class PkiQbftExtraDataCodecTest method getDecodedExtraData.

private static PkiQbftExtraData getDecodedExtraData(final Bytes cms) {
    final List<Address> validators = Arrays.asList(Address.fromHexString("1"), Address.fromHexString("2"));
    final Optional<Vote> vote = Optional.of(Vote.authVote(Address.fromHexString("1")));
    final int round = 0x00FEDCBA;
    final List<SECPSignature> committerSeals = Arrays.asList(SIGNATURE_ALGORITHM.get().createSignature(BigInteger.ONE, BigInteger.TEN, (byte) 0), SIGNATURE_ALGORITHM.get().createSignature(BigInteger.TEN, BigInteger.ONE, (byte) 0));
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = createNonEmptyVanityData();
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    return new PkiQbftExtraData(vanity_data, committerSeals, vote, round, validators, cms);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address)

Example 2 with Vote

use of org.hyperledger.besu.consensus.common.bft.Vote in project besu by hyperledger.

the class QbftExtraDataCodecTest method extraDataCanBeEncodedWithoutCommitSeals.

@Test
public void extraDataCanBeEncodedWithoutCommitSeals() {
    final List<Address> validators = Arrays.asList(Address.fromHexString("1"), Address.fromHexString("2"));
    final Optional<Vote> vote = Optional.of(Vote.authVote(Address.fromHexString("1")));
    final int round = 0x00FEDCBA;
    final List<SECPSignature> committerSeals = Arrays.asList(SIGNATURE_ALGORITHM.get().createSignature(BigInteger.ONE, BigInteger.TEN, (byte) 0), SIGNATURE_ALGORITHM.get().createSignature(BigInteger.TEN, BigInteger.ONE, (byte) 0));
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = createNonEmptyVanityData();
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    final BytesValueRLPOutput encoder = new BytesValueRLPOutput();
    encoder.startList();
    encoder.writeBytes(vanity_data);
    encoder.writeList(validators, (validator, rlp) -> rlp.writeBytes(validator));
    // encoded vote
    encoder.startList();
    encoder.writeBytes(Address.fromHexString("1"));
    encoder.writeByte(Vote.ADD_BYTE_VALUE);
    encoder.endList();
    encoder.writeIntScalar(round);
    encoder.writeEmptyList();
    encoder.endList();
    Bytes expectedEncoding = encoder.encoded();
    Bytes actualEncoding = bftExtraDataCodec.encodeWithoutCommitSeals(new BftExtraData(vanity_data, committerSeals, vote, round, validators));
    System.out.println("actualEncoding = " + actualEncoding);
    assertThat(actualEncoding).isEqualTo(expectedEncoding);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 3 with Vote

use of org.hyperledger.besu.consensus.common.bft.Vote in project besu by hyperledger.

the class QbftExtraDataCodecTest method emptyListConstituteValidContent.

@Test
public void emptyListConstituteValidContent() {
    final List<Address> validators = Lists.newArrayList();
    final Optional<Vote> vote = Optional.of(Vote.dropVote(Address.fromHexString("1")));
    final int round = 0x00FEDCBA;
    final List<SECPSignature> committerSeals = Lists.newArrayList();
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = new byte[32];
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    final BytesValueRLPOutput encoder = new BytesValueRLPOutput();
    encoder.startList();
    encoder.writeBytes(vanity_data);
    encoder.writeList(validators, (validator, rlp) -> rlp.writeBytes(validator));
    // encoded vote
    encoder.startList();
    encoder.writeBytes(vote.get().getRecipient());
    encoder.writeNull();
    encoder.endList();
    encoder.writeIntScalar(round);
    encoder.writeList(committerSeals, (committer, rlp) -> rlp.writeBytes(committer.encodedBytes()));
    encoder.endList();
    final Bytes bufferToInject = encoder.encoded();
    final BftExtraData extraData = bftExtraDataCodec.decodeRaw(bufferToInject);
    assertThat(extraData.getVanityData()).isEqualTo(vanity_data);
    assertThat(extraData.getRound()).isEqualTo(round);
    assertThat(extraData.getSeals()).isEqualTo(committerSeals);
    assertThat(extraData.getValidators()).isEqualTo(validators);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 4 with Vote

use of org.hyperledger.besu.consensus.common.bft.Vote in project besu by hyperledger.

the class QbftExtraDataCodecTest method correctlyCodedRoundConstitutesValidContent.

@Test
public void correctlyCodedRoundConstitutesValidContent() {
    final List<Address> validators = Lists.newArrayList();
    final Optional<Vote> vote = Optional.of(Vote.authVote(Address.fromHexString("1")));
    final int round = 0xDCBA;
    final byte[] roundAsByteArray = new byte[] { (byte) 0xDC, (byte) 0xBA };
    final List<SECPSignature> committerSeals = Lists.newArrayList();
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = new byte[32];
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    final BytesValueRLPOutput encoder = new BytesValueRLPOutput();
    encoder.startList();
    encoder.writeBytes(vanity_data);
    encoder.writeList(validators, (validator, rlp) -> rlp.writeBytes(validator));
    encoder.startList();
    encoder.writeBytes(vote.get().getRecipient());
    encoder.writeByte(Vote.ADD_BYTE_VALUE);
    encoder.endList();
    // This is to verify that the decoding works correctly when the round is encoded as non-fixed
    // length bytes
    encoder.writeBytes(Bytes.wrap(roundAsByteArray));
    encoder.writeList(committerSeals, (committer, rlp) -> rlp.writeBytes(committer.encodedBytes()));
    encoder.endList();
    final Bytes bufferToInject = encoder.encoded();
    final BftExtraData extraData = bftExtraDataCodec.decodeRaw(bufferToInject);
    assertThat(extraData.getVanityData()).isEqualTo(vanity_data);
    assertThat(extraData.getRound()).isEqualTo(round);
    assertThat(extraData.getSeals()).isEqualTo(committerSeals);
    assertThat(extraData.getValidators()).isEqualTo(validators);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Example 5 with Vote

use of org.hyperledger.besu.consensus.common.bft.Vote in project besu by hyperledger.

the class QbftExtraDataCodecTest method emptyListsAreEncodedAndDecodedCorrectly.

@Test
public void emptyListsAreEncodedAndDecodedCorrectly() {
    final List<Address> validators = Lists.newArrayList();
    final Optional<Vote> vote = Optional.of(Vote.authVote(Address.fromHexString("1")));
    final int round = 0x00FEDCBA;
    final List<SECPSignature> committerSeals = Lists.newArrayList();
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = new byte[32];
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    BftExtraData expectedExtraData = new BftExtraData(vanity_data, committerSeals, vote, round, validators);
    BftExtraData actualExtraData = bftExtraDataCodec.decodeRaw(bftExtraDataCodec.encode(expectedExtraData));
    assertThat(actualExtraData).isEqualToComparingFieldByField(expectedExtraData);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) Test(org.junit.Test)

Aggregations

Vote (org.hyperledger.besu.consensus.common.bft.Vote)24 Address (org.hyperledger.besu.datatypes.Address)24 Bytes (org.apache.tuweni.bytes.Bytes)22 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)22 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)20 Test (org.junit.Test)17 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)11 Arrays (java.util.Arrays)2 Collections.emptyList (java.util.Collections.emptyList)2 List (java.util.List)2 Optional (java.util.Optional)2 Random (java.util.Random)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 UInt256 (org.apache.tuweni.units.bigints.UInt256)2 Java6Assertions.assertThat (org.assertj.core.api.Java6Assertions.assertThat)2 BftBlockHashing (org.hyperledger.besu.consensus.common.bft.BftBlockHashing)2 BftBlockHeaderFunctions (org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions)2 VoteType (org.hyperledger.besu.consensus.common.validator.VoteType)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2