Skip to content
Snippets Groups Projects
Commit 56860f62 authored by Éloïs's avatar Éloïs
Browse files

Merge branch '54-add-blockstamp-method-for-blockdocument' into 'dev'

Resolve "add blockstamp() method for BlockDocument"

Closes #54

See merge request !46
parents 43404420 b3731fb0
No related branches found
No related tags found
1 merge request!46Resolve "add blockstamp() method for BlockDocument"
...@@ -94,7 +94,7 @@ dependencies = [ ...@@ -94,7 +94,7 @@ dependencies = [
[[package]] [[package]]
name = "duniter-documents" name = "duniter-documents"
version = "0.7.0" version = "0.7.1"
dependencies = [ dependencies = [
"base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
......
...@@ -19,7 +19,7 @@ use crypto::digest::Digest; ...@@ -19,7 +19,7 @@ use crypto::digest::Digest;
use crypto::sha2::Sha256; use crypto::sha2::Sha256;
use duniter_crypto::keys::{PrivateKey, ed25519}; use duniter_crypto::keys::{PrivateKey, ed25519};
use Hash; use {BlockHash, BlockId, Blockstamp, Hash};
use blockchain::{BlockchainProtocol, Document, IntoSpecializedDocument}; use blockchain::{BlockchainProtocol, Document, IntoSpecializedDocument};
use blockchain::v10::documents::{TextDocument, V10Document}; use blockchain::v10::documents::{TextDocument, V10Document};
use blockchain::v10::documents::identity::IdentityDocument; use blockchain::v10::documents::identity::IdentityDocument;
...@@ -83,7 +83,7 @@ pub struct BlockDocument { ...@@ -83,7 +83,7 @@ pub struct BlockDocument {
/// Nonce /// Nonce
nonce: u64, nonce: u64,
/// number /// number
number: u64, number: BlockId,
/// Minimal proof of work difficulty /// Minimal proof of work difficulty
pow_min: usize, pow_min: usize,
/// Local time of the block issuer /// Local time of the block issuer
...@@ -110,7 +110,7 @@ pub struct BlockDocument { ...@@ -110,7 +110,7 @@ pub struct BlockDocument {
/// This vector is empty, when the block is generated but the proof of work has not yet started /// This vector is empty, when the block is generated but the proof of work has not yet started
signatures: Vec<ed25519::Signature>, signatures: Vec<ed25519::Signature>,
/// The hash is None, when the block is generated but the proof of work has not yet started /// The hash is None, when the block is generated but the proof of work has not yet started
hash: Option<Hash>, hash: Option<BlockHash>,
/// Currency parameters (only in genesis block) /// Currency parameters (only in genesis block)
parameters: Option<BlockParameters>, parameters: Option<BlockParameters>,
/// Hash of the previous block /// Hash of the previous block
...@@ -142,12 +142,21 @@ pub struct BlockDocument { ...@@ -142,12 +142,21 @@ pub struct BlockDocument {
} }
impl BlockDocument { impl BlockDocument {
fn _compute_inner_hash(&mut self) { /// Return blockstamp
pub fn blockstamp(&self) -> Blockstamp {
Blockstamp {
id: self.number,
hash: self.hash.unwrap(),
}
}
/// Compute inner hash
pub fn compute_inner_hash(&mut self) {
let mut sha256 = Sha256::new(); let mut sha256 = Sha256::new();
sha256.input_str(&self.generate_compact_inner_text()); sha256.input_str(&self.generate_compact_inner_text());
self.inner_hash = Some(Hash::from_hex(&sha256.result_str()).unwrap()); self.inner_hash = Some(Hash::from_hex(&sha256.result_str()).unwrap());
} }
fn _change_nonce(&mut self, new_nonce: u64) { /// Change nonce
pub fn change_nonce(&mut self, new_nonce: u64) {
self.nonce = new_nonce; self.nonce = new_nonce;
self.inner_hash_and_nonce_str = format!( self.inner_hash_and_nonce_str = format!(
"InnerHash: {}\nNonce: {}\n", "InnerHash: {}\nNonce: {}\n",
...@@ -155,10 +164,12 @@ impl BlockDocument { ...@@ -155,10 +164,12 @@ impl BlockDocument {
self.nonce self.nonce
); );
} }
fn _sign(&mut self, privkey: ed25519::PrivateKey) { /// Sign block
pub fn sign(&mut self, privkey: ed25519::PrivateKey) {
self.signatures = vec![privkey.sign(self.inner_hash_and_nonce_str.as_bytes())]; self.signatures = vec![privkey.sign(self.inner_hash_and_nonce_str.as_bytes())];
} }
fn _compute_hash(&mut self) { /// Compute hash
pub fn compute_hash(&mut self) {
let mut sha256 = Sha256::new(); let mut sha256 = Sha256::new();
sha256.input_str(&format!( sha256.input_str(&format!(
"InnerHash: {}\nNonce: {}\n{}\n", "InnerHash: {}\nNonce: {}\n{}\n",
...@@ -166,7 +177,7 @@ impl BlockDocument { ...@@ -166,7 +177,7 @@ impl BlockDocument {
self.nonce, self.nonce,
self.signatures[0] self.signatures[0]
)); ));
self.hash = Some(Hash::from_hex(&sha256.result_str()).unwrap()); self.hash = Some(BlockHash(Hash::from_hex(&sha256.result_str()).unwrap()));
} }
fn generate_compact_inner_text(&self) -> String { fn generate_compact_inner_text(&self) -> String {
let mut identities_str = String::from(""); let mut identities_str = String::from("");
...@@ -322,7 +333,7 @@ mod tests { ...@@ -322,7 +333,7 @@ mod tests {
fn generate_and_verify_empty_block() { fn generate_and_verify_empty_block() {
let mut block = BlockDocument { let mut block = BlockDocument {
nonce: 10_500_000_089_933, nonce: 10_500_000_089_933,
number: 107_777, number: BlockId(107_777),
pow_min: 89, pow_min: 89,
time: 1_522_624_657, time: 1_522_624_657,
median_time: 1_522_616_790, median_time: 1_522_616_790,
...@@ -352,7 +363,7 @@ mod tests { ...@@ -352,7 +363,7 @@ mod tests {
inner_hash_and_nonce_str: String::new(), inner_hash_and_nonce_str: String::new(),
}; };
// test inner_hash computation // test inner_hash computation
block._compute_inner_hash(); block.compute_inner_hash();
println!("{}", block.generate_compact_text()); println!("{}", block.generate_compact_text());
assert_eq!( assert_eq!(
block.inner_hash.unwrap().to_hex(), block.inner_hash.unwrap().to_hex(),
...@@ -388,12 +399,12 @@ InnerHash: 95948AC4D45E46DA07CE0713EDE1CE0295C227EE4CA5557F73F56B7DD46FE89C ...@@ -388,12 +399,12 @@ InnerHash: 95948AC4D45E46DA07CE0713EDE1CE0295C227EE4CA5557F73F56B7DD46FE89C
Nonce: " Nonce: "
); );
// Test signature validity // Test signature validity
block._change_nonce(10_500_000_089_933); block.change_nonce(10_500_000_089_933);
assert_eq!(block.verify_signatures(), VerificationResult::Valid()); assert_eq!(block.verify_signatures(), VerificationResult::Valid());
// Test hash computation // Test hash computation
block._compute_hash(); block.compute_hash();
assert_eq!( assert_eq!(
block.hash.unwrap().to_hex(), block.hash.unwrap().0.to_hex(),
"000002D3296A2D257D01F6FEE8AEC5C3E5779D04EA43F08901F41998FA97D9A1" "000002D3296A2D257D01F6FEE8AEC5C3E5779D04EA43F08901F41998FA97D9A1"
); );
} }
...@@ -457,7 +468,7 @@ a9PHPuSfw7jW8FRQHXFsGi/bnLjbtDnTYvEVgUC9u0WlR7GVofa+Xb+l5iy6NwuEXiwvueAkf08wPVY8 ...@@ -457,7 +468,7 @@ a9PHPuSfw7jW8FRQHXFsGi/bnLjbtDnTYvEVgUC9u0WlR7GVofa+Xb+l5iy6NwuEXiwvueAkf08wPVY8
let mut block = BlockDocument { let mut block = BlockDocument {
nonce: 0, nonce: 0,
number: 107_984, number: BlockId(107_984),
pow_min: 88, pow_min: 88,
time: 1_522_685_861, time: 1_522_685_861,
median_time: 1522683184, median_time: 1522683184,
...@@ -487,7 +498,7 @@ a9PHPuSfw7jW8FRQHXFsGi/bnLjbtDnTYvEVgUC9u0WlR7GVofa+Xb+l5iy6NwuEXiwvueAkf08wPVY8 ...@@ -487,7 +498,7 @@ a9PHPuSfw7jW8FRQHXFsGi/bnLjbtDnTYvEVgUC9u0WlR7GVofa+Xb+l5iy6NwuEXiwvueAkf08wPVY8
inner_hash_and_nonce_str: String::new(), inner_hash_and_nonce_str: String::new(),
}; };
// test inner_hash computation // test inner_hash computation
block._compute_inner_hash(); block.compute_inner_hash();
println!("{}", block.generate_compact_text()); println!("{}", block.generate_compact_text());
assert_eq!( assert_eq!(
block.inner_hash.unwrap().to_hex(), block.inner_hash.unwrap().to_hex(),
...@@ -540,12 +551,12 @@ InnerHash: C8AB69E33ECE2612EADC7AB30D069B1F1A3D8C95EBBFD50DE583AC8E3666CCA1 ...@@ -540,12 +551,12 @@ InnerHash: C8AB69E33ECE2612EADC7AB30D069B1F1A3D8C95EBBFD50DE583AC8E3666CCA1
Nonce: " Nonce: "
); );
// Test signature validity // Test signature validity
block._change_nonce(10_300_000_018_323); block.change_nonce(10_300_000_018_323);
assert_eq!(block.verify_signatures(), VerificationResult::Valid()); assert_eq!(block.verify_signatures(), VerificationResult::Valid());
// Test hash computation // Test hash computation
block._compute_hash(); block.compute_hash();
assert_eq!( assert_eq!(
block.hash.unwrap().to_hex(), block.hash.unwrap().0.to_hex(),
"000004F8B84A3590243BA562E5F2BA379F55A0B387C5D6FAC1022DFF7FFE6014" "000004F8B84A3590243BA562E5F2BA379F55A0B387C5D6FAC1022DFF7FFE6014"
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment