[おまかせシリーズを理解する|ゼロベース| | rustlang言語をはじめ|(27)|戦闘4:ゼロBTCブロック鎖から実現]

[おまかせシリーズを理解する|ゼロベース| | rustlang言語をはじめ|(27)|戦闘4:ゼロBTCブロック鎖から実現]

プロジェクト戦闘

戦闘4:ゼロからのBTCブロック鎖を達成するために

今日は私たちのBTCブロックチェーンシステムを開発してみましょう。

簡単に説明すると、データ構造の観点から、ブロックのチェーンは、チェーンブロックで構成されています。

以下は典型的なBTCブロック鎖構造です。

元画像を参照してください。

ブロック:最小単位はブロックです。

ヘッダ領域、ブロック体:このブロックは二つの部分を含んでいます。

唯一の最も基本的なデータ構造を残して、データ構造を簡素化するために、すべての最初の、のは、Merkle木を無視してみましょう。

その領域ヘッダは、含まれていますカットオフ時間を、ブロックアドレス前

ブロック体は、私たちが店にベクトルを使用して、トランザクションデータが含まれています。

コードは以下の通りであります:

///交易结构体
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
pub struct Transaction {
    sender: String,    //发送者
    recipient: String, //接收者
    amount: i64,       //交易数量
}
/// 区块结构体
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
pub struct Block {
    pub index: u64,                     //区块高度
    timestamp: DateTime<Utc>,           //时间截
    pub transactions: Vec<Transaction>, //交易
    pub proof: u64,                     //证明
    pub previous_hash: String,          //上一个区块哈希地址
}
//区块链结构体
#[derive(Default)]
pub struct Blockchain {
    pub chain: Vec<Block>,                  //区块链帐本
    current_transactions: Vec<Transaction>, //交易集合
    pub nodes: HashSet<String>,             //节点集合
}

今、私たちは、基本ブロックのデータ構造を作成している、のは、鉱山労働者は、ブロックを作成するために聞かせてみましょう。

どのように異なるの鉱山労働者は、積極的にそれを阻止作成するには?

POWコンセンサスメカニズム:我々はと呼ばれる仕組みを導入します。

POWとは何ですか?簡単に言えば、すべてのセットに従って証明値、最初に計算されたハッシュ計算、結果の値が一定の基準を満たし、あなたは新しいブロック、このブロックと元のブロックへの接続を作成する必要がありますチェーンの権威。

例えば、難易度は5であり、SHAの鉱山労働者と証明データ、次の結果がハッシュされていること。

0ksh00000l0000000000000000000000000000000000000000000000000000000000

結果は、(外0X除く)先行ゼロが5であり、これは結果の値です。

結果の値は、上記計算されていない場合、計算された値が基準を満たすまで、プルーフ鉱山労働者は、その後、SHAハッシュ計算を1インクリメントします。

そして、その与えられたデータ値を証明するだけでなく、ヘッダ領域、生産され、ヘッダ領域に鉱夫によって、新しいブロックを作成するたびに書き込まれた値に。

両方のノードは、結果を計算し、新しいブロックを結合されている場合はもちろん、この時間は、どのように競合し、それを決め、その後、チェーンをフォークしているのだろうか?

ノードが最長のチェーンを所有して一定期間、内で使用することを私たちは、最長のチェーン原則。

だから、私たちのコンセンサスメカニズムがある:POW +最長チェーンの原則

コンセンサス機構コアコードは次の通りであります:

impl Blockchain {
    //创建创世区块
    pub fn new() -> Blockchain {
        let mut blockchain = Blockchain {
            chain: vec![],
            current_transactions: vec![],
            nodes: HashSet::new(),
        };
        blockchain.new_block(100, Some("1"));
        blockchain
    }
    /// Create a new Block in the Blockchain
    ///
    /// :param proof: The proof given by the Proof of Work algorithm
    /// :param previous_hash: (Optional) hash of previous Block
    /// :return: New Bloc
    /// 创建新区块
    pub fn new_block(&mut self, proof: u64, previous_hash: Option<&str>) -> Block {
        let block = Block {
            index: (self.chain.len() + 1) as u64,
            timestamp: Utc::now(),
            transactions: self.current_transactions.drain(0..).collect(),
            proof,
            previous_hash: previous_hash.unwrap_or("0").to_string(),
        };

        self.chain.push(block.clone());
        block
    }
    /// Creates a new transaction to go into the next mined Block
    ///
    /// :param sender: Address of the śender
    /// :param recipient: Address of the recipient
    /// :param amount: Amount
    /// :return: The index of the Block that will hold this transaction
    /// 发起一个新交易,将写入下一个区块
    pub fn new_transaction(&mut self, sender: &str, recipient: &str, amount: i64) -> u64 {
        self.current_transactions.push(Transaction {
            sender: sender.to_string(),
            recipient: recipient.to_string(),
            amount,
        });
        self.last_block().unwrap().index + 1
    }
    /// Simple Proof of Work Algorithm:
    /// - Find a number p' such that hash(pp') contains 4 leading zeroes,
    ///   where p is the previous proof, and p' is the new proof
    /// POW工作量证明共识机制算法
    pub fn proof_of_work(last_block: &Block) -> u64 {
        let mut proof = 0;
        let last_proof = last_block.proof;
        let last_hash = &last_block.previous_hash;
        while !Self::valid_proof(last_proof, proof, last_hash) {
            proof += 1;
        }
        proof
    }
    /// Validates the Proof: Does hash(last_proof, proof, last_hash) containt 4 leading zeroes
    //验证工作证明数字
    fn valid_proof(last_proof: u64, proof: u64, last_hash: &String) -> bool {
        let guess = format!("{}{}{}", last_proof, proof, last_hash);
        let guess_hash = hex_digest(Algorithm::SHA256, guess.as_bytes());
        guess_hash.ends_with("00000") //困难度为5
    }

    /// Creates a SHA-256 hash of a Block
    ///
    /// :param block: Block
    /// :return hash for the block
    /// 创建一个区块 的哈希值,基SHA-256算法
    pub fn hash(block: &Block) -> String {
        let serialized = serde_json::to_string(&block).unwrap();
        hex_digest(Algorithm::SHA256, serialized.as_bytes())
    }
    /// Returns the last Block in the chain
    /// 返回最后一个区块
    pub fn last_block(&self) -> Option<&Block> {
        self.chain.last()
    }

    /// Add a new node to the list of nodes
    ///
    /// :param address: Address of the node. Eg. 'http://192.168.0.5:5000'
    ///
    /// 节点注册,即新节点加入区块链网络,注册地址参数为节点服务器地址,如:'http://192.168.0.5:5000‘
    pub fn register_node(&mut self, address: &str) {
        let parsed_url = urlparse(address);
        self.nodes.insert(parsed_url.netloc);
    }

    /// Determine if a given blockchain is valid
    /// 链的验证
    fn valid_chain(&self, chain: &[Block]) -> bool {
        let mut last_block = &chain[0];
        let mut current_index: usize = 1;
        while current_index < chain.len() {
            let block = &chain[current_index];
            println!("{:?}", last_block);
            println!("{:?}", block);
            println!("-----------");
            if block.previous_hash != Blockchain::hash(last_block) {
                return false;
            }
            if !Blockchain::valid_proof(last_block.proof, block.proof, &last_block.previous_hash) {
                return false;
            }

            last_block = block;
            current_index += 1;
        }
        true
    }

    /// This is our Consensus Algorithm, it resolves conflicts
    /// by replacing our chain with the longest one in the network.
    ///
    /// :return True if our chain was replaced and false otherwise
    /// 解决冲突的机制,即共识机制,最长链原则处理逻辑,即共识机制为(POw+最长链原则)
    pub fn resolve_conflicts(&mut self) -> bool {
        let mut max_length = self.chain.len();
        let mut new_chain: Option<Vec<Block>> = None;

        // Grab and verify the chains from all the nodes in our network
        for node in &self.nodes {
            let mut response = reqwest::get(&format!("http://{}/chain", node)).unwrap();
            if response.status().is_success() {
                let node_chain: Chain = response.json().unwrap();
                if node_chain.length > max_length && self.valid_chain(&node_chain.chain) {
                    max_length = node_chain.length;
                    new_chain = Some(node_chain.chain);
                }
            }
        }
        // Replace our chain if we discovered a new, valid chain longer than ours
        match new_chain {
            Some(x) => {
                self.chain = x;
                true
            }
            None => false,
        }
    }
}

上記のコード、私たちは現在のプロジェクトディレクトリのsrc / blockchain.rsに入れ、完全なコードは次のとおりです。

