Skip to content

Commit

Permalink
Show wallet seqno
Browse files Browse the repository at this point in the history
  • Loading branch information
neodix42 committed Dec 28, 2021
1 parent 8b6614b commit 62b331f
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 85 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msv

## Installation and usage

Download MyLocalTon.jar, open console, go to the location where you have placed the executable and execute the following command.
Download MyLocalTon.jar from https://github.com/neodiX42/MyLocalTon/actions. Click the latest successful build and find MyLocalTon under Artifacts section. Open console, go to location where you have
just placed the executable and execute the following command.

`java -jar MyLocalTon.jar`

Expand Down
76 changes: 49 additions & 27 deletions src/main/java/org/ton/actions/MyLocalTon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.util.Strings;
import org.ton.db.DB;
import org.ton.db.entities.BlockEntity;
import org.ton.db.entities.TxEntity;
import org.ton.db.entities.WalletEntity;
Expand Down Expand Up @@ -806,37 +805,36 @@ public void createPreInstalledWallets(Node genesisNode) throws Exception {
log.info("preinstalled wallet {}", wallet.getFullAddress());

// always update account state on start
AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(genesisNode, wallet.getWc() + ":" + wallet.getHexAddress()));
//AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(genesisNode, wallet.getWc() + ":" + wallet.getHexAddress()));
Pair<AccountState, Long> stateAndSeqno = getAccountStateAndSeqno(genesisNode, wallet.getWc() + ":" + wallet.getHexAddress());
App.dbPool.updateWalletStateAndSeqno(wallet, stateAndSeqno.getLeft(), stateAndSeqno.getRight());

App.dbPool.updateWalletState(wallet, accountState);

wallet.setAccountState(accountState);
wallet.setAccountState(stateAndSeqno.getLeft());
wallet.setSeqno(stateAndSeqno.getRight());
updateAccountsTabGui(wallet);
}
}

public WalletEntity createWalletEntity(Node genesisNode, String fileBaseName, long workchain, long subWalletid) throws Exception {

WalletEntity walletEntity;
WalletEntity wallet;

if (isNull(fileBaseName)) { //generate and read address of just generated wallet
walletEntity = createWalletWithFundsAndSmartContract(genesisNode, genesisNode, workchain, subWalletid, settings.getWalletSettings().getInitialAmount());
log.debug("create wallet address {}", walletEntity.getHexAddress());
wallet = createWalletWithFundsAndSmartContract(genesisNode, genesisNode, workchain, subWalletid, settings.getWalletSettings().getInitialAmount());
log.debug("create wallet address {}", wallet.getHexAddress());
} else { //read address of initially created wallet (main-wallet and config-master)
walletEntity = new FiftExecutor().getWalletByBasename(genesisNode, fileBaseName);
log.info("read wallet address: {}", walletEntity.getHexAddress());
wallet = new FiftExecutor().getWalletByBasename(genesisNode, fileBaseName);
log.info("read wallet address: {}", wallet.getHexAddress());
}

AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(genesisNode, walletEntity.getWc() + ":" + walletEntity.getHexAddress()));
log.debug("manually added wallet wc:addr {}:{}, balance {}, state {}", accountState.getWc(), accountState.getAddress(), accountState.getBalance().getToncoins(), accountState.getStatus());

//update account state
App.dbPool.updateWalletState(walletEntity, accountState);
Pair<AccountState, Long> stateAndSeqno = getAccountStateAndSeqno(genesisNode, wallet.getWc() + ":" + wallet.getHexAddress());
App.dbPool.updateWalletStateAndSeqno(wallet, stateAndSeqno.getLeft(), stateAndSeqno.getRight());

walletEntity.setAccountState(accountState);
updateAccountsTabGui(walletEntity);
wallet.setAccountState(stateAndSeqno.getLeft());
wallet.setSeqno(stateAndSeqno.getRight());
updateAccountsTabGui(wallet);

return walletEntity;
return wallet;
}

