Skip to content

Commit

Permalink
Merge pull request #98 from qixuan123456/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
LucasMLK authored Jun 28, 2022
2 parents 59bdaa3 + 88ce5f6 commit 56c3fce
Show file tree
Hide file tree
Showing 56 changed files with 1,281 additions and 160 deletions.
4 changes: 2 additions & 2 deletions docs/XDAGJ_Devnet_Tutorial_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
```shell
$mkdir run

#Please copy xdag-devnet.config, xdag.sh & xdagj-0.4.8-shaded.jar into /run
# xdag-devnet.config is in /src-main-resources; xdag.sh is in /script; xdagj-0.4.8-shaded.jar is in /target
#Please copy xdag-devnet.config, xdag.sh & xdagj-0.4.9-shaded.jar into /run
# xdag-devnet.config is in /src-main-resources; xdag.sh is in /script; xdagj-0.4.9-shaded.jar is in /target

$cd run
$sh xdag.sh -d
Expand Down
2 changes: 1 addition & 1 deletion docs/XDAGJ_Devnet_Tutorial_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

```shell
cd pool
nohup java -jar xdagj-0.4.8-shaded.jar > xdagj.log 2>&1 &
nohup java -jar xdagj-0.4.9-shaded.jar > xdagj.log 2>&1 &
#等待系统启动完毕,采用telnet接入
telnet 127.0.0.1:6001
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.xdag</groupId>
<artifactId>xdagj</artifactId>
<version>0.4.8</version>
<version>0.4.9</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion script/xdag.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off

set XDAG_VERSION="0.4.8"
set XDAG_VERSION="0.4.9"
set XDAG_JARNAME="xdagj-%XDAG_VERSION%-shaded.jar"
set XDAG_OPTS="-t"

Expand Down
2 changes: 1 addition & 1 deletion script/xdag.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

XDAG_VERSION="0.4.8"
XDAG_VERSION="0.4.9"
XDAG_JARNAME="xdagj-${XDAG_VERSION}-shaded.jar"
XDAG_OPTS="-t"

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import io.xdag.rpc.modules.web3.Web3XdagModule;
import io.xdag.rpc.modules.web3.Web3XdagModuleImpl;
import io.xdag.rpc.modules.xdag.XdagModule;
import io.xdag.rpc.modules.xdag.XdagModuleChainBase;
import io.xdag.rpc.modules.xdag.XdagModuleTransactionEnabled;
import io.xdag.rpc.modules.xdag.XdagModuleWalletDisabled;
import io.xdag.rpc.netty.JsonRpcWeb3FilterHandler;
Expand Down Expand Up @@ -191,7 +192,8 @@ public synchronized void testStart() throws Exception {
blockStore = new BlockStore(
dbFactory.getDB(DatabaseName.INDEX),
dbFactory.getDB(DatabaseName.BLOCK),
dbFactory.getDB(DatabaseName.TIME));
dbFactory.getDB(DatabaseName.TIME),
dbFactory.getDB(DatabaseName.TXHISTORY));
log.info("Block Store init.");
blockStore.init();

