@@ -28,6 +28,7 @@ use bitcoin::hashes::Hash;
2828use bitcoin:: network:: constants:: Network ;
2929use bitcoin:: util:: hash:: bitcoin_merkle_root;
3030use bitcoin:: util:: uint:: Uint256 ;
31+ use bitcoin:: Witness ;
3132use lightning_block_sync:: poll:: {
3233 ChainPoller , Poll , Validate , ValidatedBlock , ValidatedBlockHeader ,
3334} ;
@@ -82,16 +83,18 @@ impl Blockchain {
8283 let prev_block = & self . blocks [ i - 1 ] ;
8384 let prev_blockhash = prev_block. block_hash ( ) ;
8485 let time = prev_block. header . time + height as u32 ;
86+ let txdata = vec ! [ get_random_tx( ) ] ;
87+ let hashes = txdata. iter ( ) . map ( |obj| obj. txid ( ) . as_hash ( ) ) ;
8588 self . blocks . push ( Block {
8689 header : BlockHeader {
8790 version : 0 ,
8891 prev_blockhash,
89- merkle_root : Default :: default ( ) ,
92+ merkle_root : bitcoin_merkle_root ( hashes ) . unwrap ( ) . into ( ) ,
9093 time,
9194 bits,
9295 nonce : 0 ,
9396 } ,
94- txdata : vec ! [ ] ,
97+ txdata,
9598 } ) ;
9699 }
97100 self
@@ -196,14 +199,20 @@ impl Blockchain {
196199 let prev_blockhash = prev_block. block_hash ( ) ;
197200 let time = prev_block. header . time + ( self . blocks . len ( ) + 1 ) as u32 ;
198201 let txdata = match txs {
199- Some ( t) => t,
200- None => vec ! [ ] ,
202+ Some ( v) => {
203+ if v. is_empty ( ) {
204+ vec ! [ get_random_tx( ) ]
205+ } else {
206+ v
207+ }
208+ }
209+ None => vec ! [ get_random_tx( ) ] ,
201210 } ;
202211 let hashes = txdata. iter ( ) . map ( |obj| obj. txid ( ) . as_hash ( ) ) ;
203212 let mut header = BlockHeader {
204213 version : 0 ,
205214 prev_blockhash,
206- merkle_root : bitcoin_merkle_root ( hashes) . into ( ) ,
215+ merkle_root : bitcoin_merkle_root ( hashes) . unwrap ( ) . into ( ) ,
207216 time,
208217 bits,
209218 nonce : 0 ,
@@ -222,7 +231,7 @@ impl Blockchain {
222231
223232impl BlockSource for Blockchain {
224233 fn get_header < ' a > (
225- & ' a mut self ,
234+ & ' a self ,
226235 header_hash : & ' a BlockHash ,
227236 _height_hint : Option < u32 > ,
228237 ) -> AsyncBlockSourceResult < ' a , BlockHeaderData > {
@@ -245,10 +254,7 @@ impl BlockSource for Blockchain {
245254 } )
246255 }
247256
248- fn get_block < ' a > (
249- & ' a mut self ,
250- header_hash : & ' a BlockHash ,
251- ) -> AsyncBlockSourceResult < ' a , Block > {
257+ fn get_block < ' a > ( & ' a self , header_hash : & ' a BlockHash ) -> AsyncBlockSourceResult < ' a , Block > {
252258 Box :: pin ( async move {
253259 for ( height, block) in self . blocks . iter ( ) . enumerate ( ) {
254260 if block. header . block_hash ( ) == * header_hash {
@@ -265,7 +271,7 @@ impl BlockSource for Blockchain {
265271 } )
266272 }
267273
268- fn get_best_block ( & mut self ) -> AsyncBlockSourceResult < ( BlockHash , Option < u32 > ) > {
274+ fn get_best_block ( & self ) -> AsyncBlockSourceResult < ( BlockHash , Option < u32 > ) > {
269275 Box :: pin ( async move {
270276 if * self . unreachable . lock ( ) . unwrap ( ) {
271277 return Err ( BlockSourceError :: transient ( "Connection refused" ) ) ;
@@ -300,7 +306,7 @@ pub(crate) fn get_random_tx() -> Transaction {
300306 rng. gen_range( 0 ..200 ) ,
301307 ) ,
302308 script_sig: Script :: new( ) ,
303- witness: Vec :: new( ) ,
309+ witness: Witness :: new( ) ,
304310 sequence: 0 ,
305311 } ] ,
306312 output : vec ! [ TxOut {
@@ -356,7 +362,7 @@ pub(crate) fn store_appointment_and_fks_to_db(
356362
357363pub ( crate ) async fn get_last_n_blocks ( chain : & mut Blockchain , n : usize ) -> Vec < ValidatedBlock > {
358364 let tip = chain. tip ( ) ;
359- let mut poller = ChainPoller :: new ( chain, Network :: Bitcoin ) ;
365+ let poller = ChainPoller :: new ( chain, Network :: Bitcoin ) ;
360366
361367 let mut last_n_blocks = Vec :: new ( ) ;
362368 let mut last_known_block = tip;
0 commit comments