Compare commits
1 Commits
master
...
dvf/long-f
Author | SHA1 | Date |
---|---|---|
Daniel van Flymen | 98c070af9c |
|
@ -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
|
|
@ -102,5 +102,3 @@ ENV/
|
|||
|
||||
# PyCharm
|
||||
.idea/
|
||||
|
||||
node_modules/
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
language: python
|
||||
|
||||
python:
|
||||
- 3.6
|
||||
- nightly
|
||||
|
||||
install:
|
||||
- pip install pipenv
|
||||
- pipenv install --dev
|
||||
|
||||
script:
|
||||
- pipenv run python -m unittest
|
29
Dockerfile
29
Dockerfile
|
@ -1,29 +0,0 @@
|
|||
FROM python:3.8-alpine AS builder
|
||||
|
||||
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
|
||||
|
||||
COPY pyproject.toml poetry.lock ./
|
||||
RUN poetry install --no-dev --no-root --no-interaction
|
||||
|
||||
COPY blockchain/ ./blockchain/
|
||||
|
||||
|
||||
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/ ./
|
21
LICENSE
21
LICENSE
|
@ -1,21 +0,0 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2017 Daniel van Flymen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
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,66 @@
|
|||
import json
|
||||
import os
|
||||
|
||||
from datetime import datetime
|
||||
from hashlib import sha256, md5
|
||||
|
||||
|
||||
class Blockchain(object):
|
||||
def __init__(self):
|
||||
self.chain = []
|
||||
self.pending_transactions = []
|
||||
|
||||
# Create the genesis block
|
||||
print("Creating genesis block")
|
||||
|
||||
self.new_block()
|
||||
|
||||
def new_block(self, previous_hash=None, nonce=None):
|
||||
block = {
|
||||
'index': len(self.chain),
|
||||
'timestamp': datetime.utcnow().isoformat(),
|
||||
'transactions': self.pending_transactions,
|
||||
'previous_hash': previous_hash,
|
||||
'nonce': nonce,
|
||||
}
|
||||
|
||||
# Get the hash of this new block, and add it to the block
|
||||
block["hash"] = self.hash(block)
|
||||
|
||||
print(f"Created block {block['index']}")
|
||||
|
||||
# Add the block to the chain
|
||||
self.chain.append(block)
|
||||
|
||||
# Reset the list of pending transactions
|
||||
self.pending_transactions = []
|
||||
return block
|
||||
|
||||
@staticmethod
|
||||
def hash(block):
|
||||
# Transform the block dict into a json string with sorted keys
|
||||
block_string = json.dumps(block, sort_keys=True).encode()
|
||||
|
||||
# ... and hash it
|
||||
return sha256(block_string).hexdigest()
|
||||
|
||||
def last_block(self):
|
||||
# Returns the last block in the chain (if there are blocks)
|
||||
return self.chain[-1] if self.chain else None
|
||||
|
||||
@staticmethod
|
||||
def pow_is_acceptable(hash_of_block, difficulty):
|
||||
return hash_of_block[:difficulty] == "0" * difficulty
|
||||
|
||||
@staticmethod
|
||||
def nonce():
|
||||
return sha256(os.urandom(16)).hexdigest()
|
||||
|
||||
def proof_of_work(self, block=None, difficulty=4):
|
||||
block = block or self.last_block()
|
||||
|
||||
while True:
|
||||
block["nonce"] = self.nonce()
|
||||
if self.pow_is_acceptable(hash_of_block=self.hash(block), difficulty=difficulty):
|
||||
print(f"Block hash is {self.hash(block)} with random string {block['nonce']}")
|
||||
return block
|
|
@ -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
|
|
@ -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
|
|
@ -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,3 @@
|
|||
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==1.0.2
|
||||
requests==2.18.4
|
||||
pytest==4.4.0
|
||||
|
|
7
run.py
7
run.py
|
@ -1,7 +0,0 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim:fenc=utf-8
|
||||
|
||||
from blockchain import create_app
|
||||
|
||||
app = create_app()
|
|
@ -1,104 +1,51 @@
|
|||
import hashlib
|
||||
import json
|
||||
from unittest import TestCase
|
||||
import pytest
|
||||
|
||||
from blockchain import Blockchain
|
||||
|
||||
|
||||
class BlockchainTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.blockchain = Blockchain()
|
||||
|
||||
def create_block(self, proof=123, previous_hash='abc'):
|
||||
self.blockchain.new_block(proof, previous_hash)
|
||||
|
||||
def create_transaction(self, sender='a', recipient='b', amount=1):
|
||||
self.blockchain.new_transaction(
|
||||
sender=sender,
|
||||
recipient=recipient,
|
||||
amount=amount
|
||||
)
|
||||
@pytest.fixture
|
||||
def blockchain():
|
||||
return Blockchain()
|
||||
|
||||
|
||||
class TestRegisterNodes(BlockchainTestCase):
|
||||
class TestBlockchain:
|
||||
def test_genesis_block(self, blockchain):
|
||||
genesis_block = blockchain.chain[0]
|
||||
|
||||
def test_valid_nodes(self):
|
||||
blockchain = Blockchain()
|
||||
assert len(blockchain.chain) == 1
|
||||
assert genesis_block["index"] == 0
|
||||
assert genesis_block["transactions"] == []
|
||||
assert genesis_block["timestamp"]
|
||||
assert genesis_block["previous_hash"] is None
|
||||
assert genesis_block["nonce"] is None
|
||||
assert genesis_block["hash"]
|
||||
|
||||
blockchain.register_node('http://192.168.0.1:5000')
|
||||
def test_pending_transactions_reset_after_block_addition(self, blockchain):
|
||||
blockchain.pending_transactions.append({"from": "Alice", "to": "Bob", "amount": 12})
|
||||
blockchain.new_block()
|
||||
|
||||
self.assertIn('192.168.0.1:5000', blockchain.nodes)
|
||||
assert blockchain.pending_transactions == []
|
||||
|
||||
def test_malformed_nodes(self):
|
||||
blockchain = Blockchain()
|
||||
def test_blocks_are_hashed_correctly(self, blockchain):
|
||||
some_block = blockchain.new_block(previous_hash="abc123", nonce="abc123")
|
||||
|
||||
blockchain.register_node('http//192.168.0.1:5000')
|
||||
assert some_block["hash"] == blockchain.hash({k: v for k, v in some_block.items() if k != "hash"})
|
||||
|
||||
self.assertNotIn('192.168.0.1:5000', blockchain.nodes)
|
||||
def test_blockchain_returns_last_block(self, blockchain):
|
||||
some_block = blockchain.new_block(previous_hash="abc123", nonce="abc123")
|
||||
|
||||
def test_idempotency(self):
|
||||
blockchain = Blockchain()
|
||||
assert blockchain.last_block() == some_block
|
||||
|
||||
blockchain.register_node('http://192.168.0.1:5000')
|
||||
blockchain.register_node('http://192.168.0.1:5000')
|
||||
def test_pow_is_acceptable(self, blockchain):
|
||||
assert blockchain.pow_is_acceptable("00000acbdef123123987hsdkfjskdf213", 5) is True
|
||||
assert blockchain.pow_is_acceptable("0000acbdef123123987hsdkfjskdf2134", 5) is False
|
||||
|
||||
assert len(blockchain.nodes) == 1
|
||||
def test_nonce(self, blockchain):
|
||||
assert blockchain.nonce()
|
||||
assert blockchain.nonce() != blockchain.nonce()
|
||||
|
||||
def test_proof_of_work(self, blockchain):
|
||||
mined_block = blockchain.proof_of_work(difficulty=3)
|
||||
|
||||
class TestBlocksAndTransactions(BlockchainTestCase):
|
||||
|
||||
def test_block_creation(self):
|
||||
self.create_block()
|
||||
|
||||
latest_block = self.blockchain.last_block
|
||||
|
||||
# The genesis block is create at initialization, so the length should be 2
|
||||
assert len(self.blockchain.chain) == 2
|
||||
assert latest_block['index'] == 2
|
||||
assert latest_block['timestamp'] is not None
|
||||
assert latest_block['proof'] == 123
|
||||
assert latest_block['previous_hash'] == 'abc'
|
||||
|
||||
def test_create_transaction(self):
|
||||
self.create_transaction()
|
||||
|
||||
transaction = self.blockchain.current_transactions[-1]
|
||||
|
||||
assert transaction
|
||||
assert transaction['sender'] == 'a'
|
||||
assert transaction['recipient'] == 'b'
|
||||
assert transaction['amount'] == 1
|
||||
|
||||
def test_block_resets_transactions(self):
|
||||
self.create_transaction()
|
||||
|
||||
initial_length = len(self.blockchain.current_transactions)
|
||||
|
||||
self.create_block()
|
||||
|
||||
current_length = len(self.blockchain.current_transactions)
|
||||
|
||||
assert initial_length == 1
|
||||
assert current_length == 0
|
||||
|
||||
def test_return_last_block(self):
|
||||
self.create_block()
|
||||
|
||||
created_block = self.blockchain.last_block
|
||||
|
||||
assert len(self.blockchain.chain) == 2
|
||||
assert created_block is self.blockchain.chain[-1]
|
||||
|
||||
|
||||
class TestHashingAndProofs(BlockchainTestCase):
|
||||
|
||||
def test_hash_is_correct(self):
|
||||
self.create_block()
|
||||
|
||||
new_block = self.blockchain.last_block
|
||||
new_block_json = json.dumps(self.blockchain.last_block, sort_keys=True).encode()
|
||||
new_hash = hashlib.sha256(new_block_json).hexdigest()
|
||||
|
||||
assert len(new_hash) == 64
|
||||
assert new_hash == self.blockchain.hash(new_block)
|
||||
assert mined_block["nonce"]
|
||||
assert blockchain.hash(mined_block)[:3] == "000"
|
||||
|
|
Loading…
Reference in New Issue