Skip to content
Snippets Groups Projects
Commit 30b8f60e authored by Shubham Singhal's avatar Shubham Singhal
Browse files

- Derive H160 from public key

- Implement Hashable Trait for SignedTransaction
- Implement Mempool Structure
  - Initialise in main.rs
  - Use it in worker.rs
parent 8c1e42a6
No related branches found
No related tags found
No related merge requests found
use serde::{Serialize,Deserialize};
use ring::signature::{Ed25519KeyPair, KeyPair};
use ring::digest;
use rand::Rng;
//Last 20 bytes of Public Key - used in tx
......@@ -49,6 +51,23 @@ impl std::convert::From<H160> for [u8; 20] {
}
}
pub fn address_from_public_key_ref(public_key: &<Ed25519KeyPair as KeyPair>::PublicKey) -> H160 {
let public_key_hash = digest::digest(&digest::SHA256, public_key.as_ref());
let mut raw_address: [u8; 20] = [0; 20];
raw_address.copy_from_slice(&(public_key_hash.as_ref()[12..32]));
H160(raw_address)
}
pub fn address_from_public_key(public_key: <Ed25519KeyPair as KeyPair>::PublicKey) -> H160 {
let public_key_hash = digest::digest(&digest::SHA256, public_key.as_ref());
let mut raw_address: [u8; 20] = [0; 20];
raw_address.copy_from_slice(&(public_key_hash.as_ref()[12..32]));
H160(raw_address)
}
pub fn generate_random_address() -> H160 {
let mut rng = rand::thread_rng();
let random_bytes: Vec<u8> = (0..20).map(|_| rng.gen()).collect();
......
......@@ -9,6 +9,7 @@ pub mod crypto;
pub mod miner;
pub mod network;
pub mod transaction;
pub mod mempool;
use clap::clap_app;
use crossbeam::channel;
......@@ -69,6 +70,7 @@ fn main() {
// start the miner
let blockchain = Arc::new(Mutex::new(blockchain::Blockchain::new()));
let tx_mempool = Arc::new(Mutex::new(mempool::TransactionMempool::new()));
let (miner_ctx, miner) = miner::new(
&server,
&blockchain,
......@@ -89,6 +91,7 @@ fn main() {
msg_rx,
&server,
&blockchain,
&tx_mempool
);
worker_ctx.start();
......
use crate::crypto::hash::H256;
use crate::transaction::SignedTransaction;
use std::collections::VecDeque;
use std::collections::HashMap;
pub struct TransactionMempool{
pub tx_hash_queue: VecDeque<H256>,
pub tx_to_process: HashMap<H256, bool>,
pub tx_map: HashMap<H256, SignedTransaction>,
}
impl TransactionMempool{
pub fn new() -> Self{
TransactionMempool{tx_hash_queue: VecDeque::new(),
tx_to_process: HashMap::new(),
tx_map: HashMap::new()}
}
}
use serde::{Serialize, Deserialize};
use crate::crypto::hash::H256;
use crate::block::Block;
use crate::transaction::Transaction;
use crate::transaction::SignedTransaction;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Message {
......@@ -12,5 +12,5 @@ pub enum Message {
Blocks(Vec<Block>),
NewTransactionHashes(Vec<H256>),
GetTransactions(Vec<H256>),
Transactions(Vec<Transaction>),
Transactions(Vec<SignedTransaction>),
}
use super::message::Message;
use super::peer;
use crate::network::server::Handle as ServerHandle;
use crossbeam::channel;
use log::{debug, warn};
use crate::blockchain::Blockchain;
use crate::block::*;
use crate::mempool::TransactionMempool;
use crate::crypto::hash::{H256, Hashable};
use crossbeam::channel;
use log::{debug, warn};
use std::sync::{Arc, Mutex};
use std::thread;
use crate::crypto::hash::{H256, Hashable};
#[derive(Clone)]
pub struct Context {
......@@ -15,19 +18,22 @@ pub struct Context {
num_worker: usize,
server: ServerHandle,
blockchain: Arc<Mutex<Blockchain>>,
tx_mempool: Arc<Mutex<TransactionMempool>>,
}
pub fn new(
num_worker: usize,
msg_src: channel::Receiver<(Vec<u8>, peer::Handle)>,
server: &ServerHandle,
blockchain: &Arc<Mutex<Blockchain>>
blockchain: &Arc<Mutex<Blockchain>>,
tx_mempool: &Arc<Mutex<TransactionMempool>>
) -> Context {
Context {
msg_chan: msg_src,
num_worker,
server: server.clone(),
blockchain: Arc::clone(blockchain),
tx_mempool: Arc::clone(tx_mempool)
}
}
......
......@@ -39,6 +39,13 @@ impl Hashable for Transaction {
}
}
impl Hashable for SignedTransaction {
fn hash(&self) -> H256 {
let encoded_signed_trans: Vec<u8> = bincode::serialize(&self).unwrap();
ring::digest::digest(&ring::digest::SHA256, &encoded_signed_trans[..]).into()
}
}
/// Create digital signature of a transaction
pub fn sign(t: &Transaction, key: &Ed25519KeyPair) -> Signature {
//let mut mess = [&t.input[..],&t.output[..]].concat();
......@@ -57,7 +64,6 @@ pub fn verify(t: &Transaction, public_key: &<Ed25519KeyPair as KeyPair>::PublicK
peer_public_key.verify(&encoded[..],signature.as_ref()).is_ok()
}
pub fn generate_random_transaction() -> Transaction {
let input = vec![UtxoInput{tx_hash: hash::generate_random_hash(), idx: 0}];
let output = vec![UtxoOutput{receipient_addr: address::generate_random_address(), value: 0}];
......@@ -72,24 +78,11 @@ pub fn generate_genesis_transaction() -> Transaction {
Transaction{tx_input: input, tx_output: output}
}
#[cfg(any(test, test_utilities))]
pub mod tests {
use super::*;
use crate::crypto::key_pair;
/*
pub fn generate_random_transaction() -> Transaction {
/*Default::default();*/
let mut rng = rand::thread_rng();
let mut random_bytes: Vec<u8> = (0..32).map(|_| rng.gen()).collect();
let input_bytes = random_bytes;
random_bytes = (0..32).map(|_| rng.gen()).collect();
let output_bytes = random_bytes;
Transaction{input : input_bytes,output : output_bytes}
}*/
#[test]
fn sign_verify() {
let t = generate_random_transaction();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment