@@ -3,40 +3,61 @@ use axum::{
33 routing:: get,
44 Json , Router ,
55} ;
6+ use bitcoin:: BlockHash ;
7+ use log:: debug;
68use serde:: { Deserialize , Serialize } ;
79
8- use crate :: { api_paged_response:: ApiPagedResponse , api_query:: PaginationQuery , error:: OceanResult } ;
10+ use crate :: {
11+ api_paged_response:: ApiPagedResponse ,
12+ api_query:: PaginationQuery ,
13+ error:: OceanResult ,
14+ SERVICES ,
15+ repository:: RepositoryOps ,
16+ model:: Block ,
17+ } ;
918
1019#[ derive( Deserialize ) ]
1120struct BlockId {
1221 id : String ,
1322}
1423
15- #[ derive( Deserialize ) ]
16- struct BlockHash {
17- hash : String ,
18- }
19-
2024async fn list_blocks (
2125 Query ( query) : Query < PaginationQuery > ,
2226) -> OceanResult < Json < ApiPagedResponse < Block > > > {
23- // TODO(): query from lvldb.. or maybe pull from index
24- let blocks = vec ! [
25- Block { id: "0" . into( ) } ,
26- Block { id: "1" . into( ) } ,
27- Block { id: "2" . into( ) } ,
28- ] ;
27+ let blocks = SERVICES
28+ . block
29+ . by_height
30+ . list ( None ) ?
31+ . take ( query. size )
32+ . map ( |item| {
33+ let ( _, id) = item?;
34+ let b = SERVICES
35+ . block
36+ . by_id
37+ . get ( & id) ?
38+ . ok_or ( "Missing block index" ) ?;
2939
30- Ok ( Json ( ApiPagedResponse :: of ( blocks, query. size , |block| {
31- block. clone ( ) . id
32- } ) ) )
40+ Ok ( b)
41+ } )
42+ . collect :: < OceanResult < Vec < _ > > > ( ) ?;
43+
44+ Ok ( Json ( ApiPagedResponse :: of (
45+ blocks,
46+ query. size ,
47+ |block| block. clone ( ) . id ,
48+ ) ) )
3349}
3450
35- async fn get_block ( Path ( BlockId { id } ) : Path < BlockId > ) -> OceanResult < Json < Block > > {
36- Ok ( Json ( Block { id } ) )
51+ async fn get_block ( Path ( id) : Path < BlockHash > ) -> OceanResult < Json < Option < Block > > > {
52+ let block = SERVICES
53+ . block
54+ . by_id
55+ . get ( & id) ?;
56+
57+ Ok ( Json ( block) )
3758}
3859
39- async fn get_transactions ( Path ( BlockHash { hash } ) : Path < BlockHash > ) -> String {
60+ async fn get_transactions ( Path ( hash) : Path < BlockHash > ) -> String {
4061 format ! ( "Transactions for block with hash {}" , hash)
4162}
4263
@@ -46,33 +67,3 @@ pub fn router() -> Router {
4667 . route ( "/:id" , get ( get_block) )
4768 . route ( "/:hash/transactions" , get ( get_transactions) )
4869}
49-
50- #[ derive( Clone , Debug , Serialize ) ]
51- #[ serde( default ) ]
52- pub struct Block {
53- id : String ,
54- // TODO(): type mapping
55- // hash: H256,
56- // previous_hash: H256,
57-
58- // height: u64,
59- // version: u64,
60- // time: u64, // ---------------| block time in seconds since epoch
61- // median_time: u64, // --------| meidan time of the past 11 block timestamps
62-
63- // transaction_count: u64,
64-
65- // difficulty: u64,
66-
67- // masternode: H256,
68- // minter: H256,
69- // minter_block_count: u64,
70- // reward: f64
71-
72- // state_modifier: H256,
73- // merkle_root: H256,
74-
75- // size: u64,
76- // size_stripped: u64,
77- // weight: u64,
78- }
0 commit comments