Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • jinxinl2/ece598pv-sp2022
  • kailinc2/ece598pv-sp2022
  • ece598pv/ece598pv-sp2022
  • qh11/ece598pv-sp2022
4 results
Show changes
Commits on Source (6)
......@@ -21,6 +21,7 @@ url = "2.1"
crossbeam = "0.8"
rand = "0.8"
hex-literal = "0.3"
data-encoding = "2.3.2"
clap = { version = "2.33", features = ["wrap_help"]}
[features]
......@@ -28,4 +29,4 @@ default = []
test-utilities = []
[dev-dependencies]
ntest = "0.7"
\ No newline at end of file
ntest = "0.7"
# Warmup 1
what
Hi, welcome! This is your first assignment of this course. The goal of this assignment is to let you get familiar with the **Rust** programming language. We will use Rust throughout this course so it is a good idea to start to learn it as early as possible.
## Introduction
......
......@@ -19,6 +19,15 @@ use std::sync::{Arc, Mutex};
use std::thread;
use std::time;
use crate::types::hash::{H256, Hashable};
use crate::types::transaction::SignedTransaction;
use crate::types::block::Header;
use crate::types::block::Block;
use std::time::{SystemTime, UNIX_EPOCH};
use std::collections::HashMap;
fn main() {
// parse command line arguments
let matches = clap_app!(Bitcoin =>
......@@ -81,8 +90,14 @@ fn main() {
worker_ctx.start();
// start the miner
let (miner_ctx, miner, finished_block_chan) = miner::new();
let miner_worker_ctx = miner::worker::Worker::new(&server, finished_block_chan);
let mut temp_blockchain = Blockchain::new();
let mut arc_mutex_blockchain = Arc::new(Mutex::new(temp_blockchain));
let (miner_ctx, miner, finished_block_chan) = miner::new(&arc_mutex_blockchain);
let miner_worker_ctx = miner::worker::Worker::new(&server, finished_block_chan, &arc_mutex_blockchain);
miner_ctx.start();
miner_worker_ctx.start();
......
extern crate data_encoding;
extern crate ring;
use serde::{Serialize, Deserialize};
use ring::digest::{Context, SHA256};
use data_encoding::HEXLOWER;
// 20-byte address
#[derive(Eq, PartialEq, Serialize, Deserialize, Clone, Hash, Default, Copy)]
pub struct Address([u8; 20]);
......@@ -48,7 +55,13 @@ impl std::fmt::Debug for Address {
impl Address {
pub fn from_public_key_bytes(bytes: &[u8]) -> Address {
unimplemented!()
let mut context = Context::new(&SHA256);
context.update(&bytes[..]);
let result = context.finish();
let mut a:[u8; 20] = [0u8;20];
a.copy_from_slice(&(result.as_ref()[12..32]));
Address(a)
//unimplemented!()
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. BEFORE TEST
......@@ -62,6 +75,8 @@ mod test {
let test_key = hex!("0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d");
let addr = Address::from_public_key_bytes(&test_key);
let correct_addr: Address = hex!("1851a0eae0060a132cf0f64a0ffaea248de6cba0").into();
println!("addr:{}",addr);
println!("correct_addr:{}",correct_addr);
assert_eq!(addr, correct_addr);
// "b69566be6e1720872f73651d1851a0eae0060a132cf0f64a0ffaea248de6cba0" is the hash of
// "0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d0a0b0c0d0e0f0e0d"
......@@ -69,4 +84,4 @@ mod test {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
use serde::{Serialize, Deserialize};
use crate::types::hash::{H256, Hashable};
use crate::types::transaction::SignedTransaction;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Block {
pub header : Header,
pub content : Vec<SignedTransaction>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Header {
pub parent : H256,
pub nonce : u32,
pub difficulty : H256,
pub timestamp : u128,
pub merkle_root : H256,
}
impl Hashable for Block {
fn hash(&self) -> H256 {
unimplemented!()
return self.header.hash();
}
}
impl Hashable for Header {
fn hash(&self) -> H256 {
let bytes = bincode::serialize(&self).unwrap();
return ring::digest::digest(&ring::digest::SHA256, &bytes).into();
}
}
impl Block {
pub fn get_parent(&self) -> H256 {
unimplemented!()
self.header.parent
}
pub fn get_difficulty(&self) -> H256 {
unimplemented!()
self.header.difficulty
}
}
pub fn generate_genesis_block() -> Block {
// let mut rng = rand::r;
let emptyRoot = H256::from([0;32]);
return get_random_block(&emptyRoot);
}
pub fn get_random_block(parent: &H256) -> Block {
// let mut rng = rand::r;
let content: Vec<SignedTransaction> = Vec::new();
let bytes = bincode::serialize(&content).unwrap();
let merkle_root = ring::digest::digest(&ring::digest::SHA256, &bytes).into();
let difficulty = H256::from([255u8; 32]);
let header = Header{
parent: *parent,
nonce: rand::random::<u32>(),
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
merkle_root: merkle_root,
difficulty: difficulty,
};
Block{
header: header,
content: content,
}
}
#[cfg(any(test, test_utilities))]
pub fn generate_random_block(parent: &H256) -> Block {
unimplemented!()
}
\ No newline at end of file
// let mut rng = rand::r;
let content: Vec<SignedTransaction> = Vec::new();
let bytes = bincode::serialize(&content).unwrap();
let merkle_root = ring::digest::digest(&ring::digest::SHA256, &bytes).into();
let difficulty = crate::types::hash::generate_random_hash();
let header = Header{
parent: *parent,
nonce: rand::random::<u32>(),
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(),
merkle_root: merkle_root,
difficulty: difficulty,
};
Block{
header: header,
content: content,
}
}
use serde::{Serialize,Deserialize};
use ring::signature::{Ed25519KeyPair, Signature, KeyPair, VerificationAlgorithm, EdDSAParameters};
use rand::Rng;
use crate::types::address::Address;
use bincode::serialize;
use serde::{Serialize,Deserialize};
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Transaction {
sender: Address,
receiver: Address,
value: u32
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SignedTransaction {
transaction: Transaction,
signature_vec: Vec<u8>,
public_key_vec: Vec<u8>
}
/// Create digital signature of a transaction
pub fn sign(t: &Transaction, key: &Ed25519KeyPair) -> Signature {
unimplemented!()
let serializeT = bincode::serialize(&t).unwrap();
let signature = key.sign(&serializeT);
signature
//unimplemented!()
}
/// Verify digital signature of a transaction, using public key instead of secret key
pub fn verify(t: &Transaction, public_key: &[u8], signature: &[u8]) -> bool {
unimplemented!()
let serializeT = serialize(&t).unwrap();
let unparsed = ring::signature::UnparsedPublicKey::new(&ring::signature::ED25519, public_key);
let result = unparsed.verify(&serializeT, signature);
let mut ans:bool = false;
if result == Ok(()) {
ans = true;
} else {
ans = false;
}
return ans;
//unimplemented!()
}
#[cfg(any(test, test_utilities))]
pub fn generate_random_transaction() -> Transaction {
unimplemented!()
let sender: [u8; 20] = rand::random();
let receiver: [u8; 20] = rand::random();
let value: u32 = rand::random();
let newTransaction = Transaction {
sender: sender.into(),
receiver: receiver.into(),
value: value
};
newTransaction
//unimplemented!()
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. BEFORE TEST
......@@ -53,4 +85,4 @@ mod tests {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST