1
0

More changes

This commit is contained in:
Scott E. Graves
2017-04-10 17:49:44 -05:00
parent 0975ed41d6
commit feadab847e
8 changed files with 230 additions and 41 deletions

View File

@@ -176,6 +176,7 @@ public:
void Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
public:
CSiaError<_SiaApiErrorCode> CalculateEstimatedStorage(const SiaCurrency& funds, SiaCurrency& resultGb) const;
CSiaError<_SiaApiErrorCode> DownloadFile(const SString& siaPath, const SString& location) const;
CSiaError<_SiaApiErrorCode> FileExists(const SString& siaPath, bool& exists) const;
_SiaRenterAllowance GetAllowance() const;
@@ -205,6 +206,52 @@ public:
void Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
};
class SIADRIVE_EXPORTABLE _CSiaHost :
public virtual CSiaBase
{
friend CSiaApi;
private:
explicit _CSiaHost(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig, const json& hostJson);
public:
virtual ~_CSiaHost();
//Properties
Property(bool, AcceptingContracts, public, private)
Property(std::uint64_t, MaxDownloadBatchSize, public, private)
Property(std::uint64_t, MaxDuration, public, private)
Property(std::uint64_t, MaxReviseBatchSize, public, private)
Property(SString, NetAddress, public, private)
Property(std::uint64_t, RemainingStorage, public, private)
Property(std::uint64_t, SectorSize, public, private)
Property(std::uint64_t, TotalStorage, public, private)
Property(SString, UnlockHash, public, private)
Property(std::uint64_t, WindowSize, public, private)
Property(std::pair<SString COMMA SString>, PublicKey, public, private)
};
class SIADRIVE_EXPORTABLE _CSiaHostDb :
public virtual CSiaBase
{
friend CSiaApi;
private:
explicit _CSiaHostDb(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
public:
virtual ~_CSiaHostDb();
private:
std::shared_ptr<std::vector<std::shared_ptr<_CSiaHost>>> _activeHosts;
private:
void Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
public:
std::shared_ptr<std::vector<std::shared_ptr<_CSiaHost>>> GetActiveHosts() const;
};
public:
explicit CSiaApi(const SiaHostConfig& hostConfig, CSiaDriveConfig* siaDriveConfig);
@@ -217,7 +264,8 @@ private:
CSiaDriveConfig* _siaDriveConfig;
std::shared_ptr<_CSiaWallet> _wallet;
std::shared_ptr<_CSiaRenter> _renter;
std::shared_ptr<_CSiaConsensus> _consensus;
std::shared_ptr<_CSiaConsensus> _consensus;
std::shared_ptr<_CSiaHostDb> _hostDb;
std::unique_ptr<CAutoThread> _refreshThread;
private:
@@ -229,7 +277,8 @@ public:
public:
std::shared_ptr<_CSiaWallet> GetWallet() const;
std::shared_ptr<_CSiaRenter> GetRenter() const;
std::shared_ptr<_CSiaConsensus> GetConsensus() const;
std::shared_ptr<_CSiaConsensus> GetConsensus() const;
std::shared_ptr<_CSiaHostDb> GetHostDb() const;
SString GetServerVersion() const;
SiaHostConfig GetHostConfig() const;
};
@@ -241,9 +290,15 @@ typedef CSiaApi::_CSiaWallet CSiaWallet;
typedef CSiaApi::_CSiaRenter CSiaRenter;
typedef CSiaRenter::_SiaRenterAllowance SiaRenterAllowance;
typedef CSiaApi::_CSiaConsensus CSiaConsensus;
typedef CSiaApi::_CSiaHostDb CSiaHostDb;
typedef CSiaApi::_CSiaHost CSiaHost;
typedef std::shared_ptr<CSiaHostDb> CSiaHostDbPtr;
typedef std::shared_ptr<CSiaWallet> CSiaWalletPtr;
typedef std::shared_ptr<CSiaRenter> CSiaRenterPtr;
typedef std::shared_ptr<CSiaConsensus> CSiaConsensusPtr;
typedef std::shared_ptr<CSiaHost> CSiaHostPtr;
typedef std::vector<CSiaHostPtr> CSiaHostCollection;
typedef std::shared_ptr<CSiaHostCollection> CSiaHostCollectionPtr;
typedef CSiaApi::_CSiaFile CSiaFile;
typedef std::shared_ptr<CSiaFile> CSiaFilePtr;
typedef std::vector<CSiaFilePtr> CSiaFileCollection;

View File

@@ -30,8 +30,8 @@
#define _WIN32_WINNT_WIN10 0x0A00 // Windows 10
// Windows 8.1 or above supported
#define WINVER _WIN32_WINNT_WINBLUE
#define _WIN32_WINNT _WIN32_WINNT_WINBLUE
#define WINVER _WIN32_WINNT_WIN7
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#define _WINSOCKAPI_
#include <Windows.h>
#include <Shlwapi.h>
@@ -68,6 +68,8 @@ using json = nlohmann::json;
#define NS_END(c) NS_END##c()
#define COMMA ,
NS_BEGIN(Sia)
NS_BEGIN(Api)
@@ -87,7 +89,7 @@ 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 = 50;
const std::uint32_t SIA_DEFAULT_HOST_COUNT = 24;
const std::uint32_t SIA_DEFAULT_RENEW_WINDOW = ((1440 / SIA_BLOCK_TIME_MINS) * 14);
const SiaCurrency SIA_DEFAULT_MINIMUM_FUNDS = 4000;
const std::uint32_t SIA_DEFAULT_CONTRACT_LENGTH = SIA_BLOCKS_PER_MONTH * 3;
@@ -131,7 +133,7 @@ public:
public:
Property(T, Code, public, private)
Property(SString, Reason, public, private)
Property(SString, Reason, public, private)
public:
operator bool() { return GetCode() == T::Success; }
@@ -189,41 +191,16 @@ inline static SString SiaCurrencyToGB(const SiaCurrency& value)
return value.ToWString(conv);
}
class IHost
inline static Hastings SiaCurrencyToHastings(const SiaCurrency& currency)
{
public:
IHost() {}
virtual ~IHost() {}
ttmath::Parser<SiaCurrency> parser;
parser.Parse(currency.ToString() + " * (10 ^ 24)");
public:
virtual Hastings GetStoragePrice() const = 0;
virtual Hastings GetDownloadPrice() const = 0;
virtual Hastings GetUploadPrice() const = 0;
};
template<typename T, typename R>
inline static 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 static Hastings CalculateAverageHostPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetStoragePrice(); });
}
inline static Hastings CalculateAverageDownloadPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetDownloadPrice(); });
}
inline static Hastings CalculateAverageUploadPrice(const std::vector<IHost>& hosts)
{
return CalculateAveragePrice<IHost, Hastings>(hosts, [](const IHost& host)->Hastings { return host.GetUploadPrice(); });
ttmath::Conv conv;
conv.scient_from = 256;
conv.base = 10;
conv.round = 0;
return parser.stack[0].value.ToString(conv);
}
BOOL SIADRIVE_EXPORTABLE RetryAction(std::function<BOOL()> func, std::uint16_t retryCount, const DWORD& retryDelay);