Skip to content

Commit

Permalink
PoS integration
Browse files Browse the repository at this point in the history
  • Loading branch information
uzyn committed Dec 19, 2019
1 parent 235d997 commit 4e9a4d9
Show file tree
Hide file tree
Showing 102 changed files with 3,089 additions and 2,640 deletions.
68 changes: 34 additions & 34 deletions src/bench/bench_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ static void SetupBenchArgs()

int main(int argc, char** argv)
{
SetupBenchArgs();
std::string error;
if (!gArgs.ParseParameters(argc, argv, error)) {
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
return EXIT_FAILURE;
}

if (HelpRequested(gArgs)) {
std::cout << gArgs.GetHelpMessage();

return EXIT_SUCCESS;
}

int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
bool is_list_only = gArgs.GetBoolArg("-list", false);

double scaling_factor;
if (!ParseDouble(scaling_str, &scaling_factor)) {
tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
return EXIT_FAILURE;
}

std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
if ("plot" == printer_arg) {
printer.reset(new benchmark::PlotlyPrinter(
gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
}

benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);
// SetupBenchArgs(); // TODO: (temp) disable benches
// std::string error;
// if (!gArgs.ParseParameters(argc, argv, error)) {
// tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
// return EXIT_FAILURE;
// }
//
// if (HelpRequested(gArgs)) {
// std::cout << gArgs.GetHelpMessage();
//
// return EXIT_SUCCESS;
// }
//
// int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
// std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);
// std::string scaling_str = gArgs.GetArg("-scaling", DEFAULT_BENCH_SCALING);
// bool is_list_only = gArgs.GetBoolArg("-list", false);
//
// double scaling_factor;
// if (!ParseDouble(scaling_str, &scaling_factor)) {
// tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
// return EXIT_FAILURE;
// }
//
// std::unique_ptr<benchmark::Printer> printer = MakeUnique<benchmark::ConsolePrinter>();
// std::string printer_arg = gArgs.GetArg("-printer", DEFAULT_BENCH_PRINTER);
// if ("plot" == printer_arg) {
// printer.reset(new benchmark::PlotlyPrinter(
// gArgs.GetArg("-plot-plotlyurl", DEFAULT_PLOT_PLOTLYURL),
// gArgs.GetArg("-plot-width", DEFAULT_PLOT_WIDTH),
// gArgs.GetArg("-plot-height", DEFAULT_PLOT_HEIGHT)));
// }
//
// benchmark::BenchRunner::RunAll(*printer, evaluations, scaling_factor, regex_filter, is_list_only);

return EXIT_SUCCESS;
}
2 changes: 2 additions & 0 deletions src/bench/duplicate_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <pos.h>
#include <masternodes/masternodes.h>

#include <txmempool.h>
#include <validation.h>

Expand Down
2 changes: 1 addition & 1 deletion src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <config/bitcoin-config.h>
#endif

#include <chainparams.h>
#include <clientversion.h>
#include <coins.h>
#include <consensus/consensus.h>
#include <core_io.h>
#include <key_io.h>
#include <policy/policy.h>
#include <policy/rbf.h>
#include <primitives/transaction.h>
Expand Down
2 changes: 2 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class CBlockIndex
height = 0;
mintedBlocks = 0;
sig = {};
minter = CKeyID();
}

CBlockIndex()
Expand All @@ -239,6 +240,7 @@ class CBlockIndex
mintedBlocks = block.mintedBlocks;
stakeModifier = block.stakeModifier;
sig = block.sig;
block.ExtractMinterKey(minter);
}

