Refactor and new features
- Remove csharp and js implementations - Add multiprocessed mining - Add balance querying
This commit is contained in:
parent
1369cac209
commit
624a205a85
|
@ -1,63 +0,0 @@
|
||||||
###############################################################################
|
|
||||||
# Set default behavior to automatically normalize line endings.
|
|
||||||
###############################################################################
|
|
||||||
* text=auto
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# Set default behavior for command prompt diff.
|
|
||||||
#
|
|
||||||
# This is need for earlier builds of msysgit that does not have it on by
|
|
||||||
# default for csharp files.
|
|
||||||
# Note: This is only used by command line
|
|
||||||
###############################################################################
|
|
||||||
#*.cs diff=csharp
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# Set the merge driver for project and solution files
|
|
||||||
#
|
|
||||||
# Merging from the command prompt will add diff markers to the files if there
|
|
||||||
# are conflicts (Merging from VS is not affected by the settings below, in VS
|
|
||||||
# the diff markers are never inserted). Diff markers may cause the following
|
|
||||||
# file extensions to fail to load in VS. An alternative would be to treat
|
|
||||||
# these files as binary and thus will always conflict and require user
|
|
||||||
# intervention with every merge. To do so, just uncomment the entries below
|
|
||||||
###############################################################################
|
|
||||||
#*.sln merge=binary
|
|
||||||
#*.csproj merge=binary
|
|
||||||
#*.vbproj merge=binary
|
|
||||||
#*.vcxproj merge=binary
|
|
||||||
#*.vcproj merge=binary
|
|
||||||
#*.dbproj merge=binary
|
|
||||||
#*.fsproj merge=binary
|
|
||||||
#*.lsproj merge=binary
|
|
||||||
#*.wixproj merge=binary
|
|
||||||
#*.modelproj merge=binary
|
|
||||||
#*.sqlproj merge=binary
|
|
||||||
#*.wwaproj merge=binary
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# behavior for image files
|
|
||||||
#
|
|
||||||
# image files are treated as binary by default.
|
|
||||||
###############################################################################
|
|
||||||
#*.jpg binary
|
|
||||||
#*.png binary
|
|
||||||
#*.gif binary
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# diff behavior for common document formats
|
|
||||||
#
|
|
||||||
# Convert binary document formats to text before diffing them. This feature
|
|
||||||
# is only available from the command line. Turn it on by uncommenting the
|
|
||||||
# entries below.
|
|
||||||
###############################################################################
|
|
||||||
#*.doc diff=astextplain
|
|
||||||
#*.DOC diff=astextplain
|
|
||||||
#*.docx diff=astextplain
|
|
||||||
#*.DOCX diff=astextplain
|
|
||||||
#*.dot diff=astextplain
|
|
||||||
#*.DOT diff=astextplain
|
|
||||||
#*.pdf diff=astextplain
|
|
||||||
#*.PDF diff=astextplain
|
|
||||||
#*.rtf diff=astextplain
|
|
||||||
#*.RTF diff=astextplain
|
|
12
.travis.yml
12
.travis.yml
|
@ -1,12 +0,0 @@
|
||||||
language: python
|
|
||||||
|
|
||||||
python:
|
|
||||||
- 3.6
|
|
||||||
- nightly
|
|
||||||
|
|
||||||
install:
|
|
||||||
- pip install pipenv
|
|
||||||
- pipenv install --dev
|
|
||||||
|
|
||||||
script:
|
|
||||||
- pipenv run python -m unittest
|
|
20
Dockerfile
20
Dockerfile
|
@ -1,15 +1,13 @@
|
||||||
FROM python:3.6-alpine
|
FROM python:3.8-alpine
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
EXPOSE 80
|
||||||
|
ENV FLASK_APP=run:app \
|
||||||
|
FLASK_ENV=production \
|
||||||
|
PYTHONUNBUFFERED=1
|
||||||
|
CMD ["flask", "run", "-p", "80", "-h", "0.0.0.0"]
|
||||||
|
|
||||||
# Install dependencies.
|
ADD requirements.txt run.py ./
|
||||||
ADD requirements.txt /app
|
RUN pip install -r requirements.txt
|
||||||
RUN cd /app && \
|
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
# Add actual source code.
|
ADD blockchain/ ./blockchain/
|
||||||
ADD blockchain.py /app
|
|
||||||
|
|
||||||
EXPOSE 5000
|
|
||||||
|
|
||||||
CMD ["python", "blockchain.py", "--port", "5000"]
|
|
||||||
|
|
15
Pipfile
15
Pipfile
|
@ -1,15 +0,0 @@
|
||||||
[[source]]
|
|
||||||
url = "https://pypi.python.org/simple"
|
|
||||||
verify_ssl = true
|
|
||||||
name = "pypi"
|
|
||||||
|
|
||||||
[dev-packages]
|
|
||||||
|
|
||||||
[requires]
|
|
||||||
python_version = "3.6"
|
|
||||||
|
|
||||||
[packages]
|
|
||||||
|
|
||||||
flask = "==0.12.2"
|
|
||||||
requests = "==2.18.4"
|
|
||||||
|
|
|
@ -1,109 +0,0 @@
|
||||||
{
|
|
||||||
"_meta": {
|
|
||||||
"hash": {
|
|
||||||
"sha256": "45af71184b5b013f58a8601f7f87c9f0b882afe19f197ce45d6d08e46d615159"
|
|
||||||
},
|
|
||||||
"host-environment-markers": {
|
|
||||||
"implementation_name": "cpython",
|
|
||||||
"implementation_version": "3.6.2",
|
|
||||||
"os_name": "posix",
|
|
||||||
"platform_machine": "x86_64",
|
|
||||||
"platform_python_implementation": "CPython",
|
|
||||||
"platform_release": "4.10.0-35-generic",
|
|
||||||
"platform_system": "Linux",
|
|
||||||
"platform_version": "#39~16.04.1-Ubuntu SMP Wed Sep 13 09:02:42 UTC 2017",
|
|
||||||
"python_full_version": "3.6.2",
|
|
||||||
"python_version": "3.6",
|
|
||||||
"sys_platform": "linux"
|
|
||||||
},
|
|
||||||
"pipfile-spec": 6,
|
|
||||||
"requires": {
|
|
||||||
"python_version": "3.6"
|
|
||||||
},
|
|
||||||
"sources": [
|
|
||||||
{
|
|
||||||
"name": "pypi",
|
|
||||||
"url": "https://pypi.python.org/simple",
|
|
||||||
"verify_ssl": true
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"default": {
|
|
||||||
"certifi": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:54a07c09c586b0e4c619f02a5e94e36619da8e2b053e20f594348c0611803704",
|
|
||||||
"sha256:40523d2efb60523e113b44602298f0960e900388cf3bb6043f645cf57ea9e3f5"
|
|
||||||
],
|
|
||||||
"version": "==2017.7.27.1"
|
|
||||||
},
|
|
||||||
"chardet": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691",
|
|
||||||
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"
|
|
||||||
],
|
|
||||||
"version": "==3.0.4"
|
|
||||||
},
|
|
||||||
"click": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d",
|
|
||||||
"sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b"
|
|
||||||
],
|
|
||||||
"version": "==6.7"
|
|
||||||
},
|
|
||||||
"flask": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:0749df235e3ff61ac108f69ac178c9770caeaccad2509cb762ce1f65570a8856",
|
|
||||||
"sha256:49f44461237b69ecd901cc7ce66feea0319b9158743dd27a2899962ab214dac1"
|
|
||||||
],
|
|
||||||
"version": "==0.12.2"
|
|
||||||
},
|
|
||||||
"idna": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4",
|
|
||||||
"sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f"
|
|
||||||
],
|
|
||||||
"version": "==2.6"
|
|
||||||
},
|
|
||||||
"itsdangerous": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:cbb3fcf8d3e33df861709ecaf89d9e6629cff0a217bc2848f1b41cd30d360519"
|
|
||||||
],
|
|
||||||
"version": "==0.24"
|
|
||||||
},
|
|
||||||
"jinja2": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:2231bace0dfd8d2bf1e5d7e41239c06c9e0ded46e70cc1094a0aa64b0afeb054",
|
|
||||||
"sha256:ddaa01a212cd6d641401cb01b605f4a4d9f37bfc93043d7f760ec70fb99ff9ff"
|
|
||||||
],
|
|
||||||
"version": "==2.9.6"
|
|
||||||
},
|
|
||||||
"markupsafe": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665"
|
|
||||||
],
|
|
||||||
"version": "==1.0"
|
|
||||||
},
|
|
||||||
"requests": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b",
|
|
||||||
"sha256:9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e"
|
|
||||||
],
|
|
||||||
"version": "==2.18.4"
|
|
||||||
},
|
|
||||||
"urllib3": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b",
|
|
||||||
"sha256:cc44da8e1145637334317feebd728bd869a35285b93cbb4cca2577da7e62db4f"
|
|
||||||
],
|
|
||||||
"version": "==1.22"
|
|
||||||
},
|
|
||||||
"werkzeug": {
|
|
||||||
"hashes": [
|
|
||||||
"sha256:e8549c143af3ce6559699a01e26fa4174f4c591dbee0a499f3cd4c3781cdec3d",
|
|
||||||
"sha256:903a7b87b74635244548b30d30db4c8947fe64c5198f58899ddcd3a13c23bb26"
|
|
||||||
],
|
|
||||||
"version": "==0.12.2"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"develop": {}
|
|
||||||
}
|
|
301
blockchain.py
301
blockchain.py
|
@ -1,301 +0,0 @@
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
from time import time
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
import requests
|
|
||||||
from flask import Flask, jsonify, request
|
|
||||||
|
|
||||||
|
|
||||||
class Blockchain:
|
|
||||||
def __init__(self):
|
|
||||||
self.current_transactions = []
|
|
||||||
self.chain = []
|
|
||||||
self.nodes = set()
|
|
||||||
|
|
||||||
# Create the genesis block
|
|
||||||
self.new_block(previous_hash='1', proof=100)
|
|
||||||
|
|
||||||
def register_node(self, address):
|
|
||||||
"""
|
|
||||||
Add a new node to the list of nodes
|
|
||||||
|
|
||||||
:param address: Address of node. Eg. 'http://192.168.0.5:5000'
|
|
||||||
"""
|
|
||||||
|
|
||||||
parsed_url = urlparse(address)
|
|
||||||
if parsed_url.netloc:
|
|
||||||
self.nodes.add(parsed_url.netloc)
|
|
||||||
elif parsed_url.path:
|
|
||||||
# Accepts an URL without scheme like '192.168.0.5:5000'.
|
|
||||||
self.nodes.add(parsed_url.path)
|
|
||||||
else:
|
|
||||||
raise ValueError('Invalid URL')
|
|
||||||
|
|
||||||
|
|
||||||
def valid_chain(self, chain):
|
|
||||||
"""
|
|
||||||
Determine if a given blockchain is valid
|
|
||||||
|
|
||||||
:param chain: A blockchain
|
|
||||||
:return: True if valid, False if not
|
|
||||||
"""
|
|
||||||
|
|
||||||
last_block = chain[0]
|
|
||||||
current_index = 1
|
|
||||||
|
|
||||||
while current_index < len(chain):
|
|
||||||
block = chain[current_index]
|
|
||||||
print(f'{last_block}')
|
|
||||||
print(f'{block}')
|
|
||||||
print("\n-----------\n")
|
|
||||||
# Check that the hash of the block is correct
|
|
||||||
last_block_hash = self.hash(last_block)
|
|
||||||
if block['previous_hash'] != last_block_hash:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Check that the Proof of Work is correct
|
|
||||||
if not self.valid_proof(last_block['proof'], block['proof'], last_block_hash):
|
|
||||||
return False
|
|
||||||
|
|
||||||
last_block = block
|
|
||||||
current_index += 1
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def resolve_conflicts(self):
|
|
||||||
"""
|
|
||||||
This is our consensus algorithm, it resolves conflicts
|
|
||||||
by replacing our chain with the longest one in the network.
|
|
||||||
|
|
||||||
:return: True if our chain was replaced, False if not
|
|
||||||
"""
|
|
||||||
|
|
||||||
neighbours = self.nodes
|
|
||||||
new_chain = None
|
|
||||||
|
|
||||||
# We're only looking for chains longer than ours
|
|
||||||
max_length = len(self.chain)
|
|
||||||
|
|
||||||
# Grab and verify the chains from all the nodes in our network
|
|
||||||
for node in neighbours:
|
|
||||||
response = requests.get(f'http://{node}/chain')
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
length = response.json()['length']
|
|
||||||
chain = response.json()['chain']
|
|
||||||
|
|
||||||
# Check if the length is longer and the chain is valid
|
|
||||||
if length > max_length and self.valid_chain(chain):
|
|
||||||
max_length = length
|
|
||||||
new_chain = chain
|
|
||||||
|
|
||||||
# Replace our chain if we discovered a new, valid chain longer than ours
|
|
||||||
if new_chain:
|
|
||||||
self.chain = new_chain
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def new_block(self, proof, previous_hash):
|
|
||||||
"""
|
|
||||||
Create a new Block in the Blockchain
|
|
||||||
|
|
||||||
:param proof: The proof given by the Proof of Work algorithm
|
|
||||||
:param previous_hash: Hash of previous Block
|
|
||||||
:return: New Block
|
|
||||||
"""
|
|
||||||
|
|
||||||
block = {
|
|
||||||
'index': len(self.chain) + 1,
|
|
||||||
'timestamp': time(),
|
|
||||||
'transactions': self.current_transactions,
|
|
||||||
'proof': proof,
|
|
||||||
'previous_hash': previous_hash or self.hash(self.chain[-1]),
|
|
||||||
}
|
|
||||||
|
|
||||||
# Reset the current list of transactions
|
|
||||||
self.current_transactions = []
|
|
||||||
|
|
||||||
self.chain.append(block)
|
|
||||||
return block
|
|
||||||
|
|
||||||
def new_transaction(self, sender, recipient, amount):
|
|
||||||
"""
|
|
||||||
Creates a new transaction to go into the next mined Block
|
|
||||||
|
|
||||||
:param sender: Address of the Sender
|
|
||||||
:param recipient: Address of the Recipient
|
|
||||||
:param amount: Amount
|
|
||||||
:return: The index of the Block that will hold this transaction
|
|
||||||
"""
|
|
||||||
self.current_transactions.append({
|
|
||||||
'sender': sender,
|
|
||||||
'recipient': recipient,
|
|
||||||
'amount': amount,
|
|
||||||
})
|
|
||||||
|
|
||||||
return self.last_block['index'] + 1
|
|
||||||
|
|
||||||
@property
|
|
||||||
def last_block(self):
|
|
||||||
return self.chain[-1]
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def hash(block):
|
|
||||||
"""
|
|
||||||
Creates a SHA-256 hash of a Block
|
|
||||||
|
|
||||||
:param block: Block
|
|
||||||
"""
|
|
||||||
|
|
||||||
# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
|
|
||||||
block_string = json.dumps(block, sort_keys=True).encode()
|
|
||||||
return hashlib.sha256(block_string).hexdigest()
|
|
||||||
|
|
||||||
def proof_of_work(self, last_block):
|
|
||||||
"""
|
|
||||||
Simple Proof of Work Algorithm:
|
|
||||||
|
|
||||||
- Find a number p' such that hash(pp') contains leading 4 zeroes
|
|
||||||
- Where p is the previous proof, and p' is the new proof
|
|
||||||
|
|
||||||
:param last_block: <dict> last Block
|
|
||||||
:return: <int>
|
|
||||||
"""
|
|
||||||
|
|
||||||
last_proof = last_block['proof']
|
|
||||||
last_hash = self.hash(last_block)
|
|
||||||
|
|
||||||
proof = 0
|
|
||||||
while self.valid_proof(last_proof, proof, last_hash) is False:
|
|
||||||
proof += 1
|
|
||||||
|
|
||||||
return proof
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def valid_proof(last_proof, proof, last_hash):
|
|
||||||
"""
|
|
||||||
Validates the Proof
|
|
||||||
|
|
||||||
:param last_proof: <int> Previous Proof
|
|
||||||
:param proof: <int> Current Proof
|
|
||||||
:param last_hash: <str> The hash of the Previous Block
|
|
||||||
:return: <bool> True if correct, False if not.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
guess = f'{last_proof}{proof}{last_hash}'.encode()
|
|
||||||
guess_hash = hashlib.sha256(guess).hexdigest()
|
|
||||||
return guess_hash[:4] == "0000"
|
|
||||||
|
|
||||||
|
|
||||||
# Instantiate the 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():
|
|
||||||
# We run the proof of work algorithm to get the next proof...
|
|
||||||
last_block = blockchain.last_block
|
|
||||||
proof = blockchain.proof_of_work(last_block)
|
|
||||||
|
|
||||||
# We must receive a reward for finding the proof.
|
|
||||||
# The sender is "0" to signify that this node has mined a new coin.
|
|
||||||
blockchain.new_transaction(
|
|
||||||
sender="0",
|
|
||||||
recipient=node_identifier,
|
|
||||||
amount=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Forge the new Block by adding it to the chain
|
|
||||||
previous_hash = blockchain.hash(last_block)
|
|
||||||
block = blockchain.new_block(proof, previous_hash)
|
|
||||||
|
|
||||||
response = {
|
|
||||||
'message': "New Block Forged",
|
|
||||||
'index': block['index'],
|
|
||||||
'transactions': block['transactions'],
|
|
||||||
'proof': block['proof'],
|
|
||||||
'previous_hash': block['previous_hash'],
|
|
||||||
}
|
|
||||||
return jsonify(response), 200
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/transactions/new', methods=['POST'])
|
|
||||||
def new_transaction():
|
|
||||||
values = request.get_json()
|
|
||||||
|
|
||||||
# Check that the required fields are in the POST'ed data
|
|
||||||
required = ['sender', 'recipient', 'amount']
|
|
||||||
if not all(k in values for k in required):
|
|
||||||
return 'Missing values', 400
|
|
||||||
|
|
||||||
# Create a new Transaction
|
|
||||||
index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
|
|
||||||
|
|
||||||
response = {'message': f'Transaction will be added to Block {index}'}
|
|
||||||
return jsonify(response), 201
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/chain', methods=['GET'])
|
|
||||||
def full_chain():
|
|
||||||
response = {
|
|
||||||
'chain': blockchain.chain,
|
|
||||||
'length': len(blockchain.chain),
|
|
||||||
}
|
|
||||||
return jsonify(response), 200
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/nodes/register', methods=['POST'])
|
|
||||||
def register_nodes():
|
|
||||||
values = request.get_json()
|
|
||||||
|
|
||||||
nodes = values.get('nodes')
|
|
||||||
if nodes is None:
|
|
||||||
return "Error: Please supply a valid list of nodes", 400
|
|
||||||
|
|
||||||
for node in nodes:
|
|
||||||
blockchain.register_node(node)
|
|
||||||
|
|
||||||
response = {
|
|
||||||
'message': 'New nodes have been added',
|
|
||||||
'total_nodes': list(blockchain.nodes),
|
|
||||||
}
|
|
||||||
return jsonify(response), 201
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/nodes/resolve', methods=['GET'])
|
|
||||||
def consensus():
|
|
||||||
replaced = blockchain.resolve_conflicts()
|
|
||||||
|
|
||||||
if replaced:
|
|
||||||
response = {
|
|
||||||
'message': 'Our chain was replaced',
|
|
||||||
'new_chain': blockchain.chain
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
response = {
|
|
||||||
'message': 'Our chain is authoritative',
|
|
||||||
'chain': blockchain.chain
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonify(response), 200
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
from argparse import ArgumentParser
|
|
||||||
|
|
||||||
parser = ArgumentParser()
|
|
||||||
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
|
|
||||||
args = parser.parse_args()
|
|
||||||
port = args.port
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=port)
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim:fenc=utf-8
|
||||||
|
|
||||||
|
from flask import Flask
|
||||||
|
|
||||||
|
|
||||||
|
def init_blueprints(app):
|
||||||
|
from .app import app as app_bp
|
||||||
|
app.register_blueprint(app_bp, url_prefix='/')
|
||||||
|
|
||||||
|
|
||||||
|
def create_app(package_name=__name__):
|
||||||
|
app = Flask(package_name)
|
||||||
|
|
||||||
|
init_blueprints(app)
|
||||||
|
|
||||||
|
return app
|
|
@ -0,0 +1,127 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim:fenc=utf-8
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from flask import Blueprint, request
|
||||||
|
|
||||||
|
from .blockchain import Blockchain
|
||||||
|
from .utils import jsonify
|
||||||
|
|
||||||
|
|
||||||
|
app = Blueprint('app', __name__)
|
||||||
|
identifier = str(uuid4()).replace('-', '')
|
||||||
|
blockchain = Blockchain()
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/mine', methods=['POST'])
|
||||||
|
def mine():
|
||||||
|
# We run the proof of work algorithm to get the next proof...
|
||||||
|
last_block = blockchain.last_block
|
||||||
|
proof = blockchain.proof_of_work(last_block)
|
||||||
|
|
||||||
|
# We must receive a reward for finding the proof.
|
||||||
|
# The sender is '0' to signify that this node has mined a new coin.
|
||||||
|
blockchain.new_transaction(
|
||||||
|
sender='0',
|
||||||
|
recipient=identifier,
|
||||||
|
amount=Decimal(1),
|
||||||
|
)
|
||||||
|
|
||||||
|
previous_hash = blockchain.hash(last_block)
|
||||||
|
block = blockchain.new_block(proof, previous_hash)
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'msg': 'New Block Forged',
|
||||||
|
'index': block['index'],
|
||||||
|
'transactions': block['transactions'],
|
||||||
|
'proof': block['proof'],
|
||||||
|
'previous_hash': block['previous_hash'],
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/transactions', methods=['POST'])
|
||||||
|
def create_transaction():
|
||||||
|
sender = request.json.get('sender')
|
||||||
|
recipient = request.json.get('recipient')
|
||||||
|
amount = request.json.get('amount')
|
||||||
|
if None in (sender, recipient, amount):
|
||||||
|
return {'msg': 'Missing parameter'}, 400
|
||||||
|
|
||||||
|
index = blockchain.new_transaction(
|
||||||
|
sender,
|
||||||
|
recipient,
|
||||||
|
Decimal(amount),
|
||||||
|
)
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'msg': f'Transaction will be added to Block {index}'
|
||||||
|
}), 201
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/chain', methods=['GET'])
|
||||||
|
def full_chain():
|
||||||
|
return jsonify({
|
||||||
|
'chain': blockchain.chain,
|
||||||
|
'length': len(blockchain.chain),
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/identifier', methods=['GET'])
|
||||||
|
def get_identifier():
|
||||||
|
return jsonify({
|
||||||
|
'identifier': identifier
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/nodes', methods=['GET'])
|
||||||
|
def get_nodes():
|
||||||
|
return jsonify({
|
||||||
|
'nodes': list(blockchain.nodes),
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/nodes/<identifier>/balance', methods=['GET'])
|
||||||
|
def get_balance(identifier):
|
||||||
|
balance = blockchain.get_balance(identifier)
|
||||||
|
if balance is None:
|
||||||
|
return jsonify({
|
||||||
|
'msg': 'Chain is not valid',
|
||||||
|
}), 500
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'identifier': identifier,
|
||||||
|
'balance': balance,
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/nodes/register', methods=['POST'])
|
||||||
|
def register_nodes():
|
||||||
|
nodes = request.json.get('nodes')
|
||||||
|
if None in (nodes,):
|
||||||
|
return {'msg': 'No valid list of nodes'}, 400
|
||||||
|
|
||||||
|
for node in nodes:
|
||||||
|
blockchain.register_node(node)
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'msg': 'New nodes have been added',
|
||||||
|
'total_nodes': list(blockchain.nodes),
|
||||||
|
}), 201
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/nodes/resolve', methods=['POST'])
|
||||||
|
def consensus():
|
||||||
|
replaced = blockchain.resolve_conflicts()
|
||||||
|
|
||||||
|
if replaced:
|
||||||
|
message = 'Our chain was replaced'
|
||||||
|
else:
|
||||||
|
message = 'Our chain is authoritative'
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'msg': message,
|
||||||
|
'chain': blockchain.chain,
|
||||||
|
}), 200
|
|
@ -0,0 +1,190 @@
|
||||||
|
import hashlib
|
||||||
|
from decimal import Decimal
|
||||||
|
from time import time
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from .utils import dumps, do_process_pow, valid_proof
|
||||||
|
|
||||||
|
DIFFICULTY = 24
|
||||||
|
|
||||||
|
|
||||||
|
class Blockchain:
|
||||||
|
def __init__(self):
|
||||||
|
self.current_transactions = []
|
||||||
|
self.chain = []
|
||||||
|
self.nodes = set()
|
||||||
|
|
||||||
|
# Create the genesis block
|
||||||
|
self.new_block(previous_hash='1', proof=100)
|
||||||
|
|
||||||
|
def register_node(self, address):
|
||||||
|
"""
|
||||||
|
Add a new node to the list of nodes
|
||||||
|
|
||||||
|
:param address: Address of the node
|
||||||
|
"""
|
||||||
|
|
||||||
|
parsed_url = urlparse(address)
|
||||||
|
if parsed_url.netloc:
|
||||||
|
self.nodes.add(parsed_url.netloc)
|
||||||
|
elif parsed_url.path:
|
||||||
|
# Accepts an URL without scheme like '192.168.0.5:5000'.
|
||||||
|
self.nodes.add(parsed_url.path)
|
||||||
|
else:
|
||||||
|
raise ValueError('Invalid URL')
|
||||||
|
|
||||||
|
def valid_chain(self, chain):
|
||||||
|
"""
|
||||||
|
Determine if a given blockchain is valid
|
||||||
|
|
||||||
|
:param chain: A blockchain
|
||||||
|
:return: True if valid, False if not
|
||||||
|
"""
|
||||||
|
|
||||||
|
last_block = chain[0]
|
||||||
|
current_index = 1
|
||||||
|
|
||||||
|
while current_index < len(chain):
|
||||||
|
block = chain[current_index]
|
||||||
|
# print(f'{last_block}')
|
||||||
|
# print(f'{block}')
|
||||||
|
# print("\n-----------\n")
|
||||||
|
# Check that the hash of the block is correct
|
||||||
|
last_block_hash = self.hash(last_block)
|
||||||
|
if block['previous_hash'] != last_block_hash:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Check that the Proof of Work is correct
|
||||||
|
if not valid_proof(last_block['proof'], block['proof'], last_block_hash):
|
||||||
|
return False
|
||||||
|
|
||||||
|
last_block = block
|
||||||
|
current_index += 1
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def resolve_conflicts(self):
|
||||||
|
"""
|
||||||
|
This is our consensus algorithm, it resolves conflicts
|
||||||
|
by replacing our chain with the longest one in the network.
|
||||||
|
|
||||||
|
:return: True if our chain was replaced, False if not
|
||||||
|
"""
|
||||||
|
|
||||||
|
neighbours = self.nodes
|
||||||
|
new_chain = None
|
||||||
|
|
||||||
|
# We're only looking for chains longer than ours
|
||||||
|
max_length = len(self.chain)
|
||||||
|
|
||||||
|
# Grab and verify the chains from all the nodes in our network
|
||||||
|
for node in neighbours:
|
||||||
|
response = requests.get(f'http://{node}/chain')
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
length = response.json()['length']
|
||||||
|
chain = response.json()['chain']
|
||||||
|
|
||||||
|
# Check if the length is longer and the chain is valid
|
||||||
|
if length > max_length and self.valid_chain(chain):
|
||||||
|
max_length = length
|
||||||
|
new_chain = chain
|
||||||
|
|
||||||
|
# Replace our chain if we discovered a new, valid chain longer than ours
|
||||||
|
if new_chain:
|
||||||
|
self.chain = new_chain
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def new_block(self, proof, previous_hash):
|
||||||
|
"""
|
||||||
|
Create a new Block in the Blockchain
|
||||||
|
|
||||||
|
:param proof: The proof given by the Proof of Work algorithm
|
||||||
|
:param previous_hash: Hash of previous Block
|
||||||
|
:return: New Block
|
||||||
|
"""
|
||||||
|
|
||||||
|
block = {
|
||||||
|
'index': len(self.chain) + 1,
|
||||||
|
'timestamp': time(),
|
||||||
|
'transactions': self.current_transactions,
|
||||||
|
'proof': proof,
|
||||||
|
'previous_hash': previous_hash or self.hash(self.chain[-1]),
|
||||||
|
}
|
||||||
|
|
||||||
|
self.current_transactions = []
|
||||||
|
self.chain.append(block)
|
||||||
|
|
||||||
|
return block
|
||||||
|
|
||||||
|
def new_transaction(self, sender, recipient, amount):
|
||||||
|
"""
|
||||||
|
Creates a new transaction to go into the next mined Block
|
||||||
|
|
||||||
|
:param sender: Address of the Sender
|
||||||
|
:param recipient: Address of the Recipient
|
||||||
|
:param amount: Amount
|
||||||
|
:return: The index of the Block that will hold this transaction
|
||||||
|
"""
|
||||||
|
self.current_transactions.append({
|
||||||
|
'sender': sender,
|
||||||
|
'recipient': recipient,
|
||||||
|
'amount': amount,
|
||||||
|
})
|
||||||
|
|
||||||
|
return self.last_block['index'] + 1
|
||||||
|
|
||||||
|
@property
|
||||||
|
def last_block(self):
|
||||||
|
return self.chain[-1]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def hash(block):
|
||||||
|
"""
|
||||||
|
Creates a SHA-256 hash of a Block
|
||||||
|
|
||||||
|
:param block: Block
|
||||||
|
"""
|
||||||
|
block_string = dumps(block, separators=(",", ":"), sort_keys=True).encode()
|
||||||
|
|
||||||
|
return hashlib.sha256(block_string).hexdigest()
|
||||||
|
|
||||||
|
def proof_of_work(self, last_block):
|
||||||
|
"""
|
||||||
|
Simple Proof of Work Algorithm:
|
||||||
|
|
||||||
|
- Find a number p' such that hash(pp') contains leading DIFFICULTY zeroes
|
||||||
|
- Where p is the previous proof, and p' is the new proof
|
||||||
|
|
||||||
|
:param last_block: <dict> last Block
|
||||||
|
:return: <int>
|
||||||
|
"""
|
||||||
|
|
||||||
|
last_proof = last_block['proof']
|
||||||
|
last_hash = self.hash(last_block)
|
||||||
|
|
||||||
|
return do_process_pow(last_proof, last_hash, DIFFICULTY)
|
||||||
|
|
||||||
|
def get_balance(self, identifier):
|
||||||
|
"""
|
||||||
|
Returns the balance of a node, by iterating through all transactions
|
||||||
|
|
||||||
|
:param identifier: <str> node identifier
|
||||||
|
:return: <int> balance of the node
|
||||||
|
"""
|
||||||
|
if not self.valid_chain(self.chain):
|
||||||
|
return None
|
||||||
|
|
||||||
|
balance = Decimal(0)
|
||||||
|
for block in self.chain:
|
||||||
|
for transaction in block['transactions']:
|
||||||
|
if transaction['sender'] == identifier:
|
||||||
|
balance -= transaction['amount']
|
||||||
|
if transaction['recipient'] == identifier:
|
||||||
|
balance += transaction['amount']
|
||||||
|
|
||||||
|
return balance
|
|
@ -0,0 +1,126 @@
|
||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim:fenc=utf-8
|
||||||
|
#
|
||||||
|
# Copyright © 2020 <pavle.portic@tilda.center>
|
||||||
|
#
|
||||||
|
# Distributed under terms of the BSD 3-Clause license.
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import itertools
|
||||||
|
from multiprocessing import (
|
||||||
|
cpu_count,
|
||||||
|
Pool,
|
||||||
|
Process,
|
||||||
|
Queue
|
||||||
|
)
|
||||||
|
|
||||||
|
from flask import current_app, json
|
||||||
|
|
||||||
|
|
||||||
|
def dumps(*args, **kwargs):
|
||||||
|
if len(args) == 1:
|
||||||
|
data = args[0]
|
||||||
|
else:
|
||||||
|
data = args
|
||||||
|
|
||||||
|
return json.dumps(
|
||||||
|
data,
|
||||||
|
use_decimal=True,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def jsonify(*args, **kwargs):
|
||||||
|
indent = None
|
||||||
|
separators = (",", ":")
|
||||||
|
|
||||||
|
if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
|
||||||
|
indent = 2
|
||||||
|
separators = (", ", ": ")
|
||||||
|
|
||||||
|
return current_app.response_class(
|
||||||
|
dumps(
|
||||||
|
*args,
|
||||||
|
indent=indent,
|
||||||
|
separators=separators,
|
||||||
|
) + "\n",
|
||||||
|
mimetype=current_app.config["JSONIFY_MIMETYPE"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def do_pooled_pow(last_proof, last_hash, difficulty):
|
||||||
|
queue = Queue()
|
||||||
|
with Pool() as p:
|
||||||
|
result = p.starmap_async(pool_worker, ((
|
||||||
|
queue,
|
||||||
|
i,
|
||||||
|
last_proof,
|
||||||
|
last_hash,
|
||||||
|
difficulty,
|
||||||
|
) for i in itertools.count()), chunksize=100)
|
||||||
|
|
||||||
|
proof = queue.get()
|
||||||
|
result.wait()
|
||||||
|
p.terminate()
|
||||||
|
|
||||||
|
return proof
|
||||||
|
|
||||||
|
|
||||||
|
def pool_worker(queue, proof, last_proof, last_hash, difficulty):
|
||||||
|
if valid_proof(last_proof, proof, last_hash):
|
||||||
|
queue.put(proof)
|
||||||
|
return proof
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def do_process_pow(last_proof, last_hash, difficulty):
|
||||||
|
queue = Queue()
|
||||||
|
processes = [
|
||||||
|
Process(
|
||||||
|
target=process_worker,
|
||||||
|
args=(
|
||||||
|
queue,
|
||||||
|
last_proof,
|
||||||
|
last_hash,
|
||||||
|
difficulty,
|
||||||
|
step,
|
||||||
|
)
|
||||||
|
) for step in range(cpu_count())
|
||||||
|
]
|
||||||
|
for p in processes:
|
||||||
|
p.start()
|
||||||
|
|
||||||
|
proof = queue.get()
|
||||||
|
for p in processes:
|
||||||
|
p.terminate()
|
||||||
|
|
||||||
|
return proof
|
||||||
|
|
||||||
|
|
||||||
|
def process_worker(queue, last_proof, last_hash, difficulty, step):
|
||||||
|
proof = step
|
||||||
|
while not valid_proof(last_proof, proof, last_hash, difficulty):
|
||||||
|
proof += step
|
||||||
|
|
||||||
|
queue.put(proof)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def valid_proof(last_proof, proof, last_hash, difficulty):
|
||||||
|
"""
|
||||||
|
Validates the Proof
|
||||||
|
|
||||||
|
:param last_proof: <int> Previous Proof
|
||||||
|
:param proof: <int> Current Proof
|
||||||
|
:param last_hash: <str> The hash of the Previous Block
|
||||||
|
:return: <bool> True if correct, False if not.
|
||||||
|
"""
|
||||||
|
|
||||||
|
guess = f'{last_proof}{proof}{last_hash}'.encode()
|
||||||
|
guess_hash = hashlib.sha256(guess)
|
||||||
|
binary_hash = ''.join(format(n, '08b') for n in guess_hash.digest())
|
||||||
|
|
||||||
|
return binary_hash[:difficulty] == '0' * difficulty
|
|
@ -1,10 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<configuration>
|
|
||||||
<startup>
|
|
||||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
|
|
||||||
</startup>
|
|
||||||
<appSettings>
|
|
||||||
<add key="host" value="localhost" />
|
|
||||||
<add key="port" value="12345" />
|
|
||||||
</appSettings>
|
|
||||||
</configuration>
|
|
|
@ -1,63 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
|
||||||
<PropertyGroup>
|
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
|
||||||
<ProjectGuid>{D0C795A0-6F20-4A8E-BE44-801678754DA4}</ProjectGuid>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
<RootNamespace>BlockChainDemo.Console</RootNamespace>
|
|
||||||
<AssemblyName>BlockChainDemo.Console</AssemblyName>
|
|
||||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
|
|
||||||
<FileAlignment>512</FileAlignment>
|
|
||||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
|
||||||
<SccProjectName>SAK</SccProjectName>
|
|
||||||
<SccLocalPath>SAK</SccLocalPath>
|
|
||||||
<SccAuxPath>SAK</SccAuxPath>
|
|
||||||
<SccProvider>SAK</SccProvider>
|
|
||||||
<TargetFrameworkProfile />
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
|
||||||
<DebugSymbols>true</DebugSymbols>
|
|
||||||
<DebugType>full</DebugType>
|
|
||||||
<Optimize>false</Optimize>
|
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
|
||||||
<ErrorReport>prompt</ErrorReport>
|
|
||||||
<WarningLevel>4</WarningLevel>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
|
||||||
<DebugType>pdbonly</DebugType>
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
|
||||||
<ErrorReport>prompt</ErrorReport>
|
|
||||||
<WarningLevel>4</WarningLevel>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Reference Include="System" />
|
|
||||||
<Reference Include="System.Core" />
|
|
||||||
<Reference Include="System.Xml.Linq" />
|
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
|
||||||
<Reference Include="Microsoft.CSharp" />
|
|
||||||
<Reference Include="System.Data" />
|
|
||||||
<Reference Include="System.Net.Http" />
|
|
||||||
<Reference Include="System.Xml" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Include="Program.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="App.config" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\BlockChain\BlockChain.csproj">
|
|
||||||
<Project>{e06fc4ce-77d0-4a64-94a6-32a08920e481}</Project>
|
|
||||||
<Name>BlockChain</Name>
|
|
||||||
</ProjectReference>
|
|
||||||
</ItemGroup>
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
|
||||||
</Project>
|
|
|
@ -1,12 +0,0 @@
|
||||||
namespace BlockChainDemo.Console
|
|
||||||
{
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
static void Main(string[] args)
|
|
||||||
{
|
|
||||||
var chain = new BlockChain();
|
|
||||||
var server = new WebServer(chain);
|
|
||||||
System.Console.Read();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
// General Information about an assembly is controlled through the following
|
|
||||||
// set of attributes. Change these attribute values to modify the information
|
|
||||||
// associated with an assembly.
|
|
||||||
[assembly: AssemblyTitle("BlockChain.Console")]
|
|
||||||
[assembly: AssemblyDescription("")]
|
|
||||||
[assembly: AssemblyConfiguration("")]
|
|
||||||
[assembly: AssemblyCompany("")]
|
|
||||||
[assembly: AssemblyProduct("BlockChain.Console")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
|
||||||
[assembly: AssemblyTrademark("")]
|
|
||||||
[assembly: AssemblyCulture("")]
|
|
||||||
|
|
||||||
// Setting ComVisible to false makes the types in this assembly not visible
|
|
||||||
// to COM components. If you need to access a type in this assembly from
|
|
||||||
// COM, set the ComVisible attribute to true on that type.
|
|
||||||
[assembly: ComVisible(false)]
|
|
||||||
|
|
||||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
|
||||||
[assembly: Guid("d0c795a0-6f20-4a8e-be44-801678754da4")]
|
|
||||||
|
|
||||||
// Version information for an assembly consists of the following four values:
|
|
||||||
//
|
|
||||||
// Major Version
|
|
||||||
// Minor Version
|
|
||||||
// Build Number
|
|
||||||
// Revision
|
|
||||||
//
|
|
||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
|
||||||
// by using the '*' as shown below:
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
|
@ -1,43 +0,0 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
|
||||||
# Visual Studio 15
|
|
||||||
VisualStudioVersion = 15.0.27004.2008
|
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain", "BlockChain\BlockChain.csproj", "{E06FC4CE-77D0-4A64-94A6-32A08920E481}"
|
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain.Console", "BlockChain.Console\BlockChain.Console.csproj", "{D0C795A0-6F20-4A8E-BE44-801678754DA4}"
|
|
||||||
EndProject
|
|
||||||
Global
|
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
|
||||||
Debug|Any CPU = Debug|Any CPU
|
|
||||||
Release|Any CPU = Release|Any CPU
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
|
||||||
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
|
||||||
HideSolutionNode = FALSE
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
|
||||||
SolutionGuid = {7FB3650B-CAFB-4A71-940B-0CA6F0377422}
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(TeamFoundationVersionControl) = preSolution
|
|
||||||
SccNumberOfProjects = 3
|
|
||||||
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
|
|
||||||
SccTeamFoundationServer = https://devfire.visualstudio.com/
|
|
||||||
SccLocalPath0 = .
|
|
||||||
SccProjectUniqueName1 = BlockChain\\BlockChain.csproj
|
|
||||||
SccProjectName1 = BlockChain
|
|
||||||
SccLocalPath1 = BlockChain
|
|
||||||
SccProjectUniqueName2 = BlockChain.Console\\BlockChain.Console.csproj
|
|
||||||
SccProjectName2 = BlockChain.Console
|
|
||||||
SccLocalPath2 = BlockChain.Console
|
|
||||||
EndGlobalSection
|
|
||||||
EndGlobal
|
|
|
@ -1,19 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace BlockChainDemo
|
|
||||||
{
|
|
||||||
public class Block
|
|
||||||
{
|
|
||||||
public int Index { get; set; }
|
|
||||||
public DateTime Timestamp { get; set; }
|
|
||||||
public List<Transaction> Transactions { get; set; }
|
|
||||||
public int Proof { get; set; }
|
|
||||||
public string PreviousHash { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{Index} [{Timestamp.ToString("yyyy-MM-dd HH:mm:ss")}] Proof: {Proof} | PrevHash: {PreviousHash} | Trx: {Transactions.Count}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,226 +0,0 @@
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace BlockChainDemo
|
|
||||||
{
|
|
||||||
public class BlockChain
|
|
||||||
{
|
|
||||||
private List<Transaction> _currentTransactions = new List<Transaction>();
|
|
||||||
private List<Block> _chain = new List<Block>();
|
|
||||||
private List<Node> _nodes = new List<Node>();
|
|
||||||
private Block _lastBlock => _chain.Last();
|
|
||||||
|
|
||||||
public string NodeId { get; private set; }
|
|
||||||
|
|
||||||
//ctor
|
|
||||||
public BlockChain()
|
|
||||||
{
|
|
||||||
NodeId = Guid.NewGuid().ToString().Replace("-", "");
|
|
||||||
CreateNewBlock(proof: 100, previousHash: "1"); //genesis block
|
|
||||||
}
|
|
||||||
|
|
||||||
//private functionality
|
|
||||||
private void RegisterNode(string address)
|
|
||||||
{
|
|
||||||
_nodes.Add(new Node { Address = new Uri(address) });
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsValidChain(List<Block> chain)
|
|
||||||
{
|
|
||||||
Block block = null;
|
|
||||||
Block lastBlock = chain.First();
|
|
||||||
int currentIndex = 1;
|
|
||||||
while (currentIndex < chain.Count)
|
|
||||||
{
|
|
||||||
block = chain.ElementAt(currentIndex);
|
|
||||||
Debug.WriteLine($"{lastBlock}");
|
|
||||||
Debug.WriteLine($"{block}");
|
|
||||||
Debug.WriteLine("----------------------------");
|
|
||||||
|
|
||||||
//Check that the hash of the block is correct
|
|
||||||
if (block.PreviousHash != GetHash(lastBlock))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
//Check that the Proof of Work is correct
|
|
||||||
if (!IsValidProof(lastBlock.Proof, block.Proof, lastBlock.PreviousHash))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
lastBlock = block;
|
|
||||||
currentIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool ResolveConflicts()
|
|
||||||
{
|
|
||||||
List<Block> newChain = null;
|
|
||||||
int maxLength = _chain.Count;
|
|
||||||
|
|
||||||
foreach (Node node in _nodes)
|
|
||||||
{
|
|
||||||
var url = new Uri(node.Address, "/chain");
|
|
||||||
var request = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
var response = (HttpWebResponse)request.GetResponse();
|
|
||||||
|
|
||||||
if (response.StatusCode == HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
var model = new
|
|
||||||
{
|
|
||||||
chain = new List<Block>(),
|
|
||||||
length = 0
|
|
||||||
};
|
|
||||||
string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|
||||||
var data = JsonConvert.DeserializeAnonymousType(json, model);
|
|
||||||
|
|
||||||
if (data.chain.Count > _chain.Count && IsValidChain(data.chain))
|
|
||||||
{
|
|
||||||
maxLength = data.chain.Count;
|
|
||||||
newChain = data.chain;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newChain != null)
|
|
||||||
{
|
|
||||||
_chain = newChain;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Block CreateNewBlock(int proof, string previousHash = null)
|
|
||||||
{
|
|
||||||
var block = new Block
|
|
||||||
{
|
|
||||||
Index = _chain.Count,
|
|
||||||
Timestamp = DateTime.UtcNow,
|
|
||||||
Transactions = _currentTransactions.ToList(),
|
|
||||||
Proof = proof,
|
|
||||||
PreviousHash = previousHash ?? GetHash(_chain.Last())
|
|
||||||
};
|
|
||||||
|
|
||||||
_currentTransactions.Clear();
|
|
||||||
_chain.Add(block);
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int CreateProofOfWork(int lastProof, string previousHash)
|
|
||||||
{
|
|
||||||
int proof = 0;
|
|
||||||
while (!IsValidProof(lastProof, proof, previousHash))
|
|
||||||
proof++;
|
|
||||||
|
|
||||||
return proof;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsValidProof(int lastProof, int proof, string previousHash)
|
|
||||||
{
|
|
||||||
string guess = $"{lastProof}{proof}{previousHash}";
|
|
||||||
string result = GetSha256(guess);
|
|
||||||
return result.StartsWith("0000");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetHash(Block block)
|
|
||||||
{
|
|
||||||
string blockText = JsonConvert.SerializeObject(block);
|
|
||||||
return GetSha256(blockText);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetSha256(string data)
|
|
||||||
{
|
|
||||||
var sha256 = new SHA256Managed();
|
|
||||||
var hashBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
byte[] bytes = Encoding.Unicode.GetBytes(data);
|
|
||||||
byte[] hash = sha256.ComputeHash(bytes);
|
|
||||||
|
|
||||||
foreach (byte x in hash)
|
|
||||||
hashBuilder.Append($"{x:x2}");
|
|
||||||
|
|
||||||
return hashBuilder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
//web server calls
|
|
||||||
internal string Mine()
|
|
||||||
{
|
|
||||||
int proof = CreateProofOfWork(_lastBlock.Proof, _lastBlock.PreviousHash);
|
|
||||||
|
|
||||||
CreateTransaction(sender: "0", recipient: NodeId, amount: 1);
|
|
||||||
Block block = CreateNewBlock(proof /*, _lastBlock.PreviousHash*/);
|
|
||||||
|
|
||||||
var response = new
|
|
||||||
{
|
|
||||||
Message = "New Block Forged",
|
|
||||||
Index = block.Index,
|
|
||||||
Transactions = block.Transactions.ToArray(),
|
|
||||||
Proof = block.Proof,
|
|
||||||
PreviousHash = block.PreviousHash
|
|
||||||
};
|
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal string GetFullChain()
|
|
||||||
{
|
|
||||||
var response = new
|
|
||||||
{
|
|
||||||
chain = _chain.ToArray(),
|
|
||||||
length = _chain.Count
|
|
||||||
};
|
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal string RegisterNodes(string[] nodes)
|
|
||||||
{
|
|
||||||
var builder = new StringBuilder();
|
|
||||||
foreach (string node in nodes)
|
|
||||||
{
|
|
||||||
string url = $"http://{node}";
|
|
||||||
RegisterNode(url);
|
|
||||||
builder.Append($"{url}, ");
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.Insert(0, $"{nodes.Count()} new nodes have been added: ");
|
|
||||||
string result = builder.ToString();
|
|
||||||
return result.Substring(0, result.Length - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal string Consensus()
|
|
||||||
{
|
|
||||||
bool replaced = ResolveConflicts();
|
|
||||||
string message = replaced ? "was replaced" : "is authoritive";
|
|
||||||
|
|
||||||
var response = new
|
|
||||||
{
|
|
||||||
Message = $"Our chain {message}",
|
|
||||||
Chain = _chain
|
|
||||||
};
|
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal int CreateTransaction(string sender, string recipient, int amount)
|
|
||||||
{
|
|
||||||
var transaction = new Transaction
|
|
||||||
{
|
|
||||||
Sender = sender,
|
|
||||||
Recipient = recipient,
|
|
||||||
Amount = amount
|
|
||||||
};
|
|
||||||
|
|
||||||
_currentTransactions.Add(transaction);
|
|
||||||
|
|
||||||
return _lastBlock != null ? _lastBlock.Index + 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,73 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
|
||||||
<PropertyGroup>
|
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
|
||||||
<ProjectGuid>{E06FC4CE-77D0-4A64-94A6-32A08920E481}</ProjectGuid>
|
|
||||||
<OutputType>Library</OutputType>
|
|
||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
|
||||||
<RootNamespace>BlockChainDemo</RootNamespace>
|
|
||||||
<AssemblyName>BlockChainDemo</AssemblyName>
|
|
||||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
|
|
||||||
<FileAlignment>512</FileAlignment>
|
|
||||||
<SccProjectName>SAK</SccProjectName>
|
|
||||||
<SccLocalPath>SAK</SccLocalPath>
|
|
||||||
<SccAuxPath>SAK</SccAuxPath>
|
|
||||||
<SccProvider>SAK</SccProvider>
|
|
||||||
<TargetFrameworkProfile />
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
|
||||||
<DebugSymbols>true</DebugSymbols>
|
|
||||||
<DebugType>full</DebugType>
|
|
||||||
<Optimize>false</Optimize>
|
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
|
||||||
<ErrorReport>prompt</ErrorReport>
|
|
||||||
<WarningLevel>4</WarningLevel>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
|
||||||
<DebugType>pdbonly</DebugType>
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
<OutputPath>bin\Release\</OutputPath>
|
|
||||||
<DefineConstants>TRACE</DefineConstants>
|
|
||||||
<ErrorReport>prompt</ErrorReport>
|
|
||||||
<WarningLevel>4</WarningLevel>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
|
||||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="System" />
|
|
||||||
<Reference Include="System.configuration" />
|
|
||||||
<Reference Include="System.Core" />
|
|
||||||
<Reference Include="System.Xml.Linq" />
|
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
|
||||||
<Reference Include="Microsoft.CSharp" />
|
|
||||||
<Reference Include="System.Data" />
|
|
||||||
<Reference Include="System.Net.Http" />
|
|
||||||
<Reference Include="System.Xml" />
|
|
||||||
<Reference Include="TinyWebServer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
|
||||||
<HintPath>..\packages\TinyWebServer.dll.1.0.1\lib\net40\TinyWebServer.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Include="Block.cs" />
|
|
||||||
<Compile Include="BlockChain.cs" />
|
|
||||||
<Compile Include="Node.cs" />
|
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
|
||||||
<Compile Include="Transaction.cs" />
|
|
||||||
<Compile Include="WebServer.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="packages.config" />
|
|
||||||
</ItemGroup>
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
|
||||||
<Target Name="BeforeBuild">
|
|
||||||
</Target>
|
|
||||||
<Target Name="AfterBuild">
|
|
||||||
</Target>
|
|
||||||
-->
|
|
||||||
</Project>
|
|
|
@ -1,9 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace BlockChainDemo
|
|
||||||
{
|
|
||||||
public class Node
|
|
||||||
{
|
|
||||||
public Uri Address { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
// General Information about an assembly is controlled through the following
|
|
||||||
// set of attributes. Change these attribute values to modify the information
|
|
||||||
// associated with an assembly.
|
|
||||||
[assembly: AssemblyTitle("BlockChain")]
|
|
||||||
[assembly: AssemblyDescription("")]
|
|
||||||
[assembly: AssemblyConfiguration("")]
|
|
||||||
[assembly: AssemblyCompany("")]
|
|
||||||
[assembly: AssemblyProduct("BlockChain")]
|
|
||||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
|
||||||
[assembly: AssemblyTrademark("")]
|
|
||||||
[assembly: AssemblyCulture("")]
|
|
||||||
|
|
||||||
// Setting ComVisible to false makes the types in this assembly not visible
|
|
||||||
// to COM components. If you need to access a type in this assembly from
|
|
||||||
// COM, set the ComVisible attribute to true on that type.
|
|
||||||
[assembly: ComVisible(false)]
|
|
||||||
|
|
||||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
|
||||||
[assembly: Guid("e06fc4ce-77d0-4a64-94a6-32a08920e481")]
|
|
||||||
|
|
||||||
// Version information for an assembly consists of the following four values:
|
|
||||||
//
|
|
||||||
// Major Version
|
|
||||||
// Minor Version
|
|
||||||
// Build Number
|
|
||||||
// Revision
|
|
||||||
//
|
|
||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
|
||||||
// by using the '*' as shown below:
|
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
|
@ -1,9 +0,0 @@
|
||||||
namespace BlockChainDemo
|
|
||||||
{
|
|
||||||
public class Transaction
|
|
||||||
{
|
|
||||||
public int Amount { get; set; }
|
|
||||||
public string Recipient { get; set; }
|
|
||||||
public string Sender { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,78 +0,0 @@
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.Configuration;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
|
|
||||||
namespace BlockChainDemo
|
|
||||||
{
|
|
||||||
public class WebServer
|
|
||||||
{
|
|
||||||
public WebServer(BlockChain chain)
|
|
||||||
{
|
|
||||||
var settings = ConfigurationManager.AppSettings;
|
|
||||||
string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
|
|
||||||
string port = settings["port"]?.Length > 1 ? settings["port"] : "12345";
|
|
||||||
|
|
||||||
var server = new TinyWebServer.WebServer(request =>
|
|
||||||
{
|
|
||||||
string path = request.Url.PathAndQuery.ToLower();
|
|
||||||
string query = "";
|
|
||||||
string json = "";
|
|
||||||
if (path.Contains("?"))
|
|
||||||
{
|
|
||||||
string[] parts = path.Split('?');
|
|
||||||
path = parts[0];
|
|
||||||
query = parts[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (path)
|
|
||||||
{
|
|
||||||
//GET: http://localhost:12345/mine
|
|
||||||
case "/mine":
|
|
||||||
return chain.Mine();
|
|
||||||
|
|
||||||
//POST: http://localhost:12345/transactions/new
|
|
||||||
//{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" }
|
|
||||||
case "/transactions/new":
|
|
||||||
if (request.HttpMethod != HttpMethod.Post.Method)
|
|
||||||
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
|
|
||||||
|
|
||||||
json = new StreamReader(request.InputStream).ReadToEnd();
|
|
||||||
Transaction trx = JsonConvert.DeserializeObject<Transaction>(json);
|
|
||||||
int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount);
|
|
||||||
return $"Your transaction will be included in block {blockId}";
|
|
||||||
|
|
||||||
//GET: http://localhost:12345/chain
|
|
||||||
case "/chain":
|
|
||||||
return chain.GetFullChain();
|
|
||||||
|
|
||||||
//POST: http://localhost:12345/nodes/register
|
|
||||||
//{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] }
|
|
||||||
case "/nodes/register":
|
|
||||||
if (request.HttpMethod != HttpMethod.Post.Method)
|
|
||||||
return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
|
|
||||||
|
|
||||||
json = new StreamReader(request.InputStream).ReadToEnd();
|
|
||||||
var urlList = new { Urls = new string[0] };
|
|
||||||
var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
|
|
||||||
return chain.RegisterNodes(obj.Urls);
|
|
||||||
|
|
||||||
//GET: http://localhost:12345/nodes/resolve
|
|
||||||
case "/nodes/resolve":
|
|
||||||
return chain.Consensus();
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
$"http://{host}:{port}/mine/",
|
|
||||||
$"http://{host}:{port}/transactions/new/",
|
|
||||||
$"http://{host}:{port}/chain/",
|
|
||||||
$"http://{host}:{port}/nodes/register/",
|
|
||||||
$"http://{host}:{port}/nodes/resolve/"
|
|
||||||
);
|
|
||||||
|
|
||||||
server.Run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<packages>
|
|
||||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net451" />
|
|
||||||
<package id="TinyWebServer.dll" version="1.0.1" targetFramework="net451" />
|
|
||||||
</packages>
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
node1:
|
||||||
|
image: blockchain
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- 5001:80
|
||||||
|
|
||||||
|
node2:
|
||||||
|
image: blockchain
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- 5002:80
|
|
@ -0,0 +1,34 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function register_nodes() {
|
||||||
|
http POST localhost:5001/nodes/register \
|
||||||
|
nodes:='["http://localhost:5002"]'
|
||||||
|
|
||||||
|
http POST localhost:5002/nodes/register \
|
||||||
|
nodes:='["http://localhost:5001"]'
|
||||||
|
}
|
||||||
|
|
||||||
|
function synchronise_nodes() {
|
||||||
|
http POST localhost:5001/nodes/resolve
|
||||||
|
http POST localhost:5002/nodes/resolve
|
||||||
|
}
|
||||||
|
|
||||||
|
NODE1_ID=$(http localhost:5001/identifier | jq -r ".identifier")
|
||||||
|
NODE2_ID=$(http localhost:5002/identifier | jq -r ".identifier")
|
||||||
|
|
||||||
|
register_nodes >/dev/null
|
||||||
|
|
||||||
|
# Mine a few initial blocks
|
||||||
|
http POST localhost:5001/mine
|
||||||
|
http POST localhost:5001/mine
|
||||||
|
synchronise_nodes >/dev/null
|
||||||
|
|
||||||
|
http POST localhost:5001/transactions sender=$NODE1_ID recipient=$NODE2_ID amount=1.3
|
||||||
|
http POST localhost:5001/mine
|
||||||
|
synchronise_nodes >/dev/null
|
||||||
|
|
||||||
|
http localhost:5001/nodes/$NODE1_ID/balance
|
||||||
|
http localhost:5001/nodes/$NODE2_ID/balance
|
||||||
|
|
||||||
|
echo node1: $NODE1_ID
|
||||||
|
echo node2: $NODE2_ID
|
102
js/blockchain.js
102
js/blockchain.js
|
@ -1,102 +0,0 @@
|
||||||
const crypto = require("crypto");
|
|
||||||
|
|
||||||
|
|
||||||
class Blockchain {
|
|
||||||
constructor() {
|
|
||||||
this.chain = [];
|
|
||||||
this.pendingTransactions = [];
|
|
||||||
this.newBlock();
|
|
||||||
this.peers = new Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a node to our peer table
|
|
||||||
*/
|
|
||||||
addPeer(host) {
|
|
||||||
this.peers.add(host);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a node to our peer table
|
|
||||||
*/
|
|
||||||
getPeers() {
|
|
||||||
return Array.from(this.peers);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new block containing any outstanding transactions
|
|
||||||
*/
|
|
||||||
newBlock(previousHash, nonce = null) {
|
|
||||||
let block = {
|
|
||||||
index: this.chain.length,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
transactions: this.pendingTransactions,
|
|
||||||
previousHash,
|
|
||||||
nonce
|
|
||||||
};
|
|
||||||
|
|
||||||
block.hash = Blockchain.hash(block);
|
|
||||||
|
|
||||||
console.log(`Created block ${block.index}`);
|
|
||||||
|
|
||||||
// Add the new block to the blockchain
|
|
||||||
this.chain.push(block);
|
|
||||||
|
|
||||||
// Reset pending transactions
|
|
||||||
this.pendingTransactions = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates a SHA-256 hash of the block
|
|
||||||
*/
|
|
||||||
static hash(block) {
|
|
||||||
const blockString = JSON.stringify(block, Object.keys(block).sort());
|
|
||||||
return crypto.createHash("sha256").update(blockString).digest("hex");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the last block in the chain
|
|
||||||
*/
|
|
||||||
lastBlock() {
|
|
||||||
return this.chain.length && this.chain[this.chain.length - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines if a hash begins with a "difficulty" number of 0s
|
|
||||||
*
|
|
||||||
* @param hashOfBlock: the hash of the block (hex string)
|
|
||||||
* @param difficulty: an integer defining the difficulty
|
|
||||||
*/
|
|
||||||
static powIsAcceptable(hashOfBlock, difficulty) {
|
|
||||||
return hashOfBlock.slice(0, difficulty) === "0".repeat(difficulty);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates a random 32 byte string
|
|
||||||
*/
|
|
||||||
static nonce() {
|
|
||||||
return crypto.createHash("sha256").update(crypto.randomBytes(32)).digest("hex");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Proof of Work mining algorithm
|
|
||||||
*
|
|
||||||
* We hash the block with random string until the hash begins with
|
|
||||||
* a "difficulty" number of 0s.
|
|
||||||
*/
|
|
||||||
mine(blockToMine = null, difficulty = 4) {
|
|
||||||
const block = blockToMine || this.lastBlock();
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
block.nonce = Blockchain.nonce();
|
|
||||||
if (Blockchain.powIsAcceptable(Blockchain.hash(block), difficulty)) {
|
|
||||||
console.log("We mined a block!")
|
|
||||||
console.log(` - Block hash: ${Blockchain.hash(block)}`);
|
|
||||||
console.log(` - nonce: ${block.nonce}`);
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Blockchain;
|
|
37
js/index.js
37
js/index.js
|
@ -1,37 +0,0 @@
|
||||||
const Blockchain = require("./blockchain");
|
|
||||||
const {send} = require("micro");
|
|
||||||
|
|
||||||
const blockchain = new Blockchain();
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = async (request, response) => {
|
|
||||||
const route = request.url;
|
|
||||||
|
|
||||||
// Keep track of the peers that have contacted us
|
|
||||||
blockchain.addPeer(request.headers.host);
|
|
||||||
|
|
||||||
let output;
|
|
||||||
|
|
||||||
switch (route) {
|
|
||||||
case "/new_block":
|
|
||||||
output = blockchain.newBlock();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "/last_block":
|
|
||||||
output = blockchain.lastBlock();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "/get_peers":
|
|
||||||
output = blockchain.getPeers();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "/submit_transaction":
|
|
||||||
output = blockchain.addTransaction(transaction);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
output = blockchain.lastBlock();
|
|
||||||
|
|
||||||
}
|
|
||||||
send(response, 200, output);
|
|
||||||
};
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,17 +0,0 @@
|
||||||
{
|
|
||||||
"name": "blockchain",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"dev": "nodemon blockchain.js",
|
|
||||||
"server": "micro-dev"
|
|
||||||
},
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"dependencies": {
|
|
||||||
"micro": "^9.3.4",
|
|
||||||
"micro-dev": "^3.0.0",
|
|
||||||
"nodemon": "^1.19.0"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,253 @@
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Python package for providing Mozilla's CA Bundle."
|
||||||
|
name = "certifi"
|
||||||
|
optional = false
|
||||||
|
python-versions = "*"
|
||||||
|
version = "2019.11.28"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Universal encoding detector for Python 2 and 3"
|
||||||
|
name = "chardet"
|
||||||
|
optional = false
|
||||||
|
python-versions = "*"
|
||||||
|
version = "3.0.4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Composable command line interface toolkit"
|
||||||
|
name = "click"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
|
||||||
|
version = "7.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "A simple framework for building complex web applications."
|
||||||
|
name = "flask"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
|
||||||
|
version = "1.1.1"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
Jinja2 = ">=2.10.1"
|
||||||
|
Werkzeug = ">=0.15"
|
||||||
|
click = ">=5.1"
|
||||||
|
itsdangerous = ">=0.24"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
dev = ["pytest", "coverage", "tox", "sphinx", "pallets-sphinx-themes", "sphinxcontrib-log-cabinet", "sphinx-issues"]
|
||||||
|
docs = ["sphinx", "pallets-sphinx-themes", "sphinxcontrib-log-cabinet", "sphinx-issues"]
|
||||||
|
dotenv = ["python-dotenv"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||||
|
name = "idna"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
|
||||||
|
version = "2.8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Various helpers to pass data to untrusted environments and back."
|
||||||
|
name = "itsdangerous"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
|
||||||
|
version = "1.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "A very fast and expressive template engine."
|
||||||
|
name = "jinja2"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
|
||||||
|
version = "2.11.1"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
MarkupSafe = ">=0.23"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
i18n = ["Babel (>=0.8)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Safely add untrusted strings to HTML/XML markup."
|
||||||
|
name = "markupsafe"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*"
|
||||||
|
version = "1.1.1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "dev"
|
||||||
|
description = "Add .env support to your django/flask apps in development and deployments"
|
||||||
|
name = "python-dotenv"
|
||||||
|
optional = false
|
||||||
|
python-versions = "*"
|
||||||
|
version = "0.10.5"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
cli = ["click (>=5.0)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Python HTTP for Humans."
|
||||||
|
name = "requests"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
|
||||||
|
version = "2.22.0"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
certifi = ">=2017.4.17"
|
||||||
|
chardet = ">=3.0.2,<3.1.0"
|
||||||
|
idna = ">=2.5,<2.9"
|
||||||
|
urllib3 = ">=1.21.1,<1.25.0 || >1.25.0,<1.25.1 || >1.25.1,<1.26"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
security = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)"]
|
||||||
|
socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7)", "win-inet-pton"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "Simple, fast, extensible JSON encoder/decoder for Python"
|
||||||
|
name = "simplejson"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.5, !=3.0.*, !=3.1.*, !=3.2.*"
|
||||||
|
version = "3.17.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "HTTP library with thread-safe connection pooling, file post, and more."
|
||||||
|
name = "urllib3"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4"
|
||||||
|
version = "1.25.8"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
brotli = ["brotlipy (>=0.6.0)"]
|
||||||
|
secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"]
|
||||||
|
socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
category = "main"
|
||||||
|
description = "The comprehensive WSGI web application library."
|
||||||
|
name = "werkzeug"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
|
||||||
|
version = "0.16.1"
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
dev = ["pytest", "coverage", "tox", "sphinx", "pallets-sphinx-themes", "sphinx-issues"]
|
||||||
|
termcolor = ["termcolor"]
|
||||||
|
watchdog = ["watchdog"]
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
content-hash = "8d835a6cc18d639728b1791a18607b276c0bda68e1f4b74a533142321c1da26f"
|
||||||
|
python-versions = "^3.8"
|
||||||
|
|
||||||
|
[metadata.files]
|
||||||
|
certifi = [
|
||||||
|
{file = "certifi-2019.11.28-py2.py3-none-any.whl", hash = "sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3"},
|
||||||
|
{file = "certifi-2019.11.28.tar.gz", hash = "sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f"},
|
||||||
|
]
|
||||||
|
chardet = [
|
||||||
|
{file = "chardet-3.0.4-py2.py3-none-any.whl", hash = "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"},
|
||||||
|
{file = "chardet-3.0.4.tar.gz", hash = "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"},
|
||||||
|
]
|
||||||
|
click = [
|
||||||
|
{file = "Click-7.0-py2.py3-none-any.whl", hash = "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13"},
|
||||||
|
{file = "Click-7.0.tar.gz", hash = "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"},
|
||||||
|
]
|
||||||
|
flask = [
|
||||||
|
{file = "Flask-1.1.1-py2.py3-none-any.whl", hash = "sha256:45eb5a6fd193d6cf7e0cf5d8a5b31f83d5faae0293695626f539a823e93b13f6"},
|
||||||
|
{file = "Flask-1.1.1.tar.gz", hash = "sha256:13f9f196f330c7c2c5d7a5cf91af894110ca0215ac051b5844701f2bfd934d52"},
|
||||||
|
]
|
||||||
|
idna = [
|
||||||
|
{file = "idna-2.8-py2.py3-none-any.whl", hash = "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"},
|
||||||
|
{file = "idna-2.8.tar.gz", hash = "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"},
|
||||||
|
]
|
||||||
|
itsdangerous = [
|
||||||
|
{file = "itsdangerous-1.1.0-py2.py3-none-any.whl", hash = "sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749"},
|
||||||
|
{file = "itsdangerous-1.1.0.tar.gz", hash = "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19"},
|
||||||
|
]
|
||||||
|
jinja2 = [
|
||||||
|
{file = "Jinja2-2.11.1-py2.py3-none-any.whl", hash = "sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49"},
|
||||||
|
{file = "Jinja2-2.11.1.tar.gz", hash = "sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250"},
|
||||||
|
]
|
||||||
|
markupsafe = [
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27m-macosx_10_6_intel.whl", hash = "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27m-manylinux1_i686.whl", hash = "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27m-win32.whl", hash = "sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27m-win_amd64.whl", hash = "sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_i686.whl", hash = "sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp34-cp34m-macosx_10_6_intel.whl", hash = "sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp34-cp34m-manylinux1_i686.whl", hash = "sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp34-cp34m-manylinux1_x86_64.whl", hash = "sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp34-cp34m-win32.whl", hash = "sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp34-cp34m-win_amd64.whl", hash = "sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp35-cp35m-manylinux1_x86_64.whl", hash = "sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp35-cp35m-win32.whl", hash = "sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp35-cp35m-win_amd64.whl", hash = "sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp36-cp36m-macosx_10_6_intel.whl", hash = "sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp36-cp36m-win32.whl", hash = "sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp36-cp36m-win_amd64.whl", hash = "sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp37-cp37m-macosx_10_6_intel.whl", hash = "sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp37-cp37m-win32.whl", hash = "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2"},
|
||||||
|
{file = "MarkupSafe-1.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c"},
|
||||||
|
{file = "MarkupSafe-1.1.1.tar.gz", hash = "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b"},
|
||||||
|
]
|
||||||
|
python-dotenv = [
|
||||||
|
{file = "python-dotenv-0.10.5.tar.gz", hash = "sha256:f254bfd0c970d64ccbb6c9ebef3667ab301a71473569c991253a481f1c98dddc"},
|
||||||
|
{file = "python_dotenv-0.10.5-py2.py3-none-any.whl", hash = "sha256:440c7c23d53b7d352f9c94d6f70860242c2f071cf5c029dd661ccb22d64ae42b"},
|
||||||
|
]
|
||||||
|
requests = [
|
||||||
|
{file = "requests-2.22.0-py2.py3-none-any.whl", hash = "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"},
|
||||||
|
{file = "requests-2.22.0.tar.gz", hash = "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4"},
|
||||||
|
]
|
||||||
|
simplejson = [
|
||||||
|
{file = "simplejson-3.17.0-cp27-cp27m-macosx_10_13_x86_64.whl", hash = "sha256:87d349517b572964350cc1adc5a31b493bbcee284505e81637d0174b2758ba17"},
|
||||||
|
{file = "simplejson-3.17.0-cp27-cp27m-win32.whl", hash = "sha256:1d1e929cdd15151f3c0b2efe953b3281b2fd5ad5f234f77aca725f28486466f6"},
|
||||||
|
{file = "simplejson-3.17.0-cp27-cp27m-win_amd64.whl", hash = "sha256:1ea59f570b9d4916ae5540a9181f9c978e16863383738b69a70363bc5e63c4cb"},
|
||||||
|
{file = "simplejson-3.17.0-cp33-cp33m-win32.whl", hash = "sha256:8027bd5f1e633eb61b8239994e6fc3aba0346e76294beac22a892eb8faa92ba1"},
|
||||||
|
{file = "simplejson-3.17.0-cp33-cp33m-win_amd64.whl", hash = "sha256:22a7acb81968a7c64eba7526af2cf566e7e2ded1cb5c83f0906b17ff1540f866"},
|
||||||
|
{file = "simplejson-3.17.0-cp34-cp34m-win32.whl", hash = "sha256:17163e643dbf125bb552de17c826b0161c68c970335d270e174363d19e7ea882"},
|
||||||
|
{file = "simplejson-3.17.0-cp34-cp34m-win_amd64.whl", hash = "sha256:0fe3994207485efb63d8f10a833ff31236ed27e3b23dadd0bf51c9900313f8f2"},
|
||||||
|
{file = "simplejson-3.17.0-cp35-cp35m-win32.whl", hash = "sha256:4cf91aab51b02b3327c9d51897960c554f00891f9b31abd8a2f50fd4a0071ce8"},
|
||||||
|
{file = "simplejson-3.17.0-cp35-cp35m-win_amd64.whl", hash = "sha256:fc9051d249dd5512e541f20330a74592f7a65b2d62e18122ca89bf71f94db748"},
|
||||||
|
{file = "simplejson-3.17.0-cp36-cp36m-macosx_10_13_x86_64.whl", hash = "sha256:86afc5b5cbd42d706efd33f280fec7bd7e2772ef54e3f34cf6b30777cd19a614"},
|
||||||
|
{file = "simplejson-3.17.0-cp36-cp36m-win32.whl", hash = "sha256:926bcbef9eb60e798eabda9cd0bbcb0fca70d2779aa0aa56845749d973eb7ad5"},
|
||||||
|
{file = "simplejson-3.17.0-cp36-cp36m-win_amd64.whl", hash = "sha256:daaf4d11db982791be74b23ff4729af2c7da79316de0bebf880fa2d60bcc8c5a"},
|
||||||
|
{file = "simplejson-3.17.0-cp37-cp37m-macosx_10_14_x86_64.whl", hash = "sha256:9a126c3a91df5b1403e965ba63b304a50b53d8efc908a8c71545ed72535374a3"},
|
||||||
|
{file = "simplejson-3.17.0-cp37-cp37m-win32.whl", hash = "sha256:fc046afda0ed8f5295212068266c92991ab1f4a50c6a7144b69364bdee4a0159"},
|
||||||
|
{file = "simplejson-3.17.0-cp37-cp37m-win_amd64.whl", hash = "sha256:7cce4bac7e0d66f3a080b80212c2238e063211fe327f98d764c6acbc214497fc"},
|
||||||
|
{file = "simplejson-3.17.0.tar.gz", hash = "sha256:2b4b2b738b3b99819a17feaf118265d0753d5536049ea570b3c43b51c4701e81"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py2.7.exe", hash = "sha256:1d346c2c1d7dd79c118f0cc7ec5a1c4127e0c8ffc83e7b13fc5709ff78c9bb84"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py3.3.exe", hash = "sha256:5cfd495527f8b85ce21db806567de52d98f5078a8e9427b18e251c68bd573a26"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py3.4.exe", hash = "sha256:8de378d589eccbc75941e480b4d5b4db66f22e4232f87543b136b1f093fff342"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py3.5.exe", hash = "sha256:f4b64a1031acf33e281fd9052336d6dad4d35eee3404c95431c8c6bc7a9c0588"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py3.6.exe", hash = "sha256:ad8dd3454d0c65c0f92945ac86f7b9efb67fa2040ba1b0189540e984df904378"},
|
||||||
|
{file = "simplejson-3.17.0.win-amd64-py3.7.exe", hash = "sha256:229edb079d5dd81bf12da952d4d825bd68d1241381b37d3acf961b384c9934de"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py2.7.exe", hash = "sha256:4fd5f79590694ebff8dc980708e1c182d41ce1fda599a12189f0ca96bf41ad70"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py3.3.exe", hash = "sha256:d140e9376e7f73c1f9e0a8e3836caf5eec57bbafd99259d56979da05a6356388"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py3.4.exe", hash = "sha256:da00675e5e483ead345429d4f1374ab8b949fba4429d60e71ee9d030ced64037"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py3.5.exe", hash = "sha256:7739940d68b200877a15a5ff5149e1599737d6dd55e302625650629350466418"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py3.6.exe", hash = "sha256:60aad424e47c5803276e332b2a861ed7a0d46560e8af53790c4c4fb3420c26c2"},
|
||||||
|
{file = "simplejson-3.17.0.win32-py3.7.exe", hash = "sha256:1fbba86098bbfc1f85c5b69dc9a6d009055104354e0d9880bb00b692e30e0078"},
|
||||||
|
]
|
||||||
|
urllib3 = [
|
||||||
|
{file = "urllib3-1.25.8-py2.py3-none-any.whl", hash = "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc"},
|
||||||
|
{file = "urllib3-1.25.8.tar.gz", hash = "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"},
|
||||||
|
]
|
||||||
|
werkzeug = [
|
||||||
|
{file = "Werkzeug-0.16.1-py2.py3-none-any.whl", hash = "sha256:1e0dedc2acb1f46827daa2e399c1485c8fa17c0d8e70b6b875b4e7f54bf408d2"},
|
||||||
|
{file = "Werkzeug-0.16.1.tar.gz", hash = "sha256:b353856d37dec59d6511359f97f6a4b2468442e454bd1c98298ddce53cac1f04"},
|
||||||
|
]
|
|
@ -0,0 +1,18 @@
|
||||||
|
[tool.poetry]
|
||||||
|
name = "blockchain"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = ["Pavle Portic <pavle.portic@tilda.center>"]
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
python = "^3.8"
|
||||||
|
flask = "^1.1.1"
|
||||||
|
requests = "^2.22.0"
|
||||||
|
simplejson = "^3.17.0"
|
||||||
|
|
||||||
|
[tool.poetry.dev-dependencies]
|
||||||
|
python-dotenv = "^0.10.5"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry>=0.12"]
|
||||||
|
build-backend = "poetry.masonry.api"
|
|
@ -1,2 +1,59 @@
|
||||||
flask==0.12.2
|
certifi==2019.11.28 \
|
||||||
requests==2.18.4
|
--hash=sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3 \
|
||||||
|
--hash=sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f
|
||||||
|
chardet==3.0.4 \
|
||||||
|
--hash=sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691 \
|
||||||
|
--hash=sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae
|
||||||
|
click==7.0 \
|
||||||
|
--hash=sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13 \
|
||||||
|
--hash=sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7
|
||||||
|
flask==1.1.1 \
|
||||||
|
--hash=sha256:45eb5a6fd193d6cf7e0cf5d8a5b31f83d5faae0293695626f539a823e93b13f6 \
|
||||||
|
--hash=sha256:13f9f196f330c7c2c5d7a5cf91af894110ca0215ac051b5844701f2bfd934d52
|
||||||
|
idna==2.8 \
|
||||||
|
--hash=sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c \
|
||||||
|
--hash=sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407
|
||||||
|
itsdangerous==1.1.0 \
|
||||||
|
--hash=sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749 \
|
||||||
|
--hash=sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19
|
||||||
|
jinja2==2.11.1 \
|
||||||
|
--hash=sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49 \
|
||||||
|
--hash=sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250
|
||||||
|
markupsafe==1.1.1 \
|
||||||
|
--hash=sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161 \
|
||||||
|
--hash=sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7 \
|
||||||
|
--hash=sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183 \
|
||||||
|
--hash=sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b \
|
||||||
|
--hash=sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e \
|
||||||
|
--hash=sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f \
|
||||||
|
--hash=sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1 \
|
||||||
|
--hash=sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5 \
|
||||||
|
--hash=sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1 \
|
||||||
|
--hash=sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735 \
|
||||||
|
--hash=sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21 \
|
||||||
|
--hash=sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235 \
|
||||||
|
--hash=sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b \
|
||||||
|
--hash=sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f \
|
||||||
|
--hash=sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905 \
|
||||||
|
--hash=sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1 \
|
||||||
|
--hash=sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d \
|
||||||
|
--hash=sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff \
|
||||||
|
--hash=sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473 \
|
||||||
|
--hash=sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e \
|
||||||
|
--hash=sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66 \
|
||||||
|
--hash=sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5 \
|
||||||
|
--hash=sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d \
|
||||||
|
--hash=sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e \
|
||||||
|
--hash=sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6 \
|
||||||
|
--hash=sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2 \
|
||||||
|
--hash=sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c \
|
||||||
|
--hash=sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b
|
||||||
|
requests==2.22.0 \
|
||||||
|
--hash=sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31 \
|
||||||
|
--hash=sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4
|
||||||
|
urllib3==1.25.8 \
|
||||||
|
--hash=sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc \
|
||||||
|
--hash=sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc
|
||||||
|
werkzeug==0.16.1 \
|
||||||
|
--hash=sha256:1e0dedc2acb1f46827daa2e399c1485c8fa17c0d8e70b6b875b4e7f54bf408d2 \
|
||||||
|
--hash=sha256:b353856d37dec59d6511359f97f6a4b2468442e454bd1c98298ddce53cac1f04
|
||||||
|
|
Loading…
Reference in New Issue