mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-07 08:04:04 +01:00
fix(wownero): hold the history mutex while reading transactions
The 1.0.5 build 15 macOS test crashed with SIGSEGV in WOWNERO_TransactionInfo_amount on the main thread, near the end of a restore sync. TransactionHistory_refresh runs on a background isolate under txHistoryMutex and frees the TransactionInfo objects it replaces; getAllSubaddresses() and getTransaction() read those objects without the mutex, so a read that overlaps a refresh dereferences freed memory. The wallet cache had not been stored, so the next launch synced again. Upstream fixed the same race for Monero in #3188 and in the v6.4.5 coin-control change; this ports both to cw_wownero. getAllSubaddresses() and getTransaction() take the mutex and become async, which carries through the subaddress list, its app proxy and the configure interface. The subaddress list now fetches before clearing, so the observable list is never empty while the fetch is pending, and addSubaddress and setLabelSubaddress await the update before callers read the new address. The coin-control change check compared a Transaction with 1 and was always false; it now uses the transaction's direction, as Monero does.
This commit is contained in:
@@ -57,27 +57,32 @@ int lastWptr = 0;
|
||||
int lastTxCount = 0;
|
||||
List<TinyTransactionDetails> ttDetails = [];
|
||||
|
||||
List<Subaddress> getAllSubaddresses() {
|
||||
txhistory = wownero.Wallet_history(wptr!);
|
||||
final txCount = wownero.TransactionHistory_count(txhistory!);
|
||||
if (lastTxCount != txCount && lastWptr != wptr!.address) {
|
||||
final List<TinyTransactionDetails> newttDetails = [];
|
||||
lastTxCount = txCount;
|
||||
lastWptr = wptr!.address;
|
||||
for (var i = 0; i < txCount; i++) {
|
||||
final tx = wownero.TransactionHistory_transaction(txhistory!, index: i);
|
||||
final subaddrs = wownero.TransactionInfo_subaddrIndex(tx).split(",");
|
||||
final account = wownero.TransactionInfo_subaddrAccount(tx);
|
||||
newttDetails.add(TinyTransactionDetails(
|
||||
address: List.generate(
|
||||
subaddrs.length,
|
||||
(index) => getAddress(
|
||||
accountIndex: account, addressIndex: int.tryParse(subaddrs[index]) ?? 0)),
|
||||
amount: wownero.TransactionInfo_amount(tx),
|
||||
));
|
||||
Future<List<Subaddress>> getAllSubaddresses() async {
|
||||
await txHistoryMutex.acquire();
|
||||
try {
|
||||
txhistory = wownero.Wallet_history(wptr!);
|
||||
final txCount = wownero.TransactionHistory_count(txhistory!);
|
||||
if (lastTxCount != txCount && lastWptr != wptr!.address) {
|
||||
final List<TinyTransactionDetails> newttDetails = [];
|
||||
lastTxCount = txCount;
|
||||
lastWptr = wptr!.address;
|
||||
for (var i = 0; i < txCount; i++) {
|
||||
final tx = wownero.TransactionHistory_transaction(txhistory!, index: i);
|
||||
final subaddrs = wownero.TransactionInfo_subaddrIndex(tx).split(",");
|
||||
final account = wownero.TransactionInfo_subaddrAccount(tx);
|
||||
newttDetails.add(TinyTransactionDetails(
|
||||
address: List.generate(
|
||||
subaddrs.length,
|
||||
(index) => getAddress(
|
||||
accountIndex: account, addressIndex: int.tryParse(subaddrs[index]) ?? 0)),
|
||||
amount: wownero.TransactionInfo_amount(tx),
|
||||
));
|
||||
}
|
||||
ttDetails.clear();
|
||||
ttDetails.addAll(newttDetails);
|
||||
}
|
||||
ttDetails.clear();
|
||||
ttDetails.addAll(newttDetails);
|
||||
} finally {
|
||||
txHistoryMutex.release();
|
||||
}
|
||||
final size = wownero.Wallet_numSubaddresses(wptr!, accountIndex: subaddress!.accountIndex);
|
||||
final list = List.generate(size, (index) {
|
||||
|
||||
@@ -81,8 +81,14 @@ Future<List<Transaction>> getAllTransactions() async {
|
||||
}
|
||||
|
||||
// TODO(mrcyjanek): ...
|
||||
Transaction getTransaction(String txId) {
|
||||
return Transaction(txInfo: wownero.TransactionHistory_transactionById(txhistory!, txid: txId));
|
||||
Future<Transaction> getTransaction(String txId) async {
|
||||
await txHistoryMutex.acquire();
|
||||
try {
|
||||
return Transaction(
|
||||
txInfo: wownero.TransactionHistory_transactionById(txhistory!, txid: txId));
|
||||
} finally {
|
||||
txHistoryMutex.release();
|
||||
}
|
||||
}
|
||||
|
||||
Future<PendingTransactionDescription> createTransactionSync(
|
||||
|
||||
@@ -24,7 +24,7 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
bool _isRefreshing;
|
||||
bool _isUpdating;
|
||||
|
||||
void update({required int accountIndex}) {
|
||||
Future<void> update({required int accountIndex}) async {
|
||||
refreshCoins(accountIndex);
|
||||
|
||||
if (_isUpdating) {
|
||||
@@ -34,8 +34,9 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
try {
|
||||
_isUpdating = true;
|
||||
refresh(accountIndex: accountIndex);
|
||||
final newAddrs = await getAll();
|
||||
subaddresses.clear();
|
||||
subaddresses.addAll(getAll());
|
||||
subaddresses.addAll(newAddrs);
|
||||
_isUpdating = false;
|
||||
} catch (e) {
|
||||
_isUpdating = false;
|
||||
@@ -43,8 +44,8 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
}
|
||||
}
|
||||
|
||||
List<Subaddress> getAll() {
|
||||
var subaddresses = subaddress_list.getAllSubaddresses();
|
||||
Future<List<Subaddress>> getAll() async {
|
||||
var subaddresses = await subaddress_list.getAllSubaddresses();
|
||||
|
||||
if (subaddresses.length > 2) {
|
||||
final primary = subaddresses.first;
|
||||
@@ -74,14 +75,14 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
|
||||
Future<void> addSubaddress({required int accountIndex, required String label}) async {
|
||||
await subaddress_list.addSubaddress(accountIndex: accountIndex, label: label);
|
||||
update(accountIndex: accountIndex);
|
||||
await update(accountIndex: accountIndex);
|
||||
}
|
||||
|
||||
Future<void> setLabelSubaddress(
|
||||
{required int accountIndex, required int addressIndex, required String label}) async {
|
||||
await subaddress_list.setLabelForSubaddress(
|
||||
accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
||||
update(accountIndex: accountIndex);
|
||||
await update(accountIndex: accountIndex);
|
||||
}
|
||||
|
||||
void refresh({required int accountIndex}) {
|
||||
@@ -129,7 +130,7 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
|
||||
Future<List<Subaddress>> _getAllUnusedAddresses(
|
||||
{required int accountIndex, required String label}) async {
|
||||
final allAddresses = subaddress_list.getAllSubaddresses();
|
||||
final allAddresses = await subaddress_list.getAllSubaddresses();
|
||||
final lastAddress = allAddresses.last.address;
|
||||
if (allAddresses.isEmpty || _usedAddresses.contains(lastAddress)) {
|
||||
final isAddressUnused = await _newSubaddress(accountIndex: accountIndex, label: label);
|
||||
@@ -156,7 +157,7 @@ abstract class WowneroSubaddressListBase with Store {
|
||||
Future<bool> _newSubaddress({required int accountIndex, required String label}) async {
|
||||
await subaddress_list.addSubaddress(accountIndex: accountIndex, label: label);
|
||||
|
||||
return subaddress_list.getAllSubaddresses().where((s) {
|
||||
return (await subaddress_list.getAllSubaddresses()).where((s) {
|
||||
final address = s.address;
|
||||
return !_usedAddresses.contains(address);
|
||||
}).isNotEmpty;
|
||||
|
||||
@@ -583,7 +583,12 @@ abstract class WowneroWalletBase
|
||||
wownero.CoinsInfo_unlocked(coin),
|
||||
);
|
||||
if (unspent.hash.isNotEmpty) {
|
||||
unspent.isChange = transaction_history.getTransaction(unspent.hash) == 1;
|
||||
try {
|
||||
final tx = await transaction_history.getTransaction(unspent.hash);
|
||||
unspent.isChange = tx.isSpend == true;
|
||||
} catch (e) {
|
||||
printV("isChange lookup failed for ${unspent.hash}: $e");
|
||||
}
|
||||
}
|
||||
unspentCoins.add(unspent);
|
||||
}
|
||||
|
||||
@@ -83,10 +83,9 @@ class CWWowneroSubaddressList extends WowneroSubaddressList {
|
||||
}
|
||||
|
||||
@override
|
||||
List<Subaddress> getAll(Object wallet) {
|
||||
Future<List<Subaddress>> getAll(Object wallet) async {
|
||||
final wowneroWallet = wallet as WowneroWallet;
|
||||
return wowneroWallet.walletAddresses.subaddressList
|
||||
.getAll()
|
||||
return (await wowneroWallet.walletAddresses.subaddressList.getAll())
|
||||
.map((sub) => Subaddress(id: sub.id, label: sub.label, address: sub.address))
|
||||
.toList();
|
||||
}
|
||||
|
||||
+1
-1
@@ -658,7 +658,7 @@ abstract class WowneroSubaddressList {
|
||||
ObservableList<Subaddress> get subaddresses;
|
||||
void update(Object wallet, {required int accountIndex});
|
||||
void refresh(Object wallet, {required int accountIndex});
|
||||
List<Subaddress> getAll(Object wallet);
|
||||
Future<List<Subaddress>> getAll(Object wallet);
|
||||
Future<void> addSubaddress(Object wallet, {required int accountIndex, required String label});
|
||||
Future<void> setLabelSubaddress(Object wallet,
|
||||
{required int accountIndex, required int addressIndex, required String label});
|
||||
|
||||
Reference in New Issue
Block a user