Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[NC-2058] initial scaffolding re block propagation (#860)
Browse files Browse the repository at this point in the history
* o

* add test

* clean up

* scaffolding

* update

* update

* comments

* add test

* update

* update

* update
  • Loading branch information
smatthewenglish authored Feb 14, 2019
1 parent d410ac9 commit c07e261
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c07e261

Please sign in to comment.