Skip to main content

alephium_web3/block/
mod.rs

1// Copyright 2018 - 2022 The Alephium Authors
2// This file is part of the alephium project.
3
4use serde::{Deserialize, Serialize};
5
6/// Block header information
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct BlockHeader {
9    pub hash: String,
10    pub timestamp: u64,
11    pub height: u32,
12    #[serde(rename = "chainFrom")]
13    pub chain_from: u8,
14    #[serde(rename = "chainTo")]
15    pub chain_to: u8,
16    #[serde(rename = "parentHash")]
17    pub parent_hash: String,
18    pub nonce: String,
19    pub version: u8,
20    #[serde(rename = "depStateHash")]
21    pub dep_state_hash: String,
22    #[serde(rename = "txsHash")]
23    pub txs_hash: String,
24    pub target: String,
25}
26
27/// Block with transactions
28#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct Block {
30    pub hash: String,
31    pub timestamp: u64,
32    pub height: u32,
33    #[serde(rename = "chainFrom")]
34    pub chain_from: u8,
35    #[serde(rename = "chainTo")]
36    pub chain_to: u8,
37    pub transactions: Vec<BlockTransaction>,
38    #[serde(rename = "parentHash")]
39    pub parent_hash: String,
40    pub nonce: String,
41}
42
43/// Transaction in a block
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct BlockTransaction {
46    pub hash: String,
47    pub inputs: Vec<TransactionInput>,
48    pub outputs: Vec<TransactionOutput>,
49    #[serde(rename = "gasAmount")]
50    pub gas_amount: u32,
51    #[serde(rename = "gasPrice")]
52    pub gas_price: String,
53}
54
55/// Transaction input
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct TransactionInput {
58    #[serde(rename = "outputRef")]
59    pub output_ref: OutputRef,
60    #[serde(rename = "unlockScript")]
61    pub unlock_script: String,
62}
63
64/// Output reference
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct OutputRef {
67    pub key: String,
68    pub hint: i32,
69}
70
71/// Transaction output
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct TransactionOutput {
74    #[serde(rename = "type")]
75    pub output_type: String,
76    pub address: String,
77    #[serde(rename = "attoAlphAmount")]
78    pub atto_alph_amount: String,
79    pub tokens: Option<Vec<TokenOutput>>,
80    #[serde(rename = "lockTime")]
81    pub lock_time: Option<u64>,
82    pub message: Option<String>,
83}
84
85/// Token in output
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct TokenOutput {
88    pub id: String,
89    pub amount: String,
90}
91
92impl Block {
93    /// Get transaction count
94    pub fn transaction_count(&self) -> usize {
95        self.transactions.len()
96    }
97
98    /// Check if block is from specific chain
99    pub fn is_from_chain(&self, chain: u8) -> bool {
100        self.chain_from == chain
101    }
102
103    /// Check if block is to specific chain
104    pub fn is_to_chain(&self, chain: u8) -> bool {
105        self.chain_to == chain
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    fn test_block_transaction_count() {
115        let block = Block {
116            hash: "test_hash".to_string(),
117            timestamp: 1234567890,
118            height: 100,
119            chain_from: 0,
120            chain_to: 1,
121            transactions: vec![],
122            parent_hash: "parent_hash".to_string(),
123            nonce: "nonce".to_string(),
124        };
125        assert_eq!(block.transaction_count(), 0);
126    }
127
128    #[test]
129    fn test_block_chain_check() {
130        let block = Block {
131            hash: "test_hash".to_string(),
132            timestamp: 1234567890,
133            height: 100,
134            chain_from: 0,
135            chain_to: 1,
136            transactions: vec![],
137            parent_hash: "parent_hash".to_string(),
138            nonce: "nonce".to_string(),
139        };
140        assert!(block.is_from_chain(0));
141        assert!(!block.is_from_chain(1));
142        assert!(block.is_to_chain(1));
143        assert!(!block.is_to_chain(0));
144    }
145
146    #[test]
147    fn test_block_serialization() {
148        let block = Block {
149            hash: "abc123".to_string(),
150            timestamp: 1234567890,
151            height: 100,
152            chain_from: 0,
153            chain_to: 1,
154            transactions: vec![],
155            parent_hash: "parent".to_string(),
156            nonce: "nonce_value".to_string(),
157        };
158        let json = serde_json::to_string(&block).unwrap();
159        assert!(json.contains("abc123"));
160        assert!(json.contains("1234567890"));
161    }
162}