[NOD-120] Invert WithLock standard (#256)

This commit is contained in:
Ori Newman 2019-04-18 17:06:34 +03:00 committed by Svarog
parent e22bc9af8f
commit e99af346bf
7 changed files with 25 additions and 24 deletions

View File

@ -709,20 +709,20 @@ func (dag *BlockDAG) updateFinalityPoint() {
dag.lastFinalityPoint = newFinalityPoint dag.lastFinalityPoint = newFinalityPoint
} }
// NextBlockFeeTransactionWithLock prepares the fee transaction for the next mined block // NextBlockFeeTransaction prepares the fee transaction for the next mined block
// //
// This function CAN'T be called with the DAG lock not held. // This function CAN'T be called with the DAG lock not held.
func (dag *BlockDAG) NextBlockFeeTransactionWithLock() (*wire.MsgTx, error) { func (dag *BlockDAG) NextBlockFeeTransaction() (*wire.MsgTx, error) {
dag.dagLock.RLock() dag.dagLock.RLock()
defer dag.dagLock.RUnlock() defer dag.dagLock.RUnlock()
return dag.NextBlockFeeTransaction() return dag.NextBlockFeeTransactionNoLock()
} }
// NextBlockFeeTransaction prepares the fee transaction for the next mined block // NextBlockFeeTransactionNoLock prepares the fee transaction for the next mined block
// //
// This function MUST be called with the DAG read-lock held // This function MUST be called with the DAG read-lock held
func (dag *BlockDAG) NextBlockFeeTransaction() (*wire.MsgTx, error) { func (dag *BlockDAG) NextBlockFeeTransactionNoLock() (*wire.MsgTx, error) {
_, txsAcceptanceData, _, err := dag.virtual.blockNode.verifyAndBuildUTXO(dag, nil, true) _, txsAcceptanceData, _, err := dag.virtual.blockNode.verifyAndBuildUTXO(dag, nil, true)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1183,21 +1183,21 @@ func countSpentOutputs(block *util.Block) int {
return numSpent return numSpent
} }
// CheckConnectBlockTemplateWithLock fully validates that connecting the passed block to // CheckConnectBlockTemplate fully validates that connecting the passed block to
// the DAG does not violate any consensus rules, aside from the proof of // the DAG does not violate any consensus rules, aside from the proof of
// work requirement. // work requirement.
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (dag *BlockDAG) CheckConnectBlockTemplateWithLock(block *util.Block) error { func (dag *BlockDAG) CheckConnectBlockTemplate(block *util.Block) error {
dag.dagLock.RLock() dag.dagLock.RLock()
defer dag.dagLock.RUnlock() defer dag.dagLock.RUnlock()
return dag.CheckConnectBlockTemplate(block) return dag.CheckConnectBlockTemplateNoLock(block)
} }
// CheckConnectBlockTemplate fully validates that connecting the passed block to // CheckConnectBlockTemplateNoLock fully validates that connecting the passed block to
// the DAG does not violate any consensus rules, aside from the proof of // the DAG does not violate any consensus rules, aside from the proof of
// work requirement. The block must connect to the current tip of the main dag. // work requirement. The block must connect to the current tip of the main dag.
func (dag *BlockDAG) CheckConnectBlockTemplate(block *util.Block) error { func (dag *BlockDAG) CheckConnectBlockTemplateNoLock(block *util.Block) error {
// Skip the proof of work check as this is just a block template. // Skip the proof of work check as this is just a block template.
flags := BFNoPoWCheck flags := BFNoPoWCheck

View File

@ -107,14 +107,14 @@ func TestCheckConnectBlockTemplate(t *testing.T) {
} }
// Block 3 should fail to connect since it's already inserted. // Block 3 should fail to connect since it's already inserted.
err = dag.CheckConnectBlockTemplate(blocks[3]) err = dag.CheckConnectBlockTemplateNoLock(blocks[3])
if err == nil { if err == nil {
t.Fatal("CheckConnectBlockTemplate: Did not received expected error " + t.Fatal("CheckConnectBlockTemplate: Did not received expected error " +
"on block 3") "on block 3")
} }
// Block 4 should connect successfully to tip of chain. // Block 4 should connect successfully to tip of chain.
err = dag.CheckConnectBlockTemplate(blocks[4]) err = dag.CheckConnectBlockTemplateNoLock(blocks[4])
if err != nil { if err != nil {
t.Fatalf("CheckConnectBlockTemplate: Received unexpected error on "+ t.Fatalf("CheckConnectBlockTemplate: Received unexpected error on "+
"block 4: %v", err) "block 4: %v", err)
@ -127,7 +127,7 @@ func TestCheckConnectBlockTemplate(t *testing.T) {
// Block 3a should connect even though it does not build on dag tips. // Block 3a should connect even though it does not build on dag tips.
blocks[5].SetHeight(3) // set height manually because it was set to 0 in loadBlocks blocks[5].SetHeight(3) // set height manually because it was set to 0 in loadBlocks
err = dag.CheckConnectBlockTemplate(blocks[5]) err = dag.CheckConnectBlockTemplateNoLock(blocks[5])
if err != nil { if err != nil {
t.Fatal("CheckConnectBlockTemplate: Recieved unexpected error on " + t.Fatal("CheckConnectBlockTemplate: Recieved unexpected error on " +
"block 3a that connects below the tips") "block 3a that connects below the tips")
@ -138,7 +138,7 @@ func TestCheckConnectBlockTemplate(t *testing.T) {
invalidPowMsgBlock.Header.Nonce++ invalidPowMsgBlock.Header.Nonce++
invalidPowBlock := util.NewBlock(&invalidPowMsgBlock) invalidPowBlock := util.NewBlock(&invalidPowMsgBlock)
invalidPowBlock.SetHeight(blocks[4].Height()) invalidPowBlock.SetHeight(blocks[4].Height())
err = dag.CheckConnectBlockTemplate(invalidPowBlock) err = dag.CheckConnectBlockTemplateNoLock(invalidPowBlock)
if err != nil { if err != nil {
t.Fatalf("CheckConnectBlockTemplate: Received unexpected error on "+ t.Fatalf("CheckConnectBlockTemplate: Received unexpected error on "+
"block 4 with bad nonce: %v", err) "block 4 with bad nonce: %v", err)
@ -147,7 +147,7 @@ func TestCheckConnectBlockTemplate(t *testing.T) {
// Invalid block building on chain tip should fail to connect. // Invalid block building on chain tip should fail to connect.
invalidBlock := *blocks[4].MsgBlock() invalidBlock := *blocks[4].MsgBlock()
invalidBlock.Header.Bits-- invalidBlock.Header.Bits--
err = dag.CheckConnectBlockTemplate(util.NewBlock(&invalidBlock)) err = dag.CheckConnectBlockTemplateNoLock(util.NewBlock(&invalidBlock))
if err == nil { if err == nil {
t.Fatal("CheckConnectBlockTemplate: Did not received expected error " + t.Fatal("CheckConnectBlockTemplate: Did not received expected error " +
"on block 4 with invalid difficulty bits") "on block 4 with invalid difficulty bits")

View File

@ -6,7 +6,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/daglabs/btcd/dagconfig/daghash"
"log" "log"
"net" "net"
"os" "os"
@ -15,6 +14,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/daglabs/btcd/dagconfig/daghash"
"github.com/daglabs/btcd/connmgr" "github.com/daglabs/btcd/connmgr"
"github.com/daglabs/btcd/peer" "github.com/daglabs/btcd/peer"
"github.com/daglabs/btcd/signal" "github.com/daglabs/btcd/signal"

View File

@ -413,7 +413,7 @@ func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress util.Address) (*BlockTe
} }
numCoinbaseSigOps := int64(blockdag.CountSigOps(coinbaseTx)) numCoinbaseSigOps := int64(blockdag.CountSigOps(coinbaseTx))
msgFeeTransaction, err := g.dag.NextBlockFeeTransaction() msgFeeTransaction, err := g.dag.NextBlockFeeTransactionNoLock()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -675,7 +675,7 @@ func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress util.Address) (*BlockTe
block := util.NewBlock(&msgBlock) block := util.NewBlock(&msgBlock)
block.SetHeight(nextBlockHeight) block.SetHeight(nextBlockHeight)
if err := g.dag.CheckConnectBlockTemplate(block); err != nil { if err := g.dag.CheckConnectBlockTemplateNoLock(block); err != nil {
return nil, err return nil, err
} }

View File

@ -665,12 +665,12 @@ func (sm *SyncManager) addBlocksToRequestQueue(state *peerSyncState, hashes []*d
for _, hash := range hashes { for _, hash := range hashes {
if _, exists := sm.requestedBlocks[*hash]; !exists { if _, exists := sm.requestedBlocks[*hash]; !exists {
iv := wire.NewInvVect(wire.InvTypeBlock, hash) iv := wire.NewInvVect(wire.InvTypeBlock, hash)
state.addInvToRequestQueue(iv, isRelayedInv) state.addInvToRequestQueueNoLock(iv, isRelayedInv)
} }
} }
} }
func (state *peerSyncState) addInvToRequestQueue(iv *wire.InvVect, isRelayedInv bool) { func (state *peerSyncState) addInvToRequestQueueNoLock(iv *wire.InvVect, isRelayedInv bool) {
if isRelayedInv { if isRelayedInv {
if _, exists := state.relayedInvsRequestQueueSet[*iv.Hash]; !exists { if _, exists := state.relayedInvsRequestQueueSet[*iv.Hash]; !exists {
state.relayedInvsRequestQueueSet[*iv.Hash] = struct{}{} state.relayedInvsRequestQueueSet[*iv.Hash] = struct{}{}
@ -684,10 +684,10 @@ func (state *peerSyncState) addInvToRequestQueue(iv *wire.InvVect, isRelayedInv
} }
} }
func (state *peerSyncState) addInvToRequestQueueWithLock(iv *wire.InvVect, isRelayedInv bool) { func (state *peerSyncState) addInvToRequestQueue(iv *wire.InvVect, isRelayedInv bool) {
state.requestQueueMtx.Lock() state.requestQueueMtx.Lock()
defer state.requestQueueMtx.Unlock() defer state.requestQueueMtx.Unlock()
state.addInvToRequestQueue(iv, isRelayedInv) state.addInvToRequestQueueNoLock(iv, isRelayedInv)
} }
// fetchHeaderBlocks creates and sends a request to the syncPeer for the next // fetchHeaderBlocks creates and sends a request to the syncPeer for the next
@ -955,7 +955,7 @@ func (sm *SyncManager) handleInvMsg(imsg *invMsg) {
} }
// Add it to the request queue. // Add it to the request queue.
state.addInvToRequestQueueWithLock(iv, iv.Type != wire.InvTypeSyncBlock) state.addInvToRequestQueue(iv, iv.Type != wire.InvTypeSyncBlock)
continue continue
} }

View File

@ -2154,7 +2154,7 @@ func handleGetBlockTemplateProposal(s *Server, request *btcjson.TemplateRequest)
return "bad-parentblk", nil return "bad-parentblk", nil
} }
if err := s.cfg.DAG.CheckConnectBlockTemplateWithLock(block); err != nil { if err := s.cfg.DAG.CheckConnectBlockTemplate(block); err != nil {
if _, ok := err.(blockdag.RuleError); !ok { if _, ok := err.(blockdag.RuleError); !ok {
errStr := fmt.Sprintf("Failed to process block proposal: %s", err) errStr := fmt.Sprintf("Failed to process block proposal: %s", err)
log.Error(errStr) log.Error(errStr)