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

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
smatthewenglish committed Feb 12, 2019
1 parent 1523c18 commit 55a5465
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {

private final Hash genesisHash;
private final int networkId;
private final EthPeers ethPeers;
private EthPeers ethPeers;
private final EthMessages ethMessages;
private final EthContext ethContext;
private final boolean fastSyncEnabled;
private List<Capability> supportedCapabilities;
private final Blockchain blockchain;

public void setEthPeers(EthPeers ethPeers) {
this.ethPeers = ethPeers;
}

EthProtocolManager(
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ private void onBlockAdded(final BlockAddedEvent blockAddedEvent, final Blockchai
}
}

private void broadcastBlock(final Block block, final UInt256 difficulty) {
final List<EthPeer> availablePeers =
ethContext.getEthPeers().availablePeers().collect(Collectors.toList());
void broadcastBlock(final Block block, final UInt256 difficulty) {
final List<EthPeer> availablePeers = ethContext.getEthPeers().availablePeers().collect(Collectors.toList());
for (EthPeer ethPeer : availablePeers) {
ethPeer.propagateBlock(block, difficulty);
}
}

private void handleNewBlockFromNetwork(final EthMessage message) {
void handleNewBlockFromNetwork(final EthMessage message) {
final Blockchain blockchain = protocolContext.getBlockchain();
final NewBlockMessage newBlockMessage = NewBlockMessage.readFrom(message.getData());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,49 @@ public void shouldNotImportBlocksThatAreAlreadyBeingImported() {

verify(ethScheduler, times(1)).scheduleSyncWorkerTask(any(Supplier.class));
}

@SuppressWarnings("unchecked")
@Test
public void broadcastBlockTest() {
BlockchainSetupUtil blockchainUtil = BlockchainSetupUtil.forTesting();

MutableBlockchain blockchain = spy(blockchainUtil.getBlockchain());
ProtocolSchedule protocolSchedule = blockchainUtil.getProtocolSchedule();
ProtocolContext<Void> tempProtocolContext = blockchainUtil.getProtocolContext();
ProtocolContext protocolContext = new ProtocolContext<>(blockchain, tempProtocolContext.getWorldStateArchive(), tempProtocolContext.getConsensusState());

EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create(blockchain, blockchainUtil.getWorldArchive());

SynchronizerConfiguration syncConfig = SynchronizerConfiguration.builder().blockPropagationRange(-3, 5).build().validated(blockchain);
SyncState syncState = new SyncState(blockchain, ethProtocolManager.ethContext().getEthPeers());
BlockPropagationManager blockPropagationManager = spy(new BlockPropagationManager<>(
syncConfig,
protocolSchedule,
protocolContext,
ethProtocolManager.ethContext(),
syncState,
pendingBlocks,
ethTasksTimer));

blockchainUtil.importFirstBlocks(2);
final Block nextBlock = blockchainUtil.getBlock(2);

blockPropagationManager.start();

// Setup peer and messages
final RespondingEthPeer peer0 = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 0);
final RespondingEthPeer peer1 = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 0);
final RespondingEthPeer peer2 = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 0);

final UInt256 totalDifficulty = fullBlockchain.getTotalDifficultyByHash(nextBlock.getHash()).get();
final NewBlockMessage newBlockMessage = NewBlockMessage.create(nextBlock, totalDifficulty);

// Broadcast message
EthProtocolManagerTestUtil.broadcastMessage(ethProtocolManager, peer0, newBlockMessage);

final Responder responder = RespondingEthPeer.blockchainResponder(fullBlockchain);
peer0.respondWhile(responder, peer0::hasOutstandingRequests);

verify(blockPropagationManager, times(1)).broadcastBlock(any(), any());
}
}

0 comments on commit 55a5465

Please sign in to comment.