Skip to content

Commit

Permalink
Updating file name, property path and known peer service to be includ…
Browse files Browse the repository at this point in the history
…ed into peerExplorer

Updating known peers origin, adding tests and small fixes
Removing unnecesary log to avoid log injection
Load active peers from previous session saved into file.
  • Loading branch information
asoto-iov committed Feb 14, 2024
1 parent ada6771 commit e155aa7
Show file tree
Hide file tree
Showing 16 changed files with 413 additions and 37 deletions.
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/NodeRunnerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public synchronized void stop() {
logger.info("Shutting down RSK node");

for (int i = internalServices.size() - 1; i >= 0; i--) {
internalServices.get(i).stop();
internalServices.get(i).stop();
}

logger.info("RSK node Shut down");
Expand Down
47 changes: 37 additions & 10 deletions rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import co.rsk.metrics.HashRateCalculatorNonMining;
import co.rsk.mine.*;
import co.rsk.net.*;
import co.rsk.net.discovery.KnownPeersSaver;
import co.rsk.net.discovery.PeerExplorer;
import co.rsk.net.discovery.UDPServer;
import co.rsk.net.discovery.table.KademliaOptions;
Expand Down Expand Up @@ -251,7 +252,6 @@ public class RskContext implements NodeContext, NodeBootstrapper {
private TxQuotaChecker txQuotaChecker;
private GasPriceTracker gasPriceTracker;
private BlockChainFlusher blockChainFlusher;

private final Map<String, DbKind> dbPathToDbKindMap = new HashMap<>();

private volatile boolean closed;
Expand Down Expand Up @@ -1582,17 +1582,16 @@ protected PeerExplorer getPeerExplorer() {
rskSystemProperties.getPublicIp(),
rskSystemProperties.getPeerPort()
);
List<String> initialBootNodes = rskSystemProperties.peerDiscoveryIPList();
List<Node> activePeers = rskSystemProperties.peerActive();
if (activePeers != null) {
for (Node n : activePeers) {
InetSocketAddress address = n.getAddress();
initialBootNodes.add(address.getHostName() + ":" + address.getPort());
}

KnownPeersSaver knownPeersSaver = null;
if(rskSystemProperties.usePeersFromLastSession()) {
knownPeersSaver = new KnownPeersSaver(getRskSystemProperties().getLastKnewPeersFilePath());

}

int bucketSize = rskSystemProperties.discoveryBucketSize();
peerExplorer = new PeerExplorer(
initialBootNodes,
getInitialBootNodes(),
localNode,
new NodeDistanceTable(KademliaOptions.BINS, bucketSize, localNode),
key,
Expand All @@ -1602,13 +1601,41 @@ protected PeerExplorer getPeerExplorer() {
rskSystemProperties.networkId(),
getPeerScoringManager(),
rskSystemProperties.allowMultipleConnectionsPerHostPort(),
rskSystemProperties.peerDiscoveryMaxBootRetries()
rskSystemProperties.peerDiscoveryMaxBootRetries(),
knownPeersSaver
);
}

return peerExplorer;
}

List<String> getInitialBootNodes() {
List<String> initialBootNodes = new ArrayList<>();
RskSystemProperties rskSystemProperties = getRskSystemProperties();
List<String> peerDiscoveryIPList = rskSystemProperties.peerDiscoveryIPList();
if (peerDiscoveryIPList != null) {
initialBootNodes.addAll(peerDiscoveryIPList);
}
List<Node> activePeers = rskSystemProperties.peerActive();
if (activePeers != null) {
for (Node n : activePeers) {
InetSocketAddress address = n.getAddress();
initialBootNodes.add(address.getHostName() + ":" + address.getPort());
}
}

if (rskSystemProperties.usePeersFromLastSession()) {
List<String> peerLastSession = rskSystemProperties.peerLastSession();
logger.debug("Loading peers from previous session: {}",peerLastSession);
for(String peer: peerLastSession) {
if (!initialBootNodes.contains(peer)) {
initialBootNodes.add(peer);
}
}
}
return initialBootNodes;
}

private BlockChainLoader getBlockChainLoader() {
if (blockChainLoader == null) {
blockChainLoader = new BlockChainLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class RskSystemProperties extends SystemProperties {
private static final int CHUNK_SIZE = 192;

public static final String PROPERTY_SYNC_TOP_BEST = "sync.topBest";
public static final String USE_PEERS_FROM_LAST_SESSION = "peer.discovery.usePeersFromLastSession";

//TODO: REMOVE THIS WHEN THE LocalBLockTests starts working with REMASC
private boolean remascEnabled = true;
Expand Down Expand Up @@ -251,6 +252,10 @@ public boolean skipRemasc() {
return getBoolean("rpc.skipRemasc", false);
}

public boolean usePeersFromLastSession() {
return getBoolean(USE_PEERS_FROM_LAST_SESSION, false);
}

public long peerDiscoveryMessageTimeOut() {
return getLong("peer.discovery.msg.timeout", PD_DEFAULT_TIMEOUT_MESSAGE);
}
Expand Down
58 changes: 58 additions & 0 deletions rskj-core/src/main/java/co/rsk/net/discovery/KnownPeersSaver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of RskJ
* Copyright (C) 2024 RSK Labs Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.rsk.net.discovery;

import org.ethereum.util.SimpleFileWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

public class KnownPeersSaver {
private static final Logger logger = LoggerFactory.getLogger(KnownPeersSaver.class);
private final Path peerListFileDir;
private final SimpleFileWriter fileDataSaver;

public KnownPeersSaver(Path peerListFileDir) {
this(peerListFileDir, SimpleFileWriter.getInstance());
}

public KnownPeersSaver(Path peerListFileDir, SimpleFileWriter fileDataSaver) {
this.peerListFileDir = peerListFileDir;
this.fileDataSaver = fileDataSaver;
}

public void savePeers(List<String> knownPeers) {
logger.debug("Stop in progress.. Saving known peers list to file");
if (knownPeers != null && !knownPeers.isEmpty()) {

StringBuilder sb = new StringBuilder();
for (String peerAddress : knownPeers) {
logger.debug("Saving knew peer: {}", peerAddress);
sb.append(peerAddress).append("\n");
}
try {
fileDataSaver.saveDataIntoFile(sb.toString(), peerListFileDir);
} catch (IOException e) {
logger.error("Error saving active peers to file: {}", e.getMessage());
}
}
}
}
20 changes: 19 additions & 1 deletion rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,20 @@ public class PeerExplorer {

private UDPChannel udpChannel;

private KnownPeersSaver knownPeersSaver;

public PeerExplorer(List<String> initialBootNodes,
Node localNode, NodeDistanceTable distanceTable, ECKey key,
long reqTimeOut, long updatePeriod, long cleanPeriod, Integer networkId,
PeerScoringManager peerScoringManager, boolean allowMultipleConnectionsPerHostPort, long maxBootRetries) {
this(initialBootNodes, localNode, distanceTable, key, reqTimeOut, updatePeriod, cleanPeriod, networkId, peerScoringManager, allowMultipleConnectionsPerHostPort, maxBootRetries, null);
}

public PeerExplorer(List<String> initialBootNodes,
Node localNode, NodeDistanceTable distanceTable, ECKey key,
long reqTimeOut, long updatePeriod, long cleanPeriod, Integer networkId,
PeerScoringManager peerScoringManager, boolean allowMultipleConnectionsPerHostPort,
long maxBootRetries, KnownPeersSaver knownPeersSaver) {
this.localNode = localNode;
this.key = key;
this.distanceTable = distanceTable;
Expand All @@ -108,13 +118,13 @@ public PeerExplorer(List<String> initialBootNodes,
this.cleaner = new PeerExplorerCleaner(updatePeriod, cleanPeriod, this);
this.challengeManager = new NodeChallengeManager();
this.requestTimeout = reqTimeOut;

this.peerScoringManager = peerScoringManager;

this.knownHosts = new ConcurrentHashMap<>();
this.allowMultipleConnectionsPerHostPort = allowMultipleConnectionsPerHostPort;

this.maxBootRetries = maxBootRetries;
this.knownPeersSaver = knownPeersSaver;
}

void start() {
Expand All @@ -136,6 +146,10 @@ synchronized void start(boolean startConversation) {
}

public synchronized void dispose() {
if(knownPeersSaver != null) {
knownPeersSaver.savePeers(getKnownHosts());
}

if (state == ExecState.FINISHED) {
logger.warn("Cannot dispose peer explorer as current state is {}", state);
return;
Expand Down Expand Up @@ -601,4 +615,8 @@ private boolean isBanned(Node node) {

return address != null && this.peerScoringManager.isAddressBanned(address) || this.peerScoringManager.isNodeIDBanned(node.getId());
}

List<String> getKnownHosts(){
return new ArrayList<>(knownHosts.keySet());
}
}
20 changes: 20 additions & 0 deletions rskj-core/src/main/java/org/ethereum/config/SystemProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -106,6 +109,7 @@ public abstract class SystemProperties {
public static final String PROPERTY_PERSIST_BLOOMS_CACHE_SNAPSHOT = "cache.blooms.persist-snapshot";

/* Testing */
public static final String LAST_KNEW_PEERS_FILE = "lastPeers.properties";
private static final Boolean DEFAULT_VMTEST_LOAD_LOCAL = false;

protected final Config configFromFiles;
Expand Down Expand Up @@ -251,6 +255,18 @@ public List<Node> peerActive() {
return list.stream().map(this::parsePeer).collect(Collectors.toList());
}

public List<String> peerLastSession() {
Path lastConnectedPeersFile = getLastKnewPeersFilePath();
try {
if (Files.exists(lastConnectedPeersFile)) {
return Files.readAllLines(lastConnectedPeersFile);
}
} catch (IOException e) {
logger.error("Failed to read last connected peers file path {}. Error: {}", lastConnectedPeersFile.toAbsolutePath(), e.getMessage());
}
return Collections.emptyList();
}

private Node parsePeer(ConfigObject configObject) {
if (configObject.get("url") != null) {
String url = configObject.toConfig().getString("url");
Expand Down Expand Up @@ -312,6 +328,10 @@ public String databaseDir() {
return databaseDir == null ? configFromFiles.getString(PROPERTY_BASE_PATH) : databaseDir;
}

public Path getLastKnewPeersFilePath() {
return Paths.get(databaseDir(), LAST_KNEW_PEERS_FILE);
}

public void setDataBaseDir(String dataBaseDir) {
this.databaseDir = dataBaseDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.ethereum.net.server;

import co.rsk.config.RskSystemProperties;
import co.rsk.net.Peer;
import co.rsk.net.NodeID;
import co.rsk.net.Peer;
import co.rsk.net.Status;
import co.rsk.net.messages.*;
import co.rsk.scoring.InetAddressUtils;
Expand Down Expand Up @@ -175,6 +175,7 @@ private boolean isRecent(Instant disconnectionTimeout, Instant currentTime) {
}

private void addToActives(Channel peer) {

if (peer.isUsingNewProtocol() || peer.hasEthStatusSucceeded()) {
syncPool.add(peer);
synchronized (activePeersLock) {
Expand Down
52 changes: 52 additions & 0 deletions rskj-core/src/main/java/org/ethereum/util/SimpleFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of RskJ
* Copyright (C) 2023 RSK Labs Ltd.
* (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class SimpleFileWriter {
private static final String TMP = ".tmp";
private static SimpleFileWriter instance;

private SimpleFileWriter() {
}

public static SimpleFileWriter getInstance() {
if (instance == null) {
instance = new SimpleFileWriter();
}
return instance;
}

public void saveDataIntoFile(String data, Path filePath) throws IOException {
File tempFile = File.createTempFile(filePath.toString(), TMP);
try (FileWriter writer = new FileWriter(tempFile, false)) {
writer.write(data);
}

Files.move(tempFile.toPath(), filePath, REPLACE_EXISTING);
}
}

2 changes: 2 additions & 0 deletions rskj-core/src/main/resources/expected.conf
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ blockchain = {
}

peer = {

active = [
{
url = <url>
Expand Down Expand Up @@ -137,6 +138,7 @@ peer = {
allowMultipleConnectionsPerHostPort = <bool>
maxBootRetries = <long>
bucketSize = <number>
usePeersFromLastSession = <boolean>
}
port = <port>
networkId = <networkId>
Expand Down
8 changes: 6 additions & 2 deletions rskj-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ peer {
discovery = {
# allow multiple connections per host by default
allowMultipleConnectionsPerHostPort = true
# allows to specify a number of attempts to discover at least one peer. By default, it's -1, which means an infinite number of attempts
maxBootRetries = -1

# If true, the node will try to connect to the peers from the last session
usePeersFromLastSession = false

# allows to specify a number of attempts to discover at least one peer. By default, it's -1, which means an infinite number of attempts
maxBootRetries = -1
}

# flag that allows to propagate a received block without executing it and only checking basic validation rules.
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/test/java/co/rsk/NodeRunnerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,6 @@ void nodeIsAlreadyStopped_WhenStopNode_ThenShouldNotThrowError() throws Exceptio
fail();
}
}


}
Loading

0 comments on commit e155aa7

Please sign in to comment.