Skip to content

Commit

Permalink
tested sendInternalMessage and sendExternalMessage; next to test emul…
Browse files Browse the repository at this point in the history
…ateRunMethod - WIP
  • Loading branch information
neodiX committed Jun 10, 2024
1 parent 6fff5b4 commit 21172cd
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 48 deletions.
6 changes: 6 additions & 0 deletions emulator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
<version>0.4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.neodix42</groupId>
<artifactId>cell</artifactId>
<version>0.4.3</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.ton.java.emulator;

import java.io.Serializable;

public enum TvmVerbosityLevel implements Serializable {
TRUNCATED, UNLIMITED, WITH_CELL_HASH_AND_OFFSET, WITH_ALL_STACK_VALUES
}
225 changes: 194 additions & 31 deletions emulator/src/test/java/org/ton/java/emulator/TestTvmEmulator.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package org.ton.java.emulator;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import com.sun.jna.Native;
import lombok.Builder;
import lombok.extern.java.Log;
import org.assertj.core.util.Lists;
import org.ton.java.cell.Cell;
import org.ton.java.cell.CellBuilder;
import org.ton.java.cell.CellSlice;
import org.ton.java.tlb.types.VmStack;
import org.ton.java.tlb.types.VmStackList;
import org.ton.java.tlb.types.VmStackValueInt;
import org.ton.java.tlb.types.VmStackValueTinyInt;
import org.ton.java.utils.Utils;

import java.math.BigInteger;

import static java.util.Objects.isNull;

@Log
Expand Down Expand Up @@ -33,7 +46,7 @@ public class TvmEmulator {

private String codeBoc;
private String dataBoc;
private Integer verbosityLevel;
private TvmVerbosityLevel verbosityLevel;

public static class TvmEmulatorBuilder {
}
Expand Down Expand Up @@ -78,15 +91,15 @@ public TvmEmulator build() {

super.tvmEmulatorI = Native.load(super.pathToEmulatorSharedLib, TvmEmulatorI.class);
if (isNull(super.verbosityLevel)) {
super.verbosityLevel = 3;
super.verbosityLevel = TvmVerbosityLevel.WITH_ALL_STACK_VALUES;
}
if (isNull(super.codeBoc)) {
throw new Error("codeBoc is not set");
}
if (isNull(super.dataBoc)) {
throw new Error("dataBoc is not set");
}
super.tvmEmulator = super.tvmEmulatorI.tvm_emulator_create(super.codeBoc, super.dataBoc, super.verbosityLevel);
super.tvmEmulator = super.tvmEmulatorI.tvm_emulator_create(super.codeBoc, super.dataBoc, super.verbosityLevel.ordinal());

if (super.tvmEmulator == 0) {
throw new Error("Can't create emulator instance");
Expand Down Expand Up @@ -197,12 +210,65 @@ public String runGetMethod(int methodId, String stackBoc) {
return tvmEmulatorI.tvm_emulator_run_get_method(tvmEmulator, methodId, stackBoc);
}

/**
* Run get method with empty input stack
*
* @param methodId Integer method id
* @return Json object with error:
* {
* "success": false,
* "error": "Error description"
* }
* Or success:
* {
* "success": true
* "vm_log": "...",
* "vm_exit_code": 0,
* "stack": "Base64 encoded BoC serialized stack (VmStack)",
* "missing_library": null,
* "gas_used": 1212
* }
*/
public String runGetMethod(int methodId) {
return tvmEmulatorI.tvm_emulator_run_get_method(tvmEmulator, methodId,
VmStack.builder()
.depth(0)
.stack(VmStackList.builder()
.tos(Lists.emptyList())
.build())
.build()
.toCell().toBase64());
}

public BigInteger runGetSeqNo() {
String seqNoResult = runGetMethod(85143);
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.BIG_DECIMAL).create();
GetMethodResult methodResult = gson.fromJson(seqNoResult, GetMethodResult.class);

Cell cellResult = CellBuilder.beginCell().fromBocBase64(methodResult.getStack()).endCell();
VmStack stack = VmStack.deserialize(CellSlice.beginParse(cellResult));
VmStackList vmStackList = stack.getStack();
return VmStackValueTinyInt.deserialize(CellSlice.beginParse(vmStackList.getTos().get(0).toCell())).getValue();
}

public String runGetPublicKey() {
String pubKeyResult = runGetMethod(78748);
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.BIG_DECIMAL).create();
GetMethodResult methodResult = gson.fromJson(pubKeyResult, GetMethodResult.class);

Cell cellResult = CellBuilder.beginCell().fromBocBase64(methodResult.getStack()).endCell();
VmStack stack = VmStack.deserialize(CellSlice.beginParse(cellResult));
int depth = stack.getDepth();
VmStackList vmStackList = stack.getStack();
BigInteger pubKey = VmStackValueInt.deserialize(CellSlice.beginParse(vmStackList.getTos().get(0).toCell())).getValue();
return pubKey.toString(16);
}

/**
* Optimized version of "run get method" with all passed parameters in a single call
*
* @param len Length of params_boc buffer
* @param paramsBoc BoC serialized parameters, scheme:
* request$_
* code:^Cell data:^Cell stack:^VmStack params:^[c7:^VmStack libs:^Cell]
* method_id:(## 32)
* @param gasLimit Gas limit
Expand Down Expand Up @@ -265,7 +331,6 @@ public String sendExternalMessage(String messageBodyBoc) {
public String sendInternalMessage(String messageBodyBoc, long amount) {
return tvmEmulatorI.tvm_emulator_send_internal_message(tvmEmulator, messageBodyBoc, amount);
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;
import org.ton.java.smartcontract.wallet.v4.SubscriptionInfo;
import org.ton.java.tlb.types.StateInit;

import java.math.BigInteger;

Expand All @@ -20,9 +21,12 @@ public class WalletV4R2Config implements WalletConfig {
int mode;
long validUntil;
long createdAt;
boolean bounce;
Address destination;
BigInteger amount;
Cell body;
StateInit stateInit;
String comment;
int operation; // 0 - simple send; 1 - deploy and install plugin; 2 - install plugin; 3 - remove plugin
SubscriptionInfo subscriptionInfo;
NewPlugin newPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.ton.java.smartcontract.types.WalletV4R2Config;
import org.ton.java.smartcontract.utils.MsgUtils;
import org.ton.java.smartcontract.wallet.Contract;
import org.ton.java.tlb.types.ExternalMessageInfo;
import org.ton.java.tlb.types.Message;
import org.ton.java.tlb.types.StateInit;
import org.ton.java.tlb.types.*;
import org.ton.java.tonlib.Tonlib;
import org.ton.java.tonlib.types.*;
import org.ton.java.utils.Utils;
Expand All @@ -25,6 +23,7 @@
import java.util.List;

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

@Builder
@Getter
Expand Down Expand Up @@ -106,8 +105,27 @@ public Cell createTransferBody(WalletV4R2Config config) {
message.storeUint(config.getSeqno(), 32);// msg_seqno

if (config.getOperation() == 0) {
Cell order = Message.builder()
.info(InternalMessageInfo.builder()
.bounce(config.isBounce())
.dstAddr(MsgAddressIntStd.builder()
.workchainId(config.getDestination().wc)
.address(config.getDestination().toBigInteger())
.build())
.value(CurrencyCollection.builder().coins(config.getAmount()).build())
.build())
.init(config.getStateInit())
.body((isNull(config.getBody()) && nonNull(config.getComment())) ?
CellBuilder.beginCell()
.storeUint(0, 32)
.storeString(config.getComment())
.endCell()
: config.getBody())
.build().toCell();

message.storeUint(BigInteger.ZERO, 8); // op simple send
//message.storeRef(body); ??
message.storeUint(config.getMode(), 8);
message.storeRef(order);
} else if (config.getOperation() == 1) {
message.storeUint(1, 8); // deploy and install plugin
message.storeUint(BigInteger.valueOf(config.getNewPlugin().getPluginWc()), 8);
Expand Down Expand Up @@ -139,7 +157,7 @@ public Cell createTransferBody(WalletV4R2Config config) {
* Deploy wallet without any plugins.
* One can also deploy plugin separately and later install into the wallet. See installPlugin().
*/

public ExtMessageInfo deploy() {
return tonlib.sendRawMessage(prepareDeployMsg().toCell().toBase64());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void testNft() {

//sends from adminWallet to nftItem request for static data, response comes to adminWallet
//https://github.com/ton-blockchain/token-contract/blob/main/nft/nft-item.fc#L131
getStaticData(adminWallet, Utils.toNano(0.088), nftItem1Address, BigInteger.valueOf(661), adminWallet.getKeyPair());
getStaticData(adminWallet, Utils.toNano(0.088), nftItem1Address, BigInteger.valueOf(661));

// transfer nft item to nft sale smart-contract (send amount > full_price+1ton)
transferNftItem(adminWallet, Utils.toNano(1.4), nftItem1Address, BigInteger.ZERO, nftSale1.getAddress(),
Expand Down Expand Up @@ -250,8 +250,7 @@ public void testNft() {
"ton://my-nft/collection.json", "ton://my-nft/", 0.16,
Address.of(WALLET2_ADDRESS), adminWallet.getKeyPair());

changeNftCollectionOwner(adminWallet, Utils.toNano(0.06), nftCollection.getAddress(),
Address.of(WALLET2_ADDRESS), adminWallet.getKeyPair());
changeNftCollectionOwner(adminWallet, Utils.toNano(0.06), nftCollection.getAddress(), Address.of(WALLET2_ADDRESS));

getRoyaltyParams(adminWallet, Utils.toNano(0.0777), nftCollection.getAddress());
}
Expand All @@ -275,7 +274,7 @@ private long getNftCollectionInfo(NftCollection nftCollection) {
}


public void changeNftCollectionOwner(WalletV3R1 wallet, BigInteger msgValue, Address nftCollectionAddress, Address newOwner, TweetNaclFast.Signature.KeyPair keyPair) {
public void changeNftCollectionOwner(WalletV3R1 wallet, BigInteger msgValue, Address nftCollectionAddress, Address newOwner) {

WalletV3Config walletV3Config = WalletV3Config.builder()
.walletId(42)
Expand Down Expand Up @@ -340,7 +339,7 @@ private void transferNftItem(WalletV3R1 wallet, BigInteger msgValue, Address nft
assertThat(extMessageInfo.getError().getCode()).isZero();
}

private void getStaticData(WalletV3R1 wallet, BigInteger msgValue, Address nftItemAddress, BigInteger queryId, TweetNaclFast.Signature.KeyPair keyPair) {
private void getStaticData(WalletV3R1 wallet, BigInteger msgValue, Address nftItemAddress, BigInteger queryId) {
WalletV3Config config = WalletV3Config.builder()
.walletId(42)
.seqno(wallet.getSeqno())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void testSimpleSend() throws InterruptedException {
log.info("pub-key {}", Utils.bytesToHex(contract.getKeyPair().getPublicKey()));
log.info("prv-key {}", Utils.bytesToHex(contract.getKeyPair().getSecretKey()));

BigInteger balance = TestFaucet.topUpContract(tonlib, Address.of(nonBounceableAddress), Utils.toNano(7));
BigInteger balance = TestFaucet.topUpContract(tonlib, Address.of(nonBounceableAddress), Utils.toNano(1));
log.info("new wallet {} balance: {}", contract.getName(), Utils.formatNanoValue(balance));

// deploy wallet-v4
Expand All @@ -245,7 +245,10 @@ public void testSimpleSend() throws InterruptedException {
.walletId(contract.getWalletId())
.seqno(contract.getSeqno())
.destination(Address.of(FAUCET_ADDRESS_RAW))
.amount(Utils.toNano(0.331)).build();
.mode(3)
.amount(Utils.toNano(0.331))
.comment("ton4j-v4r2-simple-send")
.build();

contract.send(config);
}
Expand Down

0 comments on commit 21172cd

Please sign in to comment.