Skip to content

Commit

Permalink
simplify features serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Oct 1, 2021
1 parent 62c9e41 commit 601fff1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import fr.acinq.eclair.router.Router.RouteResponse
import fr.acinq.eclair.transactions.DirectedHtlc
import fr.acinq.eclair.transactions.Transactions._
import fr.acinq.eclair.wire.protocol._
import fr.acinq.eclair.{CltvExpiry, CltvExpiryDelta, Features, MilliSatoshi, ShortChannelId, UInt64}
import fr.acinq.eclair.{CltvExpiry, CltvExpiryDelta, Feature, FeatureSupport, MilliSatoshi, ShortChannelId, UInt64, UnknownFeature}
import org.json4s
import org.json4s.JsonAST._
import org.json4s.jackson.Serialization
Expand Down Expand Up @@ -156,6 +156,12 @@ object PrivateKeySerializer extends MinimalSerializer({
case _: PrivateKey => JString("XXX")
})

object FeatureKeySerializer extends MinimalKeySerializer({ case f: Feature => f.rfcName })

object FeatureSupportSerializer extends MinimalSerializer({ case s: FeatureSupport => JString(s.toString) })

object UnknownFeatureSerializer extends MinimalSerializer({ case f: UnknownFeature => JInt(f.bitIndex) })

object ChannelConfigSerializer extends MinimalSerializer({
case x: ChannelConfig => JArray(x.options.toList.map(o => JString(o.name)))
})
Expand Down Expand Up @@ -275,7 +281,12 @@ object PaymentRequestSerializer extends MinimalSerializer({
val expiry = p.expiry.map(ex => JField("expiry", JLong(ex))).toSeq
val minFinalCltvExpiry = p.minFinalCltvExpiryDelta.map(mfce => JField("minFinalCltvExpiry", JInt(mfce.toInt))).toSeq
val amount = p.amount.map(msat => JField("amount", JLong(msat.toLong))).toSeq
val features = JField("features", JsonSerializers.featuresToJson(Features(p.features.bitmask)))
val features = JField("features", Extraction.decompose(p.features.features)(
DefaultFormats +
FeatureKeySerializer +
FeatureSupportSerializer +
UnknownFeatureSerializer
))
val routingInfo = JField("routingInfo", Extraction.decompose(p.routingInfo)(
DefaultFormats +
ByteVector32Serializer +
Expand All @@ -284,8 +295,7 @@ object PaymentRequestSerializer extends MinimalSerializer({
ShortChannelIdSerializer +
MilliSatoshiSerializer +
CltvExpiryDeltaSerializer
)
)
))
val fieldList = List(JField("prefix", JString(p.prefix)),
JField("timestamp", JLong(p.timestamp)),
JField("nodeId", JString(p.nodeId.toString())),
Expand All @@ -301,10 +311,6 @@ object PaymentRequestSerializer extends MinimalSerializer({
JObject(fieldList)
})

object FeaturesSerializer extends MinimalSerializer({
case features: Features => JsonSerializers.featuresToJson(features)
})

object JavaUUIDSerializer extends MinimalSerializer({
case id: UUID => JString(id.toString)
})
Expand Down Expand Up @@ -435,6 +441,9 @@ object JsonSerializers {
InetSocketAddressSerializer +
OutPointSerializer +
OutPointKeySerializer +
FeatureKeySerializer +
FeatureSupportSerializer +
UnknownFeatureSerializer +
ChannelConfigSerializer +
ChannelFeaturesSerializer +
ChannelOpenResponseSerializer +
Expand All @@ -449,15 +458,7 @@ object JsonSerializers {
DirectedHtlcSerializer +
PaymentRequestSerializer +
JavaUUIDSerializer +
FeaturesSerializer +
OriginSerializer +
GlobalBalanceSerializer

def featuresToJson(features: Features): JObject = JObject(
JField("activated", JObject(features.activated.map { case (feature, support) =>
feature.rfcName -> JString(support.toString)
}.toList)),
JField("unknown", JArray(features.unknown.map(u => JInt(u.bitIndex)).toList))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ class JsonSerializersSpec extends AnyFunSuite with Matchers {
JsonSerializers.serialization.write(inputInfo)(JsonSerializers.formats) shouldBe """{"outPoint":"9f0b9c0ce92c175ca4e78acfd13a718099c73818b6d3140cfe6f04ec052b5b34:42","amountSatoshis":456651}"""
}

test("Features serialization") {
val features = Features(
activated = Map(
Features.InitialRoutingSync -> FeatureSupport.Optional,
Features.PaymentSecret -> FeatureSupport.Mandatory,
Features.StaticRemoteKey -> FeatureSupport.Optional
),
unknown = Set(
UnknownFeature(457),
UnknownFeature(5000),
)
)

JsonSerializers.serialization.write(features)(JsonSerializers.formats) shouldBe """{"activated":{"initial_routing_sync":"optional","payment_secret":"mandatory","option_static_remotekey":"optional"},"unknown":[457,5000]}"""
}

test("Payment Request") {
val ref = "lnbcrt50n1p0fm9cdpp5al3wvsfkc6p7fxy89eu8gm4aww9mseu9syrcqtpa4mvx42qelkwqdq9v9ekgxqrrss9qypqsqsp5wl2t45v0hj4lgud0zjxcnjccd29ts0p2kh4vpw75vnhyyzyjtjtqarpvqg33asgh3z5ghfuvhvtf39xtnu9e7aqczpgxa9quwsxkd9rnwmx06pve9awgeewxqh90dqgrhzgsqc09ek6uejr93z8puafm6gsqgrk0hy"
val pr = PaymentRequest.read(ref)
Expand Down
Loading

0 comments on commit 601fff1

Please sign in to comment.