From ca9883b6c0c2dfe91f77462bc4909c5305e1126a Mon Sep 17 00:00:00 2001 From: mmbyday Date: Tue, 14 May 2019 20:21:09 -0700 Subject: [PATCH] Fix restore height when using stagenet/testnet --- js/Wizard.js | 16 ++++++++++++---- pages/settings/SettingsInfo.qml | 3 ++- wizard/WizardCreateDevice1.qml | 3 ++- wizard/WizardRestoreWallet1.qml | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/js/Wizard.js b/js/Wizard.js index 44954c8a..8bdc4616 100644 --- a/js/Wizard.js +++ b/js/Wizard.js @@ -134,15 +134,15 @@ function restoreWalletCheckViewSpendAddress(walletmanager, nettype, viewkey, spe //usage: getApproximateBlockchainHeight("March 18 2016") or getApproximateBlockchainHeight("2016-11-11") //returns estimated block height with 1 month buffer prior to requested date. -function getApproximateBlockchainHeight(_date){ +function getApproximateBlockchainHeight(_date, _nettype){ // time of monero birth 2014-04-18 10:49:53 (1397818193) - var moneroBirthTime = 1397818193; + var moneroBirthTime = _nettype == "Mainnet" ? 1397818193 : _nettype == "Testnet" ? 1410295020 : 1518932025; // avg seconds per block in v1 var secondsPerBlockV1 = 60; // time of v2 fork 2016-03-23 15:57:38 (1458748658) - var forkTime = 1458748658; + var forkTime = _nettype == "Mainnet" ? 1458748658 : _nettype == "Testnet" ? 1448285909 : 1520937818; // v2 fork block - var forkBlock = 1009827; + var forkBlock = _nettype == "Mainnet" ? 1009827 : _nettype == "Testnet" ? 624634 : 32000; // avg seconds per block in V2 var secondsPerBlockV2 = 120; // time in UTC @@ -166,6 +166,14 @@ function getApproximateBlockchainHeight(_date){ console.log("Calculated blockchain height: " + approxBlockchainHeight ); secondsPerBlock = secondsPerBlockV2; } + + if(_nettype == "Testnet"){ + // testnet got some huge rollbacks, so the estimation is way off + var approximateTestnetRolledBackBlocks = 303967; + if(approxBlockchainHeight > approximateTestnetRolledBackBlocks) + approxBlockchainHeight -= approximateTestnetRolledBackBlocks + } + var blocksPerMonth = 60*60*24*30/secondsPerBlock; if(approxBlockchainHeight - blocksPerMonth > 0){ return approxBlockchainHeight - blocksPerMonth; diff --git a/pages/settings/SettingsInfo.qml b/pages/settings/SettingsInfo.qml index 9da336bf..de6a28d2 100644 --- a/pages/settings/SettingsInfo.qml +++ b/pages/settings/SettingsInfo.qml @@ -32,6 +32,7 @@ import QtQuick.Controls 2.0 import QtQuick.Dialogs 1.2 import "../../js/Wizard.js" as Wizard +import "../../js/Utils.js" as Utils import "../../version.js" as Version import "../../components" as MoneroComponents @@ -184,7 +185,7 @@ Rectangle { var restoreHeightText = inputDialog.inputText; // Parse date string or restore height as integer if(restoreHeightText.indexOf('-') === 4 && restoreHeightText.length === 10) { - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeightText); + _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeightText, Utils.netTypeToString()); } else { _restoreHeight = parseInt(restoreHeightText) } diff --git a/wizard/WizardCreateDevice1.qml b/wizard/WizardCreateDevice1.qml index 9a2191bc..21fc5a9a 100644 --- a/wizard/WizardCreateDevice1.qml +++ b/wizard/WizardCreateDevice1.qml @@ -33,6 +33,7 @@ import QtQuick.Controls 2.0 import moneroComponents.Wallet 1.0 import "../js/Wizard.js" as Wizard +import "../js/Utils.js" as Utils import "../components" import "../components" as MoneroComponents @@ -195,7 +196,7 @@ Rectangle { if(restoreHeight.text){ // Parse date string or restore height as integer if(restoreHeight.text.indexOf('-') === 4 && restoreHeight.text.length === 10){ - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text); + _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text, Utils.netTypeToString()); } else { _restoreHeight = parseInt(restoreHeight.text) } diff --git a/wizard/WizardRestoreWallet1.qml b/wizard/WizardRestoreWallet1.qml index a41b6a16..c626742f 100644 --- a/wizard/WizardRestoreWallet1.qml +++ b/wizard/WizardRestoreWallet1.qml @@ -31,6 +31,7 @@ import QtQuick.Layouts 1.2 import QtQuick.Controls 2.0 import "../js/Wizard.js" as Wizard +import "../js/Utils.js" as Utils import "../components" as MoneroComponents Rectangle { @@ -292,7 +293,7 @@ Rectangle { if(restoreHeight.text){ // Parse date string or restore height as integer if(restoreHeight.text.indexOf('-') === 4 && restoreHeight.text.length === 10){ - _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text); + _restoreHeight = Wizard.getApproximateBlockchainHeight(restoreHeight.text, Utils.netTypeToString()); } else { _restoreHeight = parseInt(restoreHeight.text) }