Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
punk8 committed Apr 21, 2023
2 parents fbe5078 + d317c1b commit 2724d58
Show file tree
Hide file tree
Showing 47 changed files with 224 additions and 1,195 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
<dependency>
<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.1.5</version>
<version>0.1.6</version>
</dependency>

<!-- Logging framework -->
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
import io.xdag.rpc.Web3;
import io.xdag.rpc.Web3Impl;
import io.xdag.rpc.cors.CorsConfiguration;
import io.xdag.rpc.modules.web3.Web3XdagModule;
import io.xdag.rpc.modules.web3.Web3XdagModuleImpl;
import io.xdag.rpc.modules.xdag.Web3XdagModule;
import io.xdag.rpc.modules.xdag.Web3XdagModuleImpl;
import io.xdag.rpc.modules.xdag.XdagModule;
import io.xdag.rpc.modules.xdag.XdagModuleChainBase;
import io.xdag.rpc.modules.xdag.XdagModuleTransactionEnabled;
Expand All @@ -83,7 +83,7 @@
import io.xdag.rpc.netty.XdagJsonRpcHandler;
import io.xdag.rpc.serialize.JacksonBasedRpcSerializer;
import io.xdag.rpc.serialize.JsonRpcSerializer;
import io.xdag.utils.ByteArrayToByte32;
import io.xdag.utils.BytesUtils;
import io.xdag.utils.XdagTime;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -232,7 +232,7 @@ public synchronized void testStart() throws Exception {
// 如果是第一次启动,则新建一个创世块
if (xdagStats.getOurLastBlockHash() == null) {
firstAccount = Keys.toBytesAddress(wallet.getDefKey().getPublicKey());
poolMiner = new Miner(ByteArrayToByte32.arrayToByte32(firstAccount));
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 @@ -242,7 +242,7 @@ public synchronized void testStart() throws Exception {
blockchain.tryToConnect(firstBlock);
} else {
firstAccount = Keys.toBytesAddress(wallet.getDefKey().getPublicKey());
poolMiner = new Miner(ByteArrayToByte32.arrayToByte32(firstAccount));
poolMiner = new Miner(BytesUtils.arrayToByte32(firstAccount));
}
log.info("Blockchain init");

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
import io.xdag.crypto.Sign;
import io.xdag.utils.Numeric;
import io.xdag.utils.SimpleDecoder;
import io.xdag.utils.SystemUtil;
import io.xdag.utils.WalletUtils;
import io.xdag.utils.XdagTime;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.security.InvalidAlgorithmParameterException;
Expand Down Expand Up @@ -339,7 +339,7 @@ public boolean flush() {
}

// set posix permissions
if (SystemUtil.isPosix() && !file.exists()) {
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix") && !file.exists()) {
Files.createFile(file.toPath());
Files.setPosixFilePermissions(file.toPath(), POSIX_SECURED_PERMISSIONS);
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/io/xdag/cli/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import static io.xdag.utils.BasicUtils.pubAddress2Hash;
import static io.xdag.utils.BasicUtils.xdag2amount;
import static io.xdag.utils.BasicUtils.xdagHashRate;
import static io.xdag.utils.PubkeyAddressUtils.checkAddress;
import static io.xdag.utils.PubkeyAddressUtils.fromBase58;
import static io.xdag.utils.PubkeyAddressUtils.toBase58;
import static io.xdag.utils.WalletUtils.checkAddress;
import static io.xdag.utils.WalletUtils.fromBase58;
import static io.xdag.utils.WalletUtils.toBase58;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand All @@ -73,8 +73,7 @@
import io.xdag.mine.miner.MinerStates;
import io.xdag.net.node.Node;
import io.xdag.utils.BasicUtils;
import io.xdag.utils.ByteArrayToByte32;
import io.xdag.utils.PubkeyAddressUtils;
import io.xdag.utils.BytesUtils;
import io.xdag.utils.XdagTime;
import java.math.BigInteger;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -740,7 +739,7 @@ public String address(Bytes32 wrap) {
.format(XdagTime.xdagTimestampToMs(txHistory.getTimeStamp()))));
}
}else {
tx.append(String.format(" snapshot: %s %.9f %s%n", (PubkeyAddressUtils.toBase58(ByteArrayToByte32.byte32ToArray(address.getAddress()))),
tx.append(String.format(" snapshot: %s %.9f %s%n", (toBase58(BytesUtils.byte32ToArray(address.getAddress()))),
amount2xdag(address.getAmount()),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(txHistory.getTimeStamp()))));
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/xdag/cli/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.xdag.Kernel;
import io.xdag.Wallet;
import io.xdag.utils.BasicUtils;
import io.xdag.utils.PubkeyAddressUtils;
import io.xdag.utils.WalletUtils;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -130,7 +129,7 @@ private void processAddress(CommandInput input) {
String address = argv.get(0);
try {
Bytes32 hash;
if (PubkeyAddressUtils.checkAddress(address)) {
if (WalletUtils.checkAddress(address)) {
hash = pubAddress2Hash(address);
} else {
println("Incorrect address");
Expand Down Expand Up @@ -377,7 +376,7 @@ private void processXfer(CommandInput input) {
return;
}

if (PubkeyAddressUtils.checkAddress(argv.get(1))) {
if (WalletUtils.checkAddress(argv.get(1))) {
hash = pubAddress2Hash(argv.get(1));
} else {
println("Incorrect address");
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/io/xdag/consensus/XdagPow.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class XdagPow implements PoW, Listener, Runnable {
protected AtomicReference<Task> currentTask = new AtomicReference<>();
protected AtomicLong taskIndex = new AtomicLong(0L);

private boolean isWorking = false;


private final ExecutorService timerExecutor = Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
.namingPattern("XdagPow-timer-thread")
Expand Down Expand Up @@ -267,11 +269,15 @@ public void receiveNewShare(MinerChannel channel, Message msg) {
}

public void receiveNewPretop(Bytes pretop) {
if (!this.isRunning) {
// make sure the PoW is running and the main block is generating
if (!this.isRunning || !isWorking) {
return;
}
if (!equalBytes(pretop.toArray(), globalPretop.toArray())) {
globalPretop = Bytes32.wrap(blockchain.getXdagTopStatus().getPreTop());

// prevent duplicate event
if (globalPretop == null || !equalBytes(pretop.toArray(), globalPretop.toArray())) {
log.debug("update global pretop:{}", Bytes32.wrap(pretop).toHexString());
globalPretop = Bytes32.wrap(pretop);
events.add(new Event(Event.Type.NEW_PRETOP, pretop));
}
}
Expand Down Expand Up @@ -327,17 +333,20 @@ protected void onNewShare(XdagField shareInfo, MinerChannel channel) {

protected void onTimeout() {
Block b = generateBlock.get();
// stop generate main block
isWorking = false;
if (b != null) {
Block newBlock = new Block(new XdagBlock(b.toBytes()));
log.debug("Broadcast locally generated blockchain, waiting to be verified. block hash = [{}]",
newBlock.getHash().toHexString());
// 发送区块 如果有的话 然后开始生成新区块
// add new block, reward miner, and broadcast the new block
kernel.getBlockchain().tryToConnect(newBlock);
awardManager.addAwardBlock(minShare.get(), newBlock.getHash(), newBlock.getTimestamp());
BlockWrapper bw = new BlockWrapper(newBlock, kernel.getConfig().getNodeSpec().getTTL());

broadcaster.broadcast(bw);
}
// start generate main block
isWorking = true;
newBlock();
}

Expand Down Expand Up @@ -405,7 +414,7 @@ public void run() {
// resetTimeout(XdagTime.getEndOfEpoch(XdagTime.getCurrentTimestamp() + 64));
timer.timeout(XdagTime.getEndOfEpoch(XdagTime.getCurrentTimestamp() + 64));
// init pretop
globalPretop = Bytes32.wrap(blockchain.getXdagTopStatus().getPreTop());
globalPretop = null;
while (this.isRunning) {
try {
Event ev = events.poll(10, TimeUnit.MILLISECONDS);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/core/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package io.xdag.core;

import io.xdag.utils.BytesUtils;
import io.xdag.utils.PubkeyAddressUtils;
import io.xdag.utils.WalletUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.tuweni.bytes.Bytes;
Expand Down Expand Up @@ -167,7 +167,7 @@ public boolean getIsAddress() {
@Override
public String toString() {
if(isAddress){
return "Address [" + PubkeyAddressUtils.toBase58(addressHash.slice(8,20).toArray()) + "]";
return "Address [" + WalletUtils.toBase58(addressHash.slice(8,20).toArray()) + "]";
}else {
return "Block Hash[" + addressHash.toHexString() + "]";
}
Expand Down
90 changes: 49 additions & 41 deletions src/main/java/io/xdag/core/BlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
import io.xdag.listener.PretopMessage;
import io.xdag.mine.randomx.RandomX;
import io.xdag.utils.BasicUtils;
import io.xdag.utils.ByteArrayToByte32;
import io.xdag.utils.PubkeyAddressUtils;
import io.xdag.utils.BytesUtils;
import io.xdag.utils.WalletUtils;
import io.xdag.utils.XdagTime;
import java.math.BigInteger;
import java.util.ArrayList;
Expand Down Expand Up @@ -324,7 +324,7 @@ public synchronized ImportResult tryToConnect(Block block) {
log.debug("Block have no parent for " + result.getHashlow().toHexString());
return result;
} else {
// 链接块的时间需要小于该块时间,否则为不合法区块
// ensure ref block's time is earlier than block's time
if (refBlock.getTimestamp() >= block.getTimestamp()) {
result = ImportResult.INVALID_BLOCK;
result.setHashlow(refBlock.getHashLow());
Expand All @@ -337,10 +337,10 @@ public synchronized ImportResult tryToConnect(Block block) {

}
}else {
if(ref.type == XDAG_FIELD_INPUT && !addressStore.addressIsExist(ByteArrayToByte32.byte32ToArray(ref.getAddress()))){
if(ref.type == XDAG_FIELD_INPUT && !addressStore.addressIsExist(BytesUtils.byte32ToArray(ref.getAddress()))){
result = ImportResult.INVALID_BLOCK;
result.setErrorInfo("Address isn't exist " + PubkeyAddressUtils.toBase58(ByteArrayToByte32.byte32ToArray(ref.getAddress())));
log.debug("Address isn't exist " + PubkeyAddressUtils.toBase58(ByteArrayToByte32.byte32ToArray(ref.getAddress())));
result.setErrorInfo("Address isn't exist " + WalletUtils.toBase58(BytesUtils.byte32ToArray(ref.getAddress())));
log.debug("Address isn't exist " + WalletUtils.toBase58(BytesUtils.byte32ToArray(ref.getAddress())));
return result;
}
}
Expand Down Expand Up @@ -408,17 +408,10 @@ public synchronized ImportResult tryToConnect(Block block) {
updateBlockFlag(block, BI_OURS, true);
}

// 更新区块难度和maxDiffLink
// calculate block's self difficulty
BigInteger cuDiff = calculateCurrentBlockDiff(block);
BigInteger diff = calculateBlockDiff(block,cuDiff);

// 更新preTop
setPreTop(block, diff);
setPreTop(getBlockByHash(xdagTopStatus.getTop() == null ? null : Bytes32.wrap(xdagTopStatus.getTop()),
false), xdagTopStatus.getTopDiff());

// 通知XdagPoW 新pretop产生
onNewPretop();
// calculate block's chain difficulty
calculateBlockDiff(block,cuDiff);

// TODO:extra 处理
processExtraBlock();
Expand All @@ -439,9 +432,20 @@ public synchronized ImportResult tryToConnect(Block block) {
log.info("XDAG:Before unwind, height = {}, After unwind, height = {}, unwind number = {}",
currentHeight, xdagStats.nmain, currentHeight - xdagStats.nmain);
}

Block currentTop = getBlockByHash(xdagTopStatus.getTop() == null ? null :
Bytes32.wrap(xdagTopStatus.getTop()), false);
BigInteger currentTopDiff = xdagTopStatus.getTopDiff();
log.debug("update top: {}", block.getHashLow());
// update Top
xdagTopStatus.setTopDiff(block.getInfo().getDifficulty());
xdagTopStatus.setTop(block.getHashLow().toArray());
// update preTop
setPreTop(currentTop,currentTopDiff);
// if block's epoch is earlier than current epoch, then notify the PoW thread to regenerate the main block
if (XdagTime.getEpoch(block.getTimestamp()) < XdagTime.getCurrentEpoch()) {
onNewPretop();
}
result = ImportResult.IMPORTED_BEST;
xdagStats.updateMaxDiff(xdagTopStatus.getTopDiff());
xdagStats.updateDiff(xdagTopStatus.getTopDiff());
Expand All @@ -450,20 +454,13 @@ public synchronized ImportResult tryToConnect(Block block) {
// 新增区块
xdagStats.nblocks++;
xdagStats.totalnblocks = Math.max(xdagStats.nblocks, xdagStats.totalnblocks);
// if (xdagStats.getTotalnblocks() < xdagStats.getNblocks()) {
// xdagStats.setTotalnblocks(xdagStats.getNblocks());
// }

//orphan (hash , block)
// log.debug("======New block waiting to link======,{}",Hex.toHexString(block.getHashLow()));
if ((block.getInfo().flags & BI_EXTRA) != 0) {
// log.debug("block:{} is extra, put it into memOrphanPool waiting to link.", Hex.toHexString(block.getHashLow()));
memOrphanPool.put(block.getHashLow(), block);
xdagStats.nextra++;
// TODO:设置为返回 IMPORTED_EXTRA
// result = ImportResult.IMPORTED_EXTRA;
} else {
// log.debug("block:{} is extra, put it into orphanPool waiting to link.", Hex.toHexString(block.getHashLow()));
saveBlock(block);
orphanBlockStore.addOrphan(block);
xdagStats.nnoref++;
Expand Down Expand Up @@ -594,7 +591,7 @@ public void processExtraBlock() {

protected void onNewPretop() {
for (Listener listener : listeners) {
listener.onMessage(new PretopMessage(Bytes.wrap(xdagTopStatus.getPreTop()), PRE_TOP));
listener.onMessage(new PretopMessage(Bytes.wrap(xdagTopStatus.getTop()), PRE_TOP));
}
}

Expand Down Expand Up @@ -638,9 +635,8 @@ public synchronized void checkNewMain() {
*/
public void unWindMain(Block block) {
log.debug("Unwind main to block,{}", block == null ? "null" : block.getHashLow().toHexString());
// log.debug("xdagTopStatus.getTop(),{}",xdagTopStatus.getTop()==null?"null":Hex.toHexString(xdagTopStatus.getTop()));
if (xdagTopStatus.getTop() != null) {
log.debug("now top : {}", Bytes32.wrap(xdagTopStatus.getPreTop()).toHexString());
log.debug("now pretop : {}",xdagTopStatus.getPreTop() == null?"null": Bytes32.wrap(xdagTopStatus.getPreTop()).toHexString());
for (Block tmp = getBlockByHash(Bytes32.wrap(xdagTopStatus.getTop()), true); tmp != null
&& !blockEqual(block, tmp); tmp = getMaxDiffLink(tmp, true)) {
updateBlockFlag(tmp, BI_MAIN_CHAIN, false);
Expand Down Expand Up @@ -1005,37 +1001,49 @@ public Bytes32 getPreTopMainBlockForLink(long sendTime) {
return null;
}
if (XdagTime.getEpoch(topInfo.getTimestamp()) == mainTime) {
log.debug("use pretop:{}", Bytes32.wrap(xdagTopStatus.getPreTop()).toHexString());
return Bytes32.wrap(xdagTopStatus.getPreTop());
} else {
log.debug("use top:{}", Bytes32.wrap(xdagTopStatus.getTop()).toHexString());
return Bytes32.wrap(xdagTopStatus.getTop());
}
}

/**
* update pretop
* @param block block
* @param diff difficulty of block
* @param target target
* @param targetDiff difficulty of block
*/
public void setPreTop(Block block, BigInteger diff) {
if (block == null) {
public void setPreTop(Block target, BigInteger targetDiff) {
if (target == null) {
return;
}
if (XdagTime.getEpoch(block.getTimestamp()) > XdagTime.getCurrentEpoch()) {
return;

// make sure the target's epoch is earlier than current top's epoch
Block block = getBlockByHash(xdagTopStatus.getTop() == null ? null :
Bytes32.wrap(xdagTopStatus.getTop()), false);
if (block != null) {
if (XdagTime.getEpoch(target.getTimestamp()) >= XdagTime.getEpoch(block.getTimestamp())) {
return;
}
}

// if pretop is null, then update pretop to target
if (xdagTopStatus.getPreTop() == null) {
xdagTopStatus.setPreTop(block.getHashLow().toArray());
xdagTopStatus.setPreTopDiff(diff);
block.setPretopCandidate(true);
block.setPretopCandidateDiff(diff);
xdagTopStatus.setPreTop(target.getHashLow().toArray());
xdagTopStatus.setPreTopDiff(targetDiff);
target.setPretopCandidate(true);
target.setPretopCandidateDiff(targetDiff);
return;
}

if (diff.compareTo(xdagTopStatus.getPreTopDiff()) > 0) {
xdagTopStatus.setPreTop(block.getHashLow().toArray());
xdagTopStatus.setPreTopDiff(diff);
block.setPretopCandidate(true);
block.setPretopCandidateDiff(diff);
// if targetDiff greater than pretop diff, then update pretop to target
if (targetDiff.compareTo(xdagTopStatus.getPreTopDiff()) > 0) {
log.debug("update pretop:{}", Bytes32.wrap(target.getHashLow()).toHexString());
xdagTopStatus.setPreTop(target.getHashLow().toArray());
xdagTopStatus.setPreTopDiff(targetDiff);
target.setPretopCandidate(true);
target.setPretopCandidateDiff(targetDiff);
}
}
/**
Expand Down
Loading

0 comments on commit 2724d58

Please sign in to comment.