-
Notifications
You must be signed in to change notification settings - Fork 275
Offers without extra plugin #2976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d4261f5
Basic offer management without plugins
thomash-acinq 242fa8c
comments t-bast
thomash-acinq 7491779
more comments
thomash-acinq e378122
invoice requests must have an amount
thomash-acinq e6ed8fd
fix tests
thomash-acinq eef2614
Clean-up and refactoring
t-bast 25df7bf
No eclairImpl in tests
thomash-acinq 867487f
Improve offers payment e2e tests
t-bast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
51 changes: 51 additions & 0 deletions
51
eclair-core/src/main/scala/fr/acinq/eclair/db/OffersDb.scala
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * Copyright 2025 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.TimestampMilli | ||
| import fr.acinq.eclair.wire.protocol.OfferTypes.Offer | ||
|
|
||
| /** | ||
| * 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 | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Disable an offer. The offer is still stored but new invoice requests and new payment attempts for already emitted | ||
| * invoices will be rejected. | ||
| */ | ||
| def disableOffer(offer: Offer): Unit | ||
|
|
||
| /** | ||
| * Activate an offer that was previously disabled. | ||
| */ | ||
| def enableOffer(offer: Offer): Unit | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * List offers managed by eclair. | ||
| * @param onlyActive Whether to return only active offers or also disabled ones. | ||
| */ | ||
| def listOffers(onlyActive: Boolean): Seq[OfferData] | ||
| } | ||
|
|
||
| case class OfferData(offer: Offer, pathId_opt: Option[ByteVector32], createdAt: TimestampMilli, isActive: Boolean) | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
113 changes: 113 additions & 0 deletions
113
eclair-core/src/main/scala/fr/acinq/eclair/db/pg/PgOffersDb.scala
This file contains hidden or 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,113 @@ | ||
| /* | ||
| * Copyright 2025 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.TimestampMilli | ||
| 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 fr.acinq.eclair.wire.protocol.OfferTypes.Offer | ||
| import grizzled.slf4j.Logging | ||
|
|
||
| import java.sql.ResultSet | ||
| 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 PgOffersDb._ | ||
| 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") | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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)") | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 (?, ?, ?, ?, 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) | ||
| } | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| statement.setTimestamp(4, TimestampMilli.now().toSqlTimestamp) | ||
| statement.executeUpdate() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override def disableOffer(offer: OfferTypes.Offer): Unit = withMetrics("offers/disable", DbBackends.Postgres){ | ||
| withLock { pg => | ||
| using(pg.prepareStatement("UPDATE offers.managed SET is_active = FALSE WHERE offer_id = ?")) { statement => | ||
| statement.setString(1, offer.offerId.toHex) | ||
| statement.executeUpdate() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override def enableOffer(offer: OfferTypes.Offer): Unit = withMetrics("offers/enable", DbBackends.Postgres){ | ||
| withLock { pg => | ||
| using(pg.prepareStatement("UPDATE offers.managed SET is_active = TRUE WHERE offer_id = ?")) { statement => | ||
| statement.setString(1, offer.offerId.toHex) | ||
| statement.executeUpdate() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def parseOfferData(rs: ResultSet): OfferData = { | ||
| OfferData( | ||
| Offer.decode(rs.getString("offer")).get, | ||
| rs.getStringNullable("path_id").map(ByteVector32.fromValidHex), | ||
| TimestampMilli.fromSqlTimestamp(rs.getTimestamp("created_at")), | ||
| rs.getBoolean("is_active") | ||
| ) | ||
| } | ||
|
|
||
| override def listOffers(onlyActive: Boolean): Seq[OfferData] = withMetrics("offers/list", DbBackends.Postgres){ | ||
t-bast marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| withLock { pg => | ||
| if (onlyActive) { | ||
| using(pg.prepareStatement("SELECT * FROM offers.managed WHERE is_active = TRUE")) { statement => | ||
| statement.executeQuery().map(parseOfferData).toSeq | ||
| } | ||
| } else { | ||
| using(pg.prepareStatement("SELECT * FROM offers.managed")) { statement => | ||
| statement.executeQuery().map(parseOfferData).toSeq | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.