Skip to content

Commit 43631aa

Browse files
committed
core: minor code polishes + rebase fixes
1 parent 690bd8a commit 43631aa

2 files changed

Lines changed: 57 additions & 43 deletions

File tree

‎core/blockchain.go‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,17 +1465,21 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i
14651465
return 0, nil, nil, nil
14661466
}
14671467

1468-
// reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
1469-
// to be part of the new canonical chain and accumulates potential missing transactions and post an
1470-
// event about them
1468+
// reorg takes two blocks, an old chain and a new chain and will reconstruct the
1469+
// blocks and inserts them to be part of the new canonical chain and accumulates
1470+
// potential missing transactions and post an event about them.
14711471
func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14721472
var (
14731473
newChain types.Blocks
14741474
oldChain types.Blocks
14751475
commonBlock *types.Block
1476-
deletedTxs types.Transactions
1476+
1477+
deletedTxs types.Transactions
1478+
addedTxs types.Transactions
1479+
14771480
deletedLogs []*types.Log
14781481
rebirthLogs []*types.Log
1482+
14791483
// collectLogs collects the logs that were generated during the
14801484
// processing of the block that corresponds with the given hash.
14811485
// These logs are later announced as deleted or reborn
@@ -1498,46 +1502,49 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14981502
}
14991503
}
15001504
)
1501-
1502-
// first reduce whoever is higher bound
1505+
// Reduce the longer chain to the same number as the shorter one
15031506
if oldBlock.NumberU64() > newBlock.NumberU64() {
1504-
// reduce old chain
1507+
// Old chain is longer, gather all transactions and logs as deleted ones
15051508
for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) {
15061509
oldChain = append(oldChain, oldBlock)
15071510
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
1508-
15091511
collectLogs(oldBlock.Hash(), true)
15101512
}
15111513
} else {
1512-
// reduce new chain and append new chain blocks for inserting later on
1514+
// New chain is longer, stash all blocks away for subsequent insertion
15131515
for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
15141516
newChain = append(newChain, newBlock)
15151517
}
15161518
}
15171519
if oldBlock == nil {
1518-
return fmt.Errorf("Invalid old chain")
1520+
return fmt.Errorf("invalid old chain")
15191521
}
15201522
if newBlock == nil {
1521-
return fmt.Errorf("Invalid new chain")
1523+
return fmt.Errorf("invalid new chain")
15221524
}
1523-
1525+
// Both sides of the reorg are at the same number, reduce both until the common
1526+
// ancestor is found
15241527
for {
1528+
// If the common ancestor was found, bail out
15251529
if oldBlock.Hash() == newBlock.Hash() {
15261530
commonBlock = oldBlock
15271531
break
15281532
}
1529-
1533+
// Remove an old block as well as stash away a new block
15301534
oldChain = append(oldChain, oldBlock)
1531-
newChain = append(newChain, newBlock)
15321535
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
15331536
collectLogs(oldBlock.Hash(), true)
15341537

1535-
oldBlock, newBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1), bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
1538+
newChain = append(newChain, newBlock)
1539+
1540+
// Step back with both chains
1541+
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
15361542
if oldBlock == nil {
1537-
return fmt.Errorf("Invalid old chain")
1543+
return fmt.Errorf("invalid old chain")
15381544
}
1545+
newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
15391546
if newBlock == nil {
1540-
return fmt.Errorf("Invalid new chain")
1547+
return fmt.Errorf("invalid new chain")
15411548
}
15421549
}
15431550
// Ensure the user sees large reorgs
@@ -1552,42 +1559,46 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15521559
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash())
15531560
}
15541561
// Insert the new chain, taking care of the proper incremental order
1555-
var addedTxs types.Transactions
15561562
for i := len(newChain) - 1; i >= 0; i-- {
1557-
// insert the block in the canonical way, re-writing history
1563+
// Insert the block in the canonical way, re-writing history
15581564
bc.insert(newChain[i])
1559-
// collect reborn logs due to chain reorg(except head block)
1565+
1566+
// Collect reborn logs due to chain reorg (except head block (reverse order))
15601567
if i != 0 {
15611568
collectLogs(newChain[i].Hash(), false)
15621569
}
1563-
// write lookup entries for hash based transaction/receipt searches
1570+
// Write lookup entries for hash based transaction/receipt searches
15641571
rawdb.WriteTxLookupEntries(bc.db, newChain[i])
15651572
addedTxs = append(addedTxs, newChain[i].Transactions()...)
15661573
}
1567-
// calculate the difference between deleted and added transactions
1568-
diff := types.TxDifference(deletedTxs, addedTxs)
1569-
// When transactions get deleted from the database that means the
1570-
// receipts that were created in the fork must also be deleted
1574+
// When transactions get deleted from the database, the receipts that were
1575+
// created in the fork must also be deleted
15711576
batch := bc.db.NewBatch()
1572-
for _, tx := range diff {
1577+
for _, tx := range types.TxDifference(deletedTxs, addedTxs) {
15731578
rawdb.DeleteTxLookupEntry(batch, tx.Hash())
15741579
}
15751580
batch.Write()
15761581

1577-
if len(deletedLogs) > 0 {
1578-
go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
1579-
}
1580-
if len(rebirthLogs) > 0 {
1581-
go bc.logsFeed.Send(rebirthLogs)
1582-
}
1583-
if len(oldChain) > 0 {
1584-
go func() {
1582+
// If any logs need to be fired, do it now. In theory we could avoid creating
1583+
// this goroutine if there are no events to fire, but realistcally that only
1584+
// ever happens if we're reorging empty blocks, which will only happen on idle
1585+
// networks where performance is not an issue either way.
1586+
//
1587+
// TODO(karalabe): Can we get rid of the goroutine somehow to guarantee correct
1588+
// event ordering?
1589+
go func() {
1590+
if len(deletedLogs) > 0 {
1591+
bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
1592+
}
1593+
if len(rebirthLogs) > 0 {
1594+
bc.logsFeed.Send(rebirthLogs)
1595+
}
1596+
if len(oldChain) > 0 {
15851597
for _, block := range oldChain {
15861598
bc.chainSideFeed.Send(ChainSideEvent{Block: block})
15871599
}
1588-
}()
1589-
}
1590-
1600+
}
1601+
}()
15911602
return nil
15921603
}
15931604

