This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[NC-2058] Improve block propagation time #808
Merged
smatthewenglish
merged 25 commits into
PegaSysEng:master
from
smatthewenglish:propaganda
Feb 18, 2019
Merged
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
476da33
o
smatthewenglish 93c9aa8
add test
smatthewenglish 610834c
clean up
smatthewenglish d4468a5
scaffolding
smatthewenglish 96a2126
update
smatthewenglish 2c3b7e4
update
smatthewenglish 1523c18
comments
smatthewenglish e3d12b6
add test
smatthewenglish 77bf74d
update
smatthewenglish 506b55a
update ii
smatthewenglish 3e0a7b4
merge
smatthewenglish d734c70
update
smatthewenglish e947f14
format
smatthewenglish 88a8835
update ii
smatthewenglish 9911f14
fix
smatthewenglish 58ac334
verifyBroadcastBlockInvocation
smatthewenglish 4ce6c83
test
smatthewenglish 395e9bc
update
smatthewenglish f88e418
update to difficulty calculation
smatthewenglish bb29bf8
remove BlockBroadcasterTest from this pr
smatthewenglish b956425
update
smatthewenglish 52aa5ce
update
smatthewenglish cc6dc46
update II
smatthewenglish 824308c
Merge remote-tracking branch 'upstream/master' into propaganda
smatthewenglish 3e99654
Merge branch 'master' into propaganda
smatthewenglish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
ethereum/eth/src/main/java/tech/pegasys/pantheon/ethereum/eth/sync/BlockBroadcaster.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.eth.sync; | ||
|
||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.EthContext; | ||
import tech.pegasys.pantheon.util.uint.UInt256; | ||
|
||
class BlockBroadcaster { | ||
|
||
private final EthContext ethContext; | ||
|
||
BlockBroadcaster(final EthContext ethContext) { | ||
this.ethContext = ethContext; | ||
} | ||
|
||
void propagate(final Block block, final UInt256 difficulty) { | ||
ethContext | ||
.getEthPeers() | ||
.availablePeers() | ||
.forEach(ethPeer -> ethPeer.propagateBlock(block, difficulty)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import tech.pegasys.pantheon.ethereum.chain.BlockAddedEvent.EventType; | ||
import tech.pegasys.pantheon.ethereum.chain.Blockchain; | ||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.core.BlockHeader; | ||
import tech.pegasys.pantheon.ethereum.core.Hash; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.AbstractPeerTask; | ||
import tech.pegasys.pantheon.ethereum.eth.manager.EthContext; | ||
|
@@ -30,8 +31,10 @@ | |
import tech.pegasys.pantheon.ethereum.eth.sync.state.SyncState; | ||
import tech.pegasys.pantheon.ethereum.eth.sync.tasks.GetBlockFromPeerTask; | ||
import tech.pegasys.pantheon.ethereum.eth.sync.tasks.PersistBlockTask; | ||
import tech.pegasys.pantheon.ethereum.mainnet.BlockHeaderValidator; | ||
import tech.pegasys.pantheon.ethereum.mainnet.HeaderValidationMode; | ||
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule; | ||
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec; | ||
import tech.pegasys.pantheon.ethereum.p2p.wire.messages.DisconnectMessage.DisconnectReason; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLPException; | ||
import tech.pegasys.pantheon.metrics.LabelledMetric; | ||
|
@@ -63,6 +66,7 @@ public class BlockPropagationManager<C> { | |
private final EthContext ethContext; | ||
private final SyncState syncState; | ||
private final LabelledMetric<OperationTimer> ethTasksTimer; | ||
private final BlockBroadcaster blockBroadcaster; | ||
|
||
private final AtomicBoolean started = new AtomicBoolean(false); | ||
|
||
|
@@ -77,13 +81,14 @@ public class BlockPropagationManager<C> { | |
final EthContext ethContext, | ||
final SyncState syncState, | ||
final PendingBlocks pendingBlocks, | ||
final LabelledMetric<OperationTimer> ethTasksTimer) { | ||
final LabelledMetric<OperationTimer> ethTasksTimer, | ||
final BlockBroadcaster blockBroadcaster) { | ||
this.config = config; | ||
this.protocolSchedule = protocolSchedule; | ||
this.protocolContext = protocolContext; | ||
this.ethContext = ethContext; | ||
this.ethTasksTimer = ethTasksTimer; | ||
|
||
this.blockBroadcaster = blockBroadcaster; | ||
this.syncState = syncState; | ||
this.pendingBlocks = pendingBlocks; | ||
} | ||
|
@@ -105,6 +110,32 @@ private void setupListeners() { | |
.subscribe(EthPV62.NEW_BLOCK_HASHES, this::handleNewBlockHashesFromNetwork); | ||
} | ||
|
||
protected void validateAndBroadcastBlock(final Block block) { | ||
final ProtocolSpec<C> protocolSpec = | ||
protocolSchedule.getByBlockNumber(block.getHeader().getNumber()); | ||
final BlockHeaderValidator<C> blockHeaderValidator = protocolSpec.getBlockHeaderValidator(); | ||
final BlockHeader parent = | ||
protocolContext | ||
.getBlockchain() | ||
.getBlockHeader(block.getHeader().getParentHash()) | ||
.orElseThrow( | ||
() -> | ||
new IllegalArgumentException( | ||
"Incapable of retrieving header from non-existent parent of " | ||
+ block.getHeader().getNumber() | ||
+ ".")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ok for this PR, but could you follow up with a PR to change |
||
if (blockHeaderValidator.validateHeader( | ||
block.getHeader(), parent, protocolContext, HeaderValidationMode.FULL)) { | ||
final UInt256 totalDifficulty = | ||
protocolContext | ||
.getBlockchain() | ||
.getTotalDifficultyByHash(parent.getHash()) | ||
.get() | ||
.plus(block.getHeader().getDifficulty()); | ||
blockBroadcaster.propagate(block, totalDifficulty); | ||
} | ||
} | ||
|
||
private void onBlockAdded(final BlockAddedEvent blockAddedEvent, final Blockchain blockchain) { | ||
// Check to see if any of our pending blocks are now ready for import | ||
final Block newBlock = blockAddedEvent.getBlock(); | ||
|
@@ -144,23 +175,13 @@ private void onBlockAdded(final BlockAddedEvent blockAddedEvent, final Blockchai | |
} | ||
} | ||
|
||
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 | ||
|
@@ -272,6 +293,8 @@ CompletableFuture<Block> importOrSavePendingBlock(final Block block) { | |
return CompletableFuture.completedFuture(block); | ||
} | ||
|
||
validateAndBroadcastBlock(block); | ||
|
||
// Import block | ||
final PersistBlockTask<C> importTask = | ||
PersistBlockTask.create( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to filter out peers that have already seen this block:
.filter(ethPeer -> ~ethPeer.hasSeenBlock(block.getHash()))
and then should add a test for that as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in
EthPeer.propagateBlock
we should add the block to theknownBlocks
set.