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
//  Copyright (C) 2017-2019  The AXIOM TEAM Association.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Wrappers around Block document.

pub mod v10;

pub use v10::{BlockDocumentV10, BlockDocumentV10Stringified};

use crate::blockstamp::Blockstamp;
use crate::{BlockHash, BlockNumber, Document, ToStringObject};
use dup_crypto::hashs::Hash;
use dup_crypto::keys::{PrivKey, PubKey, PublicKey};

/// Wrap a Block document.
///
/// Must be created by parsing a text document or using a builder.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum BlockDocument {
    V10(BlockDocumentV10),
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// Error when verifying a hash of a block
pub enum VerifyBlockHashError {
    /// The hash is missing
    MissingHash { block_number: BlockNumber },
    /// Hash is invalid
    InvalidHash {
        block_number: BlockNumber,
        expected_hash: Hash,
        actual_hash: Hash,
    },
}

pub trait BlockDocumentTrait {
    /// Common time in block (also known as 'blockchain time')
    fn common_time(&self) -> u64;
    /// Compute hash
    fn compute_hash(&self) -> BlockHash;
    /// Compute inner hash
    fn compute_inner_hash(&self) -> Hash {
        Hash::compute_str(&self.generate_compact_inner_text())
    }
    /// Compute the character string that will be hashed
    fn compute_will_hashed_string(&self) -> String;
    /// Compute the character string that will be signed
    fn compute_will_signed_string(&self) -> String;
    /// Get current frame size (in blocks)
    fn current_frame_size(&self) -> usize;
    /// Generate compact inner text (for compute inner_hash)
    fn generate_compact_inner_text(&self) -> String;
    /// Compute hash and save it in document
    fn generate_hash(&mut self);
    /// Compute inner hash and save it in document
    fn generate_inner_hash(&mut self);
    /// Get block hash
    fn hash(&self) -> Option<BlockHash>;
    /// Increment nonce
    fn increment_nonce(&mut self);
    /// Get block inner hash
    fn inner_hash(&self) -> Option<Hash>;
    /// Get number of compute members in the current frame
    fn issuers_count(&self) -> usize;
    /// Get block number
    fn number(&self) -> BlockNumber;
    /// Get previous hash
    fn previous_hash(&self) -> Option<Hash>;
    /// Get previous blockstamp
    fn previous_blockstamp(&self) -> Blockstamp;
    /// Lightens the block (for example to store it while minimizing the space required)
    fn reduce(&mut self);
    /// Verify inner hash
    fn verify_inner_hash(&self) -> Result<(), VerifyBlockHashError>;
    /// Verify block hash
    fn verify_hash(&self) -> Result<(), VerifyBlockHashError>;
    /// Sign block
    fn sign(&mut self, privkey: PrivKey);
}

impl BlockDocumentTrait for BlockDocument {
    #[inline]
    fn compute_hash(&self) -> BlockHash {
        match self {
            BlockDocument::V10(block) => block.compute_hash(),
        }
    }
    #[inline]
    fn compute_will_hashed_string(&self) -> String {
        match self {
            BlockDocument::V10(block) => block.compute_will_hashed_string(),
        }
    }
    #[inline]
    fn compute_will_signed_string(&self) -> String {
        match self {
            BlockDocument::V10(block) => block.compute_will_signed_string(),
        }
    }
    #[inline]
    fn current_frame_size(&self) -> usize {
        match self {
            BlockDocument::V10(block) => block.current_frame_size(),
        }
    }
    #[inline]
    fn generate_compact_inner_text(&self) -> String {
        match self {
            BlockDocument::V10(block) => block.generate_compact_inner_text(),
        }
    }
    #[inline]
    fn generate_hash(&mut self) {
        match self {
            BlockDocument::V10(block) => block.generate_hash(),
        }
    }
    #[inline]
    fn generate_inner_hash(&mut self) {
        match self {
            BlockDocument::V10(block) => block.generate_inner_hash(),
        }
    }
    #[inline]
    fn hash(&self) -> Option<BlockHash> {
        match self {
            BlockDocument::V10(block) => block.hash(),
        }
    }
    #[inline]
    fn increment_nonce(&mut self) {
        match self {
            BlockDocument::V10(block) => block.increment_nonce(),
        }
    }
    #[inline]
    fn inner_hash(&self) -> Option<Hash> {
        match self {
            BlockDocument::V10(block) => block.inner_hash(),
        }
    }
    #[inline]
    fn issuers_count(&self) -> usize {
        match self {
            BlockDocument::V10(block) => block.issuers_count(),
        }
    }
    #[inline]
    fn common_time(&self) -> u64 {
        match self {
            BlockDocument::V10(block) => block.common_time(),
        }
    }
    #[inline]
    fn number(&self) -> BlockNumber {
        match self {
            BlockDocument::V10(block) => block.number(),
        }
    }
    #[inline]
    fn previous_blockstamp(&self) -> Blockstamp {
        match self {
            BlockDocument::V10(block) => block.previous_blockstamp(),
        }
    }
    #[inline]
    fn previous_hash(&self) -> Option<Hash> {
        match self {
            BlockDocument::V10(block) => block.previous_hash(),
        }
    }
    #[inline]
    fn reduce(&mut self) {
        match self {
            BlockDocument::V10(block) => block.reduce(),
        }
    }
    #[inline]
    fn verify_inner_hash(&self) -> Result<(), VerifyBlockHashError> {
        match self {
            BlockDocument::V10(block) => block.verify_inner_hash(),
        }
    }
    #[inline]
    fn verify_hash(&self) -> Result<(), VerifyBlockHashError> {
        match self {
            BlockDocument::V10(block) => block.verify_hash(),
        }
    }
    #[inline]
    fn sign(&mut self, privkey: PrivKey) {
        match self {
            BlockDocument::V10(block) => block.sign(privkey),
        }
    }
}

impl Document for BlockDocument {
    type PublicKey = PubKey;

    fn version(&self) -> u16 {
        match self {
            BlockDocument::V10(_) => 10u16,
        }
    }

    fn currency(&self) -> &str {
        match self {
            BlockDocument::V10(block) => block.currency(),
        }
    }

    fn blockstamp(&self) -> Blockstamp {
        match self {
            BlockDocument::V10(block) => block.blockstamp(),
        }
    }

    fn issuers(&self) -> &Vec<Self::PublicKey> {
        match self {
            BlockDocument::V10(block) => block.issuers(),
        }
    }

    fn signatures(&self) -> &Vec<<Self::PublicKey as PublicKey>::Signature> {
        match self {
            BlockDocument::V10(block) => block.signatures(),
        }
    }

    fn as_bytes(&self) -> &[u8] {
        match self {
            BlockDocument::V10(block) => block.as_bytes(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum BlockDocumentStringified {
    V10(BlockDocumentV10Stringified),
}

impl ToStringObject for BlockDocument {
    type StringObject = BlockDocumentStringified;

    fn to_string_object(&self) -> Self::StringObject {
        match self {
            BlockDocument::V10(block) => BlockDocumentStringified::V10(block.to_string_object()),
        }
    }
}