Git Product home page Git Product logo

rust-blockchain's Introduction

Rust blockchain

Cấu trúc của một khối

struct Block {
    hash: String,
    data: String,
    prev_hash: String,
    nonce: u64,
}

hash: Mã băm của khối
data: Dữ liệu của khối
prev_hash: Mã băm của khối trước đó
nonce: Số nguyên không âm

Cấu trúc của một chuỗi khối

struct Blockchain {
    blocks: Vec<Block>,
}

blocks: Một mảng các khối

Proof of work

struct ProofOfWork {
    block: Block,
    target: String,
}

block: Khối cần tìm nonce
target: Một chuỗi nhị phân có độ dài bằng với số lượng bit 0 ở đầu của mã băm của khối

Cách hoạt động của Proof of work

Cố gắng tìm một số nguyên không âm nonce sao cho khi thêm nonce vào khối, mã băm của khối đó bắt đầu bằng với target.

Validate proof of work

fn validate(&self) -> bool {
      let mut hasher = Sha256::new();
      let data = self.prepare_data(self.block.nonce);
      hasher.update(data);

      let hash: String = hasher
          .finalize()
          .iter()
          .map(|x| format!("{:02x}", x))
          .collect();
      hash.starts_with(&self.target)
  }

rust-blockchain's People

Contributors

npv2k1 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.