Fix ANR issue when syncing over Tor

This commit is contained in:
pokkst 2022-09-08 04:06:20 -05:00
parent 0864b15c69
commit a2b7e27e8b
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
8 changed files with 38 additions and 24 deletions

View File

@ -709,13 +709,16 @@ Java_com_m2049r_xmrwallet_model_Wallet_initJ(JNIEnv *env, jobject instance,
const char *_daemon_address = env->GetStringUTFChars(daemon_address, nullptr);
const char *_daemon_username = env->GetStringUTFChars(daemon_username, nullptr);
const char *_daemon_password = env->GetStringUTFChars(daemon_password, nullptr);
const char *_daemon_proxy = env->GetStringUTFChars(proxy, nullptr);
Monero::Wallet *wallet = getHandle<Monero::Wallet>(env, instance);
bool status = wallet->init(_daemon_address, (uint64_t) upper_transaction_size_limit,
_daemon_username,
_daemon_password, false, false, "127.0.0.1:9050");
_daemon_password, false, false, _daemon_proxy);
env->ReleaseStringUTFChars(daemon_address, _daemon_address);
env->ReleaseStringUTFChars(daemon_username, _daemon_username);
env->ReleaseStringUTFChars(daemon_password, _daemon_password);
env->ReleaseStringUTFChars(proxy, _daemon_proxy);
return static_cast<jboolean>(status);
}

View File

@ -71,7 +71,7 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
public void init(File walletFile, String password) {
Wallet wallet = WalletManager.getInstance().openWallet(walletFile.getAbsolutePath(), password);
thread = new MoneroHandlerThread("WalletService", wallet, this);
thread = new MoneroHandlerThread("WalletService", this, wallet);
this.txService = new TxService(this, thread);
this.balanceService = new BalanceService(this, thread);
this.addressService = new AddressService(this, thread);
@ -84,7 +84,6 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre
public void onRefresh() {
this.historyService.refreshHistory();
this.balanceService.refreshBalance();
this.addressService.refreshAddress();
this.blockchainService.refreshBlockchain();
}

View File

@ -39,14 +39,10 @@ public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment {
ImageView addressImageView = view.findViewById(R.id.monero_qr_imageview);
TextView addressTextView = view.findViewById(R.id.address_textview);
AddressService.getInstance().address.observe(getViewLifecycleOwner(), addr -> {
if (!addr.isEmpty()) {
System.out.println(addr);
String addr = AddressService.getInstance().getAddress();
addressTextView.setText(addr);
addressImageView.setImageBitmap(generate(addr, 256, 256));
}
});
}
public Bitmap generate(String text, int width, int height) {
if ((width <= 0) || (height <= 0)) return null;

View File

@ -14,6 +14,7 @@ import androidx.lifecycle.ViewModelProvider;
import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.model.Wallet;
import com.m2049r.xmrwallet.model.WalletManager;
import com.m2049r.xmrwallet.service.BlockchainService;
public class SettingsFragment extends Fragment {
@ -36,7 +37,7 @@ public class SettingsFragment extends Fragment {
stringBuilder.append("Private view-key: " + wallet.getSecretViewKey()+"\n\n");
stringBuilder.append("Restore height: " + wallet.getRestoreHeight() + "\n\n");
stringBuilder.append("Wallet height: " + wallet.getBlockChainHeight() + "\n\n");
stringBuilder.append("Daemon height: " + wallet.getDaemonBlockChainHeight() + "\n\n");
stringBuilder.append("Daemon height: " + BlockchainService.getInstance().getDaemonHeight() + "\n\n");
walletInfoTextView.setText(stringBuilder.toString());
}
}

View File

@ -219,11 +219,11 @@ public class Wallet {
public boolean init(long upper_transaction_size_limit) {
return initJ(WalletManager.getInstance().getDaemonAddress(), upper_transaction_size_limit,
WalletManager.getInstance().getDaemonUsername(),
WalletManager.getInstance().getDaemonPassword(), false, false, WalletManager.getInstance().getProxy());
WalletManager.getInstance().getDaemonPassword(), WalletManager.getInstance().getProxy());
}
private native boolean initJ(String daemon_address, long upper_transaction_size_limit,
String daemon_username, String daemon_password, boolean useSsl, boolean lightWallet, String proxy);
String daemon_username, String daemon_password, String proxy);
// virtual bool createWatchOnly(const std::string &path, const std::string &password, const std::string &language) const = 0;
// virtual void setRefreshFromBlockHeight(uint64_t refresh_from_block_height) = 0;

View File

@ -13,15 +13,12 @@ public class AddressService extends ServiceBase {
return instance;
}
private final MutableLiveData<String> _address = new MutableLiveData<>("");
public LiveData<String> address = _address;
public AddressService(MainActivity mainActivity, MoneroHandlerThread thread) {
super(mainActivity, thread);
instance = this;
}
public void refreshAddress() {
_address.postValue(WalletManager.getInstance().getWallet().getAddress());
public String getAddress() {
return WalletManager.getInstance().getWallet().getAddress();
}
}

View File

@ -13,6 +13,8 @@ public class BlockchainService extends ServiceBase {
return instance;
}
private long daemonHeight = 0;
private long lastDaemonHeightUpdateTimeMs = 0;
private final MutableLiveData<Long> _currentHeight = new MutableLiveData<>(0L);
public LiveData<Long> height = _currentHeight;
@ -30,6 +32,19 @@ public class BlockchainService extends ServiceBase {
}
public long getDaemonHeight() {
return WalletManager.getInstance().getWallet().getDaemonBlockChainHeight();
return this.daemonHeight;
}
public void setDaemonHeight(long height) {
long t = System.currentTimeMillis();
if(height > 0) {
daemonHeight = height;
lastDaemonHeightUpdateTimeMs = t;
} else {
if(t - lastDaemonHeightUpdateTimeMs > 120000) {
daemonHeight = WalletManager.getInstance().getWallet().getDaemonBlockChainHeight();
lastDaemonHeightUpdateTimeMs = t;
}
}
}
}

View File

@ -27,6 +27,8 @@ import com.m2049r.xmrwallet.model.Wallet;
import com.m2049r.xmrwallet.model.WalletListener;
import com.m2049r.xmrwallet.model.WalletManager;
import java.io.File;
/**
* Handy class for starting a new thread that has a looper. The looper can then be
@ -35,14 +37,14 @@ import com.m2049r.xmrwallet.model.WalletManager;
*/
public class MoneroHandlerThread extends Thread implements WalletListener {
private Listener listener = null;
private Wallet wallet = null;
// from src/cryptonote_config.h
static public final long THREAD_STACK_SIZE = 5 * 1024 * 1024;
private Wallet wallet;
public MoneroHandlerThread(String name, Wallet wallet, Listener listener) {
public MoneroHandlerThread(String name, Listener listener, Wallet wallet) {
super(null, null, name, THREAD_STACK_SIZE);
this.wallet = wallet;
this.listener = listener;
this.wallet = wallet;
}
@Override
@ -53,9 +55,9 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
@Override
public void run() {
WalletManager.getInstance().setProxy("127.0.0.1:9050");
WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.MONERUJO_ONION.getUri()));
WalletManager.getInstance().setDaemon(Node.fromString(DefaultNodes.XMRTW.getUri()));
wallet.init(0);
wallet.setProxy("127.0.0.1:9050");
wallet.setListener(this);
wallet.startRefresh();
}
@ -75,6 +77,7 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
@Override
public void newBlock(long height) {
refresh();
BlockchainService.getInstance().setDaemonHeight(wallet.isSynchronized() ? height : 0);
}
@Override