FlatFilePos GetBlockPos() const {
Expand Down
39 changes: 24 additions & 15 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <chainparamsseeds.h>
#include <consensus/merkle.h>
#include <key_io.h>
#include <masternodes/masternodes.h>
#include <streams.h>
#include <tinyformat.h>
#include <util/system.h>
#include <util/strencodings.h>
Expand Down Expand Up @@ -142,6 +142,7 @@ class CMainParams : public CChainParams {

// Masternodes' params
consensus.mn.activationDelay = 1500;
consensus.mn.resignDelay = 300;
consensus.mn.collateralUnlockDelay = 300;
consensus.mn.creationFee = 1 * COIN;
consensus.mn.collateralAmount = 100 * COIN;
Expand Down Expand Up @@ -257,10 +258,11 @@ class CTestNetParams : public CChainParams {
consensus.nMinimumChainWork = uint256S("0x00");

// By default assume that the signatures in ancestors of this block are valid.
consensus.defaultAssumeValid = uint256S("0x0000000000000037a8cd3e06cd5edbfe9dd1dbcc5dacab279376ef7cfc2b4c75"); //1354312
consensus.defaultAssumeValid = uint256S("0x00");

// Masternodes' params
consensus.mn.activationDelay = 10;
consensus.mn.resignDelay = 10;
consensus.mn.collateralUnlockDelay = 10;
consensus.mn.creationFee = 1 * COIN;
consensus.mn.collateralAmount = 10 * COIN;
Expand All @@ -283,11 +285,17 @@ class CTestNetParams : public CChainParams {

bech32_hrp = "tf";

genesis = CreateGenesisBlock(1569396815, 0x1e0fffff, 1, 50 * COIN, CreateGenesisMasternodes());
// owner base58, operator base58
vMasternodes.push_back({"7M3g9CSERjLdXisE5pv2qryDbURUj9Vpi1", "7Grgx69MZJ4wDKRx1bBxLqTnU9T3quKW7n"});
vMasternodes.push_back({"7L29itepC13pgho1X2y7mcuf4WjkBi7x2w", "773MiaEtQK2HAwWj55gyuRiU8tSwowRTTW"});
vMasternodes.push_back({"75Wramp2iARchHedXcn1qRkQtMpSt9Mi3V", "7Ku81yvqbPkxpWjZpZWZZnWydXyzJozZfN"});
vMasternodes.push_back({"7LfqHbyh9dBQDjWB6MxcWvH2PBC5iY4wPa", "75q6ftr3QGfBT3DBu15fVfetP6duAgfhNH"});

genesis = CreateGenesisBlock(1297053593, 0x207fffff, 1, 50 * COIN, CreateGenesisMasternodes()); // 0x1e0fffff
consensus.hashGenesisBlock = genesis.GetHash();

assert(consensus.hashGenesisBlock == uint256S("0xc0f410a59e9aa22afd67ee4671d41c2e3135c0efc589446e4b393cc534d178ac"));
assert(genesis.hashMerkleRoot == uint256S("0x800c7581a09c96d98bdad848db8fc027e8869d28d890ca21f6c25124baf53afe"));
assert(consensus.hashGenesisBlock == uint256S("0x00000e04e92efbd4a99b9f38abd22bc1e97651ab194a72a6390687f99c4d1555"));
assert(genesis.hashMerkleRoot == uint256S("0xc4b5095034837015551b4ef0eb56eab6706b3d102cc44a910c0eee084932ef5b"));

vFixedSeeds.clear();
vSeeds.clear();
Expand All @@ -311,8 +319,7 @@ class CTestNetParams : public CChainParams {
};

chainTxData = ChainTxData{
// Data from rpc: getchaintxstats 4096 0000000000000037a8cd3e06cd5edbfe9dd1dbcc5dacab279376ef7cfc2b4c75
/* nTime */ 1569396815,
/* nTime */ 0,
/* nTxCount */ 0,
/* dTxRate */ 0
};
Expand All @@ -328,21 +335,21 @@ class CRegTestParams : public CChainParams {
strNetworkID = "regtest";
consensus.nSubsidyHalvingInterval = 150;
consensus.BIP16Exception = uint256();
consensus.BIP34Height = 0; // BIP34 activated on regtest (Used in functional tests)
consensus.BIP34Height = 500; // BIP34 activated on regtest (Used in functional tests)
consensus.BIP34Hash = uint256();
consensus.BIP65Height = 0; // BIP65 activated on regtest (Used in functional tests)
consensus.BIP66Height = 0; // BIP66 activated on regtest (Used in functional tests)
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in functional tests)
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in functional tests)
consensus.pos.diffLimit = uint256S("00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.pos.nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.pos.nTargetSpacing = 10 * 60; // 10 minutes
consensus.pos.fAllowMinDifficultyBlocks = false; // only for regtest
consensus.pos.fNoRetargeting = false; // only for regtest
consensus.pos.fAllowMinDifficultyBlocks = true; // only for regtest
consensus.pos.fNoRetargeting = true; // only for regtest

consensus.pos.coinstakeMaturity = 100;

consensus.pos.allowMintingWithoutPeers = true; // don't mint if no peers connected

consensus.CSVHeight = 1; // CSV activated on regtest (Used in rpc activation tests)
consensus.CSVHeight = 432; // CSV activated on regtest (Used in rpc activation tests)
consensus.SegwitHeight = 0; // SEGWIT is always activated on regtest unless overridden
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
Expand All @@ -358,6 +365,7 @@ class CRegTestParams : public CChainParams {

// Masternodes' params
consensus.mn.activationDelay = 10;
consensus.mn.resignDelay = 10;
consensus.mn.collateralUnlockDelay = 10;
consensus.mn.creationFee = 1 * COIN;
consensus.mn.collateralAmount = 10 * COIN;
Expand Down Expand Up @@ -390,8 +398,9 @@ class CRegTestParams : public CChainParams {

genesis = CreateGenesisBlock(1296688602, 0x207fffff, 1, 50 * COIN, CreateGenesisMasternodes());
consensus.hashGenesisBlock = genesis.GetHash();
assert(consensus.hashGenesisBlock == uint256S("0x000006772a3244d0a5a4911a5cb1d7e910e175f4e4b77c755018459122fa7a89"));
assert(genesis.hashMerkleRoot == uint256S("0x2faeba8cd467cb0df14c48d30b57736f1ed3275790401d127a03d55037df7b5c"));

assert(consensus.hashGenesisBlock == uint256S("0x5deed118789bf5acdce79974f15b9a1cffb924875f52e702ca93d926d290b31f"));
assert(genesis.hashMerkleRoot == uint256S("0x955d1c4074ccd34639cae5d1234bbef645b34866bfd18f083dd80928618e80fb"));

vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
vSeeds.clear(); //!< Regtest mode doesn't have any DNS seeds.
Expand Down
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <chainparamsbase.h>
#include <consensus/params.h>
#include <key_io.h>
#include <primitives/block.h>
#include <protocol.h>

Expand Down
1 change: 1 addition & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct Params {
CAmount creationFee;
CAmount collateralAmount;
int activationDelay;
int resignDelay;
int collateralUnlockDelay;
int historyFrame;
};
Expand Down
1 change: 0 additions & 1 deletion src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <chainparams.h>
#include <crypto/hmac_sha256.h>
#include <httpserver.h>
#include <key_io.h>
#include <rpc/protocol.h>
#include <rpc/server.h>
#include <sync.h>
Expand Down
88 changes: 45 additions & 43 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <interfaces/chain.h>
#include <key.h>
#include <key_io.h>
#include <masternodes/masternodes.h>
#include <masternodes/mn_txdb.h>
#include <miner.h>
#include <net.h>
Expand Down Expand Up @@ -503,6 +502,7 @@ void SetupServerArgs()
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-debugexclude=<category>", strprintf("Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except one or more specified categories."), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-gen", strprintf("Generate coins (default: %u)", DEFAULT_GENERATE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
gArgs.AddArg("-logthreadnames", strprintf("Prepend debug output with name of the originating thread (only available on platforms supporting thread_local) (default: %u)", DEFAULT_LOGTHREADNAMES), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
Expand Down Expand Up @@ -1835,54 +1835,56 @@ bool AppInitMain(InitInterfaces& interfaces)
}, DUMP_BANS_INTERVAL * 1000);

// ********************************************************* Step 14: start minter thread
pos::ThreadStaker::Args stakerParams{};
{
CKey key;
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
if (wallets.size() == 0) {
LogPrintf("Warning! wallets not found");
return true;
}
std::shared_ptr<CWallet> defaultWallet = wallets[0];
if(gArgs.GetBoolArg("-gen", DEFAULT_GENERATE)) {
LOCK(cs_main);

CTxDestination destination;
auto myIDs = pmasternodesview->AmIOperator();
if (myIDs)
{
pos::ThreadStaker::Args stakerParams{};
{
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
if (wallets.size() == 0) {
LogPrintf("Warning! wallets not found\n");
return true;
}
std::shared_ptr<CWallet> defaultWallet = wallets[0];

std::string minterAddress = gArgs.GetArg("-minterAddress", "");
if (minterAddress != "") {
destination = DecodeDestination(minterAddress);
if (!IsValidDestination(destination)) {
LogPrintf("Error: coinstake destination is invalid");
return false;
}
} else {
std::string strErr;
if(!defaultWallet->GetNewDestination(OutputType::LEGACY, "", destination, strErr)) {
LogPrintf("%s\n", strErr);
return false;
}
}
CKey minterKey;
bool found =false;
for (auto&& wallet : wallets) {
if (wallet->GetKey(myIDs->operatorAuthAddress, minterKey)) {
found = true;
break;
}
}
if (!found) {
LogPrintf("Error: masternode operator private key not found\n");
return false;
}

CScript coinbaseScript = GetScriptForDestination(destination);
CKey minterKey;
if (!defaultWallet->GetKey(CKeyID(*boost::get<PKHash>(&destination)), minterKey)) {
LogPrintf("Error: private key not found");
return false;
}
CMasternode const & node = *pmasternodesview->ExistMasternode(myIDs->id);
CTxDestination destination = node.operatorType == 1 ? CTxDestination(PKHash(node.operatorAuthAddress)) : CTxDestination(WitnessV0KeyHash(node.operatorAuthAddress));

stakerParams.coinbaseScript = coinbaseScript;
stakerParams.minterKey = minterKey;
}
CScript coinbaseScript = GetScriptForDestination(destination);

// Mint proof-of-stake blocks in background
threadGroup.create_thread([=]() {
TraceThread("CoinStaker", [=]() {
// Fill Staker Args
stakerParams.coinbaseScript = coinbaseScript;
stakerParams.minterKey = minterKey;
stakerParams.masternodeID = myIDs->id;
}

// Mint proof-of-stake blocks in background
threadGroup.create_thread([=]() {
TraceThread("CoinStaker", [=]() {
// Fill Staker Args

// Run ThreadStaker
pos::ThreadStaker threadStaker{};
threadStaker(stakerParams, chainparams);
});
});
// Run ThreadStaker
pos::ThreadStaker threadStaker{};
threadStaker(stakerParams, chainparams);
});
});
}
}

return true;
}
3 changes: 1 addition & 2 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <key_io.h>

#include <base58.h>
#include <bech32.h>
#include <chainparams.h>
#include <script/script.h>
#include <util/strencodings.h>

Expand Down
3 changes: 2 additions & 1 deletion src/key_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#ifndef BITCOIN_KEY_IO_H
#define BITCOIN_KEY_IO_H

#include <chainparams.h>
#include <key.h>
#include <pubkey.h>
#include <script/standard.h>

#include <string>

class CChainParams;

CKey DecodeSecret(const std::string& str);
std::string EncodeSecret(const CKey& key);

Expand Down
Loading

0 comments on commit 4e9a4d9

Please sign in to comment.