Skip to content

Commit e27aad5

Browse files
committed
rustfmt: Run on lightning-block-sync/src/poll.rs
1 parent 5ca6d5e commit e27aad5

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

lightning-block-sync/src/poll.rs

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Adapters that make one or more [`BlockSource`]s simpler to poll for new chain tip transitions.
22
3-
use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError, BlockSourceResult};
3+
use crate::{
4+
AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource, BlockSourceError,
5+
BlockSourceResult,
6+
};
47

58
use bitcoin::hash_types::BlockHash;
69
use bitcoin::network::Network;
@@ -17,16 +20,19 @@ use std::ops::Deref;
1720
/// [`ChainPoller`]: ../struct.ChainPoller.html
1821
pub trait Poll {
1922
/// Returns a chain tip in terms of its relationship to the provided chain tip.
20-
fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
21-
AsyncBlockSourceResult<'a, ChainTip>;
23+
fn poll_chain_tip<'a>(
24+
&'a self, best_known_chain_tip: ValidatedBlockHeader,
25+
) -> AsyncBlockSourceResult<'a, ChainTip>;
2226

2327
/// Returns the header that preceded the given header in the chain.
24-
fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
25-
AsyncBlockSourceResult<'a, ValidatedBlockHeader>;
28+
fn look_up_previous_header<'a>(
29+
&'a self, header: &'a ValidatedBlockHeader,
30+
) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader>;
2631

2732
/// Returns the block associated with the given header.
28-
fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
29-
AsyncBlockSourceResult<'a, ValidatedBlock>;
33+
fn fetch_block<'a>(
34+
&'a self, header: &'a ValidatedBlockHeader,
35+
) -> AsyncBlockSourceResult<'a, ValidatedBlock>;
3036
}
3137

