1
+ use hex;
1
2
use std:: convert:: TryInto ;
2
3
3
- use bitcoin:: { BlockHash , TxOut } ;
4
- use bitcoin:: blockdata:: block:: Block ;
5
- use bitcoin:: hashes:: Hash ;
4
+ use bitcoin:: { BlockHash , TxOut , Transaction } ;
5
+ use bitcoin:: consensus:: deserialize;
6
6
use lightning:: chain;
7
7
use lightning:: chain:: AccessError ;
8
- use lightning_block_sync:: { BlockData , BlockSource } ;
9
8
use lightning_block_sync:: http:: BinaryResponse ;
10
9
use lightning_block_sync:: rest:: RestClient ;
11
10
@@ -20,32 +19,42 @@ struct RestBinaryResponse(Vec<u8>);
20
19
impl ChainVerifier {
21
20
pub ( crate ) fn new ( ) -> Self {
22
21
ChainVerifier {
23
- rest_client : RestClient :: new ( config:: bitcoin_rest_endpoint ( ) ) . unwrap ( ) ,
22
+ rest_client : RestClient :: new ( config:: middleware_rest_endpoint ( ) ) . unwrap ( ) ,
24
23
}
25
24
}
26
25
27
- fn retrieve_block ( & self , block_height : u32 ) -> Result < Block , AccessError > {
26
+ fn retrieve_tx ( & self , block_height : u32 , transaction_index : u32 ) -> Result < Transaction , AccessError > {
28
27
tokio:: task:: block_in_place ( move || { tokio:: runtime:: Handle :: current ( ) . block_on ( async move {
29
- let uri = format ! ( "blockhashbyheight /{}.bin " , block_height) ;
30
- let block_hash_result =
28
+ let uri = format ! ( "getTransaction /{}/{} " , block_height, transaction_index ) ;
29
+ let tx_result =
31
30
self . rest_client . request_resource :: < BinaryResponse , RestBinaryResponse > ( & uri) . await ;
32
- let block_hash : Vec < u8 > = block_hash_result . map_err ( |error| {
33
- eprintln ! ( "Could't find block hash at height {}: {}" , block_height, error. to_string( ) ) ;
31
+ let tx_hex_in_bytes : Vec < u8 > = tx_result . map_err ( |error| {
32
+ eprintln ! ( "Could't find transaction at height {} and pos {} : {}" , block_height, transaction_index , error. to_string( ) ) ;
34
33
AccessError :: UnknownChain
35
34
} ) ?. 0 ;
36
- let block_hash = BlockHash :: from_slice ( & block_hash) . unwrap ( ) ;
37
35
38
- let block_result = self . rest_client . get_block ( & block_hash) . await ;
39
- match block_result {
40
- Ok ( BlockData :: FullBlock ( block) ) => {
41
- Ok ( block)
42
- } ,
43
- Ok ( _) => unreachable ! ( ) ,
44
- Err ( error) => {
45
- eprintln ! ( "Couldn't retrieve block {}: {:?} ({})" , block_height, error, block_hash) ;
46
- Err ( AccessError :: UnknownChain )
47
- }
48
- }
36
+ let tx_hex_in_string =
37
+ String :: from_utf8 ( tx_hex_in_bytes)
38
+ . map_err ( |non_utf8| String :: from_utf8_lossy ( non_utf8. as_bytes ( ) ) . into_owned ( ) )
39
+ . unwrap ( ) ;
40
+
41
+ let tx_bytes =
42
+ hex:: decode ( tx_hex_in_string)
43
+ . map_err ( |error| {
44
+ eprintln ! ( "Could't find transaction at height {} and pos {}: {}" , block_height, transaction_index, error. to_string( ) ) ;
45
+ AccessError :: UnknownChain
46
+ } )
47
+ . unwrap ( ) ;
48
+
49
+ let transaction =
50
+ deserialize :: < Transaction > ( tx_bytes. as_slice ( ) )
51
+ . map_err ( |error| {
52
+ eprintln ! ( "Could't find transaction at height {} and pos {}: {}" , block_height, transaction_index, error. to_string( ) ) ;
53
+ AccessError :: UnknownChain
54
+ } )
55
+ . unwrap ( ) ;
56
+
57
+ Ok ( transaction)
49
58
} ) } )
50
59
}
51
60
}
@@ -56,11 +65,8 @@ impl chain::Access for ChainVerifier {
56
65
let transaction_index = ( ( short_channel_id >> 2 * 8 ) & 0xffffff ) as u32 ;
57
66
let output_index = ( short_channel_id & 0xffff ) as u16 ;
58
67
59
- let block = self . retrieve_block ( block_height) ?;
60
- let transaction = block. txdata . get ( transaction_index as usize ) . ok_or_else ( || {
61
- eprintln ! ( "Transaction index {} out of bounds in block {} ({})" , transaction_index, block_height, block. block_hash( ) . to_string( ) ) ;
62
- AccessError :: UnknownTx
63
- } ) ?;
68
+ let transaction = self . retrieve_tx ( block_height, transaction_index) ?;
69
+
64
70
let output = transaction. output . get ( output_index as usize ) . ok_or_else ( || {
65
71
eprintln ! ( "Output index {} out of bounds in transaction {}" , output_index, transaction. txid( ) . to_string( ) ) ;
66
72
AccessError :: UnknownTx
0 commit comments