Skip to content

Commit

Permalink
Merge pull request #2251 from rsksmart/master
Browse files Browse the repository at this point in the history
Merge master -> rskip351+rskip144
  • Loading branch information
Vovchyk authored Feb 7, 2024
2 parents 48df218 + 5844266 commit 964eb45
Show file tree
Hide file tree
Showing 106 changed files with 4,677 additions and 3,468 deletions.
6 changes: 4 additions & 2 deletions rskj-core/src/main/java/co/rsk/RskContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -1591,17 +1591,19 @@ protected PeerExplorer getPeerExplorer() {
initialBootNodes.add(address.getHostName() + ":" + address.getPort());
}
}
int bucketSize = rskSystemProperties.discoveryBucketSize();
peerExplorer = new PeerExplorer(
initialBootNodes,
localNode,
new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, localNode),
new NodeDistanceTable(KademliaOptions.BINS, bucketSize, localNode),
key,
rskSystemProperties.peerDiscoveryMessageTimeOut(),
rskSystemProperties.peerDiscoveryRefreshPeriod(),
rskSystemProperties.peerDiscoveryCleanPeriod(),
rskSystemProperties.networkId(),
getPeerScoringManager(),
rskSystemProperties.allowMultipleConnectionsPerHostPort()
rskSystemProperties.allowMultipleConnectionsPerHostPort(),
rskSystemProperties.peerDiscoveryMaxBootRetries()
);
}

Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/main/java/co/rsk/config/BridgeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.Federation;
import co.rsk.peg.federation.Federation;
import java.util.List;

