Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
84be0091
Unverified
Commit
84be0091
authored
Jan 22, 2018
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: sorted reorg insertion order for proper head header updating
parent
02aeb3d7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
7 deletions
+54
-7
blockchain.go
core/blockchain.go
+6
-7
blockchain_test.go
core/blockchain_test.go
+48
-0
No files found.
core/blockchain.go
View file @
84be0091
...
...
@@ -465,7 +465,7 @@ func (bc *BlockChain) insert(block *types.Block) {
}
bc
.
currentBlock
=
block
// If the block is better than ou
t
head or is on a different chain, force update heads
// If the block is better than ou
r
head or is on a different chain, force update heads
if
updateHeads
{
bc
.
hc
.
SetCurrentHeader
(
block
.
Header
())
...
...
@@ -1140,18 +1140,17 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
}
else
{
log
.
Error
(
"Impossible reorg, please file an issue"
,
"oldnum"
,
oldBlock
.
Number
(),
"oldhash"
,
oldBlock
.
Hash
(),
"newnum"
,
newBlock
.
Number
(),
"newhash"
,
newBlock
.
Hash
())
}
// Insert the new chain, taking care of the proper incremental order
var
addedTxs
types
.
Transactions
// insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
for
_
,
block
:=
range
newChain
{
for
i
:=
len
(
newChain
)
-
1
;
i
>=
0
;
i
--
{
// insert the block in the canonical way, re-writing history
bc
.
insert
(
block
)
bc
.
insert
(
newChain
[
i
]
)
// write lookup entries for hash based transaction/receipt searches
if
err
:=
WriteTxLookupEntries
(
bc
.
chainDb
,
block
);
err
!=
nil
{
if
err
:=
WriteTxLookupEntries
(
bc
.
chainDb
,
newChain
[
i
]
);
err
!=
nil
{
return
err
}
addedTxs
=
append
(
addedTxs
,
block
.
Transactions
()
...
)
addedTxs
=
append
(
addedTxs
,
newChain
[
i
]
.
Transactions
()
...
)
}
// calculate the difference between deleted and added transactions
diff
:=
types
.
TxDifference
(
deletedTxs
,
addedTxs
)
// When transactions get deleted from the database that means the
...
...
core/blockchain_test.go
View file @
84be0091
...
...
@@ -1197,3 +1197,51 @@ func TestEIP161AccountRemoval(t *testing.T) {
t
.
Error
(
"account should not exist"
)
}
}
// This is a regression test (i.e. as weird as it is, don't delete it ever), which
// tests that under weird reorg conditions the blockchain and its internal header-
// chain return the same latest block/header.
//
// https://github.com/ethereum/go-ethereum/pull/15941
func
TestBlockchainHeaderchainReorgConsistency
(
t
*
testing
.
T
)
{
// Generate a canonical chain to act as the main dataset
engine
:=
ethash
.
NewFaker
()
db
,
_
:=
ethdb
.
NewMemDatabase
()
genesis
:=
new
(
Genesis
)
.
MustCommit
(
db
)
blocks
,
_
:=
GenerateChain
(
params
.
TestChainConfig
,
genesis
,
engine
,
db
,
64
,
func
(
i
int
,
b
*
BlockGen
)
{
b
.
SetCoinbase
(
common
.
Address
{
1
})
})
// Generate a bunch of fork blocks, each side forking from the canonical chain
forks
:=
make
([]
*
types
.
Block
,
len
(
blocks
))
for
i
:=
0
;
i
<
len
(
forks
);
i
++
{
parent
:=
genesis
if
i
>
0
{
parent
=
blocks
[
i
-
1
]
}
fork
,
_
:=
GenerateChain
(
params
.
TestChainConfig
,
parent
,
engine
,
db
,
1
,
func
(
i
int
,
b
*
BlockGen
)
{
b
.
SetCoinbase
(
common
.
Address
{
2
})
})
forks
[
i
]
=
fork
[
0
]
}
// Import the canonical and fork chain side by side, verifying the current block
// and current header consistency
diskdb
,
_
:=
ethdb
.
NewMemDatabase
()
new
(
Genesis
)
.
MustCommit
(
diskdb
)
chain
,
err
:=
NewBlockChain
(
diskdb
,
params
.
TestChainConfig
,
engine
,
vm
.
Config
{})
if
err
!=
nil
{
t
.
Fatalf
(
"failed to create tester chain: %v"
,
err
)
}
for
i
:=
0
;
i
<
len
(
blocks
);
i
++
{
if
_
,
err
:=
chain
.
InsertChain
(
blocks
[
i
:
i
+
1
]);
err
!=
nil
{
t
.
Fatalf
(
"block %d: failed to insert into chain: %v"
,
i
,
err
)
}
if
chain
.
CurrentBlock
()
.
Hash
()
!=
chain
.
CurrentHeader
()
.
Hash
()
{
t
.
Errorf
(
"block %d: current block/header mismatch: block #%d [%x…], header #%d [%x…]"
,
i
,
chain
.
CurrentBlock
()
.
Number
(),
chain
.
CurrentBlock
()
.
Hash
()
.
Bytes
()[
:
4
],
chain
.
CurrentHeader
()
.
Number
,
chain
.
CurrentHeader
()
.
Hash
()
.
Bytes
()[
:
4
])
}
if
_
,
err
:=
chain
.
InsertChain
(
forks
[
i
:
i
+
1
]);
err
!=
nil
{
t
.
Fatalf
(
" fork %d: failed to insert into chain: %v"
,
i
,
err
)
}
if
chain
.
CurrentBlock
()
.
Hash
()
!=
chain
.
CurrentHeader
()
.
Hash
()
{
t
.
Errorf
(
" fork %d: current block/header mismatch: block #%d [%x…], header #%d [%x…]"
,
i
,
chain
.
CurrentBlock
()
.
Number
(),
chain
.
CurrentBlock
()
.
Hash
()
.
Bytes
()[
:
4
],
chain
.
CurrentHeader
()
.
Number
,
chain
.
CurrentHeader
()
.
Hash
()
.
Bytes
()[
:
4
])
}
}
}
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