Skip to content

Commit

Permalink
Fix directory structures
Browse files Browse the repository at this point in the history
Signed-off-by: Anurag Tiwari <[email protected]>
  • Loading branch information
t2013anurag committed Oct 4, 2019
1 parent 33267fd commit e393b8b
Show file tree
Hide file tree
Showing 17 changed files with 1,237 additions and 0 deletions.
46 changes: 46 additions & 0 deletions basic_blockchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Basics of Blockchain
---
This is a very simple code to learn about blockchain technology.
Blockchain is a digital ledger, a public database, in which the transactions made are accessible to all the users in
the blockchain using it, but is cryptographically secured. This digital ledger is maintained in distributed form.

Each record is stored in the form of blocks which is linked together to form a long chain of blocks called blockchain.
Blocks generally includes information about the index of the block in the chain, timestamp of when block was created,
previous block ie. previous hash, some data (like transactions), etc. The new hash of the block is generated with all
the information in the block that makes it tamperproof. The hash of the block is distributed in all the nodes in the
blockchain, thus making sure the integrity of the blocks is maintained and if a block is tampered all the nodes in the
chain knows it.
```
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self._index = index
self._timestamp = timestamp
self._data = data
self._previous_hash = previous_hash
self._nounce = None
self._hash = self.hash()
```

This example includes index, timestamp, data, previous_hash, nounce and hash.

The first block in the blockchain is generated manually or is hardcoded and included in the chain. The initial block is
called Genesis Block since initial block doesn't have the information of the previous block.
```
def genesis_block():
return Block(0, time.time(), "This is genesis block", '0')
```
What is mining?
Mining is to create a valid new block and add it into the blockchain.

Checking valid block for blockchain:
```
def valid_hash(self, the_hash):
return the_hash.startswith('0000')
```

In this simple blockchain, we add the blocks with a loop included in the code. To create a new block in blockchain a
simple task is created, ie. to create a new valid block in blockchain, the hash created must have preceeding 0000 for
new block, the content of the block is changed by a nounce value which is incremented until the hash of the block does
not have preceeding 0000.

Right now the code works for a machine only. But overview of how a blockchain works should be clear.
79 changes: 79 additions & 0 deletions basic_blockchain/python/Blockchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import time, hashlib, json


"""
Created by trustgeek on 2017-10-23 (11:04).
"""

class Block:
def __init__(self, index, timestamp, data, previous_hash):
self._index = index
self._timestamp = timestamp
self._data = data
self._previous_hash = previous_hash
self._nounce = None
self._hash = self.hash()

def valid_hash(self, the_hash):
return the_hash.startswith('0000')

def mined(self):
return self._nounce is not None

def mine(self):
start_time = time.time()
the_nounce = self._nounce or 0
while True:
the_hash = self.hash(nonce=the_nounce)
if self.valid_hash(the_hash):
self._hash = the_hash
self._nounce = the_nounce
end_time = time.time()
time_taken = end_time - start_time
print("----- Mine time: %s -----" % time_taken)
return
else:
the_nounce += 1

def hash(self, nonce=None):
self._nounce = nonce or self._nounce
block_json = {'index': self._index, 'timestamp': self._timestamp, 'data': self._data,
'previous_hash': self._previous_hash, 'nounce': self._nounce}

return hashlib.sha256(json.dumps(block_json).encode('utf-8')).hexdigest()

def __str__(self):
return json.dumps(
{'index': self._index, 'timestamp': self._timestamp, 'data': self._data,
'previous_hash': self._previous_hash,
'hash': self._hash, 'nounce': self._nounce})


# genesis block is the first block in the blockchain
def genesis_block():
return Block(0, time.time(), "This is genesis block", '0')


# create next block for blockchain
def next_block(_previous_block, data):
_new_index = _previous_block._index + 1
_new_timestamp = time.time()
_previous_hash = _previous_block._hash
return Block(_new_index, _new_timestamp, data, _previous_hash)


blockchain = [genesis_block()]

# first block for blockchain
previous_block = blockchain[0]
num_of_blocks = 20

# iteratively adding block to blockchain
for i in range(0, num_of_blocks):
transaction = {'from': '0x12345' + str(i), 'to': '0x54321' + str(i * 3), 'amount': 100}
new_block = next_block(previous_block, transaction)
new_block.mine()
blockchain.append(new_block)
previous_block = new_block
print("Block added to blockchain")
print(new_block)
99 changes: 99 additions & 0 deletions file_transfer_ftp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
Loading

0 comments on commit e393b8b

Please sign in to comment.