Expand Down Expand Up @@ -237,7 +239,7 @@ public synchronized void testStart() throws Exception {
// randomX loading
// TODO: paulochen randomx 需要恢复
// 初次快照启动
if(config.getSnapshotSpec().isSnapshotJ()){
if (config.getSnapshotSpec().isSnapshotJ()) {
randomXUtils.randomXLoadingSnapshot();
blockStore.setSnapshotBoot();
} else {
Expand Down Expand Up @@ -358,7 +360,8 @@ private Web3 getWeb3() {
private Web3 buildWeb3() {
Web3XdagModule web3XdagModule = new Web3XdagModuleImpl(
new XdagModule((byte) 0x1, new XdagModuleWalletDisabled(),
new XdagModuleTransactionEnabled(this.getBlockchain())), this);
new XdagModuleTransactionEnabled(this),
new XdagModuleChainBase(this.getBlockchain())), this);
return new Web3Impl(web3XdagModule);
}

Expand Down
46 changes: 42 additions & 4 deletions src/main/java/io/xdag/cli/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package io.xdag.cli;

import static io.xdag.config.Constants.*;

import static io.xdag.config.BlockState.MAIN;

import static io.xdag.core.XdagField.FieldType.XDAG_FIELD_IN;
import static io.xdag.core.XdagField.FieldType.XDAG_FIELD_OUT;
import static io.xdag.utils.BasicUtils.address2Hash;
Expand All @@ -35,10 +38,12 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.xdag.Kernel;
import io.xdag.config.BlockState;
import io.xdag.core.Address;
import io.xdag.core.Block;
import io.xdag.core.BlockWrapper;
import io.xdag.core.ImportResult;
import io.xdag.core.TxHistory;
import io.xdag.core.XdagState;
import io.xdag.core.XdagStats;
import io.xdag.core.XdagTopStatus;
Expand Down Expand Up @@ -122,17 +127,17 @@ public static String getStateByFlags(int flags) {
int flag = flags & ~(BI_OURS | BI_REMARK);
// 1F
if (flag == (BI_REF | BI_MAIN_REF | BI_APPLIED | BI_MAIN | BI_MAIN_CHAIN)) {
return "Main";
return MAIN.getDesc();
}
// 1C
if (flag == (BI_REF | BI_MAIN_REF | BI_APPLIED)) {
return "Accepted";
return BlockState.ACCEPTED.getDesc();
}
// 18
if (flag == (BI_REF | BI_MAIN_REF)) {
return "Rejected";
return BlockState.REJECTED.getDesc();
}
return "Pending";
return BlockState.PENDING.getDesc();
}

/**
Expand Down Expand Up @@ -485,6 +490,34 @@ public String printBlockInfo(Block block, boolean raw) {
}
}
}

String txHisFormat = """
-----------------------------------------------------------------------------------------------------------------------------
block as address: details
direction address amount time
""";
StringBuilder tx = new StringBuilder();
if (getStateByFlags(block.getInfo().getFlags()).equals(MAIN.getDesc())) {
tx.append(String.format(" earn: %s %.9f %s\n", hash2Address(block.getHashLow()),
amount2xdag(kernel.getBlockchain().getReward(block.getInfo().getHeight())),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(block.getTimestamp()))));
}
for (TxHistory txHistory : kernel.getBlockchain().getBlockTxHistoryByAddress(block.getHashLow())) {
Address address = txHistory.getAddress();
if (address.getType().equals(XDAG_FIELD_IN)) {
tx.append(String.format(" input: %s %.9f %s\n", hash2Address(address.getHashLow()),
amount2xdag(address.getAmount().longValue()),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(txHistory.getTimeStamp()))));
} else {
tx.append(String.format(" output: %s %.9f %s\n", hash2Address(address.getHashLow()),
amount2xdag(address.getAmount().longValue()),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(txHistory.getTimeStamp()))));
}
}

//TODO need add block as transaction
return String.format(heightFormat, block.getInfo().getHeight()) + String.format(otherFormat,
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS").format(time),
Expand All @@ -501,6 +534,11 @@ public String printBlockInfo(Block block, boolean raw) {
)
+ "\n"
+ (inputs == null ? "" : inputs.toString()) + (outputs == null ? "" : outputs.toString())

+ "\n"
+ txHisFormat
+ "\n"
+ tx.toString()
;
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/xdag/config/BlockState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.xdag.config;

public enum BlockState {
MAIN(0, "Main"),
REJECTED(1, "Rejected"),
ACCEPTED(2, "Accepted"),
PENDING(3, "Pending");

private final int code;
private final String desc;

BlockState(int code, String desc) {
this.code = code;
this.desc = desc;
}

public int getCode() {
return this.code;
}

public String getDesc() {
return this.desc;
}
}
23 changes: 23 additions & 0 deletions src/main/java/io/xdag/config/BlockType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.xdag.config;

public enum BlockType {
MAIN_BLOCK(0, "Main"),
WALLET(1, "Wallet"),
TRANSACTION(2, "Transaction");

private final int code;
private final String desc;

BlockType(int code, String desc) {
this.code = code;
this.desc = desc;
}

public int getCode() {
return this.code;
}

public String getDesc() {
return this.desc;
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/xdag/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class Constants {

public static final String WALLET_FILE_NAME = "wallet.data";

public static final String CLIENT_VERSION = "0.4.8";
public static final String CLIENT_VERSION = "0.4.9";

/**
* 同步问题 分叉高度
Expand All @@ -84,6 +84,7 @@ public class Constants {
public static final int HASH_RATE_LAST_MAX_TIME = 64 * 4;

public enum MessageType {
UPDATE,
PRE_TOP,
NEW_LINK;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/xdag/consensus/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public ImportResult importBlock(BlockWrapper blockWrapper) {
makeSyncDone();
}
}
} else if(importResult == IMPORTED_BEST || importResult == IMPORTED_NOT_BEST) {
// 如果是自己产生的区块则在pow的时候已经广播 这里不需要重复

if (blockWrapper.getRemoteNode() == null
|| !blockWrapper.getRemoteNode().equals(kernel.getClient().getNode())) {
if (blockWrapper.getTtl() > 0) {
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/io/xdag/consensus/XdagPow.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import io.xdag.core.XdagField;
import io.xdag.core.XdagState;
import io.xdag.crypto.Hash;
import io.xdag.listener.BlockMessage;
import io.xdag.listener.Listener;
import io.xdag.listener.PretopMessage;
import io.xdag.mine.MinerChannel;
import io.xdag.mine.manager.AwardManager;
import io.xdag.mine.manager.MinerManager;
Expand Down Expand Up @@ -430,14 +432,17 @@ public void run() {
}

@Override
public void onMessage(io.xdag.listener.Message message, MessageType type) {
if (type == MessageType.PRE_TOP) {
receiveNewPretop(message.getData());
} else if (type == MessageType.NEW_LINK) {
public void onMessage(io.xdag.listener.Message msg) {
if (msg instanceof BlockMessage) {
BlockMessage message = (BlockMessage) msg;
BlockWrapper bw = new BlockWrapper(new Block(new XdagBlock(message.getData().toArray())),
kernel.getConfig().getNodeSpec().getTTL());
broadcaster.broadcast(bw);
}
if (msg instanceof PretopMessage) {
PretopMessage message = (PretopMessage) msg;
receiveNewPretop(message.getData());
}
}

public static class Event {
Expand Down Expand Up @@ -502,13 +507,14 @@ public enum Type {

// TODO: change to scheduleAtFixRate
public class Timer implements Runnable {

private long timeout;
private volatile boolean isRunning = false;
private boolean isRunning = false;

@Override
public void run() {
isRunning = true;
while (isRunning) {
this.isRunning = true;
while (this.isRunning) {
if (timeout != -1 && XdagTime.getCurrentTimestamp() > timeout) {
events.add(new Event(Event.Type.TIMEOUT));
timeout = -1;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/xdag/core/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public interface Blockchain {

XdagTopStatus getXdagTopStatus();

long getReward(long nmain);

long getSupply(long nmain);

List<Block> getBlocksByTime(long starttime, long endtime);
Expand All @@ -72,5 +74,7 @@ public interface Blockchain {
// 注册监听器
void registerListener(Listener listener);

public List<TxHistory> getBlockTxHistoryByAddress(Bytes32 addressHashlow);

XdagExtStats getXdagExtStats();
}
Loading

0 comments on commit 56c3fce

Please sign in to comment.