Merge branch 'duke' into delete_verus

This commit is contained in:
Jonathan "Duke" Leto
2019-11-22 08:13:51 -08:00
6 changed files with 151 additions and 90 deletions

View File

@@ -1,4 +1,5 @@
// Copyright (c) 2012-2014 The Bitcoin Core developers
// Copyright (c) 2019 The Hush developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -333,13 +334,6 @@ void CCoinsViewCache::PopAnchor(const uint256 &newrt, ShieldedType type) {
}
void CCoinsViewCache::SetNullifiers(const CTransaction& tx, bool spent) {
for (const JSDescription &joinsplit : tx.vjoinsplit) {
for (const uint256 &nullifier : joinsplit.nullifiers) {
std::pair<CNullifiersMap::iterator, bool> ret = cacheSproutNullifiers.insert(std::make_pair(nullifier, CNullifiersCacheEntry()));
ret.first->second.entered = spent;
ret.first->second.flags |= CNullifiersCacheEntry::DIRTY;
}
}
for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
std::pair<CNullifiersMap::iterator, bool> ret = cacheSaplingNullifiers.insert(std::make_pair(spendDescription.nullifier, CNullifiersCacheEntry()));
ret.first->second.entered = spent;
@@ -622,41 +616,15 @@ CAmount CCoinsViewCache::GetValueIn(int32_t nHeight,int64_t *interestp,const CTr
bool CCoinsViewCache::HaveJoinSplitRequirements(const CTransaction& tx) const
{
boost::unordered_map<uint256, SproutMerkleTree, CCoinsKeyHasher> intermediates;
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit)
{
BOOST_FOREACH(const uint256& nullifier, joinsplit.nullifiers)
{
if (GetNullifier(nullifier, SPROUT)) {
// If the nullifier is set, this transaction
// double-spends!
return false;
}
}
SproutMerkleTree tree;
auto it = intermediates.find(joinsplit.anchor);
if (it != intermediates.end()) {
tree = it->second;
} else if (!GetSproutAnchorAt(joinsplit.anchor, tree)) {
return false;
}
BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
{
tree.append(commitment);
}
intermediates.insert(std::make_pair(tree.root(), tree));
}
for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
if (GetNullifier(spendDescription.nullifier, SAPLING)) // Prevent double spends
if (GetNullifier(spendDescription.nullifier, SAPLING)) { // Prevent double spends
fprintf(stderr,"%s: sapling nullifier %s exists, preventing double spend\n", __FUNCTION__, spendDescription.nullifier);
return false;
}
SaplingMerkleTree tree;
if (!GetSaplingAnchorAt(spendDescription.anchor, tree)) {
fprintf(stderr,"%s: missing sapling anchor: %s \n", __FUNCTION__, spendDescription.anchor);
return false;
}
}

View File

@@ -775,14 +775,23 @@ bool InitSanityCheck(void)
void NoParamsShutdown(void)
{
//TODO: error message incorrect about location
fprintf(stderr,"%s: no params!\n", __FUNCTION__);
LogPrintf("Could not find Sapling params anywhere! Exiting...");
fprintf(stderr,"%s: no params!\n", __FUNCTION__);
LogPrintf("Could not find valid Sapling params anywhere! Exiting...");
uiInterface.ThreadSafeMessageBox(strprintf(
_("Cannot find the Sapling network parameters in the following directory:\n"
"%s\n"
"Please run join our Discord for help: https://myhush.org/discord/"),
ZC_GetParamsDir()),
_("Cannot find the Sapling network parameters! Something is very wrong.\n"
"Please join our Discord for help: https://myhush.org/discord/")),
"", CClientUIInterface::MSG_ERROR);
StartShutdown();
return;
}
void CorruptParamsShutdown(void)
{
fprintf(stderr,"%s: corrupt params!\n", __FUNCTION__);
LogPrintf("We detected corrupt Sapling params! Exiting...");
uiInterface.ThreadSafeMessageBox(strprintf(
_("Corrupt Sapling network parameters were detected! Something is very wrong.\n"
"Please join our Discord for help: https://myhush.org/discord/")),
"", CClientUIInterface::MSG_ERROR);
StartShutdown();
return;
@@ -801,16 +810,19 @@ static void ZC_LoadParams(
float elapsed;
bool found = false;
// Some people have previous partial downloads of zcash params, so check that last
// Sapling Param Search path: . /usr/share/hush .. ../hush3 ~/.zcash-params
// Some people have previous partial downloads of zcash params, so check that last
// Sapling Param Search path: . /usr/share/hush .. ../hush3 ./Contents/MacOS/ ~/.zcash-params
LogPrintf("Looking for sapling params...");
gettimeofday(&tv_start, 0);
// PWD
boost::filesystem::path sapling_spend = "sapling-spend.params";
boost::filesystem::path sapling_output = "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in .\n");
boost::filesystem::path sapling_spend = "sapling-spend.params";
boost::filesystem::path sapling_output = "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in .\n");
found = true;
}
}
if (!found) {
// Debian global install dir: /usr/share/hush
@@ -824,33 +836,43 @@ static void ZC_LoadParams(
if (!found) {
// Try ..
sapling_spend = boost::filesystem::path("..") / "sapling-spend.params";
sapling_spend = boost::filesystem::path("..") / "sapling-spend.params";
sapling_output = boost::filesystem::path("..") / "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in ..\n");
found = true;
}
}
}
if (!found) {
// This will catch the case of any external software (i.e. GUI wallets) needing params and installed in same dir as hush3.git
sapling_spend = boost::filesystem::path("..") / "hush3" / "sapling-spend.params";
sapling_spend = boost::filesystem::path("..") / "hush3" / "sapling-spend.params";
sapling_output = boost::filesystem::path("..") / "hush3" / "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in ../hush3\n");
found = true;
}
}
}
if (!found) {
// DMG Support: Apple just has to do things differently...
sapling_spend = boost::filesystem::path("Contents/MacOS") / "hush3" / "sapling-spend.params";
sapling_output = boost::filesystem::path("Contents/MacOS") / "hush3" / "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in ../Contents/MacOS\n");
found = true;
}
}
if (!found) {
// The traditional place Zcash params are stored, should not hit this case in normal circumstances,
// as Hush packages sapling params now
sapling_spend = ZC_GetParamsDir() / "sapling-spend.params";
sapling_spend = ZC_GetParamsDir() / "sapling-spend.params";
sapling_output = ZC_GetParamsDir() / "sapling-output.params";
if (files_exist(sapling_spend, sapling_output)) {
if (files_exist(sapling_spend, sapling_output)) {
fprintf(stderr,"Found sapling params in ~/.zcash\n");
found = true;
}
}
}
if (!found) {
@@ -859,20 +881,36 @@ static void ZC_LoadParams(
return;
}
boost::system::error_code ec1, ec2;
boost::uintmax_t spend_size = file_size(sapling_spend, ec1);
boost::uintmax_t output_size = file_size(sapling_output, ec2);
fprintf(stderr,"Sapling spend: %d bytes, output: %d bytes\n", spend_size, output_size);
// We could check sha hashes, but we mostly want to detect on-disk file corruption
// or people having a full harddrive. Full validation happens in librustzcash_init_zksnark_params
// This prevents users seeing very low-level errors from that routine
boost::uintmax_t spend_valid = 47958396;
boost::uintmax_t output_valid = 3592860;
//TODO: passing the exact reason for corruption to GUI
if (spend_size != spend_valid) {
fprintf(stderr,"Sapling spend %d bytes != %d is invalid!\n", spend_size, spend_valid);
CorruptParamsShutdown();
return;
}
//LogPrintf("Loading verifying key from %s\n", vk_path.string().c_str());
gettimeofday(&tv_start, 0);
//pzcashParams = ZCJoinSplit::Prepared(vk_path.string(), pk_path.string());
if (output_size != output_valid) {
fprintf(stderr,"Sapling ouput %d bytes != %d is invalid!\n", output_size, output_valid);
CorruptParamsShutdown();
return;
}
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 verifying key in %fs seconds.\n", elapsed);
LogPrintf("Found sapling param in %fs seconds.\n", elapsed);
static_assert( sizeof(boost::filesystem::path::value_type) == sizeof(codeunit), "librustzcash not configured correctly");
auto sapling_spend_str = sapling_spend.native();
auto sapling_spend_str = sapling_spend.native();
auto sapling_output_str = sapling_output.native();
LogPrintf("Loading Sapling (Spend) parameters from %s\n", sapling_spend.string().c_str());
@@ -886,7 +924,7 @@ static void ZC_LoadParams(
reinterpret_cast<const codeunit*>(sapling_output_str.c_str()),
sapling_output_str.length(),
"657e3d38dbb5cb5e7dd2970e8b03d69b4787dd907285b5a7f0790dcc8072f60bf593b32cc2d1c030e00ff5ae64bf84c5c3beb84ddc841d48264b4a171744d028",
// These are dummy arguments, ignored by Hush-flavored librustzcash
// These are dummy arguments, ignored by Hush-flavored librustzcash
reinterpret_cast<const codeunit*>(sapling_output_str.c_str()),
sapling_output_str.length(),
"657e3d38dbb5cb5e7dd2970e8b03d69b4787dd907285b5a7f0790dcc8072f60bf593b32cc2d1c030e00ff5ae64bf84c5c3beb84ddc841d48264b4a171744d028"

View File

@@ -1160,6 +1160,10 @@ bool ContextualCheckTransaction(int32_t slowflag,const CBlock *block, CBlockInde
if (tx.fOverwintered && tx.nVersionGroupId != SAPLING_VERSION_GROUP_ID)
{
//return state.DoS(dosLevel, error("CheckTransaction(): invalid Sapling tx version"),REJECT_INVALID, "bad-sapling-tx-version-group-id");
{
string strHex = EncodeHexTx(tx);
fprintf(stderr,"invalid Sapling rawtx.%s\n",strHex.c_str());
}
return state.DoS(isInitBlockDownload() ? 0 : dosLevel,
error("CheckTransaction(): invalid Sapling tx version"),
REJECT_INVALID, "bad-sapling-tx-version-group-id");
@@ -1774,8 +1778,9 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
return error("AcceptToMemoryPool: CheckTransaction failed");
}
// DoS level set to 10 to be more forgiving.
// Check transaction contextually against the set of consensus rules which apply in the next block to be mined.
if (!fSkipExpiry && !ContextualCheckTransaction(0,0,0,tx, state, nextBlockHeight, (dosLevel == -1) ? 10 : dosLevel,0))
if (!fSkipExpiry && !ContextualCheckTransaction(0,0,0,tx, state, nextBlockHeight, (dosLevel == -1) ? 10 : dosLevel))
{
return error("AcceptToMemoryPool: ContextualCheckTransaction failed");
}

View File

@@ -267,7 +267,7 @@ struct timeval MillisToTimeval(int64_t nTimeout)
*
* @note This function requires that hSocket is in non-blocking mode.
*/
bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket)
bool static InterruptibleRecv(uint8_t* data, size_t len, int timeout, SOCKET& hSocket)
{
int64_t curTime = GetTimeMillis();
int64_t endTime = curTime + timeout;
@@ -275,7 +275,11 @@ bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSock
// to break off in case of an interruption.
const int64_t maxWait = 1000;
while (len > 0 && curTime < endTime) {
#ifdef _WIN32
ssize_t ret = recv(hSocket, (char*)data, len, 0); // Optimistically try the recv first
#else
ssize_t ret = recv(hSocket, data, len, 0); // Optimistically try the recv first
#endif
if (ret > 0) {
len -= ret;
data += ret;
@@ -335,7 +339,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
CloseSocket(hSocket);
return error("Error sending to proxy");
}
char pchRet1[2];
uint8_t pchRet1[2];
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy response");
@@ -360,7 +364,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
return error("Error sending authentication to proxy");
}
LogPrint("proxy", "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
char pchRetA[2];
uint8_t pchRetA[2];
if (!InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy authentication response");
@@ -389,7 +393,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
CloseSocket(hSocket);
return error("Error sending to proxy");
}
char pchRet2[4];
uint8_t pchRet2[4];
if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy response");
@@ -417,7 +421,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
CloseSocket(hSocket);
return error("Error: malformed proxy response");
}
char pchRet3[256];
uint8_t pchRet3[256];
switch (pchRet2[3])
{
case 0x01: ret = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, hSocket); break;
@@ -429,7 +433,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
CloseSocket(hSocket);
return error("Error reading from proxy");
}
int nRecv = pchRet3[0];
size_t nRecv = pchRet3[0];
ret = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
break;
}