block chain

A blockchain includes a group of serilized blocks. The start block is called the genesis block.

Functions

Blockchain

Block

Mine

Peer

Message


interface Blockchain {
	public function get();
	public function length();
	public function last();
	<!-- public funciton genesis(); -->
	public function first();
	public function generateBlock();
}

interface BlockInterface {

}

class Block impliment BlockInterface {
	private $index; // number of block, start with 0
	private $previousHash; // previous block hash
	private $datetime;
	private $data;
	private $hash; // sha-256
	private $nonce;

	public function __construct(int $index, string $previousHash, Carbon $datetime, string $data, int $nonce)
	{
		$this->index = $index;
		$this->previousHash = $previousHash;
		$this->datetime = $datetime;
		$this->data = $data;

		$this->hash();
	}

	private hash()
	{
		$this->hash = \hash('sha256', $this->index.$this->previousHash.$this->timestamp.$this->data.$this->nonce);
	}

	private checkHashDifficulty()
	{

	}


}

const Block = require("./Block.js");

class Blockchain {
  constructor () {
    this.blockchain = [Block.genesis()];
  }

  get() {
    return this.blockchain;
  }

  get latestBlock() {
    return this.blockchain[this.blockchain.length - 1];
  }
};

module.exports = Blockchain;
isValidHashDifficulty(hash) {
    for (var i = 0; i < hash.length; i++) {
      if (hash[i] !== "0") {
        break;
      }
    }
    return i >= this.difficulty;
  }


generateNewBlock(data) {
    const newIndex = this.latestBlock.index + 1;
    const previousHash = this.latestBlock.hash;
    let timestamp = new Date().getTime();
    let nonce = 0;
    let newHash = this.calculateHash(
      newIndex,
      previousHash,
      timestamp,
      data,
      nonce
    );

    while (!this.isValidHashDifficulty(newHash)) {
      nonce = nonce + 1;
      timestamp = new Date().getTime();
      newHash = this.calculateHash(
        newIndex,
        previousHash,
        timestamp,
        data,
        nonce
      );
    }

    const newBlock = new Block(
      newIndex,
      previousBlock.hash,
      newTimestamp,
      data,
      newHash,
      nonce
    );

    return newBlock;
  }

Ref