Skip to content

Commit

Permalink
add windows stdout handling from shared lib;
Browse files Browse the repository at this point in the history
adjust info msgs when executing from Blockchain
  • Loading branch information
neodix42 committed Nov 8, 2024
1 parent fae096d commit 6f6b6ed
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 144 deletions.
160 changes: 91 additions & 69 deletions blockchain/src/main/java/org/ton/java/Blockchain.java

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions blockchain/src/test/java/org/ton/java/BlockchainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@ public void testSendMessageV3R2ContractOnEmulator() {

blockchain.sendExternal(msg);
}

@Test
public void testSendMessageCustomContractOnTestnetTolk() {
Blockchain blockchain =
Blockchain.builder()
.network(Network.TESTNET)
.customContractAsResource("simple.tolk")
.customContractDataCell(
CellBuilder.beginCell()
.storeUint(0, 32)
.storeInt(Utils.getRandomInt(), 32)
.endCell())
.build();
assertThat(blockchain.deploy(30)).isTrue();
GetterResult result = blockchain.runGetMethod("unique");
System.out.printf("result %s\n", result);
System.out.printf("returned seqno %s\n", blockchain.runGetSeqNo());
}
}
5 changes: 5 additions & 0 deletions emulator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<version>0.7.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.15.0</version>
</dependency>
</dependencies>

</project>
15 changes: 11 additions & 4 deletions emulator/src/main/java/org/ton/java/emulator/tvm/TvmEmulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TvmEmulator {
private String codeBoc;
private String dataBoc;
private TvmVerbosityLevel verbosityLevel;
private Boolean printEmulatorInfo;

public static class TvmEmulatorBuilder {}

Expand All @@ -54,6 +55,10 @@ public TvmEmulator build() {
}
}

if (isNull(super.printEmulatorInfo)) {
super.printEmulatorInfo = true;
}

super.tvmEmulatorI = Native.load(super.pathToEmulatorSharedLib, TvmEmulatorI.class);
if (isNull(super.verbosityLevel)) {
super.verbosityLevel = TvmVerbosityLevel.WITH_ALL_STACK_VALUES;
Expand All @@ -72,10 +77,12 @@ public TvmEmulator build() {
throw new Error("Can't create emulator instance");
}

log.info(
"\nTON TVM Emulator configuration:\n" + "Location: {}\n" + "Verbosity level: {}\n",
super.pathToEmulatorSharedLib,
super.verbosityLevel);
if (super.printEmulatorInfo) {
log.info(
"\nTON TVM Emulator configuration:\n" + "Location: {}\n" + "Verbosity level: {}\n",
super.pathToEmulatorSharedLib,
super.verbosityLevel);
}
return super.build();
}
}
Expand Down
63 changes: 54 additions & 9 deletions emulator/src/main/java/org/ton/java/emulator/tx/TxEmulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class TxEmulator {
private TxEmulatorConfig configType;
private String customConfig;
private TxVerbosityLevel verbosityLevel;
private Boolean printEmulatorInfo;

public static class TxEmulatorBuilder {}

Expand All @@ -53,9 +56,16 @@ public TxEmulator build() {
}
}

if (isNull(super.printEmulatorInfo)) {
super.printEmulatorInfo = true;
}

super.txEmulatorI = Native.load(super.pathToEmulatorSharedLib, TxEmulatorI.class);

redirectNativeOutput();

if (isNull(super.verbosityLevel)) {
super.verbosityLevel = TxVerbosityLevel.TRUNCATED;
super.verbosityLevel = TxVerbosityLevel.WITH_ALL_STACK_VALUES;
}
if (isNull(super.configType)) {
super.configType = TxEmulatorConfig.MAINNET;
Expand Down Expand Up @@ -93,25 +103,60 @@ public TxEmulator build() {
super.txEmulatorI.transaction_emulator_create(
configBoc, super.verbosityLevel.ordinal());

super.txEmulatorI.emulator_set_verbosity_level(super.txEmulator, 0);

if (super.txEmulator == 0) {
throw new Error("Can't create tx emulator instance");
}

log.info(
"\nTON Tx Emulator configuration:\n"
+ "Location: {}\n"
+ "Config: {}\n"
+ "Verbosity level: {}",
super.pathToEmulatorSharedLib,
super.configType,
super.verbosityLevel);
if (super.printEmulatorInfo) {

log.info(
"\nTON Tx Emulator configuration:\n"
+ "Location: {}\n"
+ "Config: {}\n"
+ "Verbosity level: {}",
super.pathToEmulatorSharedLib,
super.configType,
super.verbosityLevel);
}
return super.build();
} catch (Exception e) {
throw new Error("Error creating tx emulator instance: " + e.getMessage());
}
}
}

private static void redirectNativeOutput() {

// Redirect native output on Windows
WinNT.HANDLE originalOut = Kernel32.INSTANCE.GetStdHandle(Kernel32.STD_OUTPUT_HANDLE);
WinNT.HANDLE originalErr = Kernel32.INSTANCE.GetStdHandle(Kernel32.STD_ERROR_HANDLE);

// try (FileOutputStream nulStream = new FileOutputStream("NUL")) {
WinNT.HANDLE hNul =
Kernel32.INSTANCE.CreateFile(
"NUL",
Kernel32.GENERIC_WRITE,
Kernel32.FILE_SHARE_WRITE,
null,
Kernel32.OPEN_EXISTING,
0,
null);

// Redirect stdout and stderr to NUL
Kernel32.INSTANCE.SetStdHandle(Kernel32.STD_OUTPUT_HANDLE, hNul);
Kernel32.INSTANCE.SetStdHandle(Kernel32.STD_ERROR_HANDLE, hNul);

// // Close the handle to NUL
// Kernel32.INSTANCE.CloseHandle(hNul);
// } finally {
// // Restore original stdout and stderr
// Kernel32.INSTANCE.SetStdHandle(Kernel32.STD_OUTPUT_HANDLE, originalOut);
// Kernel32.INSTANCE.SetStdHandle(Kernel32.STD_ERROR_HANDLE, originalErr);
// }
}

public void destroy() {
txEmulatorI.transaction_emulator_destroy(txEmulator);
}
Expand Down
30 changes: 22 additions & 8 deletions fift/src/main/java/org/ton/java/fift/FiftRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ton.java.fift;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

import java.io.File;
Expand All @@ -17,6 +18,7 @@ public class FiftRunner {
String fiftAsmLibraryPath;
String fiftSmartcontLibraryPath;
public String fiftExecutablePath;
private Boolean printInfo;

public static String fiftExecutable = "";

Expand All @@ -31,8 +33,14 @@ private static class CustomFiftRunnerBuilder extends FiftRunnerBuilder {

@Override
public FiftRunner build() {
if (isNull(super.printInfo)) {
super.printInfo = true;
}

if (StringUtils.isEmpty(super.fiftExecutablePath)) {
log.info("Checking if Fift is installed...");
if (super.printInfo) {
log.info("Checking if Fift is installed...");
}
String errorMsg =
"Make sure you have Fift installed. See https://github.com/ton-blockchain/packages for instructions.\nYou can also specify full path via SmartContractCompiler.fiftExecutablePath().";
try {
Expand All @@ -43,13 +51,17 @@ public FiftRunner build() {
throw new Error("Cannot execute simple Fift command.\n" + errorMsg);
}
fiftAbsolutePath = Utils.detectAbsolutePath("fift", false);
log.info("Fift found at " + fiftAbsolutePath);
if (super.printInfo) {
log.info("Fift found at " + fiftAbsolutePath);
}
fiftExecutable = "fift";
} catch (Exception e) {
throw new Error("Cannot execute simple Fift command.\n" + errorMsg);
}
} else {
log.info("using " + super.fiftExecutablePath);
if (super.printInfo) {
log.info("using " + super.fiftExecutablePath);
}
fiftExecutable = super.fiftExecutablePath;
}

Expand Down Expand Up @@ -83,12 +95,14 @@ public FiftRunner build() {
}
}
}
log.info(
"using include dirs: "
+ super.fiftAsmLibraryPath
+ ", "
+ super.fiftSmartcontLibraryPath);

if (super.printInfo) {
log.info(
"using include dirs: "
+ super.fiftAsmLibraryPath
+ ", "
+ super.fiftSmartcontLibraryPath);
}
return super.build();
}
}
Expand Down
19 changes: 15 additions & 4 deletions func/src/main/java/org/ton/java/func/FuncRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ton.java.func;

import static java.util.Objects.isNull;

import java.util.concurrent.TimeUnit;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -12,7 +14,7 @@
public class FuncRunner {

public String funcExecutablePath;

private Boolean printInfo;
public static String funcExecutable = "";

public static class FuncRunnerBuilder {}
Expand All @@ -25,8 +27,13 @@ private static class CustomFuncRunnerBuilder extends FuncRunnerBuilder {

@Override
public FuncRunner build() {
if (isNull(super.printInfo)) {
super.printInfo = true;
}
if (StringUtils.isEmpty(super.funcExecutablePath)) {
log.info("Checking if Func is installed...");
if (isNull(super.printInfo)) {
log.info("Checking if Func is installed...");
}

String errorMsg =
"Make sure you have Func installed. See https://github.com/ton-blockchain/packages for instructions.\nYou can also specify full path via SmartContractCompiler.funcExecutablePath().";
Expand All @@ -39,15 +46,19 @@ public FuncRunner build() {
}
String funcAbsolutePath = Utils.detectAbsolutePath("func", false);

log.info("Func found at " + funcAbsolutePath);
if (isNull(super.printInfo)) {
log.info("Func found at " + funcAbsolutePath);
}
funcExecutable = "func";

} catch (Exception e) {
log.info(e.getMessage());
throw new Error("Cannot execute simple Func command.\n" + errorMsg);
}
} else {
log.info("using " + super.funcExecutablePath);
if (isNull(super.printInfo)) {
log.info("using " + super.funcExecutablePath);
}
funcExecutable = super.funcExecutablePath;
}
return super.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class SmartContractCompiler {

private TolkRunner tolkRunner;

private Boolean printInfo;

private boolean printFiftAsmOutput;

public static class SmartContractCompilerBuilder {}
Expand All @@ -43,6 +45,9 @@ public static SmartContractCompilerBuilder builder() {
private static class CustomSmartContractCompilerBuilder extends SmartContractCompilerBuilder {
@Override
public SmartContractCompiler build() {
if (isNull(super.printInfo)) {
super.printInfo = true;
}
if (isNull(super.funcRunner)) {
super.funcRunner = FuncRunner.builder().build();
}
Expand Down Expand Up @@ -70,7 +75,9 @@ public String compile() {
throw new Error("Can't find resource " + contractAsResource);
}
}
log.info("workdir " + new File(contractPath).getParent());
if (isNull(printInfo)) {
log.info("workdir " + new File(contractPath).getParent());
}

String outputFiftAsmFile;
if (contractPath.contains(".func") || contractPath.contains(".fc")) {
Expand Down
20 changes: 17 additions & 3 deletions tolk/src/main/java/org/ton/java/tolk/TolkRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ton.java.tolk;

import static java.util.Objects.isNull;

import java.util.concurrent.TimeUnit;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -12,6 +14,7 @@
public class TolkRunner {

public String tolkExecutablePath;
private Boolean printInfo;

public static String tolkExecutable = "";

Expand All @@ -25,8 +28,15 @@ private static class CustomTolkRunnerBuilder extends TolkRunnerBuilder {

@Override
public TolkRunner build() {

if (isNull(super.printInfo)) {
super.printInfo = true;
}

if (StringUtils.isEmpty(super.tolkExecutablePath)) {
log.info("Checking if Tolk is installed...");
if (super.printInfo) {
log.info("Checking if Tolk is installed...");
}

String errorMsg =
"Make sure you have Tolk installed. See https://github.com/ton-blockchain/packages for instructions.\nYou can also specify full path via SmartContractCompiler.tolkExecutablePath().";
Expand All @@ -39,15 +49,19 @@ public TolkRunner build() {
}
String tolkAbsolutePath = Utils.detectAbsolutePath("tolk", false);

log.info("Tolk found at " + tolkAbsolutePath);
if (super.printInfo) {
log.info("Tolk found at " + tolkAbsolutePath);
}
tolkExecutable = "tolk";

} catch (Exception e) {
log.info(e.getMessage());
throw new Error("Cannot execute simple Tolk command.\n" + errorMsg);
}
} else {
log.info("using " + super.tolkExecutablePath);
if (super.printInfo) {
log.info("using " + super.tolkExecutablePath);
}
tolkExecutable = super.tolkExecutablePath;
}
return super.build();
Expand Down
5 changes: 5 additions & 0 deletions tonlib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Loading

0 comments on commit 6f6b6ed

Please sign in to comment.