Skip to content

Commit

Permalink
developed system stdout handling on linux;
Browse files Browse the repository at this point in the history
added to Cell getShortHash() method which returns "xxxx..xxxx";
  • Loading branch information
neodix42 committed Nov 12, 2024
1 parent c74d766 commit 75bc146
Show file tree
Hide file tree
Showing 13 changed files with 399 additions and 182 deletions.
44 changes: 36 additions & 8 deletions blockchain/src/main/java/org/ton/java/Blockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,44 @@ private void initializeTonlib() {
}
}

/**
* There is a huge difference between <code>sendExternal(Cell body)</code> and <code>sendExternal(Message message)</code>.
* The first one sends a body to itself, the second one - sends the message to a destination specified in a message.
* Basically, in the first method we construct <code>MsgUtils.createExternalMessage()</code> with replaced destination address.
*
* @param body Cell
* @return SendExternalResult
*/
public SendExternalResult sendExternal(Cell body) {
Address address;
if (nonNull(contract)) {
address = contract.getAddress();
} else {
address = stateInit.getAddress();
}
Message message = MsgUtils.createExternalMessage(address, null, body);
return sendExternal(message);
}

public SendExternalResult sendExternal(Message message) {

try {

Address address;
if (nonNull(contract)) {
address = contract.getAddress();
} else {
address = stateInit.getAddress();
}
String bounceableAddress =
(network == Network.TESTNET) ? address.toBounceableTestnet() : address.toBounceable();

if (network != Network.EMULATOR) {
String bounceableAddress =
(network == Network.TESTNET)
? contract.getAddress().toBounceableTestnet()
: contract.getAddress().toBounceable();
String initialBalance = Utils.formatNanoValue(tonlib.getAccountBalance(address));
log.info("initialBalance {}", initialBalance);
log.info(
"Sending external message to bounceable address {} on {}...",
"Sending external message (cell-hash: {}) to bounceable address {} on {}...",
message.toCell().getShortHash(),
bounceableAddress,
network);
ExtMessageInfo tonlibResult = tonlib.sendRawMessage(message.toCell().toBase64());
Expand All @@ -350,9 +378,9 @@ public SendExternalResult sendExternal(Message message) {
String initialBalance = Utils.formatNanoValue(customEmulatorShardAccount.getBalance());

log.info(
"Sending external message to bounceable address {} on {}...",
stateInit.getAddress().toBounceable(),
network);
"Sending external message (cell-hash: {}) to bounceable address {} on {}...",
message.toCell().getShortHash(),
bounceableAddress, network);
EmulateTransactionResult emulateTransactionResult =
txEmulator.emulateTransaction(
customEmulatorShardAccount.toCell().toBase64(), message.toCell().toBase64());
Expand Down
72 changes: 57 additions & 15 deletions blockchain/src/test/java/org/ton/java/BlockchainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;
import org.ton.java.cell.CellBuilder;
import org.ton.java.emulator.tvm.TvmVerbosityLevel;
import org.ton.java.emulator.tx.TxVerbosityLevel;
import org.ton.java.smartcontract.faucet.TestnetFaucet;
import org.ton.java.smartcontract.types.WalletV3Config;
import org.ton.java.smartcontract.utils.MsgUtils;
import org.ton.java.smartcontract.wallet.v3.WalletV3R2;
import org.ton.java.smartcontract.wallet.v5.WalletV5;
import org.ton.java.tlb.types.Message;
import org.ton.java.tonlib.types.VerbosityLevel;
import org.ton.java.utils.Utils;

@Slf4j
Expand Down Expand Up @@ -288,20 +291,23 @@ public void testSendMessageCustomContractOnTestnetTolk() {
.storeUint(0, 32)
.storeInt(Utils.getRandomInt(), 32)
.endCell())
.tvmEmulatorVerbosityLevel(TvmVerbosityLevel.WITH_ALL_STACK_VALUES)
.txEmulatorVerbosityLevel(TxVerbosityLevel.WITH_ALL_STACK_VALUES)
// .tonlibVerbosityLevel(VerbosityLevel.DEBUG)
.build();

assertThat(blockchain.deploy(30)).isTrue();
GetterResult result = blockchain.runGetMethod("unique");
System.out.printf("result %s\n", result);

blockchain.runGetMethod("unique");
System.out.printf("current seqno %s\n", blockchain.runGetSeqNo());

Cell bodyCell =
CellBuilder.beginCell()
.storeUint(0, 32) // seqno
.storeUint(1, 32) // seqno
.endCell();

Message extMsg = MsgUtils.createExternalMessage(dummyAddress, null, bodyCell);

blockchain.sendExternal(extMsg);
blockchain.sendExternal(bodyCell);
//wait till delivered
System.out.printf("current seqno %s\n", blockchain.runGetSeqNo());
}

Expand All @@ -313,24 +319,24 @@ public void testSendMessageCustomContractOnEmulatorTolk() {
.customContractAsResource("simple.tolk")
.customContractDataCell(
CellBuilder.beginCell()
.storeUint(0, 32)
.storeUint(1, 32)
.storeInt(Utils.getRandomInt(), 32)
.endCell())
// .tvmEmulatorVerbosityLevel(TvmVerbosityLevel.WITH_ALL_STACK_VALUES)
// .txEmulatorVerbosityLevel(TxVerbosityLevel.WITH_ALL_STACK_VALUES)
.build();

assertThat(blockchain.deploy(30)).isTrue();

blockchain.runGetMethod("unique");
System.out.printf("current seqno %s\n", blockchain.runGetSeqNo());

Cell bodyCell =
CellBuilder.beginCell()
.storeUint(0, 32) // seqno
.storeUint(1, 32) // seqno
.endCell();

Message extMsg = MsgUtils.createExternalMessage(dummyAddress, null, bodyCell);

blockchain.sendExternal(extMsg);
blockchain.sendExternal(bodyCell);
System.out.printf("current seqno %s\n", blockchain.runGetSeqNo());
}

Expand Down Expand Up @@ -358,8 +364,7 @@ public void testSendMessagesChainCustomContractOnEmulatorTolk() {
.storeUint(0, 32) // seqno
.endCell();

Message extMsg = MsgUtils.createExternalMessage(dummyAddress, null, bodyCell);
blockchain.sendExternal(extMsg);
blockchain.sendExternal(bodyCell);

currentSeqno = blockchain.runGetSeqNo();
System.out.printf("current seqno %s\n", currentSeqno);
Expand All @@ -369,8 +374,45 @@ public void testSendMessagesChainCustomContractOnEmulatorTolk() {
.storeUint(1, 32) // seqno
.endCell();

extMsg = MsgUtils.createExternalMessage(dummyAddress, null, bodyCell);
blockchain.sendExternal(extMsg);
blockchain.sendExternal(bodyCell);
currentSeqno = blockchain.runGetSeqNo();
System.out.printf("current seqno %s\n", currentSeqno);
}


@Test
public void testSendMessagesChainCustomContractOnEmulatorFunc() {
Blockchain blockchain =
Blockchain.builder()
.network(Network.EMULATOR)
.customContractAsResource("simple.fc")
.customContractDataCell(
CellBuilder.beginCell()
.storeUint(0, 32)
.storeInt(Utils.getRandomInt(), 32)
.endCell())
.build();
assertThat(blockchain.deploy(30)).isTrue();
blockchain.runGetMethod("unique");
BigInteger currentSeqno = blockchain.runGetSeqNo();
System.out.printf("current seqno %s\n", currentSeqno);

Cell bodyCell =
CellBuilder.beginCell()
.storeUint(0, 32) // seqno
.endCell();

blockchain.sendExternal(bodyCell);

currentSeqno = blockchain.runGetSeqNo();
System.out.printf("current seqno %s\n", currentSeqno);

bodyCell =
CellBuilder.beginCell()
.storeUint(1, 32) // seqno
.endCell();

blockchain.sendExternal(bodyCell);
currentSeqno = blockchain.runGetSeqNo();
System.out.printf("current seqno %s\n", currentSeqno);
}
Expand Down
5 changes: 5 additions & 0 deletions cell/src/main/java/org/ton/java/cell/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,11 @@ public byte[] getHash() {
return getHash(levelMask.getLevel());
}

public String getShortHash() {
String hashHex = Utils.bytesToHex(getHash(levelMask.getLevel()));
return hashHex.substring(0, 4) + ".." + hashHex.substring(hashHex.length() - 5, hashHex.length() - 1);
}

public byte[] getHash(int lvl) {
int hashIndex = levelMask.apply(lvl).getHashIndex();
if (type == CellType.PRUNED_BRANCH) {
Expand Down
5 changes: 0 additions & 5 deletions emulator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@
<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>
Loading

0 comments on commit 75bc146

Please sign in to comment.