‎core/blockchain_test.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/core/vm"
3333
"github.com/ethereum/go-ethereum/crypto"
3434
"github.com/ethereum/go-ethereum/ethdb"
35+
"github.com/ethereum/go-ethereum/ethdb/memorydb"
3536
"github.com/ethereum/go-ethereum/params"
3637
)
3738

@@ -916,7 +917,8 @@ func TestLogRebirth(t *testing.T) {
916917
var (
917918
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
918919
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
919-
db = ethdb.NewMemDatabase()
920+
db = memorydb.New()
921+
920922
// this code generates a log
921923
code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
922924
gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}}
@@ -1018,10 +1020,6 @@ func TestLogRebirth(t *testing.T) {
10181020
if _, err := blockchain.InsertChain(newBlocks); err != nil {
10191021
t.Fatalf("failed to insert forked chain: %v", err)
10201022
}
1021-
// Rebirth logs should omit a newLogEvent
1022-
if !<-newLogCh {
1023-
t.Fatalf("failed to receive new log event")
1024-
}
10251023
// Ensure removedLog events received
10261024
select {
10271025
case ev := <-rmLogsCh:
@@ -1031,13 +1029,18 @@ func TestLogRebirth(t *testing.T) {
10311029
case <-time.NewTimer(1 * time.Second).C:
10321030
t.Fatal("Timeout. There is no RemovedLogsEvent has been sent.")
10331031
}
1032+
// Rebirth logs should omit a newLogEvent
1033+
if !<-newLogCh {
1034+
t.Fatalf("failed to receive new log event")
1035+
}
10341036
}
10351037

10361038
func TestSideLogRebirth(t *testing.T) {
10371039
var (
10381040
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
10391041
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
1040-
db = ethdb.NewMemDatabase()
1042+
db = memorydb.New()
1043+
10411044
// this code generates a log
10421045
code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
10431046
gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}}

0 commit comments

Comments
 (0)