public static RespondingEthPeer createPeer( final EthProtocolManager ethProtocolManager, final Blockchain blockchain) { final ChainHead head = blockchain.getChainHead(); return RespondingEthPeer.create( ethProtocolManager, head.getHash(), head.getTotalDifficulty(), blockchain.getChainHeadBlockNumber()); } }
private void assertBlockIsHead(final Blockchain blockchain, final Block head) { assertEquals(head.getHash(), blockchain.getChainHeadHash()); assertEquals(head.getHeader().getNumber(), blockchain.getChainHeadBlockNumber()); assertEquals(head.getHash(), blockchain.getChainHead().getHash()); }
blockchain.getBlockBody(chainHeadHash).get()); final UInt256 expectedTotalDifficulty = blockchain.getChainHead().getTotalDifficulty();
private void assertTotalDifficultiesAreConsistent(final Blockchain blockchain, final Block head) { // Check that total difficulties are summed correctly long num = BlockHeader.GENESIS_BLOCK_NUMBER; UInt256 td = UInt256.of(0); while (num <= head.getHeader().getNumber()) { final Hash curHash = blockchain.getBlockHashByNumber(num).get(); final BlockHeader curHead = blockchain.getBlockHeader(curHash).get(); td = td.plus(curHead.getDifficulty()); assertEquals(td, blockchain.getTotalDifficultyByHash(curHash).get()); num += 1; } // Check reported chainhead td assertEquals(td, blockchain.getChainHead().getTotalDifficulty()); }
private MockPeerConnection setupPeer( final EthProtocolManager ethManager, final PeerSendHandler onSend) { final MockPeerConnection peer = setupPeerWithoutStatusExchange(ethManager, onSend); final StatusMessage statusMessage = StatusMessage.create( EthVersion.V63, 1, blockchain.getChainHead().getTotalDifficulty(), blockchain.getChainHeadHash(), blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get().getHash()); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, statusMessage)); return peer; }
@Test public void disconnectOnWrongGenesisHash() { try (final EthProtocolManager ethManager = new EthProtocolManager(blockchain, protocolContext.getWorldStateArchive(), 1, true, 1, 1)) { final MessageData messageData = BlockHeadersMessage.create(Collections.singletonList(blockchain.getBlockHeader(1).get())); final MockPeerConnection peer = setupPeerWithoutStatusExchange(ethManager, (cap, msg, conn) -> {}); // Send status message with wrong chain final StatusMessage statusMessage = StatusMessage.create( EthVersion.V63, 1, blockchain.getChainHead().getTotalDifficulty(), gen.hash(), blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get().getHash()); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, statusMessage)); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData)); assertThat(peer.isDisconnected()).isTrue(); } }
@Test public void disconnectOnWrongChainId() { try (final EthProtocolManager ethManager = new EthProtocolManager(blockchain, protocolContext.getWorldStateArchive(), 1, true, 1, 1)) { final MessageData messageData = BlockHeadersMessage.create(Collections.singletonList(blockchain.getBlockHeader(1).get())); final MockPeerConnection peer = setupPeerWithoutStatusExchange(ethManager, (cap, msg, conn) -> {}); // Send status message with wrong chain final StatusMessage statusMessage = StatusMessage.create( EthVersion.V63, 2222, blockchain.getChainHead().getTotalDifficulty(), blockchain.getChainHeadHash(), blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get().getHash()); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, statusMessage)); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, messageData)); assertThat(peer.isDisconnected()).isTrue(); } }