มาสร้าง 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
Articles
- スマート農業をもっと簡単に:農業資材を効率的に管理・計画するアプリ
- 🧑🌾 การทำฟาร์มอย่างชาญฉลาด: เครื่องมือช่วยวางแผนและติดตามการใช้ปัจจัยการผลิตในฟาร์มอย่างง่ายดาย
- 🌾 Smart Farming Made Simple: A Tool to Help Farmers Track and Plan Inputs Efficiently
- MEEPで電磁波をシミュレーション:はじめてのFDTD入門
- จำลองคลื่นแม่เหล็กไฟฟ้าด้วย MEEP: บทนำสู่การจำลองทางฟิสิกส์
- Simulate Electromagnetic Waves with MEEP: A Hands-On Introduction
- 🧠 LangChain はどのように動作するのか?
- LangChain ทำงานอย่างไร? เจาะลึกเบื้องหลังสมองของ AI แชทบอทอัจฉริยะ
- 🧠 How LangChain Works: A Deep Dive into the AI Framework Behind Smart Chatbots
- 🤖 為什麼中國中小企業現在就該使用 AI 聊天機器人?
- Why It's Time for Small Businesses to Start Using Chatbots – Globally
- 🤖 ถึงเวลาแล้ว! ทำไมธุรกิจ SME ไทยควรเริ่มใช้ "แชทบอท" วันนี้
- 🤖 日本の中小企業へ——今こそ「チャットボット」を導入すべき理由
- なぜ今、企業は LangChain チャットボットを導入しているのか?
- ทำไมธุรกิจยุคใหม่ถึงเลือกใช้แชทบอท LangChain? และคุณก็ควรเช่นกัน
- 为什么越来越多的企业选择 LangChain 聊天机器人?
- Why Smart Businesses Are Choosing LangChain Chatbots – And Why You Should Too
- 🚀 LangChainを活用したエージェントAIチャットボットの開発
- วิธีสร้างแชทบอท AI อัจฉริยะด้วย LangChain
- 🚀 How to Build an Agentic AI Chatbot with LangChain
Our Products
Articles
- スマート農業をもっと簡単に:農業資材を効率的に管理・計画するアプリ
- 🧑🌾 การทำฟาร์มอย่างชาญฉลาด: เครื่องมือช่วยวางแผนและติดตามการใช้ปัจจัยการผลิตในฟาร์มอย่างง่ายดาย
- 🌾 Smart Farming Made Simple: A Tool to Help Farmers Track and Plan Inputs Efficiently
- MEEPで電磁波をシミュレーション:はじめてのFDTD入門
- จำลองคลื่นแม่เหล็กไฟฟ้าด้วย MEEP: บทนำสู่การจำลองทางฟิสิกส์
- Simulate Electromagnetic Waves with MEEP: A Hands-On Introduction
- 🧠 LangChain はどのように動作するのか?
- LangChain ทำงานอย่างไร? เจาะลึกเบื้องหลังสมองของ AI แชทบอทอัจฉริยะ
- 🧠 How LangChain Works: A Deep Dive into the AI Framework Behind Smart Chatbots
- 🤖 為什麼中國中小企業現在就該使用 AI 聊天機器人?
- Why It's Time for Small Businesses to Start Using Chatbots – Globally
- 🤖 ถึงเวลาแล้ว! ทำไมธุรกิจ SME ไทยควรเริ่มใช้ "แชทบอท" วันนี้
- 🤖 日本の中小企業へ——今こそ「チャットボット」を導入すべき理由
- なぜ今、企業は LangChain チャットボットを導入しているのか?
- ทำไมธุรกิจยุคใหม่ถึงเลือกใช้แชทบอท LangChain? และคุณก็ควรเช่นกัน
- 为什么越来越多的企业选择 LangChain 聊天机器人?
- Why Smart Businesses Are Choosing LangChain Chatbots – And Why You Should Too
- 🚀 LangChainを活用したエージェントAIチャットボットの開発
- วิธีสร้างแชทบอท AI อัจฉริยะด้วย LangChain
- 🚀 How to Build an Agentic AI Chatbot with LangChain