Blockchain

Blockchain ဆိုတာ ဘာလဲ သိခ်င္လို႔ ဖတ္ၾကည့္ေတာ့ Block Chain ႏွင့္ distributed ledger မတူဘူး ဆိုတာကို နားလည္လာတယ္။ Blockchain ကေတာ့ ႐ိုး႐ိုးရွင္းရွင္းေလးပါပဲ။ Block တစ္ခု နဲ႔ တစ္ခု ကို ခ်ိတ္ဆက္ထားသည့္ သေဘာေလးပါပဲ။ Block တိုင္းမွာ hash တစ္ခုပါတယ္။ ေနာက္ၿပီး အေရွ႕ဘက္က hash ပါရွိတယ္။ idea ေလးက ႐ိုးရွင္းၿပီး တကယ္ကို အသုံးဝင္ပါတယ္။ crypto currency ေတြျဖစ္သည့္ bitcoin, ethereum အေၾကာင္းကိုေတာ့ ထည့္ၿပီး မေျပာေတာ့ပါဘူး။


Code ေလးကို တစ္ခ်က္ေလာက္ၾကည့္ၾကည့္ရေအာင္

class Block {
    public $data = array();
    public $hash = "";
    public $prevHash = "";
    public $n = 0;

    public function __construct($data) {
        $this->data = $data;
        $this->hash = $this->calculateHash();
    }
    public function calculateHash()
    {
        $d = json_encode($this->data);
        return hash('sha256', $d . $this->n . "comquasBlock");
    }


}

Block class မွာ data ပါတယ္။ hash ပါတယ္။ $n ကေတာ့ ေနာက္ပိုင္း မွာ လိုခ်င္သည့္ hash ပုံစံ တစ္ခု ထြက္လာေအာင္ပါ။

အခု block ကို ထည့္ဖို႔ အတြက္ chain class တစ္ခု ေဆာက္ပါမယ္။

class Chain {
    public $blocks =  array();
    
    function addBlock($block) {
        $total = count($this->blocks);
        if($total == 0) {
                $block->prevHash = "";
            array_push($this->blocks,$block);
        }
        else {
                        $lastBlock = $this->blocks[$total - 1];
                  $block->prevHash = $lastBlock->hash;
                    array_push($this->blocks,$block);            
        }
    }
}
$chain = new Chain();
$blockA = new Block(array("from" => "bob", "to" => "alice","amount"=>100));
$blockB = new Block(array("from" => "alice", "to" => "kk","amount"=>70));
$blockC = new Block(array("from" => "kk", "to" => "bob","amount"=>10));

$chain->addBlock($blockA);
$chain->addBlock($blockB);
$chain->addBlock($blockC);
var_dump($chain->blocks);

ဒါဆိုရင္ result က အခုလို ျမင္ရပါမယ္။

array(3) {
  [0]=>
  object(Block)#2 (4) {
    ["data"]=>
    array(3) {
      ["from"]=>
      string(3) "bob"
      ["to"]=>
      string(5) "alice"
      ["amount"]=>
      int(100)
    }
    ["hash"]=>
    string(64) "3d556697e2f530d8848c8281ec7b88b263d7c806d8551b502eeb8269b69e1227"
    ["prevHash"]=>
    string(0) ""
    ["n"]=>
    int(0)
  }
  [1]=>
  object(Block)#3 (4) {
    ["data"]=>
    array(3) {
      ["from"]=>
      string(5) "alice"
      ["to"]=>
      string(2) "kk"
      ["amount"]=>
      int(70)
    }
    ["hash"]=>
    string(64) "020bb51721077198d8ed82a7fb733d94eb7ed6c3ce98e48d256c883bda72b4e7"
    ["prevHash"]=>
    string(64) "3d556697e2f530d8848c8281ec7b88b263d7c806d8551b502eeb8269b69e1227"
    ["n"]=>
    int(0)
  }
  [2]=>
  object(Block)#4 (4) {
    ["data"]=>
    array(3) {
      ["from"]=>
      string(2) "kk"
      ["to"]=>
      string(3) "bob"
      ["amount"]=>
      int(10)
    }
    ["hash"]=>
    string(64) "7232d38c2427d861ea6901dd96ab4056d255092a87c097c1a4f27c87b3674ecb"
    ["prevHash"]=>
    string(64) "020bb51721077198d8ed82a7fb733d94eb7ed6c3ce98e48d256c883bda72b4e7"
    ["n"]=>
    int(0)
  }
}

အခုဆိုရင္ေတာ့ prevHash နဲ႔ hash က ခ်ိတ္ဆက္မႈ ရွိသြားပါၿပီ။ ေနာက္ထပ္ function တစ္ခု ထပ္ျဖည့္ပါမယ္။

