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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations