Skip to content

Commit

Permalink
minor tweaks (logging, exceptions)
Browse files Browse the repository at this point in the history
  • Loading branch information
h6x0r committed Aug 14, 2024
1 parent edbe500 commit 9620a80
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.logging.Logger;

import static java.util.Objects.isNull;

Expand All @@ -14,7 +15,7 @@
* Interestingly, but this solution loses to Deque<Boolean> array solution from memory footprint allocation.
*/
public class RealBitString {

private static final Logger log = Logger.getLogger(RealBitString.class.getName());
byte[] array;
public int writeCursor;
public int readCursor;
Expand Down Expand Up @@ -625,7 +626,7 @@ public void setTopUppedArray(byte[] arr, boolean fulfilledBytes) {
// writeCursor = saveWriteCursor;

if (!foundEndBit) {
System.err.println(Arrays.toString(arr) + ", " + fulfilledBytes);
log.info((Arrays.toString(arr) + ", " + fulfilledBytes));
throw new Error("Incorrect TopUppedArray");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ public LiteClient build() {
}
}

System.out.printf("Java Lite-Client configuration:\n" +
log.info(String.format("Java Lite-Client configuration:\n" +
"Location: %s\n" +
"Path to global config: %s\n" +
"Testnet: %s%n",
super.pathToLiteClientBinary,
super.pathToGlobalConfig,
super.testnet);
super.testnet));

} catch (Exception e) {
throw new RuntimeException("Error creating lite-client instance: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static Pair<Process, String> execute(String pathToBinary, String workDir,
log.info(e.getMessage());
return null;
} catch (Exception e) {
log.info(e.getMessage());
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static class CustomFiftRunnerBuilder extends FiftRunnerBuilder {
@Override
public FiftRunner build() {
if (StringUtils.isEmpty(super.fiftExecutablePath)) {
System.out.println("checking if fift is installed...");
log.info("checking if fift is installed...");
String errorMsg = "Make sure you have fift and func installed. See https://github.com/ton-blockchain/packages for instructions.";
try {
ProcessBuilder pb = new ProcessBuilder("fift", "-h").redirectErrorStream(true);
Expand All @@ -48,13 +48,13 @@ public FiftRunner build() {
throw new Error("Cannot execute simple fift command.\n" + errorMsg);
}
fiftAbsolutePath = detectAbsolutePath();
System.out.println("fift found at " + fiftAbsolutePath);
log.info("fift found at " + fiftAbsolutePath);
super.fiftExecutable = "fift";
} catch (Exception e) {
throw new Error("Cannot execute simple fift command.\n" + errorMsg);
}
} else {
System.out.println("using " + super.fiftExecutablePath);
log.info("using " + super.fiftExecutablePath);
super.fiftExecutable = super.fiftExecutablePath;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public FiftRunner build() {
}
}
}
System.out.println("using include dirs: " + super.fiftAsmLibraryPath + ", " + super.fiftSmartcontLibraryPath);
log.info("using include dirs: " + super.fiftAsmLibraryPath + ", " + super.fiftSmartcontLibraryPath);

return super.build();
}
Expand Down Expand Up @@ -136,7 +136,7 @@ private static String detectAbsolutePath() {
}
return null;
} catch (Exception e) {
throw new Error("Cannot detect absolute path to executable " + "fift");
throw new Error("Cannot detect absolute path to executable fift " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static class CustomFuncRunnerBuilder extends FuncRunnerBuilder {
@Override
public FuncRunner build() {
if (StringUtils.isEmpty(super.funcExecutablePath)) {
System.out.println("checking if func is installed...");
log.info("checking if func is installed...");

String errorMsg = "Make sure you have fift and func installed. See https://github.com/ton-blockchain/packages for instructions.";
try {
Expand All @@ -45,15 +45,15 @@ public FuncRunner build() {
}
String funcAbsolutePath = detectAbsolutePath();

System.out.println("func found at " + funcAbsolutePath);
log.info("func found at " + funcAbsolutePath);
super.funcExecutable = "func";

} catch (Exception e) {
e.printStackTrace();
log.info(e.getMessage());
throw new Error("Cannot execute simple func command.\n" + errorMsg);
}
} else {
System.out.println("using " + super.funcExecutablePath);
log.info("using " + super.funcExecutablePath);
super.funcExecutable = super.funcExecutablePath;
}
return super.build();
Expand All @@ -63,7 +63,11 @@ public FuncRunner build() {
public String run(String workdir, String... params) {
Pair<Process, String> result = Executor.execute(funcExecutable, workdir, params);

return result.getRight();
if (result != null && result.getRight() != null) {
return result.getRight();
}

return "";
}

private static String detectAbsolutePath() {
Expand All @@ -89,7 +93,7 @@ private static String detectAbsolutePath() {
}
return null;
} catch (Exception e) {
throw new Error("Cannot detect absolute path to executable func");
throw new Error("Cannot detect absolute path to executable func " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SmartContractCompiler build() {
* @return code of BoC in hex
*/
public String compile() throws IOException {
System.out.println("workdir " + new File(contractPath).getParent());
log.info("workdir " + new File(contractPath).getParent());

String outputFiftAsmFile = funcRunner.run(new File(contractPath).getParent(), "-W", "dummy.boc", contractPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DnsUtils {
* @return BigInteger
*/
static BigInteger categoryToInt(String category) {
if ((isNull(category)) || (category.length() == 0)) {
if ((isNull(category)) || (category.isEmpty())) {
return BigInteger.ZERO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public Cell createDataCell() {
);
cell.storeDict(cellDict);

cell.storeCoins(isNull(lockupConfig.getTotalLockedalue()) ? BigInteger.ZERO : lockupConfig.getTotalLockedalue());
cell.storeCoins(isNull(lockupConfig.getTotalLockedValue()) ? BigInteger.ZERO : lockupConfig.getTotalLockedValue());
cell.storeBit(false); // empty locked dict
cell.storeCoins(isNull(lockupConfig.getTotalRestrictedValue()) ? BigInteger.ZERO : lockupConfig.getTotalRestrictedValue());
cell.storeBit(false); // empty restricted dict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iwebpp.crypto.TweetNaclFast;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.java.Log;
import org.apache.commons.lang3.tuple.Pair;
import org.ton.java.address.Address;
import org.ton.java.cell.*;
Expand All @@ -21,6 +22,7 @@

@Builder
@Getter
@Log
public class MultiSigWallet implements Contract {

//https://github.com/akifoq/multisig/blob/master/multisig-code.fc
Expand Down Expand Up @@ -138,7 +140,6 @@ public List<BigInteger> getPublicKeys() {
for (Map.Entry<Object, Object> entry : loadedDict.elements.entrySet()) {
CellSlice cSlice = CellSlice.beginParse((Cell) entry.getValue());
BigInteger pubKey = cSlice.loadUint(256);
long flood = cSlice.loadUint(8).longValue();
publicKeys.add(pubKey);
}
return publicKeys;
Expand Down Expand Up @@ -446,7 +447,7 @@ private static void checkIfSignatureExists(Cell order, byte[] signature) {
Cell ref = cs.loadRef();
while (nonNull(ref)) {
byte[] sig = CellSlice.beginParse(ref).loadBytes(512);
System.out.println("sig " + Utils.bytesToHex(signature));
log.info("sig " + Utils.bytesToHex(signature));
if (sig == signature) {
throw new Error("Your signature is already presented");
}
Expand All @@ -468,7 +469,7 @@ public static Cell addSignature1(Cell order, int pubkeyIndex, TweetNaclFast.Sign

byte[] signature = signCell(keyPair, o.endCell());

System.out.println("sig " + Utils.bytesToHex(signature));
log.info("sig " + Utils.bytesToHex(signature));

// checkIfSignatureExists(order, signature);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iwebpp.crypto.TweetNaclFast;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;
Expand All @@ -27,6 +28,7 @@

@Builder
@Getter
@Log
public class JettonMinter implements Contract {
TweetNaclFast.Signature.KeyPair keyPair;
Address adminAddress;
Expand Down Expand Up @@ -95,7 +97,7 @@ public Cell createDataCell() {
@Override
public Cell createCodeCell() {
if (StringUtils.isNotEmpty(code)) {
System.out.println("Using custom JettonMinter");
log.info("Using custom JettonMinter");
return CellBuilder.beginCell().
fromBoc(code).
endCell();
Expand Down Expand Up @@ -195,7 +197,7 @@ public JettonMinterData getJettonData(Tonlib tonlib) {
String jettonContentUri = null;
try {
jettonContentUri = NftUtils.parseOffChainUriCell(jettonContentCell);
} catch (Exception e) {
} catch (Error e) {
//todo
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.iwebpp.crypto.TweetNaclFast;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;
Expand All @@ -27,6 +28,7 @@

@Builder
@Getter
@Log
public class JettonMinterStableCoin implements Contract {
TweetNaclFast.Signature.KeyPair keyPair;
Address adminAddress;
Expand Down Expand Up @@ -77,7 +79,7 @@ public String getName() {
@Override
public Cell createDataCell() {
if (StringUtils.isNotEmpty(code)) {
System.out.println("Using custom JettonMinter");
log.info("Using custom JettonMinter");
return CellBuilder.beginCell()
.storeCoins(BigInteger.ZERO)
.storeAddress(adminAddress)
Expand All @@ -101,7 +103,7 @@ public Cell createDataCell() {
@Override
public Cell createCodeCell() {
if (StringUtils.isNotEmpty(code)) {
System.out.println("Using custom JettonMinter");
log.info("Using custom JettonMinter");
return CellBuilder.beginCell().
fromBoc(code).
endCell();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ton.java.smartcontract.token.nft;

import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;
Expand All @@ -19,6 +20,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Log
public class NftUtils {


Expand Down Expand Up @@ -47,7 +49,7 @@ static String parseUri(String uri) {
try {
return URLDecoder.decode(uri, String.valueOf(StandardCharsets.UTF_8));
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
log.info(e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AdnlAddress(AdnlAddress anyForm) {
}

public AdnlAddress(String anyForm) {
if ((isNull(anyForm)) || (anyForm.length() == 0)) {
if ((isNull(anyForm)) || (anyForm.isEmpty())) {
throw new Error("Invalid address");
}
if ((anyForm.length() != 64)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public class LockupConfig {
public List<String> allowedDestinations;

public BigInteger totalRestrictedValue;
public BigInteger totalLockedalue;
public BigInteger totalLockedValue;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ton.java.smartcontract.wallet;


import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.ton.java.address.Address;
import org.ton.java.smartcontract.token.ft.JettonMinter;
Expand All @@ -10,6 +11,7 @@

import java.math.BigInteger;

@Log
public class ContractUtils {

public static long getSeqno(Tonlib tonlib, Address address) {
Expand All @@ -21,7 +23,7 @@ public static boolean isDeployed(Tonlib tonlib, Address address) {
}

public static void waitForDeployment(Tonlib tonlib, Address address, int timeoutSeconds) {
System.out.println("waiting for deployment up to " + timeoutSeconds + " sec");
log.info("waiting for deployment up to " + timeoutSeconds + " sec");
int i = 0;
do {
if (++i * 2 >= timeoutSeconds) {
Expand All @@ -33,7 +35,7 @@ public static void waitForDeployment(Tonlib tonlib, Address address, int timeout
}

public static void waitForBalanceChange(Tonlib tonlib, Address address, int timeoutSeconds) {
System.out.println("waiting for balance change up to " + timeoutSeconds + " sec");
log.info("waiting for balance change up to " + timeoutSeconds + " sec");
BigInteger initialBalance = tonlib.getAccountBalance(address);
int i = 0;
do {
Expand All @@ -46,7 +48,7 @@ public static void waitForBalanceChange(Tonlib tonlib, Address address, int time
}

public static void waitForJettonBalanceChange(Tonlib tonlib, Address jettonMinter, Address address, int timeoutSeconds) {
System.out.println("waiting for jetton balance change up to " + timeoutSeconds + " sec");
log.info("waiting for jetton balance change up to " + timeoutSeconds + " sec");
BigInteger initialBalance = getJettonBalance(tonlib, jettonMinter, address);
int i = 0;
do {
Expand All @@ -69,7 +71,7 @@ public static BigInteger getJettonBalance(Tonlib tonlib, Address jettonMinter, A
JettonWallet jettonWallet = jettonMinterWallet.getJettonWallet(destinationAddress);

return jettonWallet.getBalance();
} catch (Exception e) {
} catch (Error e) {
return new BigInteger("-1");
}
}
Expand Down
2 changes: 1 addition & 1 deletion tl/src/main/java/org/ton/java/tl/types/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Text {
private int chunksNum = 1;

public Cell toCell() {
if (value.length() == 0) {
if (value.isEmpty()) {
return CellBuilder.beginCell().storeUint(0, 8).endCell();
}
if (maxFirstChunkSize > MaxTextChunkSize) {
Expand Down
Loading

0 comments on commit 9620a80

Please sign in to comment.