本文整理汇总了C++中CBigNum类的典型用法代码示例。如果您正苦于以下问题:C++ CBigNum类的具体用法?C++ CBigNum怎么用?C++ CBigNum使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CBigNum类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ssStartKey
//.........这里部分代码省略.........
// Calculate nChainTrust
vector<pair<int, CBlockIndex*> > vSortedByHeight;
vSortedByHeight.reserve(mapBlockIndex.size());
BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
{
CBlockIndex* pindex = item.second;
vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
}
sort(vSortedByHeight.begin(), vSortedByHeight.end());
BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
{
CBlockIndex* pindex = item.second;
pindex->nChainTrust = (pindex->pprev ? pindex->pprev->nChainTrust : 0) + pindex->GetBlockTrust();
// ARbit: calculate stake modifier checksum
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum))
return error("CTxDB::LoadBlockIndex() : Failed stake modifier checkpoint height=%d, modifier=0x%016"PRIx64, pindex->nHeight, pindex->nStakeModifier);
}
// Load hashBestChain pointer to end of best chain
if (!ReadHashBestChain(hashBestChain))
{
if (pindexGenesisBlock == NULL)
return true;
return error("CTxDB::LoadBlockIndex() : hashBestChain not loaded");
}
if (!mapBlockIndex.count(hashBestChain))
return error("CTxDB::LoadBlockIndex() : hashBestChain not found in the block index");
pindexBest = mapBlockIndex[hashBestChain];
nBestHeight = pindexBest->nHeight;
nBestChainTrust = pindexBest->nChainTrust;
printf("LoadBlockIndex(): hashBestChain=%s height=%d trust=%s date=%s\n",
hashBestChain.ToString().substr(0,20).c_str(), nBestHeight, CBigNum(nBestChainTrust).ToString().c_str(),
DateTimeStrFormat("%x %H:%M:%S", pindexBest->GetBlockTime()).c_str());
// ARbit: load hashSyncCheckpoint
if (!ReadSyncCheckpoint(Checkpoints::hashSyncCheckpoint))
return error("CTxDB::LoadBlockIndex() : hashSyncCheckpoint not loaded");
printf("LoadBlockIndex(): synchronized checkpoint %s\n", Checkpoints::hashSyncCheckpoint.ToString().c_str());
// Load bnBestInvalidTrust, OK if it doesn't exist
CBigNum bnBestInvalidTrust;
ReadBestInvalidTrust(bnBestInvalidTrust);
nBestInvalidTrust = bnBestInvalidTrust.getuint256();
// Verify blocks in the best chain
int nCheckLevel = GetArg("-checklevel", 1);
int nCheckDepth = GetArg( "-checkblocks", 2500);
if (nCheckDepth == 0)
nCheckDepth = 1000000000; // suffices until the year 19000
if (nCheckDepth > nBestHeight)
nCheckDepth = nBestHeight;
printf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
CBlockIndex* pindexFork = NULL;
map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
{
if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
break;
CBlock block;
if (!block.ReadFromDisk(pindex))
return error("LoadBlockIndex() : block.ReadFromDisk failed");
// check level 1: verify block validity
// check level 7: verify block signature too
if (nCheckLevel>0 && !block.CheckBlock(true, true, (nCheckLevel>6)))
示例2: EvalScript
bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
{
CAutoBN_CTX pctx;
CScript::const_iterator pc = script.begin();
CScript::const_iterator pend = script.end();
CScript::const_iterator pbegincodehash = script.begin();
opcodetype opcode;
valtype vchPushValue;
vector<bool> vfExec;
vector<valtype> altstack;
if (script.size() > 10000)
return false;
int nOpCount = 0;
try
{
while (pc < pend)
{
bool fExec = !count(vfExec.begin(), vfExec.end(), false);
//
// Read instruction
//
if (!script.GetOp(pc, opcode, vchPushValue))
return false;
if (vchPushValue.size() > 520)
return false;
if (opcode > OP_16 && ++nOpCount > 201)
return false;
if (opcode == OP_CAT ||
opcode == OP_SUBSTR ||
opcode == OP_LEFT ||
opcode == OP_RIGHT ||
opcode == OP_INVERT ||
opcode == OP_AND ||
opcode == OP_OR ||
opcode == OP_XOR ||
opcode == OP_2MUL ||
opcode == OP_2DIV ||
opcode == OP_MUL ||
opcode == OP_DIV ||
opcode == OP_MOD ||
opcode == OP_LSHIFT ||
opcode == OP_RSHIFT)
return false;
if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
stack.push_back(vchPushValue);
else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
switch (opcode)
{
//
// Push value
//
case OP_1NEGATE:
case OP_1:
case OP_2:
case OP_3:
case OP_4:
case OP_5:
case OP_6:
case OP_7:
case OP_8:
case OP_9:
case OP_10:
case OP_11:
case OP_12:
case OP_13:
case OP_14:
case OP_15:
case OP_16:
{
// ( -- value)
CBigNum bn((int)opcode - (int)(OP_1 - 1));
stack.push_back(bn.getvch());
}
break;
//
// Control
//
case OP_NOP:
case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
break;
case OP_IF:
case OP_NOTIF:
{
// <expression> if [statements] [else [statements]] endif
bool fValue = false;
if (fExec)
{
if (stack.size() < 1)
return false;
valtype& vch = stacktop(-1);
fValue = CastToBool(vch);
//.........这里部分代码省略.........
示例3: verify
static bool verify(const CBigNum& bignum, const CScriptNum& scriptnum)
{
return bignum.getvch() == scriptnum.getvch() && bignum.getint() == scriptnum.getint();
}
示例4: mysetint64
// Let's force this code not to be inlined, in order to actually
// test a generic version of the function. This increases the chance
// that -ftrapv will detect overflows.
NOINLINE void mysetint64(CBigNum& num, int64_t n)
{
num.setint64(n);
}
示例5: CreateCoinStake
bool CreateCoinStake( CBlock &blocknew, CKey &key,
vector<const CWalletTx*> &StakeInputs, uint64_t &CoinAge,
CWallet &wallet, CBlockIndex* pindexPrev )
{
int64_t CoinWeight;
CBigNum StakeKernelHash;
CTxDB txdb("r");
int64_t StakeWeightSum = 0;
double StakeValueSum = 0;
int64_t StakeWeightMin=MAX_MONEY;
int64_t StakeWeightMax=0;
uint64_t StakeCoinAgeSum=0;
double StakeDiffSum = 0;
double StakeDiffMax = 0;
CTransaction &txnew = blocknew.vtx[1]; // second tx is coinstake
//initialize the transaction
txnew.nTime = blocknew.nTime & (~STAKE_TIMESTAMP_MASK);
txnew.vin.clear();
txnew.vout.clear();
// Choose coins to use
set <pair <const CWalletTx*,unsigned int> > CoinsToStake;
int64_t BalanceToStake = wallet.GetBalance();
int64_t nValueIn = 0;
//Request all the coins here, check reserve later
if ( BalanceToStake<=0
|| !wallet.SelectCoinsForStaking(BalanceToStake*2, txnew.nTime, CoinsToStake, nValueIn) )
{
LOCK(MinerStatus.lock);
MinerStatus.ReasonNotStaking+=_("No coins; ");
if (fDebug) LogPrintf("CreateCoinStake: %s",MinerStatus.ReasonNotStaking);
return false;
}
BalanceToStake -= nReserveBalance;
if(fDebug2) LogPrintf("\nCreateCoinStake: Staking nTime/16= %d Bits= %u",
txnew.nTime/16,blocknew.nBits);
for(const auto& pcoin : CoinsToStake)
{
const CTransaction &CoinTx =*pcoin.first; //transaction that produced this coin
unsigned int CoinTxN =pcoin.second; //index of this coin inside it
CTxIndex txindex;
{
LOCK2(cs_main, wallet.cs_wallet);
if (!txdb.ReadTxIndex(pcoin.first->GetHash(), txindex))
continue; //error?
}
CBlock CoinBlock; //Block which contains CoinTx
{
LOCK2(cs_main, wallet.cs_wallet);
if (!CoinBlock.ReadFromDisk(txindex.pos.nFile, txindex.pos.nBlockPos, false))
continue;
}
// only count coins meeting min age requirement
if (CoinBlock.GetBlockTime() + nStakeMinAge > txnew.nTime)
continue;
if (CoinTx.vout[CoinTxN].nValue > BalanceToStake)
continue;
{
int64_t nStakeValue= CoinTx.vout[CoinTxN].nValue;
StakeValueSum += nStakeValue /(double)COIN;
//crazy formula...
// todo: clean this
// todo reuse calculated value for interst
CBigNum bn = CBigNum(nStakeValue) * (blocknew.nTime-CoinTx.nTime) / CENT;
bn = bn * CENT / COIN / (24 * 60 * 60);
StakeCoinAgeSum += bn.getuint64();
}
if(blocknew.nVersion==7)
{
NetworkTimer();
CoinWeight = CalculateStakeWeightV3(CoinTx,CoinTxN,GlobalCPUMiningCPID);
StakeKernelHash= CalculateStakeHashV3(CoinBlock,CoinTx,CoinTxN,txnew.nTime,GlobalCPUMiningCPID,mdPORNonce);
}
else
{
uint64_t StakeModifier = 0;
if(!FindStakeModifierRev(StakeModifier,pindexPrev))
continue;
CoinWeight = CalculateStakeWeightV8(CoinTx,CoinTxN,GlobalCPUMiningCPID);
StakeKernelHash= CalculateStakeHashV8(CoinBlock,CoinTx,CoinTxN,txnew.nTime,StakeModifier,GlobalCPUMiningCPID);
}
CBigNum StakeTarget;
StakeTarget.SetCompact(blocknew.nBits);
StakeTarget*=CoinWeight;
StakeWeightSum += CoinWeight;
StakeWeightMin=std::min(StakeWeightMin,CoinWeight);
StakeWeightMax=std::max(StakeWeightMax,CoinWeight);
double StakeKernelDiff = GetBlockDifficulty(StakeKernelHash.GetCompact())*CoinWeight;
//.........这里部分代码省略.........
示例6: RetargetGPU
unsigned int RetargetGPU(const CBlockIndex* pindex, bool output)
{
/** Get the Last Block Index [1st block back in Channel]. **/
const CBlockIndex* pindexFirst = GetLastChannelIndex(pindex, 2);
if (pindexFirst->pprev == NULL)
return bnProofOfWorkStart[2].GetCompact();
/** Get Last Block Index [2nd block back in Channel]. **/
const CBlockIndex* pindexLast = GetLastChannelIndex(pindexFirst->pprev, 2);
if (pindexLast->pprev == NULL)
return bnProofOfWorkStart[2].GetCompact();
/** Get the Block Times with Minimum of 1 to Prevent Time Warps. **/
int64 nBlockTime = ((pindex->nVersion >= 4) ? GetWeightedTimes(pindexFirst, 5) : max(pindexFirst->GetBlockTime() - pindexLast->GetBlockTime(), (int64) 1));
int64 nBlockTarget = nTargetTimespan;
/** Get the Chain Modular from Reserves. **/
double nChainMod = GetFractionalSubsidy(GetChainAge(pindexFirst->GetBlockTime()), 0, ((pindex->nVersion >= 3) ? 40.0 : 20.0)) / (pindexFirst->nReleasedReserve[0] + 1);
nChainMod = min(nChainMod, 1.0);
nChainMod = max(nChainMod, (pindex->nVersion == 1) ? 0.75 : 0.5);
/** Enforce Block Version 2 Rule. Chain mod changes block time requirements, not actual mod after block times. **/
if(pindex->nVersion >= 2)
nBlockTarget *= nChainMod;
/** The Upper and Lower Bound Adjusters. **/
int64 nUpperBound = nBlockTarget;
int64 nLowerBound = nBlockTarget;
/** Handle for Version 3 Blocks. Mod determined by time multiplied by max / min. **/
if(pindex->nVersion >= 3)
{
/** If the time is above target, reduce difficulty by modular
of one interval past timespan multiplied by maximum decrease. **/
if(nBlockTime >= nBlockTarget)
{
/** Take the Minimum overlap of Target Timespan to make that maximum interval. **/
uint64 nOverlap = (uint64)min((nBlockTime - nBlockTarget), (nBlockTarget * 2));
/** Get the Mod from the Proportion of Overlap in one Interval. **/
double nProportions = (double)nOverlap / (nBlockTarget * 2);
/** Get Mod from Maximum Decrease Equation with Decimal portions multiplied by Propotions. **/
double nMod = 1.0 - (((pindex->nVersion >= 4) ? 0.15 : 0.75) * nProportions);
nLowerBound = nBlockTarget * nMod;
}
/** If the time is below target, increase difficulty by modular
of interval of 1 - Block Target with time of 1 being maximum increase **/
else
{
/** Get the overlap in reference from Target Timespan. **/
uint64 nOverlap = nBlockTarget - nBlockTime;
/** Get the mod from overlap proportion. Time of 1 will be closest to mod of 1. **/
double nProportions = (double) nOverlap / nBlockTarget;
/** Get the Mod from the Maximum Increase Equation with Decimal portion multiplied by Proportions. **/
double nMod = 1.0 + (nProportions * 0.075);
nLowerBound = nBlockTarget * nMod;
}
}
/** Handle for Version 2 Difficulty Adjustments. **/
else
{
double nBlockMod = (double) nBlockTarget / nBlockTime;
nBlockMod = min(nBlockMod, 1.125);
nBlockMod = max(nBlockMod, 0.75);
/** Calculate the Lower Bounds. **/
nLowerBound = nBlockTarget * nBlockMod;
/** Version 1 Blocks Change Lower Bound from Chain Modular. **/
if(pindex->nVersion == 1)
nLowerBound *= nChainMod;
/** Set Maximum [difficulty] up to 8%, and Minimum [difficulty] down to 50% **/
nLowerBound = min(nLowerBound, (int64)(nUpperBound + (nUpperBound / 8)));
nLowerBound = max(nLowerBound, (3 * nUpperBound ) / 4);
}
/** Get the Difficulty Stored in Bignum Compact. **/
CBigNum bnNew;
bnNew.SetCompact(pindexFirst->nBits);
/** Change Number from Upper and Lower Bounds. **/
bnNew *= nUpperBound;
bnNew /= nLowerBound;
//.........这里部分代码省略.........
示例7: RetargetPOS
/** Proof of Stake Retargeting: Modulate Difficulty based on production rate. **/
unsigned int RetargetPOS(const CBlockIndex* pindex, bool output)
{
/** Get Last Block Index [1st block back in Channel]. **/
const CBlockIndex* pindexFirst = GetLastChannelIndex(pindex, 0);
if (pindexFirst->pprev == NULL)
return bnProofOfWorkStart[0].GetCompact();
/** Get Last Block Index [2nd block back in Channel]. **/
const CBlockIndex* pindexLast = GetLastChannelIndex(pindexFirst->pprev, 0);
if (pindexLast->pprev == NULL)
return bnProofOfWorkStart[0].GetCompact();
/** Get the Block Time and Target Spacing. **/
int64 nBlockTime = GetWeightedTimes(pindexFirst, 5);
int64 nBlockTarget = STAKE_TARGET_SPACING;
/** The Upper and Lower Bound Adjusters. **/
int64 nUpperBound = nBlockTarget;
int64 nLowerBound = nBlockTarget;
/** If the time is above target, reduce difficulty by modular
of one interval past timespan multiplied by maximum decrease. **/
if(nBlockTime >= nBlockTarget)
{
/** Take the Minimum overlap of Target Timespan to make that maximum interval. **/
uint64 nOverlap = (uint64)min((nBlockTime - nBlockTarget), (nBlockTarget * 2));
/** Get the Mod from the Proportion of Overlap in one Interval. **/
double nProportions = (double)nOverlap / (nBlockTarget * 2);
/** Get Mod from Maximum Decrease Equation with Decimal portions multiplied by Propotions. **/
double nMod = 1.0 - (0.15 * nProportions);
nLowerBound = nBlockTarget * nMod;
}
/** If the time is below target, increase difficulty by modular
of interval of 1 - Block Target with time of 1 being maximum increase **/
else
{
/** Get the overlap in reference from Target Timespan. **/
uint64 nOverlap = nBlockTarget - nBlockTime;
/** Get the mod from overlap proportion. Time of 1 will be closest to mod of 1. **/
double nProportions = (double) nOverlap / nBlockTarget;
/** Get the Mod from the Maximum Increase Equation with Decimal portion multiplied by Proportions. **/
double nMod = 1.0 + (nProportions * 0.075);
nLowerBound = nBlockTarget * nMod;
}
/** Get the Difficulty Stored in Bignum Compact. **/
CBigNum bnNew;
bnNew.SetCompact(pindexFirst->nBits);
/** Change Number from Upper and Lower Bounds. **/
bnNew *= nUpperBound;
bnNew /= nLowerBound;
/** Don't allow Difficulty to decrease below minimum. **/
if (bnNew > bnProofOfWorkLimit[0])
bnNew = bnProofOfWorkLimit[0];
if(output)
{
int64 nDays, nHours, nMinutes;
GetChainTimes(GetChainAge(pindexFirst->GetBlockTime()), nDays, nHours, nMinutes);
printf("CHECK[POS] weighted time=%"PRId64" actual time =%"PRId64"[%f %%]\n\tchain time: [%"PRId64" / %"PRId64"]\n\tdifficulty: [%f to %f]\n\tPOS height: %"PRId64" [AGE %"PRId64" days, %"PRId64" hours, %"PRId64" minutes]\n\n",
nBlockTime, max(pindexFirst->GetBlockTime() - pindexLast->GetBlockTime(), (int64) 1), ((100.0 * nLowerBound) / nUpperBound), nBlockTarget, nBlockTime, GetDifficulty(pindexFirst->nBits, 0), GetDifficulty(bnNew.GetCompact(), 0), pindexFirst->nChannelHeight, nDays, nHours, nMinutes);
}
return bnNew.GetCompact();
}