Skip to content

Commit

Permalink
Merge pull request #113 from BlockMechanic/segwit
Browse files Browse the repository at this point in the history
DDD?
  • Loading branch information
BlockMechanic authored Sep 30, 2022
2 parents baf6943 + f48ec6e commit 168ec3c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 100 deletions.
7 changes: 5 additions & 2 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {

bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
CCoinsMap::const_iterator it = FetchCoin(outpoint);

LogPrintf("%s: %s\n", __func__, it->second.coin.IsSpent() ? "true": "false");

return (it != cacheCoins.end() && !it->second.coin.IsSpent());
}

Expand Down Expand Up @@ -277,8 +280,8 @@ CAmountMap CCoinsViewCache::GetValueInMap(const CTransaction& tx) const
}else{
asset = Params().GetConsensus().subsidy_asset;
amount = AccessCoin(prevout).out.nValue;
}
}

nResult[asset] += amount;
}

Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <banman.h>
#include <blockfilter.h>
#include <crown/cache.h>
#include <crown/nodewallet.h>
#include <chain.h>
#include <chainparams.h>
#include <compat/sanity.h>
Expand Down Expand Up @@ -2196,6 +2197,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
}, DUMP_BANS_INTERVAL);

node.scheduler->scheduleEvery(std::bind(&ThreadNodeSync, std::ref(*node.connman)), std::chrono::milliseconds{1000});
node.scheduler->scheduleEvery(std::bind(&NodeMinter, std::ref(Params()), std::ref(*node.connman)), std::chrono::milliseconds{5000});

#if HAVE_SYSTEM
StartupNotify(args);
Expand Down
4 changes: 0 additions & 4 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ class ChainImpl : public Chain
return ::GetAllAssets();
}

void startStake(bool fStake, CWallet *pwallet)override{
::Stake(fStake, pwallet);
}

CContract getContract(std::string name) override
{
return ::GetContract(name);
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class Chain
virtual CAsset getAsset(std::string sAssetName) = 0;
virtual std::vector<CAsset> getAllAssets() =0;
virtual CTxMemPool& getMempool() = 0;
virtual void startStake(bool fStake, CWallet *pwallet) = 0;
//! Get block height above genesis block. Returns 0 for genesis block,
//! 1 for following block, and so on. Returns nullopt for a block not
//! included in the current chain.
Expand Down
34 changes: 2 additions & 32 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,7 @@ void ThreadStakeMiner(CWallet *pwallet)
}
}
}
//
// Create new block
//
//LOCK(pwallet->cs_wallet);
if(pwallet->HaveAvailableCoinsForStaking())
{

// First just create an empty block. No need to process transactions until we know we can create a block
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(dummyscript, pwallet, true));

Expand All @@ -662,11 +657,7 @@ void ThreadStakeMiner(CWallet *pwallet)
LogPrintf("ThreadStakeMiner : ProcessBlock, block not accepted \n");
return;
}
}
else {
LogPrintf("%s: no available coins\n", __func__);
UninterruptibleSleep(std::chrono::seconds{600});
}

}
}
catch (const std::runtime_error &e)
Expand All @@ -682,24 +673,3 @@ void ThreadStakeMiner(CWallet *pwallet)
LogPrintf("ThreadStakeMiner exiting\n");
}

std::thread* stakeThread = nullptr;

void Stake(bool fStake, CWallet *pwallet)
{
if (stakeThread != nullptr)
{
LogPrintf("%s: Destroying Stake thread\n", __func__);

if (stakeThread->joinable())
stakeThread->join();
stakeThread = nullptr;
LogPrintf("%s: Stake thread destroyed \n", __func__);

}
if(fStake)
{
LogPrintf("%s: Creating Stake thread\n", __func__);

stakeThread = new std::thread([&, pwallet] { TraceThread("stake", [&, pwallet] { ThreadStakeMiner(pwallet); }); });
}
}
2 changes: 0 additions & 2 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ class BlockAssembler
* of updated descendants. */
int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs);
};
/** Generate a new block, without valid proof-of-work */
void Stake(bool fStake, CWallet *pwallet);

/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
Expand Down
39 changes: 10 additions & 29 deletions src/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,20 @@ bool CNodeConfig::read(std::string& strErr)
return false;
}
}
/*std::remove(ip.begin(), ip.end(), ' ');
std::size_t pos = ip.find(":");
std::string sport = ip.substr(pos);
int port = stoi(sport);
if(Params().NetworkIDString() == CBaseChainParams::MAIN) {
if(CService(ip).GetPort() != 9340) {
strErr = ("Invalid port detected in ") + getFileName() + "\n" +
strprintf("Line: %d", linenumber) + "\n\"" + line + "\"" + "\n" +
("(must be 9340 for mainnet)");
streamConfig.close();
return false;
}
} else if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
if(CService(ip).GetPort() != 18333) {
strErr = ("Invalid port detected in ") + getFileName() + "\n" +
strprintf("Line: %d", linenumber) + "\n\"" + line + "\"" + "\n" +
("(must be 18333 for testnet)");
streamConfig.close();
return false;
}
} else if(CService(ip).GetPort() == 9340) {
LogPrintf("%s: ALIAS: %s, IP: %s, \n", __func__, alias, ip);
if(CService(ip).GetPort() != Params().GetDefaultPort()) {
strErr = ("Invalid port detected in ") + getFileName() + "\n" +
strprintf("Line: %d", linenumber) + "\n\"" + line + "\"" + "\n" +
("(9340 could be used only on mainnet)");
strprintf("Line: %d\n", linenumber) + strprintf("GOT: %d", CService(ip).GetPort()) + "\n\"" + line + "\"" + "\n" +
strprintf("(GOT %d, must be %d for %s)", CService(ip).GetPort(), Params().GetDefaultPort(), Params().NetworkIDString());
streamConfig.close();
return false;
}
// TODO fix
//if (Params().NetworkIDString() != CBaseChainParams::DEVNET && !(CService(ip).IsIPv4() && CService(ip).IsRoutable())) {
// strErr = _("Invalid Address detected in ") + getFileName() + "\n" +
// strprintf(_("Line: %d"), linenumber) + "\n\"" + line + "\"" + "\n" +
// _("(IPV4 ONLY)");
// streamConfig.close();
// return false;
//}

}*/

