Skip to content

Commit

Permalink
code refactor and build run
Browse files Browse the repository at this point in the history
  • Loading branch information
akyo8 committed Mar 8, 2021
1 parent 20a1192 commit 810ba16
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 212 deletions.
2 changes: 2 additions & 0 deletions src/chainparams.cpp
Expand Up @@ -202,6 +202,8 @@ class CMainParams : public CChainParams {
fRequireStandard = true;
m_is_test_chain = false;
m_is_mockable_chain = false;
fMiningRequiresPeers = true;
fMiningRequiresPeers = true;

checkpointData = {
{
Expand Down
2 changes: 2 additions & 0 deletions src/chainparams.h
Expand Up @@ -95,6 +95,8 @@ class CChainParams
const CCheckpointData& Checkpoints() const { return checkpointData; }
const ChainTxData& TxData() const { return chainTxData; }
const arith_uint256& ProofOfWorkLimit() const { return consensus.nProofOfWorkLimit; }
bool MiningRequiresPeers() const { return fMiningRequiresPeers; }
bool fMiningRequiresPeers;

// proof of stake
const arith_uint256& ProofOfStakeLimit() const { return consensus.nProofOfStakeLimit; }
Expand Down
2 changes: 1 addition & 1 deletion src/chainparamsbase.cpp
Expand Up @@ -46,7 +46,7 @@ std::unique_ptr<CBaseChainParams> CreateBaseChainParams(const std::string& chain
if (chain == CBaseChainParams::MAIN) {
return MakeUnique<CBaseChainParams>("", 8332, 8334);
} else if (chain == CBaseChainParams::TESTNET) {
return MakeUnique<CBaseChainParams>("testnet3", 18332, 29665);
return MakeUnique<CBaseChainParams>("testnet4", 29665);
} else if (chain == CBaseChainParams::SIGNET) {
return MakeUnique<CBaseChainParams>("signet", 38332, 38334);
} else if (chain == CBaseChainParams::REGTEST) {
Expand Down
8 changes: 7 additions & 1 deletion src/init.cpp
Expand Up @@ -89,7 +89,6 @@ static bool fFeeEstimatesInitialized = false;
static const bool DEFAULT_PROXYRANDOMIZE = true;
static const bool DEFAULT_REST_ENABLE = false;
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;

unsigned int nMinerSleep;

#ifdef WIN32
Expand Down Expand Up @@ -1364,6 +1363,13 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
RegisterZMQRPCCommands(tableRPC);
#endif

#if ENABLE_WALLET
// Generate coins in the background
Staker::SetStaking(gArgs.GetBoolArg("-staking", true));
threadGroup.create_thread(boost::bind(&Staker::CloakStaker, boost::cref(chainparams)));
#endif


/* Start the RPC server already. It will be started in "warmup" mode
* and not really process calls already (but it will signify connections
* that the server is there and will be ready later). Warmup mode will
Expand Down
37 changes: 36 additions & 1 deletion src/miner.cpp
Expand Up @@ -15,12 +15,12 @@
#include <consensus/validation.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <net.h>
#include <pow.h>
#include <primitives/transaction.h>
#include <timedata.h>
#include <util/moneystr.h>
#include <util/system.h>
#include <net.h>

#include <algorithm>
#include <utility>
Expand All @@ -31,6 +31,9 @@ uint64_t nLastBlockWeight = 0;
int64_t nLastCoinStakeSearchInterval = 0;
uint64_t nLastSteadyTime = 0;
uint64_t nLastTime = 0;
extern unsigned int nMinerSleep;
extern std::unique_ptr<CConnman> g_connman;


int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
Expand Down Expand Up @@ -75,6 +78,17 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
return nNewTime - nOldTime;
}

void Staker::SetStaking(bool mode)
{
fStaking = mode;
}

bool Staker::GetStaking()
{
return fStaking;
}


void Staker::CloakStaker(const CChainParams& chainparams)
{
LogPrintf("CloakStaker started\n");
Expand Down Expand Up @@ -172,6 +186,27 @@ void Staker::CloakStaker(const CChainParams& chainparams)
} else {
MilliSleep(nMinerSleep);
}

pblock->nBits = GetNextTargetRequired(pindexPrev, true);
CTransaction txCoinStake;
int64_t nSearchTime = txCoinStake.nTime; // search to current time
if (nSearchTime > nLastCoinStakeSearchTime) {
bool gotCoinStake = false;
GetMainSignals().CreateCoinStake(pblock->nBits, nSearchTime - nLastCoinStakeSearchTime, MakeTransactionRef(txCoinStake), gotCoinStake);
if (gotCoinStake) {
if (txCoinStake.nTime >= std::max(pindexPrev->GetMedianTimePast() + 1, pindexPrev->GetBlockTime() - GetMaxClockDrift(pindexPrev->nHeight + 1))) { // make sure coinstake would meet timestamp protocol
// as it would be the same as the block timestamp
CMutableTransaction tx(*pblock->vtx[0]);
tx.vout[0].SetEmpty();
tx.nTime = txCoinStake.nTime;
pblock->vtx.clear();
pblock->vtx.push_back(MakeTransactionRef(CTransaction(tx)));
}
}
nLastCoinStakeSearchInterval = nSearchTime - nLastCoinStakeSearchTime;
nLastCoinStakeSearchTime = nSearchTime;
}

}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/miner.h
Expand Up @@ -33,6 +33,16 @@ struct CBlockTemplate
std::vector<unsigned char> vchCoinbaseCommitment;
};

class Staker
{
public:
void static CloakStaker(const CChainParams& chainparams);
void static SetStaking(bool mode);
bool static GetStaking();

private:
};

// Container for tracking updates to ancestor feerate as we include (parent)
// transactions in a block
struct CTxMemPoolModifiedEntry {
Expand Down
7 changes: 7 additions & 0 deletions src/net.cpp
Expand Up @@ -128,6 +128,13 @@ uint16_t GetListenPort()
return (uint16_t)(gArgs.GetArg("-port", Params().GetDefaultPort()));
}

bool CConnman::HaveNodes()
{
LOCK(g_connman->cs_vNodes);
return g_connman->vNodes.empty();
}


// find 'best' local address for a particular peer
bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
{
Expand Down
4 changes: 4 additions & 0 deletions src/net.h
Expand Up @@ -194,6 +194,10 @@ class CConnman
CONNECTIONS_OUT = (1U << 1),
CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT),
};
bool HaveNodes();
mutable std::vector<CNode*> vNodes;
mutable CCriticalSection cs_vNodes;
friend class Staker;

struct Options
{
Expand Down
23 changes: 23 additions & 0 deletions src/pos.cpp
Expand Up @@ -579,3 +579,26 @@ bool GetCoinAge(uint64_t& nCoinAge, CTransactionRef tx)
nCoinAge = bnCoinDay.GetLow64();
return true;
}



// ppcoin: total coin age spent in block, in the unit of coin-days.
bool GetCoinAgeBlock(uint64_t& nCoinAge, CBlock block)
{
nCoinAge = 0;
for (const CTransactionRef& tx : block.vtx) {
uint64_t nTxCoinAge;
if (GetCoinAge(nTxCoinAge, tx))
nCoinAge += nTxCoinAge;
else
return false;
}

if (nCoinAge == 0) // block coin age minimum 1 coin-day
nCoinAge = 1;

//if (fDebug && GetBoolArg("-printcoinage"))
// printf("block coin age total nCoinDays=%" PRI64d "\n", nCoinAge);
return true;
}

1 change: 1 addition & 0 deletions src/pos.h
Expand Up @@ -4,6 +4,7 @@

#include "consensus/params.h"
#include "primitives/transaction.h"
#include "primitives/block.h"

#include <stdint.h>

Expand Down
11 changes: 7 additions & 4 deletions src/primitives/block.h
Expand Up @@ -93,11 +93,14 @@ class CBlock : public CBlockHeader
int64_t maxTransactionTime = 0;
for (const CTransactionRef& tx : vtx) {
maxTransactionTime = std::max(maxTransactionTime, (int64_t)tx->nTime);
}
}
return maxTransactionTime;
}

SERIALIZE_METHODS(CBlock, obj)

}
bool GetCoinAge(uint64_t& nCoinAge) const;


SERIALIZE_METHODS(CBlock, obj)
{
READWRITEAS(CBlockHeader, obj);
READWRITE(obj.vtx);
Expand Down
11 changes: 11 additions & 0 deletions src/util.h
Expand Up @@ -129,6 +129,17 @@ inline bool IsSwitchChar(char c)
#define THREAD_PRIORITY_ABOVE_NORMAL 0
#endif


#if defined(__GNUC__) || defined(__clang__)
#define DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED __declspec(deprecated)
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED
#endif


void SetThreadPriority(int nPriority);

enum class OptionsCategory {
Expand Down
14 changes: 14 additions & 0 deletions src/validationinterface.cpp
Expand Up @@ -33,6 +33,9 @@ struct MainSignalsInstance {
struct ListEntry { std::shared_ptr<CValidationInterface> callbacks; int count = 1; };
std::list<ListEntry> m_list GUARDED_BY(m_mutex);
std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map GUARDED_BY(m_mutex);
boost::signals2::signal<void(unsigned int nBits, int64_t nSearchInterval, CTransactionRef txNew, bool& result)> CreateCoinStake;
g_signals.m_internals->CreateCoinStake.connect(boost::bind(&CValidationInterface::CreateCoinStake, pwalletIn, _1, _2, _3, _4));


public:
// We are not allowed to assume the scheduler only runs in one thread,
Expand Down Expand Up @@ -150,6 +153,17 @@ void UnregisterAllValidationInterfaces()
return;
}
g_signals.m_internals->Clear();
g_signals.m_internals->CreateCoinStake.disconnect_all_slots();
}

void CMainSignals::CreateCoinStake(unsigned int nBits, int64_t nSearchInterval, CTransactionRef txNew, bool& result)
{
m_internals->CreateCoinStake(nBits, nSearchInterval, txNew, result);
}

void CMainSignals::SignBlock(CBlock* pblock, bool& result)
{
m_internals->SignBlock(pblock, result);
}

void CallFunctionInValidationInterfaceQueue(std::function<void()> func)
Expand Down
1 change: 1 addition & 0 deletions src/validationinterface.h
Expand Up @@ -38,6 +38,7 @@ void UnregisterAllValidationInterfaces();
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
/** Unregister subscriber */
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
virtual void CreateCoinStake(unsigned int nBits, int64_t nSearchInterval, CTransactionRef txNew, bool& result) {}

/**
* Pushes a function to callback onto the notification queue, guaranteeing any
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/coinselection.cpp
Expand Up @@ -17,6 +17,7 @@ struct {
return a.effective_value > b.effective_value;
}
} descending;
class COutput;

/*
* This is the Branch and Bound Coin Selection algorithm designed by Murch. It searches for an input
Expand Down Expand Up @@ -60,6 +61,8 @@ struct {
* @param CAmount not_input_fees -> The fees that need to be paid for the outputs and fixed size
* overhead (version, locktime, marker and flag)
*/
// COutput output version
bool SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, const CAmount& target_value, const CAmount& cost_of_change, std::set<COutput>& out_set, CAmount& value_ret, CAmount not_input_fees);

static const size_t TOTAL_TRIES = 100000;

Expand Down

0 comments on commit 810ba16

Please sign in to comment.