Skip to content

Commit 5055994

Browse files
committed
lightning-block-sync: Implement ser. logic for header Cache types
During syncing, `lightning-block-sync` populates as a block header `Cache` that is crucial to be able to disconnected previously-connected headers cleanly in case of a reorg. Moreover, the `Cache` can have performance benefits as subsequently synced listeners might not necessarily need to lookup all headers again from the chain source. While this `Cache` is ~crucial to the clean operation of `lightning-block-sync`, it was previously not possible to persist it to disk due to an absence of serialization logic implementations for the corresponding sub-types. Here, we do just that (Implement said serialization logic) to allow users to persist the `Cache`.
1 parent 1b281f1 commit 5055994

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

lightning-block-sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rpc-client = [ "serde_json", "chunked_transfer" ]
2020
[dependencies]
2121
bitcoin = "0.32.2"
2222
lightning = { version = "0.2.0", path = "../lightning" }
23+
lightning-macros = { version = "0.2", path = "../lightning-macros" }
2324
tokio = { version = "1.35", features = [ "io-util", "net", "time", "rt" ], optional = true }
2425
serde_json = { version = "1.0", optional = true }
2526
chunked_transfer = { version = "1.4", optional = true }

lightning-block-sync/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ use bitcoin::block::{Block, Header};
4848
use bitcoin::hash_types::BlockHash;
4949
use bitcoin::pow::Work;
5050

51-
use lightning::chain;
5251
use lightning::chain::Listen;
52+
use lightning::{chain, impl_writeable_tlv_based};
5353

5454
use std::future::Future;
5555
use std::ops::Deref;
@@ -155,6 +155,12 @@ pub struct BlockHeaderData {
155155
pub chainwork: Work,
156156
}
157157

158+
impl_writeable_tlv_based!(BlockHeaderData, {
159+
(0, header, required),
160+
(2, height, required),
161+
(2, chainwork, required),
162+
});
163+
158164
/// A block including either all its transactions or only the block header.
159165
///
160166
/// [`BlockSource`] may be implemented to either always return full blocks or, in the case of

lightning-block-sync/src/poll.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::{
77

88
use bitcoin::hash_types::BlockHash;
99
use bitcoin::network::Network;
10+
1011
use lightning::chain::BestBlock;
12+
use lightning::impl_writeable_tlv_based;
1113

1214
use std::ops::Deref;
1315

@@ -171,6 +173,11 @@ impl ValidatedBlockHeader {
171173
}
172174
}
173175

176+
impl_writeable_tlv_based!(ValidatedBlockHeader, {
177+
(0, block_hash, required),
178+
(2, inner, required),
179+
});
180+
174181
/// A block with validated data against its transaction list and corresponding block hash.
175182
pub struct ValidatedBlock {
176183
pub(crate) block_hash: BlockHash,

lightning/src/util/ser.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ use core::ops::Deref;
2424
use alloc::collections::BTreeMap;
2525

2626
use bitcoin::amount::Amount;
27+
use bitcoin::block::Header;
2728
use bitcoin::consensus::Encodable;
2829
use bitcoin::constants::ChainHash;
2930
use bitcoin::hash_types::{BlockHash, Txid};
3031
use bitcoin::hashes::hmac::Hmac;
3132
use bitcoin::hashes::sha256::Hash as Sha256;
3233
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
34+
use bitcoin::pow::Work;
3335
use bitcoin::script::{self, ScriptBuf};
3436
use bitcoin::secp256k1::constants::{
3537
COMPACT_SIGNATURE_SIZE, PUBLIC_KEY_SIZE, SCHNORR_SIGNATURE_SIZE, SECRET_KEY_SIZE,
@@ -1220,6 +1222,26 @@ impl Readable for Sha256dHash {
12201222
}
12211223
}
12221224

1225+
const WORK_SIZE: usize = 32;
1226+
1227+
impl Writeable for Work {
1228+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1229+
self.to_be_bytes().write(w)
1230+
}
1231+
1232+
#[inline]
1233+
fn serialized_length(&self) -> usize {
1234+
WORK_SIZE
1235+
}
1236+
}
1237+
1238+
impl Readable for Work {
1239+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1240+
let buf: [u8; WORK_SIZE] = Readable::read(r)?;
1241+
Ok(Work::from_be_bytes(buf))
1242+
}
1243+
}
1244+
12231245
impl Writeable for ecdsa::Signature {
12241246
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
12251247
self.serialize_compact().write(w)
@@ -1429,6 +1451,7 @@ macro_rules! impl_consensus_ser {
14291451
}
14301452
};
14311453
}
1454+
impl_consensus_ser!(Header);
14321455
impl_consensus_ser!(Transaction);
14331456
impl_consensus_ser!(TxOut);
14341457
impl_consensus_ser!(Witness);

0 commit comments

Comments
 (0)