Blockchain Technology "Create virtual currency mining technology" (Part 4)
Welcome to Webzone Tech Tips Zidane, Today I will continue to share how to create virtual currency mining technology. If you haven't read part 1, part 2 and part 3, you should view back
In the Block class we add a $no, this is a variable used when calculating the Hash.
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:// Author: Zidane - Learn Tech Tips,
// Email: huuvi168@gmail.com
class Block
{
public $time;
public $data;
public $previous_hash;
public $hash;
public $no;
public $mining_time_stamp;
public $start_mining_time;
public $end_mining_time;
public function __construct($time, $data, $previous_hash = '')
{
$this->time = $time;
$this->data = $data;
$this->previous_hash = $previous_hash;
$this->hash = $this->calc_hash();
$this->no = 0;
$this->mining_time_stamp = 0;
$this->start_mining_time = 0;
$this->end_mining_time = 0;
}
public function calc_hash()
{
$data_content = "";
if(is_array($this->data))
{
$data_content = json_encode($this->data);
} else
{
$data_content = $this->data;
}
$hash_string = $this->time . $data_content . $this->previous_hash . $this->no;
return hash('sha256', $hash_string);
}
// mining_block: the core of algorithm mining block
public function mining_block($nonce) {
while (substr($this->hash, 0, $nonce) !== str_pad('', $nonce, "0") ) {
$this->no++;
$this->hash = $this->calc_hash();
}
}
}
Since we need to increase the difficulty every time the Hash, the calc_hash function remains the same, but we create a simple algorithm, that is, it does not accept the generated Hash code that does not meet the requirements.
Our requirement here is that the Hash code must start with 2 or 3 or 4 or even 5 consecutive "0".
Since the SHA256 algorithm never changes the output for an input string, it is imperative to change the input string to find a satisfactory output Hash.
Therefore, we write a function called mining_block function to repeat the process of "mining" the Hash code for the current Block.
And this mining difficulty depends on the $nonce variable.
If you increase the number of consecutive 0 digits, it takes a long time to loop to find a satisfactory Hash string.
Example:
$nonce = 2, it takes 0.001s to find a Hash with 2 consecutive zeros at the beginning.
$nonce = 3, it takes 0.5s to find a Hash with 3 consecutive 0s at the beginning.
$nonce = 4, it takes 3s to find a Hash with 4 consecutive 0s at the beginning.
$nonce = 5, it takes 20s to find a Hash with 5 consecutive zeros at the beginning.
For the BitCoin virtual currency, the difficulty of the algorithm is adjusted so that every 10 minutes there will be a Hash code.
Of course, BitCoin's algorithm is not as simple as the one here. So you already figured it out.
Proof-Of-Work is also abbreviated as PoW.
The PoW concept applied to the Blockchain is similar to the anti-SPAM and DDOS mechanism for a website using ReCapcha, users who want to verify that they are not an automatic robot have to spend time typing a strange string of characters.
//Author: Zidane - Webzone Tech Tips// Email: huuvi168@gmail.com
class Blockchain extends Block
{
public $chain = array();
public $nonce = 0;
public function __construct()
{
$this->chain[] = new Block(time(), "Genesis Block");
$this->nonce = 3;
}
public function get_latest_item()
{
return $this->chain[count($this->chain) - 1];
}
public function add_new_block($time, $data)
{
$previous_hash = $this->get_latest_item()->hash;
$block = new Block($time, $data, $previous_hash);
$start_mining_time = microtime(true);
$block->start_mining_time = gmdate("Y-m-d\TH:i:s\Z", (int)$start_mining_time);
$block->mining_block($this->nonce);
$end_mining_time = microtime(true);
$block->end_mining_time =gmdate("Y-m-d\TH:i:s\Z", (int)$end_mining_time);
$mining_time_stamp = $end_mining_time * 1000 - $start_mining_time * 1000;
$block->mining_time_stamp = $mining_time_stamp;
$this->chain[] = $block;
}
public function validate()
{
for ($i = 1; $i < count($this->chain); $i++) {
$current_block = $this->chain[$i];
$previous_block = $this->chain[$i - 1];
if ($current_block->hash != $current_block->calc_hash()) {
return false;
}
if ($current_block->previous_hash != $previous_block->hash) {
return false;
}
return true;
}
}
}
We need to create a variable $nonce in the Blockchain.
Then we call $block->mining_block($this->nonce); so that every time we create a new Block (add_new_block function), we have to dig a new Hash code for it.
Leave your comment if you need discuss anything about Blockchain technology
Learn Tech Tips - Zidane
Full source code you can checkout here - BlockchainsControllers_v2.php
Related Posts:
Blockchain Part 1: What is Blockchain: https://learn-tech-tips.blogspot.com/2021/12/what-is-blockchain.html
Blockchain Part 2: Using PHP develop Blockchain Technology: https://learn-tech-tips.blogspot.com/2021/12/using-php-develop-blockchain-technology.html
Blockchain Part 3: Loophples of the current Blockchain:
https://learn-tech-tips.blogspot.com/2021/12/loophples-of-the-current-blockchain.html
Blockchain Part 4: Create virtual currency mining technology:
Blockchain Part 5: How to create own bitcoin virtual currency
Blockchain Part 6: Apply blockchain application in the life