มาสร้าง Blockchain ของเราเองกันเถอะ ( 2 ) , Mining

Mining ( ขุด ) คือกระบวนการสร้าง block. ใหม่ แล้วเพิ่มเข้าไปใน blockchain
มีขั้นตอนการทำงานดังต่อไปนี้
const nonce = bitcoin.proofOfWork(previousBlockHash, currentBlockData);
const blockHash = bitcoin.hashBlock(previousBlockHash, currentBlockData, nonce);
const newBlock = bitcoin.createNewBlock(nonce, previousBlockHash, blockHash);
เรามาดู code ของ function เหล่านี้กันดีกว่า
Blockchain.prototype.createNewBlock = function(nonce, previousBlockHash, hash) {
const newBlock = {
index: this.chain.length + 1,
timestamp: Date.now(),
transactions: this.pendingTransactions,
nonce: nonce,
hash: hash,
previousBlockHash: previousBlockHash
};
this.pendingTransactions = [];
this.chain.push(newBlock);
return newBlock;
};
Blockchain.prototype.hashBlock = function(previousBlockHash, currentBlockData, nonce) {
const dataAsString = previousBlockHash + nonce.toString() + JSON.stringify(currentBlockData);
const hash = sha256(dataAsString);
return hash;
};
Blockchain.prototype.proofOfWork = function(previousBlockHash, currentBlockData) {
let nonce = 0;
let hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
while (hash.substring(0, 4) !== '0000') {
nonce++;
hash = this.hashBlock(previousBlockHash, currentBlockData, nonce);
}
return nonce;
};
เราควรจะเริ่มดูที่ hashBlock function ซึ่งถูกเรียกใช้บ่อย function นี้ไม่มีอะไรซับซ้อนมาก แค่ทำการ hash ค่าของ previousBlockHash + nonce + currentBlockData ด้วย sha256
proofOfWork function คือการหา hash ที่ขึ้นต้นด้วย 0000 ให้เจอ เราจะเห็นว่า nonce คือตัวแปรที่เพิ่มค่าไปเรื่อยๆ ใน while loop เมื่อเราเจอ nonce ที่ถูกต้องแล้ว function ก็จะ return
const nonce = bitcoin.proofOfWork(previousBlockHash, currentBlockData);
const blockHash = bitcoin.hashBlock(previousBlockHash, currentBlockData, nonce);
const newBlock = bitcoin.createNewBlock(nonce, previousBlockHash, blockHash);
กลับมาดู code ที่พูดในตอนแรกจะเห็นได้ว่า createNewBlock เพียงแค่รับ parameter ที่ได้จาก output ของ proofOfWork และ hashBlock
Get in Touch with us
Related Posts
- RTOS vs Linux in Drone Systems: Modern Design, Security, and Rust for Next-Gen Drones
- Why Does Spring Use So Many Annotations? Java vs. Python Web Development Explained
- From Django to Spring Boot: A Practical, Visual Guide for Web Developers
- How to Build Large, Maintainable Python Systems with Clean Architecture: Concepts & Real-World Examples
- Why Test-Driven Development Makes Better Business Sense
- Continuous Delivery for Django on DigitalOcean with GitHub Actions & Docker
- Build a Local Product Recommendation System with LangChain, Ollama, and Open-Source Embeddings
- 2025 Guide: Comparing the Top Mobile App Frameworks (Flutter, React Native, Expo, Ionic, and More)
- Understanding `np.meshgrid()` in NumPy: Why It’s Needed and What Happens When You Swap It
- How to Use PyMeasure for Automated Instrument Control and Lab Experiments
- Supercharge Your Chatbot: Custom API Integration Services for Your Business
- How to Guess an Equation Without Math: Exploring Cat vs. Bird Populations
- How to Build an AI-Resistant Project: Ideas That Thrive on Human Interaction
- Build Your Own Cybersecurity Lab with GNS3 + Wazuh + Docker: Train, Detect, and Defend in One Platform
- How to Simulate and Train with Network Devices Using GNS3
- What Is an LMS? And Why You Should Pay Attention to Frappe LMS
- Agentic AI in Factories: Smarter, Faster, and More Autonomous Operations
- Smarter, Safer EV Fleets: Geo-Fencing and Real-Time Tracking for Electric Motorcycles
- How to Implement Google Single Sign-On (SSO) in FastAPI
- Build Your Own Taxi Booking App with Simplico: Scalable, Secure & Ready to Launch