From bf92b8566b3634ff22f0cddf1b6520598d61aa69 Mon Sep 17 00:00:00 2001 From: "S. Matthew English" Date: Thu, 14 Feb 2019 01:25:53 -0500 Subject: [PATCH] [NC-2058] initial scaffolding re block propagation (#860) * o * add test * clean up * scaffolding * update * update * comments * add test * update * update * update --- .../pantheon/ethereum/eth/manager/EthPeer.java | 11 +++++++++++ .../ethereum/eth/sync/BlockPropagationManager.java | 12 +++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/EthPeer.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/EthPeer.java index a21306e4e6..5fc4a56c80 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/EthPeer.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/manager/EthPeer.java @@ -14,6 +14,7 @@ import static com.google.common.base.Preconditions.checkArgument; +import tech.pegasys.pantheon.ethereum.core.Block; import tech.pegasys.pantheon.ethereum.core.Hash; import tech.pegasys.pantheon.ethereum.eth.manager.ChainState.EstimatedHeightListener; import tech.pegasys.pantheon.ethereum.eth.manager.RequestManager.ResponseStream; @@ -23,6 +24,7 @@ import tech.pegasys.pantheon.ethereum.eth.messages.GetBlockHeadersMessage; import tech.pegasys.pantheon.ethereum.eth.messages.GetNodeDataMessage; import tech.pegasys.pantheon.ethereum.eth.messages.GetReceiptsMessage; +import tech.pegasys.pantheon.ethereum.eth.messages.NewBlockMessage; import tech.pegasys.pantheon.ethereum.p2p.api.MessageData; import tech.pegasys.pantheon.ethereum.p2p.api.PeerConnection; import tech.pegasys.pantheon.ethereum.p2p.api.PeerConnection.PeerNotConnected; @@ -130,6 +132,15 @@ public ResponseStream send(final MessageData messageData) throws PeerNotConnecte } } + public void propagateBlock(final Block block, final UInt256 totalDifficulty) { + final NewBlockMessage newBlockMessage = NewBlockMessage.create(block, totalDifficulty); + try { + connection.sendForProtocol(protocolName, newBlockMessage); + } catch (PeerNotConnected e) { + LOG.trace("Failed to broadcast new block to peer", e); + } + } + public ResponseStream getHeadersByHash( final Hash hash, final int maxHeaders, final int skip, final boolean reverse) throws PeerNotConnected { diff --git a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockPropagationManager.java b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockPropagationManager.java index 5ee4f00bae..dca15d6539 100644 --- a/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockPropagationManager.java +++ b/ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockPropagationManager.java @@ -144,13 +144,23 @@ private void onBlockAdded(final BlockAddedEvent blockAddedEvent, final Blockchai } } - private void handleNewBlockFromNetwork(final EthMessage message) { + void broadcastBlock(final Block block, final UInt256 difficulty) { + ethContext + .getEthPeers() + .availablePeers() + .forEach(ethPeer -> ethPeer.propagateBlock(block, difficulty)); + } + + void handleNewBlockFromNetwork(final EthMessage message) { final Blockchain blockchain = protocolContext.getBlockchain(); final NewBlockMessage newBlockMessage = NewBlockMessage.readFrom(message.getData()); try { final Block block = newBlockMessage.block(protocolSchedule); final UInt256 totalDifficulty = newBlockMessage.totalDifficulty(protocolSchedule); + // TODO: Extract broadcast functionality to independent class. + // broadcastBlock(block, totalDifficulty); + message.getPeer().chainState().updateForAnnouncedBlock(block.getHeader(), totalDifficulty); // Return early if we don't care about this block