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
#[allow(dead_code)]
// Included crates
extern crate chrono;
extern crate serde;
extern crate serde_json;


// Use statements
//
// Used for timestamping 
use self::chrono::Utc;
// Standard libraries used for Strings and Vectors
use std::string::String;
// For hashing necessities
use hash_util::*;
// For JSON functionality
#[allow(unused_imports)]
use self::serde_json::Error;

/*
 *
 * Block:
 *     - This file contains the structs and impls associated with creating blocks in
 *       the chain.
 *
 *
 */    

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

    // The index of the block
    pub index: u64,
    // The block's previous hash 
    pub previous_hash: String,
    // The time the block was created 
    pub timestamp: String,
    // Merkle Root
    pub merkle_root: String,
    // The block's hash
    pub hash: String,

}

// Block impl
impl Block
{   

    // Constructor for a new block
    #[allow(dead_code)]
    pub fn new( index: u64, merkle_root: String ) -> Block
    {

        // Generate a default block
        let mut block = Block{
            index: index,
            previous_hash: empty_hash(),
            timestamp: Utc::now().to_string(),
            merkle_root: merkle_root,
            hash: empty_hash(),
        };
        // Generate a hash from all of the fields of this block
        block.hash = generate_header_hash( &block );
        // Return the block
        return block;
        
    }

    // Constructor for the origin block (first block in the chain with hash 0)
    #[allow(dead_code)]
    pub fn origin() -> Block
    {
        
        // Create a new block and make the hash equal the empty hash
        let mut block : Block = Block::new( 0, empty_hash() );
        block.hash = empty_hash();
        return block;

    }

    // Getters
    #[allow(dead_code)]
    pub fn index( &self ) -> &u64
    {

        &self.index
        
    }

    #[allow(dead_code)]
    pub fn timestamp( &self ) -> &String
    {
        
        &self.timestamp
        
    }

    #[allow(dead_code)]
    pub fn previous_hash( &self ) -> &String
    {

        &self.previous_hash
        
    }

    #[allow(dead_code)]
    pub fn merkle_root( &self ) -> &String
    {

        &self.merkle_root
        
    }

    #[allow(dead_code)]
    pub fn hash( &self ) -> &String
    {

        &self.hash
        
    }


    // Setter
    #[allow(dead_code)]
    pub fn set_previous_hash( &mut self, hash: &String )
    {

        self.previous_hash = hash.clone();
        // Recalculate the header of the block with the new previous hash
        self.hash = generate_header_hash( &self );
        
    }

    
    
}