use crate::api::Chain;
use chrono::{DateTime, Utc};
use crypto_hash::{hex_digest, Algorithm};
use reqwest;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use urlparse::urlparse;
///交易结构体
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
pub struct Transaction {
    sender: String,    //发送者
    recipient: String, //接收者
    amount: i64,       //交易数量
}
/// 区块结构体
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
pub struct Block {
    pub index: u64,                     //区块高度
    timestamp: DateTime<Utc>,           //时间截
    pub transactions: Vec<Transaction>, //交易
    pub proof: u64,                     //证明
    pub previous_hash: String,          //上一个区块哈希地址
}
//区块链结构体
#[derive(Default)]
pub struct Blockchain {
    pub chain: Vec<Block>,                  //区块链帐本
    current_transactions: Vec<Transaction>, //交易集合
    pub nodes: HashSet<String>,             //节点集合
}

impl Blockchain {
    //创建创世区块
    pub fn new() -> Blockchain {
        let mut blockchain = Blockchain {
            chain: vec![],
            current_transactions: vec![],
            nodes: HashSet::new(),
        };
        blockchain.new_block(100, Some("1"));
        blockchain
    }
    /// Create a new Block in the Blockchain
    ///
    /// :param proof: The proof given by the Proof of Work algorithm
    /// :param previous_hash: (Optional) hash of previous Block
    /// :return: New Bloc
    /// 创建新区块
    pub fn new_block(&mut self, proof: u64, previous_hash: Option<&str>) -> Block {
        let block = Block {
            index: (self.chain.len() + 1) as u64,
            timestamp: Utc::now(),
            transactions: self.current_transactions.drain(0..).collect(),
            proof,
            previous_hash: previous_hash.unwrap_or("0").to_string(),
        };

        self.chain.push(block.clone());
        block
    }
    /// Creates a new transaction to go into the next mined Block
    ///
    /// :param sender: Address of the śender
    /// :param recipient: Address of the recipient
    /// :param amount: Amount
    /// :return: The index of the Block that will hold this transaction
    /// 发起一个新交易,将写入下一个区块
    pub fn new_transaction(&mut self, sender: &str, recipient: &str, amount: i64) -> u64 {
        self.current_transactions.push(Transaction {
            sender: sender.to_string(),
            recipient: recipient.to_string(),
            amount,
        });
        self.last_block().unwrap().index + 1
    }
    /// Simple Proof of Work Algorithm:
    /// - Find a number p' such that hash(pp') contains 4 leading zeroes,
    ///   where p is the previous proof, and p' is the new proof
    /// POW工作量证明共识机制算法
    pub fn proof_of_work(last_block: &Block) -> u64 {
        let mut proof = 0;
        let last_proof = last_block.proof;
        let last_hash = &last_block.previous_hash;
        while !Self::valid_proof(last_proof, proof, last_hash) {
            proof += 1;
        }
        proof
    }
    /// Validates the Proof: Does hash(last_proof, proof, last_hash) containt 4 leading zeroes
    //验证工作证明数字
    fn valid_proof(last_proof: u64, proof: u64, last_hash: &String) -> bool {
        let guess = format!("{}{}{}", last_proof, proof, last_hash);
        let guess_hash = hex_digest(Algorithm::SHA256, guess.as_bytes());
        guess_hash.ends_with("00000") //困难度为5
    }

    /// Creates a SHA-256 hash of a Block
    ///
    /// :param block: Block
    /// :return hash for the block
    /// 创建一个区块 的哈希值,基SHA-256算法
    pub fn hash(block: &Block) -> String {
        let serialized = serde_json::to_string(&block).unwrap();
        hex_digest(Algorithm::SHA256, serialized.as_bytes())
    }
    /// Returns the last Block in the chain
    /// 返回最后一个区块
    pub fn last_block(&self) -> Option<&Block> {
        self.chain.last()
    }

    /// Add a new node to the list of nodes
    ///
    /// :param address: Address of the node. Eg. 'http://192.168.0.5:5000'
    ///
    /// 节点注册,即新节点加入区块链网络,注册地址参数为节点服务器地址,如:'http://192.168.0.5:5000‘
    pub fn register_node(&mut self, address: &str) {
        let parsed_url = urlparse(address);
        self.nodes.insert(parsed_url.netloc);
    }

