Skip to content
This repository was archived by the owner on Jun 20, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ ENV BUILD_LIST git

RUN apk add --update $BUILD_LIST \
&& git clone https://github.com/leesander1/ABC.git /abc \
&& git checkout -b docker origin/networking \
&& apk --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --update add leveldb leveldb-dev \
&& apk add --no-cache gcc \
&& apk add musl-dev \
&& pip install pipenv \
&& pipenv --python=python3.6 \
&& pipenv install \
Expand Down
11 changes: 1 addition & 10 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
[[source]]

name = "pypi"
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"
plyvel = "*"
125 changes: 125 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ The core functions we will discuss, explore and develop are the following:
3. Communication with other nodes
4. Proof of work algorithms
5. Transactions


### Docker

```bash
$ docker build -t abc .
```
```bash
$ docker run -it --rm -p 80:5000 abc
$ docker run -it --rm -p 80:5001 abc
$ docker run -it --rm -p 80:5002 abc
```
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
''' Main File '''
# from block import Block
import _thread, time
from src.client import CLI
from src.network.network import start_server, seed_peers

_thread.start_new_thread(start_server, ())
time.sleep(1)
CLI().cmdloop()
42 changes: 28 additions & 14 deletions src/block/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class Block(object):
def __init__(self, previous_hash=None, transactions=None):
def __init__(self, previous_hash=None, transactions=None, **kwargs):
"""
Constructor for Block class
Block Header: Key info on block (80 bytes)
Expand All @@ -30,19 +30,33 @@ def __init__(self, previous_hash=None, transactions=None):
merkle_root - a hash of all the hashed transactions in the merkle tree

"""

# define block header attributes
self.version = version.encode('utf8') # 4 bytes
self.previous_hash = previous_hash.encode('utf8') # 32 bytes
self.merkle_root = self.merkle_root(transactions) # 32 bytes
self.timestamp = self.block_timestamp() # 4 bytes
self.nonce = None # 4 bytes
self.target = None # 4 bytes

# define rest of block
self.transactions = transactions # NOTE: may need to change
self.txcount = len(transactions) # 4 bytes
self.size = self.block_size() # 4 bytes
self.payload = kwargs.pop('payload', None)
if self.payload:
# define block header attributes
self.version = self.payload['header']['version'].encode('utf8') # 4 bytes
self.previous_hash = self.payload['header']['parent'].encode('utf8') # 32 bytes
self.merkle_root = self.payload['header']['merkle_root'] # 32 bytes
self.timestamp = self.payload['header']['timestamp'].encode('utf8') # 4 bytes
self.nonce = self.payload['header']['nonce']
self.target = self.payload['header']['target']

# define rest of block
self.transactions = self.payload['transactions'] # NOTE: may need to change
self.txcount = self.payload['txcount'] # 4 bytes
self.size = self.payload['size']
else:
# define block header attributes
self.version = version.encode('utf8') # 4 bytes
self.previous_hash = previous_hash.encode('utf8') # 32 bytes
self.merkle_root = self.merkle_root(transactions) # 32 bytes
self.timestamp = self.block_timestamp() # 4 bytes
self.nonce = None # 4 bytes
self.target = None # 4 bytes

# define rest of block
self.transactions = transactions # NOTE: may need to change
self.txcount = len(transactions) # 4 bytes
self.size = self.block_size() # 4 bytes

def block_size(self):
# calculates the size of the block and returns instance size to value
Expand Down
7 changes: 7 additions & 0 deletions src/client/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
''' The cmd line interface '''
import cmd
import json
import _thread

from src.client.helpers import cromulon
# TODO: this module should not explicitly import persist or config. Need api fot this
from src.core import mine, create_transaction, get_block, init_configuration
from src.network.network import start_server, seed_peers


class CLI(cmd.Cmd, object):
Expand Down Expand Up @@ -54,6 +56,7 @@ def do_block_info(self, arg):
print(json.dumps(b, indent=4, sort_keys=True))

def do_send(self, arg):
'Send an amount'
# TODO: add exception handling for wrong input format
try:
args = arg.split()
Expand Down Expand Up @@ -103,6 +106,10 @@ def preloop(self):
self.wallet = self.conf.get_conf("wallet")
self.peers = self.conf.get_conf("peers")

# _thread.start_new_thread(start_server, ()) # start flask server on new thread
# seed_peers()


def postloop(self):
'Do stuff on end'
# this is where we want to save all the data on exit
Expand Down
28 changes: 24 additions & 4 deletions src/configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ def create_conf(self):
'address': hashed_address,
'amount': 0
},
'ip': "0.0.0.0",
'port': "5000",
'peers': {
'1': {
'ip': "127.0.0.1",
'port': 3390
'ip': "0.0.0.0",
'port': 5001
},
'2': {
'ip': "localhost",
'port': 3390
'ip': "0.0.0.0",
'port': 5002
}
}
}
Expand All @@ -89,6 +91,24 @@ def update_previous_hash(self, block_hash):
self.save_conf()
return self.conf

def add_balance(self, new_amount):
"""
Updates the wallet amount, aka the balance, by adding new_amount
:param new_amount: amount to add
:return: None
"""
self.conf["wallet"]["amount"] = self.conf["wallet"]["amount"] + new_amount
self.save_conf()

def subtract_balance(self, amount):
"""
Subtracts an amount from the balance
:param amount: amount to subtract
:return: None
"""
self.conf["wallet"]["amount"] = self.conf["wallet"]["amount"] - amount
self.save_conf()

def get_conf(self, key=None):
"""
returns the value for the matching key in the configuration
Expand Down
3 changes: 2 additions & 1 deletion src/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from src.core.api import mine, create_transaction, get_block, init_configuration
from src.core.api import mine, create_transaction, get_block, init_configuration
from src.core import threader
13 changes: 12 additions & 1 deletion src/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from src.configuration import Configuration
from src.transaction import Transaction
from src.wallet import get_public_key, get_private_key

from src.network import network

def mine():
# config object
Expand All @@ -26,7 +26,16 @@ def mine():
save_block(b)
conf.increment_height()
conf.update_previous_hash(b.block_hash())
# TODO: create new transmit with mined block
network.transmit(b, "block")

def verify_block(b):
# need to check txns
conf = Configuration()
bh = Block(payload=json.loads(b))
save_block(bh)
conf.increment_height()
conf.update_previous_hash(bh.block_hash())

def create_transaction(recipient, amount):
"""
Expand All @@ -41,6 +50,8 @@ def create_transaction(recipient, amount):
tx.add_output(recipient, amount)
tx.unlock_inputs(get_private_key(), get_public_key("string"))
save_verified_transaction(tx.get_transaction_id(), tx.get_data())
network.transmit(tx.get_data(), "txn")

except ValueError as e:
# Will raise if insufficient utxos are found
raise ValueError("INSUFFICIENT FUNDS")
Expand Down
31 changes: 31 additions & 0 deletions src/core/threader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import threading

# class Threader(object):
# '''
# Threading class for the application
# '''
#
# def __init__(self):
# self.threads = []
#
# def startBackgroundThread(self, method, args=False):
# '''
# Start new thread
# '''
#
# if args:
# newThread = threading.Thread(target=method, args=args)
# else:
# newThread = threading.Thread(target=method)
#
# newThread.start()
#
# self.threads.append(newThread)
#
# def waitForThreads(self, timeout=5.00):
# '''
# Send stop signal to threads and wait for them to end
# '''
#
# for thread in self.threads:
# thread.join(timeout)
1 change: 1 addition & 0 deletions src/network/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from src.network import network
Loading