Skip to content

Commit 3f2de3f

Browse files
committed
refactoring of black list logging
1 parent e8367ff commit 3f2de3f

11 files changed

+32
-54
lines changed

src/main/scala/io/iohk/ethereum/blockchain/sync/BlacklistSupport.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package io.iohk.ethereum.blockchain.sync
22

33
import scala.concurrent.duration.FiniteDuration
44
import scala.concurrent.ExecutionContext.Implicits.global
5-
6-
import akka.actor.{Scheduler, Cancellable, ActorRef, Actor}
5+
import akka.actor.{Actor, ActorLogging, ActorRef, Cancellable, Scheduler}
76

87
trait BlacklistSupport {
9-
selfActor: Actor =>
8+
selfActor: Actor with ActorLogging =>
109

1110
import BlacklistSupport._
1211

1312
def scheduler: Scheduler
1413

1514
var blacklistedPeers: Seq[(ActorRef, Cancellable)] = Nil
1615

17-
def blacklist(peer: ActorRef, duration: FiniteDuration): Unit = {
16+
def blacklist(peer: ActorRef, duration: FiniteDuration, reason: String): Unit = {
1817
undoBlacklist(peer)
18+
log.info(s"Blacklisting peer (${peer.path.name}), $reason")
1919
val unblacklistCancellable = scheduler.scheduleOnce(duration, self, UnblacklistPeer(peer))
2020
blacklistedPeers :+= (peer, unblacklistCancellable)
2121
}
@@ -30,6 +30,6 @@ trait BlacklistSupport {
3030
}
3131

3232
object BlacklistSupport {
33-
case class BlacklistPeer(peer: ActorRef)
33+
case class BlacklistPeer(peer: ActorRef, reason: String)
3434
case class UnblacklistPeer(peer: ActorRef)
3535
}

src/main/scala/io/iohk/ethereum/blockchain/sync/FastSync.scala

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,13 @@ trait FastSync {
6767

6868
case PeerActor.MessageReceived(BlockHeaders(blockHeaders)) =>
6969
sender() ! PeerActor.Unsubscribe
70-
log.info("Peer ({}) did not respond with 1 header but with {}, blacklisting for {}",
71-
sender().path.name,
72-
blockHeaders.size,
73-
blacklistDuration)
74-
blacklist(sender(), blacklistDuration)
70+
blacklist(sender(), blacklistDuration,s"did not respond with 1 header but with ${blockHeaders.size}, blacklisting for $blacklistDuration")
7571
context become waitingForBlockHeaders(waitingFor - sender(), received, timeout)
7672

7773
case BlockHeadersTimeout =>
7874
waitingFor.foreach { peer =>
7975
peer ! PeerActor.Unsubscribe
80-
log.info("Peer ({}) did not respond within required time with block header, blacklisting for {}",
81-
peer.path.name,
82-
blacklistDuration)
83-
blacklist(peer, blacklistDuration)
76+
blacklist(peer, blacklistDuration, s"did not respond within required time with block header, blacklisting for $blacklistDuration")
8477
}
8578
tryStartFastSync(received)
8679
}
@@ -119,21 +112,13 @@ trait FastSync {
119112
startFastSync(initialSyncState)
120113

121114
case None =>
122-
log.info("Peer ({}) did not respond with target block header, blacklisting and scheduling retry in {}",
123-
sender().path.name,
124-
startRetryInterval)
125-
126-
blacklist(sender(), blacklistDuration)
115+
blacklist(sender(), blacklistDuration,s"did not respond with target block header, blacklisting and scheduling retry in $startRetryInterval")
127116
scheduleStartRetry(startRetryInterval)
128117
context become startingFastSync
129118
}
130119

131120
case TargetBlockTimeout =>
132-
log.info("Peer ({}) did not respond with target block header (timeout), blacklisting and scheduling retry in {}",
133-
sender().path.name,
134-
startRetryInterval)
135-
136-
blacklist(sender(), blacklistDuration)
121+
blacklist(sender(), blacklistDuration, s"did not respond with target block header (timeout), blacklisting and scheduling retry in $startRetryInterval")
137122
peer ! PeerActor.Unsubscribe
138123
scheduleStartRetry(startRetryInterval)
139124
context become startingFastSync

src/main/scala/io/iohk/ethereum/blockchain/sync/FastSyncNodesRequestHandler.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ class FastSyncNodesRequestHandler(
2323

2424
override def handleResponseMsg(nodeData: NodeData): Unit = {
2525
if (nodeData.values.isEmpty) {
26-
log.info(s"Blacklisting peer (${peer.path.name}), " +
27-
s"got empty mpt node response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.v.toArray[Byte]))}")
28-
syncController ! BlacklistSupport.BlacklistPeer(peer)
26+
val reason = s"got empty mpt node response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.v.toArray[Byte]))}"
27+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
2928
}
3029

3130
val receivedHashes = nodeData.values.map(v => ByteString(kec256(v.toArray[Byte])))
@@ -61,9 +60,8 @@ class FastSyncNodesRequestHandler(
6160
}
6261

6362
override def handleTimeout(): Unit = {
64-
log.info(s"Blacklisting peer (${peer.path.name}), " +
65-
s"time out on mpt node response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.v.toArray[Byte]))}")
66-
syncController ! BlacklistSupport.BlacklistPeer(peer)
63+
val reason = s"time out on mpt node response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.v.toArray[Byte]))}"
64+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
6765
syncController ! FastSync.EnqueueNodes(requestedHashes)
6866
cleanupAndStop()
6967
}

src/main/scala/io/iohk/ethereum/blockchain/sync/FastSyncReceiptsRequestHandler.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class FastSyncReceiptsRequestHandler(
2626
updateBestBlockIfNeeded(receivedHashes)
2727

2828
if (receipts.receiptsForBlocks.isEmpty) {
29-
log.info(s"Blacklisting peer (${peer.path.name}), " +
30-
s"got empty receipts for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}")
31-
syncController ! BlacklistSupport.BlacklistPeer(peer)
29+
val reason = s"got empty receipts for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}"
30+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
3231
}
3332

3433
val remainingReceipts = requestedHashes.drop(receipts.receiptsForBlocks.size)
@@ -57,9 +56,8 @@ class FastSyncReceiptsRequestHandler(
5756
}
5857

5958
override def handleTimeout(): Unit = {
60-
log.info(s"Blacklisting peer (${peer.path.name}), " +
61-
s"time out on receipts response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}")
62-
syncController ! BlacklistSupport.BlacklistPeer(peer)
59+
val reason = s"time out on receipts response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}"
60+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
6361
syncController ! FastSync.EnqueueReceipts(requestedHashes)
6462
cleanupAndStop()
6563
}

src/main/scala/io/iohk/ethereum/blockchain/sync/RegularSync.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ trait RegularSync {
162162
}
163163

164164
private def resumeWithDifferentPeer(currentPeer: ActorRef) = {
165-
log.info(s"Blacklisting peer (${currentPeer.path.name}), because of error in response")
166-
self ! BlacklistPeer(currentPeer)
165+
self ! BlacklistPeer(currentPeer, "because of error in response")
167166
headersQueue = Seq.empty
168167
context.self ! ResumeRegularSync
169168
}

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncBlockBodiesRequestHandler.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class SyncBlockBodiesRequestHandler(
1818

1919
override def handleResponseMsg(blockBodies: BlockBodies): Unit = {
2020
if (blockBodies.bodies.isEmpty) {
21-
log.info(s"Blacklisting peer (${peer.path.name}), " +
22-
s"got empty block bodies response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}")
23-
syncController ! BlacklistSupport.BlacklistPeer(peer)
21+
val reason = s"got empty block bodies response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}"
22+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
2423
} else {
2524
syncController ! BlockBodiesReceived(peer, requestedHashes, blockBodies.bodies)
2625
}
@@ -30,9 +29,8 @@ class SyncBlockBodiesRequestHandler(
3029
}
3130

3231
override def handleTimeout(): Unit = {
33-
log.info(s"Blacklisting peer (${peer.path.name}), " +
34-
s"time out on block bodies response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}")
35-
syncController ! BlacklistSupport.BlacklistPeer(peer)
32+
val reason = s"time out on block bodies response for known hashes: ${requestedHashes.map(h => Hex.toHexString(h.toArray[Byte]))}"
33+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
3634
syncController ! FastSync.EnqueueBlockBodies(requestedHashes)
3735
cleanupAndStop()
3836
}

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncBlockHeadersRequestHandler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class SyncBlockHeadersRequestHandler(
3232
log.info("Received {} block headers in {} ms", headers.size, timeTakenSoFar())
3333
}
3434
} else {
35-
log.info(s"Blacklisting peer (${peer.path.name}), got error in block headers response for request: $requestMsg")
36-
syncController ! BlacklistSupport.BlacklistPeer(peer)
35+
val reason = s"got error in block headers response for requested: ${requestMsg.block}"
36+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
3737
}
3838

3939
cleanupAndStop()
4040
}
4141

4242
override def handleTimeout(): Unit = {
43-
log.info(s"Blacklisting peer (${peer.path.name}), got time out waiting for block headers response for request: $requestMsg")
44-
syncController ! BlacklistSupport.BlacklistPeer(peer)
43+
val reason = s"got time out waiting for block headers response for requested: ${requestMsg.block}"
44+
syncController ! BlacklistSupport.BlacklistPeer(peer, reason)
4545
cleanupAndStop()
4646
}
4747

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncController.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class SyncController(
8080
case Terminated(ref) if handshakedPeers.contains(ref) =>
8181
removePeer(ref)
8282

83-
case BlacklistPeer(ref) =>
84-
blacklist(ref, blacklistDuration)
83+
case BlacklistPeer(ref, reason) =>
84+
blacklist(ref, blacklistDuration, reason)
8585

8686
case UnblacklistPeer(ref) =>
8787
undoBlacklist(ref)

src/test/scala/io/iohk/ethereum/blockchain/sync/FastSyncBlockBodiesRequestHandlerSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FastSyncBlockBodiesRequestHandlerSpec extends FlatSpec with Matchers {
3232
val responseBodies = Nil
3333
peer.reply(PeerActor.MessageReceived(BlockBodies(responseBodies)))
3434

35-
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref))
35+
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref, "got empty block bodies response for known hashes: List(31, 32)"))
3636
parent.expectMsg(SyncRequestHandler.Done)
3737

3838
peer.expectMsg(PeerActor.Unsubscribe)
@@ -44,7 +44,7 @@ class FastSyncBlockBodiesRequestHandlerSpec extends FlatSpec with Matchers {
4444

4545
time.advance(10.seconds)
4646

47-
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref))
47+
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref, "time out on block bodies response for known hashes: List(31, 32)"))
4848
parent.expectMsg(FastSync.EnqueueBlockBodies(requestedHashes))
4949
parent.expectMsg(SyncRequestHandler.Done)
5050

src/test/scala/io/iohk/ethereum/blockchain/sync/FastSyncBlockHeadersRequestHandlerSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FastSyncBlockHeadersRequestHandlerSpec extends FlatSpec with Matchers {
7070

7171
time.advance(10.seconds)
7272

73-
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref))
73+
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref, "got time out waiting for block headers response for requested: Left(1)"))
7474
parent.expectMsg(SyncRequestHandler.Done)
7575

7676
peer.expectMsg(PeerActor.Unsubscribe)

src/test/scala/io/iohk/ethereum/blockchain/sync/FastSyncReceiptsRequestHandlerSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FastSyncReceiptsRequestHandlerSpec extends FlatSpec with Matchers {
3434

3535
time.advance(10.seconds)
3636

37-
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref))
37+
parent.expectMsg(BlacklistSupport.BlacklistPeer(peer.ref, "time out on receipts response for known hashes: List(31, 32)"))
3838
parent.expectMsg(FastSync.EnqueueReceipts(requestedHashes))
3939
parent.expectMsg(SyncRequestHandler.Done)
4040

0 commit comments

Comments
 (0)