So while I'm having a calculation of my research, napaisip nalang ako bigla na what if gumawa ako ng basic na blockchain using Programming Language? I know that some of you already know this pero syempre para sa mga hindi pa nakakaalam and marunong gumamit ng programming language, you can actually try this!
The programming language that I've used is
Python but there's a twist on my experiment. Ginagamit ko kasi yung
Raspberry Pi ko at the moment so built in with Python na siya, bakit pa ako magta-try sa PC if meron naman sa RPi. For those people na magtatanong kung anong gamit kong model ng Rpi, Raspberry Pi 3 B+ po ang gamit ko.
Note: It's not necessary to buy an RPI for this experiment, sadyang nagustuhan ko lang since gamit ko siya rn.So para saan ba ito?It's a great experience also at madaling gawin and the most important thing is malalaman niyo yung bawat terms kasi alam niyo kung paano nagwowork.
--------
So ito na nga.
I tried the codes that I saw on github and put it on python, and this is the terminal of
Raspberry Pi using
PuTTy.
These are the codes: import datetime
import hashlib
class Block:
blockNo = 0
data = None
next = None
hash = None
nonce = 0
previous_hash = 0x0
timestamp = datetime.datetime.now()
def __init__(self, data):
self.data = data
def hash(self):
h = hashlib.sha256()
h.update(
str(self.nonce).encode('utf-8') +
str(self.data).encode('utf-8') +
str(self.previous_hash).encode('utf-8') +
str(self.timestamp).encode('utf-8') +
str(self.blockNo).encode('utf-8')
)
return h.hexdigest()
def __str__(self):
return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------"
class Blockchain:
diff = 20
maxNonce = 2**32
target = 2 ** (256-diff)
block = Block("Genesis")
dummy = head = block
def add(self, block):
block.previous_hash = self.block.hash()
block.blockNo = self.block.blockNo + 1
self.block.next = block
self.block = self.block.next
def mine(self, block):
for n in range(self.maxNonce):
if int(block.hash(), 16) <= self.target:
self.add(block)
print(block)
break
else:
block.nonce += 1
blockchain = Blockchain()
for n in range(10):
blockchain.mine(Block("Block " + str(n+1)))
while blockchain.head != None:
print(blockchain.head)
blockchain.head = blockchain.head.next
credits to howCodeORG of github.The output of the blockchain.py with the diff=20, As you can see it has a huge amount of Hashes.------------
Block Hash: 099f56cb4679e8269967eb4dd0408f64df67f050c77b10935cb32b7a7958ca11
BlockNo: 1
Block Data: Block 1
Hashes: 161183
------------
Block Hash: d4df2f16e65810d9a8aaae38e004f2b808e335381453e6ad22c7c875c64d55e2
BlockNo: 2
Block Data: Block 2
Hashes: 318974
------------
and so on...The output of the blockchain.py with the diff=0, the Hashes are 0.The hash of the block must be less than to the actual target for it to worked.
So this is it! I made a simple blockchain and learned it while trying to make it in my RPi. There are so many amazing codes that are available on github, pwede mong magamit yung python, c+, or java. I'll just want to share this simple thing, thanks for reading!