# 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
            return False
        prev_blk = self.get_prev()
        if prev_blk.chain == INVALID_CHAIN:
            return False
        if prev_blk.get_next_bits() != self.block.bits:
            return False
        return True
        
    @txn_required    
    def confirm(self):
        log.info("confirming block %s(%d)", h2h(self.hash), self.number)
        if not self.verify():
            raise Invalidblock()
        if not set_bestblock(self):
            log.info("block %s(%d) not good", h2h(self.hash), self.number)
            return False
        self.chain = MAIN_CHAIN
        self.put()
        run_hooks(settings.BLOCK_CONFIRM_HOOKS, self)
            
    @txn_required    
    def revert(self):
        log.info("reverting block %s(%d)", h2h(self.hash), self.number)
        set_bestblock(self.get_prev(), check=False)
        if self.chain != MAIN_CHAIN:
            log.debug("reverting a block, that is not in main chain? (bug?)")
        self.chain = SIDE_CHAIN 
        self.put()
        run_hooks(settings.BLOCK_REVERT_HOOKS, self)
                    

# 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

            return False
        prev_blk = self.get_prev()
        if prev_blk.chain == INVALID_CHAIN:
            return False
        if prev_blk.get_next_bits() != self.block.bits:
            return False
        return True
        
    @txn_required    
    def confirm(self):
        log.info("confirming block %s(%d)", h2h(self.hash), self.number)
        if not self.verify():
            raise Invalidblock()
        if not set_bestblock(self):
            log.info("block %s(%d) not good", h2h(self.hash), self.number)
            return False
        self.chain = MAIN_CHAIN
        self.put()
        run_hooks(settings.BLOCK_CONFIRM_HOOKS, self)
            
    @txn_required    
    def revert(self):
        log.info("reverting block %s(%d)", h2h(self.hash), self.number)
        set_bestblock(self.get_prev(), check=False)
        if self.chain != MAIN_CHAIN:
            log.debug("reverting a block, that is not in main chain? (bug?)")
        self.chain = SIDE_CHAIN 
        self.put()
        run_hooks(settings.BLOCK_REVERT_HOOKS, self)
                    

    @txn_required