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

feat(snap): add extra checks; update snap discovery #2860

Merged
merged 13 commits into from
Jan 22, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void whenStartTheServerAndClientNodes_thenTheClientWillSynchWithServer()
boolean isClientSynced = false;

while (System.currentTimeMillis() < endTime) {
if (clientNode.getOutput().contains("CLIENT - Starting Snapshot sync.") && clientNode.getOutput().contains("CLIENT - Snapshot sync finished successfully!")) {
if (clientNode.getOutput().contains("Starting Snap sync") && clientNode.getOutput().contains("Snap sync finished successfully")) {
try {
JsonNode jsonResponse = OkHttpClientTestFixture.getJsonResponseForGetBestBlockMessage(portClientRpc, serverBestBlockNumber);
JsonNode jsonResult = jsonResponse.get(0).get("result");
Expand Down
70 changes: 69 additions & 1 deletion rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ public class RskContext implements NodeContext, NodeBootstrapper {
private ProofOfWorkRule proofOfWorkRule;
private ForkDetectionDataRule forkDetectionDataRule;
private BlockParentDependantValidationRule blockParentDependantValidationRule;
private BlockParentDependantValidationRule snapBlockParentDependantValidationRule;
private BlockValidationRule blockValidationRule;
private BlockValidationRule snapBlockValidationRule;
private BlockValidationRule minerServerBlockValidationRule;
private BlockValidator blockValidator;
private BlockValidator blockHeaderValidator;
Expand Down Expand Up @@ -1138,6 +1140,33 @@ public synchronized BlockValidationRule getBlockValidationRule() {
return blockValidationRule;
}

public synchronized BlockValidationRule getSnapBlockValidationRule() {
checkIfNotClosed();

if (snapBlockValidationRule == null) {
final RskSystemProperties rskSystemProperties = getRskSystemProperties();
final Constants commonConstants = rskSystemProperties.getNetworkConstants();
final BlockTimeStampValidationRule blockTimeStampValidationRule = new BlockTimeStampValidationRule(
commonConstants.getNewBlockMaxSecondsInTheFuture(),
rskSystemProperties.getActivationConfig(),
rskSystemProperties.getNetworkConstants()
);
snapBlockValidationRule = new BlockCompositeRule(
new TxsMinGasPriceRule(),
new BlockTxsMaxGasPriceRule(rskSystemProperties.getActivationConfig()),
new BlockRootValidationRule(rskSystemProperties.getActivationConfig()),
getProofOfWorkRule(),
new RemascValidationRule(),
blockTimeStampValidationRule,
new GasLimitRule(commonConstants.getMinGasLimit()),
new ExtraDataRule(commonConstants.getMaximumExtraDataSize()),
new ValidTxExecutionSublistsEdgesRule(getRskSystemProperties().getActivationConfig())
);
}

return snapBlockValidationRule;
}

public synchronized BlockParentDependantValidationRule getBlockParentDependantValidationRule() {
checkIfNotClosed();

Expand All @@ -1156,6 +1185,23 @@ public synchronized BlockParentDependantValidationRule getBlockParentDependantVa
return blockParentDependantValidationRule;
}

public synchronized BlockParentDependantValidationRule getSnapBlockParentDependantValidationRule() {
checkIfNotClosed();

if (snapBlockParentDependantValidationRule == null) {
Constants commonConstants = getRskSystemProperties().getNetworkConstants();
snapBlockParentDependantValidationRule = new BlockParentCompositeRule(
new BlockTxsFieldsValidationRule(getBlockTxSignatureCache()),
new PrevMinGasPriceRule(),
new BlockParentNumberRule(),
new BlockDifficultyRule(getDifficultyCalculator()),
new BlockParentGasLimitRule(commonConstants.getGasLimitBoundDivisor())
);
}

return snapBlockParentDependantValidationRule;
}

public synchronized org.ethereum.db.BlockStore buildBlockStore(String databaseDir) {
checkIfNotClosed();

Expand Down Expand Up @@ -1480,7 +1526,6 @@ protected synchronized SyncConfiguration buildSyncConfiguration() {
rskSystemProperties.getTopBest(),
rskSystemProperties.isServerSnapshotSyncEnabled(),
rskSystemProperties.isClientSnapshotSyncEnabled(),
rskSystemProperties.getSnapshotChunkTimeout(),
rskSystemProperties.getSnapshotSyncLimit(),
rskSystemProperties.getSnapBootNodes());
}
Expand Down Expand Up @@ -2015,13 +2060,36 @@ private SyncPool getSyncPool() {

private SnapshotProcessor getSnapshotProcessor() {
if (snapshotProcessor == null) {
final RskSystemProperties rskSystemProperties = getRskSystemProperties();
final Constants commonConstants = rskSystemProperties.getNetworkConstants();
final BlockTimeStampValidationRule blockTimeStampValidationRule = new BlockTimeStampValidationRule(
commonConstants.getNewBlockMaxSecondsInTheFuture(),
rskSystemProperties.getActivationConfig(),
rskSystemProperties.getNetworkConstants()
);

snapshotProcessor = new SnapshotProcessor(
getBlockchain(),
getTrieStore(),
getPeersInformation(),
getBlockStore(),
getTransactionPool(),
getSnapBlockParentDependantValidationRule(),
getSnapBlockValidationRule(),
new BlockHeaderParentCompositeRule(
new PrevMinGasPriceRule(),
new BlockParentNumberRule(),
blockTimeStampValidationRule,
new BlockDifficultyRule(getDifficultyCalculator()),
new BlockParentGasLimitRule(commonConstants.getGasLimitBoundDivisor())
),
new BlockHeaderCompositeRule(
getProofOfWorkRule(),
blockTimeStampValidationRule,
new ValidGasUsedRule()
),
getRskSystemProperties().getSnapshotChunkSize(),
getRskSystemProperties().checkHistoricalHeaders(),
getRskSystemProperties().isSnapshotParallelEnabled()
);
}
Expand Down
27 changes: 7 additions & 20 deletions rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.ethereum.crypto.ECKey;
import org.ethereum.crypto.HashUtil;
import org.ethereum.listener.GasPriceCalculator;
import org.ethereum.net.client.Capability;
import org.ethereum.vm.PrecompiledContracts;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -74,6 +73,7 @@ public class RskSystemProperties extends SystemProperties {
public static final String USE_PEERS_FROM_LAST_SESSION = "peer.discovery.usePeersFromLastSession";

public static final String PROPERTY_SNAP_CLIENT_ENABLED = "sync.snapshot.client.enabled";
public static final String PROPERTY_SNAP_CLIENT_CHECK_HISTORICAL_HEADERS = "sync.snapshot.client.checkHistoricalHeaders";
public static final String PROPERTY_SNAP_NODES = "sync.snapshot.client.snapBootNodes";

//TODO: REMOVE THIS WHEN THE LocalBLockTests starts working with REMASC
Expand Down Expand Up @@ -430,20 +430,7 @@ public int getLongSyncLimit() {
public boolean isServerSnapshotSyncEnabled() { return configFromFiles.getBoolean("sync.snapshot.server.enabled");}
public boolean isClientSnapshotSyncEnabled() { return configFromFiles.getBoolean(PROPERTY_SNAP_CLIENT_ENABLED);}

@Override
public List<String> peerCapabilities() {
List<String> capabilities = super.peerCapabilities();

if (isSnapshotSyncEnabled()) {
capabilities.add(Capability.SNAP);
}

return capabilities;
}

public int getSnapshotChunkTimeout() {
return configFromFiles.getInt("sync.snapshot.client.chunkRequestTimeout");
}
public boolean checkHistoricalHeaders() { return configFromFiles.getBoolean(PROPERTY_SNAP_CLIENT_CHECK_HISTORICAL_HEADERS);}

public boolean isSnapshotParallelEnabled() { return configFromFiles.getBoolean("sync.snapshot.client.parallel");}

Expand Down Expand Up @@ -528,10 +515,14 @@ public boolean fastBlockPropagation() {
return configFromFiles.getBoolean("peer.fastBlockPropagation");
}

public Integer getMessageQueueMaxSize() {
public int getMessageQueueMaxSize() {
return configFromFiles.getInt("peer.messageQueue.maxSizePerPeer");
}

public int getMessageQueuePerMinuteThreshold() {
return configFromFiles.getInt("peer.messageQueue.thresholdPerMinutePerPeer");
}

public boolean rpcZeroSignatureIfRemasc() {
return configFromFiles.getBoolean("rpc.zeroSignatureIfRemasc");
}
Expand Down Expand Up @@ -570,10 +561,6 @@ public GasPriceCalculator.GasCalculatorType getGasCalculatorType() {
return gasCalculatorType;
}

public boolean isSnapshotSyncEnabled(){
return isServerSnapshotSyncEnabled() || isClientSnapshotSyncEnabled();
}

private void fetchMethodTimeout(Config configElement, Map<String, Long> methodTimeoutMap) {
configElement.getObject("methods.timeout")
.unwrapped()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class BlockValidatorImpl implements BlockValidator {

private static final Logger logger = LoggerFactory.getLogger("blocksyncservice");

private BlockStore blockStore;
private final BlockStore blockStore;

private BlockParentDependantValidationRule blockParentValidator;
private final BlockParentDependantValidationRule blockParentValidator;

private BlockValidationRule blockValidator;
private final BlockValidationRule blockValidator;

public BlockValidatorImpl(BlockStore blockStore, BlockParentDependantValidationRule blockParentValidator, BlockValidationRule blockValidator) {
this.blockStore = blockStore;
Expand Down
11 changes: 8 additions & 3 deletions rskj-core/src/main/java/co/rsk/net/NodeMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private void tryAddMessage(Peer sender, Message message, NodeMsgTraceInfo nodeMs
*/
private boolean controlMessageIngress(Peer sender, Message message, double score) {
return
allowByScore(score) &&
allowByScore(sender, message, score) &&
allowByMessageCount(sender) &&
allowByMinerNotBanned(sender, message) &&
allowByMessageUniqueness(sender, message); // prevent repeated is the most expensive and MUST be the last
Expand All @@ -221,8 +221,13 @@ private boolean controlMessageIngress(Peer sender, Message message, double score
/**
* assert score is acceptable
*/
private boolean allowByScore(double score) {
return score >= 0;
private boolean allowByScore(Peer sender, Message message, double score) {
boolean allow = score >= 0;
if (!allow) {
logger.debug("Message: [{}] from: [{}] with score: [{}] was not allowed", message.getMessageType(), sender, score);
}

return allow;
}

/**
Expand Down
Loading
Loading