Skip to content

Commit

Permalink
fix deserialization of MsgAddressInt
Browse files Browse the repository at this point in the history
  • Loading branch information
neodiX committed Jun 11, 2024
1 parent 21172cd commit a06a71e
Showing 10 changed files with 187 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -6,12 +6,15 @@

/**
* int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
* src:MsgAddress dest:MsgAddressInt
* value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
* created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
* src:MsgAddress
* dest:MsgAddressInt
* value:CurrencyCollection
* ihr_fee:Grams
* fwd_fee:Grams
* created_lt:uint64
* created_at:uint32 = CommonMsgInfoRelaxed;
* <p>
* ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt
* created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
* ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
*/


1 change: 1 addition & 0 deletions cell/src/main/java/org/ton/java/tlb/types/MsgAddress.java
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ static MsgAddress deserialize(CellSlice cs) {
int magic = cs.preloadUint(2).intValue();
switch (magic) {
case 0b00: {
cs.loadInt(2);
return MsgAddressExtNone.builder().build();
}
case 0b01: {
6 changes: 1 addition & 5 deletions cell/src/main/java/org/ton/java/tlb/types/MsgAddressInt.java
Original file line number Diff line number Diff line change
@@ -8,11 +8,7 @@
/**
* addr_std$10 anycast:(Maybe Anycast) workchain_id:int8 address:bits256 = MsgAddressInt;
* <p>
* addr_var$11
* anycast:(Maybe Anycast)
* addr_len:(## 9)
* workchain_id:int32
* address:(bits addr_len) = MsgAddressInt;
* addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9) workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
*/
public interface MsgAddressInt extends MsgAddress {
Cell toCell();
12 changes: 12 additions & 0 deletions cell/src/main/java/org/ton/java/tlb/types/VmStackList.java
Original file line number Diff line number Diff line change
@@ -22,6 +22,12 @@
public class VmStackList {
List<VmStackValue> tos;

/**
* Also knows as serializeTuple() in ton-core web.
*
* @return Cell
*/

public Cell toCell() {
Cell list = CellBuilder.beginCell().endCell();
int i = 0;
@@ -35,6 +41,12 @@ public Cell toCell() {
return list;
}

/**
* Also knows as parseTuple() in ton-core web.
*
* @param cs CellSlice
* @return VmStackList
*/
public static VmStackList deserialize(CellSlice cs) {
List<VmStackValue> tos = new ArrayList<>();
while (cs.getRefsCount() != 0) {
26 changes: 25 additions & 1 deletion cell/src/main/java/org/ton/java/tlb/types/VmStackValue.java
Original file line number Diff line number Diff line change
@@ -3,21 +3,45 @@
import org.ton.java.cell.Cell;
import org.ton.java.cell.CellSlice;

/**
* vm_stk_null#00 = VmStackValue;
* vm_stk_tinyint#01 value:int64 = VmStackValue;
* vm_stk_int#0201_ value:int257 = VmStackValue;
* vm_stk_nan#02ff = VmStackValue;
* vm_stk_cell#03 cell:^Cell = VmStackValue;
* cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits } st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
* vm_stk_slice#04 _:VmCellSlice = VmStackValue;
* vm_stk_builder#05 cell:^Cell = VmStackValue;
* vm_stk_cont#06 cont:VmCont = VmStackValue;
* vm_tupref_nil$_ = VmTupleRef 0;
* vm_tupref_single$_ entry:^VmStackValue = VmTupleRef 1;
* vm_tupref_any$_ {n:#} ref:^(VmTuple (n + 2)) = VmTupleRef (n + 2);
* vm_tuple_nil$_ = VmTuple 0;
* vm_tuple_tcons$_ {n:#} head:(VmTupleRef n) tail:^VmStackValue = VmTuple (n + 1);
* vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
*/
public interface VmStackValue {

Cell toCell();

static VmStackValue deserialize(CellSlice cs) {

if (cs.isSliceEmpty()) {
return null;
}
CellSlice c = cs.clone();

int magic = c.preloadUint(8).intValue();
int magic2 = c.skipBits(8).preloadUint(8).intValue();

if (magic == 0x00) {
return VmStackValueNull.deserialize(cs);
} else if (magic == 0x01) {
return VmStackValueTinyInt.deserialize(cs);
} else if (magic == 0x02) {
//int magic2 = c.skipBits(8).preloadUint(8).intValue();
return VmStackValueInt.deserialize(cs);


// if (magic2 == 0x01) {
// return VmStackValueInt.deserialize(cs);
// } else if (magic2 == 0xff) {
2 changes: 1 addition & 1 deletion cell/src/main/java/org/ton/java/tlb/types/VmTuple.java
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public Cell toCell() {
public static VmTuple deserialize(CellSlice cs) {
return VmTuple.builder()
.head(VmTupleRef.deserialize(cs))
.tail(VmStackValue.deserialize(CellSlice.beginParse(cs.loadRef())))
.tail(cs.getRefsCount() > 0 ? VmStackValue.deserialize(CellSlice.beginParse(cs.loadRef())) : null)
.build();
}
}
18 changes: 13 additions & 5 deletions cell/src/main/java/org/ton/java/tlb/types/VmTupleRef.java
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
@Getter
@Setter
@ToString
public class VmTupleRef { // todo VmTupleRef
public class VmTupleRef {
VmStackValue entry;

public Cell toCell() {
@@ -26,9 +26,17 @@ public Cell toCell() {
.endCell();
}

public static VmTupleRef deserialize(CellSlice cs) {
return VmTupleRef.builder()
.entry(VmStackValue.deserialize(CellSlice.beginParse(cs.loadRef())))
.build();
public static VmTupleRef deserialize(CellSlice cs) { // more tests are required
if (cs.getRefsCount() == 0) {
return null;
} else if (cs.getRefsCount() == 1) {
return VmTupleRef.builder()
.entry(VmStackValue.deserialize(cs))
.build();
} else {
return VmTupleRef.builder()
.entry(VmStackValue.deserialize(CellSlice.beginParse(cs.loadRef())))
.build();
}
}
}
30 changes: 30 additions & 0 deletions emulator/src/main/java/org/ton/java/emulator/GetMethodParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.ton.java.emulator;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.ton.java.address.Address;
import org.ton.java.cell.Cell;

import java.io.Serializable;
import java.math.BigInteger;

@Builder
@Setter
@Getter
@ToString
public class GetMethodParams implements Serializable {
Cell code;
Cell data;
TvmVerbosityLevel verbosityLevel;
Cell libs;
Address address;
long unixTime;
BigInteger balance;
String randSeed;
long gasLimit;
long methodId;
boolean debugEnabled;
}

96 changes: 93 additions & 3 deletions emulator/src/test/java/org/ton/java/emulator/TestTvmEmulator.java
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import java.io.IOException;
import java.math.BigInteger;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ExecutionException;

@@ -140,7 +141,11 @@ public void testTvmEmulatorEmulateRunMethod() {
.storeUint(85143, 32) // method-id - seqno
.endCell()
.toBase64();
String result = tvmEmulator.emulateRunMethod(1, paramsBoc, Utils.toNano(1).longValue());


Cell c = CellBuilder.beginCell().fromBocBase64(paramsBoc).endCell();
log.info("c {}", c);
String result = tvmEmulator.emulateRunMethod(paramsBoc.length(), paramsBoc, Utils.toNano(1).longValue());
log.info("result emulateRunMethod: {}", result); // todo - return null
}

@@ -199,6 +204,58 @@ public void testTvmEmulatorRunGetMethodGetPubKey() {
log.info("vmStackList value: {}", pubKey.toString(16)); // pubkey
}

@Test
public void testTvmEmulatorRunGetMethodGetPluginList() {
String stackSerialized = VmStack.builder()
.depth(0)
.stack(VmStackList.builder()
.tos(Lists.emptyList())
.build())
.build()
.toCell().toBase64();

String result = tvmEmulator.runGetMethod(
107653, // CRC-16/XMODEM of get_plugin_list
stackSerialized);
log.info("result runGetMethod: {}", result);

GetMethodResult methodResult = gson.fromJson(result, GetMethodResult.class);
log.info("methodResult: {}", methodResult);
log.info("methodResult stack: {}", methodResult.getStack());

Cell cellResult = CellBuilder.beginCell().fromBocBase64(methodResult.getStack()).endCell();
log.info("cellResult {}", cellResult);
VmStackValueTuple tuple = VmStackValueTuple.deserialize(CellSlice.beginParse(cellResult));
log.info("tuple {}", tuple);
}

@Test
public void testTvmEmulatorRunGetMethodIsPluginInstalled() {
String stackSerialized = VmStack.builder()
.depth(0)
.stack(VmStackList.builder()
.tos(Arrays.asList(
VmStackValueTinyInt.builder().value(BigInteger.ZERO).build(),
VmStackValueTinyInt.builder().value(BigInteger.ZERO).build()))
.build())
.build()
.toCell().toBase64();

String result = tvmEmulator.runGetMethod(
76407, // CRC-16/XMODEM of is_plugin_installed(int wc, int addr_hash)
stackSerialized);
log.info("result runGetMethod: {}", result);

GetMethodResult methodResult = gson.fromJson(result, GetMethodResult.class);
log.info("methodResult: {}", methodResult);
log.info("methodResult stack: {}", methodResult.getStack());

Cell cellResult = CellBuilder.beginCell().fromBocBase64(methodResult.getStack()).endCell();
log.info("cellResult {}", cellResult);
VmStackValueTuple tuple = VmStackValueTuple.deserialize(CellSlice.beginParse(cellResult));
log.info("tuple {}", tuple);
}

@Test
public void testTvmEmulatorRunGetMethodGetPubKeyShortVersion() {
log.info("contract's pubKey: {}", tvmEmulator.runGetPublicKey()); // pubkey
@@ -235,7 +292,12 @@ public void testTvmEmulatorSendExternalMessage() {

SendExternalMessageResult result = gson.fromJson(resultBoc, SendExternalMessageResult.class);
log.info("result sendExternalMessage, exitCode: {}", result.getVm_exit_code());
log.info("result sendExternalMessage, actions: {}", result.getActions());
log.info("seqno value: {}", tvmEmulator.runGetSeqNo());
OutList actions = OutList.deserialize(
CellSlice.beginParse(
CellBuilder.beginCell().fromBocBase64(result.getActions()).endCell()));
log.info("parsed actions {}", actions);
}

@Test
@@ -333,6 +395,25 @@ public void testTvmEmulatorSendExternalMessageCustom() throws IOException, Execu

result = gson.fromJson(resultBoc, SendExternalMessageResult.class);
log.info("result sendExternalMessage, exitCode: {}", result.getVm_exit_code());


// still to review
String stackSerialized = VmStack.builder()
.depth(2)
.stack(VmStackList.builder()
.tos(Arrays.asList(
VmStackValueTinyInt.builder().value(BigInteger.ZERO).build(),
VmStackValueTinyInt.builder().value(BigInteger.TEN).build()))
.build())
.build()
.toCell()
.toBase64();

// is_plugin_installed
String resultStr = tvmEmulator.runGetMethod(
76407, // CRC-16/XMODEM of is_plugin_installed(int wc, int addr_hash)
stackSerialized);
log.info("result runGetMethod (is_plugin_installed): {}", resultStr);
}

@Test
@@ -390,7 +471,6 @@ public void testTvmEmulatorSendInternalMessageCustomContract() throws IOExceptio
log.info("resultBoc {}", resultBoc);
SendExternalMessageResult result = gson.fromJson(resultBoc, SendExternalMessageResult.class);
log.info("result sendInternalMessage, exitCode: {}", result.getVm_exit_code());

}

@Test
@@ -410,7 +490,17 @@ public void testTvmEmulatorSendInternalMessage() {

@Test
public void testTvmEmulatorSetPrevBlockInfo() {
// assertTrue(tvmEmulator.setPrevBlockInfo());
String infoBocBase64 = "";
// must be tuple, below won't work
// BlkPrevInfo blkPrevInfo = BlkPrevInfo.builder()
// .prev1(ExtBlkRef.builder()
// .seqno(10)
// .endLt(BigInteger.valueOf(500000000))
// .fileHash(BigInteger.ONE)
// .rootHash(BigInteger.TEN)
// .build())
// .build();
assertTrue(tvmEmulator.setPrevBlockInfo(infoBocBase64));
}


Original file line number Diff line number Diff line change
@@ -129,7 +129,9 @@ public boolean setLibs(String libsBoc) {
}

/**
* C7 tlb-scheme:
* Prepares the c7 tuple (virtual machine context) for a compute phase of a transaction.
* <p>
* C7 tlb-scheme FYI:
* <p>
* smc_info#076ef1ea
* actions:uint16

0 comments on commit a06a71e

Please sign in to comment.