From 4f1c37980e13167f01cd6fdc07b201bf4dabbf52 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Mon, 4 Jan 2016 07:30:53 -0700 Subject: [PATCH] Load proving/verifying keys at startup from the public alpha parameters file. --- src/init.cpp | 43 +++++++++++++++++++++++++++++++++ src/init.h | 3 +++ src/test/test_bitcoin.cpp | 1 + src/util.cpp | 51 +++++++++++++++++++++++++++++++++++++++ src/util.h | 2 ++ 5 files changed, 100 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index a04e4e0a..ff47a843 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -49,6 +49,8 @@ using namespace std; +libzerocash::ZerocashParams *pzerocashParams = NULL; + #ifdef ENABLE_WALLET CWallet* pwalletMain = NULL; #endif @@ -591,6 +593,44 @@ bool InitSanityCheck(void) return true; } + +static void ZC_LoadParams() +{ + struct timeval tv_start, tv_end; + float elapsed; + + boost::filesystem::path pk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-proving.key"; + boost::filesystem::path vk_path = ZC_GetParamsDir() / "zc-testnet-public-alpha-verification.key"; + + LogPrintf("Loading proving key from %s\n", pk_path.string().c_str()); + gettimeofday(&tv_start, 0); + libzerocash::ZerocashParams::zerocash_pp::init_public_params(); + auto pk_loaded = libzerocash::ZerocashParams::LoadProvingKeyFromFile( + pk_path.string(), + INCREMENTAL_MERKLE_TREE_DEPTH + ); + gettimeofday(&tv_end, 0); + elapsed = float(tv_end.tv_sec-tv_start.tv_sec) + (tv_end.tv_usec-tv_start.tv_usec)/float(1000000); + LogPrintf("Loaded proving key in %fs seconds.\n", elapsed); + + + LogPrintf("Loading verification key from %s\n", vk_path.string().c_str()); + gettimeofday(&tv_start, 0); + auto vk_loaded = libzerocash::ZerocashParams::LoadVerificationKeyFromFile( + vk_path.string(), + INCREMENTAL_MERKLE_TREE_DEPTH + ); + gettimeofday(&tv_end, 0); + elapsed = float(tv_end.tv_sec-tv_start.tv_sec) + (tv_end.tv_usec-tv_start.tv_usec)/float(1000000); + LogPrintf("Loaded verification key in %fs seconds.\n", elapsed); + + pzerocashParams = new libzerocash::ZerocashParams( + INCREMENTAL_MERKLE_TREE_DEPTH, + &pk_loaded, + &vk_loaded + ); +} + /** Initialize bitcoin. * @pre Parameters should be parsed and config file should be read. */ @@ -1223,6 +1263,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) mempool.ReadFeeEstimates(est_filein); fFeeEstimatesInitialized = true; + // ********************************************************* Step 7i: Load zcash params + ZC_LoadParams(); + // ********************************************************* Step 8: load wallet #ifdef ENABLE_WALLET if (fDisableWallet) { diff --git a/src/init.h b/src/init.h index dcb2b293..0dd6da7e 100644 --- a/src/init.h +++ b/src/init.h @@ -8,6 +8,8 @@ #include +#include "libzerocash/ZerocashParams.h" + class CScheduler; class CWallet; @@ -17,6 +19,7 @@ class thread_group; } // namespace boost extern CWallet* pwalletMain; +extern libzerocash::ZerocashParams* pzerocashParams; void StartShutdown(); bool ShutdownRequested(); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index c727303e..fa322dcd 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -23,6 +23,7 @@ CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h CWallet* pwalletMain; +libzerocash::ZerocashParams *pzerocashParams; extern bool fPrintToConsole; extern void noui_connect(); diff --git a/src/util.cpp b/src/util.cpp index 4fc2d339..b4d3fa6b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -430,8 +430,59 @@ boost::filesystem::path GetDefaultDataDir() static boost::filesystem::path pathCached; static boost::filesystem::path pathCachedNetSpecific; +static boost::filesystem::path zc_paramsPathCached; static CCriticalSection csPathCached; +static boost::filesystem::path ZC_GetBaseParamsDir() +{ + // Copied from GetDefaultDataDir and adapter for zcash params. + + namespace fs = boost::filesystem; + // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams + // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams + // Mac: ~/Library/Application Support/ZcashParams + // Unix: ~/.zcash-params +#ifdef WIN32 + // Windows + return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams"; +#else + fs::path pathRet; + char* pszHome = getenv("HOME"); + if (pszHome == NULL || strlen(pszHome) == 0) + pathRet = fs::path("/"); + else + pathRet = fs::path(pszHome); +#ifdef MAC_OSX + // Mac + pathRet /= "Library/Application Support"; + TryCreateDirectory(pathRet); + return pathRet / "ZcashParams"; +#else + // Unix + return pathRet / ".zcash-params"; +#endif +#endif +} + +const boost::filesystem::path &ZC_GetParamsDir() +{ + namespace fs = boost::filesystem; + + LOCK(csPathCached); // Reuse the same lock as upstream. + + fs::path &path = zc_paramsPathCached; + + // This can be called during exceptions by LogPrintf(), so we cache the + // value so we don't have to do memory allocations after that. + if (!path.empty()) + return path; + + path = ZC_GetBaseParamsDir(); + path /= BaseParams().DataDir(); + + return path; +} + const boost::filesystem::path &GetDataDir(bool fNetSpecific) { namespace fs = boost::filesystem; diff --git a/src/util.h b/src/util.h index 4cc0faf4..336e2b1b 100644 --- a/src/util.h +++ b/src/util.h @@ -104,6 +104,8 @@ static inline bool error(const char* format) return false; } +const boost::filesystem::path &ZC_GetParamsDir(); + void PrintExceptionContinue(const std::exception *pex, const char* pszThread); void ParseParameters(int argc, const char*const argv[]); void FileCommit(FILE *fileout);