มาสร้าง 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
- Rebuilding Trust with Technology After a Crisis
- Digital Beauty: Reimagining Cosmetic Clinics with Mobile Apps
- Smarter Product Discovery with AI: Image Labeling, Translation, and Cross-Selling
- How TAK Systems Transform Flood Disaster Response
- Smarter Shopping: From Photo to Product Recommendations with AI
- Tackling Antenna Coupling Challenges with Our Advanced Simulation Program
- The Future of Work: Open-Source Projects Driving Labor-Saving Automation
- 下一个前沿:面向富裕人群的数字私人俱乐部
- The Next Frontier: A Digital Private Club for the Affluent
- Thinking Better with Code: Using Mathematical Shortcuts to Master Large Codebases
- Building the Macrohard of Today: AI Agents Platform for Enterprises
- Build Vue.js Apps Smarter with Aider + IDE Integration
- Yo Dev! Here’s How I Use AI Tools Like Codex CLI and Aider to Speed Up My Coding
- Working With AI in Coding the Right Way
- How to Select the Right LLM Model: Instruct, MLX, 8-bit, and Embedding Models
- How to Use Local LLM Models in Daily Work
- How to Use Embedding Models with LLMs for Smarter AI Applications
- Smart Vision System for Continuous Material Defect Detection
- Building a Real-Time Defect Detector with Line-Scan + ML (Reusable Playbook)
- How to Read Source Code: Frappe Framework Sample