From 3866592324c5b4609dfa21b1fa28246116432208 Mon Sep 17 00:00:00 2001 From: pedro-at-decenomy Date: Tue, 23 Apr 2024 17:54:35 +0100 Subject: [PATCH 1/8] adds dynamic rewards support --- contrib/pivx-qt.pro | 2 + src/Makefile.am | 2 + src/chainparams.cpp | 14 +- src/consensus/params.h | 5 +- src/consensus/upgrades.cpp | 8 +- src/init.cpp | 12 +- src/main.cpp | 11 +- src/masternode-payments.cpp | 3 +- src/masternode.cpp | 24 +-- src/miner.cpp | 3 +- src/rewards.cpp | 383 ++++++++++++++++++++++++++++++++++++ src/rewards.h | 25 +++ src/test/main_tests.cpp | 11 +- src/wallet/wallet.cpp | 3 +- 14 files changed, 468 insertions(+), 38 deletions(-) create mode 100644 src/rewards.cpp create mode 100644 src/rewards.h diff --git a/contrib/pivx-qt.pro b/contrib/pivx-qt.pro index 0bdfbb13d4..022888f914 100644 --- a/contrib/pivx-qt.pro +++ b/contrib/pivx-qt.pro @@ -107,6 +107,7 @@ HEADERS += src/activemasternode.h \ src/protocol.h \ src/pubkey.h \ src/random.h \ + src/rewards.h \ src/rpcclient.h \ src/rpcprotocol.h \ src/rpcserver.h \ @@ -407,6 +408,7 @@ SOURCES += src/activemasternode.cpp \ src/pubkey.cpp \ src/random.cpp \ src/rest.cpp \ + src/rewards.cpp \ src/rpcblockchain.cpp \ src/rpcclient.cpp \ src/rpcdump.cpp \ diff --git a/src/Makefile.am b/src/Makefile.am index f1a12b25b4..275cbeec50 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -157,6 +157,7 @@ BITCOIN_CORE_H = \ random.h \ reverselock.h \ reverse_iterate.h \ + rewards.h \ rpc/client.h \ rpc/protocol.h \ rpc/server.h \ @@ -241,6 +242,7 @@ libbitcoin_server_a_SOURCES = \ policy/policy.cpp \ pow.cpp \ rest.cpp \ + rewards.cpp \ rpc/blockchain.cpp \ rpc/masternode.cpp \ rpc/mining.cpp \ diff --git a/src/chainparams.cpp b/src/chainparams.cpp index eda2e9b244..5e6d0ac68c 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -174,6 +174,7 @@ class CMainParams : public CChainParams consensus.nTargetTimespanV2 = 30 * 60; consensus.nTargetSpacing = 1 * 60; consensus.nTimeSlotLength = 15; + consensus.nRewardAdjustmentInterval = 7 * 1440; // spork keys consensus.strSporkPubKey = "0371d4e47e0ab43865206e9df7c065d6c68471b154bab3815d99f8380d46c7015f"; @@ -197,6 +198,8 @@ class CMainParams : public CChainParams consensus.vUpgrades[Consensus::UPGRADE_P2PKH_BLOCK_SIGNATURES].nActivationHeight = 1741; consensus.vUpgrades[Consensus::UPGRADE_STAKE_MIN_DEPTH_V2].nActivationHeight = 5001; consensus.vUpgrades[Consensus::UPGRADE_MASTERNODE_RANK_V2].nActivationHeight = 5001; + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_REWARDS].nActivationHeight = Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT; + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_COLLATERALS].nActivationHeight = Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT; consensus.vUpgrades[Consensus::UPGRADE_POS].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_POS_V2].hashActivationBlock = uint256S("0x0"); @@ -206,6 +209,8 @@ class CMainParams : public CChainParams consensus.vUpgrades[Consensus::UPGRADE_P2PKH_BLOCK_SIGNATURES].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_STAKE_MIN_DEPTH_V2].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_MASTERNODE_RANK_V2].hashActivationBlock = uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_REWARDS].hashActivationBlock = uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_COLLATERALS].hashActivationBlock = uint256S("0x0"); /** * The message start string is designed to be unlikely to occur in normal data. @@ -280,6 +285,7 @@ class CTestNetParams : public CMainParams consensus.nTargetTimespanV2 = 30 * 60; consensus.nTargetSpacing = 1 * 60; consensus.nTimeSlotLength = 15; + consensus.nRewardAdjustmentInterval = 60; // spork keys consensus.strSporkPubKey = "04E88BB455E2A04E65FCC41D88CD367E9CCE1F5A409BE94D8C2B4B35D223DED9C8E2F4E061349BA3A38839282508066B6DC4DB72DD432AC4067991E6BF20176127"; @@ -297,14 +303,20 @@ class CTestNetParams : public CMainParams consensus.vUpgrades[Consensus::UPGRADE_TIME_PROTOCOL_V2].nActivationHeight = 1641; consensus.vUpgrades[Consensus::UPGRADE_P2PKH_BLOCK_SIGNATURES].nActivationHeight = 1741; consensus.vUpgrades[Consensus::UPGRADE_STAKE_MIN_DEPTH_V2].nActivationHeight = 1841; + consensus.vUpgrades[Consensus::UPGRADE_MASTERNODE_RANK_V2].nActivationHeight = 1941; + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_REWARDS].nActivationHeight = Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT; + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_COLLATERALS].nActivationHeight = Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT; consensus.vUpgrades[Consensus::UPGRADE_POS].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_POS_V2].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_BIP65].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_STAKE_MODIFIER_V2].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_TIME_PROTOCOL_V2].hashActivationBlock = uint256S("0x0"); - consensus.vUpgrades[Consensus::UPGRADE_P2PKH_BLOCK_SIGNATURES].hashActivationBlock =uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_P2PKH_BLOCK_SIGNATURES].hashActivationBlock = uint256S("0x0"); consensus.vUpgrades[Consensus::UPGRADE_STAKE_MIN_DEPTH_V2].hashActivationBlock = uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_STAKE_MIN_DEPTH_V2].hashActivationBlock = uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_REWARDS].hashActivationBlock = uint256S("0x0"); + consensus.vUpgrades[Consensus::UPGRADE_DYNAMIC_COLLATERALS].hashActivationBlock = uint256S("0x0"); /** * The message start string is designed to be unlikely to occur in normal data. diff --git a/src/consensus/params.h b/src/consensus/params.h index 8fadf4052a..cddb364c37 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -33,7 +33,8 @@ enum UpgradeIndex : uint32_t { UPGRADE_P2PKH_BLOCK_SIGNATURES, UPGRADE_STAKE_MIN_DEPTH_V2, UPGRADE_MASTERNODE_RANK_V2, - UPGRADE_DYNAMIC_COLLATERAL, + UPGRADE_DYNAMIC_REWARDS, + UPGRADE_DYNAMIC_COLLATERALS, // NOTE: Also add new upgrades to NetworkUpgradeInfo in upgrades.cpp UPGRADE_TESTDUMMY, MAX_NETWORK_UPGRADES, @@ -103,6 +104,8 @@ struct Params { int64_t nTargetSpacing; int nTimeSlotLength; + int nRewardAdjustmentInterval; + // burn addresses std::map mBurnAddresses = {}; diff --git a/src/consensus/upgrades.cpp b/src/consensus/upgrades.cpp index e8f53572ed..e08c5a1170 100644 --- a/src/consensus/upgrades.cpp +++ b/src/consensus/upgrades.cpp @@ -53,8 +53,12 @@ const struct NUInfo NetworkUpgradeInfo[Consensus::MAX_NETWORK_UPGRADES] = { /*.strInfo =*/ "new masternode ranking system", }, { - /*.strName =*/ "dynamic_collateral_v2", - /*.strInfo =*/ "new dynamic collateral system", + /*.strName =*/ "dynamic_rewards", + /*.strInfo =*/ "dynamic reward system", + }, + { + /*.strName =*/ "dynamic_collaterals", + /*.strInfo =*/ "dynamic collateral system", }, { /*.strName =*/ "Test_dummy", diff --git a/src/init.cpp b/src/init.cpp index 5d6c2f9a6f..e37686e3c5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -36,6 +36,7 @@ #include "netbase.h" #include "net.h" #include "policy/policy.h" +#include "rewards.h" #include "rpc/server.h" #include "script/standard.h" #include "scheduler.h" @@ -238,6 +239,9 @@ void PrepareShutdown() { LOCK(cs_main); + + CRewards::Shutdown(); + if (pcoinsTip != NULL) { FlushStateToDisk(); @@ -1084,6 +1088,12 @@ bool AppInit2() // ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log + fReindex = GetBoolArg("-reindex", false); + + // Initialize dynamic rewards + if(!CRewards::Init(fReindex)) + return false; + // Initialize elliptic curve code RandomInit(); ECC_Start(); @@ -1460,8 +1470,6 @@ bool AppInit2() // ********************************************************* Step 7: load block chain - fReindex = GetBoolArg("-reindex", false); - // Create blocks directory if it doesn't already exist fs::create_directories(GetDataDir() / "blocks"); diff --git a/src/main.cpp b/src/main.cpp index 13ac6fb7dc..797288e63e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,7 @@ #include "policy/policy.h" #include "pow.h" #include "reverse_iterate.h" +#include "rewards.h" #include "spork.h" #include "sporkdb.h" #include "txdb.h" @@ -1971,6 +1972,9 @@ DisconnectResult DisconnectBlock(CBlock& block, CBlockIndex* pindex, CCoinsViewC } } + // Dynamic rewards management + if(!CRewards::DisconnectBlock(pindex)) return DISCONNECT_UNCLEAN; + return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; } @@ -2163,7 +2167,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs - 1), nTimeConnect * 0.000001); //PoW phase redistributed fees to miner. PoS stage destroys fees. - CAmount nExpectedMint = CMasternode::GetBlockValue(pindex->pprev->nHeight + 1); + CAmount nExpectedMint = CRewards::GetBlockValue(pindex->pprev->nHeight + 1); if (block.IsProofOfWork()) nExpectedMint += nFees; @@ -2264,6 +2268,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin } } + // Dynamic rewards management + if(!CRewards::ConnectBlock(pindex, nMint, view)) return false; + return true; } @@ -4110,7 +4117,7 @@ void ResyncSupply() boost::this_thread::interruption_point(); COutPoint key; Coin coin; - if (pcursor->GetKey(key) && pcursor->GetValue(coin)) { + if (pcursor->GetKey(key) && pcursor->GetValue(coin) && !coin.IsSpent()) { // ----------- burn address scanning ----------- CTxDestination source; if (ExtractDestination(coin.out.scriptPubKey, source)) { diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index 787091017c..0deaba7279 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -11,6 +11,7 @@ #include "masternode-sync.h" #include "masternodeman.h" #include "netmessagemaker.h" +#include "rewards.h" #include "spork.h" #include "sync.h" #include "util.h" @@ -327,7 +328,7 @@ void CMasternodePayments::FillBlockPayee(CMutableTransaction& txNew, const CBloc txNew.vout.resize(2); txNew.vout[1].scriptPubKey = payee; txNew.vout[1].nValue = masternodePayment; - txNew.vout[0].nValue = CMasternode::GetBlockValue(pindexPrev->nHeight + 1) - masternodePayment; + txNew.vout[0].nValue = CRewards::GetBlockValue(pindexPrev->nHeight + 1) - masternodePayment; } CTxDestination address1; diff --git a/src/masternode.cpp b/src/masternode.cpp index 6ce68635f8..8993c4da60 100644 --- a/src/masternode.cpp +++ b/src/masternode.cpp @@ -12,6 +12,7 @@ #include "masternode-sync.h" #include "masternodeman.h" #include "netbase.h" +#include "rewards.h" #include "spork.h" #include "sync.h" #include "util.h" @@ -383,32 +384,11 @@ CAmount CMasternode::GetMasternodeNodeCollateral(int nHeight) return 0; } -CAmount CMasternode::GetBlockValue(int nHeight) -{ - CAmount nSubsidy; - - if (nHeight == 1) { - nSubsidy = 30000000 * COIN; // __DSW__ coin supply (30M) - } else if (nHeight <= 100000) { - nSubsidy = 100 * COIN; - } else if (nHeight > 100000 && nHeight <= 200000) { - nSubsidy = 125 * COIN; - } else if (nHeight > 200000 && nHeight <= 300000) { - nSubsidy = 150 * COIN; - } else if (nHeight > 300000 && nHeight <= 400000) { - nSubsidy = 125 * COIN; - } else if (nHeight > 400000) { - nSubsidy = 100 * COIN; - } - - return nSubsidy; -} - CAmount CMasternode::GetMasternodePayment(int nHeight) { if(nHeight <= 5000) return 0; - return CMasternode::GetBlockValue(nHeight) * 95 / 100; + return CRewards::GetBlockValue(nHeight) * 95 / 100; } void CMasternode::InitMasternodeCollateralList() { diff --git a/src/miner.cpp b/src/miner.cpp index eb5cdc5cc1..25b98b1abb 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -21,6 +21,7 @@ #include "pow.h" #include "primitives/block.h" #include "primitives/transaction.h" +#include "rewards.h" #include "timedata.h" #include "util.h" #include "utilmoneystr.h" @@ -114,7 +115,7 @@ bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex txNew.vin[0].scriptSig = CScript() << pindexPrev->nHeight + 1 << OP_0; // If no payee was detected, then the whole block value goes to the first output. if (txNew.vout.size() == 1) { - txNew.vout[0].nValue = CMasternode::GetBlockValue(pindexPrev->nHeight + 1); + txNew.vout[0].nValue = CRewards::GetBlockValue(pindexPrev->nHeight + 1); } pblock->vtx.emplace_back(txNew); diff --git a/src/rewards.cpp b/src/rewards.cpp new file mode 100644 index 0000000000..f70a97c28c --- /dev/null +++ b/src/rewards.cpp @@ -0,0 +1,383 @@ +// Copyright (c) 2014-2015 The Dash developers +// Copyright (c) 2015-2020 The PIVX developers +// Copyright (c) 2021-2024 The DECENOMY Core Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "fs.h" +#include "logging.h" +#include "masternode.h" +#include "masternode-sync.h" +#include "rewards.h" +#include "sqlite3/sqlite3.h" +#include "timedata.h" +#include "utilmoneystr.h" + +#include +#include +#include +#include + +std::unordered_map mDynamicRewards; + +sqlite3* db = nullptr; +sqlite3_stmt* insertStmt = nullptr; +sqlite3_stmt* deleteStmt = nullptr; + +bool CRewards::Init(bool fReindex) +{ + std::ostringstream oss; + auto ok = true; + + try + { + const std::string dirname = (GetDataDir() / "chainstate").string(); + const std::string filename = (GetDataDir() / "chainstate" / "rewards.db").string(); + + // Create the chainstate directory if it doesn't exist + if (!fs::exists(dirname.c_str())) { + // Directory doesn't exist, create it + if (fs::create_directory(dirname.c_str())) { + oss << "CRewards::" << __func__ << " Created directory: " << dirname << std::endl; + } else { + oss << "CRewards::" << __func__ << " Failed to create directory: " << dirname << std::endl; + ok = false; + } + } + + // Delete the database file if it exists when reindexing + if(ok && fReindex) + { + if (auto file = std::fopen(filename.c_str(), "r")) { + std::fclose(file); + // File exists, delete it + if (std::remove(filename.c_str()) == 0) { + oss << "CRewards::" << __func__ << " Deleted existing database file: " << filename << std::endl; + } else { + oss << "CRewards::" << __func__ << " Failed to delete existing database file: " << filename << std::endl; + ok = false; + } + } + } + + if(ok) { // Create and/or open the database + oss << "CRewards::" << __func__ << " Opening database: " << filename << std::endl; + auto rc = sqlite3_open(filename.c_str(), &db); + + if (rc) + { + oss << "CRewards::" << __func__ << " Can't open database: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + } + + if(ok) { // database is open and working + // Create the rewards table if not exists + const auto create_table_query = "CREATE TABLE IF NOT EXISTS rewards (height INT PRIMARY KEY, amount INTEGER)"; + auto rc = sqlite3_exec(db, create_table_query, NULL, NULL, NULL); + + if (rc != SQLITE_OK) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + } + + if(ok) { // Create insert statement + const std::string insertSql = "INSERT OR REPLACE INTO rewards (height, amount) VALUES (?, ?)"; + auto rc = sqlite3_prepare_v2(db, insertSql.c_str(), insertSql.length(), &insertStmt, nullptr); + if (rc != SQLITE_OK) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + } + + if(ok) { // Create delete statement + const std::string deleteSql = "DELETE FROM rewards WHERE height >= ?"; + auto rc = sqlite3_prepare_v2(db, deleteSql.c_str(), deleteSql.length(), &deleteStmt, nullptr); + if (rc != SQLITE_OK) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + } + + if(ok) { // Loads the database into the in-memory map + const char* sql = "SELECT height, amount FROM rewards"; + auto rc = sqlite3_exec(db, sql, [](void* data, int argc, char** argv, char** /* azColName */) -> int { + int height = std::stoi(argv[0]); + CAmount amount = std::stoll(argv[1]); + mDynamicRewards[height] = amount; + return 0; + }, nullptr, nullptr); + + if (rc != SQLITE_OK) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + } + + if(ok && mDynamicRewards.size() > 0) { // Printing the map + oss << "CRewards::" << __func__ << " Dynamic Rewards:" << std::endl; + for (const auto& pair : mDynamicRewards) { + oss << "CRewards::" << __func__ << " Height: " << pair.first << ", Amount: " << FormatMoney(pair.second) << std::endl; + } + } + } + catch(const std::exception& e) + { + oss << "CRewards::" << __func__ << " An exception was thrown: " << e.what() << std::endl; + ok = false; + } + + if (!oss.str().empty()) LogPrintf("%s: %s", __func__, oss.str()); + + return ok; +} + +void CRewards::Shutdown() +{ + if(insertStmt != nullptr) sqlite3_finalize(insertStmt); + if(deleteStmt != nullptr) sqlite3_finalize(deleteStmt); + if(db != nullptr) sqlite3_close(db); + + return; +} + +int CRewards::GetDynamicRewardsEpoch(int nHeight) +{ + auto& consensus = Params().GetConsensus(); + const auto nRewardAdjustmentInterval = consensus.nRewardAdjustmentInterval; + return nHeight / nRewardAdjustmentInterval; +} + +int CRewards::GetDynamicRewardsEpochHeight(int nHeight) +{ + auto& consensus = Params().GetConsensus(); + const auto nRewardAdjustmentInterval = consensus.nRewardAdjustmentInterval; + return GetDynamicRewardsEpoch(nHeight) * nRewardAdjustmentInterval; +} + +bool CRewards::IsDynamicRewardsEpochHeight(int nHeight) +{ + return GetDynamicRewardsEpochHeight(nHeight) == nHeight; +} + +bool CRewards::ConnectBlock(CBlockIndex* pindex, CAmount nSubsidy, CCoinsViewCache& coins) +{ + auto& consensus = Params().GetConsensus(); + const auto nHeight = pindex->nHeight; + const auto nEpochHeight = GetDynamicRewardsEpochHeight(nHeight); + std::ostringstream oss; + auto ok = true; + + if (consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_DYNAMIC_REWARDS)) + { + CAmount nNewSubsidy = 0; + + if ( + masternodeSync.IsSynced() && + IsDynamicRewardsEpochHeight(nHeight) + ) { + auto nBlocksPerDay = DAY_IN_SECONDS / consensus.nTargetSpacing; + auto nBlocksPerWeek = WEEK_IN_SECONDS / consensus.nTargetSpacing; + auto nBlocksPerMonth = MONTH_IN_SECONDS / consensus.nTargetSpacing; + + // get total money supply + const auto nMoneySupply = pindex->nMoneySupply.get(); + oss << "CRewards::" << __func__ << " nMoneySupply: " << FormatMoney(nMoneySupply) << std::endl; + + // get the current masternode collateral, and the next week collateral + auto nCollateralAmount = CMasternode::GetMasternodeNodeCollateral(nHeight); + auto nNextWeekCollateralAmount = CMasternode::GetMasternodeNodeCollateral(nHeight + nBlocksPerWeek); + + // calculate the current circulating supply + CAmount nCirculatingSupply = 0; + std::unique_ptr pcursor(coins.Cursor()); + + while (pcursor->Valid()) { + COutPoint key; + Coin coin; + if (pcursor->GetKey(key) && pcursor->GetValue(coin)) { + // ----------- burn address scanning ----------- + CTxDestination source; + if (ExtractDestination(coin.out.scriptPubKey, source)) { + const std::string addr = EncodeDestination(source); + if (consensus.mBurnAddresses.find(addr) != consensus.mBurnAddresses.end() && + consensus.mBurnAddresses.at(addr) < nHeight + ) { + pcursor->Next(); // Skip + continue; + } + } + + // ----------- masternode collaterals scanning ----------- + if( + coin.out.nValue == nCollateralAmount || + coin.out.nValue == nNextWeekCollateralAmount + ) { + pcursor->Next(); // Skip + continue; + } + + // ----------- UTXOs age related scanning ----------- + auto nBlocksDiff = static_cast(nHeight - coin.nHeight); + const auto nMultiplier = 100000000L; + + // y = mx + b + // 3 months old or less => 100% + // 12 months old or greater => 0% + const auto nSupplyWeightRatio = + std::min( + std::max( + (100LL * nMultiplier - (((100LL * nMultiplier)/(9LL * nBlocksPerMonth)) * (nBlocksDiff - 3LL * nBlocksPerMonth))) / nMultiplier, + 0LL), + 100LL); + + nCirculatingSupply += coin.out.nValue * nSupplyWeightRatio / 100L; + } + + pcursor->Next(); + } + oss << "CRewards::" << __func__ << " nCirculatingSupply: " << FormatMoney(nCirculatingSupply) << std::endl; + + // calculate target emissions + const auto nRewardAdjustmentInterval = consensus.nRewardAdjustmentInterval; + oss << "CRewards::" << __func__ << " nRewardAdjustmentInterval: " << nRewardAdjustmentInterval << std::endl; + const auto nTotalEmissionRate = sporkManager.GetSporkValue(SPORK_116_TOT_SPLY_TRGT_EMISSION); + oss << "CRewards::" << __func__ << " nTotalEmissionRate: " << nTotalEmissionRate << std::endl; + const auto nCirculatingEmissionRate = sporkManager.GetSporkValue(SPORK_117_CIRC_SPLY_TRGT_EMISSION); + oss << "CRewards::" << __func__ << " nCirculatingEmissionRate: " << nCirculatingEmissionRate << std::endl; + const auto nActualEmission = nSubsidy * nRewardAdjustmentInterval; + oss << "CRewards::" << __func__ << " nActualEmission: " << FormatMoney(nActualEmission) << std::endl; + const auto nSupplyTargetEmission = ((nMoneySupply / (365L * nBlocksPerDay)) / 1000000) * nTotalEmissionRate * nRewardAdjustmentInterval; + oss << "CRewards::" << __func__ << " nSupplyTargetEmission: " << FormatMoney(nSupplyTargetEmission) << std::endl; + const auto nCirculatingTargetEmission = ((nCirculatingSupply / (365L * nBlocksPerDay)) / 1000000) * nCirculatingEmissionRate * nRewardAdjustmentInterval; + oss << "CRewards::" << __func__ << " nCirculatingTargetEmission: " << FormatMoney(nCirculatingTargetEmission) << std::endl; + + // calculate required delta values + const auto nDelta = (nActualEmission - std::max(nSupplyTargetEmission, nCirculatingTargetEmission)) / nRewardAdjustmentInterval; + oss << "CRewards::" << __func__ << " nDelta: " << FormatMoney(nDelta) << std::endl; + + // y = mx + b + // <= 0% |ratio| => 1% + // >= 100% |ratio| => 10% + + const auto nRatio = std::llabs((nDelta * 100) / nSubsidy); // percentage of the difference on emissions and the current reward + oss << "CRewards::" << __func__ << " nRatio: " << nRatio << std::endl; + + const auto nWeightRatio = ((std::min(nRatio, 100LL) * 9LL) / 100LL) + 1LL; + + const auto nDampedDelta = nDelta * nWeightRatio / 100LL; + oss << "CRewards::" << __func__ << " nDampedDelta: " << FormatMoney(nDampedDelta) << std::endl; + + // adjust the reward for this epoch + nNewSubsidy = nSubsidy - nDampedDelta; + + oss << "CRewards::" << __func__ << " Adjustment at height " << nHeight << ": " << FormatMoney(nSubsidy) << " => " << FormatMoney(nNewSubsidy) << std::endl; + } + + if ( // if the wallet is syncing get the reward value from the blocks of the epoch + !masternodeSync.IsSynced() && + nHeight != nEpochHeight && + mDynamicRewards.find(nEpochHeight) == mDynamicRewards.end() + ) { + nNewSubsidy = nSubsidy; + } + + if(ok && nNewSubsidy > 0) { // store it + mDynamicRewards[nEpochHeight] = nNewSubsidy; // on the in-memory map + + sqlite3_bind_int(insertStmt, 1, nEpochHeight); // on the file database + sqlite3_bind_int64(insertStmt, 2, nNewSubsidy); + auto rc = sqlite3_step(insertStmt); + if (rc != SQLITE_DONE) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + sqlite3_reset(insertStmt); + } + } + + if (!oss.str().empty()) LogPrintf("%s: %s", __func__, oss.str()); + + return ok; +} + +bool CRewards::DisconnectBlock(CBlockIndex* pindex) +{ + auto& consensus = Params().GetConsensus(); + const auto nHeight = pindex->nHeight; + std::ostringstream oss; + auto ok = true; + + try + { + if (consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_DYNAMIC_REWARDS) && + IsDynamicRewardsEpochHeight(nHeight) + ) { + auto it = mDynamicRewards.find(nHeight); + if (it != mDynamicRewards.end()) { + // delete it + mDynamicRewards.erase(it); // on the in-memory map + + sqlite3_bind_int(deleteStmt, 1, nHeight); // on the file database + auto rc = sqlite3_step(deleteStmt); + if (rc != SQLITE_DONE) { + oss << "CRewards::" << __func__ << " SQL error: " << sqlite3_errmsg(db) << std::endl; + ok = false; + } + sqlite3_reset(deleteStmt); + } + } + } + catch(const std::exception& e) + { + oss << "CRewards::" << __func__ << " An exception was thrown: " << e.what() << std::endl; + ok = false; + } + + if (!oss.str().empty()) LogPrintf("%s: %s", __func__, oss.str()); + + return ok; +} + +CAmount CRewards::GetBlockValue(int nHeight) +{ + auto& consensus = Params().GetConsensus(); + + CAmount nSubsidy; + + // ---- Static reward table ---- + if (nHeight == 1) { + nSubsidy = 30000000 * COIN; // __DSW__ coin supply (30M) + } else if (nHeight <= 100000) { + nSubsidy = 100 * COIN; + } else if (nHeight > 100000 && nHeight <= 200000) { + nSubsidy = 125 * COIN; + } else if (nHeight > 200000 && nHeight <= 300000) { + nSubsidy = 150 * COIN; + } else if (nHeight > 300000 && nHeight <= 400000) { + nSubsidy = 125 * COIN; + } else if (nHeight > 400000) { + nSubsidy = 100 * COIN; + } + // ---- Static reward table ---- + + if (masternodeSync.IsSynced() && + consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_DYNAMIC_REWARDS) + ) { + // if this is the block where calculations are made on ConnectBlock + // return the reward value from the previous block + if(IsDynamicRewardsEpochHeight(nHeight)) + return GetBlockValue(nHeight - 1); + + // find and return the dynamic reward + const auto nEpochHeight = GetDynamicRewardsEpochHeight(nHeight); + auto it = mDynamicRewards.find(nEpochHeight); + if (it != mDynamicRewards.end()) { + return std::min(nSubsidy, it->second); + } + } + + // fallback non-dynamic reward return + return nSubsidy; +} \ No newline at end of file diff --git a/src/rewards.h b/src/rewards.h new file mode 100644 index 0000000000..d7af454573 --- /dev/null +++ b/src/rewards.h @@ -0,0 +1,25 @@ +// Copyright (c) 2014-2015 The Dash developers +// Copyright (c) 2015-2020 The PIVX developers +// Copyright (c) 2021-2024 The DECENOMY Core Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef REWARDS_H +#define REWARDS_H + +#include "main.h" + +class CRewards +{ +public: + static bool Init(bool fReindex); + static void Shutdown(); + static int GetDynamicRewardsEpoch(int nHeight); + static int GetDynamicRewardsEpochHeight(int nHeight); + static bool IsDynamicRewardsEpochHeight(int nHeight); + static bool ConnectBlock(CBlockIndex* pindex, CAmount nSubsidy, CCoinsViewCache& coins); + static bool DisconnectBlock(CBlockIndex* pindex); + static CAmount GetBlockValue(int nHeight); +}; + +#endif \ No newline at end of file diff --git a/src/test/main_tests.cpp b/src/test/main_tests.cpp index dbce927dbc..416860b628 100644 --- a/src/test/main_tests.cpp +++ b/src/test/main_tests.cpp @@ -12,6 +12,7 @@ #include "test_pivx.h" #include #include "masternode.h" +#include "rewards.h" BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup) @@ -117,35 +118,35 @@ BOOST_AUTO_TEST_CASE(subsidy_limit_test) CAmount nSum = 0; for (int nHeight = 0; nHeight <= 1; nHeight += 1) { /* premine in block 1 (30000000 __DSW__) */ - CAmount nSubsidy = CMasternode::GetBlockValue(nHeight); + CAmount nSubsidy = CRewards::GetBlockValue(nHeight); BOOST_CHECK(nSubsidy <= 30000000 * COIN); nSum += nSubsidy; } for (int nHeight = 2; nHeight <= 100000; nHeight += 1) { /* PoW Phase One */ - CAmount nSubsidy = CMasternode::GetBlockValue(nHeight); + CAmount nSubsidy = CRewards::GetBlockValue(nHeight); BOOST_CHECK(nSubsidy == 100 * COIN); nSum += nSubsidy; } for (int nHeight = 100001; nHeight <= 200000; nHeight += 1) { /* PoW Phase Two */ - CAmount nSubsidy = CMasternode::GetBlockValue(nHeight); + CAmount nSubsidy = CRewards::GetBlockValue(nHeight); BOOST_CHECK(nSubsidy == 125 * COIN); nSum += nSubsidy; } for (int nHeight = 200001; nHeight <= 300000; nHeight += 1) { /* PoW Phase Two */ - CAmount nSubsidy = CMasternode::GetBlockValue(nHeight); + CAmount nSubsidy = CRewards::GetBlockValue(nHeight); BOOST_CHECK(nSubsidy == 150 * COIN); nSum += nSubsidy; } for (int nHeight = 300001; nHeight <= 400000; nHeight += 1) { /* PoW Phase Two */ - CAmount nSubsidy = CMasternode::GetBlockValue(nHeight); + CAmount nSubsidy = CRewards::GetBlockValue(nHeight); BOOST_CHECK(nSubsidy == 125 * COIN); nSum += nSubsidy; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 277a172be4..794e6021a3 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -15,6 +15,7 @@ #include "masternode-payments.h" #include "masternodeconfig.h" #include "policy/policy.h" +#include "rewards.h" #include "script/sign.h" #include "spork.h" #include "util.h" @@ -2521,7 +2522,7 @@ bool CWallet::CreateCoinStake( nCredit += stakeInput.GetValue(); // Add block reward to the credit - nCredit += CMasternode::GetBlockValue(pindexPrev->nHeight + 1); + nCredit += CRewards::GetBlockValue(pindexPrev->nHeight + 1); CAmount nMasternodeCredit = CMasternode::GetMasternodePayment(pindexPrev->nHeight + 1); // Create the output transaction(s) From 32dc6edd2da992dea22201e97760a6b799600ead Mon Sep 17 00:00:00 2001 From: pedro-at-decenomy Date: Tue, 23 Apr 2024 17:59:36 +0100 Subject: [PATCH 2/8] small fix --- src/qt/walletmodel.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 6d95330e59..f760474175 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -298,27 +298,27 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact // Pre-check input data for validity Q_FOREACH (const SendCoinsRecipient& rcp, recipients) { - { // User-entered __DSW__ address / amount: - if (!validateAddress(rcp.address)) { - return InvalidAddress; - } - if (rcp.amount <= 0) { - return InvalidAmount; - } - setAddress.insert(rcp.address); - ++nAddresses; + // User-entered __DSW__ address / amount: + if (!validateAddress(rcp.address)) { + return InvalidAddress; + } + if (rcp.amount <= 0) { + return InvalidAmount; + } + setAddress.insert(rcp.address); + ++nAddresses; - CScript scriptPubKey; - CTxDestination out = DecodeDestination(rcp.address.toStdString()); + CScript scriptPubKey; + CTxDestination out = DecodeDestination(rcp.address.toStdString()); - // Regular P2PK or P2PKH - scriptPubKey = GetScriptForDestination(out); + // Regular P2PK or P2PKH + scriptPubKey = GetScriptForDestination(out); - vecSend.push_back(CRecipient{scriptPubKey, rcp.amount, false}); + vecSend.push_back(CRecipient{scriptPubKey, rcp.amount, false}); - total += rcp.amount; - } + total += rcp.amount; } + if (setAddress.size() != nAddresses) { return DuplicateAddress; } From c7151f24286052dd79544d7f192b16e2e50a6a75 Mon Sep 17 00:00:00 2001 From: zimbora Date: Wed, 24 Apr 2024 10:46:29 +0100 Subject: [PATCH 3/8] src/init: small fix on bootstrap folder deletion --- src/init.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 5d6c2f9a6f..97ff5ff976 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1262,8 +1262,7 @@ bool AppInit2() LogPrintf("-bootstrap: Download: %s\n", url.c_str()); - if(Bootstrap::isDirectory(extractPath)) - Bootstrap::rmDirectory(extractPath); + Bootstrap::rmDirectory(extractPath); if (Bootstrap::DownloadFile(url, outputFileName, DownloadProgressCallback)) { LogPrintf("-bootstrap: File downloaded successfully \n"); @@ -1274,18 +1273,22 @@ bool AppInit2() fs::rename(extractPath+"/blocks", blocksDir); fs::rename(extractPath+"/chainstate", chainstateDir); LogPrintf("-bootstrap: Folders moved successfully \n"); - fs::remove(extractPath); } catch (const std::exception& e) { LogPrintf("-bootstrap: Error moving folder: %s\n",e.what()); } + } else { LogPrintf("-bootstrap: Error extracting zip file"); } - fs::remove(outputFileName); + Bootstrap::rmDirectory(extractPath); + } else { LogPrintf("-bootstrap: Error downloading file"); } + + if(fs::exists(outputFileName)) + fs::remove(outputFileName); } #else LogPrintf("-bootstrap: not enabled\n"); From 0b4933e671dea07903445e661604cbc4627dbee4 Mon Sep 17 00:00:00 2001 From: zimbora Date: Sat, 27 Apr 2024 12:51:01 +0100 Subject: [PATCH 4/8] bootstrap: formats logs --- docker-build.sh | 2 +- src/bootstrap/bootstrap.cpp | 51 +- src/bootstrap/bootstrap.h | 1 + src/bootstrap/logging.h | 34 ++ src/bootstrap/makefile | 6 +- src/bootstrap/test.cpp | 9 +- src/bootstrap/tinyformat.h | 1059 +++++++++++++++++++++++++++++++++++ 7 files changed, 1131 insertions(+), 31 deletions(-) create mode 100644 src/bootstrap/logging.h create mode 100644 src/bootstrap/tinyformat.h diff --git a/docker-build.sh b/docker-build.sh index 21684cfc9e..7756d5e184 100755 --- a/docker-build.sh +++ b/docker-build.sh @@ -3,7 +3,7 @@ # Check if all required arguments are provided if [ $# -ne 6 ]; then echo "Usage: $0 " - echo "Usage: $0 DSW __Decenomy__ __decenomy__ bootstrap 3.0.0.0 Linux" + echo "Usage: $0 DSW __Decenomy__ __decenomy__ develop 3.0.0.0 Linux" exit 1 fi diff --git a/src/bootstrap/bootstrap.cpp b/src/bootstrap/bootstrap.cpp index 0bcfede5d0..4e48a6665c 100644 --- a/src/bootstrap/bootstrap.cpp +++ b/src/bootstrap/bootstrap.cpp @@ -11,12 +11,12 @@ bool Bootstrap::rmDirectory(const std::string& directory_path) { if (fs::exists(directory_path)) { // Remove the directory and its contents fs::remove_all(directory_path); - //std::cout << "Directory removed successfully." << std::endl; + //LogPrintf("-bootstrap: Directory removed successfully."); } else { - //std::cerr << "Directory does not exist." << std::endl; + //LogPrintf("-bootstrap: Directory does not exist."); } } catch (const fs::filesystem_error& ex) { - //std::cerr << "Error removing directory: " << ex.what() << std::endl; + //LogPrintf("-bootstrap: Error removing directory: " << ex.what()); return false; } @@ -47,8 +47,7 @@ int Bootstrap::ProgressCallback(void *clientp, double dltotal, double dlnow, dou auto duration = std::chrono::duration_cast(now.time_since_epoch()); if(!log_flag && duration.count() % 2 == 0){ log_flag = true; - std::printf("-Bootstrap: Download: %d%%\n", (uint8_t)progress); - //LogPrintf("-Bootstrap: Download: %d%%\n", (uint8_t)progress); + LogPrintf("-bootstrap: Download: %d%%\n", (uint8_t)progress); //uiInterface.ShowProgress(_("Download: "), (uint8_t)progress); }else if(duration.count() % 2 != 0) log_flag = false; @@ -60,7 +59,7 @@ bool Bootstrap::DownloadFile(const std::string& url, const std::string& outputFi CURL* curl = curl_easy_init(); if (!curl) { - //std::cerr << "Error initializing libcurl." << std::endl; + //LogPrintf("-bootstrap: Error initializing libcurl."); return false; } @@ -68,16 +67,16 @@ bool Bootstrap::DownloadFile(const std::string& url, const std::string& outputFi if (info) { // Print libcurl version information - printf("libcurl version: %s\n", info->version); - printf("libcurl SSL version: %s\n", info->ssl_version); - printf("libcurl zlib version: %s\n", info->libz_version); + LogPrintf("-bootstrap: libcurl version: %s \n", info->version); + LogPrintf("-bootstrap: libcurl SSL version: %s \n", info->ssl_version); + //LogPrintf("-bootstrap: libcurl zlib version: %s \n", info->libz_version); } else { - printf("Failed to retrieve libcurl version information.\n"); + LogPrintf("-bootstrap: Failed to retrieve libcurl version information.\n"); } std::ofstream outputFile(outputFileName, std::ios::binary); if (!outputFile.is_open()) { - //std::cerr << "Error opening output file." << std::endl; + LogPrintf("-bootstrap: Error opening output file."); return false; } @@ -93,16 +92,16 @@ bool Bootstrap::DownloadFile(const std::string& url, const std::string& outputFi curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, func); #if defined(__APPLE__) - std::cout << "apple ca path: " << (const char*)APPLE_CA_PATH << std::endl; + LogPrintf("-bootstrap: apple ca path: %s \n",(const char*)APPLE_CA_PATH); curl_easy_setopt(curl, CURLOPT_CAINFO, (const char*)APPLE_CA_PATH); #elif defined(__linux__) - //std::cout << "linux ca path: " << (const char*)LINUX_CA_PATH << std::endl; + //LogPrintf("-bootstrap: linux ca path: " << (const char*)LINUX_CA_PATH); //curl_easy_setopt(curl, CURLOPT_CAINFO, (const char*)LINUX_CA_PATH); #elif defined(_WIN32) - ///std::cout << "windows ca path: " << (const char*)WIN_CA_PATH << std::endl; + ///LogPrintf("-bootstrap: windows ca path: " << (const char*)WIN_CA_PATH); //curl_easy_setopt(curl, CURLOPT_CAINFO, (const char*)WIN_CA_PATH); #else - std::cerr << "OS not recognized, CA Path not defined" << std::endl; + LogPrintf("-bootstrap: OS not recognized, CA Path not defined"); #endif CURLcode res = curl_easy_perform(curl); @@ -111,7 +110,7 @@ bool Bootstrap::DownloadFile(const std::string& url, const std::string& outputFi outputFile.close(); if (res != CURLE_OK) { - std::cerr << "Error downloading file: " << curl_easy_strerror(res) << std::endl; + LogPrintf("-bootstrap: Error downloading file: %s \n",curl_easy_strerror(res)); return false; } @@ -123,13 +122,13 @@ bool Bootstrap::extractZip(const std::string& zipFilePath, const std::string& ou // Open the zip file unzFile zipFile = unzOpen(zipFilePath.c_str()); if (!zipFile) { - std::cerr << "Error opening zip file: " << zipFilePath << std::endl; + LogPrintf("-bootstrap: Error opening zip file: %s \n",zipFilePath); return false; } // Create the output folder if it doesn't exist if (!ensureOutputFolder(outputFolderPath)) { - std::cerr << "Error creating output folder: " << outputFolderPath << std::endl; + LogPrintf("-bootstrap: Error creating output folder: %s \n",outputFolderPath); unzClose(zipFile); return false; } @@ -137,7 +136,7 @@ bool Bootstrap::extractZip(const std::string& zipFilePath, const std::string& ou // Go through each file in the zip and extract it unz_global_info globalInfo; if (unzGetGlobalInfo(zipFile, &globalInfo) != UNZ_OK) { - std::cerr << "Error getting global info from zip file." << std::endl; + LogPrintf("-bootstrap: Error getting global info from zip file."); unzClose(zipFile); return false; } @@ -147,26 +146,26 @@ bool Bootstrap::extractZip(const std::string& zipFilePath, const std::string& ou unz_file_info fileInfo; if (unzGetCurrentFileInfo(zipFile, &fileInfo, fileName, sizeof(fileName), nullptr, 0, nullptr, 0) != UNZ_OK) { - std::cerr << "Error getting file info from zip file." << std::endl; + LogPrintf("-bootstrap: Error getting file info from zip file."); unzClose(zipFile); return false; } if (unzOpenCurrentFile(zipFile) != UNZ_OK) { - std::cerr << "Error opening current file in zip." << std::endl; + LogPrintf("-bootstrap: Error opening current file in zip."); unzClose(zipFile); return false; } std::string outputPath = std::string(outputFolderPath) + "/" + fileName; - std::cout << "extract file: " << fileName << std::endl; + LogPrintf("-bootstrap: extract file: %s \n",fileName); if(endsWithSlash(outputPath)) ensureOutputFolder(outputPath); else{ std::ofstream outFile(outputPath, std::ios::binary); if (!outFile.is_open()) { - std::cerr << "Error creating output file: " << outputPath << std::endl; + LogPrintf("-bootstrap: Error creating output file: %s \n",outputPath); unzCloseCurrentFile(zipFile); unzClose(zipFile); return false; @@ -193,7 +192,7 @@ bool Bootstrap::extractZip(const std::string& zipFilePath, const std::string& ou // Close the zip file unzClose(zipFile); - std::cout << "Zip extraction successful." << std::endl; + LogPrintf("-bootstrap: Zip extraction successful."); return true; @@ -206,12 +205,12 @@ bool Bootstrap::ensureOutputFolder(const std::string& outputPath) { fs::create_directories(outputPath); } else if (!fs::is_directory(outputPath)) { // If it exists but is not a directory, print an error - std::cerr << "Error: Output path '" << outputPath << "' is not a directory." << std::endl; + LogPrintf("-bootstrap: Error: Output path %s is not a directory.", outputPath); return false; } } catch (const std::exception& e) { // Handle any exceptions that may occur during filesystem operations - std::cerr << "Error creating output folder: " << e.what() << std::endl; + LogPrintf("-bootstrap: Error creating output folder: %s \n",e.what()); return false; } diff --git a/src/bootstrap/bootstrap.h b/src/bootstrap/bootstrap.h index 9fcd1c319a..4bdf9d88ec 100644 --- a/src/bootstrap/bootstrap.h +++ b/src/bootstrap/bootstrap.h @@ -15,6 +15,7 @@ #include #include "minizip/unzip.h" +#include "logging.h" #ifndef TICKER #define TICKER "__DSW__" diff --git a/src/bootstrap/logging.h b/src/bootstrap/logging.h new file mode 100644 index 0000000000..206e984980 --- /dev/null +++ b/src/bootstrap/logging.h @@ -0,0 +1,34 @@ +#ifndef LOGGING_H +#define LOGGING_H + +#include "tinyformat.h" +#include // For perror +#include // For strerror +#include +#include + +/** Get format string from VA_ARGS for error reporting */ +template std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; } + +#define LogPrintf(...) do { \ + if(true) { \ + std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \ + try { \ + _log_msg_ = tfm::format(__VA_ARGS__); \ + } catch (tinyformat::format_error &e) { \ + /* Original format string will have newline so don't add one here */ \ + _log_msg_ = "Error \"" + std::string(e.what()) + \ + "\" while formatting log message: " + \ + FormatStringFromLogArgs(__VA_ARGS__); \ + } \ + std::cout << _log_msg_; \ + } \ +} while(0) + +#define LogPrint(category, ...) do { \ + if (LogAcceptCategory((category))) { \ + LogPrintf(__VA_ARGS__); \ + } \ +} while(0) + +#endif \ No newline at end of file diff --git a/src/bootstrap/makefile b/src/bootstrap/makefile index 3ba33b51fe..a795bc63df 100644 --- a/src/bootstrap/makefile +++ b/src/bootstrap/makefile @@ -36,7 +36,7 @@ docker_macos_test: -I/DSW/depends/x86_64-apple-darwin14/include \ -L/DSW/depends/x86_64-apple-darwin14/lib \ -L/DSW/depends/x86_64-apple-darwin14/lib64 \ - test.cpp bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ + test.cpp logging.h tinyformat.h bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ -lcurl -DCURL_STATICLIB `/DSW/depends/x86_64-apple-darwin14/bin/curl-config --cflags --static-libs` -pthread -lz -lssl -lcrypto -lboost_system-mt-x64 -lboost_filesystem-mt-x64 docker_linux_test: @@ -44,7 +44,7 @@ docker_linux_test: -I/DSW/depends/x86_64-pc-linux-gnu/include \ -L/DSW/depends/x86_64-pc-linux-gnu/lib \ -L/DSW/depends/x86_64-pc-linux-gnu/lib64 \ - -o test test.cpp bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ + -o test test.cpp logging.h tinyformat.h bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ -lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64 docker_windows_test: @@ -52,7 +52,7 @@ docker_windows_test: -I/DSW/depends/x86_64-w64-mingw32/include \ -L/DSW/depends/x86_64-w64-mingw32/lib \ -L/DSW/depends/x86_64-w64-mingw32/lib64 \ - -o test.exe test.cpp bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ + -o test.exe test.cpp logging.h tinyformat.h bootstrap.h bootstrap.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \ -lcurl -DCURL_STATICLIB `/DSW/depends/x86_64-w64-mingw32/bin/curl-config --cflags --static-libs` -pthread -lz -lssl -lcrypto -lboost_system-mt-s-x64 -lboost_filesystem-mt-s-x64 clean: diff --git a/src/bootstrap/test.cpp b/src/bootstrap/test.cpp index 2f2c6c8dc1..d09cb172c5 100644 --- a/src/bootstrap/test.cpp +++ b/src/bootstrap/test.cpp @@ -1,6 +1,7 @@ #define COIN "777" #include "bootstrap.h" +namespace fs = boost::filesystem; // Define the progress callback function static int downloadProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) { @@ -13,7 +14,7 @@ static int downloadProgressCallback(void *clientp, double dltotal, double dlnow, static bool log_flag = false; // Declare log_flag as static if (!log_flag && duration.count() % 2 == 0) { log_flag = true; - std::printf("-Bootstrap: Download: %d%%\n", (uint8_t)progress); + std::printf("-bootstrap: Download: %d%%\n", (uint8_t)progress); // LogPrintf("-Bootstrap: Download: %d%%\n", (uint8_t)progress); // uiInterface.ShowProgress(_("Download: "), (uint8_t)progress); } else if (duration.count() % 2 != 0) { @@ -75,9 +76,15 @@ int main() { } else { std::cerr << "Error extracting zip file." << std::endl; } + + if(Bootstrap::isDirectory(extractPath)) + Bootstrap::rmDirectory(extractPath); + } else { std::cerr << "Error downloading file." << std::endl; } + fs::remove(outputFileName); + return 0; } diff --git a/src/bootstrap/tinyformat.h b/src/bootstrap/tinyformat.h new file mode 100644 index 0000000000..00114eae97 --- /dev/null +++ b/src/bootstrap/tinyformat.h @@ -0,0 +1,1059 @@ +// tinyformat.h +// Copyright (C) 2011, Chris Foster [chris42f (at) gmail (d0t) com] +// +// Boost Software License - Version 1.0 +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//------------------------------------------------------------------------------ +// Tinyformat: A minimal type safe printf replacement +// +// tinyformat.h is a type safe printf replacement library in a single C++ +// header file. Design goals include: +// +// * Type safety and extensibility for user defined types. +// * C99 printf() compatibility, to the extent possible using std::ostream +// * Simplicity and minimalism. A single header file to include and distribute +// with your projects. +// * Augment rather than replace the standard stream formatting mechanism +// * C++98 support, with optional C++11 niceties +// +// +// Main interface example usage +// ---------------------------- +// +// To print a date to std::cout: +// +// std::string weekday = "Wednesday"; +// const char* month = "July"; +// size_t day = 27; +// long hour = 14; +// int min = 44; +// +// tfm::printf("%s, %s %d, %.2d:%.2d\n", weekday, month, day, hour, min); +// +// The strange types here emphasize the type safety of the interface; it is +// possible to print a std::string using the "%s" conversion, and a +// size_t using the "%d" conversion. A similar result could be achieved +// using either of the tfm::format() functions. One prints on a user provided +// stream: +// +// tfm::format(std::cerr, "%s, %s %d, %.2d:%.2d\n", +// weekday, month, day, hour, min); +// +// The other returns a std::string: +// +// std::string date = tfm::format("%s, %s %d, %.2d:%.2d\n", +// weekday, month, day, hour, min); +// std::cout << date; +// +// These are the three primary interface functions. There is also a +// convenience function printfln() which appends a newline to the usual result +// of printf() for super simple logging. +// +// +// User defined format functions +// ----------------------------- +// +// Simulating variadic templates in C++98 is pretty painful since it requires +// writing out the same function for each desired number of arguments. To make +// this bearable tinyformat comes with a set of macros which are used +// internally to generate the API, but which may also be used in user code. +// +// The three macros TINYFORMAT_ARGTYPES(n), TINYFORMAT_VARARGS(n) and +// TINYFORMAT_PASSARGS(n) will generate a list of n argument types, +// type/name pairs and argument names respectively when called with an integer +// n between 1 and 16. We can use these to define a macro which generates the +// desired user defined function with n arguments. To generate all 16 user +// defined function bodies, use the macro TINYFORMAT_FOREACH_ARGNUM. For an +// example, see the implementation of printf() at the end of the source file. +// +// Sometimes it's useful to be able to pass a list of format arguments through +// to a non-template function. The FormatList class is provided as a way to do +// this by storing the argument list in a type-opaque way. Continuing the +// example from above, we construct a FormatList using makeFormatList(): +// +// FormatListRef formatList = tfm::makeFormatList(weekday, month, day, hour, min); +// +// The format list can now be passed into any non-template function and used +// via a call to the vformat() function: +// +// tfm::vformat(std::cout, "%s, %s %d, %.2d:%.2d\n", formatList); +// +// +// Additional API information +// -------------------------- +// +// Error handling: Define TINYFORMAT_ERROR to customize the error handling for +// format strings which are unsupported or have the wrong number of format +// specifiers (calls assert() by default). +// +// User defined types: Uses operator<< for user defined types by default. +// Overload formatValue() for more control. + + +#ifndef TINYFORMAT_H_INCLUDED +#define TINYFORMAT_H_INCLUDED + +namespace tinyformat +{ +} +//------------------------------------------------------------------------------ +// Config section. Customize to your liking! + +// Namespace alias to encourage brevity +namespace tfm = tinyformat; + +// Error handling; calls assert() by default. +#define TINYFORMAT_ERROR(reasonString) throw tinyformat::format_error(reasonString) + +// Define for C++11 variadic templates which make the code shorter & more +// general. If you don't define this, C++11 support is autodetected below. +#ifndef __APPLE__ +#define TINYFORMAT_USE_VARIADIC_TEMPLATES +#endif + +//------------------------------------------------------------------------------ +// Implementation details. +#include +#include +#include +#include +#include + +#ifndef TINYFORMAT_ERROR +#define TINYFORMAT_ERROR(reason) assert(0 && reason) +#endif + +#if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES) +#ifdef __GXX_EXPERIMENTAL_CXX0X__ +#define TINYFORMAT_USE_VARIADIC_TEMPLATES +#endif +#endif + +#if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201 +// std::showpos is broken on old libstdc++ as provided with OSX. See +// http://gcc.gnu.org/ml/libstdc++/2007-11/msg00075.html +#define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND +#endif + +#ifdef __APPLE__ +// Workaround OSX linker warning: xcode uses different default symbol +// visibilities for static libs vs executables (see bitcoin's issue #25) +# define TINYFORMAT_HIDDEN __attribute__((visibility("hidden"))) +#else +# define TINYFORMAT_HIDDEN +#endif + +namespace tinyformat +{ + +class format_error: public std::runtime_error +{ +public: + format_error(const std::string &what): std::runtime_error(what) { + } +}; + +//------------------------------------------------------------------------------ +namespace detail +{ +// Test whether type T1 is convertible to type T2 +template +struct is_convertible { +private: + // two types of different size + struct fail { + char dummy[2]; + }; + struct succeed { + char dummy; + }; + // Try to convert a T1 to a T2 by plugging into tryConvert + static fail tryConvert(...); + static succeed tryConvert(const T2&); + static const T1& makeT1(); + +public: +#ifdef _MSC_VER +// Disable spurious loss of precision warnings in tryConvert(makeT1()) +#pragma warning(push) +#pragma warning(disable : 4244) +#pragma warning(disable : 4267) +#endif + // Standard trick: the (...) version of tryConvert will be chosen from + // the overload set only if the version taking a T2 doesn't match. + // Then we compare the sizes of the return types to check which + // function matched. Very neat, in a disgusting kind of way :) + static const bool value = + sizeof(tryConvert(makeT1())) == sizeof(succeed); +#ifdef _MSC_VER +#pragma warning(pop) +#endif +}; + + +// Detect when a type is not a wchar_t string +template +struct is_wchar { + typedef int tinyformat_wchar_is_not_supported; +}; +template <> +struct is_wchar { +}; +template <> +struct is_wchar { +}; +template +struct is_wchar { +}; +template +struct is_wchar { +}; + + +// Format the value by casting to type fmtT. This default implementation +// should never be called. +template ::value> +struct formatValueAsType { + static void invoke(std::ostream& /*out*/, const T& /*value*/) { assert(0); } +}; +// Specialized version for types that can actually be converted to fmtT, as +// indicated by the "convertible" template parameter. +template +struct formatValueAsType { + static void invoke(std::ostream& out, const T& value) + { + out << static_cast(value); + } +}; + +#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND +template ::value> +struct formatZeroIntegerWorkaround { + static bool invoke(std::ostream& /**/, const T& /**/) { return false; } +}; +template +struct formatZeroIntegerWorkaround { + static bool invoke(std::ostream& out, const T& value) + { + if (static_cast(value) == 0 && out.flags() & std::ios::showpos) { + out << "+0"; + return true; + } + return false; + } +}; +#endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND + +// Convert an arbitrary type to integer. The version with convertible=false +// throws an error. +template ::value> +struct convertToInt { + static int invoke(const T& /*value*/) + { + TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to " + "integer for use as variable width or precision"); + return 0; + } +}; +// Specialization for convertToInt when conversion is possible +template +struct convertToInt { + static int invoke(const T& value) { return static_cast(value); } +}; + +// Format at most ntrunc characters to the given stream. +template +inline void formatTruncated(std::ostream& out, const T& value, int ntrunc) +{ + std::ostringstream tmp; + tmp << value; + std::string result = tmp.str(); + out.write(result.c_str(), (std::min)(ntrunc, static_cast(result.size()))); +} +#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \ +inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \ +{ \ + std::streamsize len = 0; \ + while(len < ntrunc && value[len] != 0) \ + ++len; \ + out.write(value, len); \ +} +// Overload for const char* and char*. Could overload for signed & unsigned +// char too, but these are technically unneeded for printf compatibility. +TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(const char) +TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(char) +#undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR + +} // namespace detail + + +//------------------------------------------------------------------------------ +/// Format a value into a stream, delegating to operator<< by default. +/// +/// Users may override this for their own types. When this function is called, +/// the stream flags will have been modified according to the format string. +/// The format specification is provided in the range [fmtBegin, fmtEnd). For +/// truncating conversions, ntrunc is set to the desired maximum number of +/// characters, for example "%.7s" calls formatValue with ntrunc = 7. +/// +/// By default, formatValue() uses the usual stream insertion operator +/// operator<< to format the type T, with special cases for the %c and %p +/// conversions. +template +inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, + const char* fmtEnd, int ntrunc, const T& value) +{ +#ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS + // Since we don't support printing of wchar_t using "%ls", make it fail at + // compile time in preference to printing as a void* at runtime. + typedef typename detail::is_wchar::tinyformat_wchar_is_not_supported DummyType; + (void)DummyType(); // avoid unused type warning with gcc-4.8 +#endif + // The mess here is to support the %c and %p conversions: if these + // conversions are active we try to convert the type to a char or const + // void* respectively and format that instead of the value itself. For the + // %p conversion it's important to avoid dereferencing the pointer, which + // could otherwise lead to a crash when printing a dangling (const char*). + const bool canConvertToChar = detail::is_convertible::value; + const bool canConvertToVoidPtr = detail::is_convertible::value; + if (canConvertToChar && *(fmtEnd - 1) == 'c') + detail::formatValueAsType::invoke(out, value); + else if (canConvertToVoidPtr && *(fmtEnd - 1) == 'p') + detail::formatValueAsType::invoke(out, value); +#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND + else if (detail::formatZeroIntegerWorkaround::invoke(out, value)) /**/; +#endif + else if(ntrunc >= 0) + { + // Take care not to overread C strings in truncating conversions like + // "%.4s" where at most 4 characters may be read. + detail::formatTruncated(out, value, ntrunc); + } + else + out << value; +} + + +// Overloaded version for char types to support printing as an integer +#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \ + inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \ + const char* fmtEnd, int /**/, charType value) \ + { \ + switch (*(fmtEnd - 1)) { \ + case 'u': \ + case 'd': \ + case 'i': \ + case 'o': \ + case 'X': \ + case 'x': \ + out << static_cast(value); \ + break; \ + default: \ + out << value; \ + break; \ + } \ + } +// per 3.9.1: char, signed char and unsigned char are all distinct types +TINYFORMAT_DEFINE_FORMATVALUE_CHAR(char) +TINYFORMAT_DEFINE_FORMATVALUE_CHAR(signed char) +TINYFORMAT_DEFINE_FORMATVALUE_CHAR(unsigned char) +#undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR + + +//------------------------------------------------------------------------------ +// Tools for emulating variadic templates in C++98. The basic idea here is +// stolen from the boost preprocessor metaprogramming library and cut down to +// be just general enough for what we need. + +#define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_##n +#define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_##n +#define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_##n +#define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_##n + +// To keep it as transparent as possible, the macros below have been generated +// using python via the excellent cog.py code generation script. This avoids +// the need for a bunch of complex (but more general) preprocessor tricks as +// used in boost.preprocessor. +// +// To rerun the code generation in place, use `cog.py -r tinyformat.h` +// (see http://nedbatchelder.com/code/cog). Alternatively you can just create +// extra versions by hand. + +/*[[[cog +maxParams = 16 + +def makeCommaSepLists(lineTemplate, elemTemplate, startInd=1): + for j in range(startInd,maxParams+1): + std::list = ', '.join([elemTemplate % {'i':i} for i in range(startInd,j+1)]) + cog.outl(lineTemplate % {'j':j, 'list':list}) + +makeCommaSepLists('#define TINYFORMAT_ARGTYPES_%(j)d %(list)s', + 'class T%(i)d') + +cog.outl() +makeCommaSepLists('#define TINYFORMAT_VARARGS_%(j)d %(list)s', + 'const T%(i)d& v%(i)d') + +cog.outl() +makeCommaSepLists('#define TINYFORMAT_PASSARGS_%(j)d %(list)s', 'v%(i)d') + +cog.outl() +cog.outl('#define TINYFORMAT_PASSARGS_TAIL_1') +makeCommaSepLists('#define TINYFORMAT_PASSARGS_TAIL_%(j)d , %(list)s', + 'v%(i)d', startInd = 2) + +cog.outl() +cog.outl('#define TINYFORMAT_FOREACH_ARGNUM(m) \\\n ' + + ' '.join(['m(%d)' % (j,) for j in range(1,maxParams+1)])) +]]]*/ +#define TINYFORMAT_ARGTYPES_1 class T1 +#define TINYFORMAT_ARGTYPES_2 class T1, class T2 +#define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3 +#define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4 +#define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5 +#define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6 +#define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7 +#define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8 +#define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9 +#define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10 +#define TINYFORMAT_ARGTYPES_11 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11 +#define TINYFORMAT_ARGTYPES_12 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12 +#define TINYFORMAT_ARGTYPES_13 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13 +#define TINYFORMAT_ARGTYPES_14 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14 +#define TINYFORMAT_ARGTYPES_15 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15 +#define TINYFORMAT_ARGTYPES_16 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15, class T16 + +#define TINYFORMAT_VARARGS_1 const T1& v1 +#define TINYFORMAT_VARARGS_2 const T1 &v1, const T2 &v2 +#define TINYFORMAT_VARARGS_3 const T1 &v1, const T2 &v2, const T3 &v3 +#define TINYFORMAT_VARARGS_4 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4 +#define TINYFORMAT_VARARGS_5 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5 +#define TINYFORMAT_VARARGS_6 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6 +#define TINYFORMAT_VARARGS_7 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7 +#define TINYFORMAT_VARARGS_8 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8 +#define TINYFORMAT_VARARGS_9 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9 +#define TINYFORMAT_VARARGS_10 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10 +#define TINYFORMAT_VARARGS_11 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11 +#define TINYFORMAT_VARARGS_12 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11, const T12 &v12 +#define TINYFORMAT_VARARGS_13 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11, const T12 &v12, const T13 &v13 +#define TINYFORMAT_VARARGS_14 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11, const T12 &v12, const T13 &v13, const T14 &v14 +#define TINYFORMAT_VARARGS_15 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11, const T12 &v12, const T13 &v13, const T14 &v14, const T15 &v15 +#define TINYFORMAT_VARARGS_16 const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4, const T5 &v5, const T6 &v6, const T7 &v7, const T8 &v8, const T9 &v9, const T10 &v10, const T11 &v11, const T12 &v12, const T13 &v13, const T14 &v14, const T15 &v15, const T16 &v16 + +#define TINYFORMAT_PASSARGS_1 v1 +#define TINYFORMAT_PASSARGS_2 v1, v2 +#define TINYFORMAT_PASSARGS_3 v1, v2, v3 +#define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4 +#define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5 +#define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6 +#define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7 +#define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8 +#define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9 +#define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 +#define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 +#define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 +#define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13 +#define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 +#define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 +#define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16 + +#define TINYFORMAT_PASSARGS_TAIL_1 +#define TINYFORMAT_PASSARGS_TAIL_2 , v2 +#define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3 +#define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4 +#define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5 +#define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6 +#define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7 +#define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8 +#define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9 +#define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10 +#define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 +#define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 +#define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13 +#define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14 +#define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 +#define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16 + +#define TINYFORMAT_FOREACH_ARGNUM(m) \ + m(1) m(2) m(3) m(4) m(5) m(6) m(7) m(8) m(9) m(10) m(11) m(12) m(13) m(14) m(15) m(16) +//[[[end]]] + + +namespace detail +{ +// Type-opaque holder for an argument to format(), with associated actions on +// the type held as explicit function pointers. This allows FormatArg's for +// each argument to be allocated as a homogenous array inside FormatList +// whereas a naive implementation based on inheritance does not. +class FormatArg +{ +public: + FormatArg() {} + + template + FormatArg(const T& value) + : m_value(static_cast(&value)), + m_formatImpl(&formatImpl), + m_toIntImpl(&toIntImpl) + { } + + void format(std::ostream& out, const char* fmtBegin, + const char* fmtEnd, int ntrunc) const + { + m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value); + } + + int toInt() const + { + return m_toIntImpl(m_value); + } + +private: + template + TINYFORMAT_HIDDEN static void formatImpl(std::ostream& out, const char* fmtBegin, + const char* fmtEnd, int ntrunc, const void* value) + { + formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast(value)); + } + + template + TINYFORMAT_HIDDEN static int toIntImpl(const void* value) + { + return convertToInt::invoke(*static_cast(value)); + } + + const void* m_value; + void (*m_formatImpl)(std::ostream& out, const char* fmtBegin, + const char* fmtEnd, int ntrunc, const void* value); + int (*m_toIntImpl)(const void* value); +}; + + +// Parse and return an integer from the string c, as atoi() +// On return, c is set to one past the end of the integer. +inline int parseIntAndAdvance(const char*& c) +{ + int i = 0; + for(;*c >= '0' && *c <= '9'; ++c) + i = 10*i + (*c - '0'); + return i; +} + +// Print literal part of format string and return next format spec +// position. +// +// Skips over any occurrences of '%%', printing a literal '%' to the +// output. The position of the first % character of the next +// nontrivial format spec is returned, or the end of string. +inline const char* printFormatStringLiteral(std::ostream& out, const char* fmt) +{ + const char* c = fmt; + for(;; ++c) { + switch(*c) + { + case '\0': + out.write(fmt, c - fmt); + return c; + case '%': + out.write(fmt, c - fmt); + if(*(c+1) != '%') + return c; + // for "%%", tack trailing % onto next literal section. + fmt = ++c; + break; + default: + break; + } + } +} + + +// Parse a format string and set the stream state accordingly. +// +// The format mini-language recognized here is meant to be the one from C99, +// with the form "%[flags][width][.precision][length]type". +// +// Formatting options which can't be natively represented using the ostream +// state are returned in spacePadPositive (for space padded positive numbers) +// and ntrunc (for truncating conversions). argIndex is incremented if +// necessary to pull out variable width and precision . The function returns a +// pointer to the character after the end of the current format spec. +inline const char* streamStateFromFormat(std::ostream& out, bool& spacePadPositive, + int& ntrunc, const char* fmtStart, + const detail::FormatArg* formatters, + int& argIndex, int numFormatters) +{ + if (*fmtStart != '%') { + TINYFORMAT_ERROR("tinyformat: Not enough conversion specifiers in format string"); + return fmtStart; + } + // Reset stream state to defaults. + out.width(0); + out.precision(6); + out.fill(' '); + // Reset most flags; ignore irrelevant unitbuf & skipws. + out.unsetf(std::ios::adjustfield | std::ios::basefield | + std::ios::floatfield | std::ios::showbase | std::ios::boolalpha | + std::ios::showpoint | std::ios::showpos | std::ios::uppercase); + bool precisionSet = false; + bool widthSet = false; + int widthExtra = 0; + const char* c = fmtStart + 1; + // 1) Parse flags + for (;; ++c) { + switch (*c) { + case '#': + out.setf(std::ios::showpoint | std::ios::showbase); + continue; + case '0': + // overridden by left alignment ('-' flag) + if (!(out.flags() & std::ios::left)) { + // Use internal padding so that numeric values are + // formatted correctly, eg -00010 rather than 000-10 + out.fill('0'); + out.setf(std::ios::internal, std::ios::adjustfield); + } + continue; + case '-': + out.fill(' '); + out.setf(std::ios::left, std::ios::adjustfield); + continue; + case ' ': + // overridden by show positive sign, '+' flag. + if (!(out.flags() & std::ios::showpos)) + spacePadPositive = true; + continue; + case '+': + out.setf(std::ios::showpos); + spacePadPositive = false; + widthExtra = 1; + continue; + default: + break; + } + break; + } + // 2) Parse width + if (*c >= '0' && *c <= '9') { + widthSet = true; + out.width(parseIntAndAdvance(c)); + } + if (*c == '*') { + widthSet = true; + int width = 0; + if(argIndex < numFormatters) + width = formatters[argIndex++].toInt(); + else + TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable width"); + if(width < 0) { + // negative widths correspond to '-' flag set + out.fill(' '); + out.setf(std::ios::left, std::ios::adjustfield); + width = -width; + } + out.width(width); + ++c; + } + // 3) Parse precision + if (*c == '.') { + ++c; + int precision = 0; + if (*c == '*') { + ++c; + if(argIndex < numFormatters) + precision = formatters[argIndex++].toInt(); + else + TINYFORMAT_ERROR("tinyformat: Not enough arguments to read variable precision"); + } else { + if (*c >= '0' && *c <= '9') + precision = parseIntAndAdvance(c); + else if (*c == '-') // negative precisions ignored, treated as zero. + parseIntAndAdvance(++c); + } + out.precision(precision); + precisionSet = true; + } + // 4) Ignore any C99 length modifier + while (*c == 'l' || *c == 'h' || *c == 'L' || + *c == 'j' || *c == 'z' || *c == 't') + ++c; + // 5) We're up to the conversion specifier character. + // Set stream flags based on conversion specifier (thanks to the + // boost::format class for forging the way here). + bool intConversion = false; + switch (*c) { + case 'u': + case 'd': + case 'i': + out.setf(std::ios::dec, std::ios::basefield); + intConversion = true; + break; + case 'o': + out.setf(std::ios::oct, std::ios::basefield); + intConversion = true; + break; + case 'X': + out.setf(std::ios::uppercase); + case 'x': + case 'p': + out.setf(std::ios::hex, std::ios::basefield); + intConversion = true; + break; + case 'E': + out.setf(std::ios::uppercase); + case 'e': + out.setf(std::ios::scientific, std::ios::floatfield); + out.setf(std::ios::dec, std::ios::basefield); + break; + case 'F': + out.setf(std::ios::uppercase); + case 'f': + out.setf(std::ios::fixed, std::ios::floatfield); + break; + case 'G': + out.setf(std::ios::uppercase); + case 'g': + out.setf(std::ios::dec, std::ios::basefield); + // As in boost::format, let stream decide float format. + out.flags(out.flags() & ~std::ios::floatfield); + break; + case 'a': + case 'A': + TINYFORMAT_ERROR("tinyformat: the %a and %A conversion specs " + "are not supported"); + break; + case 'c': + // Handled as special case inside formatValue() + break; + case 's': + if (precisionSet) + ntrunc = static_cast(out.precision()); + // Make %s print booleans as "true" and "false" + out.setf(std::ios::boolalpha); + break; + case 'n': + // Not supported - will cause problems! + TINYFORMAT_ERROR("tinyformat: %n conversion spec not supported"); + break; + case '\0': + TINYFORMAT_ERROR("tinyformat: Conversion spec incorrectly " + "terminated by end of string"); + return c; + default: + break; + } + if (intConversion && precisionSet && !widthSet) { + // "precision" for integers gives the minimum number of digits (to be + // padded with zeros on the left). This isn't really supported by the + // iostreams, but we can approximately simulate it with the width if + // the width isn't otherwise used. + out.width(out.precision() + widthExtra); + out.setf(std::ios::internal, std::ios::adjustfield); + out.fill('0'); + } + return c + 1; +} + + +//------------------------------------------------------------------------------ +inline void formatImpl(std::ostream& out, const char* fmt, + const detail::FormatArg* formatters, + int numFormatters) +{ + // Saved stream state + std::streamsize origWidth = out.width(); + std::streamsize origPrecision = out.precision(); + std::ios::fmtflags origFlags = out.flags(); + char origFill = out.fill(); + + for (int argIndex = 0; argIndex < numFormatters; ++argIndex) + { + // Parse the format string + fmt = printFormatStringLiteral(out, fmt); + bool spacePadPositive = false; + int ntrunc = -1; + const char* fmtEnd = streamStateFromFormat(out, spacePadPositive, ntrunc, fmt, + formatters, argIndex, numFormatters); + if (argIndex >= numFormatters) + { + // Check args remain after reading any variable width/precision + TINYFORMAT_ERROR("tinyformat: Not enough format arguments"); + return; + } + const FormatArg& arg = formatters[argIndex]; + // Format the arg into the stream. + if(!spacePadPositive) + arg.format(out, fmt, fmtEnd, ntrunc); + else + { + // The following is a special case with no direct correspondence + // between stream formatting and the printf() behaviour. Simulate + // it crudely by formatting into a temporary string stream and + // munging the resulting string. + std::ostringstream tmpStream; + tmpStream.copyfmt(out); + tmpStream.setf(std::ios::showpos); + arg.format(tmpStream, fmt, fmtEnd, ntrunc); + std::string result = tmpStream.str(); // allocates... yuck. + for(size_t i = 0, iend = result.size(); i < iend; ++i) + if(result[i] == '+') result[i] = ' '; + out << result; + } + fmt = fmtEnd; + } + + // Print remaining part of format string. + fmt = printFormatStringLiteral(out, fmt); + if(*fmt != '\0') + TINYFORMAT_ERROR("tinyformat: Too many conversion specifiers in format string"); + + // Restore stream state + out.width(origWidth); + out.precision(origPrecision); + out.flags(origFlags); + out.fill(origFill); +} + +} // namespace detail + + +/// List of template arguments format(), held in a type-opaque way. +/// +/// A const reference to FormatList (typedef'd as FormatListRef) may be +/// conveniently used to pass arguments to non-template functions: All type +/// information has been stripped from the arguments, leaving just enough of a +/// common interface to perform formatting as required. +class FormatList +{ +public: + FormatList(detail::FormatArg* formatters, int N) + : m_formatters(formatters), m_N(N) { } + + friend void vformat(std::ostream& out, const char* fmt, + const FormatList& list); + +private: + const detail::FormatArg* m_formatters; + int m_N; +}; + +/// Reference to type-opaque format list for passing to vformat() +typedef const FormatList& FormatListRef; + + +namespace detail { + +// Format list subclass with fixed storage to avoid dynamic allocation +template +class FormatListN : public FormatList +{ +public: +#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES + template + FormatListN(const Args&... args) + : FormatList(&m_formatterStore[0], N), + m_formatterStore { FormatArg(args)... } + { static_assert(sizeof...(args) == N, "Number of args must be N"); } +#else // C++98 version + void init(int) {} +# define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \ + \ + template \ + FormatListN(TINYFORMAT_VARARGS(n)) \ + : FormatList(&m_formatterStore[0], n) \ + { assert(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \ + \ + template \ + void init(int i, TINYFORMAT_VARARGS(n)) \ + { \ + m_formatterStore[i] = FormatArg(v1); \ + init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \ + } + + TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR) +# undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR +#endif +private: + FormatArg m_formatterStore[N]; +}; + +// Special 0-arg version - MSVC says zero-sized C array in struct is nonstandard +template<> class FormatListN<0> : public FormatList +{ + public: FormatListN() : FormatList(0, 0) {} +}; + +} // namespace detail + + +//------------------------------------------------------------------------------ +// Primary API functions + +#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES + +/// Make type-agnostic format list from list of template arguments. +/// +/// The exact return type of this function is an implementation detail and +/// shouldn't be relied upon. Instead it should be stored as a FormatListRef: +/// +/// FormatListRef formatList = makeFormatList( /*...*/ ); +template +detail::FormatListN makeFormatList(const Args&... args) +{ + return detail::FormatListN(args...); +} + +#else // C++98 version + +inline detail::FormatListN<0> makeFormatList() +{ + return detail::FormatListN<0>(); +} +#define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \ +template \ +detail::FormatListN makeFormatList(TINYFORMAT_VARARGS(n)) \ +{ \ + return detail::FormatListN(TINYFORMAT_PASSARGS(n)); \ +} +TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_MAKEFORMATLIST) +#undef TINYFORMAT_MAKE_MAKEFORMATLIST + +#endif + +/// Format list of arguments to the stream according to the given format string. +/// +/// The name vformat() is chosen for the semantic similarity to vprintf(): the +/// list of format arguments is held in a single function argument. +inline void vformat(std::ostream& out, const char* fmt, FormatListRef list) +{ + detail::formatImpl(out, fmt, list.m_formatters, list.m_N); +} + + +#ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES + +/// Format list of arguments to the stream according to given format string. +template +void format(std::ostream& out, const char* fmt, const Args&... args) +{ + vformat(out, fmt, makeFormatList(args...)); +} + +/// Format list of arguments according to the given format string and return +/// the result as a string. +template +std::string format(const char* fmt, const Args&... args) +{ + std::ostringstream oss; + format(oss, fmt, args...); + return oss.str(); +} + +/// Format list of arguments to std::cout, according to the given format string +template +void printf(const char* fmt, const Args&... args) +{ + format(std::cout, fmt, args...); +} + +template +void printfln(const char* fmt, const Args&... args) +{ + format(std::cout, fmt, args...); + std::cout << '\n'; +} + +#else // C++98 version + +inline void format(std::ostream& out, const char* fmt) +{ + vformat(out, fmt, makeFormatList()); +} + +inline std::string format(const char* fmt) +{ + std::ostringstream oss; + format(oss, fmt); + return oss.str(); +} + +inline void printf(const char* fmt) +{ + format(std::cout, fmt); +} + +inline void printfln(const char* fmt) +{ + format(std::cout, fmt); + std::cout << '\n'; +} + +#define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \ + \ + template \ + void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \ + { \ + vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \ + } \ + \ + template \ + std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \ + { \ + std::ostringstream oss; \ + format(oss, fmt, TINYFORMAT_PASSARGS(n)); \ + return oss.str(); \ + } \ + \ + template \ + void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \ + { \ + format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ + } \ + \ + template \ + void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \ + { \ + format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \ + std::cout << '\n'; \ + } + +TINYFORMAT_FOREACH_ARGNUM(TINYFORMAT_MAKE_FORMAT_FUNCS) +#undef TINYFORMAT_MAKE_FORMAT_FUNCS +#endif + +// Added for Bitcoin Core +template +std::string format(const std::string &fmt, const Args&... args) +{ + std::ostringstream oss; + format(oss, fmt.c_str(), args...); + return oss.str(); +} + +} // namespace tinyformat + +#define strprintf tfm::format + +#endif // TINYFORMAT_H_INCLUDED From 8ae485a00cec382a9b6dc990b83f51032ea86b06 Mon Sep 17 00:00:00 2001 From: zimbora Date: Sat, 27 Apr 2024 13:23:48 +0100 Subject: [PATCH 5/8] logging: avoids double include --- src/bootstrap/logging.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/logging.h b/src/bootstrap/logging.h index 206e984980..2ea9214ff8 100644 --- a/src/bootstrap/logging.h +++ b/src/bootstrap/logging.h @@ -1,5 +1,5 @@ -#ifndef LOGGING_H -#define LOGGING_H +#ifndef BITCOIN_LOGGING_H +#define BITCOIN_LOGGING_H #include "tinyformat.h" #include // For perror From e43765e1a500b02927678006c1480be66c8dd61c Mon Sep 17 00:00:00 2001 From: zimbora Date: Sat, 27 Apr 2024 15:07:52 +0100 Subject: [PATCH 6/8] adds macro to import logging file from different locations --- src/bootstrap/bootstrap.h | 6 +++++- src/bootstrap/makefile | 8 ++++---- src/bootstrap/test.cpp | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/bootstrap.h b/src/bootstrap/bootstrap.h index 4bdf9d88ec..73b7a81692 100644 --- a/src/bootstrap/bootstrap.h +++ b/src/bootstrap/bootstrap.h @@ -15,7 +15,11 @@ #include #include "minizip/unzip.h" -#include "logging.h" +#ifdef BOOTSTRAPTEST + #include "logging.h" +#else + #include "../logging.h" +#endif #ifndef TICKER #define TICKER "__DSW__" diff --git a/src/bootstrap/makefile b/src/bootstrap/makefile index a795bc63df..5420ef50e4 100644 --- a/src/bootstrap/makefile +++ b/src/bootstrap/makefile @@ -23,7 +23,7 @@ curl_test: #linux_x86=true make test test: - $(CXX) \ + $(CXX) -DBOOTSTRAPTEST \ -I$(work_dir)/../../depends/$(arch)/include \ -L$(work_dir)/../../depends/$(arch)/lib \ -L$(work_dir)/../../depends/$(arch)/lib64 \ @@ -31,7 +31,7 @@ test: -lcurl -pthread -lz -lssl -lcrypto -lboost_system-mt-x64 -lboost_filesystem-mt-x64 docker_macos_test: - /DSW/depends/x86_64-apple-darwin14/native/bin/clang++ \ + /DSW/depends/x86_64-apple-darwin14/native/bin/clang++ -DBOOTSTRAPTEST \ -target x86_64-apple-darwin14 -mmacosx-version-min=10.10 --sysroot /DSW/depends/SDKs/MacOSX10.11.sdk -mlinker-version=253.9 -stdlib=libc++ \ -I/DSW/depends/x86_64-apple-darwin14/include \ -L/DSW/depends/x86_64-apple-darwin14/lib \ @@ -40,7 +40,7 @@ docker_macos_test: -lcurl -DCURL_STATICLIB `/DSW/depends/x86_64-apple-darwin14/bin/curl-config --cflags --static-libs` -pthread -lz -lssl -lcrypto -lboost_system-mt-x64 -lboost_filesystem-mt-x64 docker_linux_test: - $(CXX) \ + $(CXX) -DBOOTSTRAPTEST \ -I/DSW/depends/x86_64-pc-linux-gnu/include \ -L/DSW/depends/x86_64-pc-linux-gnu/lib \ -L/DSW/depends/x86_64-pc-linux-gnu/lib64 \ @@ -48,7 +48,7 @@ docker_linux_test: -lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64 docker_windows_test: - /usr/bin/x86_64-w64-mingw32-g++ -static \ + /usr/bin/x86_64-w64-mingw32-g++ -DBOOTSTRAPTEST -static \ -I/DSW/depends/x86_64-w64-mingw32/include \ -L/DSW/depends/x86_64-w64-mingw32/lib \ -L/DSW/depends/x86_64-w64-mingw32/lib64 \ diff --git a/src/bootstrap/test.cpp b/src/bootstrap/test.cpp index d09cb172c5..e83c54657c 100644 --- a/src/bootstrap/test.cpp +++ b/src/bootstrap/test.cpp @@ -78,7 +78,7 @@ int main() { } if(Bootstrap::isDirectory(extractPath)) - Bootstrap::rmDirectory(extractPath); + Bootstrap::rmDirectory(extractPath); } else { std::cerr << "Error downloading file." << std::endl; From 7b99bcd02195868227ce5e8b9eaa4bc9420ad04c Mon Sep 17 00:00:00 2001 From: pedro-at-decenomy Date: Tue, 30 Apr 2024 17:15:15 +0100 Subject: [PATCH 7/8] only adjusts the reward value by integer values --- src/rewards.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rewards.cpp b/src/rewards.cpp index f70a97c28c..fb617d6a8d 100644 --- a/src/rewards.cpp +++ b/src/rewards.cpp @@ -270,7 +270,7 @@ bool CRewards::ConnectBlock(CBlockIndex* pindex, CAmount nSubsidy, CCoinsViewCac oss << "CRewards::" << __func__ << " nDampedDelta: " << FormatMoney(nDampedDelta) << std::endl; // adjust the reward for this epoch - nNewSubsidy = nSubsidy - nDampedDelta; + nNewSubsidy = ((nSubsidy - nDampedDelta) / COIN) * COIN; oss << "CRewards::" << __func__ << " Adjustment at height " << nHeight << ": " << FormatMoney(nSubsidy) << " => " << FormatMoney(nNewSubsidy) << std::endl; } From 048670423590db02e20331ecf56a665ac8dfd274 Mon Sep 17 00:00:00 2001 From: zimbora Date: Thu, 2 May 2024 11:56:33 +0100 Subject: [PATCH 8/8] rewards: fixes block rewards --- src/rewards.cpp | 55 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/rewards.cpp b/src/rewards.cpp index fb617d6a8d..707b8a26bc 100644 --- a/src/rewards.cpp +++ b/src/rewards.cpp @@ -347,19 +347,48 @@ CAmount CRewards::GetBlockValue(int nHeight) CAmount nSubsidy; // ---- Static reward table ---- - if (nHeight == 1) { - nSubsidy = 30000000 * COIN; // __DSW__ coin supply (30M) - } else if (nHeight <= 100000) { - nSubsidy = 100 * COIN; - } else if (nHeight > 100000 && nHeight <= 200000) { - nSubsidy = 125 * COIN; - } else if (nHeight > 200000 && nHeight <= 300000) { - nSubsidy = 150 * COIN; - } else if (nHeight > 300000 && nHeight <= 400000) { - nSubsidy = 125 * COIN; - } else if (nHeight > 400000) { - nSubsidy = 100 * COIN; - } + if (nHeight > 1700000) + nSubsidy = 80.00 * COIN; + else if (nHeight > 1600000) + nSubsidy = 120.00 * COIN; + else if (nHeight > 1500000) + nSubsidy = 140.00 * COIN; + else if (nHeight > 1400000) + nSubsidy = 220.00 * COIN; + else if (nHeight > 1300000) + nSubsidy = 300.00 * COIN; + else if (nHeight > 1200000) + nSubsidy = 380.00 * COIN; + else if (nHeight > 1100000) + nSubsidy = 440.00 * COIN; + else if (nHeight > 1000000) + nSubsidy = 600.00 * COIN; + else if (nHeight > 900000) + nSubsidy = 700.00 * COIN; + else if (nHeight > 800000) + nSubsidy = 440.00 * COIN; + else if (nHeight > 700000) + nSubsidy = 300.00 * COIN; + else if (nHeight > 600000) + nSubsidy = 140.00 * COIN; + else if (nHeight > 525000) + nSubsidy = 80.00 * COIN; + else if (nHeight > 500000) + nSubsidy = 60.00 * COIN; + else if (nHeight > 350000) + nSubsidy = 25.00 * COIN; + else if (nHeight > 292500) + nSubsidy = 4.50 * COIN; + else if (nHeight > 292000) + nSubsidy = 005.50 * COIN; // +1001 LiquiMining + else if (nHeight > 210241) + nSubsidy = 4.50 * COIN; + else if (nHeight > 1) + nSubsidy = 5.00 * COIN; + else if (nHeight > 0) + nSubsidy = 180000.00 * COIN; + else + nSubsidy = 1 * COIN; // ---- Static reward table ---- if (masternodeSync.IsSynced() &&