8000 fix pool whitelist bug by yyajphd · Pull Request #302 · XDagger/xdagj · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix pool whitelist bug #302

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

Merged
merged 1 commit into from
Mar 12, 2024
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
3 changes: 1 addition & 2 deletions src/main/java/io/xdag/config/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class AbstractConfig implements Config, AdminSpec, NodeSpec, WalletSpec,

protected int maxShareCountPerChannel = 20;
protected int awardEpoch = 0xf;
protected int waitEpoch = 20;
protected int waitEpoch = 32;
// =========================
// foundation spec
// =========================
Expand Down Expand Up @@ -164,7 +164,6 @@ protected AbstractConfig(String rootDir, String configName, Network network, sho
this.configName = configName;
this.network = network;
this.networkVersion = networkVersion;

getSetting();
setDir();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/xdag/net/websocket/PoolHandShakeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;

import static io.netty.handler.codec.http.HttpUtil.isKeepAlive;
import static io.xdag.utils.BasicUtils.extractIpAddress;

@Slf4j
@ChannelHandler.Sharable
Expand Down Expand Up @@ -52,13 +53,13 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
*/
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
boolean isIPAllowed;
String clientIP = ctx.channel().remoteAddress().toString();
String clientIPWithPort = ctx.channel().remoteAddress().toString();
// Determine the mining pool whitelist. If there is 0.0.0.0 in the whitelist, the whitelist is open.
// Any IP can connect to this node to become a pool.
// Otherwise, determine the specific IP
if (!allIPAllowed) {
// No 0.0.0.0 in the whitelist, determine the specific IP
isIPAllowed = clientIPList.contains(clientIP);
isIPAllowed = clientIPList.contains(extractIpAddress(clientIPWithPort));
} else {
isIPAllowed = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/xdag/pool/PoolAwardManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public int payPools(long time) {
}
if (compareTo(block.getNonce().slice(0, 20).toArray(), 0,
20, block.getCoinBase().getAddress().slice(8, 20).toArray(), 0, 20) == 0) {
log.debug("This block is not produced by mining and belongs to the node");
log.debug("This block is not produced by mining and belongs to the node, block hash:{}",
hashlow.toHexString());
return -3;
}
if (kernel.getBlockchain().getMemOurBlocks().get(hashlow) == null) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/xdag/utils/BasicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.CRC32;

import static io.xdag.config.Constants.HASH_RATE_LAST_MAX_TIME;
Expand Down Expand Up @@ -214,4 +216,15 @@ public static double div(double v1, double v2, int scale) {
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
}
// Parse and extract IP addresses, for example /133.22.245.177:11328 -> 133.22.245.177
public static String extractIpAddress(String ipAddressAndPort) {
String pattern = "/(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):\\d+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(ipAddressAndPort);
if (m.find()) {
return m.group(1);
} else {
return null;
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/io/xdag/utils/BasicUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

import static io.xdag.utils.BasicUtils.*;
import static io.xdag.utils.WalletUtils.toBase58;
Expand Down Expand Up @@ -178,5 +179,29 @@ public void testPoolWhiteIPList() {
assertTrue(poolWhiteIPList.contains("127.0.0.2"));
assertTrue(poolWhiteIPList.contains("0.0.0.0"));
assertFalse(poolWhiteIPList.contains("127.0.0.5"));
assertFalse(poolWhiteIPList.contains(null));
}

public String randomIpAddress(Random random) {
StringBuilder ipAddress = new StringBuilder();
for (int i = 0; i < 4; i++) {
ipAddress.append(random.nextInt(256));
if (i < 3) {
ipAddress.append(".");
}
}
return ipAddress.toString();
}

@Test
public void testParseIP() {
Random random = new Random();
for (int i = 0; i < 10000; i++) {
String ipAddress = randomIpAddress(random);
// Generate a random port number between 0 and 65535
int port = random.nextInt(65536);
String ipAddressWithPort = "/" + ipAddress + ":" + port;
assertEquals(ipAddress, extractIpAddress(ipAddressWithPort));
}
}
}
0