ergo_types/block/
header.rs1use ergo_chain_types::{BlockId, Digest32, ADDigest, AutolykosSolution, Votes};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Header {
6 pub version: u8,
7 pub id: BlockId,
8 #[serde(rename = "parentId")]
9 pub parent_id: BlockId,
10 #[serde(rename = "adProofsRoot")]
11 pub ad_proofs_root: Digest32,
12 #[serde(rename = "stateRoot")]
13 pub state_root: ADDigest,
14 #[serde(rename = "transactionsRoot")]
15 pub transaction_root: Digest32,
16 pub timestamp: u64,
17 #[serde(rename = "nBits")]
18 pub n_bits: u64,
19 pub height: u32,
20 #[serde(rename = "extensionHash")]
21 pub extension_root: Digest32,
22 #[serde(rename = "powSolutions")]
23 pub autolykos_solution: AutolykosSolution,
24 pub difficulty: String,
25 #[serde(rename = "extensionId")]
26 pub extension_id: String,
27 #[serde(rename = "transactionsId")]
28 pub transactions_id: String,
29 #[serde(rename = "adProofsId")]
30 pub ad_proofs_id: String,
31 pub votes: Votes,
32 pub size: u64,
33}
34
35impl Header {
36 pub fn new(
37 version: u8,
38 id: BlockId,
39 parent_id: BlockId,
40 ad_proofs_root: Digest32,
41 state_root: ADDigest,
42 transaction_root: Digest32,
43 timestamp: u64,
44 n_bits: u64,
45 height: u32,
46 extension_root: Digest32,
47 autolykos_solution: AutolykosSolution,
48 difficulty: String,
49 extension_id: String,
50 transactions_id: String,
51 ad_proofs_id: String,
52 votes: Votes,
53 size: u64,
54 ) -> Self {
55 Self {
56 version,
57 id,
58 parent_id,
59 ad_proofs_root,
60 state_root,
61 transaction_root,
62 timestamp,
63 n_bits,
64 height,
65 extension_root,
66 autolykos_solution,
67 difficulty,
68 extension_id,
69 transactions_id,
70 ad_proofs_id,
71 votes,
72 size,
73 }
74 }
75}
76
77#[cfg(test)]
78mod test {
79 use anyhow::Result;
80 use crate::block::block::Block;
81 use serde_json::Value;
82
83 const NODE_IP: &str = "213.239.193.208";
84 const NODE_PORT: &str = "9053";
85
86 #[tokio::test]
87 async fn test_header() {
88 let block_id = get_block_id_for_height(1).await.unwrap();
89 let block = get_block_by_id(&block_id).await.unwrap();
90
91 assert_eq!(block.header.height, 1);
92 }
93
94 async fn get_block_id_for_height(height: u64) -> Result<String> {
95 let url = format!("http://{}:{}/blocks/at/{}", NODE_IP, NODE_PORT, height);
96 let response = reqwest::get(&url).await?.text().await?;
97 let response: Value = serde_json::from_str(&response)?;
98 let response = response[0].as_str().unwrap();
99 Ok(response.to_string())
100 }
101
102 async fn get_block_by_id(id: &str) -> Result<Block> {
103 let url = format!("http://{}:{}/blocks/{}", NODE_IP, NODE_PORT, id);
104 let client = reqwest::Client::new();
105 let response = client.get(&url).send().await?.json::<Block>().await?;
106 Ok(response)
107 }
108}