Skip to content

Commit d40732a

Browse files
authored
[Kaizen] Add debug logs for PendingTransactionsManager (#978)
1 parent c38c2c2 commit d40732a

File tree

10 files changed

+47
-20
lines changed

10 files changed

+47
-20
lines changed

bytes/src/main/scala/io/iohk/ethereum/utils/ByteStringUtils.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ object ByteStringUtils {
3030
}
3131
}
3232

33+
implicit class ByteStringOps(val bytes: ByteString) extends AnyVal {
34+
def toHex: String = Hex.toHexString(bytes.toArray[Byte])
35+
}
36+
3337
sealed trait ByteStringElement {
3438
def len: Int
3539
def asByteArray: Array[Byte]

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,12 @@ class BlockImporterItSpec
265265

266266
eventually {
267267

268-
val msg = fetcherProbe.fishForMessage(Timeouts.longTimeout) {
269-
case BlockFetcher.FetchStateNode(_) => true
270-
case _ => false
271-
}.asInstanceOf[BlockFetcher.FetchStateNode]
268+
val msg = fetcherProbe
269+
.fishForMessage(Timeouts.longTimeout) {
270+
case BlockFetcher.FetchStateNode(_) => true
271+
case _ => false
272+
}
273+
.asInstanceOf[BlockFetcher.FetchStateNode]
272274

273275
msg.hash.length should be > 0
274276
}

src/main/scala/io/iohk/ethereum/consensus/pow/MockedMiner.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import io.iohk.ethereum.domain.{Block, Blockchain}
1313
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
1414
import io.iohk.ethereum.nodebuilder.Node
1515
import io.iohk.ethereum.utils.ByteStringUtils
16+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
1617
import monix.execution.Scheduler
18+
1719
import scala.concurrent.duration._
1820

1921
class MockedMiner(
@@ -79,7 +81,7 @@ class MockedMiner(
7981
log.info(
8082
s"Mining mocked block {} successful. Included transactions: {}",
8183
minedBlock.idTag,
82-
minedBlock.body.transactionList.map(_.hashAsHexString)
84+
minedBlock.body.transactionList.map(_.hash.toHex)
8385
)
8486
syncEventListener ! SyncProtocol.MinedBlock(minedBlock)
8587
// because of using seconds to calculate block timestamp, we can't mine blocks faster than one block per second

src/main/scala/io/iohk/ethereum/domain/BlockBody.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package io.iohk.ethereum.domain
22

33
import io.iohk.ethereum.domain.BlockHeaderImplicits._
44
import io.iohk.ethereum.rlp.{RLPEncodeable, RLPList, RLPSerializable, rawDecode}
5+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
56

67
case class BlockBody(transactionList: Seq[SignedTransaction], uncleNodesList: Seq[BlockHeader]) {
78
override def toString: String =
89
s"BlockBody{ transactionList: $transactionList, uncleNodesList: $uncleNodesList }"
910

1011
def toShortString: String =
11-
s"BlockBody { transactionsList: ${transactionList.map(_.hashAsHexString)}, uncleNodesList: ${uncleNodesList.map(_.hashAsHexString)} }"
12+
s"BlockBody { transactionsList: ${transactionList.map(_.hash.toHex)}, uncleNodesList: ${uncleNodesList.map(_.hashAsHexString)} }"
1213

1314
lazy val numberOfTxs: Int = transactionList.size
1415

src/main/scala/io/iohk/ethereum/domain/SignedTransaction.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ case class SignedTransaction(tx: Transaction, signature: ECDSASignature) {
170170
signature.v != ECDSASignature.negativePointSign && signature.v != ECDSASignature.positivePointSign
171171

172172
lazy val hash: ByteString = ByteString(kec256(this.toBytes: Array[Byte]))
173-
lazy val hashAsHexString: String = Hex.toHexString(hash.toArray[Byte])
174173
}
175174

176175
case class SignedTransactionWithSender(tx: SignedTransaction, senderAddress: Address)

src/main/scala/io/iohk/ethereum/jsonrpc/PersonalService.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.iohk.ethereum.jsonrpc
22

33
import java.time.Duration
4-
54
import akka.actor.ActorRef
65
import akka.util.{ByteString, Timeout}
76
import io.iohk.ethereum.crypto
@@ -15,10 +14,11 @@ import io.iohk.ethereum.keystore.{KeyStore, Wallet}
1514
import io.iohk.ethereum.rlp.RLPList
1615
import io.iohk.ethereum.transactions.PendingTransactionsManager
1716
import io.iohk.ethereum.transactions.PendingTransactionsManager.{AddOrOverrideTransaction, PendingTransactionsResponse}
18-
import io.iohk.ethereum.utils.{BlockchainConfig, TxPoolConfig}
17+
import io.iohk.ethereum.utils.{BlockchainConfig, Logger, TxPoolConfig}
1918
import io.iohk.ethereum.rlp
2019
import io.iohk.ethereum.rlp.RLPImplicits._
2120
import io.iohk.ethereum.rlp.RLPImplicitConversions._
21+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
2222
import monix.eval.Task
2323

2424
import scala.util.Try
@@ -72,7 +72,7 @@ class PersonalService(
7272
appStateStorage: AppStateStorage,
7373
blockchainConfig: BlockchainConfig,
7474
txPoolConfig: TxPoolConfig
75-
) {
75+
) extends Logger {
7676

7777
private val unlockedWallets: ExpiringMap[Address, Wallet] = ExpiringMap.empty(Duration.ofSeconds(defaultUnlockTime))
7878

@@ -212,6 +212,7 @@ class PersonalService(
212212
} else {
213213
wallet.signTx(tx, None)
214214
}
215+
log.debug("Trying to add personal transaction: {}", stx.tx.hash.toHex)
215216

216217
txPool ! AddOrOverrideTransaction(stx.tx)
217218

src/main/scala/io/iohk/ethereum/ledger/BlockPreparator.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.iohk.ethereum.domain._
77
import io.iohk.ethereum.ledger.BlockExecutionError.{StateBeforeFailure, TxsExecutionError}
88
import io.iohk.ethereum.ledger.Ledger._
99
import io.iohk.ethereum.ledger.BlockPreparator._
10+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
1011
import io.iohk.ethereum.utils.{BlockchainConfig, Config, Logger}
1112
import io.iohk.ethereum.vm.{PC => _, _}
1213

@@ -237,7 +238,7 @@ class BlockPreparator(
237238
blockHeader: BlockHeader,
238239
world: InMemoryWorldStateProxy
239240
): TxResult = {
240-
log.debug(s"Transaction ${stx.hashAsHexString} execution start")
241+
log.debug(s"Transaction ${stx.hash.toHex} execution start")
241242
val gasPrice = UInt256(stx.tx.gasPrice)
242243
val gasLimit = stx.tx.gasLimit
243244

@@ -266,7 +267,7 @@ class BlockPreparator(
266267

267268
val world2 = (deleteAccountsFn andThen deleteTouchedAccountsFn andThen persistStateFn)(worldAfterPayments)
268269

269-
log.debug(s"""Transaction ${stx.hashAsHexString} execution end. Summary:
270+
log.debug(s"""Transaction ${stx.hash.toHex} execution end. Summary:
270271
| - Error: ${result.error}.
271272
| - Total Gas to Refund: $totalGasToRefund
272273
| - Execution gas paid to miner: $executionGasToPayToMiner""".stripMargin)
@@ -339,7 +340,7 @@ class BlockPreparator(
339340
logs = logs
340341
)
341342

342-
log.debug(s"Receipt generated for tx ${stx.hashAsHexString}, $receipt")
343+
log.debug(s"Receipt generated for tx ${stx.hash.toHex}, $receipt")
343344

344345
executeTransactions(otherStxs, newWorld, blockHeader, receipt.cumulativeGasUsed, acumReceipts :+ receipt)
345346
case Left(error) =>
@@ -361,7 +362,7 @@ class BlockPreparator(
361362

362363
result match {
363364
case Left(TxsExecutionError(stx, StateBeforeFailure(worldState, gas, receipts), reason)) =>
364-
log.debug(s"failure while preparing block because of $reason in transaction with hash ${stx.hashAsHexString}")
365+
log.debug(s"failure while preparing block because of $reason in transaction with hash ${stx.hash.toHex}")
365366
val txIndex = signedTransactions.indexWhere(tx => tx.hash == stx.hash)
366367
executePreparedTransactions(
367368
signedTransactions.drop(txIndex + 1),

src/main/scala/io/iohk/ethereum/network/p2p/messages/CommonMessages.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.iohk.ethereum.network.p2p.{Message, MessageSerializableImplicit}
77
import io.iohk.ethereum.rlp.RLPImplicitConversions._
88
import io.iohk.ethereum.rlp.RLPImplicits._
99
import io.iohk.ethereum.rlp._
10+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
1011
import io.iohk.ethereum.utils.Config
1112
import org.bouncycastle.util.encoders.Hex
1213

@@ -204,6 +205,6 @@ object CommonMessages {
204205
case class SignedTransactions(txs: Seq[SignedTransaction]) extends Message {
205206
override def code: Int = Codes.SignedTransactionsCode
206207
override def toShortString: String =
207-
s"SignedTransactions { txs: ${txs.map(_.hashAsHexString)} }"
208+
s"SignedTransactions { txs: ${txs.map(_.hash.toHex)} }"
208209
}
209210
}

src/main/scala/io/iohk/ethereum/transactions/PendingTransactionsManager.scala

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.iohk.ethereum.transactions
22

3-
import akka.actor.{Actor, ActorRef, Props}
3+
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
44
import akka.util.{ByteString, Timeout}
55
import com.google.common.cache.{Cache, CacheBuilder, RemovalNotification}
66
import io.iohk.ethereum.domain.{SignedTransaction, SignedTransactionWithSender}
@@ -10,7 +10,9 @@ import io.iohk.ethereum.network.PeerManagerActor.Peers
1010
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions
1111
import io.iohk.ethereum.network.{EtcPeerManagerActor, Peer, PeerId, PeerManagerActor}
1212
import io.iohk.ethereum.transactions.SignedTransactionsFilterActor.ProperSignedTransactions
13+
import io.iohk.ethereum.utils.ByteStringUtils.ByteStringOps
1314
import io.iohk.ethereum.utils.TxPoolConfig
15+
1416
import scala.jdk.CollectionConverters._
1517
import scala.concurrent.ExecutionContext.Implicits.global
1618
import scala.concurrent.duration._
@@ -52,7 +54,8 @@ class PendingTransactionsManager(
5254
etcPeerManager: ActorRef,
5355
peerEventBus: ActorRef
5456
) extends Actor
55-
with MetricsContainer {
57+
with MetricsContainer
58+
with ActorLogging {
5659

5760
import PendingTransactionsManager._
5861
import akka.pattern.ask
@@ -77,16 +80,17 @@ class PendingTransactionsManager(
7780
.maximumSize(txPoolConfig.txPoolSize)
7881
.removalListener((notification: RemovalNotification[ByteString, PendingTransaction]) =>
7982
if (notification.wasEvicted()) {
83+
log.debug("Evicting transaction: {} due to {}", notification.getKey.toHex, notification.getCause)
8084
knownTransactions = knownTransactions.filterNot(_._1 == notification.getKey)
8185
}
8286
)
8387
.build()
8488

85-
implicit val timeout = Timeout(3.seconds)
89+
implicit val timeout: Timeout = Timeout(3.seconds)
8690

8791
peerEventBus ! Subscribe(SubscriptionClassifier.PeerHandshaked)
8892

89-
val transactionFilter = context.actorOf(SignedTransactionsFilterActor.props(context.self, peerEventBus))
93+
val transactionFilter: ActorRef = context.actorOf(SignedTransactionsFilterActor.props(context.self, peerEventBus))
9094

9195
// scalastyle:off method.length
9296
override def receive: Receive = {
@@ -101,6 +105,7 @@ class PendingTransactionsManager(
101105

102106
case AddTransactions(signedTransactions) =>
103107
pendingTransactions.cleanUp()
108+
log.debug("Adding transactions: {}", signedTransactions.map(_.tx.hash.toHex))
104109
val stxs = pendingTransactions.asMap().values().asScala.map(_.stx).toSet
105110
val transactionsToAdd = signedTransactions.diff(stxs)
106111
if (transactionsToAdd.nonEmpty) {
@@ -115,6 +120,7 @@ class PendingTransactionsManager(
115120

116121
case AddOrOverrideTransaction(newStx) =>
117122
pendingTransactions.cleanUp()
123+
log.debug("Overriding transaction: {}", newStx.hash.toHex)
118124
// Only validated transactions are added this way, it is safe to call get
119125
val newStxSender = SignedTransaction.getSender(newStx).get
120126
val obsoleteTxs = pendingTransactions
@@ -135,6 +141,11 @@ class PendingTransactionsManager(
135141

136142
case NotifyPeers(signedTransactions, peers) =>
137143
pendingTransactions.cleanUp()
144+
log.debug(
145+
"Notifying peers {} about transactions {}",
146+
peers.map(_.nodeId.map(_.toHex)),
147+
signedTransactions.map(_.tx.hash.toHex)
148+
)
138149
val pendingTxMap = pendingTransactions.asMap()
139150
val stillPending = signedTransactions
140151
.filter(stx => pendingTxMap.containsKey(stx.tx.hash)) // signed transactions that are still pending
@@ -153,13 +164,15 @@ class PendingTransactionsManager(
153164

154165
case RemoveTransactions(signedTransactions) =>
155166
pendingTransactions.invalidateAll(signedTransactions.map(_.hash).asJava)
167+
log.debug("Removing transactions: {}", signedTransactions.map(_.hash.toHex))
156168
knownTransactions = knownTransactions -- signedTransactions.map(_.hash)
157169

158170
case ProperSignedTransactions(transactions, peerId) =>
159171
self ! AddTransactions(transactions)
160172
transactions.foreach(stx => setTxKnown(stx.tx, peerId))
161173

162174
case ClearPendingTransactions =>
175+
log.debug("Dropping all cached transactions")
163176
pendingTransactions.invalidateAll()
164177
}
165178

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ class RegularSyncSpec
295295
"fetching state node" should {
296296
abstract class MissingStateNodeFixture(system: ActorSystem) extends Fixture(system) {
297297
val failingBlock: Block = testBlocksChunked.head.head
298-
ledger.setImportResult(failingBlock, Task.now(BlockImportFailedDueToMissingNode(new MissingNodeException(failingBlock.hash))))
298+
ledger.setImportResult(
299+
failingBlock,
300+
Task.now(BlockImportFailedDueToMissingNode(new MissingNodeException(failingBlock.hash)))
301+
)
299302
}
300303

301304
"blacklist peer which returns empty response" in sync(new MissingStateNodeFixture(testSystem) {

0 commit comments

Comments
 (0)