# 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


import eventlet
import genesisblock
import msgs
import jserialize as js
import bserialize as bs
import logging
import database
import struct
import ec
import settings
from utils import *
from database import txn_required
    pass
    
txs = database.open_db("txs.dat", dbtype=database.DB.DB_HASH, table_name="txs")
txs.set_get_returns_none(0)
log = logging.getLogger("pycoin.transactions")

class Tx(js.Entity, bs.Entity):
    fields = {
        "tx":msgs.Tx,
        "block":js.Hash,
        "blkindex":js.Int,
        #"flags":js.Int,
        "redeemed":js.List(js.Hash),
    }
    bfields = [
        ("tx", msgs.Tx),
        ("block", bs.Hash),
        ("blkindex", bs.structfmt("<L")),
        #("flags", bs.VarInt),
        ("redeemed", bs.VarList(bs.Hash)),
    ]
    
    @staticmethod
    def get_by_hash(h):
        """get a transaction from the database.
        throws KeyError, if not found.
        """
        log.debug("getting tx %s", h2h(h))
        return Tx.frombinary(txs.get(h))[0]
        

# 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


# Path: transactions.py
import eventlet
import genesisblock
import msgs
import jserialize as js
import bserialize as bs
import logging
import database
import struct
import ec
import settings
from utils import *
from database import txn_required

    pass
    
txs = database.open_db("txs.dat", dbtype=database.DB.DB_HASH, table_name="txs")
txs.set_get_returns_none(0)
log = logging.getLogger("pycoin.transactions")

class Tx(js.Entity, bs.Entity):
    fields = {
        "tx":msgs.Tx,
        "block":js.Hash,
        "blkindex":js.Int,
        #"flags":js.Int,
        "redeemed":js.List(js.Hash),
    }
    bfields = [
        ("tx", msgs.Tx),
        ("block", bs.Hash),
        ("blkindex", bs.structfmt("<L")),
        #("flags", bs.VarInt),
        ("redeemed", bs.VarList(bs.Hash)),
    ]
    
    @staticmethod
    def get_by_hash(h):
        """get a transaction from the database.
        throws KeyError, if not found.
        """
        log.debug("getting tx %s", h2h(h))
        return Tx.frombinary(txs.get(h))[0]
        

    @txn_required