Skip to content

Commit

Permalink
Move nBetValue out of CTxOut and clean up vExpectedPayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kokary authored and Cryptarchist committed May 1, 2019
1 parent 198f7d4 commit fd62b54
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 44 deletions.
34 changes: 17 additions & 17 deletions src/bet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ bool IsValidOracleTx(const CTxIn &txin)
* Takes a payout vector and aggregates the total WGR that is required to pay out all bets.
* We also calculate and add the OMNO and dev fund rewards.
*
* @param vexpectedPayouts A vector containing all the winning bets that need to be paid out.
* @param vExpectedPayouts A vector containing all the winning bets that need to be paid out.
* @param nMNBetReward The Oracle masternode reward.
* @return
*/
int64_t GetBlockPayouts(std::vector<CTxOut>& vexpectedPayouts, CAmount& nMNBetReward)
int64_t GetBlockPayouts(std::vector<CBetOut>& vExpectedPayouts, CAmount& nMNBetReward)
{
CAmount profitAcc = 0;
CAmount nPayout = 0;
Expand All @@ -77,23 +77,23 @@ int64_t GetBlockPayouts(std::vector<CTxOut>& vexpectedPayouts, CAmount& nMNBetRe
std::string OMNOPayoutAddr = Params().OMNOPayoutAddr();

// Loop over the payout vector and aggregate values.
for (unsigned i = 0; i < vexpectedPayouts.size(); i++) {
CAmount betValue = vexpectedPayouts[i].nBetValue;
CAmount payValue = vexpectedPayouts[i].nValue;
for (unsigned i = 0; i < vExpectedPayouts.size(); i++) {
CAmount betValue = vExpectedPayouts[i].nBetValue;
CAmount payValue = vExpectedPayouts[i].nValue;

totalAmountBet += betValue;
profitAcc += payValue - betValue;
nPayout += payValue;
}

if (vexpectedPayouts.size() > 0) {
if (vExpectedPayouts.size() > 0) {
// Calculate the OMNO reward and the Dev reward.
CAmount nOMNOReward = (CAmount)(profitAcc * Params().OMNORewardPermille() / (1000.0 - Params().BetXPermille()));
CAmount nDevReward = (CAmount)(profitAcc * Params().DevRewardPermille() / (1000.0 - Params().BetXPermille()));

// Add both reward payouts to the payout vector.
vexpectedPayouts.emplace_back(nDevReward, GetScriptForDestination(CBitcoinAddress(devPayoutAddr).Get()));
vexpectedPayouts.emplace_back(nOMNOReward, GetScriptForDestination(CBitcoinAddress(OMNOPayoutAddr).Get()));
vExpectedPayouts.emplace_back(nDevReward, GetScriptForDestination(CBitcoinAddress(devPayoutAddr).Get()));
vExpectedPayouts.emplace_back(nOMNOReward, GetScriptForDestination(CBitcoinAddress(OMNOPayoutAddr).Get()));

nPayout += nDevReward + nOMNOReward;
}
Expand All @@ -108,7 +108,7 @@ int64_t GetBlockPayouts(std::vector<CTxOut>& vexpectedPayouts, CAmount& nMNBetRe
* @param nMNBetReward The Oracle masternode reward.
* @return
*/
int64_t GetCGBlockPayouts(std::vector<CTxOut>& vexpectedCGPayouts, CAmount& nMNBetReward)
int64_t GetCGBlockPayouts(std::vector<CBetOut>& vexpectedCGPayouts, CAmount& nMNBetReward)
{
CAmount nPayout = 0;

Expand All @@ -127,7 +127,7 @@ int64_t GetCGBlockPayouts(std::vector<CTxOut>& vexpectedCGPayouts, CAmount& nMNB
* @param nHeight - The current chain height.
* @return
*/
bool IsBlockPayoutsValid(std::vector<CTxOut> vExpectedPayouts, CBlock block)
bool IsBlockPayoutsValid(std::vector<CBetOut> vExpectedPayouts, CBlock block)
{
unsigned long size = vExpectedPayouts.size();

Expand Down Expand Up @@ -1730,9 +1730,9 @@ std::vector<CPeerlessResult> getEventResults( int height )
*
* @return payout vector.
*/
std::vector<CTxOut> GetBetPayouts(int height)
std::vector<CBetOut> GetBetPayouts(int height)
{
std::vector<CTxOut> vexpectedPayouts;
std::vector<CBetOut> vExpectedPayouts;
int nCurrentHeight = chainActive.Height();

// Get all the results posted in the latest block.
Expand Down Expand Up @@ -1970,7 +1970,7 @@ std::vector<CTxOut> GetBetPayouts(int height)

LogPrintf("Result found ending search \n");

return vexpectedPayouts;
return vExpectedPayouts;
}

// Only payout bets that are between 25 - 10000 WRG inclusive (MaxBetPayoutRange).
Expand Down Expand Up @@ -2036,7 +2036,7 @@ std::vector<CTxOut> GetBetPayouts(int height)
// Only add valid payouts to the vector.
if (payout > 0) {
// Add winning bet payout to the bet vector.
vexpectedPayouts.emplace_back(payout, GetScriptForDestination(CBitcoinAddress(payoutAddress).Get()), betAmount);
vExpectedPayouts.emplace_back(payout, GetScriptForDestination(CBitcoinAddress(payoutAddress).Get()), betAmount);
}
}
}
Expand Down Expand Up @@ -2103,7 +2103,7 @@ std::vector<CTxOut> GetBetPayouts(int height)
}
}

return vexpectedPayouts;
return vExpectedPayouts;
}

/**
Expand Down Expand Up @@ -2176,9 +2176,9 @@ std::pair<std::vector<CChainGamesResult>,std::vector<std::string>> getCGLottoEve
*
* @return payout vector.
*/
std::vector<CTxOut> GetCGLottoBetPayouts (int height)
std::vector<CBetOut> GetCGLottoBetPayouts (int height)
{
std::vector<CTxOut> vexpectedCGLottoBetPayouts;
std::vector<CBetOut> vexpectedCGLottoBetPayouts;
int nCurrentHeight = chainActive.Height();
long long totalValueOfBlock = 0;

Expand Down
41 changes: 36 additions & 5 deletions src/bet.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,48 @@ typedef enum MappingTypes {
tournamentMapping = 0x04
} MappingTypes;

// Class derived from CTxOut
// nBetValue is NOT serialized, nor is it included in the hash.
class CBetOut : public CTxOut {
public:

CAmount nBetValue;

CBetOut() : CTxOut() {
SetNull();
}

CBetOut(const CAmount& nValueIn, CScript scriptPubKeyIn) : CTxOut(nValueIn, scriptPubKeyIn), nBetValue(0) {};

CBetOut(const CAmount& nValueIn, CScript scriptPubKeyIn, const CAmount& nBetValueIn) :
CTxOut(nValueIn, scriptPubKeyIn), nBetValue(nBetValueIn) {};

void SetNull() {
CTxOut::SetNull();
nBetValue = -1;
}

void SetEmpty() {
CTxOut::SetEmpty();
nBetValue = 0;
}

bool IsEmpty() const {
return CTxOut::IsEmpty() && nBetValue == 0;
}
};

/** Ensures a TX has come from an OMNO wallet. **/
bool IsValidOracleTx(const CTxIn &txin);

/** Aggregates the amount of WGR to be minted to pay out all bets as well as dev and OMNO rewards. **/
int64_t GetBlockPayouts(std::vector<CTxOut>& vexpectedPayouts, CAmount& nMNBetReward);
int64_t GetBlockPayouts(std::vector<CBetOut>& vExpectedPayouts, CAmount& nMNBetReward);

/** Aggregates the amount of WGR to be minted to pay out all CG Lotto winners as well as OMNO rewards. **/
int64_t GetCGBlockPayouts(std::vector<CTxOut>& vexpectedCGPayouts, CAmount& nMNBetReward);
int64_t GetCGBlockPayouts(std::vector<CBetOut>& vexpectedCGPayouts, CAmount& nMNBetReward);

/** Validating the payout block using the payout vector. **/
bool IsBlockPayoutsValid(std::vector<CTxOut> vExpectedPayouts, CBlock block);
bool IsBlockPayoutsValid(std::vector<CBetOut> vExpectedPayouts, CBlock block);

class CPeerlessEvent
{
Expand Down Expand Up @@ -443,10 +474,10 @@ std::vector<CPeerlessResult> getEventResults(int height);
std::pair<std::vector<CChainGamesResult>,std::vector<std::string>> getCGLottoEventResults(int height);

/** Get the peerless winning bets from the block chain and return the payout vector. **/
std::vector<CTxOut> GetBetPayouts(int height);
std::vector<CBetOut> GetBetPayouts(int height);

/** Get the chain games winner and return the payout vector. **/
std::vector<CTxOut> GetCGLottoBetPayouts(int height);
std::vector<CBetOut> GetCGLottoBetPayouts(int height);

/** Set a peerless event spread odds **/
void SetEventSpreadOdds(CPeerlessSpreadsEvent sEventOdds);
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3020,9 +3020,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
nExpectedMint += nFees;

// Calculate the expected bet payouts.
std::vector<CTxOut> vExpectedAllPayouts;
std::vector<CTxOut> vExpectedPLPayouts;
std::vector<CTxOut> vExpectedCGLottoPayouts;
std::vector<CBetOut> vExpectedAllPayouts;
std::vector<CBetOut> vExpectedPLPayouts;
std::vector<CBetOut> vExpectedCGLottoPayouts;

if( pindex->nHeight > Params().BetStartHeight()) {
std::string strBetNetBlockTxt;
Expand Down
19 changes: 11 additions & 8 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
}
else {
// Calculate the bet payouts.
std::vector<CTxOut> vAllPayouts;
std::vector<CTxOut> vPLPayouts;
std::vector<CTxOut> vCGLottoPayouts;
std::vector<CTxOut> vAllBetTxOuts;
std::vector<CBetOut> vPLPayouts;
std::vector<CBetOut> vCGLottoPayouts;
CAmount nMNBetReward = 0;

if( nHeight > Params().BetStartHeight()) {
Expand All @@ -480,13 +480,16 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
GetCGBlockPayouts(vCGLottoPayouts, nMNBetReward);

// Merge vectors into single payout vector.
vAllPayouts = vPLPayouts;
vAllPayouts.insert(vAllPayouts.end(), vCGLottoPayouts.begin(), vCGLottoPayouts.end());
for (auto vPLPayout : vPLPayouts) {
vAllBetTxOuts.emplace_back(vPLPayout.nValue, vPLPayout.scriptPubKey);
}
for (auto vCGLottoPayout : vCGLottoPayouts) {
vAllBetTxOuts.emplace_back(vCGLottoPayout.nValue, vCGLottoPayout.scriptPubKey);
}
}

// Fill coin stake transaction.
// pwallet->FillCoinStake(txCoinStake, nMNBetReward, voutPayouts); // Kokary: add betting fee
if (pwallet->FillCoinStake(*pwallet, txCoinStake, nMNBetReward, vAllPayouts, stakeInput)) {
if (pwallet->FillCoinStake(*pwallet, txCoinStake, nMNBetReward, vAllBetTxOuts, stakeInput)) {
LogPrintf("%s: filled coin stake tx [%s]\n", __func__, txCoinStake.ToString());
}
else {
Expand All @@ -498,7 +501,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
// pwallet->SignCoinStake(txCoinStake, vwtxPrev);

// Clear all vectors after a payout.
vAllPayouts.clear();
vAllBetTxOuts.clear();
vPLPayouts.clear();
vCGLottoPayouts.clear();
}
Expand Down
8 changes: 0 additions & 8 deletions src/primitives/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
nRounds = -10;
}

CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn,const CAmount& nBetValueIn)
{
nValue = nValueIn;
nBetValue = nBetValueIn;
scriptPubKey = scriptPubKeyIn;
nRounds = -10;
}

bool COutPoint::IsMasternodeReward(const CTransaction* tx) const
{
if(!tx->IsCoinStake())
Expand Down
3 changes: 0 additions & 3 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class CTxOut
{
public:
CAmount nValue;
CAmount nBetValue;
CScript scriptPubKey;
int nRounds;

Expand All @@ -126,7 +125,6 @@ class CTxOut
}

CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn, const CAmount& nBetValueIn);

ADD_SERIALIZE_METHODS;

Expand All @@ -139,7 +137,6 @@ class CTxOut
void SetNull()
{
nValue = -1;
nBetValue = -1;
scriptPubKey.clear();
nRounds = -10; // an initial value, should be no way to get this by calculations
}
Expand Down

0 comments on commit fd62b54

Please sign in to comment.