import org.ethereum.config.blockchain.upgrades.ActivationConfig;
Expand Down
13 changes: 5 additions & 8 deletions rskj-core/src/main/java/co/rsk/config/BridgeDevNetConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -57,12 +58,8 @@ public BridgeDevNetConstants(List<BtcECKey> federationPublicKeys) {

// Expected federation address is:
// 2NCEo1RdmGDj6MqiipD6DUSerSxKv79FNWX
genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 1;
btc2RskMinimumAcceptableConfirmationsOnRsk = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import com.google.common.collect.Lists;
import java.time.Instant;
import java.util.Arrays;
Expand Down Expand Up @@ -52,12 +53,8 @@ public class BridgeMainNetConstants extends BridgeConstants {
// Wednesday, January 3, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1514948400L);

genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 100;
btc2RskMinimumAcceptableConfirmationsOnRsk = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -55,12 +56,8 @@ public BridgeRegTestConstants(List<BtcECKey> federationPublicKeys) {

Instant genesisFederationCreatedAt = ZonedDateTime.parse("2016-01-01T00:00:00Z").toInstant();

genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 3;
btc2RskMinimumAcceptableConfirmationsOnRsk = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import co.rsk.bitcoinj.core.Coin;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.peg.AddressBasedAuthorizer;
import co.rsk.peg.FederationMember;
import co.rsk.peg.StandardMultisigFederation;
import co.rsk.peg.federation.FederationArgs;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.federation.FederationFactory;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -65,12 +66,8 @@ public class BridgeTestNetConstants extends BridgeConstants {
// Currently set to: Monday, October 8, 2018 12:00:00 AM GMT-03:00
Instant genesisFederationAddressCreatedAt = Instant.ofEpochMilli(1538967600l);

genesisFederation = new StandardMultisigFederation(
federationMembers,
genesisFederationAddressCreatedAt,
1L,
getBtcParams()
);
FederationArgs federationArgs = new FederationArgs(federationMembers, genesisFederationAddressCreatedAt, 1L, getBtcParams());
genesisFederation = FederationFactory.buildStandardMultiSigFederation(federationArgs);

btc2RskMinimumAcceptableConfirmations = 10;
btc2RskMinimumAcceptableConfirmationsOnRsk = 10;
Expand Down
13 changes: 13 additions & 0 deletions rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package co.rsk.config;

import co.rsk.core.RskAddress;
import co.rsk.net.discovery.table.KademliaOptions;
import co.rsk.rpc.ModuleDescription;
import com.google.common.annotations.VisibleForTesting;
import com.typesafe.config.Config;
Expand Down Expand Up @@ -49,6 +50,9 @@ public class RskSystemProperties extends SystemProperties {
private static final int PD_DEFAULT_CLEAN_PERIOD = 15000; //miliseconds
private static final int PD_DEFAULT_TIMEOUT_MESSAGE = PD_DEFAULT_CLEAN_PERIOD - 1; //miliseconds
private static final int PD_DEFAULT_REFRESH_PERIOD = 60000; //miliseconds
private static final int PD_DEFAULT_MAX_BOOTSTRAP_RETRIES = -1;

private static final String PD_MAX_BOOTSTRAP_RETRIES_CONFIG = "peer.discovery.maxBootRetries";

private static final String REGTEST_BLOCKCHAIN_CONFIG = "regtest";

Expand All @@ -58,6 +62,7 @@ public class RskSystemProperties extends SystemProperties {
private static final String RPC_ETH_GET_LOGS_MAX_BLOCKS_TO_QUERY = "rpc.logs.maxBlocksToQuery";
private static final String RPC_ETH_GET_LOGS_MAX_LOGS_TO_RETURN = "rpc.logs.maxLogsToReturn";
private static final String RPC_GAS_PRICE_MULTIPLIER_CONFIG = "rpc.gasPriceMultiplier";
private static final String DISCOVERY_BUCKET_SIZE = "peer.discovery.bucketSize";

private static final int CHUNK_SIZE = 192;

Expand Down Expand Up @@ -276,6 +281,14 @@ public boolean allowMultipleConnectionsPerHostPort() {
return getBoolean("peer.discovery.allowMultipleConnectionsPerHostPort", true);
}

public long peerDiscoveryMaxBootRetries() {
return getLong(PD_MAX_BOOTSTRAP_RETRIES_CONFIG, PD_DEFAULT_MAX_BOOTSTRAP_RETRIES);
}

public int discoveryBucketSize() {
return getInt(DISCOVERY_BUCKET_SIZE, KademliaOptions.BUCKET_SIZE);
}

public List<ModuleDescription> getRpcModules() {
if (this.moduleDescriptions != null) {
return this.moduleDescriptions;
Expand Down
41 changes: 31 additions & 10 deletions rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand All @@ -63,6 +58,9 @@ public class PeerExplorer {
private static final int MAX_NODES_TO_CHECK = 16;
private static final int RETRIES_COUNT = 3;

private final List<String> initialBootNodes;
private int retryCounter = 0;
private final long maxBootRetries;
private final Set<InetSocketAddress> bootNodes = ConcurrentHashMap.newKeySet();

private final Map<String, PeerDiscoveryRequest> pendingPingRequests = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -98,12 +96,13 @@ public class PeerExplorer {
public PeerExplorer(List<String> initialBootNodes,
Node localNode, NodeDistanceTable distanceTable, ECKey key,
long reqTimeOut, long updatePeriod, long cleanPeriod, Integer networkId,
PeerScoringManager peerScoringManager, boolean allowMultipleConnectionsPerHostPort) {
PeerScoringManager peerScoringManager, boolean allowMultipleConnectionsPerHostPort, long maxBootRetries) {
this.localNode = localNode;
this.key = key;
this.distanceTable = distanceTable;
this.updateEntryLock = new ReentrantLock();
this.networkId = networkId;
this.initialBootNodes = Collections.unmodifiableList(new ArrayList<>(initialBootNodes));
loadInitialBootNodes(initialBootNodes);

this.cleaner = new PeerExplorerCleaner(updatePeriod, cleanPeriod, this);
Expand All @@ -114,6 +113,8 @@ public PeerExplorer(List<String> initialBootNodes,

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

this.maxBootRetries = maxBootRetries;
}

void start() {
Expand Down Expand Up @@ -149,6 +150,11 @@ ExecState getState() {
return state;
}

@VisibleForTesting
int getRetryCounter() {
return retryCounter;
}

@VisibleForTesting
Set<String> startConversationWithNewNodes() {
Set<String> sentAddresses = new HashSet<>();
Expand Down Expand Up @@ -429,10 +435,25 @@ synchronized void update() {

List<Node> closestNodes = this.distanceTable.getClosestNodes(this.localNode.getId());

logger.trace("update - closestNodes: [{}]", closestNodes);
if (shouldRetryConnection(closestNodes)) {
if (maxBootRetries == -1 || retryCounter < maxBootRetries) {
retryCounter++;
logger.info("retrying connection to bootstrap nodes. Attempt: {}", retryCounter);
loadInitialBootNodes(initialBootNodes);
startConversationWithNewNodes();
} else {
logger.warn("max retry attempts reached.");
}
} else {
logger.trace("update - closestNodes: [{}]", closestNodes);
this.askForMoreNodes(closestNodes);
this.checkPeersPulse(closestNodes);
}
}

this.askForMoreNodes(closestNodes);
this.checkPeersPulse(closestNodes);
private boolean shouldRetryConnection(List<Node> closestNodes) {
return closestNodes.isEmpty() && bootNodes.isEmpty() && pendingPingRequests.isEmpty() &&
pendingFindNodeRequests.isEmpty() && establishedConnections.isEmpty();
}

private void checkPeersPulse(List<Node> closestNodes) {
Expand Down
30 changes: 27 additions & 3 deletions rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import co.rsk.util.ExecState;
import com.google.common.annotations.VisibleForTesting;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannelConfig;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,6 +42,8 @@ public class UDPServer implements InternalService {

private static final Logger logger = LoggerFactory.getLogger(UDPServer.class);

private static final int BUFFER_SIZE = 8_192;

private final int port;
private final String address;

Expand Down Expand Up @@ -163,6 +164,29 @@ private Bootstrap createBootstrap(EventLoopGroup group) {
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
public void initChannel(@Nonnull NioDatagramChannel ch) {
DatagramChannelConfig channelConfig = ch.config();
channelConfig.setRecvByteBufAllocator(new FixedRecvByteBufAllocator(BUFFER_SIZE));

Integer defaultSndBuf = channelConfig.getOption(ChannelOption.SO_SNDBUF);
if (defaultSndBuf == null || defaultSndBuf < BUFFER_SIZE) {
logger.info("Default {} size of {} bytes is not sufficient. Changing to {}",
ChannelOption.SO_SNDBUF, defaultSndBuf, BUFFER_SIZE);
channelConfig.setOption(ChannelOption.SO_SNDBUF, BUFFER_SIZE);
}

Integer defaultRcvBuf = channelConfig.getOption(ChannelOption.SO_RCVBUF);
if (defaultRcvBuf == null || defaultRcvBuf < BUFFER_SIZE) {
logger.info("Default {} size of {} bytes is not sufficient. Changing to {}",
ChannelOption.SO_RCVBUF, defaultRcvBuf, BUFFER_SIZE);
channelConfig.setOption(ChannelOption.SO_RCVBUF, BUFFER_SIZE);
}

logger.info("Init channel with {}({}), {}={}, {}={}",
FixedRecvByteBufAllocator.class.getSimpleName(),
BUFFER_SIZE,
ChannelOption.SO_SNDBUF, channelConfig.getOption(ChannelOption.SO_SNDBUF),
ChannelOption.SO_RCVBUF, channelConfig.getOption(ChannelOption.SO_RCVBUF));

ch.pipeline().addLast(new PacketDecoder());
UDPChannel udpChannel = new UDPChannel(ch, peerExplorer);
peerExplorer.setUDPChannel(udpChannel);
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/java/co/rsk/peg/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import co.rsk.crypto.Keccak256;
import co.rsk.panic.PanicProcessor;
import co.rsk.peg.bitcoin.MerkleBranch;
import co.rsk.peg.federation.Federation;
import co.rsk.peg.federation.FederationMember;
import co.rsk.peg.flyover.FlyoverTxResponseCodes;
import co.rsk.peg.utils.BtcTransactionFormatUtils;
import co.rsk.peg.whitelist.LockWhitelistEntry;
Expand Down
1 change: 1 addition & 0 deletions rskj-core/src/main/java/co/rsk/peg/BridgeBtcWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import co.rsk.bitcoinj.core.Context;
import co.rsk.bitcoinj.wallet.RedeemData;
import co.rsk.bitcoinj.wallet.Wallet;
import co.rsk.peg.federation.Federation;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down
Loading

0 comments on commit 964eb45

Please sign in to comment.