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

test(LiteFullNode): add DbLiteTest test case #5384

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
compile 'io.github.tronprotocol:leveldbjni-all:1.18.2'
compile 'io.github.tronprotocol:leveldb:1.18.2'
compile project(":protocol")
testCompile project(":framework")
}

check.dependsOn 'lint'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tron.program;
package org.tron.plugins;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
Expand All @@ -16,16 +16,16 @@
import org.tron.common.config.DbBackupConfig;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.PublicMethod;
import org.tron.common.utils.Utils;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.services.RpcApiService;
import org.tron.core.services.interfaceOnSolidity.RpcApiServiceOnSolidity;
import org.tron.tool.litefullnode.LiteFullNodeTool;
import org.tron.plugins.utils.PublicMethod;
import picocli.CommandLine;

@Slf4j
public class LiteFullNodeToolTest {
public class DbLiteTest {

private TronApplicationContext context;
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
Expand Down Expand Up @@ -134,7 +134,7 @@ private void testTools(String dbType, int checkpointVersion)
"--dataset-path", dbPath + File.separator + "history"};
Args.getInstance().getStorage().setDbEngine(dbType);
Args.getInstance().getStorage().setCheckpointVersion(checkpointVersion);
LiteFullNodeTool.setRecentBlks(3);
DbLite.setRecentBlks(3);
// start fullNode
startApp();
// produce transactions for 18 seconds
Expand All @@ -144,15 +144,17 @@ private void testTools(String dbType, int checkpointVersion)
// delete tran-cache
FileUtil.deleteDir(Paths.get(dbPath, databaseDir, "trans-cache").toFile());
// generate snapshot
LiteFullNodeTool.main(argsForSnapshot);
CommandLine snapshotCommand = new CommandLine(new DbLite());
snapshotCommand.execute(argsForSnapshot);
// start fullNode
startApp();
// produce transactions for 6 seconds
generateSomeTransactions(6);
// stop the node
shutdown();
// generate history
LiteFullNodeTool.main(argsForHistory);
CommandLine historyCommand = new CommandLine(new DbLite());
historyCommand.execute(argsForHistory);
// backup original database to database_bak
File database = new File(Paths.get(dbPath, databaseDir).toString());
if (!database.renameTo(new File(Paths.get(dbPath, databaseDir + "_bak").toString()))) {
Expand All @@ -173,12 +175,13 @@ private void testTools(String dbType, int checkpointVersion)
// stop the node
shutdown();
// merge history
LiteFullNodeTool.main(argsForMerge);
CommandLine mergeCommand = new CommandLine(new DbLite());
mergeCommand.execute(argsForMerge);
// start and validate
startApp();
generateSomeTransactions(6);
shutdown();
LiteFullNodeTool.reSetRecentBlks();
DbLite.reSetRecentBlks();
}

private void generateSomeTransactions(int during) {
Expand Down
163 changes: 163 additions & 0 deletions plugins/src/test/java/org/tron/plugins/utils/PublicMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package org.tron.plugins.utils;

import com.google.protobuf.ByteString;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import java.util.Random;
import org.tron.api.GrpcAPI;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.Wallet;
import org.tron.protos.Protocol;
import org.tron.protos.contract.BalanceContract;

/**
* @author liukai
* @since 2023/5/24.
*/
public class PublicMethod {

public static byte[] getFinalAddress(String priKey) {
Wallet.setAddressPreFixByte((byte) 0x41);
ECKey key = ECKey.fromPrivate(new BigInteger(priKey, 16));
return key.getAddress();
}

public static Boolean sendcoin(byte[] to, long amount, byte[] owner, String priKey,
WalletGrpc.WalletBlockingStub blockingStubFull) {
Wallet.setAddressPreFixByte((byte) 0x41);
ECKey temKey = null;
try {
BigInteger priK = new BigInteger(priKey, 16);
temKey = ECKey.fromPrivate(priK);
} catch (Exception ex) {
ex.printStackTrace();
}
final ECKey ecKey = temKey;

int times = 0;
while (times++ <= 2) {

BalanceContract.TransferContract.Builder builder =
BalanceContract.TransferContract.newBuilder();
com.google.protobuf.ByteString bsTo = com.google.protobuf.ByteString.copyFrom(to);
com.google.protobuf.ByteString bsOwner = ByteString.copyFrom(owner);
builder.setToAddress(bsTo);
builder.setOwnerAddress(bsOwner);
builder.setAmount(amount);

BalanceContract.TransferContract contract = builder.build();
Protocol.Transaction transaction = blockingStubFull.createTransaction(contract);
if (transaction == null || transaction.getRawData().getContractCount() == 0) {
continue;
}
transaction = signTransaction(ecKey, transaction);
GrpcAPI.Return response = broadcastTransaction(transaction, blockingStubFull);
return response.getResult();
}
return false;
}

/**
* Sign TX.
*
* @param ecKey ecKey of the private key
* @param transaction transaction object
*/
public static Protocol.Transaction signTransaction(ECKey ecKey,
Protocol.Transaction transaction) {
if (ecKey == null || ecKey.getPrivKey() == null) {
return null;
}
transaction = setTimestamp(transaction);
return sign(transaction, ecKey);
}

/**
* Broadcast TX.
*
* @param transaction transaction object
* @param blockingStubFull Grpc interface
*/
public static GrpcAPI.Return broadcastTransaction(
Protocol.Transaction transaction, WalletGrpc.WalletBlockingStub blockingStubFull) {
int i = 10;
GrpcAPI.Return response = blockingStubFull.broadcastTransaction(transaction);
while (!response.getResult() && response.getCode() == GrpcAPI.Return.response_code.SERVER_BUSY
&& i > 0) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
i--;
response = blockingStubFull.broadcastTransaction(transaction);
}
return response;

}

/**
* constructor.
*/

public static Protocol.Transaction setTimestamp(Protocol.Transaction transaction) {
long currentTime = System.currentTimeMillis();//*1000000 + System.nanoTime()%1000000;
Protocol.Transaction.Builder builder = transaction.toBuilder();
org.tron.protos.Protocol.Transaction.raw.Builder rowBuilder = transaction.getRawData()
.toBuilder();
rowBuilder.setTimestamp(currentTime);
builder.setRawData(rowBuilder.build());
return builder.build();
}

public static Protocol.Transaction sign(Protocol.Transaction transaction, ECKey myKey) {
ByteString lockSript = ByteString.copyFrom(myKey.getAddress());
Protocol.Transaction.Builder transactionBuilderSigned = transaction.toBuilder();

byte[] hash = Sha256Hash.hash(CommonParameter
.getInstance().isECKeyCryptoEngine(), transaction.getRawData().toByteArray());
List<Protocol.Transaction.Contract> listContract = transaction.getRawData().getContractList();
for (int i = 0; i < listContract.size(); i++) {
ECKey.ECDSASignature signature = myKey.sign(hash);
ByteString bsSign = ByteString.copyFrom(signature.toByteArray());
transactionBuilderSigned.addSignature(
bsSign);//Each contract may be signed with a different private key in the future.
}

transaction = transactionBuilderSigned.build();
return transaction;
}

public static int chooseRandomPort() {
return chooseRandomPort(10240, 65000);
}

public static int chooseRandomPort(int min, int max) {
int port = new Random().nextInt(max - min + 1) + min;
try {
while (!checkPortAvailable(port)) {
port = new Random().nextInt(max - min + 1) + min;
}
} catch (IOException e) {
return new Random().nextInt(max - min + 1) + min;
}
return port;
}

private static boolean checkPortAvailable(int port) throws IOException {
InetAddress theAddress = InetAddress.getByName("127.0.0.1");
try (Socket socket = new Socket(theAddress, port)) {
// only check
socket.getPort();
} catch (IOException e) {
return true;
}
return false;
}
}