public void runBlockchainSizeMonitor() {
Expand Down Expand Up @@ -1073,6 +1071,14 @@ public void populateAccountRowWithData(WalletEntity walletEntity, javafx.scene.N
((Label) accountRow.lookup("#walledId")).setText("-1");
}

if (walletEntity.getSeqno() > 0) {
((Label) accountRow.lookup("#seqno")).setVisible(true);
((Label) accountRow.lookup("#seqno")).setText("Seqno " + walletEntity.getSeqno());
} else {
((Label) accountRow.lookup("#seqno")).setVisible(false);
((Label) accountRow.lookup("#seqno")).setText("Seqno -1");
}

Value value = walletEntity.getAccountState().getBalance();
BigDecimal balance = isNull(value) ? BigDecimal.ZERO : value.getToncoins();
String formattedBalance = String.format("%,.8f", balance.divide(BigDecimal.valueOf(1000000000L), 9, RoundingMode.CEILING));
Expand Down Expand Up @@ -1397,12 +1403,13 @@ public List<TxEntity> extractTxsAndMsgs(ResultLastBlock lastBlock, ResultListBlo

public WalletEntity insertNewAccountEntity(ResultLastBlock lastBlock, Transaction txDetails) {

AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(settings.getGenesisNode(), lastBlock.getWc() + ":" + txDetails.getAccountAddr()));
log.debug("insertAccountEntity, wallet {}:{}, balance {}, state {}", accountState.getWc(), accountState.getAddress(), accountState.getBalance(), accountState.getStatus());
//AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(settings.getGenesisNode(), lastBlock.getWc() + ":" + txDetails.getAccountAddr()));
Pair<AccountState, Long> stateAndSeqno = getAccountStateAndSeqno(settings.getGenesisNode(), lastBlock.getWc() + ":" + txDetails.getAccountAddr());
log.debug("insertAccountEntity, wallet {}:{}, balance {}, state {}", stateAndSeqno.getLeft().getWc(), stateAndSeqno.getLeft().getAddress(), stateAndSeqno.getLeft().getBalance(), stateAndSeqno.getLeft().getStatus());

Pair<WalletVersion, Long> walletVersionAndId = Utils.detectWalledVersionAndId(accountState);
Pair<WalletVersion, Long> walletVersionAndId = Utils.detectWalledVersionAndId(stateAndSeqno.getLeft());

WalletEntity foundWallet = DB.findWallet(WalletPk.builder()
WalletEntity foundWallet = App.dbPool.findWallet(WalletPk.builder()
.wc(lastBlock.getWc())
.hexAddress(txDetails.getAccountAddr())
.build());
Expand All @@ -1428,17 +1435,18 @@ public WalletEntity insertNewAccountEntity(ResultLastBlock lastBlock, Transactio
.subWalletId(walletAddress.getSubWalletId())
.walletVersion(walletVersionAndId.getLeft())
.wallet(walletAddress)
.accountState(accountState)
.accountState(stateAndSeqno.getLeft())
.createdAt(txDetails.getNow())
.build();

App.dbPool.insertWallet(walletEntity);

return walletEntity;
} else {
foundWallet.setAccountState(accountState);
foundWallet.setAccountState(stateAndSeqno.getLeft());
foundWallet.setSeqno(stateAndSeqno.getRight());
log.debug("Wallet found! Update state {}", foundWallet.getFullAddress()); // update state
App.dbPool.updateWalletState(foundWallet, accountState);
App.dbPool.updateWalletStateAndSeqno(foundWallet, stateAndSeqno.getLeft(), stateAndSeqno.getRight());

return foundWallet;
}
Expand Down Expand Up @@ -1683,11 +1691,16 @@ public void runAccountsMonitor() {
if (Main.appActive.get()) {
AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(getInstance().getSettings().getGenesisNode(), wallet.getWc() + ":" + wallet.getHexAddress()));
if (nonNull(accountState.getBalance())) {
wallet.setAccountState(accountState);

Pair<AccountState, Long> stateAndSeqno = getAccountStateAndSeqno(getInstance().getSettings().getGenesisNode(), wallet.getWc() + ":" + wallet.getHexAddress());
App.dbPool.updateWalletStateAndSeqno(wallet, stateAndSeqno.getLeft(), stateAndSeqno.getRight());

wallet.setAccountState(stateAndSeqno.getLeft());
wallet.setSeqno(stateAndSeqno.getRight());
updateAccountsTabGui(wallet);
App.dbPool.updateWalletState(wallet, accountState);
}


}
}
} catch (Exception e) {
Expand All @@ -1696,6 +1709,15 @@ public void runAccountsMonitor() {
}, 0L, 5L, TimeUnit.SECONDS);
}