function validate() {

        for($i=1; $i < count($this->blocks);$i++) {
            $prevBlock = $this->blocks[$i-1];
            $currentBlock = $this->blocks[$i];
            
            if($currentBlock->hash != $currentBlock->calculateHash()) {
                return false;
            }
           
            if($currentBlock->prevHash != $prevBlock->hash) {
                return false;
            }
            
        }

        return true;
}

အခုဆိုရင္ ကြၽန္ေတာ္တို႔ block chain ဟာ validate ဟုတ္မဟုတ္ စစ္လို႔ ရပါၿပီ။ validate function ကေတာ့ ႐ိုးရွင္းပါတယ္။ hash ကို မွန္မမွန္ စစ္တယ္။ ေနာက္ၿပီး အရင္ hash နဲ႔ အခု hash တူမတူ စစ္ပါတယ္။

$chain = new Chain();
$blockA = new Block(array("from" => "bob", "to" => "alice","amount"=>100));
$blockB = new Block(array("from" => "alice", "to" => "kk","amount"=>70));
$blockC = new Block(array("from" => "kk", "to" => "bob","amount"=>10));

$chain->addBlock($blockA);
$chain->addBlock($blockB);
$chain->addBlock($blockC);

var_dump($chain->validate());
$chain->blocks[1] = new Block(array("from" => "alice", "to" => "kk","amount"=>10));
var_dump($chain->validate());

အခု get amount တစ္ခု ထပ္ျဖည့္ပါမယ္။

function getAmount($name) {

                $fromValue = 0;
                $toValue = 0;

                for($i=0; $i < count($this->blocks);$i++) {
                    $block = $this->blocks[$i];

                    if ($block->data["from"] == $name) {
                        $fromValue = $fromValue + $block->data["amount"];
                    }

                    if ($block->data["to"] == $name) {
                        $toValue = $toValue + $block->data["amount"];
                    }

                   
                }
                return ($toValue - $fromValue);
                
}

အခုဆိုရင္ ကြၽန္ေတာ္တို႔ class က လူ နာမည္နဲ႔ block chain ထဲမွာ amount ဘယ္ေလာက္ ရွိလဲ ရွာလို႔ ရပါၿပီ။

echo $chain->getAmount("alice");

အခု code က block chain ဆိုတာ ဘာလဲ နားလည္သေဘာေပါက္ ႐ုံပါပဲ။ Crypto currency အပိုင္းေတြ အတြက္ distributed ledger ပိုင္းေတြ အျပင္ အျခား အပိုင္းေတြလည္း ေလ့လာဖို႔ လိုပါအုံးမယ္။ Code အျပည့္အစုံကေတာ့

<?php
class Block {
    public $data = array();
    public $hash = "";
    public $prevHash = "";
    public $n = 0;

    public function __construct($data) {
        $this->data = $data;
        $this->hash = $this->calculateHash();
    }
    public function calculateHash()
    {
        $d = json_encode($this->data);
        return hash('sha256', $d . $this->n . "comquasBlock");
    }


}
class Chain {
    public $blocks =  array();
    
    function addBlock($block) {
        $total = count($this->blocks);
        if($total == 0) {
                $block->prevHash = "";
            array_push($this->blocks,$block);
        }
        else {
                        $lastBlock = $this->blocks[$total - 1];
                  $block->prevHash = $lastBlock->hash;
                    array_push($this->blocks,$block);            
        }
    }
    
    function validate() {

            for($i=1; $i < count($this->blocks);$i++) {
                $prevBlock = $this->blocks[$i-1];
                $currentBlock = $this->blocks[$i];
                
                if($currentBlock->hash != $currentBlock->calculateHash()) {
                    return false;
                }
               
                if($currentBlock->prevHash != $prevBlock->hash) {
                    return false;
                }
                
            }

            return true;
        }
        
        function getAmount($name) {

                $fromValue = 0;
                $toValue = 0;

                for($i=0; $i < count($this->blocks);$i++) {
                    $block = $this->blocks[$i];

                    if ($block->data["from"] == $name) {
                        $fromValue = $fromValue + $block->data["amount"];
                    }

                    if ($block->data["to"] == $name) {
                        $toValue = $toValue + $block->data["amount"];
                    }

                   
                }
                return ($toValue - $fromValue);
                
            }
}
$chain = new Chain();
$blockA = new Block(array("from" => "bob", "to" => "alice","amount"=>100));
$blockB = new Block(array("from" => "alice", "to" => "kk","amount"=>70));
$blockC = new Block(array("from" => "kk", "to" => "bob","amount"=>10));

$chain->addBlock($blockA);
$chain->addBlock($blockB);
$chain->addBlock($blockC);

echo $chain->getAmount("alice");
var_dump($chain->validate());
$chain->blocks[1] = new Block(array("from" => "alice", "to" => "kk","amount"=>10));
var_dump($chain->validate());

 
29 Kudos
Don't
move!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.