Auto merge of #4349 - oxarbitrage:issue3996, r=daira

Prevent creation of shielded transactions when chain is not synced up

Part of zcash/zcash#3996.

Inspired by jl777/komodo#1486 and jl777/komodo#1493, but takes a different approach:

- Uses a function `ThrowIfInitialBlockDownload()` that checks `IsInitialBlockDownload()`, instead of macros.
- Added `initial_block_download_complete` output only to `getblockchaininfo`.
This commit is contained in:
Homu 2020-09-03 16:21:51 +00:00
commit cf8cd09ae1
2 changed files with 17 additions and 2 deletions

View File

@ -995,6 +995,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
"{\n"
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
" \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n"
" \"initial_block_download_complete\": xx, (boolean) true if the initial download of the blockchain is complete\n"
" \"headers\": xxxxxx, (numeric) the current number of headers we have validated\n"
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
" \"difficulty\": xxxxxx, (numeric) the current difficulty\n"
@ -1039,6 +1040,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
UniValue obj(UniValue::VOBJ);
obj.pushKV("chain", Params().NetworkIDString());
obj.pushKV("blocks", (int)chainActive.Height());
obj.pushKV("initial_block_download_complete", !IsInitialBlockDownload(Params()));
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1);
obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex());
obj.pushKV("difficulty", (double)GetNetworkDifficulty());
@ -1048,9 +1050,9 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
obj.pushKV("size_on_disk", CalculateCurrentUsage());
if (IsInitialBlockDownload(Params()))
obj.push_back(Pair("estimatedheight", EstimateNetHeight(Params().GetConsensus(), (int)chainActive.Height(), chainActive.Tip()->GetMedianTimePast())));
obj.pushKV("estimatedheight", EstimateNetHeight(Params().GetConsensus(), (int)chainActive.Height(), chainActive.Tip()->GetMedianTimePast()));
else
obj.push_back(Pair("estimatedheight", (int)chainActive.Height()));
obj.pushKV("estimatedheight", (int)chainActive.Height());
SproutMerkleTree tree;
pcoinsTip->GetSproutAnchorAt(pcoinsTip->GetBestAnchor(SPROUT), tree);

View File

@ -85,6 +85,13 @@ void EnsureWalletIsUnlocked()
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
}
void ThrowIfInitialBlockDownload()
{
if (IsInitialBlockDownload(Params())) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Error: Sending transactions is not supported during initial block download.");
}
}
void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
{
int confirms = wtx.GetDepthInMainChain();
@ -4026,6 +4033,8 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
ThrowIfInitialBlockDownload();
// Check that the from address is valid.
auto fromaddress = params[0].get_str();
bool fromTaddr = false;
@ -4463,6 +4472,8 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
ThrowIfInitialBlockDownload();
// Validate the from address
auto fromaddress = params[0].get_str();
bool isFromWildcard = fromaddress == "*";
@ -4699,6 +4710,8 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
ThrowIfInitialBlockDownload();
bool useAnyUTXO = false;
bool useAnySprout = false;
bool useAnySapling = false;