Search in sources :

Example 26 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class NewBlockMessageTest method roundTripNewBlockMessage.

@Test
public void roundTripNewBlockMessage() {
    final Difficulty totalDifficulty = Difficulty.of(98765);
    final BlockDataGenerator blockGenerator = new BlockDataGenerator();
    final Block blockForInsertion = blockGenerator.block();
    final NewBlockMessage msg = NewBlockMessage.create(blockForInsertion, totalDifficulty);
    assertThat(msg.getCode()).isEqualTo(EthPV62.NEW_BLOCK);
    assertThat(msg.totalDifficulty(protocolSchedule)).isEqualTo(totalDifficulty);
    final Block extractedBlock = msg.block(protocolSchedule);
    assertThat(extractedBlock).isEqualTo(blockForInsertion);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Test(org.junit.Test)

Example 27 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class NewBlockMessageTest method rawMessageUpCastsToANewBlockMessage.

@Test
public void rawMessageUpCastsToANewBlockMessage() {
    final Difficulty totalDifficulty = Difficulty.of(12345);
    final BlockDataGenerator blockGenerator = new BlockDataGenerator();
    final Block blockForInsertion = blockGenerator.block();
    final BytesValueRLPOutput tmp = new BytesValueRLPOutput();
    tmp.startList();
    blockForInsertion.writeTo(tmp);
    tmp.writeUInt256Scalar(totalDifficulty);
    tmp.endList();
    final RawMessage rawMsg = new RawMessage(EthPV62.NEW_BLOCK, tmp.encoded());
    final NewBlockMessage newBlockMsg = NewBlockMessage.readFrom(rawMsg);
    assertThat(newBlockMsg.getCode()).isEqualTo(EthPV62.NEW_BLOCK);
    assertThat(newBlockMsg.totalDifficulty(protocolSchedule)).isEqualTo(totalDifficulty);
    final Block extractedBlock = newBlockMsg.block(protocolSchedule);
    assertThat(extractedBlock).isEqualTo(blockForInsertion);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Block(org.hyperledger.besu.ethereum.core.Block) RawMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.RawMessage) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Example 28 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class StatusMessageTest method serializeDeserialize.

@Test
public void serializeDeserialize() {
    final int version = EthProtocol.EthVersion.V62;
    final BigInteger networkId = BigInteger.ONE;
    final Difficulty td = Difficulty.of(1000L);
    final Hash bestHash = randHash(1L);
    final Hash genesisHash = randHash(2L);
    final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, genesisHash);
    // Make a message copy from serialized data and check deserialized results
    final StatusMessage copy = new StatusMessage(msg.getData());
    assertThat(copy.protocolVersion()).isEqualTo(version);
    assertThat(copy.networkId()).isEqualTo(networkId);
    assertThat(copy.totalDifficulty()).isEqualTo(td);
    assertThat(copy.bestHash()).isEqualTo(bestHash);
    assertThat(copy.genesisHash()).isEqualTo(genesisHash);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) BigInteger(java.math.BigInteger) Hash(org.hyperledger.besu.datatypes.Hash) Test(org.junit.Test)

Example 29 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class StatusMessageTest method serializeDeserializeWithForkId.

@Test
public void serializeDeserializeWithForkId() {
    final int version = EthProtocol.EthVersion.V64;
    final BigInteger networkId = BigInteger.ONE;
    final Difficulty td = Difficulty.of(1000L);
    final Hash bestHash = randHash(1L);
    final Hash genesisHash = randHash(2L);
    final ForkId forkId = new ForkId(Bytes.fromHexString("0xa00bc334"), 0L);
    final MessageData msg = StatusMessage.create(version, networkId, td, bestHash, genesisHash, forkId);
    final StatusMessage copy = new StatusMessage(msg.getData());
    assertThat(copy.protocolVersion()).isEqualTo(version);
    assertThat(copy.networkId()).isEqualTo(networkId);
    assertThat(copy.totalDifficulty()).isEqualTo(td);
    assertThat(copy.bestHash()).isEqualTo(bestHash);
    assertThat(copy.genesisHash()).isEqualTo(genesisHash);
    assertThat(copy.forkId()).isEqualTo(forkId);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) BigInteger(java.math.BigInteger) ForkId(org.hyperledger.besu.ethereum.eth.manager.ForkId) Hash(org.hyperledger.besu.datatypes.Hash) Test(org.junit.Test)

Example 30 with Difficulty

use of org.hyperledger.besu.ethereum.core.Difficulty in project besu by hyperledger.

the class ChainStateTest method updateBestBlockAndHeightFromBetterBlockHeaderAndTd.

@Test
public void updateBestBlockAndHeightFromBetterBlockHeaderAndTd() {
    final long blockNumber = 12;
    final BlockHeader bestBlockHeader = new BlockHeaderTestFixture().number(blockNumber).buildHeader();
    chainState.statusReceived(bestBlockHeader.getHash(), INITIAL_TOTAL_DIFFICULTY);
    assertThat(chainState.getEstimatedHeight()).isEqualTo(0L);
    assertThat(chainState.getBestBlock().getNumber()).isEqualTo(0L);
    final long betterBlockNumber = blockNumber + 2;
    final Difficulty betterTd = INITIAL_TOTAL_DIFFICULTY.add(100L);
    final BlockHeader betterBlock = new BlockHeaderTestFixture().number(betterBlockNumber).buildHeader();
    chainState.updateForAnnouncedBlock(betterBlock, betterTd);
    assertThat(chainState.getEstimatedHeight()).isEqualTo(betterBlockNumber - 1);
    assertThat(chainState.getBestBlock().getNumber()).isEqualTo(betterBlockNumber - 1);
    assertThat(chainState.getBestBlock().getHash()).isEqualTo(betterBlock.getParentHash());
    assertThat(chainState.getBestBlock().getTotalDifficulty()).isEqualTo(betterTd);
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)51 Test (org.junit.Test)29 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)20 Block (org.hyperledger.besu.ethereum.core.Block)16 Hash (org.hyperledger.besu.datatypes.Hash)14 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)9 BigInteger (java.math.BigInteger)8 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)8 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 List (java.util.List)6 Optional (java.util.Optional)6 Wei (org.hyperledger.besu.datatypes.Wei)6 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)6 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)6 ArrayList (java.util.ArrayList)5 Bytes (org.apache.tuweni.bytes.Bytes)5 Address (org.hyperledger.besu.datatypes.Address)5 Collections (java.util.Collections)4 MergeContext (org.hyperledger.besu.consensus.merge.MergeContext)4 NewBlockMessage (org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage)4