Skip to content

Commit 5900e02

Browse files
committed
make AccountStateActive serializable, improve getParticipants() in AdnlLiteClient, test getLibraries() in TonCenter
1 parent 0be88d1 commit 5900e02

File tree

8 files changed

+97
-19
lines changed

8 files changed

+97
-19
lines changed

adnl/src/main/java/org/ton/ton4j/adnl/AdnlLiteClient.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,15 +1995,48 @@ public SendMsgStatus sendMessage(Message externalMessage) {
19951995
}
19961996
}
19971997

1998+
public void flatValues(VmTuple tuple, List<VmStackValue> result) {
1999+
for (VmStackValue vmStackValue : tuple.getValues()) {
2000+
if (vmStackValue instanceof VmTuple) {
2001+
flatValues((VmTuple) vmStackValue, result);
2002+
}
2003+
else {
2004+
result.add(vmStackValue);
2005+
}
2006+
}
2007+
}
2008+
19982009
public List<Participant> getElectionParticipants() {
19992010
List<Participant> participants = new ArrayList<>();
20002011
RunMethodResult result = runMethod(ELECTION_ADDRESS, "participant_list");
20012012
VmStack vmStack = VmStack.deserialize(CellSlice.beginParse(Cell.fromBoc(result.result)));
2013+
2014+
List<VmStackValue> flatValues = new ArrayList<>();
2015+
// List<VmTuple> res = new ArrayList<>();
2016+
20022017
for (VmStackValue l : vmStack.getStack().getTos()) {
2003-
VmStackValueTuple tuple = VmStackValueTuple.deserialize(CellSlice.beginParse(l.toCell()));
2004-
BigInteger addr = tuple.getData().getIntByIndex(0);
2005-
BigInteger stake = tuple.getData().getIntByIndex(1);
2006-
participants.add(Participant.builder().address(addr).stake(stake).build());
2018+
if (l instanceof VmStackValueNull) {
2019+
continue;
2020+
}
2021+
if (l instanceof VmTuple) {
2022+
flatValues((VmTuple) l, flatValues);
2023+
}
2024+
}
2025+
BigInteger addr = null;
2026+
BigInteger stake = null;
2027+
for (VmStackValue genericValue : flatValues) {
2028+
2029+
if (genericValue instanceof VmStackValueInt) {
2030+
addr = ((VmStackValueInt) genericValue).getValue();
2031+
}
2032+
if (genericValue instanceof VmStackValueTinyInt) {
2033+
stake = ((VmStackValueTinyInt) genericValue).getValue();
2034+
}
2035+
if (nonNull(addr) && nonNull(stake)) {
2036+
participants.add(Participant.builder().address(addr).stake(stake).build());
2037+
addr = null;
2038+
stake = null;
2039+
}
20072040
}
20082041
return participants;
20092042
}

adnl/src/test/java/org/ton/ton4j/adnl/AdnlLiteClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,16 @@ void testRunSmcMethodParticipantsList() throws Exception {
973973
log.info("runMethodResult {}", runMethodResult);
974974
}
975975

