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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/primitives/block.h]
//-------------------------------------------[.cpp/bitcoin/src/primitives/block.cpp]

pub type BlockMap = HashMap<u256,*mut BlockIndex,BlockHasher>;

#[derive(Serialize,Deserialize)]
pub struct Block {

    pub header: BlockHeader,

    /**
      | network and disk
      |
      */
    pub vtx:     Vec<TransactionRef>,

    /**
      | memory only
      |
      */
    #[serde(skip)]
    pub checked: RefCell<bool>,
}

impl RecursiveDynamicUsage for Block {

    fn recursive_dynamic_usage(&self) -> usize {
        
        todo!();
            /*
            size_t mem = memusage::DynamicUsage(block.vtx);
            for (const auto& tx : block.vtx) {
                mem += memusage::DynamicUsage(tx) + RecursiveDynamicUsage(*tx);
            }
            return mem;
            */
    }
}

unsafe impl Send for Block {}
unsafe impl Sync for Block {}

impl Default for Block {
    
    fn default() -> Self {

        let mut x: Self = unsafe { std::mem::zeroed() };

        x.set_null();

        x
    }
}

impl Block {
    
    pub fn new(header: &BlockHeader) -> Self {
    
        let mut x: Self = unsafe { std::mem::zeroed() };

        x.set_null();
        x.header = header.clone();

        x
    }

    delegate!{
        to self.header {
            pub fn get_hash(&self) -> u256;
        }
    }

    pub fn set_null(&mut self)  {
        
        self.header.set_null();
        self.vtx.clear();
        self.checked.replace(false);
    }
    
    pub fn get_block_header<'a>(&'a self) -> &'a BlockHeader {
        &self.header
    }
    
    pub fn to_string(&self) -> String {
        
        let mut s = String::default();

        let mut s = format!{
            "CBlock(hash={}, ver=0x{:08x}, hashPrevBlock={}, hashMerkleRoot={}, nTime={}, nBits={:08x}, nNonce={}, vtx={})\n",
            self.header.get_hash().to_string(),
            self.header.n_version,
            self.header.hash_prev_block.to_string(),
            self.header.hash_merkle_root.to_string(),
            self.header.n_time,
            self.header.n_bits,
            self.header.n_nonce,
            self.vtx.len()
        };

        for tx in self.vtx.iter() {
            s.push_str("  ");
            s.push_str(&tx.get().to_string());
            s.push_str("\n");
        }

         s
    }
}

pub fn decode_hex_blk(
        _0:          &mut Block,
        str_hex_blk: &String) -> bool {
    
    todo!();
        /*
        
        */
}