-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(coordinator): Persist spendable outputs generated by LDK
Fixes #845.
- Loading branch information
Showing
11 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
coordinator/migrations/2023-07-12-040736_spendable_outputs/down.sql
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 @@ | ||
DROP TABLE "spendable_outputs"; |
8 changes: 8 additions & 0 deletions
8
coordinator/migrations/2023-07-12-040736_spendable_outputs/up.sql
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,8 @@ | ||
CREATE TABLE "spendable_outputs" ( | ||
id SERIAL PRIMARY KEY NOT NULL, | ||
-- hex encoded | ||
txid TEXT NOT NULL, | ||
vout int NOT NULL, | ||
-- hex representation of LDK's own encoding | ||
descriptor TEXT NOT NULL | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod custom_types; | ||
pub mod positions; | ||
pub mod spendable_outputs; | ||
pub mod trades; | ||
pub mod user; |
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,102 @@ | ||
use crate::schema::spendable_outputs; | ||
use anyhow::anyhow; | ||
use anyhow::Result; | ||
use autometrics::autometrics; | ||
use bitcoin::hashes::hex::FromHex; | ||
use bitcoin::hashes::hex::ToHex; | ||
use diesel::prelude::*; | ||
use lightning::chain::keysinterface::DelayedPaymentOutputDescriptor; | ||
use lightning::chain::keysinterface::SpendableOutputDescriptor; | ||
use lightning::chain::keysinterface::StaticPaymentOutputDescriptor; | ||
use lightning::chain::transaction::OutPoint; | ||
use lightning::util::ser::Readable; | ||
use lightning::util::ser::Writeable; | ||
|
||
#[autometrics] | ||
pub(crate) fn insert( | ||
conn: &mut PgConnection, | ||
output: SpendableOutputDescriptor, | ||
) -> QueryResult<()> { | ||
diesel::insert_into(spendable_outputs::table) | ||
.values(NewSpendableOutput::from(output)) | ||
.execute(conn)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[autometrics] | ||
pub fn get( | ||
conn: &mut PgConnection, | ||
outpoint: &OutPoint, | ||
) -> Result<Option<SpendableOutputDescriptor>> { | ||
let output: Option<SpendableOutput> = spendable_outputs::table | ||
.filter(spendable_outputs::txid.eq(outpoint.txid.to_string())) | ||
.first(conn) | ||
.optional()?; | ||
|
||
let output = output | ||
.map(|output| anyhow::Ok(output.try_into()?)) | ||
.transpose()?; | ||
|
||
Ok(output) | ||
} | ||
|
||
#[autometrics] | ||
pub fn get_all(conn: &mut PgConnection) -> Result<Vec<SpendableOutputDescriptor>> { | ||
let outputs: Vec<SpendableOutput> = spendable_outputs::table.load(conn)?; | ||
outputs | ||
.into_iter() | ||
.map(SpendableOutputDescriptor::try_from) | ||
.collect() | ||
} | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[diesel(table_name = spendable_outputs)] | ||
struct NewSpendableOutput { | ||
txid: String, | ||
vout: i32, | ||
descriptor: String, | ||
} | ||
|
||
#[derive(Queryable, Debug, Clone)] | ||
#[diesel(table_name = spendable_outputs)] | ||
struct SpendableOutput { | ||
#[diesel(column_name = "id")] | ||
_id: i32, | ||
#[diesel(column_name = "txid")] | ||
_txid: String, | ||
#[diesel(column_name = "vout")] | ||
_vout: i32, | ||
descriptor: String, | ||
} | ||
|
||
impl From<SpendableOutputDescriptor> for NewSpendableOutput { | ||
fn from(descriptor: SpendableOutputDescriptor) -> Self { | ||
use SpendableOutputDescriptor::*; | ||
let outpoint = match &descriptor { | ||
StaticOutput { outpoint, .. } => outpoint, | ||
DelayedPaymentOutput(DelayedPaymentOutputDescriptor { outpoint, .. }) => outpoint, | ||
StaticPaymentOutput(StaticPaymentOutputDescriptor { outpoint, .. }) => outpoint, | ||
}; | ||
|
||
let descriptor = descriptor.encode().to_hex(); | ||
|
||
Self { | ||
txid: outpoint.txid.to_string(), | ||
vout: outpoint.index as i32, | ||
descriptor, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<SpendableOutput> for SpendableOutputDescriptor { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: SpendableOutput) -> Result<Self, Self::Error> { | ||
let bytes = Vec::from_hex(&value.descriptor)?; | ||
let descriptor = Self::read(&mut lightning::io::Cursor::new(bytes)) | ||
.map_err(|e| anyhow!("Failed to decode spendable output descriptor: {e}"))?; | ||
|
||
Ok(descriptor) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use anyhow::Result; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::r2d2::Pool; | ||
use diesel::PgConnection; | ||
use lightning::chain::keysinterface::SpendableOutputDescriptor; | ||
use lightning::chain::transaction::OutPoint; | ||
use lightning::ln::PaymentHash; | ||
use lightning::ln::PaymentPreimage; | ||
use lightning::ln::PaymentSecret; | ||
use ln_dlc_node::node; | ||
use ln_dlc_node::HTLCStatus; | ||
use ln_dlc_node::MillisatAmount; | ||
use ln_dlc_node::PaymentFlow; | ||
use ln_dlc_node::PaymentInfo; | ||
|
||
#[derive(Clone)] | ||
pub struct NodeStorage { | ||
pool: Pool<ConnectionManager<PgConnection>>, | ||
} | ||
|
||
impl NodeStorage { | ||
pub fn new(pool: Pool<ConnectionManager<PgConnection>>) -> Self { | ||
Self { pool } | ||
} | ||
} | ||
|
||
impl node::Storage for NodeStorage { | ||
// Payments | ||
|
||
fn insert_payment(&self, _payment_hash: PaymentHash, _info: PaymentInfo) -> Result<()> { | ||
todo!() | ||
} | ||
fn merge_payment( | ||
&self, | ||
_payment_hash: &PaymentHash, | ||
_flow: PaymentFlow, | ||
_amt_msat: MillisatAmount, | ||
_htlc_status: HTLCStatus, | ||
_preimage: Option<PaymentPreimage>, | ||
_secret: Option<PaymentSecret>, | ||
) -> Result<()> { | ||
todo!() | ||
} | ||
fn get_payment( | ||
&self, | ||
_payment_hash: &PaymentHash, | ||
) -> Result<Option<(PaymentHash, PaymentInfo)>> { | ||
todo!() | ||
} | ||
fn all_payments(&self) -> Result<Vec<(PaymentHash, PaymentInfo)>> { | ||
todo!() | ||
} | ||
|
||
// Spendable outputs | ||
|
||
fn insert_spendable_output(&self, output: SpendableOutputDescriptor) -> Result<()> { | ||
let mut conn = self.pool.get()?; | ||
crate::db::spendable_outputs::insert(&mut conn, output)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_spendable_output( | ||
&self, | ||
outpoint: &OutPoint, | ||
) -> Result<Option<SpendableOutputDescriptor>> { | ||
let mut conn = self.pool.get()?; | ||
crate::db::spendable_outputs::get(&mut conn, outpoint) | ||
} | ||
|
||
fn all_spendable_outputs(&self) -> Result<Vec<SpendableOutputDescriptor>> { | ||
let mut conn = self.pool.get()?; | ||
crate::db::spendable_outputs::get_all(&mut conn) | ||
} | ||
} |
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