How to create your own Crypto Blockchain.


SUBMITTED BY: godsend

DATE: Dec. 1, 2020, 4:54 p.m.

UPDATED: Feb. 12, 2021, 3:59 p.m.

FORMAT: Text only

SIZE: 14.7 kB

HITS: 1263

  1. Tips or donations can be made at 18e1561MhqrJwmb4tXuCU3V9cGtb3yJCSx further tips or hacks or tutorials on winning big money
  2. Contact Telegram @Passionateforcoin
  3. No charge, I accept only donations.
  4. Here is the basic blueprint of the Python class we’ll use for creating the blockchain:
  5. 1
  6. class Block(object):
  7. 2
  8. ​
  9. 3
  10. def __init__():
  11. 4
  12. ​
  13. 5
  14. pass
  15. 6
  16. ​
  17. 7
  18. #initial structure of the block class
  19. 8
  20. ​
  21. 9
  22. def compute_hash():
  23. 10
  24. ​
  25. 11
  26. pass
  27. 12
  28. ​
  29. 13
  30. #producing the cryptographic hash of each block
  31. 14
  32. ​
  33. 15
  34. class BlockChain(object):
  35. 16
  36. ​
  37. 17
  38. def __init__(self):
  39. 18
  40. ​
  41. 19
  42. #building the chain
  43. 20
  44. ​
  45. 21
  46. def build_genesis(self):
  47. 22
  48. ​
  49. 23
  50. pass
  51. 24
  52. ​
  53. 25
  54. #creating the initial block
  55. 26
  56. ​
  57. 27
  58. def build_block(self, proof_number, previous_hash):
  59. 28
  60. ​
  61. 29
  62. pass
  63. 30
  64. ​
  65. 31
  66. #builds new block and adds to the chain
  67. 32
  68. ​
  69. 33
  70. @staticmethod
  71. 34
  72. ​
  73. 35
  74. def confirm_validity(block, previous_block):
  75. 36
  76. ​
  77. 37
  78. pass
  79. 38
  80. ​
  81. 39
  82. #checks whether the blockchain is valid
  83. 40
  84. ​
  85. 41
  86. def get_data(self, sender, receiver, amount):
  87. 42
  88. ​
  89. 43
  90. pass
  91. 44
  92. ​
  93. 45
  94. # declares data of transactions
  95. 46
  96. ​
  97. 47
  98. @staticmethod
  99. 48
  100. ​
  101. 49
  102. def proof_of_work(last_proof):
  103. 50
  104. ​
  105. 51
  106. pass
  107. 52
  108. ​
  109. 53
  110. #adds to the security of the blockchain
  111. 54
  112. ​
  113. 55
  114. @property
  115. 56
  116. ​
  117. 57
  118. def latest_block(self):
  119. 58
  120. ​
  121. 59
  122. pass
  123. 60
  124. ​
  125. 61
  126. #returns the last block in the chain
  127. Now, let’s explain how the blockchain class works.
  128. Initial Structure of the Block Class
  129. Here is the code for our initial block class:
  130. 1
  131. import hashlib
  132. 2
  133. ​
  134. 3
  135. import time
  136. 4
  137. ​
  138. 5
  139. class Block(object):
  140. 6
  141. ​
  142. 7
  143. def __init__(self, index, proof_number, previous_hash, data, timestamp=None):
  144. 8
  145. ​
  146. 9
  147. self.index = index
  148. 10
  149. ​
  150. 11
  151. self.proof_number = proof_number
  152. 12
  153. ​
  154. 13
  155. self.previous_hash = previous_hash
  156. 14
  157. ​
  158. 15
  159. self.data = data
  160. 16
  161. ​
  162. 17
  163. self.timestamp = timestamp or time.time()
  164. 18
  165. ​
  166. 19
  167. @property
  168. 20
  169. ​
  170. 21
  171. def compute_hash(self):
  172. 22
  173. ​
  174. 23
  175. string_block = "{}{}{}{}{}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
  176. 24
  177. ​
  178. 25
  179. return hashlib.sha256(string_block.encode()).hexdigest()
  180. As you can see above, the class constructor or initiation method ( __init__()) above takes the following parameters:
  181. self — just like any other Python class, this parameter is used to refer to the class itself. Any variable associated with the class can be accessed using it.
  182. index — it’s used to track the position of a block within the blockchain.
  183. previous_hash — it used to reference the hash of the previous block within the blockchain.
  184. data—it gives details of the transactions done, for example, the amount bought.
  185. timestamp—it inserts a timestamp for all the transactions performed.
  186. The second method in the class, compute_hash , is used to produce the cryptographic hash of each block based on the above values.
  187. As you can see, we imported the SHA-256 algorithm into the cryptocurrency blockchain project to help in getting the hashes of the blocks.
  188. Once the values have been placed inside the hashing module, the algorithm will return a 256-bit string denoting the contents of the block.
  189. So, this is what gives the blockchain immutability. Since each block will be represented by a hash, which will be computed from the hash of the previous block, corrupting any block in the chain will make the other blocks have invalid hashes, resulting in breakage of the whole blockchain network.
  190. Building the Chain
  191. The whole concept of a blockchain is based on the fact that the blocks are “chained” to each other. Now, we’ll create a blockchain class that will play the critical role of managing the entire chain.
  192. It will keep the transactions data and include other helper methods for completing various roles, such as adding new blocks.
  193. Let’s talk about the helper methods.
  194. Adding the Constructor Method
  195. Here is the code:
  196. 1
  197. class BlockChain(object):
  198. 2
  199. ​
  200. 3
  201. def __init__(self):
  202. 4
  203. ​
  204. 5
  205. self.chain = []
  206. 6
  207. ​
  208. 7
  209. self.current_data = []
  210. 8
  211. ​
  212. 9
  213. self.nodes = set()
  214. 10
  215. ​
  216. 11
  217. self.build_genesis()
  218. The __init__() constructor method is what instantiates the blockchain.
  219. Here are the roles of its attributes:
  220. self.chain — this variable stores all the blocks.
  221. self.current_data — this variable stores information about the transactions in the block.
  222. self.build_genesis() — this method is used to create the initial block in the chain.
  223. Building the Genesis Block
  224. The build_genesis() method is used for creating the initial block in the chain, that is, a block without any predecessors. The genesis block is what represents the beginning of the blockchain.
  225. To create it, we’ll call the build_block() method and give it some default values. The parameters proof_number and previous_hash are both given a value of zero, though you can give them any value you desire.
  226. Here is the code:
  227. 1
  228. def build_genesis(self):
  229. 2
  230. ​
  231. 3
  232. self.build_block(proof_number=0, previous_hash=0)
  233. 4
  234. ​
  235. 5
  236. def build_block(self, proof_number, previous_hash):
  237. 6
  238. ​
  239. 7
  240. block = Block(
  241. 8
  242. ​
  243. 9
  244. index=len(self.chain),
  245. 10
  246. ​
  247. 11
  248. proof_number=proof_number,
  249. 12
  250. ​
  251. 13
  252. previous_hash=previous_hash,
  253. 14
  254. ​
  255. 15
  256. data=self.current_data
  257. 16
  258. ​
  259. 17
  260. )
  261. 18
  262. ​
  263. 19
  264. self.current_data = []
  265. 20
  266. ​
  267. 21
  268. self.chain.append(block)
  269. 22
  270. ​
  271. 23
  272. return block
  273. Confirming Validity of the Blockchain
  274. The confirm_validity method is critical in examining the integrity of the blockchain and making sure inconsistencies are lacking.
  275. As explained earlier, hashes are pivotal for realizing the security of the cryptocurrency blockchain, because any slight alteration in an object will result in the creation of an entirely different hash.
  276. Thus, the confirm_validity method utilizes a series of if statements to assess whether the hash of each block has been compromised.
  277. Furthermore, it also compares the hash values of every two successive blocks to identify any anomalies. If the chain is working properly, it returns true; otherwise, it returns false.
  278. Here is the code:
  279. 1
  280. def confirm_validity(block, previous_block):
  281. 2
  282. ​
  283. 3
  284. if previous_block.index + 1 != block.index:
  285. 4
  286. ​
  287. 5
  288. return False
  289. 6
  290. ​
  291. 7
  292. elif previous_block.compute_hash != block.previous_hash:
  293. 8
  294. ​
  295. 9
  296. return False
  297. 10
  298. ​
  299. 11
  300. elif block.timestamp <= previous_block.timestamp:
  301. 12
  302. ​
  303. 13
  304. return False
  305. 14
  306. ​
  307. 15
  308. return True
  309. Declaring Data of Transactions
  310. The get_data method is important in declaring the data of transactions on a block. This method takes three parameters (sender’s information, receiver’s information, and amount) and adds the transaction data to the self.current_data list.
  311. Here is the code:
  312. 1
  313. def get_data(self, sender, receiver, amount):
  314. 2
  315. ​
  316. 3
  317. self.current_data.append({
  318. 4
  319. ​
  320. 5
  321. 'sender': sender,
  322. 6
  323. ​
  324. 7
  325. 'receiver': receiver,
  326. 8
  327. ​
  328. 9
  329. 'amount': amount
  330. 10
  331. ​
  332. 11
  333. })
  334. 12
  335. ​
  336. 13
  337. return True
  338. Effecting the Proof of Work
  339. In blockchain technology, Proof of Work (PoW) refers to the complexity involved in mining or generating new blocks on the blockchain.
  340. For example, the PoW can be implemented by identifying a number that solves a problem whenever a user completes some computing work. Anyone on the blockchain network should find the number complex to identify but easy to verify — this is the main concept of PoW.
  341. This way, it discourages spamming and compromising the integrity of the network.
  342. In this article, we’ll illustrate how to include a Proof of Work algorithm in a blockchain cryptocurrency project.
  343. Finalizing With the Last Block
  344. Finally, the latest_block() helper method is used for retrieving the last block on the network, which is actually the current block.
  345. Here is the code:
  346. 1
  347. def latest_block(self):
  348. 2
  349. ​
  350. 3
  351. return self.chain[-1]
  352. Implementing Blockchain Mining
  353. Now, this is the most exciting section!
  354. Initially, the transactions are kept in a list of unverified transactions. Mining refers to the process of placing the unverified transactions in a block and solving the PoW problem. It can be referred to as the computing work involved in verifying the transactions.
  355. If everything has been figured out correctly, a block is created or mined and joined together with the others in the blockchain. If users have successfully mined a block, they are often rewarded for using their computing resources to solve the PoW problem.
  356. Here is the mining method in this simple cryptocurrency blockchain project:
  357. 1
  358. def block_mining(self, details_miner):
  359. 2
  360. ​
  361. 3
  362. self.get_data(
  363. 4
  364. ​
  365. 5
  366. sender="0", #it implies that this node has created a new block
  367. 6
  368. ​
  369. 7
  370. receiver=details_miner,
  371. 8
  372. ​
  373. 9
  374. quantity=1, #creating a new block (or identifying the proof number) is awarded with 1
  375. 10
  376. ​
  377. 11
  378. )
  379. 12
  380. ​
  381. 13
  382. last_block = self.latest_block
  383. 14
  384. ​
  385. 15
  386. last_proof_number = last_block.proof_number
  387. 16
  388. ​
  389. 17
  390. proof_number = self.proof_of_work(last_proof_number)
  391. 18
  392. ​
  393. 19
  394. ​
  395. 20
  396. ​
  397. 21
  398. last_hash = last_block.compute_hash
  399. 22
  400. ​
  401. 23
  402. block = self.build_block(proof_number, last_hash)
  403. 24
  404. ​
  405. 25
  406. ​
  407. 26
  408. ​
  409. 27
  410. return vars(block)
  411. 28
  412. ​
  413. 29
  414. ​
  415. Summary
  416. Here is the whole code for our crypto blockchain class in Python:
  417. 1
  418. import hashlib
  419. 2
  420. ​
  421. 3
  422. import time
  423. 4
  424. ​
  425. 5
  426. class Block(object):
  427. 6
  428. ​
  429. 7
  430. def __init__(self, index, proof_number, previous_hash, data, timestamp=None):
  431. 8
  432. ​
  433. 9
  434. self.index = index
  435. 10
  436. ​
  437. 11
  438. self.proof_number = proof_number
  439. 12
  440. ​
  441. 13
  442. self.previous_hash = previous_hash
  443. 14
  444. ​
  445. 15
  446. self.data = data
  447. 16
  448. ​
  449. 17
  450. self.timestamp = timestamp or time.time()
  451. 18
  452. ​
  453. 19
  454. @property
  455. 20
  456. ​
  457. 21
  458. def compute_hash(self):
  459. 22
  460. ​
  461. 23
  462. string_block = "{}{}{}{}{}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
  463. 24
  464. ​
  465. 25
  466. return hashlib.sha256(string_block.encode()).hexdigest()
  467. 26
  468. ​
  469. 27
  470. def __repr__(self):
  471. 28
  472. ​
  473. 29
  474. return "{} - {} - {} - {} - {}".format(self.index, self.proof_number, self.previous_hash, self.data, self.timestamp)
  475. 30
  476. ​
  477. 31
  478. class BlockChain(object):
  479. 32
  480. ​
  481. 33
  482. def __init__(self):
  483. 34
  484. ​
  485. 35
  486. self.chain = []
  487. 36
  488. ​
  489. 37
  490. self.current_data = []
  491. 38
  492. ​
  493. 39
  494. self.nodes = set()
  495. 40
  496. ​
  497. 41
  498. self.build_genesis()
  499. 42
  500. ​
  501. 43
  502. def build_genesis(self):
  503. 44
  504. ​
  505. 45
  506. self.build_block(proof_number=0, previous_hash=0)
  507. 46
  508. ​
  509. 47
  510. def build_block(self, proof_number, previous_hash):
  511. 48
  512. ​
  513. 49
  514. block = Block(
  515. 50
  516. ​
  517. 51
  518. index=len(self.chain),
  519. 52
  520. ​
  521. 53
  522. proof_number=proof_number,
  523. 54
  524. ​
  525. 55
  526. previous_hash=previous_hash,
  527. 56
  528. ​
  529. 57
  530. data=self.current_data
  531. 58
  532. ​
  533. 59
  534. )
  535. 60
  536. ​
  537. 61
  538. self.current_data = []
  539. 62
  540. ​
  541. 63
  542. self.chain.append(block)
  543. 64
  544. ​
  545. 65
  546. return block
  547. 66
  548. ​
  549. 67
  550. @staticmethod
  551. 68
  552. ​
  553. 69
  554. def confirm_validity(block, previous_block):
  555. 70
  556. ​
  557. 71
  558. if previous_block.index + 1 != block.index:
  559. 72
  560. ​
  561. 73
  562. return False
  563. 74
  564. ​
  565. 75
  566. elif previous_block.compute_hash != block.previous_hash:
  567. 76
  568. ​
  569. 77
  570. return False
  571. 78
  572. ​
  573. 79
  574. elif block.timestamp <= previous_block.timestamp:
  575. 80
  576. ​
  577. 81
  578. return False
  579. 82
  580. ​
  581. 83
  582. return True
  583. 84
  584. ​
  585. 85
  586. def get_data(self, sender, receiver, amount):
  587. 86
  588. ​
  589. 87
  590. self.current_data.append({
  591. 88
  592. ​
  593. 89
  594. 'sender': sender,
  595. 90
  596. ​
  597. 91
  598. 'receiver': receiver,
  599. 92
  600. ​
  601. 93
  602. 'amount': amount
  603. 94
  604. ​
  605. 95
  606. })
  607. 96
  608. ​
  609. 97
  610. return True
  611. 98
  612. ​
  613. 99
  614. @staticmethod
  615. 100
  616. ​
  617. 101
  618. def proof_of_work(last_proof):
  619. 102
  620. ​
  621. 103
  622. pass
  623. 104
  624. ​
  625. 105
  626. @property
  627. 106
  628. ​
  629. 107
  630. def latest_block(self):
  631. 108
  632. ​
  633. 109
  634. return self.chain[-1]
  635. 110
  636. ​
  637. 111
  638. def chain_validity(self):
  639. 112
  640. ​
  641. 113
  642. pass
  643. 114
  644. ​
  645. 115
  646. def block_mining(self, details_miner):
  647. 116
  648. ​
  649. 117
  650. self.get_data(
  651. 118
  652. ​
  653. 119
  654. sender="0", #it implies that this node has created a new block
  655. 120
  656. ​
  657. 121
  658. receiver=details_miner,
  659. 122
  660. ​
  661. 123
  662. quantity=1, #creating a new block (or identifying the proof number) is awared with 1
  663. 124
  664. ​
  665. 125
  666. )
  667. 126
  668. ​
  669. 127
  670. last_block = self.latest_block
  671. 128
  672. ​
  673. 129
  674. last_proof_number = last_block.proof_number
  675. 130
  676. ​
  677. 131
  678. proof_number = self.proof_of_work(last_proof_number)
  679. 132
  680. ​
  681. 133
  682. last_hash = last_block.compute_hash
  683. 134
  684. ​
  685. 135
  686. block = self.build_block(proof_number, last_hash)
  687. 136
  688. ​
  689. 137
  690. return vars(block)
  691. 138
  692. ​
  693. 139
  694. def create_node(self, address):
  695. 140
  696. ​
  697. 141
  698. self.nodes.add(address)
  699. 142
  700. ​
  701. 143
  702. return True
  703. 144
  704. ​
  705. 145
  706. @staticmethod
  707. 146
  708. ​
  709. 147
  710. def get_block_object(block_data):
  711. 148
  712. ​
  713. 149
  714. return Block(
  715. 150
  716. ​
  717. 151
  718. block_data['index'],
  719. 152
  720. ​
  721. 153
  722. block_data['proof_number'],
  723. 154
  724. ​
  725. 155
  726. block_data['previous_hash'],
  727. 156
  728. ​
  729. 157
  730. block_data['data'],
  731. 158
  732. ​
  733. 159
  734. timestamp=block_data['timestamp']
  735. 160
  736. ​
  737. 161
  738. )
  739. 162
  740. ​
  741. 163
  742. blockchain = BlockChain()
  743. 164
  744. ​
  745. 165
  746. print("GET READY MINING ABOUT TO START")
  747. 166
  748. ​
  749. 167
  750. print(blockchain.chain)
  751. 168
  752. ​
  753. 169
  754. last_block = blockchain.latest_block
  755. 170
  756. ​
  757. 171
  758. last_proof_number = last_block.proof_number
  759. 172
  760. ​
  761. 173
  762. proof_number = blockchain.proof_of_work(last_proof_number)
  763. 174
  764. ​
  765. 175
  766. blockchain.get_data(
  767. 176
  768. ​
  769. 177
  770. sender="0", #this means that this node has constructed another block
  771. 178
  772. ​
  773. 179
  774. receiver="LiveEdu.tv",
  775. 180
  776. ​
  777. 181
  778. amount=1, #building a new block (or figuring out the proof number) is awarded with 1
  779. 182
  780. ​
  781. 183
  782. )
  783. 184
  784. ​
  785. 185
  786. last_hash = last_block.compute_hash
  787. 186
  788. ​
  789. 187
  790. block = blockchain.build_block(proof_number, last_hash)
  791. 188
  792. ​
  793. 189
  794. print("WOW, MINING HAS BEEN SUCCESSFUL!")
  795. 190
  796. ​
  797. 191
  798. print(blockchain.chain)
  799. Now, let’s try to run our code to see if we can generate some digital coins...

comments powered by Disqus