Skip to content

Commit b17aaa9

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

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

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

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

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

0 commit comments

Comments
 (0)