Refactoring and added common utils
This commit is contained in:
@@ -2,4 +2,35 @@
|
||||
#include "SiaCommon.h"
|
||||
#include <ttmath/ttmath.h>
|
||||
|
||||
using namespace Sia::Api;
|
||||
using namespace Sia::Api;
|
||||
|
||||
|
||||
/*
|
||||
// avgHostMetric computes the average of the metric given by `metric` on the
|
||||
// list of `hosts`.
|
||||
const avgHostMetric = (hosts, metric) = >
|
||||
hosts.reduce((sum, host) = > sum.add(host[metric]), new BigNumber(0))
|
||||
.dividedBy(hosts.size)
|
||||
|
||||
// avgStorageCost returns the average storage cost from a list of hosts given a
|
||||
// period (blocks) and redundancy.
|
||||
const avgStorageCost = (hosts, period, redundancy) = >
|
||||
avgHostMetric(hosts, 'storageprice')
|
||||
.times(period)
|
||||
.plus(avgHostMetric(hosts, 'uploadbandwidthprice'))
|
||||
.times(redundancy)
|
||||
.plus(avgHostMetric(hosts, 'downloadbandwidthprice'))
|
||||
|
||||
// Compute an estimated amount of storage from an amount of funds (Hastings)
|
||||
// and a list of hosts.
|
||||
export const estimatedStorage = (funds, hosts) = > {
|
||||
const validHosts = List(hosts).take(28)
|
||||
const avgStorage = avgStorageCost(validHosts, allowancePeriod, baseRedundancy)
|
||||
|
||||
let fee = SiaAPI.siacoinsToHastings(baseFee)
|
||||
fee = fee.plus(avgHostMetric(validHosts, 'contractprice').times(ncontracts))
|
||||
fee = fee.plus(funds.minus(fee).times(siafundRate))
|
||||
|
||||
return '~' + readableFilesize(Math.max(0, funds.minus(fee).dividedBy(avgStorage).toNumber().toPrecision(1)))
|
||||
}
|
||||
*/
|
@@ -20,7 +20,8 @@ using json = nlohmann::json;
|
||||
NS_BEGIN(Sia)
|
||||
NS_BEGIN(Api)
|
||||
|
||||
#define String std::wstring
|
||||
typedef std::wstring String;
|
||||
#define DEFAULT_CONFIG_FILE_PATH L".\\Config\\SiaDriveConfig.json"
|
||||
|
||||
#define Property(type, name, get_access, set_access) \
|
||||
private:\
|
||||
@@ -43,10 +44,12 @@ struct SiaHostConfig
|
||||
String RequiredVersion;
|
||||
};
|
||||
|
||||
#define API_SUCCESS(t, x) (x == t::Success)
|
||||
|
||||
#define DEFAULT_CONFIG_FILE_PATH L".\\Config\\SiaDriveConfig.json"
|
||||
template<typename T>
|
||||
inline bool ApiSuccess(const T& t) {
|
||||
return t == T::Success;
|
||||
}
|
||||
|
||||
typedef ttmath::UInt<256> Hastings;
|
||||
typedef ttmath::Big<1, 30> SiaCurrency;
|
||||
/*
|
||||
BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
|
||||
@@ -72,4 +75,41 @@ static inline String SiaCurrencyToString(const SiaCurrency& value)
|
||||
return value.ToWString(conv);
|
||||
}
|
||||
|
||||
class IHost
|
||||
{
|
||||
public:
|
||||
IHost() {}
|
||||
virtual ~IHost() {}
|
||||
|
||||
public:
|
||||
virtual Hastings GetStoragePrice() const = 0;
|
||||
virtual Hastings GetDownloadPrice() const = 0;
|
||||
virtual Hastings GetUploadPrice() const = 0;
|
||||
};
|
||||
|
||||
template<typename T, typename R>
|
||||
R CalculateAveragePrice(const std::vector<T>& v, std::function<R(const T& t)> PriceGetter)
|
||||
{
|
||||
R result = v.size() ? std::accumulate(std::next(v.begin()), v.end(), PriceGetter(v[0]), [&](const R& a, const T& b) {
|
||||
return a + PriceGetter(b);
|
||||
}).Div(v.size()) : 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Hastings CalculateAverageHostPrice(const std::vector<IHost>& hosts)
|
||||
{
|
||||
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetStoragePrice(); });
|
||||
}
|
||||
|
||||
inline Hastings CalculateAverageDownloadPrice(const std::vector<IHost>& hosts)
|
||||
{
|
||||
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetDownloadPrice(); });
|
||||
}
|
||||
|
||||
inline Hastings CalculateAverageUploadPrice(const std::vector<IHost>& hosts)
|
||||
{
|
||||
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetUploadPrice(); });
|
||||
}
|
||||
|
||||
NS_END(2)
|
@@ -24,7 +24,7 @@ void CSiaApi::_CSiaConsensus::StartRefreshThread()
|
||||
do
|
||||
{
|
||||
json result;
|
||||
if (API_SUCCESS(SiaCurlError, _siaCurl.Get(L"/consensus", result)))
|
||||
if (ApiSuccess(_siaCurl.Get(L"/consensus", result)))
|
||||
{
|
||||
SetHeight(result["height"].get<std::uint64_t>());
|
||||
SetSynced(result["synced"].get<bool>());
|
||||
|
@@ -62,7 +62,7 @@ SiaCurlError CSiaCurl::ProcessResponse(const int& res, const std::string& result
|
||||
if (res == CURLE_OK)
|
||||
{
|
||||
ret = CheckHttpError(result);
|
||||
if (API_SUCCESS(SiaCurlError, ret))
|
||||
if (ApiSuccess(ret))
|
||||
{
|
||||
ret = (result.length() ? CheckApiError((response = json::parse(result.c_str()))) : SiaCurlError::Success);
|
||||
}
|
||||
@@ -114,13 +114,13 @@ bool CSiaCurl::CheckVersion(SiaCurlError& error) const
|
||||
}
|
||||
}
|
||||
|
||||
return API_SUCCESS(SiaCurlError, error);
|
||||
return ApiSuccess(error);
|
||||
}
|
||||
|
||||
String CSiaCurl::GetServerVersion() const
|
||||
{
|
||||
json response;
|
||||
if (API_SUCCESS(SiaCurlError, _Get(L"/daemon/version", response)))
|
||||
if (ApiSuccess(_Get(L"/daemon/version", response)))
|
||||
{
|
||||
return String(CA2W(response["version"].get<std::string>().c_str()));
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists
|
||||
{
|
||||
CSiaFileTreePtr siaFileTree;
|
||||
SiaApiError ret = GetFileTree(siaFileTree);
|
||||
if (API_SUCCESS(SiaApiError, ret))
|
||||
if (ApiSuccess(ret))
|
||||
{
|
||||
exists = siaFileTree->FileExists(siaPath);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ SiaApiError CSiaApi::_CSiaRenter::GetFileTree(CSiaFileTreePtr& siaFileTree) cons
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
siaFileTree.reset(new CSiaFileTree(_siaCurl));
|
||||
json result;
|
||||
if (API_SUCCESS(SiaCurlError, _siaCurl.Get(L"/renter/files", result)))
|
||||
if (ApiSuccess(_siaCurl.Get(L"/renter/files", result)))
|
||||
{
|
||||
siaFileTree->BuildTree(result);
|
||||
ret = SiaApiError::Success;
|
||||
|
@@ -38,7 +38,7 @@ bool CSiaApi::_CSiaWallet::Refresh()
|
||||
{
|
||||
json result;
|
||||
SiaCurlError error = _siaCurl.Get(L"/wallet", result);
|
||||
if (API_SUCCESS(SiaCurlError, error))
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
SetCreated(result["encrypted"].get<bool>());
|
||||
SetLocked(!result["unlocked"].get<bool>());
|
||||
@@ -60,7 +60,7 @@ SiaApiError CSiaApi::_CSiaWallet::Create(const SiaSeedLanguage& seedLanguage, St
|
||||
error = SiaApiError::RequestError;
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/init", { {L"dictionary", SeedLangToString(seedLanguage)} }, result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
error = SiaApiError::Success;
|
||||
seed = CA2W(result["primaryseed"].get<std::string>().c_str());
|
||||
@@ -82,13 +82,13 @@ SiaApiError CSiaApi::_CSiaWallet::Restore(const String& seed)
|
||||
SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
{
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::WalletLocked : SiaApiError::Success) : SiaApiError::WalletNotCreated;
|
||||
if (API_SUCCESS(SiaApiError, error))
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/lock", {}, result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
Refresh();
|
||||
error = SiaApiError::Success;
|
||||
@@ -101,13 +101,13 @@ SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
SiaApiError CSiaApi::_CSiaWallet::Unlock(const String& password)
|
||||
{
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::Success : SiaApiError::WalletUnlocked) : SiaApiError::WalletNotCreated;
|
||||
if (API_SUCCESS(SiaApiError, error))
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
Refresh();
|
||||
error = SiaApiError::Success;
|
||||
@@ -135,7 +135,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetConfirmedBalance(SiaCurrency& balance) cons
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet", result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
balance = HastingsStringToSiaCurrency(String(CA2W(result["confirmedsiacoinbalance"].get<std::string>().c_str())));
|
||||
ret = SiaApiError::Success;
|
||||
@@ -151,7 +151,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetUnonfirmedBalance(SiaCurrency& balance) con
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet", result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
balance = HastingsStringToSiaCurrency(String(CA2W(result["unconfirmedincomingsiacoins"].get<std::string>().c_str())));
|
||||
ret = SiaApiError::Success;
|
||||
@@ -167,7 +167,7 @@ SiaApiError CSiaApi::_CSiaWallet::GetAddress(String& address) const
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = _siaCurl.Get(L"/wallet/address", result);
|
||||
if (API_SUCCESS(SiaCurlError, cerror))
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
address = CA2W(result["address"].get<std::string>().c_str());
|
||||
ret = SiaApiError::Success;
|
||||
|
Reference in New Issue
Block a user