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

Fix some code spec issues #280

Merged
merged 1 commit into from
Sep 15, 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
2 changes: 1 addition & 1 deletion src/main/java/io/xdag/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class Kernel {
protected OrphanBlockStore orphanBlockStore;
protected TransactionHistoryStore txHistoryStore;

protected SnapshotStore SnapshotStore;
protected SnapshotStore snapshotStore;
protected Blockchain blockchain;
protected NetDB netDB;
protected PeerClient client;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/xdag/consensus/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.crypto.SecureRandomProvider;

import java.util.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -220,8 +221,7 @@ public boolean syncPushBlock(BlockWrapper blockWrapper, Bytes32 hashLow) {
if(syncMap.size() >= MAX_SIZE){
for (int j = 0; j < DELETE_NUM; j++) {
List<Bytes32> keyList = new ArrayList<>(syncMap.keySet());
Random rand = new Random();
Bytes32 key = keyList.get(rand.nextInt(keyList.size()));
Bytes32 key = keyList.get(SecureRandomProvider.publicSecureRandom().nextInt(keyList.size()));
assert key != null;
if(syncMap.remove(key) != null) blockchain.getXdagStats().nwaitsync--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public List<TxHistory> listTxHistoryByAddress(String address, int page, Object..
int page_size = Integer.parseInt(parameters[2].toString());
PAGE_SIZE = (page_size > 0 && page_size <= TX_PAGE_SIZE_LIMIT) ? page_size : PAGE_SIZE;
}
default -> {
}
}
try {
conn = DruidUtils.getConnection();
Expand Down
45 changes: 40 additions & 5 deletions src/main/java/io/xdag/utils/DruidUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,68 @@ public static Connection getConnection(){
}
}
public static void close(Connection connection, Statement statement){
if (connection != null && statement != null){
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

if (connection != null){
try {
connection.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet){
if (connection != null && statement != null && resultSet != null){
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

if(statement != null) {
try {
statement.close();
connection.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

if(connection != null) {
try {
resultSet.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}
}

public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet){
if (connection != null && statement != null && resultSet != null){
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

if(statement != null) {
try {
statement.close();
connection.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

if(connection != null) {
try {
resultSet.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/io/xdag/cli/XdagCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void testMain() throws Exception {
@Test
public void testHelp() throws Exception {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setOut(new PrintStream(captureOutputStream));
setOut(new PrintStream(captureOutputStream,true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.start(new String[]{"--help"});

Expand All @@ -108,7 +109,7 @@ public void testHelp() throws Exception {
@Test
public void testVersion() throws Exception {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setOut(new PrintStream(captureOutputStream));
setOut(new PrintStream(captureOutputStream, true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.start(new String[]{"--version"});
assertEquals(Constants.CLIENT_VERSION + "\n", tapSystemOut(xdagCLI::printVersion));
Expand Down Expand Up @@ -249,7 +250,7 @@ public void testStartKernelWithEmptyWallet() throws Exception {
@Test
public void testStartKernelWithEmptyWalletInvalidNewPassword() throws Exception {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setErr(new PrintStream(captureOutputStream));
setErr(new PrintStream(captureOutputStream, true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.setConfig(config);
// mock wallet
Expand Down Expand Up @@ -297,7 +298,7 @@ public void testAccountList() throws Exception {
@Test
public void testCreateAccount() throws Exception {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setOut(new PrintStream(captureOutputStream));
setOut(new PrintStream(captureOutputStream, true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.setConfig(config);
// mock wallet
Expand Down Expand Up @@ -355,7 +356,7 @@ public void testListAccounts() throws Exception {
@Test
public void testChangePasswordIncorrectConfirmation() {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setErr(new PrintStream(captureOutputStream));
setErr(new PrintStream(captureOutputStream, true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.setConfig(config);

Expand Down Expand Up @@ -480,7 +481,7 @@ public void testImportPrivateKeyFailedToFlushWalletFile() throws Exception {
@Test
public void testImportPrivateKey() throws Exception {
ByteArrayOutputStream captureOutputStream = new ByteArrayOutputStream();
setOut(new PrintStream(captureOutputStream));
setOut(new PrintStream(captureOutputStream, true, Charset.defaultCharset()));
XdagCli xdagCLI = spy(new XdagCli());
xdagCLI.setConfig(config);

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/xdag/core/XAmountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testGtLtEtc() {
}

@Test
public void TestAmount2xdag() {
public void testAmount2xdag() {
assertEquals(972.80, XAmount.ofXAmount(4178144185548L).toDecimal(2, XDAG).doubleValue(), 0.0);

// 3333333334
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
import io.xdag.utils.XdagTime;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.crypto.SECPPrivateKey;
import org.hyperledger.besu.crypto.SecureRandomProvider;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -67,22 +69,22 @@ PRIMARY KEY (`fid`),
KEY `faddress_index` (`faddress`)
)
""";
Random random = new Random();
long txPageSizeLimit = random.nextLong();
long txPageSizeLimit = SecureRandomProvider.publicSecureRandom().nextLong();
private final TransactionHistoryStore txHistoryStore = new TransactionHistoryStoreImpl(txPageSizeLimit);

BigInteger private_1 = new BigInteger("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4", 16);
BigInteger private_2 = new BigInteger("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aa66", 16);
SECPPrivateKey secretkey_1 = SECPPrivateKey.create(private_1, Sign.CURVE_NAME);
SECPPrivateKey secretkey_2 = SECPPrivateKey.create(private_2, Sign.CURVE_NAME);
@BeforeClass
public static void setUp() throws Exception {
Statement stmt;
public static void setUp() throws SQLException {
Statement stmt = null;
Connection conn = DruidUtils.getConnection();
if (conn != null) {
stmt = conn.createStatement();
stmt.execute(SQL_CTEATE_TABLE);
}
DruidUtils.close(conn, stmt);
}

@Test
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/io/xdag/net/NodeManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
public class NodeManagerTest {

Config config = new DevnetConfig();
Kernel kernel;

@Before
public void setUp() throws Exception {
Expand All @@ -53,9 +52,7 @@ public void setUp() throws Exception {
InetSocketAddress inetSocketAddress = new InetSocketAddress(address.split(":")[0],Integer.parseInt(address.split(":")[1]));
addressList.add(inetSocketAddress);
}
KeyPair key = KeyPair.create(SampleKeys.SRIVATE_KEY, Sign.CURVE, Sign.CURVE_NAME);
config.getNodeSpec().setWhiteIPList(addressList);
kernel = new Kernel(config, key);
}

@Test
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/io/xdag/net/WhliteListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

public class WhliteListTest {

Kernel kernel;
Config config = new DevnetConfig();

@Before
Expand All @@ -57,11 +56,10 @@ public void setup() throws InvalidAlgorithmParameterException, NoSuchAlgorithmEx
addressList.add(new InetSocketAddress(address.split(":")[0],Integer.parseInt(address.split(":")[1])));
}
config.getNodeSpec().setWhiteIPList(addressList);
kernel = new Kernel(config, Keys.createEcKeyPair());
}

@Test
public void WhileList()
public void whileList()
throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
PeerClient client = new PeerClient(config, Keys.createEcKeyPair());
// 新增白名单节点
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/io/xdag/rpc/Web3XdagModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ public class Web3XdagModuleTest {
Kernel kernel;
DatabaseFactory dbFactory;

BigInteger private_1 = new BigInteger("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4", 16);
BigInteger private_2 = new BigInteger("10a55f0c18c46873ddbf9f15eddfc06f10953c601fd144474131199e04148046", 16);

@Before
public void setUp() throws Exception {
config.getNodeSpec().setStoreDir(root.newFolder().getAbsolutePath());
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/xdag/utils/BasicUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class BasicUtilsTest {

@Test
public void TestXdag2amount() {
public void testXdag2amount() {
BigDecimal a = BigDecimal.valueOf(972.8);
assertEquals(4178144185549L, BasicUtils.xdag2amount(a.doubleValue()).toLong());

Expand All @@ -53,13 +53,13 @@ public void TestXdag2amount() {
}

@Test(expected = XdagOverFlowException.class)
public void TestXdag2amountOverflow() {
public void testXdag2amountOverflow() {
double d = -1.3;
BasicUtils.xdag2amount(d);
}

@Test
public void TestAmount2xdag() {
public void testAmount2xdag() {
long a = 4178144185548L;
// 3CC CCCC CCCD?
assertEquals(972.799999999814, BasicUtils.amount2xdag(a), 0.0);
Expand Down