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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 Copyright (c) 2022 ParallelChain Lab
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

 use std::mem;
use std::thread::{self, JoinHandle};
use crate::encodings::{
    fmt_sizes, 
    fmt_blockheader, 
    serialize,
    deserialize_u32, 
    deserialize_u64, 
    deserialize_32bytes,
    deserialize_64bytes};
use crate::{Transaction, Receipt, crypto, Serializable, Deserializable, Error, ErrorKind};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block {
    pub header : BlockHeader,
    pub transactions : Vec<Transaction>,
    pub receipts : Vec<Receipt>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[deprecated="No necessary use cases. Please consider using Vec<Vec<u8> or Vec<Block> directly."]
pub struct Blocks {
    pub blocks : Vec<Block>
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockHeader {
    pub blockchain_id : u64,
    pub block_version_number :u64,
    /// Unix timestamp
    pub timestamp :u32, 
    pub prev_block_hash : crypto::Sha256Hash,
    pub this_block_hash : crypto::Sha256Hash,
    pub txs_hash : crypto::Sha256Hash,
    pub state_hash : crypto::Sha256Hash,
    pub receipts_hash : crypto::Sha256Hash,
    pub proposer_public_key  : crypto::PublicAddress,
    pub signature : crypto::Signature
}

/// unsafe pointer to be shared with threads
struct Ptr<T>(*const T);
unsafe impl<T> Send for Ptr<T> {}

impl Serializable for Block {

    fn serialize<'a>(msg: &'a Block) -> Vec<u8> {
        let raw_blockheader = BlockHeader::serialize(&msg.header);

        // transactions serialization
        let tx_ptr = Ptr(&msg.transactions as *const Vec<Transaction>);
        let tx_join_handle : JoinHandle<Vec<u8>> = thread::spawn(move || {
            let transactions = unsafe {&*tx_ptr.0};
            // allocate all memory once. Avoid allocating memory in iterations because it slows down the process
            let total_size = transactions.iter().map(|t|{
                Transaction::size_of(t)
            }).sum();
            let mut return_slice = vec![0u8; total_size];
            // serialize sequentially
            let mut pos :usize = 0;
            transactions.iter().for_each(|t| {
                pos += Transaction::serialize_to_slice(t, &mut return_slice[pos..]);
            });
            return_slice
        });

        // receipts serialization
        let recp_ptr = Ptr(&msg.receipts as *const Vec<Receipt>);
        let recp_join_handle : JoinHandle<Vec<u8>> = thread::spawn(move || {
            let receipts = unsafe {&*recp_ptr.0};
            // allocate all memory once. Avoid allocating memory in iterations because it slows down the process
            let total_size :usize = receipts.iter().map(|r|{
                Receipt::size_of(r)
            }).sum();
            let mut return_slice = vec![0u8; total_size];
            // serialize sequentially
            let mut pos :usize = 0;
            receipts.iter().for_each(|r| {
                pos += Receipt::serialize_to_slice(r, &mut return_slice[pos..]);
            });
            return_slice
        });

        // join all serialization threads for in sequential order
        let tx_data = tx_join_handle.join().unwrap();
        let tx_size = (tx_data.len() as u32).to_le_bytes().to_vec();

        let recp_data = recp_join_handle.join().unwrap();
        let recp_size = (recp_data.len() as u32).to_le_bytes().to_vec();

        // return serialized Block
        [
            raw_blockheader, 
            [tx_size, recp_size].concat(),
            [tx_data, recp_data].concat()
        ].concat()
    }
}

impl Deserializable<Block> for Block {
    
    fn deserialize(buf: &[u8]) -> Result<Block, Error> {
        let blockheader = BlockHeader::deserialize(buf)?;

        let blockdata_offset = fmt_blockheader::BASESIZE as usize;

        // decode size of transactions and receipts
        let buf = &buf[blockdata_offset..];
        if buf.len() < fmt_sizes::BASESIZE {
            return Err(Error::new(ErrorKind::IncorrectLength));
        }
        let tx_size = deserialize_u32!(buf, fmt_sizes::TRANSACTION) as usize;
        let recp_size = deserialize_u32!(buf, fmt_sizes::RECEIPT) as usize;

        // get slices for transactions and receipts
        let buf = &buf[fmt_sizes::BASESIZE..];
        if buf.len() < tx_size + recp_size {
            return Err(Error::new(ErrorKind::IncorrectLength));
        }
        let (raw_transactions, raw_receipts) = buf.split_at(tx_size);

        // deserialize transactions slice
        let raw_transactions = raw_transactions.to_vec(); // convert to vec for passing the content to thread
        let tx_join_handle = thread::spawn(move ||{
            let mut transactions = vec![];
            let mut pos = 0usize;
            while pos < raw_transactions.len() {
                let raw_buf = &raw_transactions[pos..];
                let size_of_i_th_tx = Transaction::size_from_slice(raw_buf)?;
                transactions.push(
                    Transaction::deserialize(&raw_buf[..size_of_i_th_tx])?
                );
                pos += size_of_i_th_tx;
            }
            Ok(transactions)
        });

        // deserialize receipts slice
        let raw_receipts = raw_receipts.to_vec(); // convert to vec for passing the content to thread
        let recp_join_handle = thread::spawn(move || {
            let mut receipts = vec![];
            let mut pos = 0usize;
            while pos < raw_receipts.len() {
                let raw_buf = &raw_receipts[pos..];
                let size_of_i_th_recp = Receipt::size_from_slice(&raw_buf)?;
                receipts.push(
                    Receipt::deserialize(&raw_buf[..size_of_i_th_recp])?
                );
                pos += size_of_i_th_recp;
            }
            Ok(receipts)
        });

        let transactions = tx_join_handle.join().unwrap()?;
        let receipts = recp_join_handle.join().unwrap()?;

        Ok(Block{
            header: blockheader,
            transactions,
            receipts
        })
    }

}

#[allow(deprecated)]
impl Serializable for Blocks {

    fn serialize(msg: &Blocks) -> Vec<u8> {
        let blocks :Vec<u8> = msg.blocks.iter().flat_map(|block|{
            let serialized_block = Block::serialize(&block);
            let block_size = serialized_block.len();
            let mut entry_bytes = vec![0u8; mem::size_of::<u32>() + block_size];
            entry_bytes[0..mem::size_of::<u32>()].copy_from_slice((block_size as u32).to_le_bytes().as_slice());
            entry_bytes[mem::size_of::<u32>()..].copy_from_slice(serialized_block.as_slice());
            entry_bytes
        }).collect();
        blocks
    }

}

#[allow(deprecated)]
impl Deserializable<Blocks> for Blocks {

    fn deserialize(buf: &[u8]) -> Result<Blocks, Error> {
        let mut blocks :Vec<Block> = vec![];
        let mut pos = 0usize;
        while pos < buf.len() {
            // get the size of block
            if buf.len() < pos + mem::size_of::<u32>() {
                return Err(Error::new(ErrorKind::IncorrectLength));
            }
            let block_size ={
                let mut bs = [0u8; mem::size_of::<u32>()];
                bs.copy_from_slice(& buf[pos..pos+mem::size_of::<u32>()]);
                u32::from_le_bytes(bs)
            };

            // get the block
            if buf.len() < pos + mem::size_of::<u32>() + block_size as usize {
                return Err(Error::new(ErrorKind::IncorrectLength));
            }
            let block = Block::deserialize(&buf[pos+mem::size_of::<u32>()..pos+mem::size_of::<u32>()+block_size as usize])?;
            blocks.push(block);
            pos += mem::size_of::<u32>() + block_size as usize;
        }

        Ok(Blocks {
            blocks
        })
    }

}

impl Serializable for BlockHeader {
    fn serialize(msg: &BlockHeader) -> Vec<u8> {

        // declare space to contains the bytes
        let mut ret :Vec<u8> = vec![0u8; fmt_blockheader::BASESIZE];

        // serialize fixed size data, the fields in msg, one-by-one
        serialize!(
            fmt_blockheader::ID          | msg.blockchain_id.to_le_bytes().as_slice(),
            fmt_blockheader::VERSION     | msg.block_version_number.to_le_bytes().as_slice(),
            fmt_blockheader::TIMESTAMP   | msg.timestamp.to_le_bytes().as_slice(),
            fmt_blockheader::PREVHASH    | msg.prev_block_hash.as_slice(),
            fmt_blockheader::BLOCKHASH   | msg.this_block_hash.as_slice(),
            fmt_blockheader::STATEHASH   | msg.state_hash.as_slice(),
            fmt_blockheader::TXSHASH     | msg.txs_hash.as_slice(),
            fmt_blockheader::RECEIPTHASH | msg.receipts_hash.as_slice(),
            fmt_blockheader::PUBKEY      | msg.proposer_public_key.as_slice(),
            fmt_blockheader::SIGNATURE   | msg.signature.as_slice()
            => ret
        );

        ret
    }

}

impl Deserializable<BlockHeader> for BlockHeader {
    fn deserialize(buf: &[u8]) -> Result<BlockHeader, Error> {
        // deserialize fixed size data
        if buf.len() < fmt_blockheader::BASESIZE {
            return Err(Error::new(ErrorKind::IncorrectLength))
        }
        let (
            blockchain_id,
            block_version_number,
            timestamp,
            prev_block_hash,
            this_block_hash,
            state_hash,
            txs_hash,
            receipts_hash,
            proposer_public_key,
            signature
        ) = (
            deserialize_u64!(buf, fmt_blockheader::ID),
            deserialize_u64!(buf, fmt_blockheader::VERSION),
            deserialize_u32!(buf, fmt_blockheader::TIMESTAMP),
            deserialize_32bytes!(buf, fmt_blockheader::PREVHASH),
            deserialize_32bytes!(buf, fmt_blockheader::BLOCKHASH),
            deserialize_32bytes!(buf, fmt_blockheader::STATEHASH),
            deserialize_32bytes!(buf, fmt_blockheader::TXSHASH),
            deserialize_32bytes!(buf, fmt_blockheader::RECEIPTHASH),
            deserialize_32bytes!(buf, fmt_blockheader::PUBKEY),
            deserialize_64bytes!(buf, fmt_blockheader::SIGNATURE)
        );

        Ok(BlockHeader {
            blockchain_id,
            block_version_number,
            timestamp,
            prev_block_hash,
            this_block_hash,
            txs_hash,
            state_hash,
            receipts_hash,
            proposer_public_key,
            signature
        })
    }
}