From be556ada9b92ed84beb0b615096586e8091b994f Mon Sep 17 00:00:00 2001 From: Svarog Date: Sun, 2 Feb 2020 18:27:18 +0200 Subject: [PATCH] [NOD-719] Added defers to unlocks (#618) * [NOD-719] Added defers to unlocks * [NOD-719] Added another defer to another Unlock * [NOD-719] Added yet another defer to yet another Unlock --- blockdag/dag.go | 4 ++-- blockdag/thresholdstate.go | 4 ++-- netsync/manager.go | 9 +++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/blockdag/dag.go b/blockdag/dag.go index 9b5a1769f..cf6745316 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -1713,11 +1713,11 @@ func (dag *BlockDAG) antiPastBetween(lowHash, highHash *daghash.Hash, maxEntries // This function is safe for concurrent access. func (dag *BlockDAG) AntiPastHashesBetween(lowHash, highHash *daghash.Hash, maxHashes uint64) ([]*daghash.Hash, error) { dag.dagLock.RLock() + defer dag.dagLock.RUnlock() hashes, err := dag.antiPastHashesBetween(lowHash, highHash, maxHashes) if err != nil { return nil, err } - dag.dagLock.RUnlock() return hashes, nil } @@ -1791,11 +1791,11 @@ func (dag *BlockDAG) RUnlock() { // This function is safe for concurrent access. func (dag *BlockDAG) AntiPastHeadersBetween(lowHash, highHash *daghash.Hash) ([]*wire.BlockHeader, error) { dag.dagLock.RLock() + defer dag.dagLock.RUnlock() headers, err := dag.antiPastHeadersBetween(lowHash, highHash, wire.MaxBlockHeadersPerMsg) if err != nil { return nil, err } - dag.dagLock.RUnlock() return headers, nil } diff --git a/blockdag/thresholdstate.go b/blockdag/thresholdstate.go index a08a1c9af..0ea99c7af 100644 --- a/blockdag/thresholdstate.go +++ b/blockdag/thresholdstate.go @@ -267,8 +267,8 @@ func (dag *BlockDAG) thresholdState(prevNode *blockNode, checker thresholdCondit // This function is safe for concurrent access. func (dag *BlockDAG) ThresholdState(deploymentID uint32) (ThresholdState, error) { dag.dagLock.Lock() + defer dag.dagLock.Unlock() state, err := dag.deploymentState(dag.selectedTip(), deploymentID) - dag.dagLock.Unlock() return state, err } @@ -279,8 +279,8 @@ func (dag *BlockDAG) ThresholdState(deploymentID uint32) (ThresholdState, error) // This function is safe for concurrent access. func (dag *BlockDAG) IsDeploymentActive(deploymentID uint32) (bool, error) { dag.dagLock.Lock() + defer dag.dagLock.Unlock() state, err := dag.deploymentState(dag.selectedTip(), deploymentID) - dag.dagLock.Unlock() if err != nil { return false, err } diff --git a/netsync/manager.go b/netsync/manager.go index 5d8d0e602..2f5824752 100644 --- a/netsync/manager.go +++ b/netsync/manager.go @@ -6,6 +6,11 @@ package netsync import ( "fmt" + "net" + "sync" + "sync/atomic" + "time" + "github.com/kaspanet/kaspad/blockdag" "github.com/kaspanet/kaspad/dagconfig" "github.com/kaspanet/kaspad/database" @@ -15,10 +20,6 @@ import ( "github.com/kaspanet/kaspad/util/daghash" "github.com/kaspanet/kaspad/wire" "github.com/pkg/errors" - "net" - "sync" - "sync/atomic" - "time" ) const (