    /// Determine if a given blockchain is valid
    /// 链的验证
    fn valid_chain(&self, chain: &[Block]) -> bool {
        let mut last_block = &chain[0];
        let mut current_index: usize = 1;
        while current_index < chain.len() {
            let block = &chain[current_index];
            println!("{:?}", last_block);
            println!("{:?}", block);
            println!("-----------");
            if block.previous_hash != Blockchain::hash(last_block) {
                return false;
            }
            if !Blockchain::valid_proof(last_block.proof, block.proof, &last_block.previous_hash) {
                return false;
            }

            last_block = block;
            current_index += 1;
        }
        true
    }

    /// This is our Consensus Algorithm, it resolves conflicts
    /// by replacing our chain with the longest one in the network.
    ///
    /// :return True if our chain was replaced and false otherwise
    /// 最长链原则处理逻辑,即共识机制为(POw+最长链原则)
    pub fn resolve_conflicts(&mut self) -> bool {
        let mut max_length = self.chain.len();
        let mut new_chain: Option<Vec<Block>> = None;

        // Grab and verify the chains from all the nodes in our network
        for node in &self.nodes {
            let mut response = reqwest::get(&format!("http://{}/chain", node)).unwrap();
            if response.status().is_success() {
                let node_chain: Chain = response.json().unwrap();
                if node_chain.length > max_length && self.valid_chain(&node_chain.chain) {
                    max_length = node_chain.length;
                    new_chain = Some(node_chain.chain);
                }
            }
        }
        // Replace our chain if we discovered a new, valid chain longer than ours
        match new_chain {
            Some(x) => {
                self.chain = x;
                true
            }
            None => false,
        }
    }
}

私たちは今、外の世界で利用可能なAPIの一部を提供します。

私たちは、新しいファイルを作成します:SRC / api.rsを次のように、コードは次のとおりです。

use crate::blockchain::{Block, Blockchain, Transaction};

