Last active
March 4, 2021 16:03
-
-
Save harshvishu/dce63113b596db197bfb4ff1a4dee16a to your computer and use it in GitHub Desktop.
Block & Chain skeleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct Block { | |
index: u64, | |
timestamp: DateTime<Utc>, | |
transactions: Vec<Transaction>, | |
proof: u64, | |
previous_hash: String, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct Chain { | |
chain: Vec<Block>, | |
current_transactions: Vec<Transaction>, | |
nodes: HashSet<String>, | |
} | |
impl Chain { | |
pub fn new() -> Self { | |
let mut chain = Chain { | |
chain: vec![], | |
current_transactions: vec![], | |
nodes: HashSet::new(), | |
}; | |
chain.new_block(Some("1".to_string()), 100); | |
return chain; | |
} | |
pub fn new_block(&mut self, previous_hash: Option<String>, proof: u64) { | |
let previous_hash: String = match previous_hash { | |
Some(previous_hash) => previous_hash.to_string(), | |
None => match self.chain.last_mut() { | |
Some(last_block) => Chain::hash(last_block), | |
None => "".to_string(), | |
}, | |
}; | |
let block = Block::new( | |
&self, | |
self.current_transactions.to_vec(), | |
proof, | |
previous_hash.to_string(), | |
); | |
self.chain.push(block); | |
self.current_transactions.clear(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct Transaction { | |
sender: String, | |
recipient: String, | |
amount: u64, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment