-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic offer management without plugins
- Loading branch information
1 parent
2d94d22
commit 665ad0a
Showing
16 changed files
with
449 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
eclair-core/src/main/scala/fr/acinq/eclair/db/OffersDb.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.eclair.db | ||
|
||
import fr.acinq.bitcoin.scalacompat.ByteVector32 | ||
import fr.acinq.eclair.wire.protocol.OfferTypes.Offer | ||
|
||
case class OfferData(offer: Offer, pathId_opt: Option[ByteVector32]) | ||
|
||
/** | ||
* Database for offers fully managed by eclair, as opposed to offers managed by a plugin. | ||
*/ | ||
trait OffersDb { | ||
/** | ||
* Add an offer managed by eclair. | ||
* @param pathId_opt If the offer uses a blinded path, this is the corresponding pathId. | ||
*/ | ||
def addOffer(offer: Offer, pathId_opt: Option[ByteVector32]): Unit | ||
|
||
/** | ||
* Disable an offer. The offer is still stored but new invoice requests and new payment attempts for already emitted | ||
* invoices will be rejected. To reenable an offer, use `addOffer`. | ||
*/ | ||
def disableOffer(offer: Offer): Unit | ||
|
||
/** | ||
* List offers managed by eclair. | ||
* @param onlyActive Whether to return only active offers or also disabled ones. | ||
*/ | ||
def listOffers(onlyActive: Boolean): Seq[OfferData] | ||
} |
76 changes: 76 additions & 0 deletions
76
eclair-core/src/main/scala/fr/acinq/eclair/db/pg/PgOffersDb.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.eclair.db.pg | ||
|
||
import fr.acinq.bitcoin.scalacompat.ByteVector32 | ||
import fr.acinq.eclair.db.Monitoring.Metrics.withMetrics | ||
import fr.acinq.eclair.db.Monitoring.Tags.DbBackends | ||
import fr.acinq.eclair.db.{OfferData, OffersDb} | ||
import fr.acinq.eclair.db.pg.PgUtils.PgLock | ||
import fr.acinq.eclair.wire.protocol.OfferTypes | ||
import grizzled.slf4j.Logging | ||
|
||
import java.sql.Statement | ||
import javax.sql.DataSource | ||
|
||
object PgOffersDb { | ||
val DB_NAME = "offers" | ||
val CURRENT_VERSION = 1 | ||
} | ||
|
||
class PgOffersDb(implicit ds: DataSource, lock: PgLock) extends OffersDb with Logging { | ||
|
||
import PgPaymentsDb._ | ||
import PgUtils.ExtendedResultSet._ | ||
import PgUtils._ | ||
import lock._ | ||
|
||
inTransaction { pg => | ||
using(pg.createStatement()) { statement => | ||
getVersion(statement, DB_NAME) match { | ||
case None => | ||
statement.executeUpdate("CREATE SCHEMA offers") | ||
|
||
statement.executeUpdate("CREATE TABLE offers.managed (offer_id TEXT NOT NULL PRIMARY KEY, offer TEXT NOT NULL, path_id TEXT, created_at TIMESTAMP WITH TIME ZONE NOT NULL, is_active BOOLEAN NOT NULL)") | ||
|
||
statement.executeUpdate("CREATE INDEX offer_is_active_idx ON offers.managed(is_active)") | ||
case Some(CURRENT_VERSION) => () // table is up-to-date, nothing to do | ||
case Some(unknownVersion) => throw new RuntimeException(s"Unknown version of DB $DB_NAME found, version=$unknownVersion") | ||
} | ||
setVersion(statement, DB_NAME, CURRENT_VERSION) | ||
} | ||
} | ||
|
||
override def addOffer(offer: OfferTypes.Offer, pathId_opt: Option[ByteVector32]): Unit = withMetrics("offers/add", DbBackends.Postgres){ | ||
withLock { pg => | ||
using(pg.prepareStatement("INSERT INTO offers.managed (offer_id, offer, path_id, created_at, is_active) VALUES (?, ?, ?, NOW, TRUE)")) { statement => | ||
statement.setString(1, offer.offerId.toHex) | ||
statement.setString(2, offer.toString) | ||
pathId_opt match { | ||
case Some(pathId) => statement.setString(3, pathId.toHex) | ||
case None => statement.setNull(3, java.sql.Types.VARCHAR) | ||
} | ||
|
||
statement.executeUpdate() | ||
} | ||
} | ||
} | ||
|
||
override def disableOffer(offer: OfferTypes.Offer): Unit = ??? | ||
|
||
override def listOffers(onlyActive: Boolean): Seq[OfferData] = ??? | ||
} |
33 changes: 33 additions & 0 deletions
33
eclair-core/src/main/scala/fr/acinq/eclair/db/sqlite/SqliteOffersDb.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.eclair.db.sqlite | ||
|
||
import fr.acinq.bitcoin.scalacompat.ByteVector32 | ||
import fr.acinq.eclair.db.{OfferData, OffersDb} | ||
import fr.acinq.eclair.wire.protocol.OfferTypes | ||
import grizzled.slf4j.Logging | ||
|
||
import java.sql.Connection | ||
|
||
class SqliteOffersDb(val sqlite: Connection) extends OffersDb with Logging { | ||
|
||
override def addOffer(offer: OfferTypes.Offer, pathId_opt: Option[ByteVector32]): Unit = ??? | ||
|
||
override def disableOffer(offer: OfferTypes.Offer): Unit = ??? | ||
|
||
override def listOffers(onlyActive: Boolean): Seq[OfferData] = ??? | ||
} |
Oops, something went wrong.