Skip to content

Commit

Permalink
add Utils.calculateMethodId("method_name") to calculate methodId for …
Browse files Browse the repository at this point in the history
…run_method in Tonlib.
  • Loading branch information
neodiX committed Sep 7, 2024
1 parent 7385bce commit d121aae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void testTvmEmulatorEmulateRunMethod() {
@Test
public void testTvmEmulatorRunGetMethodGetSeqNo() {
String result = tvmEmulator.runGetMethod(
85143 // seqno
Utils.calculateMethodId("seqno")
);
log.info("result runGetMethod: {}", result);

Expand All @@ -189,7 +189,7 @@ public void testTvmEmulatorRunGetMethodGetSeqNoShortVersion() {
@Test
public void testTvmEmulatorRunGetMethodGetPubKey() {
String result = tvmEmulator.runGetMethod(
78748, // get_public_key
Utils.calculateMethodId("get_public_key"),
VmStack.builder()
.depth(0)
.stack(VmStackList.builder()
Expand Down Expand Up @@ -233,7 +233,7 @@ public void testTvmEmulatorRunGetMethodGetPluginList() {
.toCell().toBase64();

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

Expand All @@ -260,7 +260,7 @@ public void testTvmEmulatorRunGetMethodIsPluginInstalled() {
.toCell().toBase64();

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

Expand Down Expand Up @@ -454,7 +454,7 @@ public void testTvmEmulatorSendExternalMessageCustom() throws IOException {

// is_plugin_installed
String resultStr = tvmEmulator.runGetMethod(
76407, // CRC-16/XMODEM of is_plugin_installed(int wc, int addr_hash)
Utils.calculateMethodId("is_plugin_installed"),
stackSerialized);
log.info("result runGetMethod (is_plugin_installed): {}", resultStr); // should be no
GetMethodResult methodResult = gson.fromJson(resultStr, GetMethodResult.class);
Expand All @@ -480,7 +480,7 @@ public void testTvmEmulatorSendExternalMessageCustom() throws IOException {
.toCell().toBase64();

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

Expand Down Expand Up @@ -568,7 +568,7 @@ public void testTvmEmulatorSendExternalMessageCustom() throws IOException {
.toCell().toBase64();

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

Expand Down Expand Up @@ -601,7 +601,7 @@ public void testTvmEmulatorSendExternalMessageCustom() throws IOException {

log.info("is installed ????");
resultStr = tvmEmulator.runGetMethod(
76407, // CRC-16/XMODEM of is_plugin_installed(int wc, int addr_hash)
Utils.calculateMethodId("is_plugin_installed"),
stackSerialized);
log.info("result runGetMethod (is_plugin_installed) 2: {}", resultStr);
methodResult = gson.fromJson(resultStr, GetMethodResult.class);
Expand Down
4 changes: 2 additions & 2 deletions emulator/src/test/java/org/ton/java/emulator/TvmEmulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public String runGetMethod(int methodId) {
}

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

Expand All @@ -254,7 +254,7 @@ public BigInteger runGetSeqNo() {
}

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

Expand Down
14 changes: 11 additions & 3 deletions utils/src/main/java/org/ton/java/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ public static int getCRC16ChecksumAsInt(byte[] bytes) {
return crc;
}

public static int calculateMethodId(String methodName) {
int l = Utils.getCRC16ChecksumAsInt(methodName.getBytes());
l = (l & 0xffff) | 0x10000;
return l;
}

public static String getCRC16ChecksumAsHex(byte[] bytes) {
return bytesToHex(getCRC16ChecksumAsBytes(bytes));
}
Expand Down Expand Up @@ -821,7 +827,7 @@ public static String int2ip(long ip) {
((ip >> 16) & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." +
(ip & 0xFF);

}

public static int[] reverseIntArray(int[] in) {
Expand All @@ -830,7 +836,8 @@ public static int[] reverseIntArray(int[] in) {
int tmp = in[i];
in[i] = in[j];
in[j] = tmp;
i++; j--;
i++;
j--;
}
return in;
}
Expand All @@ -841,7 +848,8 @@ public static byte[] reverseByteArray(byte[] in) {
byte tmp = in[i];
in[i] = in[j];
in[j] = tmp;
i++; j--;
i++;
j--;
}
return in;
}
Expand Down

0 comments on commit d121aae

Please sign in to comment.