Skip to content
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

Develop #287

Merged
merged 6 commits into from
Nov 1, 2023
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
15 changes: 14 additions & 1 deletion src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.xdag.net.message.MessageQueue;
import io.xdag.net.NetDB;
import io.xdag.net.node.NodeManager;
import io.xdag.net.websocket.WebSocketServer;
import io.xdag.rpc.Web3;
import io.xdag.rpc.Web3Impl;
import io.xdag.rpc.cors.CorsConfiguration;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class Kernel {

protected byte[] firstAccount;
protected Block firstBlock;
protected WebSocketServer webSocketServer;
protected XdagState xdagState;

protected AtomicInteger channelsAccount = new AtomicInteger(0);
Expand Down Expand Up @@ -206,7 +208,7 @@ public synchronized void testStart() throws Exception {
// 如果是第一次启动,则新建一个创世块
if (xdagStats.getOurLastBlockHash() == null) {
firstAccount = Keys.toBytesAddress(wallet.getDefKey().getPublicKey());
firstBlock = new Block(config, XdagTime.getCurrentTimestamp(), null, null, false, null, null, -1);
firstBlock = new Block(config, XdagTime.getCurrentTimestamp(), null, null, false, null, null, -1, XAmount.ZERO);
firstBlock.signOut(wallet.getDefKey());
xdagStats.setOurLastBlockHash(firstBlock.getHashLow().toArray());
if (xdagStats.getGlobalMiner() == null) {
Expand Down Expand Up @@ -279,6 +281,10 @@ public synchronized void testStart() throws Exception {
syncMgr.start();
log.info("SyncManager start...");

// ====================================
// set up pool websocket channel
// ====================================
getWsServer().start();
// ====================================
// pow
// ====================================
Expand Down Expand Up @@ -338,6 +344,13 @@ private JsonRpcWeb3ServerHandler getJsonRpcWeb3ServerHandler() {
return jsonRpcWeb3ServerHandler;
}

private WebSocketServer getWsServer(){
if (webSocketServer == null) {
webSocketServer = new WebSocketServer(config.getPoolIP(), config.getPoolTag(), config.getWebsocketServerPort());
}
return webSocketServer;
}

private Web3WebSocketServer getWeb3WebSocketServer() throws UnknownHostException {
if (web3WebSocketServer == null) {
JsonRpcSerializer jsonRpcSerializer = getJsonRpcSerializer();
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/io/xdag/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@

import com.google.common.collect.Lists;
import io.xdag.config.Config;
import io.xdag.core.Address;
import io.xdag.core.Block;
import io.xdag.core.BlockWrapper;
import io.xdag.core.*;
import io.xdag.utils.SimpleEncoder;
import io.xdag.core.XAmount;
import io.xdag.crypto.Aes;
import io.xdag.crypto.Bip32ECKeyPair;
import io.xdag.crypto.Keys;
Expand Down Expand Up @@ -605,7 +602,7 @@ private Block createNewBlock(Map<Address, KeyPair> pairs, List<Address> to,

long sendTime = XdagTime.getCurrentTimestamp();

return new Block(getConfig(), sendTime, all, null, false, keys, remark, defKeyIndex);
return new Block(getConfig(), sendTime, all, null, false, keys, remark, defKeyIndex, XAmount.of(100, XUnit.MILLI_XDAG));
}


Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/xdag/cli/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.xdag.Kernel;
import io.xdag.core.*;
import io.xdag.net.Channel;
import io.xdag.net.websocket.ChannelSupervise;
import io.xdag.utils.BasicUtils;
import io.xdag.utils.BytesUtils;
import io.xdag.utils.XdagTime;
Expand Down Expand Up @@ -306,7 +307,7 @@ private List<BlockWrapper> createTransactionBlock(Map<Address, KeyPair> ourKeys,

private BlockWrapper createTransaction(Bytes32 to, XAmount amount, Map<Address, KeyPair> keys, String remark) {
List<Address> tos = Lists.newArrayList(new Address(to, XDAG_FIELD_OUTPUT, amount, true));
Block block = kernel.getBlockchain().createNewBlock(new HashMap<>(keys), tos, false, remark);
Block block = kernel.getBlockchain().createNewBlock(new HashMap<>(keys), tos, false, remark, XAmount.of(100,XUnit.MILLI_XDAG));

if (block == null) {
return null;
Expand Down Expand Up @@ -462,6 +463,11 @@ public String printBlockInfo(Block block, boolean raw) {
if (getStateByFlags(block.getInfo().getFlags()).equals(MAIN.getDesc()) && block.getInfo().getHeight() > kernel.getConfig().getSnapshotSpec().getSnapshotHeight()) {
tx.append(String.format(" earn: %s %s %s%n", hash2Address(block.getHashLow()),
kernel.getBlockchain().getReward(block.getInfo().getHeight()).toDecimal(9, XUnit.XDAG).toPlainString(),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(block.getTimestamp()))))
.append(String.format("fee earn: %s %s %s%n", hash2Address(block.getHashLow()),
block.getInfo().getAmount().equals(XAmount.ZERO) ? XAmount.ZERO.toString() :
block.getInfo().getAmount().subtract(kernel.getBlockchain().getReward(block.getInfo().getHeight())).toDecimal(9, XUnit.XDAG).toPlainString(),
FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS")
.format(XdagTime.xdagTimestampToMs(block.getTimestamp()))));
}
Expand Down Expand Up @@ -502,7 +508,9 @@ public String printBlockInfo(Block block, boolean raw) {
hash2Address(block.getHash()), block.getInfo().getAmount().toDecimal(9, XUnit.XDAG).toPlainString(),
// fee目前为0
block.getInfo().getRef() == null ? "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" : hash2Address(Bytes32.wrap(block.getInfo().getRef())),
XAmount.ZERO.toDecimal(9, XUnit.XDAG).toPlainString()
block.getInfo().getRef() == null ? XAmount.ZERO.toDecimal(9, XUnit.XDAG).toPlainString() :
(block.getFee().equals(XAmount.ZERO) ? XAmount.of(100,XUnit.MILLI_XDAG).multiply(block.getOutputs().size()).toDecimal(9,XUnit.XDAG).toPlainString() :
block.getFee().multiply(block.getOutputs().size()).toDecimal(9,XUnit.XDAG).toPlainString())
)
+ "\n"
+ (inputs == null ? "" : inputs.toString()) + (outputs == null ? "" : outputs.toString())
Expand Down Expand Up @@ -567,6 +575,10 @@ public String listConnect() {
return stringBuilder.toString();
}

public String pool(){
return ChannelSupervise.showChannel();
}

public String keygen()
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
kernel.getXdagState().tempSet(XdagState.KEYS);
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/io/xdag/cli/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Shell() {
commandExecute.put("stats", new CommandMethods(this::processStats, this::defaultCompleter));
commandExecute.put("xfer", new CommandMethods(this::processXfer, this::defaultCompleter));
commandExecute.put("xfertonew", new CommandMethods(this::processXferToNew, this::defaultCompleter));
// commandExecute.put("miners", new CommandMethods(this::processMiners, this::defaultCompleter));
commandExecute.put("pool", new CommandMethods(this::processPool, this::defaultCompleter));
// commandExecute.put("run", new CommandMethods(this::processRun, this::defaultCompleter));
commandExecute.put("keygen", new CommandMethods(this::processKeygen, this::defaultCompleter));
commandExecute.put("net", new CommandMethods(this::processNet, this::defaultCompleter));
Expand Down Expand Up @@ -396,6 +396,23 @@ private void processXfer(CommandInput input) {
}
}

private void processPool(CommandInput input){
final String[] usage = {
"pool - for pool, print list of recent connected pool",
"Usage: pool ",
" -? --help Show help",
};
try {
Options opt = parseOptions(usage, input.args());
if (opt.isSet("help")) {
throw new Options.HelpException(opt.usage());
}
println(commands.pool());
} catch (Exception e) {
saveException(e);
}
}

private void processKeygen(CommandInput input) {
final String[] usage = {
"keygen - generate new private/public key pair and set it by default",
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/xdag/config/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public class AbstractConfig implements Config, AdminSpec, NodeSpec, WalletSpec,
protected int telnetPort = 7001;
protected String telnetPassword;

// =========================
// Pool websocket spec
// =========================
protected String poolIp;
protected int WebsocketServerPort;
protected String poolTag;

protected int maxShareCountPerChannel = 20;
protected int awardEpoch = 0xf;
protected int waitEpoch = 20;
Expand Down Expand Up @@ -245,6 +252,10 @@ public void getSetting() {
telnetPort = config.hasPath("admin.telnet.port")?config.getInt("admin.telnet.port"):6001;
telnetPassword = config.getString("admin.telnet.password");

poolIp = config.hasPath("pool.ip")?config.getString("pool.ip"):"127.0.0.1";
WebsocketServerPort = config.hasPath("pool.ws.port")?config.getInt("pool.ws.port"):7001;
poolTag = config.hasPath("pool.tag")?config.getString("pool.tag"):"xdagj";

nodeIp = config.hasPath("node.ip")?config.getString("node.ip"):"127.0.0.1";
nodePort = config.hasPath("node.port")?config.getInt("node.port"):8001;
nodeTag = config.hasPath("node.tag")?config.getString("node.tag"):"xdagj";
Expand Down Expand Up @@ -373,6 +384,13 @@ public List<ModuleDescription> getRpcModules() {
return modules;
}

@Override
public String getPoolIP(){return poolIp;}
@Override
public int getWebsocketServerPort() {return WebsocketServerPort;}
@Override
public String getPoolTag(){return poolTag;}

@Override
public boolean isRPCEnabled() {
return rpcEnabled;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/xdag/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public interface Config {
boolean getEnableGenerateBlock();

long getTxPageSizeLimit();

//websocket
String getPoolIP();

int getWebsocketServerPort();

String getPoolTag();

}
4 changes: 4 additions & 0 deletions src/main/java/io/xdag/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package io.xdag.config;

import io.xdag.core.XAmount;
import io.xdag.core.XUnit;
import org.apache.tuweni.units.bigints.UInt64;

public class Constants {
Expand Down Expand Up @@ -95,4 +97,6 @@ public enum MessageType {
public static final short TESTNET_VERSION = 0;
public static final short DEVNET_VERSION = 0;

public static final XAmount minGas = XAmount.of(100, XUnit.MILLI_XDAG);

}
11 changes: 3 additions & 8 deletions src/main/java/io/xdag/consensus/XdagPow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
import static io.xdag.utils.BytesUtils.equalBytes;

import io.xdag.Kernel;
import io.xdag.core.Block;
import io.xdag.core.BlockWrapper;
import io.xdag.core.Blockchain;
import io.xdag.core.XdagBlock;
import io.xdag.core.XdagField;
import io.xdag.core.XdagState;
import io.xdag.core.*;
import io.xdag.crypto.Hash;
import io.xdag.listener.BlockMessage;
import io.xdag.listener.Listener;
Expand Down Expand Up @@ -181,7 +176,7 @@ public void newBlock() {


public Block generateRandomXBlock(long sendTime) {
Block block = blockchain.createNewBlock(null, null, true, null);
Block block = blockchain.createNewBlock(null, null, true, null, XAmount.ZERO);
block.signOut(kernel.getWallet().getDefKey());

minShare.set(Bytes32.wrap(RandomUtils.nextBytes(32)));
Expand All @@ -192,7 +187,7 @@ public Block generateRandomXBlock(long sendTime) {
}

public Block generateBlock(long sendTime) {
Block block = blockchain.createNewBlock(null, null, true, null);
Block block = blockchain.createNewBlock(null, null, true, null, XAmount.ZERO);
block.signOut(kernel.getWallet().getDefKey());

minShare.set(Bytes32.wrap(RandomUtils.nextBytes(32)));
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/xdag/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SECPPublicKey;
import org.hyperledger.besu.crypto.SECPSignature;

import com.google.common.collect.Lists;

@Slf4j
Expand Down Expand Up @@ -122,11 +121,12 @@ public Block(
boolean mining,
List<KeyPair> keys,
String remark,
int defKeyIndex) {
int defKeyIndex,
XAmount fee) {
parsed = true;
info = new BlockInfo();
this.info.setTimestamp(timestamp);
this.info.setFee(0);
this.info.setFee(fee);
int lenghth = 0;

setType(config.getXdagFieldHeader(), lenghth++);
Expand Down Expand Up @@ -206,7 +206,7 @@ public Block(
public Block(Config config, long timestamp,
List<Address> pendings,
boolean mining) {
this(config, timestamp, null, pendings, mining, null, null, -1);
this(config, timestamp, null, pendings, mining, null, null, -1, XAmount.ZERO);
}

/**
Expand Down Expand Up @@ -257,7 +257,7 @@ public void parse() {
this.transportHeader = header.getLong(0, ByteOrder.LITTLE_ENDIAN);
this.info.type = header.getLong(8, ByteOrder.LITTLE_ENDIAN);
this.info.setTimestamp(header.getLong(16, ByteOrder.LITTLE_ENDIAN));
this.info.setFee(header.getLong(24, ByteOrder.LITTLE_ENDIAN));
this.info.setFee(XAmount.of(header.getLong(24, ByteOrder.LITTLE_ENDIAN), XUnit.NANO_XDAG));
for (int i = 1; i < XdagBlock.XDAG_BLOCK_FIELDS; i++) {
XdagField field = xdagBlock.getField(i);
if (field == null) {
Expand Down Expand Up @@ -375,7 +375,8 @@ private byte[] getEncodedBody() {
}

private byte[] getEncodedHeader() {
byte[] fee = BytesUtils.longToBytes(getFee(), true);
//byte[] fee = BytesUtils.longToBytes(getFee(), true);
byte[] fee = BytesUtils.longToBytes(Long.parseLong(getFee().toString()), true);
byte[] time = BytesUtils.longToBytes(getTimestamp(), true);
byte[] type = BytesUtils.longToBytes(getType(), true);
byte[] transport = new byte[8];
Expand Down Expand Up @@ -510,7 +511,7 @@ public long getType() {
return this.info.type;
}

public long getFee() {
public XAmount getFee() {
return this.info.getFee();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/core/BlockInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class BlockInfo {
private BigInteger difficulty;
private byte[] ref;
private byte[] maxDiffLink;
private long fee;
private XAmount fee = XAmount.ZERO;
private byte[] remark;
private byte[] hash;
private byte[] hashlow;
Expand All @@ -64,7 +64,7 @@ public String toString() {
", ref=" + Arrays.toString(ref) +
", maxDiffLink=" + Arrays.toString(maxDiffLink) +
", flags=" + Integer.toHexString(flags) +
", fee=" + fee +
", fee=" + fee.toString() +
", timestamp=" + timestamp +
", remark=" + Arrays.toString(remark) +
", isSnapshot=" + isSnapshot +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/xdag/core/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface Blockchain {

ImportResult tryToConnect(Block block);

Block createNewBlock(Map<Address, KeyPair> pairs, List<Address> to, boolean mining, String remark);
Block createNewBlock(Map<Address, KeyPair> pairs, List<Address> to, boolean mining, String remark, XAmount fee);

Block getBlockByHash(Bytes32 hash, boolean isRaw);

Expand Down
Loading