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());

Leave a Comment

Your email address will not be published. Required fields are marked *

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