Skip to content

Commit

Permalink
[release-v1.5] server: increase ban score for notfound messages
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh committed Aug 27, 2020
1 parent 4268ca1 commit 6b61c54
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ func directionString(inbound bool) string {
return "outbound"
}

// pickNoun returns the singular or plural form of a noun depending
// on the count n.
func pickNoun(n uint64, singular, plural string) string {
if n == 1 {
return singular
}
return plural
}

// fatalf logs a string, then cleanly exits.
func fatalf(str string) {
dcrdLog.Errorf("Unable to create profiler: %v", str)
Expand Down
28 changes: 28 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,34 @@ func (sp *serverPeer) OnWrite(p *peer.Peer, bytesWritten int, msg wire.Message,

// OnNotFound is invoked when a peer sends a notfound message.
func (sp *serverPeer) OnNotFound(_ *peer.Peer, msg *wire.MsgNotFound) {
if !sp.Connected() {
return
}

var numBlocks, numTxns uint32
for _, inv := range msg.InvList {
switch inv.Type {
case wire.InvTypeBlock:
numBlocks++
case wire.InvTypeTx:
numTxns++
default:
peerLog.Debugf("Invalid inv type '%d' in notfound message from %s",
inv.Type, sp)
sp.Disconnect()
return
}
}
if numBlocks > 0 {
blockStr := pickNoun(uint64(numBlocks), "block", "blocks")
reason := fmt.Sprintf("%d %v not found", numBlocks, blockStr)
sp.addBanScore(20*numBlocks, 0, reason)
}
if numTxns > 0 {
txStr := pickNoun(uint64(numTxns), "transaction", "transactions")
reason := fmt.Sprintf("%d %v not found", numBlocks, txStr)
sp.addBanScore(0, 10*numTxns, reason)
}
sp.server.blockManager.QueueNotFound(msg, sp)
}

Expand Down

0 comments on commit 6b61c54

Please sign in to comment.