Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
defunctec
crown-core
Commits
6450b496
Commit
6450b496
authored
11 years ago
by
Gavin Andresen
Browse files
Options
Download
Plain Diff
Merge pull request #2418 from sipa/uintwork
Use a uint256 for bnChainWork
parents
e91441c1
94b77ae5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
contrib/test-patches/bitcoind-comparison.patch
+5
-5
contrib/test-patches/bitcoind-comparison.patch
src/bignum.h
+1
-1
src/bignum.h
src/main.cpp
+25
-23
src/main.cpp
src/main.h
+7
-7
src/main.h
src/uint256.h
+10
-0
src/uint256.h
with
48 additions
and
36 deletions
+48
-36
contrib/test-patches/bitcoind-comparison.patch
View file @
6450b496
diff --git a/
src/main.cpp b/src/main.cpp
index
8c115c2..1e70ff2
100644
diff --git a/
contrib/test-patches/bitcoind-comparison.patch b/contrib/test-patches/bitcoind-comparison.patch
index
04a8618..519429a
100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,8 +31,8 @@
CTxMemPool mempool;
...
...
@@ -12,7 +12,7 @@ index 8c115c2..1e70ff2 100644
+static CBigNum bnProofOfWorkLimit(~uint256(0) >> 1);
CBlockIndex* pindexGenesisBlock = NULL;
int nBestHeight = -1;
CBigNum b
nBestChainWork = 0;
uint256
nBestChainWork = 0;
@@ -1055,7 +1055,7 @@
int64 static GetBlockValue(int nHeight, int64 nFees)
int64 nSubsidy = 50 * COIN;
...
...
@@ -22,7 +22,7 @@ index 8c115c2..1e70ff2 100644
return nSubsidy + nFees;
}
@@ -27
0
6,9 +27
0
6,9 @@
bool InitBlockIndex() {
@@ -27
3
6,9 +27
3
6,9 @@
bool InitBlockIndex() {
block.hashPrevBlock = 0;
block.hashMerkleRoot = block.BuildMerkleTree();
block.nVersion = 1;
...
...
@@ -35,7 +35,7 @@ index 8c115c2..1e70ff2 100644
if (fTestNet)
{
@@ -30
07
,7 +30
07
,7 @@
bool static AlreadyHave(const CInv& inv)
@@ -30
24
,7 +30
24
,7 @@
bool static AlreadyHave(const CInv& inv)
// The message start string is designed to be unlikely to occur in normal data.
// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
// a large 4-byte int at any alignment.
...
...
This diff is collapsed.
Click to expand it.
src/bignum.h
View file @
6450b496
...
...
@@ -222,7 +222,7 @@ public:
BN_mpi2bn
(
pch
,
p
-
pch
,
this
);
}
uint256
getuint256
()
uint256
getuint256
()
const
{
unsigned
int
nSize
=
BN_bn2mpi
(
this
,
NULL
);
if
(
nSize
<
4
)
...
...
This diff is collapsed.
Click to expand it.
src/main.cpp
View file @
6450b496
...
...
@@ -35,8 +35,8 @@ uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3
static
CBigNum
bnProofOfWorkLimit
(
~
uint256
(
0
)
>>
32
);
CBlockIndex
*
pindexGenesisBlock
=
NULL
;
int
nBestHeight
=
-
1
;
CBigNum
b
nBestChainWork
=
0
;
CBigNum
b
nBestInvalidWork
=
0
;
uint256
nBestChainWork
=
0
;
uint256
nBestInvalidWork
=
0
;
uint256
hashBestChain
=
0
;
CBlockIndex
*
pindexBest
=
NULL
;
set
<
CBlockIndex
*
,
CBlockIndexWorkComparator
>
setBlockIndexValid
;
// may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
...
...
@@ -1191,20 +1191,20 @@ bool IsInitialBlockDownload()
void
static
InvalidChainFound
(
CBlockIndex
*
pindexNew
)
{
if
(
pindexNew
->
b
nChainWork
>
b
nBestInvalidWork
)
if
(
pindexNew
->
nChainWork
>
nBestInvalidWork
)
{
b
nBestInvalidWork
=
pindexNew
->
b
nChainWork
;
pblocktree
->
WriteBestInvalidWork
(
b
nBestInvalidWork
);
nBestInvalidWork
=
pindexNew
->
nChainWork
;
pblocktree
->
WriteBestInvalidWork
(
CBigNum
(
nBestInvalidWork
)
)
;
uiInterface
.
NotifyBlocksChanged
();
}
printf
(
"InvalidChainFound: invalid block=%s height=%d work=%
s
date=%s
\n
"
,
printf
(
"InvalidChainFound: invalid block=%s height=%d
log2_
work=%
.8g
date=%s
\n
"
,
pindexNew
->
GetBlockHash
().
ToString
().
c_str
(),
pindexNew
->
nHeight
,
pindexNew
->
b
nChainWork
.
ToString
().
c_str
(
),
DateTimeStrFormat
(
"%Y-%m-%d %H:%M:%S"
,
log
(
pindexNew
->
nChainWork
.
getdouble
())
/
log
(
2.0
),
DateTimeStrFormat
(
"%Y-%m-%d %H:%M:%S"
,
pindexNew
->
GetBlockTime
()).
c_str
());
printf
(
"InvalidChainFound: current best=%s height=%d work=%
s
date=%s
\n
"
,
hashBestChain
.
ToString
().
c_str
(),
nBestHeight
,
b
nBestChainWork
.
ToString
().
c_str
(
),
printf
(
"InvalidChainFound: current best=%s height=%d
log2_
work=%
.8g
date=%s
\n
"
,
hashBestChain
.
ToString
().
c_str
(),
nBestHeight
,
log
(
nBestChainWork
.
getdouble
())
/
log
(
2.0
),
DateTimeStrFormat
(
"%Y-%m-%d %H:%M:%S"
,
pindexBest
->
GetBlockTime
()).
c_str
());
if
(
pindexBest
&&
b
nBestInvalidWork
>
b
nBestChainWork
+
pindexBest
->
GetBlockWork
()
*
6
)
if
(
pindexBest
&&
nBestInvalidWork
>
nBestChainWork
+
(
pindexBest
->
GetBlockWork
()
*
6
)
.
getuint256
())
printf
(
"InvalidChainFound: Warning: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade.
\n
"
);
}
...
...
@@ -1230,7 +1230,7 @@ bool ConnectBestBlock(CValidationState &state) {
pindexNewBest
=
*
it
;
}
if
(
pindexNewBest
==
pindexBest
||
(
pindexBest
&&
pindexNewBest
->
b
nChainWork
==
pindexBest
->
b
nChainWork
))
if
(
pindexNewBest
==
pindexBest
||
(
pindexBest
&&
pindexNewBest
->
nChainWork
==
pindexBest
->
nChainWork
))
return
true
;
// nothing to do
// check ancestry
...
...
@@ -1250,7 +1250,7 @@ bool ConnectBestBlock(CValidationState &state) {
break
;
}
if
(
pindexBest
==
NULL
||
pindexTest
->
b
nChainWork
>
pindexBest
->
b
nChainWork
)
if
(
pindexBest
==
NULL
||
pindexTest
->
nChainWork
>
pindexBest
->
nChainWork
)
vAttach
.
push_back
(
pindexTest
);
if
(
pindexTest
->
pprev
==
NULL
||
pindexTest
->
pnext
!=
NULL
)
{
...
...
@@ -1858,11 +1858,11 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
pindexBest
=
pindexNew
;
pblockindexFBBHLast
=
NULL
;
nBestHeight
=
pindexBest
->
nHeight
;
b
nBestChainWork
=
pindexNew
->
b
nChainWork
;
nBestChainWork
=
pindexNew
->
nChainWork
;
nTimeBestReceived
=
GetTime
();
nTransactionsUpdated
++
;
printf
(
"SetBestChain: new best=%s height=%d work=%
s
tx=%lu date=%s
progress=%f
\n
"
,
hashBestChain
.
ToString
().
c_str
(),
nBestHeight
,
b
nBestChainWork
.
ToString
().
c_str
(
),
(
unsigned
long
)
pindexNew
->
nChainTx
,
printf
(
"SetBestChain: new best=%s height=%d
log2_
work=%
.8g
tx=%lu date=%s progress=%f
\n
"
,
hashBestChain
.
ToString
().
c_str
(),
nBestHeight
,
log
(
nBestChainWork
.
getdouble
())
/
log
(
2.0
),
(
unsigned
long
)
pindexNew
->
nChainTx
,
DateTimeStrFormat
(
"%Y-%m-%d %H:%M:%S"
,
pindexBest
->
GetBlockTime
()).
c_str
(),
Checkpoints
::
GuessVerificationProgress
(
pindexBest
));
...
...
@@ -1915,7 +1915,7 @@ bool CBlock::AddToBlockIndex(CValidationState &state, const CDiskBlockPos &pos)
pindexNew
->
nHeight
=
pindexNew
->
pprev
->
nHeight
+
1
;
}
pindexNew
->
nTx
=
vtx
.
size
();
pindexNew
->
b
nChainWork
=
(
pindexNew
->
pprev
?
pindexNew
->
pprev
->
b
nChainWork
:
0
)
+
pindexNew
->
GetBlockWork
();
pindexNew
->
nChainWork
=
(
pindexNew
->
pprev
?
pindexNew
->
pprev
->
nChainWork
:
0
)
+
pindexNew
->
GetBlockWork
()
.
getuint256
()
;
pindexNew
->
nChainTx
=
(
pindexNew
->
pprev
?
pindexNew
->
pprev
->
nChainTx
:
0
)
+
pindexNew
->
nTx
;
pindexNew
->
nFile
=
pos
.
nFile
;
pindexNew
->
nDataPos
=
pos
.
nPos
;
...
...
@@ -2537,7 +2537,7 @@ bool static LoadBlockIndexDB()
boost
::
this_thread
::
interruption_point
();
// Calculate
b
nChainWork
// Calculate nChainWork
vector
<
pair
<
int
,
CBlockIndex
*>
>
vSortedByHeight
;
vSortedByHeight
.
reserve
(
mapBlockIndex
.
size
());
BOOST_FOREACH
(
const
PAIRTYPE
(
uint256
,
CBlockIndex
*
)
&
item
,
mapBlockIndex
)
...
...
@@ -2549,7 +2549,7 @@ bool static LoadBlockIndexDB()
BOOST_FOREACH
(
const
PAIRTYPE
(
int
,
CBlockIndex
*
)
&
item
,
vSortedByHeight
)
{
CBlockIndex
*
pindex
=
item
.
second
;
pindex
->
b
nChainWork
=
(
pindex
->
pprev
?
pindex
->
pprev
->
b
nChainWork
:
0
)
+
pindex
->
GetBlockWork
();
pindex
->
nChainWork
=
(
pindex
->
pprev
?
pindex
->
pprev
->
nChainWork
:
0
)
+
pindex
->
GetBlockWork
()
.
getuint256
()
;
pindex
->
nChainTx
=
(
pindex
->
pprev
?
pindex
->
pprev
->
nChainTx
:
0
)
+
pindex
->
nTx
;
if
((
pindex
->
nStatus
&
BLOCK_VALID_MASK
)
>=
BLOCK_VALID_TRANSACTIONS
&&
!
(
pindex
->
nStatus
&
BLOCK_FAILED_MASK
))
setBlockIndexValid
.
insert
(
pindex
);
...
...
@@ -2561,8 +2561,10 @@ bool static LoadBlockIndexDB()
if
(
pblocktree
->
ReadBlockFileInfo
(
nLastBlockFile
,
infoLastBlockFile
))
printf
(
"LoadBlockIndexDB(): last block file info: %s
\n
"
,
infoLastBlockFile
.
ToString
().
c_str
());
// Load bnBestInvalidWork, OK if it doesn't exist
// Load nBestInvalidWork, OK if it doesn't exist
CBigNum
bnBestInvalidWork
;
pblocktree
->
ReadBestInvalidWork
(
bnBestInvalidWork
);
nBestInvalidWork
=
bnBestInvalidWork
.
getuint256
();
// Check whether we need to continue reindexing
bool
fReindexing
=
false
;
...
...
@@ -2579,7 +2581,7 @@ bool static LoadBlockIndexDB()
return
true
;
hashBestChain
=
pindexBest
->
GetBlockHash
();
nBestHeight
=
pindexBest
->
nHeight
;
b
nBestChainWork
=
pindexBest
->
b
nChainWork
;
nBestChainWork
=
pindexBest
->
nChainWork
;
// set 'next' pointers in best chain
CBlockIndex
*
pindex
=
pindexBest
;
...
...
@@ -2675,8 +2677,8 @@ void UnloadBlockIndex()
setBlockIndexValid
.
clear
();
pindexGenesisBlock
=
NULL
;
nBestHeight
=
0
;
b
nBestChainWork
=
0
;
b
nBestInvalidWork
=
0
;
nBestChainWork
=
0
;
nBestInvalidWork
=
0
;
hashBestChain
=
0
;
pindexBest
=
NULL
;
}
...
...
@@ -2953,7 +2955,7 @@ string GetWarnings(string strFor)
}
// Longer invalid proof-of-work chain
if
(
pindexBest
&&
b
nBestInvalidWork
>
b
nBestChainWork
+
pindexBest
->
GetBlockWork
()
*
6
)
if
(
pindexBest
&&
nBestInvalidWork
>
nBestChainWork
+
(
pindexBest
->
GetBlockWork
()
*
6
)
.
getuint256
())
{
nPriority
=
2000
;
strStatusBar
=
strRPC
=
_
(
"Warning: Displayed transactions may not be correct! You may need to upgrade, or other nodes may need to upgrade."
);
...
...
This diff is collapsed.
Click to expand it.
src/main.h
View file @
6450b496
...
...
@@ -77,8 +77,8 @@ extern std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
extern
uint256
hashGenesisBlock
;
extern
CBlockIndex
*
pindexGenesisBlock
;
extern
int
nBestHeight
;
extern
CBigNum
b
nBestChainWork
;
extern
CBigNum
b
nBestInvalidWork
;
extern
uint256
nBestChainWork
;
extern
uint256
nBestInvalidWork
;
extern
uint256
hashBestChain
;
extern
CBlockIndex
*
pindexBest
;
extern
unsigned
int
nTransactionsUpdated
;
...
...
@@ -1619,7 +1619,7 @@ public:
unsigned
int
nUndoPos
;
// (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
CBigNum
b
nChainWork
;
uint256
nChainWork
;
// Number of transactions in this block.
// Note: in a potential headers-first mode, this number cannot be relied upon
...
...
@@ -1648,7 +1648,7 @@ public:
nFile
=
0
;
nDataPos
=
0
;
nUndoPos
=
0
;
b
nChainWork
=
0
;
nChainWork
=
0
;
nTx
=
0
;
nChainTx
=
0
;
nStatus
=
0
;
...
...
@@ -1669,7 +1669,7 @@ public:
nFile
=
0
;
nDataPos
=
0
;
nUndoPos
=
0
;
b
nChainWork
=
0
;
nChainWork
=
0
;
nTx
=
0
;
nChainTx
=
0
;
nStatus
=
0
;
...
...
@@ -1793,8 +1793,8 @@ public:
struct
CBlockIndexWorkComparator
{
bool
operator
()(
CBlockIndex
*
pa
,
CBlockIndex
*
pb
)
{
if
(
pa
->
b
nChainWork
>
pb
->
b
nChainWork
)
return
false
;
if
(
pa
->
b
nChainWork
<
pb
->
b
nChainWork
)
return
true
;
if
(
pa
->
nChainWork
>
pb
->
nChainWork
)
return
false
;
if
(
pa
->
nChainWork
<
pb
->
nChainWork
)
return
true
;
if
(
pa
->
GetBlockHash
()
<
pb
->
GetBlockHash
())
return
false
;
if
(
pa
->
GetBlockHash
()
>
pb
->
GetBlockHash
())
return
true
;
...
...
This diff is collapsed.
Click to expand it.
src/uint256.h
View file @
6450b496
...
...
@@ -55,6 +55,16 @@ public:
return
ret
;
}
double
getdouble
()
const
{
double
ret
=
0.0
;
double
fact
=
1.0
;
for
(
int
i
=
0
;
i
<
WIDTH
;
i
++
)
{
ret
+=
fact
*
pn
[
i
];
fact
*=
4294967296.0
;
}
return
ret
;
}
base_uint
&
operator
=
(
uint64
b
)
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help