Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old blocks are ignored when processed #510

Merged
merged 1 commit into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rskj-core/src/main/java/co/rsk/core/RskFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ public SyncProcessor getSyncProcessor(RskSystemProperties config,
}

@Bean
public BlockSyncService getBlockSyncService(Blockchain blockchain,
public BlockSyncService getBlockSyncService(RskSystemProperties config,
Blockchain blockchain,
BlockStore store,
BlockNodeInformation nodeInformation,
SyncConfiguration syncConfiguration) {
return new BlockSyncService(store, blockchain, nodeInformation, syncConfiguration);
return new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
}

@Bean
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/java/co/rsk/net/BlockProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public interface BlockProcessor {
void processBlockHashRequest(MessageChannel sender, long requestId, long height);

void processSkeletonRequest(MessageChannel sender, long requestId, long startNumber);

boolean canBeIgnoredForUnclesRewards(long blockNumber);
}
9 changes: 9 additions & 0 deletions rskj-core/src/main/java/co/rsk/net/BlockSyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package co.rsk.net;

import co.rsk.config.RskSystemProperties;
import co.rsk.core.bc.BlockUtils;
import co.rsk.crypto.Keccak256;
import co.rsk.net.messages.GetBlockMessage;
Expand Down Expand Up @@ -52,10 +53,12 @@ public class BlockSyncService {
private final Blockchain blockchain;
private final SyncConfiguration syncConfiguration;
private final BlockNodeInformation nodeInformation; // keep tabs on which nodes know which blocks.
private final RskSystemProperties config;

// this is tightly coupled with NodeProcessorService and SyncProcessor,
// and we should use the same objects everywhere to ensure consistency
public BlockSyncService(
@Nonnull final RskSystemProperties config,
@Nonnull final BlockStore store,
@Nonnull final Blockchain blockchain,
@Nonnull final BlockNodeInformation nodeInformation,
Expand All @@ -65,6 +68,7 @@ public BlockSyncService(
this.syncConfiguration = syncConfiguration;
this.nodeInformation = nodeInformation;
this.unknownBlockHashes = new HashMap<>();
this.config = config;
}

public BlockProcessResult processBlock(@Nonnull Block block, MessageChannel sender, boolean ignoreMissingHashes) {
Expand Down Expand Up @@ -134,6 +138,11 @@ public boolean hasBetterBlockToSync() {
return getLastKnownBlockNumber() >= getBestBlockNumber() + blocksDistance;
}

public boolean canBeIgnoredForUnclesRewards(long blockNumber) {
int blockDistance = config.getBlockchainConfig().getCommonConstants().getUncleGenerationLimit();
return blockNumber < getBestBlockNumber() - blockDistance;
}

public long getLastKnownBlockNumber() {
return this.lastKnownBlockNumber;
}
Expand Down
5 changes: 5 additions & 0 deletions rskj-core/src/main/java/co/rsk/net/NodeBlockProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public void processSkeletonRequest(@Nonnull final MessageChannel sender, long re
sender.sendMessage(responseMessage);
}

@Override
public boolean canBeIgnoredForUnclesRewards(long blockNumber) {
return blockSyncService.canBeIgnoredForUnclesRewards(blockNumber);
}

/**
*
* @param skeletonBlockNumber a block number that belongs to the skeleton
Expand Down
22 changes: 15 additions & 7 deletions rskj-core/src/main/java/co/rsk/net/NodeMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,28 @@ private void processBlockMessage(@Nonnull final MessageChannel sender, @Nonnull
return;
}

boolean wasOrphan = !this.blockProcessor.hasBlockInSomeBlockchain(block.getHash().getBytes());
BlockProcessResult result = this.blockProcessor.processBlock(sender, block);
if (blockProcessor.canBeIgnoredForUnclesRewards(block.getNumber())){
logger.trace("Block ignored: too far from best block {} {}", blockNumber, block.getShortHash());
Metrics.processBlockMessage("blockIgnored", block, sender.getPeerNodeID());
return;
}

Metrics.processBlockMessage("blockProcessed", block, sender.getPeerNodeID());
if (blockProcessor.hasBlockInSomeBlockchain(block.getHash().getBytes())){
logger.trace("Block ignored: it's included in blockchain {} {}", blockNumber, block.getShortHash());
Metrics.processBlockMessage("blockIgnored", block, sender.getPeerNodeID());
return;
}

BlockProcessResult result = this.blockProcessor.processBlock(sender, block);
Metrics.processBlockMessage("blockProcessed", block, sender.getPeerNodeID());
tryRelayBlock(sender, block, result);
recordEvent(sender, EventType.VALID_BLOCK);
tryRelayBlock(sender, block, wasOrphan, result);

Metrics.processBlockMessage("finish", block, sender.getPeerNodeID());
}

private void tryRelayBlock(@Nonnull MessageChannel sender, Block block, boolean wasOrphan, BlockProcessResult result) {
private void tryRelayBlock(@Nonnull MessageChannel sender, Block block, BlockProcessResult result) {
// is new block and it is not orphan, it is in some blockchain
if (wasOrphan && result.wasBlockAdded(block) && !this.blockProcessor.hasBetterBlockToSync()) {
if (result.wasBlockAdded(block) && !this.blockProcessor.hasBetterBlockToSync()) {
relayBlock(sender, block);
}
}
Expand Down
10 changes: 7 additions & 3 deletions rskj-core/src/test/java/co/rsk/net/BlockSyncServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package co.rsk.net;

import co.rsk.blockchain.utils.BlockGenerator;
import co.rsk.config.RskSystemProperties;
import co.rsk.net.sync.SyncConfiguration;
import co.rsk.test.builders.BlockChainBuilder;
import org.ethereum.core.Block;
Expand All @@ -36,7 +37,8 @@ public void sendBlockMessagesAndAddThemToBlockchain() {
Blockchain blockchain = BlockChainBuilder.ofSize(10 * i);
BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
BlockSyncService blockSyncService = new BlockSyncService(store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
Assert.assertEquals(10 * i, blockchain.getBestBlock().getNumber());

List<Block> extendedChain = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), i);
Expand All @@ -54,7 +56,8 @@ public void sendBlockMessagesAndAddThemToBlockchainInReverseOrder() {
Blockchain blockchain = BlockChainBuilder.ofSize(10 * i);
BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
BlockSyncService blockSyncService = new BlockSyncService(store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
Assert.assertEquals(10 * i, blockchain.getBestBlock().getNumber());

Block initialBestBlock = blockchain.getBestBlock();
Expand Down Expand Up @@ -82,7 +85,8 @@ public void sendBlockMessageAndAddItToBlockchainWithCommonAncestors() {
Blockchain blockchain = BlockChainBuilder.ofSize(10);
BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
BlockSyncService blockSyncService = new BlockSyncService(store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);

Block initialBestBlock = blockchain.getBestBlock();
Assert.assertEquals(10, initialBestBlock.getNumber());
Expand Down
Loading