# Path: database.py
# def txn_required(func):
#     @functools.wraps(func)
#     def _func(*args, **kwargs):
#         txn = current_txn()
#         if txn:
#             return func(*args, **kwargs)
#         else:
#             return run_in_transaction(func, txn_name=func.__name__, *args, **kwargs)
#     return _func
# 
# class TxnAbort(Exception):
#     def __init__(self, callback=None, *args, **kwargs):
#         self.__callback = callback
#         self.__args = args
#         self.__kwargs = kwargs
#         
#     @property
#     def callback(self):
#         if self.__callback:
#             return lambda: self.__callback(*self.__args, **self.__kwargs)
#         else:
#             return None
#             
#     def __call__(self):
#         cb = self.callback
#         if cb: return cb()
# 
# @contextmanager
# def Transaction(name="*UNKNOWN*", flags=DEFAULT_TXN_FLAGS):
#         txn = begin_txn(flags)
#         txn.set_name(name)
#         txn_log.debug("TXN BEGIN %s", repr(txn))
#         try:
#             yield txn
#         except Exception as e:
#             txn_log.debug("TXN ABORT %s", repr(txn)) 
#             abort_txn()
#             raise
#         txn_log.debug("TXN COMMIT %s", repr(txn))
#         commit_txn()


import eventlet
import genesisblock
import msgs
import jserialize as js
import bserialize as bs
import logging
import settings
import database
import transactions
import struct
    import sys
    import time
        import debug
from utils import *
from database import txn_required, TxnAbort, Transaction
    else:
        log.info("block(%s) missing prev(%s)", h2h(blk.hash), h2h(blk.prev))
        
if not Block.exist_by_hash(genesisblock.hash):
    add_genesis(genesisblock.blkmsg)
    
def read_blocks(f):
    while True:
        try:
            print "reading block at %d" % f.tell()
            magic, size = struct.unpack("<LL", f.read(8))
            blkdata = f.read(size)
            blkmsg = msgs.Blockmsg.frombinary(blkdata)[0]
            yield blkmsg
        except struct.error:
            return

if __name__ == "__main__":
    logging.basicConfig(format='%(name)s - %(message)s', level=logging.INFO)
    cmd = sys.argv[1]
    if cmd == "loadblocks":
        blkfile = open(sys.argv[2],"r")
        try:
            blkfile.seek(int(sys.argv[3]))
        except IndexError:
            pass
        for blkmsg in read_blocks(blkfile):
            process_blockmsg(blkmsg)
            eventlet.sleep(0)
    if cmd == "bestblock":

# Path: database.py
# def txn_required(func):
#     @functools.wraps(func)
#     def _func(*args, **kwargs):
#         txn = current_txn()
#         if txn:
#             return func(*args, **kwargs)
#         else:
#             return run_in_transaction(func, txn_name=func.__name__, *args, **kwargs)
#     return _func
# 
# class TxnAbort(Exception):
#     def __init__(self, callback=None, *args, **kwargs):
#         self.__callback = callback
#         self.__args = args
#         self.__kwargs = kwargs
#         
#     @property
#     def callback(self):
#         if self.__callback:
#             return lambda: self.__callback(*self.__args, **self.__kwargs)
#         else:
#             return None
#             
#     def __call__(self):
#         cb = self.callback
#         if cb: return cb()
# 
# @contextmanager
# def Transaction(name="*UNKNOWN*", flags=DEFAULT_TXN_FLAGS):
#         txn = begin_txn(flags)
#         txn.set_name(name)
#         txn_log.debug("TXN BEGIN %s", repr(txn))
#         try:
#             yield txn
#         except Exception as e:
#             txn_log.debug("TXN ABORT %s", repr(txn)) 
#             abort_txn()
#             raise
#         txn_log.debug("TXN COMMIT %s", repr(txn))
#         commit_txn()


# Path: blockchain.py
import eventlet
import genesisblock
import msgs
import jserialize as js
import bserialize as bs
import logging
import settings
import database
import transactions
import struct
    import sys
    import time
        import debug
from utils import *
from database import txn_required, TxnAbort, Transaction

    else:
        log.info("block(%s) missing prev(%s)", h2h(blk.hash), h2h(blk.prev))
        
if not Block.exist_by_hash(genesisblock.hash):
    add_genesis(genesisblock.blkmsg)
    
def read_blocks(f):
    while True:
        try:
            print "reading block at %d" % f.tell()
            magic, size = struct.unpack("<LL", f.read(8))
            blkdata = f.read(size)
            blkmsg = msgs.Blockmsg.frombinary(blkdata)[0]
            yield blkmsg
        except struct.error:
            return

if __name__ == "__main__":
    logging.basicConfig(format='%(name)s - %(message)s', level=logging.INFO)
    cmd = sys.argv[1]
    if cmd == "loadblocks":
        blkfile = open(sys.argv[2],"r")
        try:
            blkfile.seek(int(sys.argv[3]))
        except IndexError:
            pass
        for blkmsg in read_blocks(blkfile):
            process_blockmsg(blkmsg)
            eventlet.sleep(0)
    if cmd == "bestblock":

        with Transaction("bestblock", flags=[database.DB.DB_READ_UNCOMMITTED]):