Skip to content

Commit

Permalink
define an implicit getPrettySimpleName
Browse files Browse the repository at this point in the history
This is most consistent with Java API, it adds
`obj.getClass.getPrettySimpleName` to the existing
`obj.getClass.get*Name`.

Instead we could go with a nicer `obj.prettyClassName`.
  • Loading branch information
pm47 committed Jan 27, 2022
1 parent bb10ef7 commit de8bc92
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 62 deletions.
10 changes: 5 additions & 5 deletions eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2143,15 +2143,15 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder, remo
}

private def handleAddHtlcCommandError(c: CMD_ADD_HTLC, cause: ChannelException, channelUpdate: Option[ChannelUpdate]) = {
log.warning(s"${cause.getMessage} while processing cmd=${c.getClass.getSimpleName} in state=$stateName")
log.warning(s"${cause.getMessage} while processing cmd=${c.getClass.getPrettySimpleName} in state=$stateName")
val replyTo = if (c.replyTo == ActorRef.noSender) sender() else c.replyTo
replyTo ! RES_ADD_FAILED(c, cause, channelUpdate)
context.system.eventStream.publish(ChannelErrorOccurred(self, stateData.channelId, remoteNodeId, stateData, LocalError(cause), isFatal = false))
stay()
}

private def handleCommandError(cause: ChannelException, c: channel.Command) = {
log.warning(s"${cause.getMessage} while processing cmd=${c.getClass.getSimpleName} in state=$stateName")
log.warning(s"${cause.getMessage} while processing cmd=${c.getClass.getPrettySimpleName} in state=$stateName")
val replyTo_opt = c match {
case hasOptionalReplyTo: HasOptionalReplyToCommand => hasOptionalReplyTo.replyTo_opt
case hasReplyTo: HasReplyToCommand => if (hasReplyTo.replyTo == ActorRef.noSender) Some(sender()) else Some(hasReplyTo.replyTo)
Expand Down Expand Up @@ -2335,9 +2335,9 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder, remo
log.warning(s"force-closing channel at user request")
case _ if msg.exists(_.isInstanceOf[OpenChannel]) || msg.exists(_.isInstanceOf[AcceptChannel]) =>
// invalid remote channel parameters are logged as warning
log.warning(s"${cause.getMessage} while processing msg=${msg.getOrElse("n/a").getClass.getSimpleName} in state=$stateName")
log.warning(s"${cause.getMessage} while processing msg=${msg.getOrElse("n/a").getClass.getPrettySimpleName} in state=$stateName")
case _: ChannelException =>
log.error(s"${cause.getMessage} while processing msg=${msg.getOrElse("n/a").getClass.getSimpleName} in state=$stateName")
log.error(s"${cause.getMessage} while processing msg=${msg.getOrElse("n/a").getClass.getPrettySimpleName} in state=$stateName")
case _ =>
// unhandled error: we dump the channel data, and print the stack trace
log.error(cause, s"msg=${msg.getOrElse("n/a")} stateData=$stateData:")
Expand Down Expand Up @@ -2724,7 +2724,7 @@ class Channel(val nodeParams: NodeParams, val wallet: OnChainChannelFunder, remo
override val supervisorStrategy: OneForOneStrategy = OneForOneStrategy(loggingEnabled = true) { case _ => SupervisorStrategy.Escalate }

override def aroundReceive(receive: Actor.Receive, msg: Any): Unit = {
KamonExt.time(ProcessMessage.withTag("MessageType", msg.getClass.getSimpleName)) {
KamonExt.time(ProcessMessage.withTag("MessageType", msg.getClass.getPrettySimpleName)) {
super.aroundReceive(receive, msg)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import fr.acinq.eclair.crypto.ChaCha20Poly1305.ChaCha20Poly1305Error
import fr.acinq.eclair.crypto.Noise._
import fr.acinq.eclair.remote.EclairInternalsSerializer.RemoteTypes
import fr.acinq.eclair.wire.protocol.{AnnouncementSignatures, RoutingMessage}
import fr.acinq.eclair.{Diagnostics, FSMDiagnosticActorLogging, Logs, getSimpleClassName}
import fr.acinq.eclair.{Diagnostics, FSMDiagnosticActorLogging, Logs, PrettySimpleClassName}
import scodec.bits.ByteVector
import scodec.{Attempt, Codec, DecodeResult}

Expand Down Expand Up @@ -263,7 +263,7 @@ class TransportHandler[T: ClassTag](keyPair: KeyPair, rs: Option[ByteVector], co
case Event(msg, d) =>
d match {
case n: NormalData[T] => log.warning(s"unhandled message $msg in state normal unackedSent=${n.unackedSent.size} unackedReceived=${n.unackedReceived.size} sendBuffer.lowPriority=${n.sendBuffer.lowPriority.size} sendBuffer.normalPriority=${n.sendBuffer.normalPriority.size}")
case _ => log.warning(s"unhandled message $msg in state ${d.getClass.getSimpleName}")
case _ => log.warning(s"unhandled message $msg in state ${d.getClass.getPrettySimpleName}")
}
stay()
}
Expand All @@ -277,7 +277,7 @@ class TransportHandler[T: ClassTag](keyPair: KeyPair, rs: Option[ByteVector], co
stateData match {
case normal: NormalData[_] =>
// NB: we deduplicate on the class name: each class will appear once but there may be many instances (less verbose and gives debug hints)
log.info("stopping (unackedReceived={} unackedSent={})", normal.unackedReceived.keys.map(getSimpleClassName).toSet.mkString(","), normal.unackedSent.map(getSimpleClassName))
log.info("stopping (unackedReceived={} unackedSent={})", normal.unackedReceived.keys.map(_.getClass.getPrettySimpleName).toSet.mkString(","), normal.unackedSent.map(_.getClass.getPrettySimpleName))
case _ =>
log.info("stopping")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class PgAuditDb(implicit ds: DataSource) extends AuditDb with Logging {
inTransaction { pg =>
using(pg.prepareStatement("INSERT INTO audit.channel_errors VALUES (?, ?, ?, ?, ?, ?)")) { statement =>
val (errorName, errorMessage) = e.error match {
case LocalError(t) => (t.getClass.getSimpleName, t.getMessage)
case LocalError(t) => (t.getClass.getPrettySimpleName, t.getMessage)
case RemoteError(error) => ("remote", error.toAscii)
}
statement.setString(1, e.channelId.toHex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class PgPaymentsDb(implicit ds: DataSource, lock: PgLock) extends PaymentsDb wit
}

override def addOutgoingPayment(sent: OutgoingPayment): Unit = withMetrics("payments/add-outgoing", DbBackends.Postgres) {
require(sent.status == OutgoingPaymentStatus.Pending, s"outgoing payment isn't pending (${sent.status.getClass.getSimpleName})")
require(sent.status == OutgoingPaymentStatus.Pending, s"outgoing payment isn't pending (${sent.status.getClass.getPrettySimpleName})")
withLock { pg =>
using(pg.prepareStatement("INSERT INTO payments.sent (id, parent_id, external_id, payment_hash, payment_type, amount_msat, recipient_amount_msat, recipient_node_id, created_at, payment_request) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { statement =>
statement.setString(1, sent.id.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class SqliteAuditDb(sqlite: Connection) extends AuditDb with Logging {
override def add(e: ChannelErrorOccurred): Unit = withMetrics("audit/add-channel-error", DbBackends.Sqlite) {
using(sqlite.prepareStatement("INSERT INTO channel_errors VALUES (?, ?, ?, ?, ?, ?)")) { statement =>
val (errorName, errorMessage) = e.error match {
case LocalError(t) => (t.getClass.getSimpleName, t.getMessage)
case LocalError(t) => (t.getClass.getPrettySimpleName, t.getMessage)
case RemoteError(error) => ("remote", error.toAscii)
}
statement.setBytes(1, e.channelId.toArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SqlitePaymentsDb(sqlite: Connection) extends PaymentsDb with Logging {
}

override def addOutgoingPayment(sent: OutgoingPayment): Unit = withMetrics("payments/add-outgoing", DbBackends.Sqlite) {
require(sent.status == OutgoingPaymentStatus.Pending, s"outgoing payment isn't pending (${sent.status.getClass.getSimpleName})")
require(sent.status == OutgoingPaymentStatus.Pending, s"outgoing payment isn't pending (${sent.status.getClass.getPrettySimpleName})")
using(sqlite.prepareStatement("INSERT INTO sent_payments (id, parent_id, external_id, payment_hash, payment_type, amount_msat, recipient_amount_msat, recipient_node_id, created_at, payment_request) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { statement =>
statement.setString(1, sent.id.toString)
statement.setString(2, sent.parentId.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ object PaymentFailedSummarySerializer extends ConvertClassSerializer[PaymentFail

object ThrowableSerializer extends MinimalSerializer({
case t: Throwable if t.getMessage != null => JString(t.getMessage)
case t: Throwable => JString(t.getClass.getSimpleName)
case t: Throwable => JString(t.getClass.getPrettySimpleName)
})

object FailureMessageSerializer extends MinimalSerializer({
Expand Down Expand Up @@ -400,7 +400,7 @@ object ChannelEventSerializer extends MinimalSerializer({
case e: ChannelClosed => JObject(
JField("type", JString("channel-closed")),
JField("channelId", JString(e.channelId.toHex)),
JField("closingType", JString(e.closingType.getClass.getSimpleName))
JField("closingType", JString(e.closingType.getClass.getPrettySimpleName))
)
})

Expand Down
8 changes: 4 additions & 4 deletions eclair-core/src/main/scala/fr/acinq/eclair/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ package object eclair {
// @formatter:on
}

/**
* Apparently .getClass.getSimpleName can crash java 8 with a "Malformed class name" error
*/
def getSimpleClassName(o: Any): String = o.getClass.getName.split("\\$").last
implicit class PrettySimpleClassName[T](private val o: Class[T]) extends AnyVal {
/** This removes trailing $ from `case object` class names */
def getPrettySimpleName: String = o.getSimpleName.replace("$", "")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ object Monitoring {

def apply(cmdFail: CMD_FAIL_HTLC): String = cmdFail.reason match {
case Left(_) => Remote
case Right(f) => f.getClass.getSimpleName
case Right(f) => f.getClass.getPrettySimpleName
}

def apply(pf: PaymentFailure): String = pf match {
case LocalFailure(_, _, t) => t.getClass.getSimpleName
case RemoteFailure(_, _, e) => e.failureMessage.getClass.getSimpleName
case LocalFailure(_, _, t) => t.getClass.getPrettySimpleName
case RemoteFailure(_, _, e) => e.failureMessage.getClass.getPrettySimpleName
case UnreadableRemoteFailure(_, _) => "UnreadableRemoteFailure"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class MultiPartHandler(nodeParams: NodeParams, register: ActorRef, db: IncomingP

case MultiPartPaymentFSM.MultiPartPaymentFailed(paymentHash, failure, parts) if doHandle(paymentHash) =>
Logs.withMdc(log)(Logs.mdc(paymentHash_opt = Some(paymentHash))) {
Metrics.PaymentFailed.withTag(Tags.Direction, Tags.Directions.Received).withTag(Tags.Failure, failure.getClass.getSimpleName).increment()
Metrics.PaymentFailed.withTag(Tags.Direction, Tags.Directions.Received).withTag(Tags.Failure, failure.getClass.getPrettySimpleName).increment()
log.warning("payment with paidAmount={} failed ({})", parts.map(_.amount).sum, failure)
pendingPayments.get(paymentHash).foreach { case (_, handler: ActorRef) => handler ! PoisonPill }
parts.collect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MultiPartPaymentFSM(nodeParams: NodeParams, paymentHash: ByteVector32, tot
replyTo ! MultiPartPaymentSucceeded(paymentHash, parts)
Metrics.ReceivedPaymentDuration.withTag(Tags.Success, value = true).record((TimestampMilli.now() - start).toMillis, TimeUnit.MILLISECONDS)
case d =>
log.error("unexpected payment success data {}", d.getClass.getSimpleName)
log.error("unexpected payment success data {}", d.getClass.getPrettySimpleName)
}
case _ -> PAYMENT_FAILED =>
nextStateData match {
Expand All @@ -108,7 +108,7 @@ class MultiPartPaymentFSM(nodeParams: NodeParams, paymentHash: ByteVector32, tot
replyTo ! MultiPartPaymentFailed(paymentHash, failure, parts)
Metrics.ReceivedPaymentDuration.withTag(Tags.Success, value = false).record((TimestampMilli.now() - start).toMillis, TimeUnit.MILLISECONDS)
case d =>
log.error("unexpected payment failure data {}", d.getClass.getSimpleName)
log.error("unexpected payment failure data {}", d.getClass.getPrettySimpleName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PaymentHandler(nodeParams: NodeParams, register: ActorRef) extends Actor w

private def addReceiveHandler(handle: Receive): Receive = {
case handler: ReceiveHandler =>
log.info(s"registering handler of type=${handler.getClass.getSimpleName}")
log.info(s"registering handler of type=${handler.getClass.getPrettySimpleName}")
// NB: the last handler that was added will be the first called
context become normal(handler.handle(context, log) orElse handle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ChannelRelay private(nodeParams: NodeParams,
safeSendAndStop(o.add.channelId, cmdFail)

case WrappedAddResponse(addFailed@RES_ADD_FAILED(CMD_ADD_HTLC(_, _, _, _, _, _: Origin.ChannelRelayedHot, _), _, _)) =>
context.log.info("attempt failed with reason={}", addFailed.t.getClass.getSimpleName)
context.log.info("attempt failed with reason={}", addFailed.t.getClass.getPrettySimpleName)
context.self ! DoRelay
relay(previousFailures :+ PreviouslyTried(selectedShortChannelId, addFailed))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class NodeRelay private(nodeParams: NodeParams,
receiving(htlcs :+ add, nextPayload, nextPacket, handler)
case WrappedMultiPartPaymentFailed(MultiPartPaymentFSM.MultiPartPaymentFailed(_, failure, parts)) =>
context.log.warn("could not complete incoming multi-part payment (parts={} paidAmount={} failure={})", parts.size, parts.map(_.amount).sum, failure)
Metrics.recordPaymentRelayFailed(failure.getClass.getSimpleName, Tags.RelayType.Trampoline)
Metrics.recordPaymentRelayFailed(failure.getClass.getPrettySimpleName, Tags.RelayType.Trampoline)
parts.collect { case p: MultiPartPaymentFSM.HtlcPart => rejectHtlc(p.htlc.id, p.htlc.channelId, p.amount, Some(failure)) }
stopping()
case WrappedMultiPartPaymentSucceeded(MultiPartPaymentFSM.MultiPartPaymentSucceeded(_, parts)) =>
Expand Down Expand Up @@ -319,7 +319,7 @@ class NodeRelay private(nodeParams: NodeParams,
}

private def rejectPayment(upstream: Upstream.Trampoline, failure: Option[FailureMessage]): Unit = {
Metrics.recordPaymentRelayFailed(failure.map(_.getClass.getSimpleName).getOrElse("Unknown"), Tags.RelayType.Trampoline)
Metrics.recordPaymentRelayFailed(failure.map(_.getClass.getPrettySimpleName).getOrElse("Unknown"), Tags.RelayType.Trampoline)
upstream.adds.foreach(add => rejectHtlc(add.id, add.channelId, upstream.amountIn, failure))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PostRestartHtlcCleaner(nodeParams: NodeParams, register: ActorRef, initial
handleDownstreamFulfill(brokenHtlcs, o, htlc, fulfill.paymentPreimage)

case RES_ADD_SETTLED(o: Origin.Cold, htlc, fail: HtlcResult.Fail) =>
log.info("htlc failed downstream: ({},{},{})", htlc.channelId, htlc.id, fail.getClass.getSimpleName)
log.info("htlc failed downstream: ({},{},{})", htlc.channelId, htlc.id, fail.getClass.getPrettySimpleName)
handleDownstreamFailure(brokenHtlcs, o, htlc, fail)

case GetBrokenHtlcs => sender() ! brokenHtlcs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package fr.acinq.eclair.remote

import java.nio.ByteBuffer

import akka.serialization.{ByteBufferSerializer, SerializerWithStringManifest}
import scodec.Codec
import scodec.bits.BitVector

import java.nio.ByteBuffer

class ScodecSerializer[T <: AnyRef](override val identifier: Int, codec: Codec[T]) extends SerializerWithStringManifest with ByteBufferSerializer {

override def toBinary(o: AnyRef, buf: ByteBuffer): Unit = buf.put(toBinary(o))
Expand All @@ -33,7 +33,7 @@ class ScodecSerializer[T <: AnyRef](override val identifier: Int, codec: Codec[T
}

/** we don't rely on the manifest to provide backward compatibility, we will use dedicated codecs instead */
override def manifest(o: AnyRef): String = fr.acinq.eclair.getSimpleClassName(o)
override def manifest(o: AnyRef): String = o.getClass.getName

override def toBinary(o: AnyRef): Array[Byte] = codec.encode(o.asInstanceOf[T]).require.toByteArray

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package fr.acinq.eclair.router
import fr.acinq.bitcoin.{BtcDouble, MilliBtcDouble, SatoshiLong}
import fr.acinq.eclair.router.Router.GossipDecision
import fr.acinq.eclair.wire.protocol.ChannelUpdate
import fr.acinq.eclair.{MilliSatoshi, getSimpleClassName}
import fr.acinq.eclair.{MilliSatoshi, PrettySimpleClassName}
import kamon.Kamon
import kamon.metric.{Counter, MeasurementUnit}

Expand Down Expand Up @@ -64,7 +64,7 @@ object Monitoring {

def gossipResult(decision: GossipDecision): Counter = decision match {
case _: GossipDecision.Accepted => GossipResult.withTag("result", "accepted")
case rejected: GossipDecision.Rejected => GossipResult.withTag("result", "rejected").withTag("reason", getSimpleClassName(rejected))
case rejected: GossipDecision.Rejected => GossipResult.withTag("result", "rejected").withTag("reason", rejected.getClass.getPrettySimpleName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ object RouteCalculation {
ctx.sender() ! Status.Failure(failure)
case Failure(t) =>
val failure = if (isNeighborBalanceTooLow(d.graph, r)) BalanceTooLow else t
Metrics.FindRouteErrors.withTags(tags.withTag(Tags.Error, failure.getClass.getSimpleName)).increment()
Metrics.FindRouteErrors.withTags(tags.withTag(Tags.Error, failure.getClass.getPrettySimpleName)).increment()
ctx.sender() ! Status.Failure(failure)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ object LightningMessageCodecs {
discriminatorWithDefault(lightningMessageCodec, unknownMessageCodec.upcast)

val meteredLightningMessageCodec = Codec[LightningMessage](
(msg: LightningMessage) => KamonExt.time(Metrics.EncodeDuration.withTag(Tags.MessageType, msg.getClass.getSimpleName))(lightningMessageCodecWithFallback.encode(msg)),
(msg: LightningMessage) => KamonExt.time(Metrics.EncodeDuration.withTag(Tags.MessageType, msg.getClass.getPrettySimpleName))(lightningMessageCodecWithFallback.encode(msg)),
(bits: BitVector) => {
// this is a bit more involved, because we don't know beforehand what the type of the message will be
val begin = System.nanoTime()
val res = lightningMessageCodecWithFallback.decode(bits)
val end = System.nanoTime()
val messageType = res match {
case Attempt.Successful(decoded) => decoded.value.getClass.getSimpleName
case Attempt.Successful(decoded) => decoded.value.getClass.getPrettySimpleName
case Attempt.Failure(_) => "unknown"
}
Metrics.DecodeDuration.withTag(Tags.MessageType, messageType).record(end - begin)
Expand Down
18 changes: 6 additions & 12 deletions eclair-core/src/test/scala/fr/acinq/eclair/PackageSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,12 @@ class PackageSpec extends AnyFunSuite {
assert(ShortChannelId(Long.MaxValue) < ShortChannelId(Long.MaxValue + 1))
}

test("non-reg getSimpleName") {
/** We used to have this code due to crash issues on java 8 with a "Malformed class name" error */
def getSimpleClassName(o: Any): String = o.getClass.getName.split("\\$").last

// let's make sure that getSimpleClassName == getClass.getSimpleName even for nested classes and objects

List(
Foo(),
Foo.Bar,
Foo.Baz(42)
).foreach { o =>
assert(getSimpleClassName(o) == o.getClass.getSimpleName)
test("non-reg getPrettySimpleName") {
Map(
Foo() -> "Foo",
Foo.Bar -> "Bar",
Foo.Baz(42) -> "Baz"
).foreach { case (o, ref) => assert(o.getClass.getPrettySimpleName == ref)
}
}

Expand Down
2 changes: 1 addition & 1 deletion eclair-front/src/main/scala/fr/acinq/eclair/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object Boot extends App with Logging {
}

def onError(t: Throwable): Unit = {
val errorMsg = if (t.getMessage != null) t.getMessage else t.getClass.getSimpleName
val errorMsg = if (t.getMessage != null) t.getMessage else t.getClass.getPrettySimpleName
System.err.println(s"fatal error: $errorMsg")
logger.error(s"fatal error: $errorMsg", t)
sys.exit(1)
Expand Down
Loading

0 comments on commit de8bc92

Please sign in to comment.