Skip to content
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

[WIP] Add payment custom records #443

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
19 changes: 15 additions & 4 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#[cfg(debug_assertions)]
use crate::fiber::network::DebugEvent;
use crate::{
debug_event,
fiber::{serde_utils::U64Hex, types::BroadcastMessageWithTimestamp},
};
use crate::fiber::network::PaymentCustomRecord;
use crate::fiber::types::BroadcastMessageWithTimestamp;
use crate::{debug_event, fiber::serde_utils::U64Hex};
use bitflags::bitflags;
use futures::future::OptionFuture;
use secp256k1::XOnlyPublicKey;
Expand Down Expand Up @@ -982,6 +981,12 @@ where
.update_invoice_status(&payment_hash, CkbInvoiceStatus::Received)
.expect("update invoice status failed");
}

if let Some(custom_records) = peeled_onion_packet.current.custom_records {
self.store
.insert_payment_custom_records(&payment_hash, custom_records);
}

self.store
.insert_payment_preimage(payment_hash, preimage)
.map_err(|_| {
Expand Down Expand Up @@ -6748,6 +6753,12 @@ pub trait ChannelActorStateStore {
.collect()
}
fn get_channel_state_by_outpoint(&self, id: &OutPoint) -> Option<ChannelActorState>;
fn insert_payment_custom_records(
&self,
payment_hash: &Hash256,
custom_records: PaymentCustomRecord,
);
fn get_payment_custom_records(&self, payment_hash: &Hash256) -> Option<PaymentCustomRecord>;
}

/// A wrapper on CommitmentTransaction that has a partial signature along with
Expand Down
11 changes: 9 additions & 2 deletions src/fiber/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,13 @@ where
next_hop,
hash_algorithm: hash_algorithm,
expiry: current_expiry,
funding_tx_hash,
payment_preimage: if is_last { preimage } else { None },
custom_records: if is_last {
payment_data.custom_records.clone()
} else {
None
},
funding_tx_hash,
});
current_expiry += expiry_delta;
current_amount += fee;
Expand All @@ -772,8 +777,9 @@ where
next_hop: Some(route[0].target),
hash_algorithm: hash_algorithm,
expiry: current_expiry,
funding_tx_hash: route[0].channel_outpoint.tx_hash().into(),
payment_preimage: None,
custom_records: None,
funding_tx_hash: route[0].channel_outpoint.tx_hash().into(),
});
hops_data.reverse();
assert_eq!(hops_data.len(), route.len() + 1);
Expand Down Expand Up @@ -1272,6 +1278,7 @@ impl From<PaymentSession> for SendPaymentResponse {
failed_error: session.last_error,
created_at: session.created_at,
last_updated_at: session.last_updated_at,
custom_records: session.request.custom_records,
fee,
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub struct SendPaymentResponse {
pub created_at: u64,
pub last_updated_at: u64,
pub failed_error: Option<String>,
pub custom_records: Option<PaymentCustomRecord>,
pub fee: u128,
}

Expand Down Expand Up @@ -305,12 +306,20 @@ pub struct SendPaymentCommand {
pub udt_type_script: Option<Script>,
// allow self payment, default is false
pub allow_self_payment: bool,
// custom records
pub custom_records: Option<PaymentCustomRecord>,
// the hop hint which may help the find path algorithm to find the path
pub hop_hints: Option<Vec<HopHint>>,
// dry_run only used for checking, default is false
pub dry_run: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default)]
pub struct PaymentCustomRecord {
/// The custom records to be included in the payment.
pub data: HashMap<u32, Vec<u8>>,
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HopHint {
Expand Down Expand Up @@ -338,6 +347,7 @@ pub struct SendPaymentData {
#[serde_as(as = "Option<EntityHex>")]
pub udt_type_script: Option<Script>,
pub preimage: Option<Hash256>,
pub custom_records: Option<PaymentCustomRecord>,
pub allow_self_payment: bool,
pub hop_hints: Vec<HopHint>,
pub dry_run: bool,
Expand Down Expand Up @@ -482,6 +492,7 @@ impl SendPaymentData {
keysend,
udt_type_script,
preimage,
custom_records: command.custom_records,
allow_self_payment: command.allow_self_payment,
hop_hints,
dry_run: command.dry_run,
Expand Down
Loading
Loading