From e44be4f1f47849052e08c2e945caaea0fc9aa811 Mon Sep 17 00:00:00 2001 From: Daira-Emma Hopwood Date: Fri, 4 Oct 2024 10:48:50 +0100 Subject: [PATCH 1/3] Funding streams: Rename INSUFFICIENT_ADDRESSES to INSUFFICIENT_RECIPIENTS. Signed-off-by: Daira-Emma Hopwood --- src/consensus/params.cpp | 6 +++--- src/consensus/params.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/consensus/params.cpp b/src/consensus/params.cpp index f8bd2df713..b048bc24ac 100644 --- a/src/consensus/params.cpp +++ b/src/consensus/params.cpp @@ -194,7 +194,7 @@ namespace Consensus { const auto expectedRecipients = params.FundingPeriodIndex(startHeight, endHeight - 1) + 1; if (expectedRecipients > recipients.size()) { - return FundingStreamError::INSUFFICIENT_ADDRESSES; + return FundingStreamError::INSUFFICIENT_RECIPIENTS; } // Lockbox output periods must not start before NU6 @@ -221,8 +221,8 @@ namespace Consensus { throw std::runtime_error("Canopy network upgrade not active at funding stream start height."); case FundingStreamError::ILLEGAL_RANGE: throw std::runtime_error("Illegal start/end height combination for funding stream."); - case FundingStreamError::INSUFFICIENT_ADDRESSES: - throw std::runtime_error("Insufficient payment addresses to fully exhaust funding stream."); + case FundingStreamError::INSUFFICIENT_RECIPIENTS: + throw std::runtime_error("Insufficient recipient identifiers to fully exhaust funding stream."); case FundingStreamError::NU6_NOT_ACTIVE: throw std::runtime_error("NU6 network upgrade not active at lockbox period start height."); default: diff --git a/src/consensus/params.h b/src/consensus/params.h index 62eee52a36..28567eb071 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -134,7 +134,7 @@ extern const struct FSInfo FundingStreamInfo[]; enum FundingStreamError { CANOPY_NOT_ACTIVE, ILLEGAL_RANGE, - INSUFFICIENT_ADDRESSES, + INSUFFICIENT_RECIPIENTS, NU6_NOT_ACTIVE, }; From d60c51eb8d8e52c83fac4e64f6ce515397d27fa4 Mon Sep 17 00:00:00 2001 From: Daira-Emma Hopwood Date: Fri, 4 Oct 2024 12:32:13 +0100 Subject: [PATCH 2/3] sapling::Builder: add funding stream outputs in a more predictable order. Previously it was deterministic, but depended on details of the `operator<` implementations of `FundingStreamRecipient`, `SaplingPaymentAddress`, and `CScript`. Now it is just the same order as in `vFundingStreams`. Signed-off-by: Daira-Emma Hopwood --- src/miner.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/miner.cpp b/src/miner.cpp index 84a41a1d6d..ca040adefb 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -115,39 +115,38 @@ class AddOutputsToCoinbaseTxAndSign } CAmount SetFoundersRewardAndGetMinerValue(sapling::Builder& saplingBuilder) const { - auto block_subsidy = chainparams.GetConsensus().GetBlockSubsidy(nHeight); + const auto& consensus = chainparams.GetConsensus(); + const auto block_subsidy = consensus.GetBlockSubsidy(nHeight); auto miner_reward = block_subsidy; // founders' reward or funding stream amounts will be subtracted below if (nHeight > 0) { if (chainparams.GetConsensus().NetworkUpgradeActive(nHeight, Consensus::UPGRADE_CANOPY)) { - auto fundingStreamElements = chainparams.GetConsensus().GetActiveFundingStreamElements( - nHeight, - block_subsidy); - LogPrint("pow", "%s: Constructing funding stream outputs for height %d", __func__, nHeight); - for (Consensus::FundingStreamElement fselem : fundingStreamElements) { - miner_reward -= fselem.second; - examine(fselem.first, match { + for (const auto& [fsinfo, fs] : consensus.GetActiveFundingStreams(nHeight)) { + const auto amount = fsinfo.Value(block_subsidy); + miner_reward -= amount; + + examine(fs.Recipient(consensus, nHeight), match { [&](const libzcash::SaplingPaymentAddress& pa) { - LogPrint("pow", "%s: Adding Sapling funding stream output of value %d", __func__, fselem.second); + LogPrint("pow", "%s: Adding Sapling funding stream output of value %d", __func__, amount); saplingBuilder.add_recipient( {}, pa.GetRawBytes(), - fselem.second, + amount, libzcash::Memo::ToBytes(std::nullopt)); }, [&](const CScript& scriptPubKey) { - LogPrint("pow", "%s: Adding transparent funding stream output of value %d", __func__, fselem.second); - mtx.vout.emplace_back(fselem.second, scriptPubKey); + LogPrint("pow", "%s: Adding transparent funding stream output of value %d", __func__, amount); + mtx.vout.emplace_back(amount, scriptPubKey); }, [&](const Consensus::Lockbox& lockbox) { - LogPrint("pow", "%s: Noting lockbox output of value %d", __func__, fselem.second); + LogPrint("pow", "%s: Noting lockbox output of value %d", __func__, amount); } }); } } else if (nHeight <= chainparams.GetConsensus().GetLastFoundersRewardBlockHeight(nHeight)) { // Founders reward is 20% of the block subsidy - auto vFoundersReward = miner_reward / 5; + const auto vFoundersReward = miner_reward / 5; // Take some reward away from us miner_reward -= vFoundersReward; // And give it to the founders From 4ddd3405c58165dcea6aadf2a784c939e54f9cf7 Mon Sep 17 00:00:00 2001 From: Daira-Emma Hopwood Date: Fri, 4 Oct 2024 12:51:36 +0100 Subject: [PATCH 3/3] Simplify `GetActiveFundingStreamElements`. Signed-off-by: Daira-Emma Hopwood --- src/consensus/params.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/consensus/params.cpp b/src/consensus/params.cpp index b048bc24ac..a0d447e73d 100644 --- a/src/consensus/params.cpp +++ b/src/consensus/params.cpp @@ -349,7 +349,11 @@ namespace Consensus { // Funding streams are disabled if Canopy is not active. if (NetworkUpgradeActive(nHeight, Consensus::UPGRADE_CANOPY)) { for (uint32_t idx = Consensus::FIRST_FUNDING_STREAM; idx < Consensus::MAX_FUNDING_STREAMS; idx++) { + // The following indexed access is safe as Consensus::MAX_FUNDING_STREAMS is used + // in the definition of vFundingStreams. auto fs = vFundingStreams[idx]; + + // Funding period is [startHeight, endHeight). if (fs && nHeight >= fs.value().GetStartHeight() && nHeight < fs.value().GetEndHeight()) { activeStreams.push_back(std::make_pair(FundingStreamInfo[idx], fs.value())); } @@ -372,16 +376,10 @@ namespace Consensus { // Funding streams are disabled if Canopy is not active. if (NetworkUpgradeActive(nHeight, Consensus::UPGRADE_CANOPY)) { - for (uint32_t idx = Consensus::FIRST_FUNDING_STREAM; idx < Consensus::MAX_FUNDING_STREAMS; idx++) { - // The following indexed access is safe as Consensus::MAX_FUNDING_STREAMS is used - // in the definition of vFundingStreams. - auto fs = vFundingStreams[idx]; - // Funding period is [startHeight, endHeight) - if (fs && nHeight >= fs.value().GetStartHeight() && nHeight < fs.value().GetEndHeight()) { - requiredElements.insert(std::make_pair( - fs.value().Recipient(*this, nHeight), - FundingStreamInfo[idx].Value(blockSubsidy))); - } + for (const auto& [fsinfo, fs] : GetActiveFundingStreams(nHeight)) { + requiredElements.insert(std::make_pair( + fs.Recipient(*this, nHeight), + fsinfo.Value(blockSubsidy))); } }