976+
@Test
977+
void testRunSmcMethodParticipantsListMethod() {
978+
log.info("Testing runSmcMethod participant_list query");
979+
assertTrue(client.isConnected(), "Client should be connected");
980+
981+
List<Participant> participants = client.getElectionParticipants();
982+
983+
log.info("runMethodResult {}", participants);
984+
}
985+
976986
@Test
977987
void testRunSmcMethodSeqno() {
978988
log.info("Testing runSmcMethod seqno query");

tlb/src/main/java/org/ton/ton4j/tlb/Account.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.ton.ton4j.cell.CellBuilder;
1010
import org.ton.ton4j.cell.CellSlice;
1111

12+
import static java.util.Objects.isNull;
13+
1214
@Builder
1315
@Data
1416
public class Account implements Serializable {
@@ -45,10 +47,33 @@ public static Account deserialize(CellSlice cs) {
4547
}
4648

4749
public StateInit getStateInit() {
50+
if (isNull(accountStorage)) {
51+
return null;
52+
}
4853
return ((AccountStateActive) accountStorage.getAccountState()).getStateInit();
4954
}
5055

5156
public BigInteger getBalance() {
57+
if (isNull(accountStorage)) {
58+
return null;
59+
}
5260
return accountStorage.getBalance().getCoins();
5361
}
62+
63+
public String getAccountState() {
64+
if (isNull(accountStorage) || isNull(accountStorage.getAccountState())) {
65+
return "uninitialized";
66+
}
67+
if (accountStorage.getAccountState() instanceof AccountStateActive) {
68+
return "active";
69+
}
70+
if (accountStorage.getAccountState() instanceof AccountStateFrozen) {
71+
return "frozen";
72+
}
73+
if (accountStorage.getAccountState() instanceof AccountStateUninit) {
74+
return "uninitialized";
75+
} else {
76+
return "unknown";
77+
}
78+
}
5479
}

tlb/src/main/java/org/ton/ton4j/tlb/AccountStateActive.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import org.ton.ton4j.cell.CellBuilder;
77
import org.ton.ton4j.cell.CellSlice;
88

9+
import java.io.Serializable;
10+
911
@Builder
1012
@Data
11-
public class AccountStateActive implements AccountState {
13+
public class AccountStateActive implements AccountState, Serializable {
1214
int magic;
1315
StateInit stateInit;
1416

tlb/src/main/java/org/ton/ton4j/tlb/MsgAddress.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.ton.ton4j.cell.Cell;
44
import org.ton.ton4j.cell.CellSlice;
55

6+
import java.io.Serializable;
7+
68
/**
79
*
810
*
@@ -23,7 +25,7 @@
2325
* _ _:MsgAddressExt = MsgAddress;
2426
* }</pre>
2527
*/
26-
public interface MsgAddress {
28+
public interface MsgAddress extends Serializable {
2729
Cell toCell();
2830

2931
static MsgAddress deserialize(CellSlice cs) {

tlb/src/main/java/org/ton/ton4j/tlb/ShardDescr.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ private String getMagic() {
8383
return Long.toHexString(magic);
8484
}
8585

86-
private String getRootHash() {
86+
public String getRootHash() {
8787
return rootHash.toString(16);
8888
}
8989

90-
private String getFileHash() {
90+
public String getFileHash() {
9191
return fileHash.toString(16);
9292
}
9393

94-
private String getNextValidatorShard() {
94+
public String getNextValidatorShard() {
9595
return nextValidatorShard.toString(16);
9696
}
9797

tlb/src/main/java/org/ton/ton4j/tlb/StorageExtraInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.ton.ton4j.cell.Cell;
44
import org.ton.ton4j.cell.CellSlice;
55

6-
public interface StorageExtraInfo {
6+
import java.io.Serializable;
7+
8+
public interface StorageExtraInfo extends Serializable {
79

810
Cell toCell();
911

toncenter/src/test/java/org/ton/ton4j/toncenter/TonCenterTest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.ton.ton4j.toncenter;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.commons.codec.DecoderException;
45
import org.junit.Test;
56
import org.junit.runner.RunWith;
67
import org.junit.runners.Parameterized;
78
import org.ton.ton4j.cell.Cell;
89
import org.ton.ton4j.cell.CellBuilder;
910
import org.ton.ton4j.toncenter.model.*;
11+
import org.ton.ton4j.utils.Utils;
12+
1013
import static org.ton.ton4j.toncenter.model.CommonResponses.*;
1114

1215
import java.math.BigInteger;
@@ -563,21 +566,22 @@ public void testGetConfigAll() {
563566
}
564567

565568
@Test
566-
public void testGetLibraries() {
569+
public void testGetLibraries() throws DecoderException {
570+
List<String> libraryHashes;
567571
if (network == Network.MAINNET) {
568-
log.info("Not yet implemented in MAINNET");
569-
return;
572+
libraryHashes = Arrays.asList(
573+
Utils.hexStringToBase64("459BAABBCDD21981E937B0FFE73F9D52837F84ED3C117AD1260D535EC162800A")
574+
);
575+
}
576+
else {
577+
libraryHashes = Arrays.asList(
578+
"IINLe3KxEhR+Gy+0V7hOdNGjDwT3N9T2KmaOlVLSty8="
579+
);
570580
}
571581
enforceRateLimit();
572582
TonCenter client = TonCenter.builder().apiKey(apiKey).network(network).build();
573583

574584
try {
575-
576-
// Test with some example library hashes (these may not exist, but tests the endpoint structure)
577-
List<String> libraryHashes = Arrays.asList(
578-
"IINLe3KxEhR+Gy+0V7hOdNGjDwT3N9T2KmaOlVLSty8=" // in testnet only
579-
// "20834b7b72b112147e1b2fb457b84e74d1a30f04f737d4f62a668e9552d2b72f" // in testnet only
580-
);
581585

582586
TonResponse<GetLibrariesResponse> response = client.getLibraries(libraryHashes);
583587
log.info("response {}", response.getResult());

0 commit comments

Comments
 (0)