Skip to content

Commit

Permalink
Store order of transactions in block in Transaction objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Feb 13, 2024
1 parent 8ce0c58 commit 71296b2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions bitcoinlib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ class DbTransaction(Base):
raw = Column(LargeBinary,
doc="Raw transaction hexadecimal string. Transaction is included in raw format on the blockchain")
verified = Column(Boolean, default=False, doc="Is transaction verified. Default is False")
order_n = Column(Integer, doc="Order of transaction in block")

__table_args__ = (
UniqueConstraint('wallet_id', 'txid', name='constraint_wallet_transaction_hash_unique'),
Expand Down
3 changes: 2 additions & 1 deletion bitcoinlib/services/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ def commit(self):
def _parse_db_transaction(db_tx):
t = Transaction(locktime=db_tx.locktime, version=db_tx.version, network=db_tx.network_name,
fee=db_tx.fee, txid=db_tx.txid.hex(), date=db_tx.date, confirmations=db_tx.confirmations,
block_height=db_tx.block_height, status='confirmed', witness_type=db_tx.witness_type.value)
block_height=db_tx.block_height, status='confirmed', witness_type=db_tx.witness_type.value,
order_n=db_tx.order_n)
for n in db_tx.nodes:
if n.is_input:
if n.ref_txid == b'\00' * 32:
Expand Down
6 changes: 5 additions & 1 deletion bitcoinlib/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,8 @@ def load(txid=None, filename=None):
def __init__(self, inputs=None, outputs=None, locktime=0, version=None,
network=DEFAULT_NETWORK, fee=None, fee_per_kb=None, size=None, txid='', txhash='', date=None,
confirmations=None, block_height=None, block_hash=None, input_total=0, output_total=0, rawtx=b'',
status='new', coinbase=False, verified=False, witness_type='segwit', flag=None, replace_by_fee=False):
status='new', coinbase=False, verified=False, witness_type='segwit', flag=None, replace_by_fee=False,
order_n=None):
"""
Create a new transaction class with provided inputs and outputs.
Expand Down Expand Up @@ -1118,6 +1119,8 @@ def __init__(self, inputs=None, outputs=None, locktime=0, version=None,
:type witness_type: str
:param flag: Transaction flag to indicate version, for example for SegWit
:type flag: bytes, str
:param order_n: Order of transaction in block. Used when parsing blocks
:type order_n: int
"""

Expand Down Expand Up @@ -1178,6 +1181,7 @@ def __init__(self, inputs=None, outputs=None, locktime=0, version=None,
self.witness_type = witness_type
self.replace_by_fee = replace_by_fee
self.change = 0
self.order_n = order_n
self.calc_weight_units()
if self.witness_type not in ['legacy', 'segwit']:
raise TransactionError("Please specify a valid witness type: legacy or segwit")
Expand Down

0 comments on commit 71296b2

Please sign in to comment.