A mini blockchain in python
Here is a little exemple
# create a new BlockChain with difficulty 4
chain = Chain(4)
# add a new block containing the transactions :
# "3 from Bob to Claris"
# "2 from Claris to Jean"
chain.addBlock( Block("3 from Bob to Claris, 2 from Claris to Jean") )
# check validity
chain.isChainValid()
# true
# let's try to modify the chain
chain.blocks[0].transaction = "9 from Bob to Claris, 9 from Claris to Jean"
chain.blocks[0].mine(chain.diff)
# check validity again
chain.isChainValid()
# false
Block class.
- parameters :
- data :
string
<= transactions stored in the block
- data :
- fields
- index:
int
index of the block - hash:
string
hash of the block - prevHash:
string
hash of the previous block - data:
string
transactions written in the block - nonce:
int
nonce (to mine) - timestamp:
string
date of creation
- index:
- methods
- getHash:
string
- checkDiff:
boolean
- mine:
string
- getHash:
Calculate the hahs according to : self.index, self.nonce, self.data, self.prevHash, self.timestamp
- parameters : none
- return :
string
=> self.hash
Check if the hash matches with the difficulty
- parameters:
- diff :
int
<= number of 0 needed at the begining of the hash
- diff :
- return :
string
=> self.hash
Increment the nonce to have a valid hash (according to a specific difficulty)
- parameters:
- diff :
int
<= number of 0 needed at the begining of the hash
- diff :
- return :
string
=> self.hash
Chain class (block container)
- parametres :
- diff :
int
difficulty to mine a block
- diff :
- fields :
- diff :
int
difficulty to mine a block - blocks :
array
container for all blocks - number :
int
actual number of blocks
- diff :
- methods :
- createGenesis :
none
- addBlock:
none
- getPrevHash :
none
- isChainValid :
boolean
- printChain :
none
- createGenesis :
Init the chain with a genesis block
- parameters : none
- return : none
Add a new block to the chain. (it mines the block before)
- parameters :
- block :
Block
- block :
- return : none
Compute hash of the last block and return it.
- parameters : none
- return :
string
=> hash of the last block
Check if all blocks match the difficulty and if all blocks have a valid hash.
- parameters : none
- return :
boolean
Display all blocks of the chain in the console.
- parameters : none
- return : none
Just to have fun, we can try to cheat the chain with the cheater module.
Replace data in a specific block and reset hashs to have a valid chain again.
- parameters :
- chain :
Chain
<= chain to modify - index :
int
<= index of the targeted block - blockData :
string
<= fake data to change
- chain :
- return : none
chain = Chain(4)
# add a new block containing the transactions :
# "3 from Bob to Claris"
# "2 from Claris to Jean"
chain.addBlock( Block("3 from Bob to Claris, 2 from Claris to Jean") )
# check validity
chain.isChainValid()
# TRUE
# let's try to modify the chain
cheat(chain, 0, "9 from Bob to Claris, 9 from Claris to Jean")
# check validity again
chain.isChainValid()
# TRUE <= success