add(alias, ip, privKey, txHash, outputIndex);
}
Expand Down
8 changes: 5 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,9 @@ int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)

if (view.HaveCoin(out)){
fClean = false; // overwriting transaction output
//LogPrintf("%s %s \n", __func__, fClean ? "true": "false");

if(!fClean)
LogPrintf("%s PROBLEM %s\n", __func__, !fClean ? "true": "false");
}

if (undo.nHeight == 0) {
Expand Down Expand Up @@ -1940,8 +1942,8 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
if (!is_spent || tx.vpout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.fCoinBase || is_coinstake != coin.fCoinStake) {
if(!tx.vpout[o].IsEmpty())
fClean = false; // transaction output mismatch
//LogPrintf("fClean %s , SPENT %s , OUT %s, CB %s, CS %s\n", fClean ? "true": "false", is_spent ? "true": "false", tx.vpout[o] != coin.out ? "true": "false", is_coinbase != coin.fCoinBase ? "true": "false",is_coinstake != coin.fCoinStake ? "true": "false");
//LogPrintf("VOUT %s \n %s \n", tx.vpout[o].ToString(), coin.out.ToString());
LogPrintf("fClean %s , SPENT %s , OUT %s, CB %s, CS %s\n", fClean ? "true": "false", is_spent ? "true": "false", tx.vpout[o] != coin.out ? "true": "false", is_coinbase != coin.fCoinBase ? "true": "false",is_coinstake != coin.fCoinStake ? "true": "false");
LogPrintf("VOUT %s \n %s \n", tx.vpout[o].ToString(), coin.out.ToString());
}
}
}
Expand Down
24 changes: 0 additions & 24 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5417,27 +5417,3 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat

return ret;
}

bool CWallet::HaveAvailableCoinsForStaking() const
{
std::vector<COutput> vCoins;
CCoinControl coin_control;
LOCK(cs_wallet);

//To do , either map out or make calls for each stakable asset
AvailableCoins(vCoins, true, &coin_control, 1, MAX_MONEY, 0, MAX_MONEY, &Params().GetConsensus().subsidy_asset);

//CAmountMap reserved {{Params().GetConsensus().subsidy_asset,gArgs.GetArg("-reserve-balance", DEFAULT_RESERVE_BALANCE)}};
CAmountMap balance = GetAvailableBalance(&coin_control);

//balance -= reserved;
LogPrintf("Balance = %s, vCoins.size()= %d \n", balance, vCoins.size());
//return (balance > CAmountMap() && vCoins.size() > 0);
return (balance > CAmountMap());

}

void CWallet::Stake(bool fStake)
{
m_chain->startStake(fStake, this, stakeThread);
}
5 changes: 2 additions & 3 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
*/
void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlySafe = true, const CCoinControl* coinControl = nullptr, const CAmount& nMinimumAmount = 1, const CAmount& nMaximumAmount = MAX_MONEY, const CAmount& nMinimumSumAmount = MAX_MONEY, const uint64_t nMaximumCount = 0, const CAsset* asset_filter=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void AvailableCoins2(std::vector<COutput>& vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl = NULL, AvailableCoinsType coin_type=ALL_COINS, bool useIX = false) const;
bool HaveAvailableCoinsForStaking() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
std::map<CTxDestination, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed = true, CAmount maxCoinValue = 0);
/**
* Return list of available coins and locked coins grouped by non-change output address.
Expand Down Expand Up @@ -1364,9 +1363,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
bool GetActiveMasternode(CMasternode*& activeStakingNode);
bool GetActiveSystemnode(CSystemnode*& activeStakingNode);
uint256 GenerateStakeModifier(const CBlockIndex* prewardBlockIndex) const;
bool CreateCoinStake(const int nHeight, const uint32_t& nBits, const uint32_t& nTime, CMutableTransaction& txCoinStake, uint32_t& nTxNewTime, StakePointer& stakePointer);
//bool CreateCoinStake(const int nHeight, const uint32_t& nBits, const uint32_t& nTime, CMutableTransaction& txCoinStake, uint32_t& nTxNewTime, StakePointer& stakePointer);

void Stake(bool fStake);
//void Stake(bool fStake);
};

/**
Expand Down

0 comments on commit 168ec3c

Please sign in to comment.