PRCYCoin  2.0.0.7rc1
P2P Digital Currency
chain.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "chain.h"
7 
8 
13 {
14  if (pindex == NULL) {
15  vChain.clear();
16  return;
17  }
18  vChain.resize(pindex->nHeight + 1);
19  while (pindex && vChain[pindex->nHeight] != pindex) {
20  vChain[pindex->nHeight] = pindex;
21  pindex = pindex->pprev;
22  }
23 }
24 
26 {
27  int nStep = 1;
28  std::vector<uint256> vHave;
29  vHave.reserve(32);
30 
31  if (!pindex)
32  pindex = Tip();
33  while (pindex) {
34  vHave.push_back(pindex->GetBlockHash());
35  // Stop when we have added the genesis block.
36  if (pindex->nHeight == 0)
37  break;
38  // Exponentially larger steps back, plus the genesis block.
39  int nHeight = std::max(pindex->nHeight - nStep, 0);
40  if (Contains(pindex)) {
41  // Use O(1) CChain index if possible.
42  pindex = (*this)[nHeight];
43  } else {
44  // Otherwise, use O(log n) skiplist.
45  pindex = pindex->GetAncestor(nHeight);
46  }
47  if (vHave.size() > 10)
48  nStep *= 2;
49  }
50 
51  return CBlockLocator(vHave);
52 }
53 
54 const CBlockIndex* CChain::FindFork(const CBlockIndex* pindex) const
55 {
56  if (pindex == nullptr) {
57  return nullptr;
58  }
59  if (pindex->nHeight > Height())
60  pindex = pindex->GetAncestor(Height());
61  while (pindex && !Contains(pindex))
62  pindex = pindex->pprev;
63  return pindex;
64 }
65 
67 {
68  uint256 bnTarget;
69  bnTarget.SetCompact(nBits);
70  if (bnTarget <= 0)
71  return UINT256_ZERO;
72 
73  if (IsProofOfStake()) {
74  // Return trust score as usual
75  return (UINT256_ONE << 256) / (bnTarget + 1);
76  } else {
77  // Calculate work amount for block
78  uint256 bnPoWTrust = ((~UINT256_ZERO >> 20) / (bnTarget + 1));
79  return bnPoWTrust > 1 ? bnPoWTrust : 1;
80  }
81 }
UINT256_ZERO
const uint256 UINT256_ZERO
constant uint256 instances
Definition: uint256.h:129
CBlockIndex::IsProofOfStake
bool IsProofOfStake() const
Definition: chain.h:390
CBlockIndex::GetAncestor
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: main.cpp:4856
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:169
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:181
uint256::SetCompact
uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
Definition: uint256.cpp:14
CBlockIndex::nBits
unsigned int nBits
Definition: chain.h:229
CChain::FindFork
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:54
UINT256_ONE
const uint256 UINT256_ONE
Definition: uint256.h:130
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
chain.h
CChain::SetTip
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:12
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:359
CChain::Height
int Height() const
Return the maximal height in the chain.
Definition: chain.h:641
CChain::GetLocator
CBlockLocator GetLocator(const CBlockIndex *pindex=NULL) const
Return a CBlockLocator that refers to a block in this chain (by default the tip).
Definition: chain.cpp:25
CChain::vChain
std::vector< CBlockIndex * > vChain
Definition: chain.h:586
CChain::Contains
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:626
CChain::Tip
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or NULL if none.
Definition: chain.h:596
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:249
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:162
CBlockIndex::GetBlockTrust
uint256 GetBlockTrust() const
Definition: chain.cpp:66