From 688c56ad5c68c99ddeb9aaf6b749b5b6612ab801 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 29 Mar 2017 20:11:15 -0500 Subject: [PATCH] Require renter settings to mount --- htdocs/js/index.js | 78 ++++++++++++++++++++++---------- include/siadrive_api/siacommon.h | 11 +++-- src/siadrive/siadriveapp.cpp | 1 + src/siadrive_api/siarenter.cpp | 7 +++ 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/htdocs/js/index.js b/htdocs/js/index.js index 2705482..8425be1 100644 --- a/htdocs/js/index.js +++ b/htdocs/js/index.js @@ -9,6 +9,10 @@ document.getElementById(id).value = value; } + function getValue(id) { + return document.getElementById(id).value; + } + function setSelect(id, itemList) { const select = document.getElementById(id); select.innerHTML = ""; @@ -116,11 +120,16 @@ return window.uiState.isWalletLocked; } + function _allocatedRenterFunds() { + return window.uiState.allocatedRenterFunds + } + return { clientVersion: _clientVersion, isOnline: _isOnline, isWalletConfigured: _isWalletConfigured, - isWalletLocked: _isWalletLocked + isWalletLocked: _isWalletLocked, + allocatedRenterFunds: _allocatedRenterFunds }; })(); @@ -220,42 +229,65 @@ const mountSelect = document.getElementById('ID_MountDrives'); _mountHandler = ()=> { - mountButton.onclick = null; - mountButton.disabled = true; - mountSelect.disabled = true; - if (mountButton.innerText === "Mount") { - AppActions.mountDrive(mountSelect.value, (success, reason) => { - if (success) { - mountButton.innerText = "Unmount"; - } else { - displayErrorPopup('Mount Failed', reason); - } - mountButton.onclick = _mountHandler; - mountButton.disabled = false; - }); + if (UiState.allocatedRenterFunds() === '0') { + displayErrorPopup('Error', 'Renter funding must be configured before trying to mount.') } else { - AppActions.unmountDrive((success, reason) => { - if (success) { - _notifyDriveUnmounted() - } else { - displayErrorPopup('Unmount Failed', reason); + mountButton.onclick = null; + mountButton.disabled = true; + mountSelect.disabled = true; + if (mountButton.innerText === "Mount") { + AppActions.mountDrive(mountSelect.value, (success, reason) => { + if (success) { + mountButton.innerText = "Unmount"; + } else { + displayErrorPopup('Mount Failed', reason); + } mountButton.onclick = _mountHandler; mountButton.disabled = false; - } - }); + }); + } else { + AppActions.unmountDrive((success, reason) => { + if (success) { + _notifyDriveUnmounted() + } else { + displayErrorPopup('Unmount Failed', reason); + mountButton.onclick = _mountHandler; + mountButton.disabled = false; + } + }); + } } }; mountButton.onclick = _mountHandler; } function handleRenterEditSettings() { - setMainWindow('renter_settings_window'); const cancelButton = document.getElementById('ID_RenterSettingsCancel'); cancelButton.onclick = ()=> { + saveButton.onclick = null; cancelButton.onclick = null; beginMainApplication(); - } + }; + + const saveButton = document.getElementById('ID_RenterSettingsOK'); + saveButton.onclick = ()=> { + saveButton.onclick = null; + cancelButton.onclick = null; + AppActions.setRenterAllowance({ + 'Funds': getValue('ID_RenterSetFunds'), + 'Hosts': getValue('ID_RenterSetHosts'), + 'Period': getValue('ID_RenterSetPeriod'), + 'RenewWindowInBlocks': getValue('ID_RenterSetRenewWindow') + }, (success, reason)=> { + if (success) { + beginMainApplication(); + } else { + displayErrorPopup('Allocation Failed', reason); + handleRenterEditSettings(); + } + }); + }; } function handleUnlockWallet() { diff --git a/include/siadrive_api/siacommon.h b/include/siadrive_api/siacommon.h index d97eb9d..48d2dd0 100644 --- a/include/siadrive_api/siacommon.h +++ b/include/siadrive_api/siacommon.h @@ -5,10 +5,6 @@ #define SIDRIVE_VERSION_STRING "0.0.1" #define COMPAT_SIAD_VERSION "1.1.2" -const std::uint8_t SIA_BLOCKS_PER_MIN = 10; -const std::uint32_t MINUTES_PER_MONTH = (730 * 60); -const std::uint32_t SIA_BLOCKS_PER_MONTH = MINUTES_PER_MONTH / SIA_BLOCKS_PER_MIN; - #ifdef _WIN32 // Disable DLL-interface warnings #pragma warning(disable: 4251) @@ -117,6 +113,13 @@ inline bool ApiSuccess(const T& t) { typedef ttmath::UInt<256> Hastings; typedef ttmath::Big<1, 30> SiaCurrency; +const std::uint8_t SIA_BLOCK_TIME_MINS = 10; +const std::uint32_t MINUTES_PER_MONTH = (730 * 60); +const std::uint32_t SIA_BLOCKS_PER_MONTH = MINUTES_PER_MONTH / SIA_BLOCK_TIME_MINS; +const std::uint32_t SIA_DEFAULT_HOST_COUNT = 24; +const std::uint32_t SIA_DEFAULT_RENEW_WINDOW = (SIA_BLOCK_TIME_MINS * (SIA_DEFAULT_HOST_COUNT * 2)); +const SiaCurrency SIA_DEFAULT_MINIMUM_FUNDS = 4000; + /* BigNumber.config({ EXPONENTIAL_AT: 1e+9 }) BigNumber.config({ DECIMAL_PLACES: 30 }) diff --git a/src/siadrive/siadriveapp.cpp b/src/siadrive/siadriveapp.cpp index 1b3736c..6221a46 100644 --- a/src/siadrive/siadriveapp.cpp +++ b/src/siadrive/siadriveapp.cpp @@ -274,6 +274,7 @@ void CSiaDriveApp::OnContextCreated( obj->SetValue("isWalletConfigured", CefV8Value::CreateBool(_siaApi->GetWallet()->GetCreated()), V8_PROPERTY_ATTRIBUTE_NONE); obj->SetValue("isOnline", CefV8Value::CreateBool(_siaApi->GetWallet()->GetConnected()), V8_PROPERTY_ATTRIBUTE_NONE); obj->SetValue("clientVersion", CefV8Value::CreateString(SIDRIVE_VERSION_STRING), V8_PROPERTY_ATTRIBUTE_NONE); + obj->SetValue("allocatedRenterFunds", CefV8Value::CreateString(_siaApi->GetRenter()->GetFunds().ToString()), V8_PROPERTY_ATTRIBUTE_NONE); global->SetValue("uiState", obj, V8_PROPERTY_ATTRIBUTE_NONE); CefRefPtr handler(new FunctionHandler(*_siaApi, _appStarted, _siaDriveConfig, _siaDrive, [this]() {this->ShutdownServices(); }, [this](CefRefPtr context) {this->SiaApiRefreshCallback(context, *_siaCurl, _siaDriveConfig.get()); })); diff --git a/src/siadrive_api/siarenter.cpp b/src/siadrive_api/siarenter.cpp index e27b1a9..51c4e2e 100644 --- a/src/siadrive_api/siarenter.cpp +++ b/src/siadrive_api/siarenter.cpp @@ -80,6 +80,13 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia SetPeriod(period); _currentAllowance = { funds, hosts, period, renewWindow }; + if (_currentAllowance.Funds == 0) + { + _currentAllowance.Funds = SIA_DEFAULT_MINIMUM_FUNDS; + _currentAllowance.Hosts = SIA_DEFAULT_HOST_COUNT; + _currentAllowance.Period = SIA_BLOCKS_PER_MONTH * 3; + _currentAllowance.RenewWindowInBlocks = SIA_DEFAULT_RENEW_WINDOW; + } if (ApiSuccess(RefreshFileTree())) { CSiaFileTreePtr fileTree;