Pair<AccountState, Long> getAccountStateAndSeqno(Node node, String address) {
AccountState accountState = LiteClientParser.parseGetAccount(new LiteClientExecutor().executeGetAccount(node, address));
if (nonNull(accountState.getBalance())) {
long seqno = new LiteClientExecutor().executeGetSeqno(getInstance().getSettings().getGenesisNode(), address);
return Pair.of(accountState, seqno);
}
return Pair.of(null, -1L);
}


/*
public void participate(Node node, long electionId) throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ton/callables/SearchBlocksCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;
import java.util.concurrent.Callable;

import static org.ton.db.DB.*;
import static org.ton.db.DbPool.*;

@Slf4j
public class SearchBlocksCallable implements Callable<BlockCallbackParam> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ton/callables/SearchTxsCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;
import java.util.concurrent.Callable;

import static org.ton.db.DB.*;
import static org.ton.db.DbPool.*;

@Slf4j
public class SearchTxsCallable implements Callable<TxCallbackParam> {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/ton/callables/SearchWalletsCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.util.List;
import java.util.concurrent.Callable;

import static org.ton.db.DB.HEX_ADDR;
import static org.ton.db.DB.WC;
import static org.ton.db.DbPool.HEX_ADDR;
import static org.ton.db.DbPool.WC;

@Slf4j
public class SearchWalletsCallable implements Callable<WalletCallbackParam> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public class UpdateAccountStateCallable implements Callable<WalletCallbackParam>
DB2 db;
WalletPk walletPk;
AccountState accountState;
Long seqno;

public UpdateAccountStateCallable(WalletCallbackParam walletCallbackParam) {
this.db = walletCallbackParam.getDb();
this.walletPk = walletCallbackParam.getWalletPk();
this.accountState = walletCallbackParam.getAccountState();
this.seqno = walletCallbackParam.getSeqno();
}

public WalletCallbackParam call() {
Expand All @@ -39,6 +41,7 @@ public WalletCallbackParam call() {
if (nonNull(walletFound)) {
em.getTransaction().begin();
walletFound.setAccountState(accountState);
walletFound.setSeqno(seqno);
if ((!accountState.getStateCode().isEmpty()) && (!accountState.getStateData().isEmpty())) {
Pair<WalletVersion, Long> walletVersionAndId = Utils.detectWalledVersionAndId(accountState);
walletFound.setWalletVersion(walletVersionAndId.getLeft());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class WalletCallbackParam {
WalletEntity foundWallet;
List<WalletEntity> foundWallets;
AccountState accountState;
Long seqno;
String searchText;
}
21 changes: 2 additions & 19 deletions src/main/java/org/ton/db/DB.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
package org.ton.db;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.ton.db.entities.*;
import org.ton.executors.liteclient.api.AccountState;
import org.ton.executors.liteclient.api.block.Block;
import org.ton.settings.MyLocalTonSettings;
import org.ton.utils.Utils;
import org.ton.wallet.WalletVersion;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.ton.actions.MyLocalTon.SCROLL_BAR_DELTA;

@Slf4j
public class DB {
/*
public static final String SEQNO = "seqno";
public static final String SHARD = "shard";
public static final String WC = "wc";
Expand Down Expand Up @@ -472,4 +454,5 @@ public static List<WalletEntity> searchAccounts(String wcShardSeqnoHash) {
em.close();
}
}
*/
}
23 changes: 19 additions & 4 deletions src/main/java/org/ton/db/DbPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
@Slf4j
public class DbPool {
public static final String SEQNO = "seqno";
public static final String SHARD = "shard";
public static final String WC = "wc";
public static final String HEX_ADDR = "hexAddr";
public static final String HASH = "hash";

public static final String TOO_MANY_PERSISTENT_OBJECTS_1000000 = "Too many persistent objects (>1000000)";
AtomicBoolean spawned = new AtomicBoolean(false);
MyLocalTonSettings settings;
Expand Down Expand Up @@ -111,7 +117,7 @@ public WalletEntity findWallet(WalletPk walletPk) {
threadPoolService.shutdown();
return result;
} catch (Exception e) {
log.error("Error findWallet(), {}" + e.getMessage());
//log.error("Error findWallet(), {}" + e.getMessage());
return result;
}
}
Expand Down Expand Up @@ -359,7 +365,7 @@ public List<TxEntity> loadTxsBefore(long datetimeFrom) {
}
}

