Skip to content

Commit 25631a9

Browse files
committed
add atomic custom records
1 parent 4aa2bcf commit 25631a9

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

crates/fiber-lib/src/fiber/tests/types.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use crate::{
66
gen::{fiber as molecule_fiber, gossip},
77
hash_algorithm::HashAlgorithm,
88
types::{
9-
pack_hop_data, secp256k1_instance, unpack_hop_data, AddTlc, BroadcastMessageID, Cursor,
10-
Hash256, NodeAnnouncement, NodeId, PaymentHopData, PeeledOnionPacket, Privkey, Pubkey,
11-
TlcErr, TlcErrPacket, TlcErrorCode, NO_SHARED_SECRET,
9+
pack_hop_data, secp256k1_instance, unpack_hop_data, AMPDataRecord, AddTlc,
10+
BroadcastMessageID, Cursor, Hash256, NodeAnnouncement, NodeId, PaymentHopData,
11+
PeeledOnionPacket, Privkey, Pubkey, TlcErr, TlcErrPacket, TlcErrorCode,
12+
NO_SHARED_SECRET,
1213
},
1314
PaymentCustomRecords,
1415
},
1516
gen_deterministic_fiber_private_key, gen_rand_channel_outpoint, gen_rand_fiber_private_key,
16-
gen_rand_fiber_public_key, now_timestamp_as_millis_u64,
17+
gen_rand_fiber_public_key, gen_rand_sha256_hash, now_timestamp_as_millis_u64,
1718
};
1819
use ckb_hash::blake2b_256;
1920
use ckb_jsonrpc_types::OutPoint;
@@ -701,3 +702,15 @@ fn test_serde_node_id() {
701702
"to NodeId"
702703
);
703704
}
705+
706+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
707+
#[cfg_attr(not(target_arch = "wasm32"), test)]
708+
fn test_amp_custom_records() {
709+
let mut payment_custom_records = PaymentCustomRecords::default();
710+
let parent_payment_hash = gen_rand_sha256_hash();
711+
let amp_record = AMPDataRecord::new(parent_payment_hash, 0, 3);
712+
amp_record.write(&mut payment_custom_records);
713+
714+
let new_amp_record = AMPDataRecord::read(&payment_custom_records).unwrap();
715+
assert_eq!(new_amp_record, amp_record);
716+
}

crates/fiber-lib/src/fiber/types.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,58 @@ impl PaymentDataRecord {
37863786
}
37873787
}
37883788

3789+
#[derive(Hash, Eq, PartialEq, Debug)]
3790+
pub struct AMPDataRecord {
3791+
pub parent_payment_hash: Hash256,
3792+
pub total_amp_count: u16,
3793+
pub index: u16,
3794+
}
3795+
3796+
impl AMPDataRecord {
3797+
pub const CUSTOM_RECORD_KEY: u32 = 65537;
3798+
3799+
pub fn new(parent_payment_hash: Hash256, index: u16, total_amp_count: u16) -> Self {
3800+
Self {
3801+
parent_payment_hash,
3802+
total_amp_count,
3803+
index,
3804+
}
3805+
}
3806+
3807+
fn to_vec(&self) -> Vec<u8> {
3808+
let mut vec = Vec::new();
3809+
vec.extend_from_slice(self.parent_payment_hash.as_ref());
3810+
vec.extend_from_slice(&self.total_amp_count.to_le_bytes());
3811+
vec.extend_from_slice(&self.index.to_le_bytes());
3812+
vec
3813+
}
3814+
3815+
pub fn write(&self, custom_records: &mut PaymentCustomRecords) {
3816+
custom_records
3817+
.data
3818+
.insert(Self::CUSTOM_RECORD_KEY, self.to_vec());
3819+
}
3820+
3821+
pub fn read(custom_records: &PaymentCustomRecords) -> Option<Self> {
3822+
custom_records
3823+
.data
3824+
.get(&Self::CUSTOM_RECORD_KEY)
3825+
.and_then(|data| {
3826+
if data.len() != 32 + 4 {
3827+
return None;
3828+
}
3829+
let parent_hash: [u8; 32] = data[..32].try_into().unwrap();
3830+
let total_amp_count = u16::from_le_bytes(data[32..34].try_into().unwrap());
3831+
let index = u16::from_le_bytes(data[34..].try_into().unwrap());
3832+
Some(Self::new(
3833+
Hash256::from(parent_hash),
3834+
index,
3835+
total_amp_count,
3836+
))
3837+
})
3838+
}
3839+
}
3840+
37893841
#[serde_as]
37903842
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37913843
pub struct PaymentHopData {

0 commit comments

Comments
 (0)