From 0a0b39f1e4b599a3a63457d0b6be05ea33073a6f Mon Sep 17 00:00:00 2001 From: kyeongwook ma Date: Mon, 4 Sep 2017 18:30:36 +0900 Subject: [PATCH] =?UTF-8?q?1=EC=9D=BC=EC=B0=A8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__init__.py | 3 +- app/block/__init__.py | 89 ++++++++++++++++++++++++++++++++- app/node/__init__.py | 4 ++ app/util/__init__.py | 1 - mychain.py | 114 +++++++++++++----------------------------- 5 files changed, 129 insertions(+), 82 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 2fa4e20..696fff9 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 @@ -11,7 +11,6 @@ def initiate_node(*args): set_my_node() - communicator.start() log.write("Start node") start_node() diff --git a/app/block/__init__.py b/app/block/__init__.py index d12b3e8..e0c302d 100755 --- a/app/block/__init__.py +++ b/app/block/__init__.py @@ -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) \ No newline at end of file + 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'])) diff --git a/app/node/__init__.py b/app/node/__init__.py index 21953b1..27099e2 100755 --- a/app/node/__init__.py +++ b/app/node/__init__.py @@ -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) diff --git a/app/util/__init__.py b/app/util/__init__.py index aed33af..b2cf090 100755 --- a/app/util/__init__.py +++ b/app/util/__init__.py @@ -1,7 +1,6 @@ import netifaces import socket -1 def get_ip_address(ifname): diff --git a/mychain.py b/mychain.py index ad13c90..6bc6a85 100755 --- a/mychain.py +++ b/mychain.py @@ -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") @@ -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() @@ -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()