Skip to content

Commit

Permalink
Reorganize internal codecs (master) (#1732)
Browse files Browse the repository at this point in the history
The goal is to ensure maximum safety when migrating data.

To achieve this, we strictly segregate each codec version (to make sure
that we don't accidentally mix codec versions), while still letting
tests access each unitary codecs (which using inner `private classes`
would have prevented). Relevant tests only need to be moved to the
appropriate package.

The package structure is now:

```
wire
 |
 `-- internal
 |       |
 |       `-- channel
 |       |      |
 |       |      `-- version0
 |       |      |      |
 |       |      |      `-- ChannelCodecs0
 |       |      |
 |       |      `-- version1
 |       |      |      |
 |       |      |      `-- ChannelCodecs1
 |       |      |
 |       |      `-- ChannelCodecs
 |       |
 |       `-- CommandCodecs
 |
 `-- others
```

Co-authored-by: Bastien Teinturier <[email protected]>
  • Loading branch information
pm47 and t-bast authored Mar 19, 2021
1 parent 8dc64db commit 4bc2dec
Show file tree
Hide file tree
Showing 22 changed files with 1,033 additions and 950 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package fr.acinq.eclair.blockchain.electrum.db.sqlite

import java.sql.Connection

import fr.acinq.bitcoin.{BlockHeader, ByteVector32, Transaction}
import fr.acinq.eclair.blockchain.electrum.ElectrumClient.{GetMerkleResponse, TransactionHistoryItem}
import fr.acinq.eclair.blockchain.electrum.ElectrumWallet.PersistentData
import fr.acinq.eclair.blockchain.electrum.db.WalletDb
import fr.acinq.eclair.blockchain.electrum.{ElectrumClient, ElectrumWallet}
import fr.acinq.eclair.db.sqlite.SqliteUtils

import java.sql.Connection
import scala.collection.immutable.Queue

class SqliteWalletDb(sqlite: Connection) extends WalletDb {
Expand Down Expand Up @@ -135,7 +134,6 @@ class SqliteWalletDb(sqlite: Connection) extends WalletDb {

object SqliteWalletDb {

import fr.acinq.eclair.wire.ChannelCodecs._
import fr.acinq.eclair.wire.CommonCodecs._
import scodec.Codec
import scodec.bits.BitVector
Expand All @@ -152,8 +150,6 @@ object SqliteWalletDb {

def deserializeMerkleProof(bin: Array[Byte]): GetMerkleResponse = proofCodec.decode(BitVector(bin)).require.value

import fr.acinq.eclair.wire.LightningMessageCodecs._

val statusListCodec: Codec[List[(ByteVector32, String)]] = listOfN(uint16, bytes32 ~ cstring)

val statusCodec: Codec[Map[ByteVector32, String]] = Codec[Map[ByteVector32, String]](
Expand All @@ -168,6 +164,8 @@ object SqliteWalletDb {
(wire: BitVector) => heightsListCodec.decode(wire).map(_.map(_.toMap))
)

val txCodec: Codec[Transaction] = lengthDelimited(bytes.xmap(d => Transaction.read(d.toArray), d => Transaction.write(d)))

val transactionListCodec: Codec[List[(ByteVector32, Transaction)]] = listOfN(uint16, bytes32 ~ txCodec)

val transactionsCodec: Codec[Map[ByteVector32, Transaction]] = Codec[Map[ByteVector32, Transaction]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import fr.acinq.eclair.db.ChannelsDb
import fr.acinq.eclair.db.DbEventHandler.ChannelEvent
import fr.acinq.eclair.db.Monitoring.Metrics.withMetrics
import fr.acinq.eclair.db.pg.PgUtils.DatabaseLock
import fr.acinq.eclair.wire.ChannelCodecs.stateDataCodec
import fr.acinq.eclair.wire.internal.channel.ChannelCodecs.stateDataCodec
import grizzled.slf4j.Logging

import java.sql.Statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import fr.acinq.eclair.channel.{Command, HtlcSettlementCommand}
import fr.acinq.eclair.db.Monitoring.Metrics.withMetrics
import fr.acinq.eclair.db.PendingRelayDb
import fr.acinq.eclair.db.pg.PgUtils._
import fr.acinq.eclair.wire.CommandCodecs.cmdCodec
import fr.acinq.eclair.wire.internal.CommandCodecs.cmdCodec
import javax.sql.DataSource

import scala.collection.immutable.Queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import fr.acinq.eclair.db.ChannelsDb
import fr.acinq.eclair.db.DbEventHandler.ChannelEvent
import fr.acinq.eclair.db.Monitoring.Metrics.withMetrics
import fr.acinq.eclair.payment.{ChannelPaymentRelayed, PaymentEvent, PaymentReceived, PaymentRelayed, PaymentSent}
import fr.acinq.eclair.wire.ChannelCodecs.stateDataCodec
import fr.acinq.eclair.wire.internal.channel.ChannelCodecs.stateDataCodec
import grizzled.slf4j.Logging

import java.sql.{Connection, Statement}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import fr.acinq.bitcoin.ByteVector32
import fr.acinq.eclair.channel.{Command, HtlcSettlementCommand}
import fr.acinq.eclair.db.Monitoring.Metrics.withMetrics
import fr.acinq.eclair.db.PendingRelayDb
import fr.acinq.eclair.wire.CommandCodecs.cmdCodec
import fr.acinq.eclair.wire.internal.CommandCodecs.cmdCodec

import scala.collection.immutable.Queue

Expand Down
311 changes: 0 additions & 311 deletions eclair-core/src/main/scala/fr/acinq/eclair/wire/ChannelCodecs.scala

This file was deleted.

Loading

0 comments on commit 4bc2dec

Please sign in to comment.