Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Mateus Schmitz <matteuschmitz@gmail.com>
  4. * @package BlockchainPHP\Block
  5. */
  6. namespace BlockchainPHP;
  7. class Block
  8. {
  9. const PREVIOUS_GENESIS_HASH = '0000000000000000000000000000000000000000000000000000000000000000';
  10. const BLOCK_DATA_LIMIT = 50000;
  11. const HASH_ALGORITHM = 'sha256';
  12. private $timestamp;
  13. private $previousHash = self::PREVIOUS_GENESIS_HASH;
  14. private $blockHash;
  15. private $dataLength;
  16. private $data;
  17. /**
  18. * Sets up the data to be saved on block and instance the object
  19. *
  20. * @param mixed $data can be string/array/object
  21. */
  22. public function __construct($data)
  23. {
  24. $this->data = json_encode($data);
  25. $this->dataLength = strlen($this->data);
  26. $this->timestamp = strtotime('now');
  27. if ($this->dataLength > self::BLOCK_DATA_LIMIT) {
  28. throw new \InvalidArgumentException(
  29. "'Data' field can't have more than " . self::BLOCK_DATA_LIMIT . " characters"
  30. );
  31. }
  32. }
  33. /**
  34. * Generate the hash to the actual block
  35. *
  36. * @return bool
  37. */
  38. public function generateBlockHash()
  39. {
  40. if (!isset($this->timestamp)) {
  41. throw new \LogicException("Attribute 'timestamp' invalid");
  42. }
  43. if (!isset($this->previousHash)) {
  44. throw new \LogicException("Attribute 'previousHash' invalid.");
  45. }
  46. if (!isset($this->dataLength)) {
  47. throw new \LogicException("Attribute 'dataLength' invalid");
  48. }
  49. if (!isset($this->data)) {
  50. throw new \LogicException("Attribute 'data' invalid");
  51. }
  52. $dataToHash = $this->timestamp.$this->previousHash.$this->dataLength.$this->data;
  53. $this->blockHash = hash(self::HASH_ALGORITHM, $dataToHash);
  54. return true;
  55. }
  56. public function getTimestamp()
  57. {
  58. return $this->timestamp;
  59. }
  60. public function getPreviousHash()
  61. {
  62. return $this->previousHash;
  63. }
  64. public function getBlockHash()
  65. {
  66. return $this->blockHash;
  67. }
  68. public function getDataLength()
  69. {
  70. return $this->dataLength;
  71. }
  72. public function getData()
  73. {
  74. return $this->data;
  75. }
  76. public function setTimestamp($timestamp)
  77. {
  78. $this->timestamp = $timestamp;
  79. return $this;
  80. }
  81. public function setPreviousHash($previousHash)
  82. {
  83. $this->previousHash = $previousHash;
  84. return $this;
  85. }
  86. public function setBlockHash($blockHash)
  87. {
  88. $this->blockHash = $blockHash;
  89. return $this;
  90. }
  91. public function setDataLength($dataLength)
  92. {
  93. if ($dataLength > 50000) {
  94. throw new \InvalidArgumentException("'Data' field can't have more than 50000 characters");
  95. }
  96. $this->dataLength = $dataLength;
  97. return $this;
  98. }
  99. public function setData($data)
  100. {
  101. $this->data = $data;
  102. return $this;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement