Skip to content

Commit

Permalink
Merge pull request #277 from Holt666/develop
Browse files Browse the repository at this point in the history
refactor network protocols
  • Loading branch information
LucasMLK authored Sep 12, 2023
2 parents a12a927 + 27a9352 commit 29641df
Show file tree
Hide file tree
Showing 146 changed files with 3,395 additions and 8,206 deletions.
89 changes: 0 additions & 89 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

<netty.version>4.1.97.Final</netty.version>
<tuweni.version>2.3.1</tuweni.version>
<libp2p.version>1.0.0-RELEASE</libp2p.version>
<json.version>2.14.2</json.version>
<kryo.version>5.4.0</kryo.version>
<lombok.version>1.18.26</lombok.version>
Expand Down Expand Up @@ -88,18 +87,6 @@
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>libp2p-jvm-libp2p</id>
<url>https://dl.cloudsmith.io/public/libp2p/jvm-libp2p/maven/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>hyperledger.jfrog.io</id>
<url>https://hyperledger.jfrog.io/artifactory/besu-maven/</url>
Expand Down Expand Up @@ -532,82 +519,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.libp2p</groupId>
<artifactId>jvm-libp2p</artifactId>
<version>${libp2p.version}</version>
<exclusions>
<exclusion>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
<exclusion>
<artifactId>kotlin-stdlib</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</exclusion>
<exclusion>
<artifactId>kotlin-stdlib-common</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</exclusion>
<exclusion>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down
129 changes: 43 additions & 86 deletions src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

package io.xdag;

import io.libp2p.core.crypto.KEY_TYPE;
import io.libp2p.core.crypto.KeyKt;
import io.libp2p.core.crypto.PrivKey;
import io.xdag.cli.TelnetServer;
import io.xdag.config.Config;
import io.xdag.config.DevnetConfig;
Expand All @@ -40,21 +37,13 @@
import io.xdag.db.*;
import io.xdag.db.mysql.TransactionHistoryStoreImpl;
import io.xdag.db.rocksdb.*;
import io.xdag.mine.MinerServer;
import io.xdag.mine.manager.AwardManager;
import io.xdag.mine.manager.AwardManagerImpl;
import io.xdag.mine.manager.MinerManager;
import io.xdag.mine.manager.MinerManagerImpl;
import io.xdag.mine.miner.Miner;
import io.xdag.mine.miner.MinerStates;
import io.xdag.mine.randomx.RandomX;
import io.xdag.net.XdagClient;
import io.xdag.net.XdagServer;
import io.xdag.net.libp2p.Libp2pNetwork;
import io.xdag.net.manager.NetDBManager;
import io.xdag.net.manager.XdagChannelManager;
import io.xdag.crypto.RandomX;
import io.xdag.net.PeerClient;
import io.xdag.net.PeerServer;
import io.xdag.net.NetDBManager;
import io.xdag.net.ChannelManager;
import io.xdag.net.message.MessageQueue;
import io.xdag.net.message.NetDB;
import io.xdag.net.NetDB;
import io.xdag.net.node.NodeManager;
import io.xdag.rpc.Web3;
import io.xdag.rpc.Web3Impl;
Expand All @@ -63,12 +52,12 @@
import io.xdag.rpc.netty.*;
import io.xdag.rpc.serialize.JacksonBasedRpcSerializer;
import io.xdag.rpc.serialize.JsonRpcSerializer;
import io.xdag.utils.BytesUtils;
import io.xdag.utils.XdagTime;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.crypto.KeyPair;

import java.net.InetAddress;
import java.net.UnknownHostException;
Expand All @@ -81,48 +70,43 @@
@Setter
public class Kernel {

private Status status = Status.STOPPED;
private Config config;
private Wallet wallet;
private DatabaseFactory dbFactory;
private AddressStore addressStore;
private BlockStore blockStore;
private OrphanBlockStore orphanBlockStore;
private TransactionHistoryStore txHistoryStore;

private SnapshotStore SnapshotStore;
private Blockchain blockchain;
private NetDB netDB;
private XdagClient client;
private XdagChannelManager channelMgr;
private NodeManager nodeMgr;
private NetDBManager netDBMgr;
private XdagServer p2p;
private XdagSync sync;
private XdagPow pow;
protected Status status = Status.STOPPED;
protected Config config;
protected Wallet wallet;
protected KeyPair coinbase;
protected DatabaseFactory dbFactory;
protected AddressStore addressStore;
protected BlockStore blockStore;
protected OrphanBlockStore orphanBlockStore;
protected TransactionHistoryStore txHistoryStore;

protected SnapshotStore SnapshotStore;
protected Blockchain blockchain;
protected NetDB netDB;
protected PeerClient client;
protected ChannelManager channelMgr;
protected NodeManager nodeMgr;
protected NetDBManager netDBMgr;
protected PeerServer p2p;
protected XdagSync sync;
protected XdagPow pow;
private SyncManager syncMgr;

private byte[] firstAccount;
private Block firstBlock;
private Miner poolMiner;
private AwardManager awardManager;
private MinerManager minerManager;
private MinerServer minerServer;
private XdagState xdagState;
private Libp2pNetwork libp2pNetwork;
// protected DiscoveryController discoveryController;
private AtomicInteger channelsAccount = new AtomicInteger(0);
private PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
protected byte[] firstAccount;
protected Block firstBlock;
protected XdagState xdagState;

private TelnetServer telnetServer;
protected AtomicInteger channelsAccount = new AtomicInteger(0);

private RandomX randomx;
protected TelnetServer telnetServer;

protected RandomX randomx;

// 记录运行状态
private AtomicBoolean isRunning = new AtomicBoolean(false);
protected AtomicBoolean isRunning = new AtomicBoolean(false);
// 记录启动时间片
@Getter
private long startEpoch;
protected long startEpoch;


// rpc
Expand All @@ -136,13 +120,15 @@ public class Kernel {
public Kernel(Config config, Wallet wallet) {
this.config = config;
this.wallet = wallet;
this.coinbase = wallet.getDefKey();
this.xdagState = XdagState.INIT;
this.telnetServer = new TelnetServer(config.getAdminSpec().getTelnetIp(), config.getAdminSpec().getTelnetPort(),
this);
}

public Kernel(Config config) {
public Kernel(Config config, KeyPair coinbase) {
this.config = config;
this.coinbase = coinbase;
}

/**
Expand All @@ -158,7 +144,7 @@ public synchronized void testStart() throws Exception {
// ====================================
// start channel manager
// ====================================
channelMgr = new XdagChannelManager(this);
channelMgr = new ChannelManager(this);
channelMgr.start();
log.info("Channel Manager start...");
netDBMgr = new NetDBManager(this.config);
Expand Down Expand Up @@ -220,7 +206,6 @@ public synchronized void testStart() throws Exception {
// 如果是第一次启动,则新建一个创世块
if (xdagStats.getOurLastBlockHash() == null) {
firstAccount = Keys.toBytesAddress(wallet.getDefKey().getPublicKey());
poolMiner = new Miner(BytesUtils.arrayToByte32(firstAccount));
firstBlock = new Block(config, XdagTime.getCurrentTimestamp(), null, null, false, null, null, -1);
firstBlock.signOut(wallet.getDefKey());
xdagStats.setOurLastBlockHash(firstBlock.getHashLow().toArray());
Expand All @@ -230,7 +215,6 @@ public synchronized void testStart() throws Exception {
blockchain.tryToConnect(firstBlock);
} else {
firstAccount = Keys.toBytesAddress(wallet.getDefKey().getPublicKey());
poolMiner = new Miner(BytesUtils.arrayToByte32(firstAccount));
}
log.info("Blockchain init");

Expand Down Expand Up @@ -263,11 +247,10 @@ public synchronized void testStart() throws Exception {
// set up client
// ====================================

p2p = new XdagServer(this);
p2p = new PeerServer(this);
p2p.start();
log.info("Node server start...");
client = new XdagClient(this.config);
log.info("XdagClient nodeId {}", client.getNode().getHexId());
client = new PeerClient(this.config, this.coinbase);

// libp2pNetwork = new Libp2pNetwork(this);
// libp2pNetwork.start();
Expand Down Expand Up @@ -296,29 +279,10 @@ public synchronized void testStart() throws Exception {
syncMgr.start();
log.info("SyncManager start...");

// ====================================
// set up pool miner
// ====================================
poolMiner.setMinerStates(MinerStates.MINER_SERVICE);

// ====================================
// set up minermanager awardmanager
// ====================================
minerManager = new MinerManagerImpl(this);
awardManager = new AwardManagerImpl(this);
// ====================================
// poolnode open
// ====================================
minerServer = new MinerServer(this);
log.info("Pool Server init");

// ====================================
// pow
// ====================================
pow = new XdagPow(this);
minerManager.setPoW(pow);
minerManager.start();
awardManager.start();

// register pow
blockchain.registerListener(pow);
Expand Down Expand Up @@ -461,8 +425,7 @@ public synchronized void testStop() {
log.info("Node manager stop.");

log.info("ChannelManager stop.");
// discoveryController.stop();
// libp2pNetwork.stop();

// close timer
MessageQueue.timer.shutdown();

Expand All @@ -473,12 +436,6 @@ public synchronized void testStop() {
client.close();
log.info("Node client stop.");

minerServer.close();
log.info("Pool server stop.");
minerManager.stop();
log.info("Miner manager stop.");
awardManager.stop();

// 3. 数据层关闭
// TODO 关闭checkmain线程
blockchain.stopCheckMain();
Expand Down
22 changes: 1 addition & 21 deletions src/main/java/io/xdag/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,6 @@ private static synchronized void shutdownHook() {
LogManager.shutdown();
}

public static void logPoolInfo(Config config) {
log.info(
"Xdag Node IP Address:[{}:{}], Xdag Pool Service IP Address:[{}:{}],Configure:miner[{}],maxip[{}],maxconn[{}],fee[{}],reward[{}],direct[{}],fun[{}]",
config.getNodeSpec().getNodeIp(),
config.getNodeSpec().getNodePort(),
config.getPoolSpec().getPoolIp(),
config.getPoolSpec().getPoolPort(),
config.getPoolSpec().getGlobalMinerLimit(),
config.getPoolSpec().getMaxConnectPerIp(),
config.getPoolSpec().getMaxMinerPerAccount(),
config.getPoolSpec().getPoolRation(),
config.getPoolSpec().getRewardRation(),
config.getPoolSpec().getDirectRation(),
config.getPoolSpec().getFundRation());
}

/**
* Adds a supported option.
*/
Expand All @@ -141,7 +125,7 @@ protected CommandLine parseOptions(String[] args) throws ParseException {
return cmd;
}

protected Config buildConfig(String[] args) throws Exception {
protected Config buildConfig(String[] args) {
Config config = null;
for (String arg : args) {
if ("-d".equals(arg)) {
Expand All @@ -159,10 +143,6 @@ protected Config buildConfig(String[] args) throws Exception {
}
config.changePara(args);
config.setDir();
logPoolInfo(config);

// init keys
config.initKeys();

return config;
}
Expand Down
Loading

0 comments on commit 29641df

Please sign in to comment.