use actix_web::{web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
///返回消息体
#[derive(Serialize, Deserialize)]
pub struct MessageResponse {
    message: String,
}
//交易请求信息
#[derive(Serialize, Deserialize)]
pub struct TransactionRequest {
    sender: String,
    recipient: String,
    amount: i64,
}
///挖矿响应消息
#[derive(Serialize)]
pub struct MiningRespose {
    message: String,
    index: u64,
    transactions: Vec<Transaction>,
    proof: u64,
    previous_hash: String,
}
///链结构体,代表现在网络上的最长链
#[derive(Serialize, Deserialize)]
pub struct Chain {
    pub chain: Vec<Block>,
    pub length: usize,
}
///节点注册请求信息
#[derive(Deserialize)]
pub struct RegisterRequest {
    nodes: Vec<String>,
}
///节点注册响应信息
#[derive(Serialize)]
pub struct RegisterResponse {
    message: String,
    total_nodes: Vec<String>,
}
//解决冲突响应信息
#[derive(Serialize)]
pub struct ResolveResponse {
    message: String,
    chain: Vec<Block>,
}
///发起新交易
pub fn new_transaction(
    state: web::Data<Mutex<Blockchain>>,
    req: web::Json<TransactionRequest>,
) -> HttpResponse {
    let sender = req.sender.to_owned();
    let recipient = req.recipient.to_owned();
    let index = state
        .lock()
        .unwrap()
        .new_transaction(&sender, &recipient, req.amount);
    HttpResponse::Created().json(MessageResponse {
        message: format! {"Transaction will be added to Block {}", index},
    })
}
///矿工挖矿
pub fn mine(
    node_identifier: web::Data<String>,
    state: web::Data<Mutex<Blockchain>>,
    _req: HttpRequest,
) -> HttpResponse {
    let (proof, previous_hash) = {
        let blockchain = state.lock().unwrap();
        let last_block = blockchain.last_block().unwrap();
        let proof = Blockchain::proof_of_work(&last_block);
        let previous_hash = Blockchain::hash(last_block);
        (proof, previous_hash)
    };
    let mut blockchain = state.lock().unwrap();
    blockchain.new_transaction("0", &*node_identifier, 1);
    let block = blockchain.new_block(proof, Some(&previous_hash));
    HttpResponse::Ok().json(MiningRespose {
        message: "New Block Forged".to_string(),
        index: block.index,
        transactions: block.transactions,
        proof,
        previous_hash,
    })
}
///当前最新链的信息
pub fn chain(state: web::Data<Mutex<Blockchain>>, _req: HttpRequest) -> HttpResponse {
    let length = state.lock().unwrap().chain.len();
    HttpResponse::Ok().json(Chain {
        chain: state.lock().unwrap().chain.clone(),
        length,
    })
}
///节点注册
pub fn register_node(
    state: web::Data<Mutex<Blockchain>>,
    req: web::Json<RegisterRequest>,
) -> HttpResponse {
    if req.nodes.is_empty() {
        return HttpResponse::BadRequest().json(MessageResponse {
            message: "Error: Please supply a valid list of nodes".to_string(),
        });
    }
    let mut blockchain = state.lock().unwrap();
    for node in req.nodes.iter() {
        blockchain.register_node(node)
    }
    HttpResponse::Created().json(RegisterResponse {
        message: "New nodes have been added".to_string(),
        total_nodes: blockchain.nodes.iter().cloned().collect(),
    })
}
///跟网络上其他节点达成共识,即解决冲突
pub fn resolve_nodes(state: web::Data<Mutex<Blockchain>>, _req: HttpRequest) -> HttpResponse {
    let mut blockchain = state.lock().unwrap();
    let replaced = blockchain.resolve_conflicts();
    let message = if replaced {
        "Our chain was replaced"
    } else {
        "Our chain is authorative"
    };
    HttpResponse::Ok().json(ResolveResponse {
        message: message.to_string(),
        chain: blockchain.chain.clone(),
    })
}

次のようにもちろん、私たちはいくつかの素晴らしいライブラリを使用する必要があり、私たちのCargo.tomlファイルで、我々は依存関係を追加し、完全なコードは次のようになります。

[dependencies]
chrono = { version = "0.4.6", features = ["serde"] }
crypto-hash = "0.3.3"
serde = { version = "1.0.90", features = ["derive"] }
serde_json = "1.0"
actix-web = "1.0"
uuid = { version = "0.7", features = ["v4"] }
urlparse = "0.7.3"
reqwest = "=0.9.17"

最後に、私たちのメインのsrc / main.rs次のように:

pub mod api;
pub mod blockchain;

use actix_web::{web, App, HttpServer};
use std::env;
use std::sync::Mutex;
use uuid::Uuid;

fn main() {
    let args: Vec<String> = env::args().collect();
    let port = match args.as_slice() {
        [_, key, value] => {
            if key == "--p" {
                value
            } else {
                panic!("Illegal arguments passed to the program.");
            }
        }
        _ => "5000",
    };
    // TODO: make chain shared across threads
    let sharedchain = web::Data::new(Mutex::new(blockchain::Blockchain::new()));
    let node_identifier = web::Data::new(Uuid::new_v4().to_simple().to_string());

    HttpServer::new(move || {
        App::new()
            .register_data(sharedchain.clone())
            .register_data(node_identifier.clone())
            .data(web::JsonConfig::default().limit(4096))
            .service(web::resource("/mine").route(web::get().to(api::mine)))
            .service(web::resource("/transactions/new").route(web::post().to(api::new_transaction)))
            .service(web::resource("/chain").route(web::get().to(api::chain)))
            .service(web::resource("/nodes/register").route(web::post().to(api::register_node)))
            .service(web::resource("/nodes/resolve").route(web::get().to(api::resolve_nodes)))
    })
    .bind(format!("127.0.0.1:{}", port))
    .unwrap()
    .run();
}

その後、我々は、呼び出しに次のコマンドを使用できます。

鉱業:

curl http://localhost:5000/mine

新しい契約を作成します。

curl -H "Content-Type: application/json" --request POST --data '{"sender":"e79fcabd1d70433191701d17c4d13112", "recipient":"some-other-address", "amount":5}' http://localhost:5000/transactions/new

チェーン全体の情報を表示します。

curl http://localhost:5000/chain

登録ノード:

curl -H "Content-Type: application/json" --request POST --data '{"nodes":["http://localhost:5001"]}' http://localhost:5000/nodes/register

他のノードと合意(コンセンサス機構)

curl http://localhost:5000/nodes/resolve

上記は、私はあなたに役立つ願っています。

如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust

https://asymmetric.github.io/2018/02/11/blockchain-rust/

https://jeiwan.net/posts/building-blockchain-in-go-part-1/

https://freestartupkits.com/articles/technology/cryptocurrency-news-and-tips/ultimate-rust-blockchain-tutorial/

https://hackernoon.com/learn-blockchains-by-building-one-117428612f46

https://medium.com/@vanflymen/learn-blockchains-by-building-one-117428612f46?

https://github.com/Koura/blockchain-example

おすすめ

転載: www.cnblogs.com/gyc567/p/12079503.html