Skip to content

Commit

Permalink
Merge pull request #278 from Holt666/develop
Browse files Browse the repository at this point in the history
 Refactor code
  • Loading branch information
LucasMLK authored Sep 12, 2023
2 parents 29641df + f27e4aa commit 788343c
Show file tree
Hide file tree
Showing 42 changed files with 132 additions and 511 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
<dependency>
<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.1.6</version>
<version>0.1.7</version>
</dependency>

<!-- Logging framework -->
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/xdag/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
import io.xdag.core.Address;
import io.xdag.core.Block;
import io.xdag.core.BlockWrapper;
import io.xdag.core.SimpleEncoder;
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;
import io.xdag.crypto.MnemonicUtils;
import io.xdag.crypto.SecureRandomUtils;
import io.xdag.crypto.Sign;
import io.xdag.utils.Numeric;
import io.xdag.utils.SimpleDecoder;
Expand Down Expand Up @@ -77,6 +76,7 @@
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SECPPrivateKey;
import org.hyperledger.besu.crypto.SecureRandomProvider;

@Slf4j
@Getter
Expand Down Expand Up @@ -222,7 +222,7 @@ protected void writeAccounts(byte[] key, SimpleEncoder enc) {
synchronized (accounts) {
enc.writeInt(accounts.size());
for (KeyPair keyPair : accounts.values()) {
byte[] iv = SecureRandomUtils.secureRandom().generateSeed(16);
byte[] iv = SecureRandomProvider.publicSecureRandom().generateSeed(16);

enc.writeBytes(iv);
enc.writeBytes(Aes.encrypt(keyPair.getPrivateKey().getEncoded(), key, iv));
Expand Down Expand Up @@ -251,7 +251,7 @@ protected void writeHdSeed(byte[] key, SimpleEncoder enc) {
e.writeString(mnemonicPhrase);
e.writeInt(nextAccountIndex);

byte[] iv = SecureRandomUtils.secureRandom().generateSeed(16);
byte[] iv = SecureRandomProvider.publicSecureRandom().generateSeed(16);
byte[] hdSeedRaw = e.toBytes();
byte[] hdSeedEncrypted = Aes.encrypt(hdSeedRaw, key, iv);

Expand Down Expand Up @@ -325,7 +325,7 @@ public boolean flush() {
SimpleEncoder enc = new SimpleEncoder();
enc.writeInt(VERSION);

byte[] salt = SecureRandomUtils.secureRandom().generateSeed(SALT_LENGTH);
byte[] salt = SecureRandomProvider.publicSecureRandom().generateSeed(SALT_LENGTH);
enc.writeBytes(salt);

byte[] key = BCrypt.generate(password.getBytes(UTF_8), salt, BCRYPT_COST);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/cli/XdagCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.xdag.config.Constants;
import io.xdag.crypto.Keys;
import io.xdag.crypto.MnemonicUtils;
import io.xdag.crypto.SecureRandomUtils;
import io.xdag.crypto.Sign;
import io.xdag.db.SnapshotStore;
import io.xdag.db.rocksdb.DatabaseName;
Expand All @@ -57,6 +56,7 @@
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SECPPrivateKey;
import org.hyperledger.besu.crypto.SecureRandomProvider;

import com.google.common.collect.Lists;

Expand Down Expand Up @@ -467,7 +467,7 @@ public boolean initializedHdSeed(Wallet wallet, PrintStream printer) {
// HD Mnemonic
printer.println("HdWallet Initializing...");
byte[] initialEntropy = new byte[16];
SecureRandomUtils.secureRandom().nextBytes(initialEntropy);
SecureRandomProvider.publicSecureRandom().nextBytes(initialEntropy);
String phrase = MnemonicUtils.generateMnemonic(initialEntropy);
printer.println("HdWallet Mnemonic:" + phrase);

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/io/xdag/core/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public Address(XdagField field, Boolean isAddress) {
parse();
}

public Address(XdagField field) {

}

/**
* 只用于ref 跟 maxdifflink
*/
Expand Down Expand Up @@ -121,7 +117,6 @@ public Address(Bytes32 hash, XdagField.FieldType type, XAmount amount, Boolean i
parsed = true;
}


public Bytes getData() {
if (this.data == null) {
this.data = MutableBytes32.create();
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/io/xdag/core/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

import io.xdag.utils.SimpleEncoder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -80,20 +82,25 @@ public class Block implements Cloneable {
/**
* 区块的links 列表 输入输出*
*/
@Getter
private List<Address> inputs = new CopyOnWriteArrayList<>();
/**
* ouput包含pretop
*/
@Getter
private List<Address> outputs = new CopyOnWriteArrayList<>();
/**
* 记录公钥 前缀+压缩公钥*
*/
@Getter
private List<SECPPublicKey> pubKeys = new CopyOnWriteArrayList<>();
@Getter
private Map<SECPSignature, Integer> insigs = new LinkedHashMap<>();
private SECPSignature outsig;
/**
* 主块的nonce记录矿工地址跟nonce*
*/
@Getter
private Bytes32 nonce;
private XdagBlock xdagBlock;
private boolean parsed;
Expand Down Expand Up @@ -468,30 +475,10 @@ public MutableBytes32 getHashLow() {
return MutableBytes32.wrap(info.getHashlow());
}

public List<Address> getOutputs() {
return outputs;
}

public List<Address> getInputs() {
return inputs;
}

public List<SECPPublicKey> getPubKeys() {
return pubKeys;
}

public Bytes32 getNonce() {
return nonce;
}

public SECPSignature getOutsig() {
return outsig == null ? null : outsig;
}

public Map<SECPSignature, Integer> getInsigs() {
return insigs;
}

@Override
public String toString() {
return String.format("Block info:[Hash:{%s}][Time:{%s}]", getHashLow().toHexString(),
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/xdag/core/BlockState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package io.xdag.core;

import lombok.Getter;

@Getter
public enum BlockState {
MAIN(0, "Main"),
REJECTED(1, "Rejected"),
Expand All @@ -37,11 +40,4 @@ public enum BlockState {
this.desc = desc;
}

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

public String getDesc() {
return this.desc;
}
}
10 changes: 3 additions & 7 deletions src/main/java/io/xdag/core/BlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package io.xdag.core;

import lombok.Getter;

@Getter
public enum BlockType {
MAIN_BLOCK(0, "Main"),
WALLET(1, "Wallet"),
Expand All @@ -37,11 +40,4 @@ public enum BlockType {
this.desc = desc;
}

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

public String getDesc() {
return this.desc;
}
}
1 change: 0 additions & 1 deletion src/main/java/io/xdag/core/BlockWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package io.xdag.core;

import io.xdag.net.Peer;
import io.xdag.net.node.Node;
import lombok.Getter;
import lombok.Setter;

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/xdag/core/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

package io.xdag.core;

import io.xdag.listener.Listener;
import java.util.List;
import java.util.Map;
import org.apache.tuweni.bytes.Bytes;

import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.crypto.KeyPair;

import io.xdag.listener.Listener;

public interface Blockchain {

// for snapshot pre-seed
Expand All @@ -52,8 +53,6 @@ public interface Blockchain {

List<Block> listMinedBlocks(int count);

Map<Bytes, Integer> getMemOurBlocks();

XdagStats getXdagStats();

XdagTopStatus getXdagTopStatus();
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/xdag/core/BlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ private XAmount applyBlock(Block block) {
updateBlockFlag(block, BI_MAIN_REF, true);

List<Address> links = block.getLinks();
if (links == null || links.size() == 0) {
if (links == null || links.isEmpty()) {
updateBlockFlag(block, BI_APPLIED, true);
return XAmount.ZERO;
}
Expand Down Expand Up @@ -1096,7 +1096,7 @@ public BigInteger calculateBlockDiff(Block block, BigInteger cuDiff) {

// 临时区块
Block tmpBlock;
if (block.getLinks().size() == 0) {
if (block.getLinks().isEmpty()) {
return cuDiff;
}

Expand Down Expand Up @@ -1323,7 +1323,7 @@ public XdagStats getXdagStats() {
public boolean canUseInput(Block block) {
List<SECPPublicKey> keys = block.verifiedKeys();
List<Address> inputs = block.getInputs();
if (inputs == null || inputs.size() == 0) {
if (inputs == null || inputs.isEmpty()) {
return true;
}
/*
Expand Down Expand Up @@ -1729,10 +1729,6 @@ public List<Block> listMinedBlocks(int count) {
return res;
}

public Map<Bytes, Integer> getMemOurBlocks() {
return memOurBlocks;
}

enum OrphanRemoveActions {
ORPHAN_REMOVE_NORMAL, ORPHAN_REMOVE_REUSE, ORPHAN_REMOVE_EXTRA
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/xdag/core/ImportResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.apache.tuweni.bytes.MutableBytes32;

import lombok.Getter;

public enum ImportResult {
ERROR,
EXIST,
Expand All @@ -39,6 +41,7 @@ public enum ImportResult {

MutableBytes32 hashLow;

@Getter
String errorInfo;

public MutableBytes32 getHashlow() {
Expand All @@ -49,15 +52,8 @@ public void setHashlow(MutableBytes32 hashLow) {
this.hashLow = hashLow;
}

public String getErrorInfo() {
return errorInfo;
}

public void setErrorInfo(String errorInfo) {
this.errorInfo = errorInfo;
}

public boolean isNormal() {
return this == IMPORTED_NOT_BEST || this == IMPORTED_BEST || this == EXIST || this == IMPORTED_EXTRA || this == IN_MEM;
}
}
7 changes: 5 additions & 2 deletions src/main/java/io/xdag/core/SnapshotInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
package io.xdag.core;

import java.util.Arrays;
import lombok.Data;

@Data
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SnapshotInfo {

protected boolean type; // true PUBKEY false BLOCK_DATA
Expand Down
Loading

0 comments on commit 788343c

Please sign in to comment.