Skip to content

Commit

Permalink
1일차 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeongwook ma committed Sep 4, 2017
1 parent 4df1e35 commit 0a0b39f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 82 deletions.
3 changes: 1 addition & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app import *
from app import log, storage, node, transaction, util, key
from app import log, storage, node, transaction, util, key, block
from app.communicator import Receiver
from app.node import Node

Expand All @@ -11,7 +11,6 @@

def initiate_node(*args):
set_my_node()
communicator.start()

log.write("Start node")
start_node()
Expand Down
89 changes: 88 additions & 1 deletion app/block/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
import datetime

from sqlalchemy import Column, String, Integer, DateTime

from app import storage


class Block(storage.Base):
__tablename__ = 'blocks'

_id = Column(Integer, primary_key=True, autoincrement=True)
type = Column(String)
prev_block_id = Column(String)
prev_block_hash = Column(String)
tx_list = Column(String)
merkle_root = Column(String)
time_stamp = Column(DateTime)
block_id = Column(String)
block_hash = Column(String)
nonce = Column(String)
block_info = Column(String)
block_miner = Column(Integer)

def __init__(self, prev_block_id, prev_block_hash, tx_list, merkle_root, nonce, block_info):
self.type = 'B'
self.prev_block_id = prev_block_id
self.prev_block_hash = prev_block_hash
self.tx_list = tx_list
self.merkle_root = merkle_root
self.time_stamp = datetime.datetime.now()
self.block_id = self.type + self.time_stamp.strftime('%Y%m%d%H%M%S')
self.block_hash = ''
self.nonce = nonce
self.block_info = block_info
self.block_miner = 1

def __str__(self):
return self.to_json()

def to_json(self):
return json.dumps({
'type': self.type,
'time_stamp': self.time_stamp.strftime('%Y%m%d%H%M%S'),
'prev_block_id': self.prev_block_id,
'prev_block_hash': self.prev_block_hash,
'merkle_root': self.merkle_root,
'block_hash': self.block_hash,
'nonce': self.nonce,
'block_id': self.block_id
})


class GenesisBlock(object):
def __init__(self):
self.type = 'B'
self.prev_block_id = 'B000000000000'
self.prev_block_hash = 'block_hash'
self.tx_list = 'sechain'
self.timp_stamp = '0000-00-00-00-00-00'
self.block_id = 'B000000000000'
self.merkle_root = 'sogangfinotek2017'
self.block_hash = 'sechainfinochain2017'
self.nonce = 2010101010


def create_block(block):
storage.insert(block)
storage.insert(block)


def get_my_block():
return 0


def count():
storage.count(Block)


def get_all_block():
return storage.get_all(Block)


def get_last_block():
return get_all_block()[-1]


if __name__ == '__main__':
import json

t = GenesisBlock()
temp = json.dumps(t, indent=4, default=lambda o: o.__dict__, sort_keys=True)
temps = json.loads(temp)
print(type(temps['nonce']))
4 changes: 4 additions & 0 deletions app/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def to_json(self):
'pri_key': self.private_key
})


def remove_node(node):
storage.remove(node)

def add_node(node):
storage.insert_or_update(node, ip_address=node.ip_address)

Expand Down
1 change: 0 additions & 1 deletion app/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import netifaces
import socket

1


def get_ip_address(ifname):
Expand Down
114 changes: 36 additions & 78 deletions mychain.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,19 @@
import signal

import click

from app import *


@click.group()
def cli():
"""Command line interface for private block chain."""
pass


@cli.command()
@click.option('--port', '-p', help="Port number for socket.")
def run(port=None):
if port:
initiate_node(port)
else:
initiate_node(3000)



@cli.command()
@click.option('--message', '-m', help="Message included in transaction.")
def sendtx(message):
pri_key, pub_key = key.get_key()
tx = transaction.create_tx(pub_key, pri_key, message)
transaction.send_tx(tx)


@cli.command()
@click.argument('target')
def list(target):
if target == 'block':
click.echo('list of blocks')
list_all_block()

elif target == 'transaction':
click.echo('list of transactions')
list_all_transaction()

elif target == 'node':
click.echo('list of nodes')
list_all_node()

else:
click.echo('unknown list type')
click.echo('list type should be one of [block|transaction|node]')


def signal_handler(_signal, frame):
import os
os._exit(1)


signal.signal(signal.SIGINT, signal_handler)
communicator.start()
initiate_node(3000)


# Menu 1
def menu1():
print("Input a port number \n")
print("9. Back")
print("0. Quit")
choice = input(" >> ")
print(choice)

if choice != '9' or choice != '0':
port = int(choice)
if port:
initiate_node(port)
else:
initiate_node(3000)

communicator.start()

exec_menu(choice)
return


# Menu 2
def menu2():
def send_tx():
print("Input a message \n")
print("9. Back")
print("0. Quit")
Expand All @@ -97,6 +31,26 @@ def menu2():
return


def show_node_list():
print("\nNode list\n")

list_all_node()
back()


def show_transaction_list():
print("\nTransaction list\n")

list_all_transaction()
back()


def show_block_list():
print("\nBlock list\n")

list_all_block()
back()

# Execute menu
def exec_menu(choice):
ch = choice.lower()
Expand All @@ -113,30 +67,34 @@ def exec_menu(choice):


def main_menu():
print("Command line interface for private block chain.\n")
print("Please choose the menu you want to start:")
print("1. Run")
print("2. Send a transaction")
print("\n0. Quit")
print("\nPlease choose the menu you want to start:")
print("1. Send a transaction")
print("2. Show node list")
print("3. Show transaction list")
print("4. Show block list")

print("\n0. Quit\n")
choice = input(" >> ")
exec_menu(choice)

return


# Back to main menu
def back():
menu_actions['main_menu']()


# Menu definition
menu_actions = {
'main_menu': main_menu,
'1': menu1,
'2': menu2,
'1': send_tx,
'2': show_node_list,
'3': show_transaction_list,
'4': show_block_list,
'9': back,
'0': exit,
}

print("\nCommand line interface for private block chain.\n")

if __name__ == '__main__':
main_menu()

0 comments on commit 0a0b39f

Please sign in to comment.