3238
/// A chain tip relative to another chain tip in terms of block hash and chainwork.
@@ -59,9 +65,8 @@ impl Validate for BlockHeaderData {
5965
type T = ValidatedBlockHeader;
6066

6167
fn validate(self, block_hash: BlockHash) -> BlockSourceResult<Self::T> {
62-
let pow_valid_block_hash = self.header
63-
.validate_pow(self.header.target())
64-
.map_err(BlockSourceError::persistent)?;
68+
let pow_valid_block_hash =
69+
self.header.validate_pow(self.header.target()).map_err(BlockSourceError::persistent)?;
6570

6671
if pow_valid_block_hash != block_hash {
6772
return Err(BlockSourceError::persistent("invalid block hash"));
@@ -80,9 +85,8 @@ impl Validate for BlockData {
8085
BlockData::HeaderOnly(header) => header,
8186
};
8287

83-
let pow_valid_block_hash = header
84-
.validate_pow(header.target())
85-
.map_err(BlockSourceError::persistent)?;
88+
let pow_valid_block_hash =
89+
header.validate_pow(header.target()).map_err(BlockSourceError::persistent)?;
8690

8791
if pow_valid_block_hash != block_hash {
8892
return Err(BlockSourceError::persistent("invalid block hash"));
@@ -120,7 +124,9 @@ impl std::ops::Deref for ValidatedBlockHeader {
120124
impl ValidatedBlockHeader {
121125
/// Checks that the header correctly builds on previous_header: the claimed work differential
122126
/// matches the actual PoW and the difficulty transition is possible, i.e., within 4x.
123-
fn check_builds_on(&self, previous_header: &ValidatedBlockHeader, network: Network) -> BlockSourceResult<()> {
127+
fn check_builds_on(
128+
&self, previous_header: &ValidatedBlockHeader, network: Network,
129+
) -> BlockSourceResult<()> {
124130
if self.header.prev_blockhash != previous_header.block_hash {
125131
return Err(BlockSourceError::persistent("invalid previous block hash"));
126132
}
@@ -141,28 +147,28 @@ impl ValidatedBlockHeader {
141147
let min_target = previous_target.min_difficulty_transition_threshold();
142148
let max_target = previous_target.max_difficulty_transition_threshold();
143149
if target > max_target || target < min_target {
144-
return Err(BlockSourceError::persistent("invalid difficulty transition"))
150+
return Err(BlockSourceError::persistent("invalid difficulty transition"));
145151
}
146152
} else if self.header.bits != previous_header.header.bits {
147-
return Err(BlockSourceError::persistent("invalid difficulty"))
153+
return Err(BlockSourceError::persistent("invalid difficulty"));
148154
}
149155
}
150156

151157
Ok(())
152158
}
153159

154-
/// Returns the [`BestBlock`] corresponding to this validated block header, which can be passed
155-
/// into [`ChannelManager::new`] as part of its [`ChainParameters`]. Useful for ensuring that
156-
/// the [`SpvClient`] and [`ChannelManager`] are initialized to the same block during a fresh
157-
/// start.
158-
///
159-
/// [`SpvClient`]: crate::SpvClient
160-
/// [`ChainParameters`]: lightning::ln::channelmanager::ChainParameters
161-
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
162-
/// [`ChannelManager::new`]: lightning::ln::channelmanager::ChannelManager::new
163-
pub fn to_best_block(&self) -> BestBlock {
164-
BestBlock::new(self.block_hash, self.inner.height)
165-
}
160+
/// Returns the [`BestBlock`] corresponding to this validated block header, which can be passed
161+
/// into [`ChannelManager::new`] as part of its [`ChainParameters`]. Useful for ensuring that
162+
/// the [`SpvClient`] and [`ChannelManager`] are initialized to the same block during a fresh
163+
/// start.
164+
///
165+
/// [`SpvClient`]: crate::SpvClient
166+
/// [`ChainParameters`]: lightning::ln::channelmanager::ChainParameters
167+
/// [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
168+
/// [`ChannelManager::new`]: lightning::ln::channelmanager::ChannelManager::new
169+
pub fn to_best_block(&self) -> BestBlock {
170+
BestBlock::new(self.block_hash, self.inner.height)
171+
}
166172
}
167173

168174
/// A block with validated data against its transaction list and corresponding block hash.
@@ -191,12 +197,12 @@ mod sealed {
191197
///
192198
/// Other `Poll` implementations should be built using `ChainPoller` as it provides the simplest way
193199
/// of validating chain data and checking consistency.
194-
pub struct ChainPoller<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
200+
pub struct ChainPoller<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> {
195201
block_source: B,
196202
network: Network,
197203
}
198204

199-
impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
205+
impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPoller<B, T> {
200206
/// Creates a new poller for the given block source.
201207
///
202208
/// If the `network` parameter is mainnet, then the difficulty between blocks is checked for
@@ -206,19 +212,20 @@ impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> ChainPol
206212
}
207213
}
208214

209-
impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for ChainPoller<B, T> {
210-
fn poll_chain_tip<'a>(&'a self, best_known_chain_tip: ValidatedBlockHeader) ->
211-
AsyncBlockSourceResult<'a, ChainTip>
212-
{
215+
impl<B: Deref<Target = T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll
216+
for ChainPoller<B, T>
217+
{
218+
fn poll_chain_tip<'a>(
219+
&'a self, best_known_chain_tip: ValidatedBlockHeader,
220+
) -> AsyncBlockSourceResult<'a, ChainTip> {
213221
Box::pin(async move {
214222
let (block_hash, height) = self.block_source.get_best_block().await?;
215223
if block_hash == best_known_chain_tip.header.block_hash() {
216224
return Ok(ChainTip::Common);
217225
}
218226

219-
let chain_tip = self.block_source
220-
.get_header(&block_hash, height).await?
221-
.validate(block_hash)?;
227+
let chain_tip =
228+
self.block_source.get_header(&block_hash, height).await?.validate(block_hash)?;
222229
if chain_tip.chainwork > best_known_chain_tip.chainwork {
223230
Ok(ChainTip::Better(chain_tip))
224231
} else {
@@ -227,41 +234,41 @@ impl<B: Deref<Target=T> + Sized + Send + Sync, T: BlockSource + ?Sized> Poll for
227234
})
228235
}
229236

230-
fn look_up_previous_header<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
231-
AsyncBlockSourceResult<'a, ValidatedBlockHeader>
232-
{
237+
fn look_up_previous_header<'a>(
238+
&'a self, header: &'a ValidatedBlockHeader,
239+
) -> AsyncBlockSourceResult<'a, ValidatedBlockHeader> {
233240
Box::pin(async move {
234241
if header.height == 0 {
235242
return Err(BlockSourceError::persistent("genesis block reached"));
236243
}
237244

238245
let previous_hash = &header.header.prev_blockhash;
239246
let height = header.height - 1;
240-
let previous_header = self.block_source
241-
.get_header(previous_hash, Some(height)).await?
247+
let previous_header = self
248+
.block_source
249+
.get_header(previous_hash, Some(height))
250+
.await?
242251
.validate(*previous_hash)?;
243252
header.check_builds_on(&previous_header, self.network)?;
244253

245254
Ok(previous_header)
246255
})
247256
}
248257

249-
fn fetch_block<'a>(&'a self, header: &'a ValidatedBlockHeader) ->
250-
AsyncBlockSourceResult<'a, ValidatedBlock>
251-
{
258+
fn fetch_block<'a>(
259+
&'a self, header: &'a ValidatedBlockHeader,
260+
) -> AsyncBlockSourceResult<'a, ValidatedBlock> {
252261
Box::pin(async move {
253-
self.block_source
254-
.get_block(&header.block_hash).await?
255-
.validate(header.block_hash)
262+
self.block_source.get_block(&header.block_hash).await?.validate(header.block_hash)
256263
})
257264
}
258265
}
259266

260267
#[cfg(test)]
261268
mod tests {
262-
use crate::*;
263-
use crate::test_utils::Blockchain;
264269
use super::*;
270+
use crate::test_utils::Blockchain;
271+
use crate::*;
265272

266273
#[tokio::test]
267274
async fn poll_empty_chain() {
@@ -307,7 +314,10 @@ mod tests {
307314
match poller.poll_chain_tip(best_known_chain_tip).await {
308315
Err(e) => {
309316
assert_eq!(e.kind(), BlockSourceErrorKind::Persistent);
310-
assert_eq!(e.into_inner().as_ref().to_string(), "block target correct but not attained");
317+
assert_eq!(
318+
e.into_inner().as_ref().to_string(),
319+
"block target correct but not attained"
320+
);
311321
},
312322
Ok(_) => panic!("Expected error"),
313323
}

rustfmt_excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
./lightning-background-processor/src/lib.rs
22
./lightning-block-sync/src/lib.rs
3-
./lightning-block-sync/src/poll.rs
43
./lightning-block-sync/src/rest.rs
54
./lightning-block-sync/src/rpc.rs
65
./lightning-block-sync/src/test_utils.rs

0 commit comments

Comments
 (0)