Compare commits
1 Commits
master
...
dependabot
Author | SHA1 | Date |
---|---|---|
dependabot[bot] | 96db84f183 |
|
@ -1 +0,0 @@
|
|||
__pycache__
|
|
@ -0,0 +1,63 @@
|
|||
###############################################################################
|
||||
# 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
|
|
@ -0,0 +1,12 @@
|
|||
language: python
|
||||
|
||||
python:
|
||||
- 3.6
|
||||
- nightly
|
||||
|
||||
install:
|
||||
- pip install pipenv
|
||||
- pipenv install --dev
|
||||
|
||||
script:
|
||||
- pipenv run python -m unittest
|
32
Dockerfile
32
Dockerfile
|
@ -1,29 +1,15 @@
|
|||
FROM python:3.8-alpine AS builder
|
||||
FROM python:3.6-alpine
|
||||
|
||||
WORKDIR /app
|
||||
ENV PATH="/root/.poetry/bin:$PATH"
|
||||
|
||||
RUN apk add --no-cache build-base libffi-dev curl \
|
||||
&& curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python \
|
||||
&& python -m venv .venv \
|
||||
&& poetry config virtualenvs.in-project true \
|
||||
&& .venv/bin/pip install --no-cache-dir -U pip setuptools
|
||||
# Install dependencies.
|
||||
ADD requirements.txt /app
|
||||
RUN cd /app && \
|
||||
pip install -r requirements.txt
|
||||
|
||||
COPY pyproject.toml poetry.lock ./
|
||||
RUN poetry install --no-dev --no-root --no-interaction
|
||||
# Add actual source code.
|
||||
ADD blockchain.py /app
|
||||
|
||||
COPY blockchain/ ./blockchain/
|
||||
EXPOSE 5000
|
||||
|
||||
|
||||
FROM python:3.8-alpine
|
||||
|
||||
EXPOSE 80
|
||||
WORKDIR /app
|
||||
CMD ["uvicorn", "run:app", "--host", "0.0.0.0", "--port", "80"]
|
||||
ENV PATH="/app/.venv/bin:$PATH" \
|
||||
PYTHONUNBUFFERED=1
|
||||
|
||||
RUN apk add --no-cache libc-dev binutils
|
||||
|
||||
COPY run.py ./
|
||||
COPY --from=builder /app/ ./
|
||||
CMD ["python", "blockchain.py", "--port", "5000"]
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
[[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"
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "45af71184b5b013f58a8601f7f87c9f0b882afe19f197ce45d6d08e46d615159"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
"python_version": "3.6"
|
||||
},
|
||||
"sources": [
|
||||
{
|
||||
"name": "pypi",
|
||||
"url": "https://pypi.python.org/simple",
|
||||
"verify_ssl": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"default": {
|
||||
"certifi": {
|
||||
"hashes": [
|
||||
"sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50",
|
||||
"sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef"
|
||||
],
|
||||
"version": "==2019.9.11"
|
||||
},
|
||||
"chardet": {
|
||||
"hashes": [
|
||||
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
|
||||
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
|
||||
],
|
||||
"version": "==3.0.4"
|
||||
},
|
||||
"click": {
|
||||
"hashes": [
|
||||
"sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13",
|
||||
"sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
|
||||
],
|
||||
"version": "==7.0"
|
||||
},
|
||||
"flask": {
|
||||
"hashes": [
|
||||
"sha256:0749df235e3ff61ac108f69ac178c9770caeaccad2509cb762ce1f65570a8856",
|
||||
"sha256:49f44461237b69ecd901cc7ce66feea0319b9158743dd27a2899962ab214dac1"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.12.2"
|
||||
},
|
||||
"idna": {
|
||||
"hashes": [
|
||||
"sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f",
|
||||
"sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4"
|
||||
],
|
||||
"version": "==2.6"
|
||||
},
|
||||
"itsdangerous": {
|
||||
"hashes": [
|
||||
"sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19",
|
||||
"sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749"
|
||||
],
|
||||
"version": "==1.1.0"
|
||||
},
|
||||
"jinja2": {
|
||||
"hashes": [
|
||||
"sha256:74320bb91f31270f9551d46522e33af46a80c3d619f4a4bf42b3164d30b5911f",
|
||||
"sha256:9fe95f19286cfefaa917656583d020be14e7859c6b0252588391e47db34527de"
|
||||
],
|
||||
"version": "==2.10.3"
|
||||
},
|
||||
"markupsafe": {
|
||||
"hashes": [
|
||||
"sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473",
|
||||
"sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161",
|
||||
"sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235",
|
||||
"sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5",
|
||||
"sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff",
|
||||
"sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b",
|
||||
"sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1",
|
||||
"sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e",
|
||||
"sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183",
|
||||
"sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66",
|
||||
"sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1",
|
||||
"sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1",
|
||||
"sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e",
|
||||
"sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b",
|
||||
"sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905",
|
||||
"sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735",
|
||||
"sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d",
|
||||
"sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e",
|
||||
"sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d",
|
||||
"sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c",
|
||||
"sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21",
|
||||
"sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2",
|
||||
"sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5",
|
||||
"sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b",
|
||||
"sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6",
|
||||
"sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f",
|
||||
"sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f",
|
||||
"sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7"
|
||||
],
|
||||
"version": "==1.1.1"
|
||||
},
|
||||
"requests": {
|
||||
"hashes": [
|
||||
"sha256:6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b",
|
||||
"sha256:9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.18.4"
|
||||
},
|
||||
"urllib3": {
|
||||
"hashes": [
|
||||
"sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b",
|
||||
"sha256:cc44da8e1145637334317feebd728bd869a35285b93cbb4cca2577da7e62db4f"
|
||||
],
|
||||
"version": "==1.22"
|
||||
},
|
||||
"werkzeug": {
|
||||
"hashes": [
|
||||
"sha256:97660b282aa7e29f94f3fe378e5c7162d7ab9d601a8dbb1cbb2ffc8f0e54607d",
|
||||
"sha256:cfd1281b1748288e59762c0e174d64d8bcb2b70e7c57bc4a1203c8825af24ac3"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.15.3"
|
||||
}
|
||||
},
|
||||
"develop": {}
|
||||
}
|
67
README.md
67
README.md
|
@ -1,48 +1,65 @@
|
|||
# Blockchain 101
|
||||
# Learn Blockchains by Building One
|
||||
|
||||
Simple blockchain written in python
|
||||
[![Build Status](https://travis-ci.org/dvf/blockchain.svg?branch=master)](https://travis-ci.org/dvf/blockchain)
|
||||
|
||||
This is the source code for my post on [Building a Blockchain](https://medium.com/p/117428612f46).
|
||||
|
||||
## Installation
|
||||
|
||||
1. Make sure [Python 3.6+](https://www.python.org/downloads/) is installed.
|
||||
2. Install [poetry](https://github.com/sdispater/poetry).
|
||||
3. Install requirements
|
||||
1. Make sure [Python 3.6+](https://www.python.org/downloads/) is installed.
|
||||
2. Install [pipenv](https://github.com/kennethreitz/pipenv).
|
||||
|
||||
```
|
||||
$ poetry install
|
||||
$ pip install pipenv
|
||||
```
|
||||
3. Install requirements
|
||||
```
|
||||
$ pipenv install
|
||||
```
|
||||
|
||||
4. Copy `.env.example` to `.env`
|
||||
|
||||
5. Start some nodes:
|
||||
* `$ poetry run flask run -p 5000`
|
||||
* `$ poetry run flask run -p 5001`
|
||||
|
||||
4. Run the server:
|
||||
* `$ pipenv run python blockchain.py`
|
||||
* `$ pipenv run python blockchain.py -p 5001`
|
||||
* `$ pipenv run python blockchain.py --port 5002`
|
||||
|
||||
## Docker
|
||||
|
||||
Another option for running this blockchain program is to use Docker. Follow the instructions below to create a local Docker container:
|
||||
|
||||
1. Build the docker image
|
||||
1. Clone this repository
|
||||
2. Build the docker container
|
||||
|
||||
```
|
||||
$ docker-compose build
|
||||
$ docker build -t blockchain .
|
||||
```
|
||||
|
||||
2. Run the nodes
|
||||
3. Run the container
|
||||
|
||||
```
|
||||
$ docker-compose up -d
|
||||
$ docker run --rm -p 80:5000 blockchain
|
||||
```
|
||||
|
||||
4. To add more nodes, add a new service to the `docker-compose.yml` file
|
||||
and adjust the port number.
|
||||
4. To add more instances, vary the public port number before the colon:
|
||||
|
||||
```yaml
|
||||
node2:
|
||||
build: .
|
||||
ports:
|
||||
- 5002:80
|
||||
```
|
||||
$ docker run --rm -p 81:5000 blockchain
|
||||
$ docker run --rm -p 82:5000 blockchain
|
||||
$ docker run --rm -p 83:5000 blockchain
|
||||
```
|
||||
|
||||
## Credits
|
||||
## Installation (C# Implementation)
|
||||
|
||||
1. Install a free copy of Visual Studio IDE (Community Edition):
|
||||
https://www.visualstudio.com/vs/
|
||||
|
||||
2. Once installed, open the solution file (BlockChain.sln) using the File > Open > Project/Solution menu options within Visual Studio.
|
||||
|
||||
3. From within the "Solution Explorer", right click the BlockChain.Console project and select the "Set As Startup Project" option.
|
||||
|
||||
4. Click the "Start" button, or hit F5 to run. The program executes in a console window, and is controlled via HTTP with the same commands as the Python version.
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request.
|
||||
|
||||
[dvf](github.com/dvf/) for the original blockchain code
|
||||
|
|
|
@ -0,0 +1,301 @@
|
|||
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)
|
|
@ -1,35 +0,0 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .blockchain import Blockchain
|
||||
|
||||
blockchain = Blockchain()
|
||||
identifier = str(uuid4()).replace('-', '')
|
||||
|
||||
|
||||
def init_routers(app):
|
||||
from .routers import misc
|
||||
app.include_router(
|
||||
misc.router,
|
||||
prefix='',
|
||||
tags=['misc'],
|
||||
)
|
||||
|
||||
from .routers import nodes
|
||||
app.include_router(
|
||||
nodes.router,
|
||||
prefix='/nodes',
|
||||
tags=['nodes'],
|
||||
)
|
||||
|
||||
|
||||
def create_app():
|
||||
app = FastAPI()
|
||||
init_routers(app)
|
||||
|
||||
return app
|
|
@ -1,204 +0,0 @@
|
|||
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 = 8
|
||||
|
||||
|
||||
class Blockchain:
|
||||
def __init__(self):
|
||||
self.current_transactions = []
|
||||
self.chain = []
|
||||
self.peers = 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.peers.add(parsed_url.netloc)
|
||||
elif parsed_url.path:
|
||||
# Accepts an URL without scheme like '192.168.0.5:5000'.
|
||||
self.peers.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, DIFFICULTY):
|
||||
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
|
||||
"""
|
||||
|
||||
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 peers in our network
|
||||
for node in self.peers:
|
||||
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 broadcast_transaction(self, transaction):
|
||||
for node in self.peers:
|
||||
response = requests.post(
|
||||
f'http://{node}/transactions',
|
||||
data=dumps(transaction, separators=(",", ":")),
|
||||
headers={'content-type': 'application/json'},
|
||||
)
|
||||
|
||||
if response.status_code != 202:
|
||||
print(f'Failed to send transaction to node {node}', response)
|
||||
|
||||
def new_transaction(self, sender, recipient, amount, timestamp=None, mine=False):
|
||||
"""
|
||||
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
|
||||
"""
|
||||
transaction = {
|
||||
'sender': sender,
|
||||
'recipient': recipient,
|
||||
'amount': amount,
|
||||
'timestamp': timestamp or time(),
|
||||
}
|
||||
if transaction not in self.current_transactions:
|
||||
self.current_transactions.append(transaction)
|
||||
print(f'added transaction to list {mine}')
|
||||
if not mine:
|
||||
self.broadcast_transaction(transaction)
|
||||
|
||||
@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
|
|
@ -1,7 +0,0 @@
|
|||
#! /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.
|
|
@ -1,69 +0,0 @@
|
|||
#! /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.
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .. import blockchain, identifier
|
||||
from ..schemas import Transaction
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post('/mine', status_code=200)
|
||||
async def mine():
|
||||
last_block = blockchain.last_block
|
||||
proof = blockchain.proof_of_work(last_block)
|
||||
blockchain.new_transaction(
|
||||
sender='0',
|
||||
recipient=identifier,
|
||||
amount=Decimal(1),
|
||||
timestamp=None,
|
||||
mine=True,
|
||||
)
|
||||
|
||||
previous_hash = blockchain.hash(last_block)
|
||||
block = blockchain.new_block(proof, previous_hash)
|
||||
|
||||
return {
|
||||
'msg': 'New Block Forged',
|
||||
'index': block['index'],
|
||||
'transactions': block['transactions'],
|
||||
'proof': block['proof'],
|
||||
'previous_hash': block['previous_hash'],
|
||||
}
|
||||
|
||||
|
||||
@router.post('/transactions', status_code=202)
|
||||
async def create_transaction(request: Transaction):
|
||||
blockchain.new_transaction(
|
||||
request.sender,
|
||||
request.recipient,
|
||||
request.amount,
|
||||
request.timestamp,
|
||||
)
|
||||
|
||||
return {
|
||||
'msg': f'Transaction sent'
|
||||
}
|
||||
|
||||
|
||||
@router.get('/chain', status_code=200)
|
||||
async def full_chain():
|
||||
return {
|
||||
'chain': blockchain.chain,
|
||||
'length': len(blockchain.chain),
|
||||
}
|
||||
|
||||
|
||||
@router.get('/identifier', status_code=200)
|
||||
async def get_identifier():
|
||||
return {
|
||||
'identifier': identifier
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
#! /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.
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from .. import blockchain
|
||||
from ..schemas import NodeList
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('/', status_code=200)
|
||||
async def get_nodes():
|
||||
return {
|
||||
'nodes': list(blockchain.peers),
|
||||
}
|
||||
|
||||
|
||||
@router.get('/{identifier}/balance', status_code=200)
|
||||
async def get_balance(identifier: str):
|
||||
balance = blockchain.get_balance(identifier)
|
||||
if balance is None:
|
||||
return {
|
||||
'msg': 'Chain is not valid',
|
||||
}, 500
|
||||
|
||||
return {
|
||||
'identifier': identifier,
|
||||
'balance': balance,
|
||||
}
|
||||
|
||||
|
||||
@router.post('/register', status_code=201)
|
||||
async def register_nodes(request: NodeList):
|
||||
if None in (request.nodes,):
|
||||
return {'msg': 'No valid list of nodes'}, 400
|
||||
|
||||
for node in request.nodes:
|
||||
blockchain.register_node(node)
|
||||
|
||||
return {
|
||||
'msg': 'New peers have been added',
|
||||
'peers': list(blockchain.peers),
|
||||
}
|
||||
|
||||
|
||||
@router.post('/resolve', status_code=200)
|
||||
async def consensus():
|
||||
replaced = blockchain.resolve_conflicts()
|
||||
|
||||
if replaced:
|
||||
message = 'Our chain was replaced'
|
||||
else:
|
||||
message = 'Our chain is authoritative'
|
||||
|
||||
return {
|
||||
'msg': message,
|
||||
'chain': blockchain.chain,
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
#! /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.
|
||||
|
||||
from decimal import Decimal
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class NodeList(BaseModel):
|
||||
nodes: List[str]
|
||||
|
||||
|
||||
class Transaction(BaseModel):
|
||||
sender: str
|
||||
recipient: str
|
||||
amount: Decimal
|
||||
timestamp: float = None
|
|
@ -1,110 +0,0 @@
|
|||
#! /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
|
||||
import json
|
||||
from decimal import Decimal
|
||||
from multiprocessing import (
|
||||
cpu_count,
|
||||
Pool,
|
||||
Process,
|
||||
Queue
|
||||
)
|
||||
|
||||
|
||||
class DecimalJsonEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Decimal):
|
||||
return float(obj)
|
||||
return super(DecimalJsonEncoder, self).default(obj)
|
||||
|
||||
|
||||
def dumps(*data, **kwargs):
|
||||
return json.dumps(
|
||||
data,
|
||||
cls=DecimalJsonEncoder,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
def do_pooled_pow(last_proof, last_hash, difficulty):
|
||||
queue = Queue()
|
||||
with Pool(1) 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
|
|
@ -0,0 +1,10 @@
|
|||
<?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>
|
|
@ -0,0 +1,63 @@
|
|||
<?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>
|
|
@ -0,0 +1,12 @@
|
|||
namespace BlockChainDemo.Console
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var chain = new BlockChain();
|
||||
var server = new WebServer(chain);
|
||||
System.Console.Read();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
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")]
|
|
@ -0,0 +1,43 @@
|
|||
|
||||
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
|
|
@ -0,0 +1,19 @@
|
|||
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}";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,226 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?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>
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace BlockChainDemo
|
||||
{
|
||||
public class Node
|
||||
{
|
||||
public Uri Address { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
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")]
|
|
@ -0,0 +1,9 @@
|
|||
namespace BlockChainDemo
|
||||
{
|
||||
public class Transaction
|
||||
{
|
||||
public int Amount { get; set; }
|
||||
public string Recipient { get; set; }
|
||||
public string Sender { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<?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>
|
|
@ -1,52 +0,0 @@
|
|||
version: '3'
|
||||
services:
|
||||
node0:
|
||||
build: .
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5000:80
|
||||
|
||||
node1:
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5001:80
|
||||
|
||||
node2:
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5002:80
|
||||
|
||||
node3:
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5003:80
|
||||
|
||||
node4:
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5004:80
|
||||
|
||||
node5:
|
||||
image: blockchain
|
||||
ports:
|
||||
- 5005:80
|
||||
|
||||
# node6:
|
||||
# image: blockchain
|
||||
# ports:
|
||||
# - 5006:80
|
||||
|
||||
# node7:
|
||||
# image: blockchain
|
||||
# ports:
|
||||
# - 5007:80
|
||||
|
||||
# node8:
|
||||
# image: blockchain
|
||||
# ports:
|
||||
# - 5008:80
|
||||
|
||||
# node9:
|
||||
# image: blockchain
|
||||
# ports:
|
||||
# - 5009:80
|
46
init
46
init
|
@ -1,46 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
function register_nodes() {
|
||||
from=$1
|
||||
to_list=$2
|
||||
for to in $to_list; do
|
||||
http POST localhost:500$from/nodes/register \
|
||||
nodes:="[\"http://node$to\"]"
|
||||
done
|
||||
}
|
||||
|
||||
function synchronise_nodes() {
|
||||
http POST localhost:5000/nodes/resolve
|
||||
http POST localhost:5001/nodes/resolve
|
||||
}
|
||||
|
||||
NODE0_ID=$(http localhost:5000/identifier | jq -r ".identifier")
|
||||
NODE1_ID=$(http localhost:5001/identifier | jq -r ".identifier")
|
||||
|
||||
register_nodes "0" "1 3 4"
|
||||
register_nodes "1" "0 5"
|
||||
register_nodes "2" "5"
|
||||
register_nodes "3" "0 4"
|
||||
register_nodes "4" "0 3 5"
|
||||
register_nodes "5" "1 2 4"
|
||||
|
||||
# # Mine a few initial blocks
|
||||
http POST localhost:5000/mine
|
||||
http POST localhost:5000/mine
|
||||
http POST localhost:5001/nodes/resolve
|
||||
http POST localhost:5003/nodes/resolve
|
||||
http POST localhost:5005/nodes/resolve
|
||||
http POST localhost:5002/nodes/resolve
|
||||
# synchronise_nodes >/dev/null
|
||||
|
||||
# http POST localhost:5000/transactions sender=$NODE0_ID recipient=$NODE1_ID amount=1.3
|
||||
# http POST localhost:5000/mine
|
||||
# synchronise_nodes >/dev/null
|
||||
|
||||
# http localhost:5000/nodes/$NODE0_ID/balance
|
||||
# http localhost:5000/nodes/$NODE1_ID/balance
|
||||
|
||||
# echo node0: $NODE0_ID
|
||||
# echo node1: $NODE1_ID
|
|
@ -0,0 +1,102 @@
|
|||
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;
|
|
@ -0,0 +1,37 @@
|
|||
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
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"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"
|
||||
}
|
||||
}
|
|
@ -1,267 +0,0 @@
|
|||
[[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 = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
|
||||
name = "fastapi"
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
version = "0.48.0"
|
||||
|
||||
[package.dependencies]
|
||||
pydantic = ">=0.32.2,<2.0.0"
|
||||
starlette = "0.12.9"
|
||||
|
||||
[package.extras]
|
||||
all = ["requests", "aiofiles", "jinja2", "python-multipart", "itsdangerous", "pyyaml", "graphene", "ujson", "email-validator", "uvicorn", "async-exit-stack", "async-generator"]
|
||||
dev = ["pyjwt", "passlib", "autoflake", "flake8", "uvicorn", "graphene"]
|
||||
doc = ["mkdocs", "mkdocs-material", "markdown-include"]
|
||||
test = ["pytest (>=4.0.0)", "pytest-cov", "mypy", "black", "isort", "requests", "email-validator", "sqlalchemy", "peewee", "databases", "orjson", "async-exit-stack", "async-generator"]
|
||||
|
||||
[[package]]
|
||||
category = "main"
|
||||
description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
|
||||
name = "h11"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
version = "0.9.0"
|
||||
|
||||
[[package]]
|
||||
category = "main"
|
||||
description = "A collection of framework independent HTTP protocol utils."
|
||||
marker = "sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"pypy\""
|
||||
name = "httptools"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
version = "0.0.13"
|
||||
|
||||
[[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 = "Data validation and settings management using python 3.6 type hinting"
|
||||
name = "pydantic"
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
version = "1.4"
|
||||
|
||||
[package.extras]
|
||||
dotenv = ["python-dotenv (>=0.10.4)"]
|
||||
email = ["email-validator (>=1.0.3)"]
|
||||
typing_extensions = ["typing-extensions (>=3.7.2)"]
|
||||
|
||||
[[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 = "The little ASGI library that shines."
|
||||
name = "starlette"
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
version = "0.12.9"
|
||||
|
||||
[package.extras]
|
||||
full = ["aiofiles", "graphene", "itsdangerous", "jinja2", "python-multipart", "pyyaml", "requests", "ujson"]
|
||||
|
||||
[[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 lightning-fast ASGI server."
|
||||
name = "uvicorn"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
version = "0.11.2"
|
||||
|
||||
[package.dependencies]
|
||||
click = ">=7.0.0,<8.0.0"
|
||||
h11 = ">=0.8,<0.10"
|
||||
httptools = "0.0.13"
|
||||
uvloop = ">=0.14.0"
|
||||
websockets = ">=8.0.0,<9.0.0"
|
||||
|
||||
[[package]]
|
||||
category = "main"
|
||||
description = "Fast implementation of asyncio event loop on top of libuv"
|
||||
marker = "sys_platform != \"win32\" and sys_platform != \"cygwin\" and platform_python_implementation != \"pypy\""
|
||||
name = "uvloop"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
version = "0.14.0"
|
||||
|
||||
[[package]]
|
||||
category = "main"
|
||||
description = "An implementation of the WebSocket Protocol (RFC 6455 & 7692)"
|
||||
name = "websockets"
|
||||
optional = false
|
||||
python-versions = ">=3.6.1"
|
||||
version = "8.1"
|
||||
|
||||
[metadata]
|
||||
content-hash = "bd7659287574f6e4041d1f1a687f7ed818fdebcb9ea3ddd9fdf16ede2279db6f"
|
||||
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"},
|
||||
]
|
||||
fastapi = [
|
||||
{file = "fastapi-0.48.0-py3-none-any.whl", hash = "sha256:87d409d3ac3957713c016ba3b28fa5214e0903d4351cc3fe486b170d29e8aacd"},
|
||||
{file = "fastapi-0.48.0.tar.gz", hash = "sha256:2e00347f6a84291a5f04302733fbcf7c2ad9c674c0d0448cbee661db0e01ca16"},
|
||||
]
|
||||
h11 = [
|
||||
{file = "h11-0.9.0-py2.py3-none-any.whl", hash = "sha256:4bc6d6a1238b7615b266ada57e0618568066f57dd6fa967d1290ec9309b2f2f1"},
|
||||
{file = "h11-0.9.0.tar.gz", hash = "sha256:33d4bca7be0fa039f4e84d50ab00531047e53d6ee8ffbc83501ea602c169cae1"},
|
||||
]
|
||||
httptools = [
|
||||
{file = "httptools-0.0.13.tar.gz", hash = "sha256:e00cbd7ba01ff748e494248183abc6e153f49181169d8a3d41bb49132ca01dfc"},
|
||||
]
|
||||
idna = [
|
||||
{file = "idna-2.8-py2.py3-none-any.whl", hash = "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"},
|
||||
{file = "idna-2.8.tar.gz", hash = "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407"},
|
||||
]
|
||||
pydantic = [
|
||||
{file = "pydantic-1.4-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:07911aab70f3bc52bb845ce1748569c5e70478ac977e106a150dd9d0465ebf04"},
|
||||
{file = "pydantic-1.4-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:012c422859bac2e03ab3151ea6624fecf0e249486be7eb8c6ee69c91740c6752"},
|
||||
{file = "pydantic-1.4-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:61d22d36808087d3184ed6ac0d91dd71c533b66addb02e4a9930e1e30833202f"},
|
||||
{file = "pydantic-1.4-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:f863456d3d4bf817f2e5248553dee3974c5dc796f48e6ddb599383570f4215ac"},
|
||||
{file = "pydantic-1.4-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:bbbed364376f4a0aebb9ea452ff7968b306499a9e74f4db69b28ff2cd4043a11"},
|
||||
{file = "pydantic-1.4-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:e27559cedbd7f59d2375bfd6eea29a330ea1a5b0589c34d6b4e0d7bec6027bbf"},
|
||||
{file = "pydantic-1.4-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:50e4e948892a6815649ad5a9a9379ad1e5f090f17842ac206535dfaed75c6f2f"},
|
||||
{file = "pydantic-1.4-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:8848b4eb458469739126e4c1a202d723dd092e087f8dbe3104371335f87ba5df"},
|
||||
{file = "pydantic-1.4-cp38-cp38-manylinux1_i686.whl", hash = "sha256:831a0265a9e3933b3d0f04d1a81bba543bafbe4119c183ff2771871db70524ab"},
|
||||
{file = "pydantic-1.4-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:47b8db7024ba3d46c3d4768535e1cf87b6c8cf92ccd81e76f4e1cb8ee47688b3"},
|
||||
{file = "pydantic-1.4-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:51f11c8bbf794a68086540da099aae4a9107447c7a9d63151edbb7d50110cf21"},
|
||||
{file = "pydantic-1.4-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:6100d7862371115c40be55cc4b8d766a74b1d0dbaf99dbfe72bb4bac0faf89ed"},
|
||||
{file = "pydantic-1.4-py36.py37.py38-none-any.whl", hash = "sha256:72184c1421103cca128300120f8f1185fb42a9ea73a1c9845b1c53db8c026a7d"},
|
||||
{file = "pydantic-1.4.tar.gz", hash = "sha256:f17ec336e64d4583311249fb179528e9a2c27c8a2eaf590ec6ec2c6dece7cb3f"},
|
||||
]
|
||||
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"},
|
||||
]
|
||||
starlette = [
|
||||
{file = "starlette-0.12.9.tar.gz", hash = "sha256:c2ac9a42e0e0328ad20fe444115ac5e3760c1ee2ac1ff8cdb5ec915c4a453411"},
|
||||
]
|
||||
urllib3 = [
|
||||
{file = "urllib3-1.25.8-py2.py3-none-any.whl", hash = "sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc"},
|
||||
{file = "urllib3-1.25.8.tar.gz", hash = "sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc"},
|
||||
]
|
||||
uvicorn = [
|
||||
{file = "uvicorn-0.11.2-py3-none-any.whl", hash = "sha256:4a35496af38e4deeec911f4af99b0bace19669c986210b0a950ad2b7bfd5737a"},
|
||||
{file = "uvicorn-0.11.2.tar.gz", hash = "sha256:11f397855c7f35dc034a3d288883382a4c16afdfe6675b70896f55bd6051da64"},
|
||||
]
|
||||
uvloop = [
|
||||
{file = "uvloop-0.14.0-cp35-cp35m-macosx_10_11_x86_64.whl", hash = "sha256:08b109f0213af392150e2fe6f81d33261bb5ce968a288eb698aad4f46eb711bd"},
|
||||
{file = "uvloop-0.14.0-cp35-cp35m-manylinux2010_x86_64.whl", hash = "sha256:4544dcf77d74f3a84f03dd6278174575c44c67d7165d4c42c71db3fdc3860726"},
|
||||
{file = "uvloop-0.14.0-cp36-cp36m-macosx_10_11_x86_64.whl", hash = "sha256:b4f591aa4b3fa7f32fb51e2ee9fea1b495eb75b0b3c8d0ca52514ad675ae63f7"},
|
||||
{file = "uvloop-0.14.0-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:f07909cd9fc08c52d294b1570bba92186181ca01fe3dc9ffba68955273dd7362"},
|
||||
{file = "uvloop-0.14.0-cp37-cp37m-macosx_10_11_x86_64.whl", hash = "sha256:afd5513c0ae414ec71d24f6f123614a80f3d27ca655a4fcf6cabe50994cc1891"},
|
||||
{file = "uvloop-0.14.0-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:e7514d7a48c063226b7d06617cbb12a14278d4323a065a8d46a7962686ce2e95"},
|
||||
{file = "uvloop-0.14.0-cp38-cp38-macosx_10_11_x86_64.whl", hash = "sha256:bcac356d62edd330080aed082e78d4b580ff260a677508718f88016333e2c9c5"},
|
||||
{file = "uvloop-0.14.0-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:4315d2ec3ca393dd5bc0b0089d23101276778c304d42faff5dc4579cb6caef09"},
|
||||
{file = "uvloop-0.14.0.tar.gz", hash = "sha256:123ac9c0c7dd71464f58f1b4ee0bbd81285d96cdda8bc3519281b8973e3a461e"},
|
||||
]
|
||||
websockets = [
|
||||
{file = "websockets-8.1-cp36-cp36m-macosx_10_6_intel.whl", hash = "sha256:3762791ab8b38948f0c4d281c8b2ddfa99b7e510e46bd8dfa942a5fff621068c"},
|
||||
{file = "websockets-8.1-cp36-cp36m-manylinux1_i686.whl", hash = "sha256:3db87421956f1b0779a7564915875ba774295cc86e81bc671631379371af1170"},
|
||||
{file = "websockets-8.1-cp36-cp36m-manylinux1_x86_64.whl", hash = "sha256:4f9f7d28ce1d8f1295717c2c25b732c2bc0645db3215cf757551c392177d7cb8"},
|
||||
{file = "websockets-8.1-cp36-cp36m-manylinux2010_i686.whl", hash = "sha256:295359a2cc78736737dd88c343cd0747546b2174b5e1adc223824bcaf3e164cb"},
|
||||
{file = "websockets-8.1-cp36-cp36m-manylinux2010_x86_64.whl", hash = "sha256:1d3f1bf059d04a4e0eb4985a887d49195e15ebabc42364f4eb564b1d065793f5"},
|
||||
{file = "websockets-8.1-cp36-cp36m-win32.whl", hash = "sha256:2db62a9142e88535038a6bcfea70ef9447696ea77891aebb730a333a51ed559a"},
|
||||
{file = "websockets-8.1-cp36-cp36m-win_amd64.whl", hash = "sha256:0e4fb4de42701340bd2353bb2eee45314651caa6ccee80dbd5f5d5978888fed5"},
|
||||
{file = "websockets-8.1-cp37-cp37m-macosx_10_6_intel.whl", hash = "sha256:9b248ba3dd8a03b1a10b19efe7d4f7fa41d158fdaa95e2cf65af5a7b95a4f989"},
|
||||
{file = "websockets-8.1-cp37-cp37m-manylinux1_i686.whl", hash = "sha256:ce85b06a10fc65e6143518b96d3dca27b081a740bae261c2fb20375801a9d56d"},
|
||||
{file = "websockets-8.1-cp37-cp37m-manylinux1_x86_64.whl", hash = "sha256:965889d9f0e2a75edd81a07592d0ced54daa5b0785f57dc429c378edbcffe779"},
|
||||
{file = "websockets-8.1-cp37-cp37m-manylinux2010_i686.whl", hash = "sha256:751a556205d8245ff94aeef23546a1113b1dd4f6e4d102ded66c39b99c2ce6c8"},
|
||||
{file = "websockets-8.1-cp37-cp37m-manylinux2010_x86_64.whl", hash = "sha256:3ef56fcc7b1ff90de46ccd5a687bbd13a3180132268c4254fc0fa44ecf4fc422"},
|
||||
{file = "websockets-8.1-cp37-cp37m-win32.whl", hash = "sha256:7ff46d441db78241f4c6c27b3868c9ae71473fe03341340d2dfdbe8d79310acc"},
|
||||
{file = "websockets-8.1-cp37-cp37m-win_amd64.whl", hash = "sha256:20891f0dddade307ffddf593c733a3fdb6b83e6f9eef85908113e628fa5a8308"},
|
||||
{file = "websockets-8.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:c1ec8db4fac31850286b7cd3b9c0e1b944204668b8eb721674916d4e28744092"},
|
||||
{file = "websockets-8.1-cp38-cp38-manylinux1_i686.whl", hash = "sha256:5c01fd846263a75bc8a2b9542606927cfad57e7282965d96b93c387622487485"},
|
||||
{file = "websockets-8.1-cp38-cp38-manylinux1_x86_64.whl", hash = "sha256:9bef37ee224e104a413f0780e29adb3e514a5b698aabe0d969a6ba426b8435d1"},
|
||||
{file = "websockets-8.1-cp38-cp38-manylinux2010_i686.whl", hash = "sha256:d705f8aeecdf3262379644e4b55107a3b55860eb812b673b28d0fbc347a60c55"},
|
||||
{file = "websockets-8.1-cp38-cp38-manylinux2010_x86_64.whl", hash = "sha256:c8a116feafdb1f84607cb3b14aa1418424ae71fee131642fc568d21423b51824"},
|
||||
{file = "websockets-8.1-cp38-cp38-win32.whl", hash = "sha256:e898a0863421650f0bebac8ba40840fc02258ef4714cb7e1fd76b6a6354bda36"},
|
||||
{file = "websockets-8.1-cp38-cp38-win_amd64.whl", hash = "sha256:f8a7bff6e8664afc4e6c28b983845c5bc14965030e3fb98789734d416af77c4b"},
|
||||
{file = "websockets-8.1.tar.gz", hash = "sha256:5c65d2da8c6bce0fca2528f69f44b2f977e06954c8512a952222cea50dad430f"},
|
||||
]
|
|
@ -1,18 +0,0 @@
|
|||
[tool.poetry]
|
||||
name = "blockchain"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Pavle Portic <pavle.portic@tilda.center>"]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.8"
|
||||
requests = "^2.22.0"
|
||||
fastapi = {extras = ["uvicorn"], version = "^0.48.0"}
|
||||
uvicorn = "^0.11.2"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
python-dotenv = "^0.10.5"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry>=0.12"]
|
||||
build-backend = "poetry.masonry.api"
|
|
@ -1,79 +1,2 @@
|
|||
certifi==2019.11.28 \
|
||||
--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
|
||||
fastapi==0.48.0 \
|
||||
--hash=sha256:87d409d3ac3957713c016ba3b28fa5214e0903d4351cc3fe486b170d29e8aacd \
|
||||
--hash=sha256:2e00347f6a84291a5f04302733fbcf7c2ad9c674c0d0448cbee661db0e01ca16
|
||||
h11==0.9.0 \
|
||||
--hash=sha256:4bc6d6a1238b7615b266ada57e0618568066f57dd6fa967d1290ec9309b2f2f1 \
|
||||
--hash=sha256:33d4bca7be0fa039f4e84d50ab00531047e53d6ee8ffbc83501ea602c169cae1
|
||||
httptools==0.0.13; sys_platform != "win32" and sys_platform != "cygwin" and platform_python_implementation != "pypy" \
|
||||
--hash=sha256:e00cbd7ba01ff748e494248183abc6e153f49181169d8a3d41bb49132ca01dfc
|
||||
idna==2.8 \
|
||||
--hash=sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c \
|
||||
--hash=sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407
|
||||
pydantic==1.4 \
|
||||
--hash=sha256:07911aab70f3bc52bb845ce1748569c5e70478ac977e106a150dd9d0465ebf04 \
|
||||
--hash=sha256:012c422859bac2e03ab3151ea6624fecf0e249486be7eb8c6ee69c91740c6752 \
|
||||
--hash=sha256:61d22d36808087d3184ed6ac0d91dd71c533b66addb02e4a9930e1e30833202f \
|
||||
--hash=sha256:f863456d3d4bf817f2e5248553dee3974c5dc796f48e6ddb599383570f4215ac \
|
||||
--hash=sha256:bbbed364376f4a0aebb9ea452ff7968b306499a9e74f4db69b28ff2cd4043a11 \
|
||||
--hash=sha256:e27559cedbd7f59d2375bfd6eea29a330ea1a5b0589c34d6b4e0d7bec6027bbf \
|
||||
--hash=sha256:50e4e948892a6815649ad5a9a9379ad1e5f090f17842ac206535dfaed75c6f2f \
|
||||
--hash=sha256:8848b4eb458469739126e4c1a202d723dd092e087f8dbe3104371335f87ba5df \
|
||||
--hash=sha256:831a0265a9e3933b3d0f04d1a81bba543bafbe4119c183ff2771871db70524ab \
|
||||
--hash=sha256:47b8db7024ba3d46c3d4768535e1cf87b6c8cf92ccd81e76f4e1cb8ee47688b3 \
|
||||
--hash=sha256:51f11c8bbf794a68086540da099aae4a9107447c7a9d63151edbb7d50110cf21 \
|
||||
--hash=sha256:6100d7862371115c40be55cc4b8d766a74b1d0dbaf99dbfe72bb4bac0faf89ed \
|
||||
--hash=sha256:72184c1421103cca128300120f8f1185fb42a9ea73a1c9845b1c53db8c026a7d \
|
||||
--hash=sha256:f17ec336e64d4583311249fb179528e9a2c27c8a2eaf590ec6ec2c6dece7cb3f
|
||||
requests==2.22.0 \
|
||||
--hash=sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31 \
|
||||
--hash=sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4
|
||||
starlette==0.12.9 \
|
||||
--hash=sha256:c2ac9a42e0e0328ad20fe444115ac5e3760c1ee2ac1ff8cdb5ec915c4a453411
|
||||
urllib3==1.25.8 \
|
||||
--hash=sha256:2f3db8b19923a873b3e5256dc9c2dedfa883e33d87c690d9c7913e1f40673cdc \
|
||||
--hash=sha256:87716c2d2a7121198ebcb7ce7cccf6ce5e9ba539041cfbaeecfb641dc0bf6acc
|
||||
uvicorn==0.11.2 \
|
||||
--hash=sha256:4a35496af38e4deeec911f4af99b0bace19669c986210b0a950ad2b7bfd5737a \
|
||||
--hash=sha256:11f397855c7f35dc034a3d288883382a4c16afdfe6675b70896f55bd6051da64
|
||||
uvloop==0.14.0; sys_platform != "win32" and sys_platform != "cygwin" and platform_python_implementation != "pypy" \
|
||||
--hash=sha256:08b109f0213af392150e2fe6f81d33261bb5ce968a288eb698aad4f46eb711bd \
|
||||
--hash=sha256:4544dcf77d74f3a84f03dd6278174575c44c67d7165d4c42c71db3fdc3860726 \
|
||||
--hash=sha256:b4f591aa4b3fa7f32fb51e2ee9fea1b495eb75b0b3c8d0ca52514ad675ae63f7 \
|
||||
--hash=sha256:f07909cd9fc08c52d294b1570bba92186181ca01fe3dc9ffba68955273dd7362 \
|
||||
--hash=sha256:afd5513c0ae414ec71d24f6f123614a80f3d27ca655a4fcf6cabe50994cc1891 \
|
||||
--hash=sha256:e7514d7a48c063226b7d06617cbb12a14278d4323a065a8d46a7962686ce2e95 \
|
||||
--hash=sha256:bcac356d62edd330080aed082e78d4b580ff260a677508718f88016333e2c9c5 \
|
||||
--hash=sha256:4315d2ec3ca393dd5bc0b0089d23101276778c304d42faff5dc4579cb6caef09 \
|
||||
--hash=sha256:123ac9c0c7dd71464f58f1b4ee0bbd81285d96cdda8bc3519281b8973e3a461e
|
||||
websockets==8.1 \
|
||||
--hash=sha256:3762791ab8b38948f0c4d281c8b2ddfa99b7e510e46bd8dfa942a5fff621068c \
|
||||
--hash=sha256:3db87421956f1b0779a7564915875ba774295cc86e81bc671631379371af1170 \
|
||||
--hash=sha256:4f9f7d28ce1d8f1295717c2c25b732c2bc0645db3215cf757551c392177d7cb8 \
|
||||
--hash=sha256:295359a2cc78736737dd88c343cd0747546b2174b5e1adc223824bcaf3e164cb \
|
||||
--hash=sha256:1d3f1bf059d04a4e0eb4985a887d49195e15ebabc42364f4eb564b1d065793f5 \
|
||||
--hash=sha256:2db62a9142e88535038a6bcfea70ef9447696ea77891aebb730a333a51ed559a \
|
||||
--hash=sha256:0e4fb4de42701340bd2353bb2eee45314651caa6ccee80dbd5f5d5978888fed5 \
|
||||
--hash=sha256:9b248ba3dd8a03b1a10b19efe7d4f7fa41d158fdaa95e2cf65af5a7b95a4f989 \
|
||||
--hash=sha256:ce85b06a10fc65e6143518b96d3dca27b081a740bae261c2fb20375801a9d56d \
|
||||
--hash=sha256:965889d9f0e2a75edd81a07592d0ced54daa5b0785f57dc429c378edbcffe779 \
|
||||
--hash=sha256:751a556205d8245ff94aeef23546a1113b1dd4f6e4d102ded66c39b99c2ce6c8 \
|
||||
--hash=sha256:3ef56fcc7b1ff90de46ccd5a687bbd13a3180132268c4254fc0fa44ecf4fc422 \
|
||||
--hash=sha256:7ff46d441db78241f4c6c27b3868c9ae71473fe03341340d2dfdbe8d79310acc \
|
||||
--hash=sha256:20891f0dddade307ffddf593c733a3fdb6b83e6f9eef85908113e628fa5a8308 \
|
||||
--hash=sha256:c1ec8db4fac31850286b7cd3b9c0e1b944204668b8eb721674916d4e28744092 \
|
||||
--hash=sha256:5c01fd846263a75bc8a2b9542606927cfad57e7282965d96b93c387622487485 \
|
||||
--hash=sha256:9bef37ee224e104a413f0780e29adb3e514a5b698aabe0d969a6ba426b8435d1 \
|
||||
--hash=sha256:d705f8aeecdf3262379644e4b55107a3b55860eb812b673b28d0fbc347a60c55 \
|
||||
--hash=sha256:c8a116feafdb1f84607cb3b14aa1418424ae71fee131642fc568d21423b51824 \
|
||||
--hash=sha256:e898a0863421650f0bebac8ba40840fc02258ef4714cb7e1fd76b6a6354bda36 \
|
||||
--hash=sha256:f8a7bff6e8664afc4e6c28b983845c5bc14965030e3fb98789734d416af77c4b \
|
||||
--hash=sha256:5c65d2da8c6bce0fca2528f69f44b2f977e06954c8512a952222cea50dad430f
|
||||
flask==0.12.2
|
||||
requests==2.18.4
|
||||
|
|
Loading…
Reference in New Issue