-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathblock.rs
More file actions
84 lines (68 loc) · 1.91 KB
/
Copy pathblock.rs
File metadata and controls
84 lines (68 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use axum::{debug_handler, extract::{Path, Query}, Json, routing::get, Router};
use serde::{Deserialize, Serialize};
use crate::api_paged_response::ApiPagedResponse;
#[derive(Deserialize)]
struct BlockId {
id: String,
}
#[derive(Deserialize)]
struct BlockHash {
hash: String,
}
#[derive(Deserialize)]
pub struct ListBlocksQuery {
pub size: usize,
pub next: Option<String>
}
#[debug_handler]
async fn list_blocks(Query(query): Query<ListBlocksQuery>) -> Json<ApiPagedResponse<Block>> {
// TODO(): query from db
// db::block::list(req).await...
let blocks = vec![
Block { id: "0".into() },
Block { id: "1".into() },
Block { id: "2".into() },
];
Json(ApiPagedResponse
::of(
blocks,
query.size,
|block| block.clone().id
)
)
}
async fn get_block(Path(BlockId { id }): Path<BlockId>) -> String {
format!("Details of block with id {}", id)
}
async fn get_transactions(Path(BlockHash { hash }): Path<BlockHash>) -> String {
format!("Transactions for block with hash {}", hash)
}
pub fn router() -> Router {
Router::new()
.route("/", get(list_blocks))
.route("/:id", get(get_block))
.route("/:hash/transactions", get(get_transactions))
}
#[derive(Clone, Debug, Serialize)]
#[serde(default)]
pub struct Block {
id: String,
// TODO(): type mapping
// hash: H256,
// previous_hash: H256,
// height: u64,
// version: u64,
// time: u64, // ---------------| block time in seconds since epoch
// median_time: u64, // --------| meidan time of the past 11 block timestamps
// transaction_count: u64,
// difficulty: u64,
// masternode: H256,
// minter: H256,
// minter_block_count: u64,
// reward: f64
// state_modifier: H256,
// merkle_root: H256,
// size: u64,
// size_stripped: u64,
// weight: u64,
}