From ccc3db50072c35961f355a8592aa38babc13649e Mon Sep 17 00:00:00 2001 From: JoeGruff Date: Tue, 5 Mar 2024 16:01:16 +0900 Subject: [PATCH] decred: Add rescan. --- cw_decred/lib/api/libdcrwallet.dart | 10 ++++++++++ cw_decred/lib/wallet.dart | 5 ++++- lib/decred/cw_decred.dart | 12 ++++++++++++ lib/di.dart | 2 +- lib/src/screens/rescan/rescan_page.dart | 5 ++++- lib/src/widgets/blockchain_height_widget.dart | 9 ++++++++- lib/view_model/dashboard/dashboard_view_model.dart | 1 + scripts/android/build_decred.sh | 6 ++++-- scripts/ios/build_decred.sh | 6 ++++-- tool/configure.dart | 2 ++ 10 files changed, 50 insertions(+), 8 deletions(-) diff --git a/cw_decred/lib/api/libdcrwallet.dart b/cw_decred/lib/api/libdcrwallet.dart index 156ac3641..fc1c369e0 100644 --- a/cw_decred/lib/api/libdcrwallet.dart +++ b/cw_decred/lib/api/libdcrwallet.dart @@ -206,3 +206,13 @@ String listUnspents(String walletName) { ); return res.payload; } + +String rescanFromHeight(String walletName, String height) { + final cName = walletName.toCString(); + final cHeight = height.toCString(); + final res = executePayloadFn( + fn: () => dcrwalletApi.rescanFromHeight(cName, cHeight), + ptrsToFree: [cName, cHeight], + ); + return res.payload; +} diff --git a/cw_decred/lib/wallet.dart b/cw_decred/lib/wallet.dart index d633954d4..38bc44584 100644 --- a/cw_decred/lib/wallet.dart +++ b/cw_decred/lib/wallet.dart @@ -401,7 +401,10 @@ abstract class DecredWalletBase extends WalletBase rescan({required int height}) async { - // TODO. + if (height > this.bestHeight) { + return; + } + libdcrwallet.rescanFromHeight(walletInfo.name, height.toString()); } @override diff --git a/lib/decred/cw_decred.dart b/lib/decred/cw_decred.dart index 9c8f0dd2a..f52c57854 100644 --- a/lib/decred/cw_decred.dart +++ b/lib/decred/cw_decred.dart @@ -103,5 +103,17 @@ class CWDecred extends Decred { return decredWallet.unspents(); } + @override void updateUnspents(Object wallet) async {} + + @override + int heightByDate(DateTime date) { + final genesisBlocktime = + DateTime.fromMillisecondsSinceEpoch(1454954400 * 1000); + final minutesDiff = date.difference(genesisBlocktime).inMinutes; + // Decred has five minute blocks on mainnet. + // NOTE: This is off by about a day. + // TODO: Remove this and just rescan from the wallet birthday. + return (minutesDiff / 5).toInt(); + } } diff --git a/lib/di.dart b/lib/di.dart index 8b3fcc2ca..7b8525fec 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -1113,7 +1113,7 @@ Future setup({ getIt.registerFactory(() => RescanViewModel(getIt.get().wallet!)); - getIt.registerFactory(() => RescanPage(getIt.get())); + getIt.registerFactory(() => RescanPage(getIt.get(), getIt.get().wallet!.type)); getIt.registerFactory(() => FaqPage(getIt.get())); diff --git a/lib/src/screens/rescan/rescan_page.dart b/lib/src/screens/rescan/rescan_page.dart index 2c1c213c1..52fd8a977 100644 --- a/lib/src/screens/rescan/rescan_page.dart +++ b/lib/src/screens/rescan/rescan_page.dart @@ -9,9 +9,10 @@ import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/widgets/blockchain_height_widget.dart'; import 'package:cake_wallet/src/widgets/primary_button.dart'; import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cw_core/wallet_type.dart'; class RescanPage extends BasePage { - RescanPage(this._rescanViewModel) + RescanPage(this._rescanViewModel, this.type) : _blockchainHeightWidgetKey = GlobalKey(); @override @@ -19,6 +20,7 @@ class RescanPage extends BasePage { _rescanViewModel.isSilentPaymentsScan ? S.current.silent_payments_scanning : S.current.rescan; final GlobalKey _blockchainHeightWidgetKey; final RescanViewModel _rescanViewModel; + final WalletType type; @override Widget body(BuildContext context) { @@ -30,6 +32,7 @@ class RescanPage extends BasePage { child: Column(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Observer( builder: (_) => BlockchainHeightWidget( + type: this.type, key: _blockchainHeightWidgetKey, onHeightOrDateEntered: (value) => _rescanViewModel.isButtonEnabled = value, isSilentPaymentsScan: _rescanViewModel.isSilentPaymentsScan, diff --git a/lib/src/widgets/blockchain_height_widget.dart b/lib/src/widgets/blockchain_height_widget.dart index 650ee684d..47cacdda2 100644 --- a/lib/src/widgets/blockchain_height_widget.dart +++ b/lib/src/widgets/blockchain_height_widget.dart @@ -9,10 +9,14 @@ import 'package:intl/intl.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; +import 'package:cake_wallet/decred/decred.dart'; +import 'package:cw_core/wallet_type.dart'; + class BlockchainHeightWidget extends StatefulWidget { BlockchainHeightWidget({ GlobalKey? key, + this.type, this.onHeightChange, this.focusNode, this.onHeightOrDateEntered, @@ -26,6 +30,7 @@ class BlockchainHeightWidget extends StatefulWidget { this.blockHeightTextFieldKey, }) : super(key: key); + final WalletType? type; final Function(int)? onHeightChange; final Function(bool)? onHeightOrDateEntered; final FocusNode? focusNode; @@ -183,7 +188,9 @@ class BlockchainHeightState extends State { bitcoinMempoolAPIEnabled: await widget.bitcoinMempoolAPIEnabled, ); } else { - if (widget.walletType == WalletType.monero) { + if (widget.type == WalletType.decred) { + height = decred!.heightByDate(date); + } else if (widget.walletType == WalletType.monero) { height = monero!.getHeightByDate(date: date); } else { assert(widget.walletType == WalletType.wownero, diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 387c66511..69e3dcf2f 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -388,6 +388,7 @@ abstract class DashboardViewModelBase with Store { wallet.type == WalletType.monero || wallet.type == WalletType.litecoin || wallet.type == WalletType.wownero || + wallet.type == WalletType.decred || wallet.type == WalletType.haven; @computed diff --git a/scripts/android/build_decred.sh b/scripts/android/build_decred.sh index 4326f7192..942dc5b58 100755 --- a/scripts/android/build_decred.sh +++ b/scripts/android/build_decred.sh @@ -3,14 +3,16 @@ . ./config.sh CW_DECRED_DIR=${WORKDIR}/cake_wallet/cw_decred LIBWALLET_PATH="${WORKDIR}/decred/libwallet" -LIBWALLET_URL="https://github.com/itswisdomagain/libwallet.git" +LIBWALLET_URL="https://github.com/decred/libwallet.git" +LIBWALLET_COMMIT="9f39f38b460e2dece5704cbc4aee293c741ee710" if [ -e $LIBWALLET_PATH ]; then rm -fr $LIBWALLET_PATH fi mkdir -p $LIBWALLET_PATH -git clone $LIBWALLET_URL $LIBWALLET_PATH --branch cgo +git clone $LIBWALLET_URL $LIBWALLET_PATH cd $LIBWALLET_PATH +git checkout $LIBWALLET_COMMIT export CPATH="$(clang -v 2>&1 | grep "Selected GCC installation" | rev | cut -d' ' -f1 | rev)/include" diff --git a/scripts/ios/build_decred.sh b/scripts/ios/build_decred.sh index 36f81f841..4c541033c 100755 --- a/scripts/ios/build_decred.sh +++ b/scripts/ios/build_decred.sh @@ -2,14 +2,16 @@ . ./config.sh LIBWALLET_PATH="${EXTERNAL_IOS_SOURCE_DIR}/libwallet" -LIBWALLET_URL="https://github.com/itswisdomagain/libwallet.git" +LIBWALLET_URL="https://github.com/decred/libwallet.git" +LIBWALLET_COMMIT="9f39f38b460e2dece5704cbc4aee293c741ee710" if [ -e $LIBWALLET_PATH ]; then rm -fr $LIBWALLET_PATH fi mkdir -p $LIBWALLET_PATH -git clone $LIBWALLET_URL $LIBWALLET_PATH --branch cgo +git clone $LIBWALLET_URL $LIBWALLET_PATH cd $LIBWALLET_PATH +git checkout $LIBWALLET_COMMIT SYSROOT=`xcrun --sdk iphoneos --show-sdk-path` CLANG="clang -isysroot ${SYSROOT}" diff --git a/tool/configure.dart b/tool/configure.dart index 07b30f90d..bfb534934 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -1461,6 +1461,8 @@ abstract class Decred { List getUnspents(Object wallet); void updateUnspents(Object wallet); + + int heightByDate(DateTime date); } """;