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

acquire chain lock where applicable #1431

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,8 @@ func locateHeaders(locator []*BlockHash, stopHash *BlockHash, maxHeaders uint32,
func (bc *Blockchain) LocateBestBlockChainHeaders(
locator []*BlockHash, stopHash *BlockHash, maxHeaders uint32) []*MsgDeSoHeader {

// TODO: Shouldn't we hold a ChainLock here? I think it's fine though because the place
// where it's currently called is single-threaded via a channel in server.go. Going to
// avoid messing with it for now.
bc.ChainLock.RLock()
defer bc.ChainLock.RUnlock()
headers := locateHeaders(locator, stopHash, maxHeaders,
bc.blockIndexByHash, bc.bestChain, bc.bestChainMap)

Expand Down Expand Up @@ -1422,6 +1421,8 @@ func (bc *Blockchain) GetBlockNodeWithHash(hash *BlockHash) *BlockNode {
if hash == nil {
return nil
}
bc.ChainLock.RLock()
defer bc.ChainLock.RUnlock()
return bc.bestChainMap[*hash]
}

Expand Down
9 changes: 9 additions & 0 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,8 @@ func (srv *Server) shouldVerifySignatures(header *MsgDeSoHeader, isHeaderChain b
}
var hasSeenCheckpointBlockHash bool
var checkpointBlockNode *BlockNode
srv.blockchain.ChainLock.RLock()
defer srv.blockchain.ChainLock.RUnlock()
if isHeaderChain {
checkpointBlockNode, hasSeenCheckpointBlockHash = srv.blockchain.bestHeaderChainMap[*checkpointBlockInfo.Hash]
} else {
Expand Down Expand Up @@ -1047,6 +1049,8 @@ func (srv *Server) getCheckpointSyncingStatus(isHeaders bool) string {
return "<No checkpoint block info>"
}
hasSeenCheckPointBlockHash := false
srv.blockchain.ChainLock.RLock()
defer srv.blockchain.ChainLock.RUnlock()
if isHeaders {
_, hasSeenCheckPointBlockHash = srv.blockchain.bestHeaderChainMap[*checkpointBlockInfo.Hash]
} else {
Expand Down Expand Up @@ -1692,6 +1696,8 @@ func (srv *Server) _handleSnapshot(pp *Peer, msg *MsgDeSoSnapshotData) {
// being too large and possibly causing an error in badger.
glog.V(0).Infof("Server._handleSnapshot: Updating snapshot block nodes in the database")
var blockNodeBatch []*BlockNode
// acquire the chain lock while we update the best chain and best chain map.
srv.blockchain.ChainLock.Lock()
for ii := uint64(1); ii <= srv.HyperSyncProgress.SnapshotMetadata.SnapshotBlockHeight; ii++ {
currentNode := srv.blockchain.bestHeaderChain[ii]
// Do not set the StatusBlockStored flag, because we still need to download the past blocks.
Expand Down Expand Up @@ -1750,6 +1756,9 @@ func (srv *Server) _handleSnapshot(pp *Peer, msg *MsgDeSoSnapshotData) {
srv.snapshot.Status.CurrentBlockHeight = msg.SnapshotMetadata.SnapshotBlockHeight
srv.snapshot.Status.SaveStatus()

// Unlock chain lock now that we're done modifying the chain state.
srv.blockchain.ChainLock.Unlock()

glog.Infof("server._handleSnapshot: FINAL snapshot checksum is (%v) (%v)",
srv.snapshot.CurrentEpochSnapshotMetadata.CurrentEpochChecksumBytes,
hex.EncodeToString(srv.snapshot.CurrentEpochSnapshotMetadata.CurrentEpochChecksumBytes))
Expand Down
Loading