public void updateWalletState(WalletEntity walletEntity, AccountState accountState) {
public void updateWalletStateAndSeqno(WalletEntity walletEntity, AccountState accountState, long seqno) {
log.debug("updating account state, {}, {}", walletEntity.getFullAddress(), accountState);
try {

Expand All @@ -369,7 +375,9 @@ public void updateWalletState(WalletEntity walletEntity, AccountState accountSta
UpdateAccountStateCallable callable = new UpdateAccountStateCallable(WalletCallbackParam.builder()
.db(db)
.walletPk(walletEntity.getPrimaryKey())
.accountState(accountState).build());
.accountState(accountState)
.seqno(seqno)
.build());
callablesList.add(callable);
}

Expand Down Expand Up @@ -427,7 +435,7 @@ public List<WalletEntity> getAllWallets() {
threadPoolService.shutdown();
return result;
} catch (Exception e) {
log.error("Error findWallet(), {}" + e.getMessage());
//log.error("Error getAllWallets(), {}" + e.getMessage());
return result;
}
}
Expand Down Expand Up @@ -593,4 +601,11 @@ public List<TxEntity> searchTxs(String wcShardSeqnoHash) {
return result;
}
}

public void closeDbs() {
log.info("Closing database...");
for (DB2 db : allDBs) {
db.getEmf().close();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/org/ton/db/entities/WalletEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class WalletEntity {
String hexAddress;

Long subWalletId;
long seqno;

WalletVersion walletVersion; //walletV1, walletV2, walletV3
WalletAddress wallet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ public String executeLast(Node node) {
}
}

public long executeGetSeqno(Node node, String contractAddress) throws Exception {
return LiteClientParser.parseRunMethodSeqno(executeRunMethod(node, contractAddress, "seqno", ""));
public long executeGetSeqno(Node node, String contractAddress) {
try {
return LiteClientParser.parseRunMethodSeqno(executeRunMethod(node, contractAddress, "seqno", ""));
} catch (Exception e) {
return -1L;
}
}

/**
Expand Down Expand Up @@ -235,7 +239,7 @@ public String executeGetAccount(Node node, String address) {

public String executeRunMethod(Node node, String address, String methodId, String params) throws Exception {
final String command = String.format("runmethod %s %s %s", address, methodId, params);
return execute(node, command).getRight().get().toString();
return execute(node, command).getRight().get();
}

public String executeSendfile(Node node, String absolutePathFile) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,11 @@ public static AccountState parseGetAccount(String stdout) {
}

public static long parseRunMethodSeqno(String stdout) {
return Long.parseLong(StringUtils.substringBetween(stdout, "result: [", "]").trim());
try {
return Long.parseLong(StringUtils.substringBetween(stdout, "result: [", "]").trim());
} catch (Exception e) {
return -1L;
}
}

public static List<ResultListParticipants> parseRunMethodParticipantList(String stdout) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/ton/ui/controllers/AccountController.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public class AccountController {
@FXML
public Label type;

@FXML
public Label seqno;

@FXML
public Label createdat;

@FXML
Label status;

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/ton/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.ton.actions.MyLocalTon;
import org.ton.executors.liteclient.api.AccountState;
import org.ton.executors.liteclient.api.ResultLastBlock;
import org.ton.main.App;
import org.ton.main.Main;
import org.ton.wallet.WalletVersion;

Expand Down Expand Up @@ -274,8 +275,9 @@ public static String getMyPath() {
public static boolean doShutdown() {
try {
if (Main.appActive.get()) {
log.debug("do shutdown");
log.debug("Do shutdown");
Main.appActive.set(false);
App.dbPool.closeDbs();
Main.fileLock.release();
Main.randomAccessFile.close();
Main.file.delete();
Expand Down
Loading

0 comments on commit 62b331f

Please sign in to comment.