projects
/
rust-lightning
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #1404 from TheBlueMatt/2022-04-buf-writes
[rust-lightning]
/
lightning-block-sync
/
src
/
test_utils.rs
diff --git
a/lightning-block-sync/src/test_utils.rs
b/lightning-block-sync/src/test_utils.rs
index 70a8982a0efb746a303bdcd530ec536c322e0be3..fe57c0c606d02d5cfbc5bf79f3bba1637904711f 100644
(file)
--- a/
lightning-block-sync/src/test_utils.rs
+++ b/
lightning-block-sync/src/test_utils.rs
@@
-1,4
+1,4
@@
-use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError,
ChainListener,
UnboundedCache};
+use crate::{AsyncBlockSourceResult, BlockHeaderData, BlockSource, BlockSourceError, UnboundedCache};
use crate::poll::{Validate, ValidatedBlockHeader};
use bitcoin::blockdata::block::{Block, BlockHeader};
use crate::poll::{Validate, ValidatedBlockHeader};
use bitcoin::blockdata::block::{Block, BlockHeader};
@@
-7,6
+7,9
@@
use bitcoin::hash_types::BlockHash;
use bitcoin::network::constants::Network;
use bitcoin::util::uint::Uint256;
use bitcoin::network::constants::Network;
use bitcoin::util::uint::Uint256;
+use lightning::chain;
+
+use std::cell::RefCell;
use std::collections::VecDeque;
#[derive(Default)]
use std::collections::VecDeque;
#[derive(Default)]
@@
-110,7
+113,7
@@
impl Blockchain {
}
impl BlockSource for Blockchain {
}
impl BlockSource for Blockchain {
- fn get_header<'a>(&'a
mut
self, header_hash: &'a BlockHash, _height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
+ fn get_header<'a>(&'a self, header_hash: &'a BlockHash, _height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
Box::pin(async move {
if self.without_headers {
return Err(BlockSourceError::persistent("header not found"));
Box::pin(async move {
if self.without_headers {
return Err(BlockSourceError::persistent("header not found"));
@@
-130,7
+133,7
@@
impl BlockSource for Blockchain {
})
}
})
}
- fn get_block<'a>(&'a
mut
self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
+ fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
Box::pin(async move {
for (height, block) in self.blocks.iter().enumerate() {
if block.header.block_hash() == *header_hash {
Box::pin(async move {
for (height, block) in self.blocks.iter().enumerate() {
if block.header.block_hash() == *header_hash {
@@
-147,7
+150,7
@@
impl BlockSource for Blockchain {
})
}
})
}
- fn get_best_block<'a>(&'a
mut
self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
+ fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
Box::pin(async move {
match self.blocks.last() {
None => Err(BlockSourceError::transient("empty chain")),
Box::pin(async move {
match self.blocks.last() {
None => Err(BlockSourceError::transient("empty chain")),
@@
-160,33
+163,40
@@
impl BlockSource for Blockchain {
}
}
}
}
+pub struct NullChainListener;
+
+impl chain::Listen for NullChainListener {
+ fn block_connected(&self, _block: &Block, _height: u32) {}
+ fn block_disconnected(&self, _header: &BlockHeader, _height: u32) {}
+}
+
pub struct MockChainListener {
pub struct MockChainListener {
- expected_blocks_connected:
VecDeque<BlockHeaderData
>,
- expected_blocks_disconnected:
VecDeque<BlockHeaderData
>,
+ expected_blocks_connected:
RefCell<VecDeque<BlockHeaderData>
>,
+ expected_blocks_disconnected:
RefCell<VecDeque<BlockHeaderData>
>,
}
impl MockChainListener {
pub fn new() -> Self {
Self {
}
impl MockChainListener {
pub fn new() -> Self {
Self {
- expected_blocks_connected:
VecDeque::new(
),
- expected_blocks_disconnected:
VecDeque::new(
),
+ expected_blocks_connected:
RefCell::new(VecDeque::new()
),
+ expected_blocks_disconnected:
RefCell::new(VecDeque::new()
),
}
}
}
}
- pub fn expect_block_connected(
mut
self, block: BlockHeaderData) -> Self {
- self.expected_blocks_connected.push_back(block);
+ pub fn expect_block_connected(self, block: BlockHeaderData) -> Self {
+ self.expected_blocks_connected.
borrow_mut().
push_back(block);
self
}
self
}
- pub fn expect_block_disconnected(
mut
self, block: BlockHeaderData) -> Self {
- self.expected_blocks_disconnected.push_back(block);
+ pub fn expect_block_disconnected(self, block: BlockHeaderData) -> Self {
+ self.expected_blocks_disconnected.
borrow_mut().
push_back(block);
self
}
}
self
}
}
-impl
ChainListener
for MockChainListener {
- fn block_connected(&
mut
self, block: &Block, height: u32) {
- match self.expected_blocks_connected.pop_front() {
+impl
chain::Listen
for MockChainListener {
+ fn block_connected(&self, block: &Block, height: u32) {
+ match self.expected_blocks_connected.
borrow_mut().
pop_front() {
None => {
panic!("Unexpected block connected: {:?}", block.block_hash());
},
None => {
panic!("Unexpected block connected: {:?}", block.block_hash());
},
@@
-197,8
+207,8
@@
impl ChainListener for MockChainListener {
}
}
}
}
- fn block_disconnected(&
mut
self, header: &BlockHeader, height: u32) {
- match self.expected_blocks_disconnected.pop_front() {
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ match self.expected_blocks_disconnected.
borrow_mut().
pop_front() {
None => {
panic!("Unexpected block disconnected: {:?}", header.block_hash());
},
None => {
panic!("Unexpected block disconnected: {:?}", header.block_hash());
},
@@
-215,11
+225,15
@@
impl Drop for MockChainListener {
if std::thread::panicking() {
return;
}
if std::thread::panicking() {
return;
}
- if !self.expected_blocks_connected.is_empty() {
- panic!("Expected blocks connected: {:?}", self.expected_blocks_connected);
+
+ let expected_blocks_connected = self.expected_blocks_connected.borrow();
+ if !expected_blocks_connected.is_empty() {
+ panic!("Expected blocks connected: {:?}", expected_blocks_connected);
}
}
- if !self.expected_blocks_disconnected.is_empty() {
- panic!("Expected blocks disconnected: {:?}", self.expected_blocks_disconnected);
+
+ let expected_blocks_disconnected = self.expected_blocks_disconnected.borrow();
+ if !expected_blocks_disconnected.is_empty() {
+ panic!("Expected blocks disconnected: {:?}", expected_blocks_disconnected);
}
}
}
}
}
}