redgold_schema/block.rs
1use crate::helpers::with_metadata_hashable::WithMetadataHashableFields;
2use crate::structs::{Block, ErrorInfo, StructMetadata};
3use crate::HashClear;
4use crate::SafeOption;
5
6impl HashClear for Block {
7 fn hash_clear(&mut self) {
8 self.transactions.clear();
9 if let Some(sm) = self.struct_metadata.as_mut() {
10 sm.hash_clear()
11 }
12 }
13}
14
15impl WithMetadataHashableFields for Block {
16 fn struct_metadata_opt(&mut self) -> Option<&mut StructMetadata> {
17 self.struct_metadata.as_mut()
18 }
19
20 fn struct_metadata_opt_ref(&self) -> Option<&StructMetadata> {
21 self.struct_metadata.as_ref()
22 }
23}
24
25impl Block {
26
27 pub fn time(&self) -> Result<i64, ErrorInfo> {
28 Ok(self
29 .struct_metadata
30 .safe_get()?
31 .time
32 .safe_get()?
33 .clone()
34 )
35 }
36
37 // pub fn from(transactions: Vec<Transaction>, last_time: i64, ) {
38 // if let Some(txa) = txs {
39 // // TODO: re-query observation edge here.
40 // let leafs = transactions
41 // .clone()
42 // .iter()
43 // .map(|e| e.hash_vec().clone())
44 // .collect_vec();
45 // let mut block = Block {
46 // // TODO: use a real merkle root here
47 // merkle_root: Some(rg_merkle::build_root_simple(&leafs)),
48 // transactions: vec, // TODO: can leave this blank and enforce it properly
49 // // to remove the clone on hash calculation? That's clever do it as part
50 // // of a constructor function.
51 // struct_metadata: struct_metadata(last_time as i64),
52 // previous_block_hash: self.last_block.hash.clone(),
53 // metadata: None,
54 // hash: None,
55 // height: self.last_block.height + 1,
56 // }
57 // .with_hash();
58 // }
59}
60
61#[test]
62fn verify_hash_equivalent_schema_change() {
63 let block = Block {
64 merkle_root: None,
65 transactions: vec![],
66 struct_metadata: None,
67 previous_block_hash: None,
68 metadata: None,
69 height: 0,
70 };
71 // before adding 'test_field: Option<String>'
72 // 1440a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26
73 // println!("Hash hex empty block: {}", block.hash_hex().expect(""));
74 // same hash after adding test_field: None to schema, verifying we can evolve schema while
75 // keeping hashes consistent so long as all fields are None at first.
76}