Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 374 additions & 0 deletions Blockchain/Scripts/Activate.ps1

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions Blockchain/Scripts/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r
fi

if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
if [ ! "${1:-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="C:\Users\user\Documents\CS\Blockchain\Blockchain"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
if [ "x(Blockchain) " != x ] ; then
PS1="(Blockchain) ${PS1:-}"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
fi
fi
export PS1
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r
fi
33 changes: 33 additions & 0 deletions Blockchain/Scripts/activate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@echo off

rem This file is UTF-8 encoded, so we need to update the current code page while executing it
for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do (
set _OLD_CODEPAGE=%%a
)
if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" 65001 > nul
)

set VIRTUAL_ENV=C:\Users\user\Documents\CS\Blockchain\Blockchain

if not defined PROMPT set PROMPT=$P$G

if defined _OLD_VIRTUAL_PROMPT set PROMPT=%_OLD_VIRTUAL_PROMPT%
if defined _OLD_VIRTUAL_PYTHONHOME set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%

set _OLD_VIRTUAL_PROMPT=%PROMPT%
set PROMPT=(Blockchain) %PROMPT%

if defined PYTHONHOME set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
set PYTHONHOME=

if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%
if not defined _OLD_VIRTUAL_PATH set _OLD_VIRTUAL_PATH=%PATH%

set PATH=%VIRTUAL_ENV%\Scripts;%PATH%

:END
if defined _OLD_CODEPAGE (
"%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul
set _OLD_CODEPAGE=
)
12 changes: 12 additions & 0 deletions Blockchain/Scripts/autopep8-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!C:\Users\user\Documents\CS\Blockchain\Blockchain\Scripts\python.exe
# EASY-INSTALL-ENTRY-SCRIPT: 'autopep8==1.5.1','console_scripts','autopep8'
__requires__ = 'autopep8==1.5.1'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('autopep8==1.5.1', 'console_scripts', 'autopep8')()
)
Binary file added Blockchain/Scripts/autopep8.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions Blockchain/Scripts/deactivate.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@echo off

if defined _OLD_VIRTUAL_PROMPT (
set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
)
set _OLD_VIRTUAL_PROMPT=

if defined _OLD_VIRTUAL_PYTHONHOME (
set "PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%"
set _OLD_VIRTUAL_PYTHONHOME=
)

if defined _OLD_VIRTUAL_PATH (
set "PATH=%_OLD_VIRTUAL_PATH%"
)

set _OLD_VIRTUAL_PATH=

set VIRTUAL_ENV=

:END
Binary file added Blockchain/Scripts/easy_install-3.8.exe
Binary file not shown.
Binary file added Blockchain/Scripts/easy_install.exe
Binary file not shown.
Binary file added Blockchain/Scripts/epylint.exe
Binary file not shown.
Binary file added Blockchain/Scripts/flake8.exe
Binary file not shown.
Binary file added Blockchain/Scripts/isort.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pip.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pip3.8.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pip3.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pycodestyle.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pyflakes.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pylint.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pyreverse.exe
Binary file not shown.
Binary file added Blockchain/Scripts/python.exe
Binary file not shown.
Binary file added Blockchain/Scripts/pythonw.exe
Binary file not shown.
Binary file added Blockchain/Scripts/symilar.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions Blockchain/pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
home = C:\Users\user\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0
include-system-site-packages = false
version = 3.8.2
48 changes: 35 additions & 13 deletions basic_block_gp/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,50 @@
import json
from time import time
from uuid import uuid4

from flask import Flask, jsonify, request


class Blockchain(object):
def __init__(self):
self.chain = []
self.current_transactions = []

# Create the genesis block
self.new_block(previous_hash=1, proof=100)

def new_block(self, proof, previous_hash=None):
"""
Create a new Block in the Blockchain

A block should have:
* Index
* Timestamp
* List of current transactions
* The proof used to mine this block
* The hash of the previous block

:param proof: <int> The proof given by the Proof of Work algorithm
:param previous_hash: (Optional) <str> Hash of previous Block
:return: <dict> New Block
"""

block = {
# TODO
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.last_block)
}

# Reset the current list of transactions
self.current_transaction = []
# Append the chain to the block
self.chain.append(block)
# Return the new block
pass

return block

def hash(self, block):
"""
Creates a SHA-256 hash of a Block

:param block": <dict> Block
"return": <str>
"""
Expand All @@ -57,16 +60,21 @@ def hash(self, block):

# TODO: Create the block_string

string_object = json.dumps(block, sort_keys=True)
block_string = string_object.encode()
# TODO: Hash this string using sha256
raw_hash = hashlib.sha256(block_string)

# By itself, the sha256 function returns the hash in a raw string
# that will likely include escaped characters.
# This can be hard to read, but .hexdigest() converts the
# hash to a string of hexadecimal characters, which is
# easier to work with and understand
hash_string = raw_hash.hexdigest()

# TODO: Return the hashed block string in hexadecimal format
pass

return hash_string

@property
def last_block(self):
Expand All @@ -81,8 +89,13 @@ def proof_of_work(self, block):
:return: A valid proof for the provided block
"""
# TODO
pass

# return proof
block_string = json.dumps(block, sort_keys=True)
proof = 0
while self.valid_proof(block_string, proof) is False:
proof += 1
return proof

@staticmethod
def valid_proof(block_string, proof):
Expand All @@ -97,27 +110,34 @@ def valid_proof(block_string, proof):
:return: True if the resulting hash is a valid proof, False otherwise
"""
# TODO
pass

print(f"I will now check if {proof} is valid")
guess = block_string + str(proof)
guess = guess.encode()

hash_value = hashlib.sha256(guess).hexdigest()
return hash_value[:3] == '000'
# return True or False


# Instantiate our Node
app = Flask(__name__)

# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')

# Instantiate the Blockchain
blockchain = Blockchain()


@app.route('/mine', methods=['GET'])
def mine():
# Run the proof of work algorithm to get the next proof

print("We shall now mine a block")
proof = blockchain.proof_of_work(blockchain.last_block)
print(f"After a long procees, we got a value {proof}")
# Forge the new Block by adding it to the chain with the proof

new_block = blockchain.new_block(proof)
response = {
'block': new_block
# TODO: Send a JSON response with the new block
}

Expand All @@ -128,6 +148,8 @@ def mine():
def full_chain():
response = {
# TODO: Return the chain and its current length
'len': len(blockchain.chain),
'chain': blockchain.chain
}
return jsonify(response), 200

Expand Down
Loading