data_chain/node_block.rs
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
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net
// Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3,
// depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project
// generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.
// This, along with the
// Licenses can be found in the root directory of this project at LICENSE,
// COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network
// Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions
// and limitations
// relating to use of the SAFE Network Software.
use maidsafe_utilities::serialisation;
use sodiumoxide::crypto::sign::{Signature, PublicKey, SecretKey};
use sodiumoxide::crypto;
use block_identifier::{BlockIdentifier, LinkDescriptor};
use error::Error;
/// Descriptor is the xored group members starting with base of 0000..:32
/// This process is faster than hash and means group can be unordered
/// which is beneficial under heavy churn and out of order links being sent.
pub fn create_link_descriptor(group: &[PublicKey]) -> LinkDescriptor {
let mut base = [0u8; 32];
for key in group.iter() {
for item in key.0.iter().cloned().enumerate() {
base[item.0] ^= item.1
}
}
base
}
/// Proof as provided by a close group member
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug, Clone)]
pub struct NodeBlockProof {
key: PublicKey,
sig: Signature,
}
impl NodeBlockProof {
/// cstr
pub fn new(key: PublicKey, sig: Signature) -> NodeBlockProof {
NodeBlockProof {
key: key,
sig: sig,
}
}
/// getter
pub fn key(&self) -> &PublicKey {
&self.key
}
/// getter
pub fn sig(&self) -> &Signature {
&self.sig
}
}
/// If data block then this is sent by any group member when data is `Put`, `Post` or `Delete`.
/// If this is a link then it is sent with a `churn` event.
/// A `Link` is a nodeblock that each member must send each other in times of churn.
/// These will not accumulate but be `ManagedNode` to `ManagedNode` messages in the routing layer
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug, Clone)]
pub struct NodeBlock {
identifier: BlockIdentifier,
proof: NodeBlockProof,
}
impl NodeBlock {
/// Create a Block (used by nodes in network to send to holders of `DataChains`)
pub fn new(pub_key: &PublicKey,
secret_key: &SecretKey,
data_identifier: BlockIdentifier)
-> Result<NodeBlock, Error> {
let signature =
crypto::sign::sign_detached(&try!(serialisation::serialise(&data_identifier))[..],
secret_key);
Ok(NodeBlock {
identifier: data_identifier,
proof: NodeBlockProof::new(*pub_key, signature),
})
}
/// Getter
pub fn identifier(&self) -> &BlockIdentifier {
&self.identifier
}
/// Getter
pub fn proof(&self) -> &NodeBlockProof {
&self.proof
}
/// validate signed correctly
pub fn validate(&self) -> bool {
let data = if let Ok(data) = serialisation::serialise(&self.identifier) {
data
} else {
return false;
};
crypto::sign::verify_detached(self.proof.sig(), &data[..], self.proof.key())
}
/// validate signed correctly
pub fn validate_detached(&self, identifier: BlockIdentifier) -> bool {
let data = if let Ok(data) = serialisation::serialise(&identifier) {
data
} else {
return false;
};
crypto::sign::verify_detached(self.proof.sig(), &data[..], self.proof.key())
}
}
#[cfg(test)]
mod tests {
use super::*;
use block_identifier::BlockIdentifier;
use sodiumoxide::crypto;
use sodiumoxide::crypto::hash::sha256;
#[test]
fn node_block_comparisons() {
::sodiumoxide::init();
let keys = crypto::sign::gen_keypair();
let test_data1 = BlockIdentifier::Link(sha256::hash(b"1").0);
let test_data2 = BlockIdentifier::Link(sha256::hash(b"1").0);
let test_data3 = BlockIdentifier::ImmutableData(sha256::hash(b"1"));
let test_node_data_block1 = NodeBlock::new(&keys.0, &keys.1, test_data1).expect("fail1");
let test_node_data_block2 = NodeBlock::new(&keys.0, &keys.1, test_data2).expect("fail2");
let test_node_data_block3 = NodeBlock::new(&keys.0, &keys.1, test_data3).expect("fail3");
assert!(test_node_data_block1.validate());
assert!(test_node_data_block2.validate());
assert!(test_node_data_block3.validate());
assert_eq!(test_node_data_block1.clone(), test_node_data_block2.clone());
assert!(test_node_data_block1 != test_node_data_block3.clone());
assert!(test_node_data_block2 != test_node_data_block3);
}
}