Use java.time.Instant to represent timestamps in EclairApi (#1118)

This commit is contained in:
rorp 2020-02-13 04:47:20 -08:00 committed by GitHub
parent 59ec73269c
commit 25dae68238
4 changed files with 104 additions and 40 deletions

View File

@ -40,8 +40,8 @@ object PaymentLog {
case Some(e) =>
e match {
case PaymentReceived(_, parts) =>
parts.maxBy(_.timestamp).timestamp.toMillis
case PaymentFailed(_, _, _, timestamp) => timestamp.toMillis
parts.maxBy(_.timestamp).timestamp.toEpochMilli
case PaymentFailed(_, _, _, timestamp) => timestamp.toEpochMilli
case _: WebSocketEvent =>
throw new RuntimeException("Can't extract a timestamp")
}

View File

@ -783,7 +783,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
it should "be able to generate a payment invoice and then check that invoice" in {
val amt = 1000.msats
val description = "bitcoin-s test case"
val expiry = (System.currentTimeMillis() / 1000).seconds
val expiry = 10.seconds
val invoiceF = clientF.flatMap(
_.createInvoice(description = description,
@ -797,7 +797,7 @@ class EclairRpcClientTest extends BitcoinSAsyncTest {
paymentRequestF.map { paymentRequest =>
val i = LnInvoice.fromString(paymentRequest.serialized).get
assert(i.amount.get.toMSat == amt)
assert(paymentRequest.timestamp == expiry)
assert(paymentRequest.expiry == expiry)
}
}

View File

@ -1,6 +1,7 @@
package org.bitcoins.eclair.rpc.api
import java.net.InetSocketAddress
import java.time.Instant
import java.util.UUID
import org.bitcoins.core.crypto.{
@ -84,7 +85,7 @@ case class OpenChannelInfo(
case class NodeInfo(
signature: ECDigitalSignature,
features: String,
timestamp: Long,
timestamp: Instant,
nodeId: NodeId,
rgbColor: String,
alias: String,
@ -104,7 +105,7 @@ case class NetworkFeesResult(
txId: DoubleSha256DigestBE,
fee: Satoshis,
txType: String,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
)
case class ChannelStats(
@ -132,7 +133,7 @@ object ReceivedPayment {
case class Part(
amount: MilliSatoshis,
fromChannelId: FundedChannelId,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
)
}
@ -142,7 +143,7 @@ case class RelayedPayment(
paymentHash: Sha256Digest,
fromChannelId: FundedChannelId,
toChannelId: FundedChannelId,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
)
case class SentPayment(
@ -160,7 +161,7 @@ object SentPayment {
amount: MilliSatoshis,
feesPaid: MilliSatoshis,
toChannelId: FundedChannelId,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
)
}
@ -168,7 +169,7 @@ case class ChannelUpdate(
signature: ECDigitalSignature,
chainHash: DoubleSha256Digest,
shortChannelId: ShortChannelId,
timestamp: Long, //seconds
timestamp: Instant, //seconds
messageFlags: Int,
channelFlags: Int,
cltvExpiryDelta: Int,
@ -193,7 +194,7 @@ case class ChannelResult(
case class InvoiceResult(
prefix: LnHumanReadablePart,
timestamp: FiniteDuration, //seconds
timestamp: Instant, //seconds
nodeId: NodeId,
serialized: String,
description: String,
@ -208,7 +209,7 @@ case class SendToRouteResult(paymentId: PaymentId, parentId: PaymentId)
case class PaymentRequest(
prefix: LnHumanReadablePart,
timestamp: FiniteDuration, //seconds
timestamp: Instant, //seconds
nodeId: NodeId,
serialized: String,
description: String,
@ -242,14 +243,14 @@ case class OutgoingPayment(
amount: MilliSatoshis,
recipientAmount: MilliSatoshis,
recipientNodeId: NodeId,
createdAt: FiniteDuration, //milliseconds
createdAt: Instant, //milliseconds
paymentRequest: Option[PaymentRequest],
status: OutgoingPaymentStatus)
case class IncomingPayment(
paymentRequest: PaymentRequest,
paymentPreimage: PaymentPreimage,
createdAt: FiniteDuration, //milliseconds
createdAt: Instant, //milliseconds
status: IncomingPaymentStatus)
sealed trait IncomingPaymentStatus
@ -260,7 +261,7 @@ object IncomingPaymentStatus {
case object Expired extends IncomingPaymentStatus
case class Received(amount: MilliSatoshis, receivedAt: Long //milliseconds
case class Received(amount: MilliSatoshis, receivedAt: Instant //milliseconds
) extends IncomingPaymentStatus
}
@ -274,7 +275,7 @@ object OutgoingPaymentStatus {
paymentPreimage: PaymentPreimage,
feesPaid: MilliSatoshis,
route: Seq[Hop],
completedAt: FiniteDuration //milliseconds
completedAt: Instant //milliseconds
) extends OutgoingPaymentStatus
case class Failed(failures: Seq[PaymentFailure]) extends OutgoingPaymentStatus
@ -306,10 +307,9 @@ object WebSocketEvent {
paymentHash: Sha256Digest,
fromChannelId: FundedChannelId,
toChannelId: FundedChannelId,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
) extends WebSocketEvent
// {"type":"payment-received","paymentHash":"e1367ac5f913708f9ecc754c49477db3e7de404de7e921cab2dfe489227e07a7","parts":[{"amount":1000,"fromChannelId":"f59a3347ac6ef95ae4ad3e3777d137f80e02bf0a88d65b88f521676c7c713bf8","timestamp":1578080963457}]}
case class PaymentReceived(
paymentHash: Sha256Digest,
parts: Vector[PaymentReceived.Part]
@ -319,14 +319,14 @@ object WebSocketEvent {
case class Part(
amount: MilliSatoshis,
fromChannelId: FundedChannelId,
timestamp: FiniteDuration // milliseconds
timestamp: Instant // milliseconds
)
}
case class PaymentFailed(
id: PaymentId,
paymentHash: Sha256Digest,
failures: Vector[String],
timestamp: FiniteDuration // milliseconds
timestamp: Instant // milliseconds
) extends WebSocketEvent
case class PaymentSent(
@ -342,14 +342,14 @@ object WebSocketEvent {
amount: MilliSatoshis,
feesPaid: MilliSatoshis,
toChannelId: FundedChannelId,
timestamp: FiniteDuration // milliseconds
timestamp: Instant // milliseconds
)
}
case class PaymentSettlingOnchain(
amount: MilliSatoshis,
paymentHash: Sha256Digest,
timestamp: FiniteDuration //milliseconds
timestamp: Instant //milliseconds
) extends WebSocketEvent
}

View File

@ -1,9 +1,15 @@
package org.bitcoins.eclair.rpc.client
import java.net.InetSocketAddress
import java.time.Instant
import java.util.UUID
import org.bitcoins.core.crypto.{DoubleSha256DigestBE, Sha256Digest}
import org.bitcoins.core.crypto.{
DoubleSha256Digest,
DoubleSha256DigestBE,
ECDigitalSignature,
Sha256Digest
}
import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.protocol.ln._
import org.bitcoins.core.protocol.ln.channel.{ChannelState, FundedChannelId}
@ -112,7 +118,24 @@ object JsonReaders {
}
implicit val nodeInfoReads: Reads[NodeInfo] = {
Json.reads[NodeInfo]
Reads { jsValue =>
for {
signature <- (jsValue \ "signature").validate[ECDigitalSignature]
features <- (jsValue \ "features").validate[String]
timestamp <- (jsValue \ "timestamp")
.validate[Instant](instantReadsSeconds)
nodeId <- (jsValue \ "nodeId").validate[NodeId]
rgbColor <- (jsValue \ "rgbColor").validate[String]
alias <- (jsValue \ "alias").validate[String]
addresses <- (jsValue \ "addresses").validate[Vector[InetSocketAddress]]
} yield NodeInfo(signature,
features,
timestamp,
nodeId,
rgbColor,
alias,
addresses)
}
}
implicit val paymentPreimageReads: Reads[PaymentPreimage] = {
@ -135,14 +158,15 @@ object JsonReaders {
Reads { jsValue =>
for {
prefix <- (jsValue \ "prefix").validate[LnHumanReadablePart]
timestamp <- (jsValue \ "timestamp").validate[Long]
timestamp <- (jsValue \ "timestamp")
.validate[Instant](instantReadsSeconds)
nodeId <- (jsValue \ "nodeId").validate[NodeId]
serialized <- (jsValue \ "serialized").validate[String]
description <- (jsValue \ "description").validate[String]
paymentHash <- (jsValue \ "paymentHash").validate[Sha256Digest]
expiry <- (jsValue \ "expiry").validate[Long]
} yield InvoiceResult(prefix,
timestamp.seconds,
timestamp,
nodeId,
serialized,
description,
@ -200,7 +224,36 @@ object JsonReaders {
}
implicit val channelUpdateReads: Reads[ChannelUpdate] = {
Json.reads[ChannelUpdate]
Reads { jsValue =>
for {
signature <- (jsValue \ "signature").validate[ECDigitalSignature]
chainHash <- (jsValue \ "chainHash").validate[DoubleSha256Digest]
shortChannelId <- (jsValue \ "shortChannelId").validate[ShortChannelId]
timestamp <- (jsValue \ "timestamp")
.validate[Instant](instantReadsSeconds)
messageFlags <- (jsValue \ "messageFlags").validate[Int]
channelFlags <- (jsValue \ "channelFlags").validate[Int]
cltvExpiryDelta <- (jsValue \ "cltvExpiryDelta").validate[Int]
htlcMinimumMsat <- (jsValue \ "htlcMinimumMsat").validate[MilliSatoshis]
feeProportionalMillionths <- (jsValue \ "feeProportionalMillionths")
.validate[FeeProportionalMillionths]
htlcMaximumMsat <- (jsValue \ "htlcMaximumMsat")
.validateOpt[MilliSatoshis]
feeBaseMsat <- (jsValue \ "feeBaseMsat").validate[MilliSatoshis]
} yield ChannelUpdate(
signature,
chainHash,
shortChannelId,
timestamp,
messageFlags,
channelFlags,
cltvExpiryDelta,
htlcMinimumMsat,
feeProportionalMillionths,
htlcMaximumMsat,
feeBaseMsat
)
}
}
implicit val paymentIdReads: Reads[PaymentId] = Reads { jsValue =>
@ -221,6 +274,17 @@ object JsonReaders {
SerializerUtil.processJsNumberBigInt(_.longValue.seconds)(js)
}
val instantReadsMilliseconds: Reads[Instant] =
Reads { js =>
SerializerUtil.processJsNumberBigInt(x =>
Instant.ofEpochMilli(x.longValue))(js)
}
val instantReadsSeconds: Reads[Instant] = Reads { js =>
SerializerUtil.processJsNumberBigInt(x =>
Instant.ofEpochSecond(x.longValue))(js)
}
implicit val paymentTypeReads: Reads[PaymentType] = Reads { jsValue =>
SerializerUtil.processJsString(PaymentType.fromString)(jsValue)
}
@ -236,7 +300,7 @@ object JsonReaders {
feesPaid <- (js \ "feesPaid").validate[MilliSatoshis]
route <- (js \ "route").validate[Seq[Hop]]
completed <- (js \ "completedAt")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield OutgoingPaymentStatus.Succeeded(paymentPreimage = preimage,
feesPaid = feesPaid,
route = route,
@ -289,7 +353,7 @@ object JsonReaders {
for {
prefix <- (js \ "prefix").validate[LnHumanReadablePart]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsSeconds)
.validate[Instant](instantReadsSeconds)
nodeId <- (js \ "nodeId").validate[NodeId]
serialized <- (js \ "serialized").validate[String]
description <- (js \ "serialized").validate[String]
@ -318,7 +382,7 @@ object JsonReaders {
recipientAmount <- (js \ "recipientAmount").validate[MilliSatoshis]
recipientNodeId <- (js \ "recipientNodeId").validate[NodeId]
createdAt <- (js \ "createdAt")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
paymentRequest <- (js \ "paymentRequest").validateOpt[PaymentRequest]
status <- (js \ "status").validate[OutgoingPaymentStatus]
} yield OutgoingPayment(id,
@ -340,7 +404,7 @@ object JsonReaders {
paymentRequest <- (js \ "paymentRequest").validate[PaymentRequest]
paymentPreimage <- (js \ "paymentPreimage").validate[PaymentPreimage]
createdAt <- (js \ "createdAt")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
status <- (js \ "status").validate[IncomingPaymentStatus]
} yield IncomingPayment(paymentRequest,
paymentPreimage,
@ -385,7 +449,7 @@ object JsonReaders {
amount <- (js \ "amount").validate[MilliSatoshis]
fromChannelId <- (js \ "fromChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield ReceivedPayment.Part(amount, fromChannelId, timestamp)
}
@ -399,7 +463,7 @@ object JsonReaders {
feesPaid <- (js \ "feesPaid").validate[MilliSatoshis]
toChannelId <- (js \ "toChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield SentPayment.Part(id, amount, feesPaid, toChannelId, timestamp)
}
@ -413,7 +477,7 @@ object JsonReaders {
fromChannelId <- (js \ "fromChannelId").validate[FundedChannelId]
toChannelId <- (js \ "toChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield RelayedPayment(amountIn,
amountOut,
paymentHash,
@ -431,7 +495,7 @@ object JsonReaders {
fee <- (js \ "fee").validate[Satoshis]
txType <- (js \ "txType").validate[String]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield NetworkFeesResult(remoteNodeId,
channelId,
txId,
@ -455,7 +519,7 @@ object JsonReaders {
fromChannelId <- (js \ "fromChannelId").validate[FundedChannelId]
toChannelId <- (js \ "toChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield WebSocketEvent.PaymentRelayed(amountIn,
amountOut,
paymentHash,
@ -470,7 +534,7 @@ object JsonReaders {
amount <- (js \ "amount").validate[MilliSatoshis]
fromChannelId <- (js \ "fromChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield WebSocketEvent.PaymentReceived.Part(amount,
fromChannelId,
timestamp)
@ -492,7 +556,7 @@ object JsonReaders {
paymentHash <- (js \ "paymentHash").validate[Sha256Digest]
failures <- (js \ "failures").validate[Vector[String]]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield WebSocketEvent.PaymentFailed(id, paymentHash, failures, timestamp)
}
@ -504,7 +568,7 @@ object JsonReaders {
feesPaid <- (js \ "feesPaid").validate[MilliSatoshis]
toChannelId <- (js \ "toChannelId").validate[FundedChannelId]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield WebSocketEvent.PaymentSent.Part(id,
amount,
feesPaid,
@ -532,7 +596,7 @@ object JsonReaders {
amount <- (js \ "amount").validate[MilliSatoshis]
paymentHash <- (js \ "paymentHash").validate[Sha256Digest]
timestamp <- (js \ "timestamp")
.validate[FiniteDuration](finiteDurationReadsMilliseconds)
.validate[Instant](instantReadsMilliseconds)
} yield WebSocketEvent.PaymentSettlingOnchain(amount,
paymentHash,
timestamp)