Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of dual mutex in blockchain.go #1099

Merged
merged 7 commits into from Feb 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 7 additions & 30 deletions blockchain/blockchain.go
Expand Up @@ -159,8 +159,7 @@ type BlockChain struct {
scope event.SubscriptionScope
genesisBlock *types.Block

mu sync.RWMutex // global mutex for locking chain operations
chainmu sync.RWMutex // blockchain insertion lock
mu sync.RWMutex // global mutex for locking chain operations
jeongkyun-oh marked this conversation as resolved.
Show resolved Hide resolved
yoomee1313 marked this conversation as resolved.
Show resolved Hide resolved

checkpoint int // checkpoint counts towards the new checkpoint
currentBlock atomic.Value // Current head of the block chain
Expand Down Expand Up @@ -1572,8 +1571,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
bc.wg.Add(1)
defer bc.wg.Done()

bc.chainmu.Lock()
defer bc.chainmu.Unlock()
bc.mu.Lock()
defer bc.mu.Unlock()

// A queued approach to delivering events. This is generally
// faster than direct delivery and requires much less mutex
Expand Down Expand Up @@ -1721,9 +1720,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
winner[j], winner[len(winner)-1-j] = winner[len(winner)-1-j], winner[j]
}
// Import all the pruned blocks to make the state available
bc.chainmu.Unlock()
bc.mu.Unlock()
hqjang-pepper marked this conversation as resolved.
Show resolved Hide resolved
_, evs, logs, err := bc.insertChain(winner)
bc.chainmu.Lock()
bc.mu.Lock()
events, coalescedLogs = evs, logs

if err != nil {
Expand Down Expand Up @@ -2262,15 +2261,13 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
}

// Make sure only one thread manipulates the chain at once
bc.chainmu.Lock()
defer bc.chainmu.Unlock()
bc.mu.Lock()
defer bc.mu.Unlock()

bc.wg.Add(1)
defer bc.wg.Done()

whFunc := func(header *types.Header) error {
bc.mu.Lock()
kjhman21 marked this conversation as resolved.
Show resolved Hide resolved
defer bc.mu.Unlock()

_, err := bc.hc.WriteHeader(header)
return err
Expand All @@ -2279,26 +2276,6 @@ func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (i
return bc.hc.InsertHeaderChain(chain, whFunc, start)
}

// writeHeader writes a header into the local chain, given that its parent is
// already known. If the total blockscore of the newly inserted header becomes
// greater than the current known TD, the canonical chain is re-routed.
//
// Note: This method is not concurrent-safe with inserting blocks simultaneously
// into the chain, as side effects caused by reorganisations cannot be emulated
// without the real blocks. Hence, writing headers directly should only be done
// in two scenarios: pure-header mode of operation (light clients), or properly
// separated header/block phases (non-archive clients).
func (bc *BlockChain) writeHeader(header *types.Header) error {
yoomee1313 marked this conversation as resolved.
Show resolved Hide resolved
bc.wg.Add(1)
defer bc.wg.Done()

bc.mu.Lock()
defer bc.mu.Unlock()

_, err := bc.hc.WriteHeader(header)
return err
}

// CurrentHeader retrieves the current head header of the canonical chain. The
// header is retrieved from the HeaderChain's internal cache.
func (bc *BlockChain) CurrentHeader() *types.Header {
Expand Down