Skip to content

Commit 1b4f678

Browse files
committed
Rename CTransaction/CMutableTransaction GetHash() to GetTxid()
Preparation for a future segwit-enabled release.
1 parent 47af6c6 commit 1b4f678

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

bitcoin/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2012-2016 The python-bitcoinlib developers
1+
# Copyright (C) 2012-2017 The python-bitcoinlib developers
22
#
33
# This file is part of python-bitcoinlib.
44
#
@@ -16,7 +16,7 @@
1616
# Note that setup.py can break if __init__.py imports any external
1717
# dependencies, as these might not be installed when setup.py runs. In this
1818
# case __version__ could be moved to a separate version.py and imported here.
19-
__version__ = '0.7.1-SNAPSHOT'
19+
__version__ = '0.8.0-SNAPSHOT'
2020

2121
class MainParams(bitcoin.core.CoreMainParams):
2222
MESSAGE_START = b'\xf9\xbe\xb4\xd9'

bitcoin/core/__init__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2012-2015 The python-bitcoinlib developers
1+
# Copyright (C) 2012-2017 The python-bitcoinlib developers
22
#
33
# This file is part of python-bitcoinlib.
44
#
@@ -356,8 +356,21 @@ def from_tx(cls, tx):
356356
else:
357357
return cls(tx.vin, tx.vout, tx.nLockTime, tx.nVersion)
358358

359+
def GetTxid(self):
360+
return super(CTransaction, self).GetHash()
359361

360-
@__make_mutable
362+
def GetHash(self):
363+
raise AttributeError("CTransaction.GetHash() has been removed; use GetTxid() instead")
364+
365+
366+
# preserve GetHash so our AttributeError-raising stub works
367+
def __make_CMutableTransaction_mutable(cls):
368+
get_hash_fn = cls.GetHash
369+
cls = __make_mutable(cls)
370+
cls.GetHash = get_hash_fn
371+
return cls
372+
373+
@__make_CMutableTransaction_mutable
361374
class CMutableTransaction(CTransaction):
362375
"""A mutable transaction"""
363376
__slots__ = []
@@ -384,7 +397,11 @@ def from_tx(cls, tx):
384397

385398
return cls(vin, vout, tx.nLockTime, tx.nVersion)
386399

400+
def GetTxid(self):
401+
return Serializable.GetHash(self)
387402

403+
def GetHash(self):
404+
raise AttributeError("CMutableTransaction.GetHash() has been removed; use GetTxid() instead")
388405

389406

390407
class CBlockHeader(ImmutableSerializable):
@@ -478,7 +495,7 @@ def build_merkle_tree_from_txids(txids):
478495
@staticmethod
479496
def build_merkle_tree_from_txs(txs):
480497
"""Build a full merkle tree from transactions"""
481-
txids = [tx.GetHash() for tx in txs]
498+
txids = [tx.GetTxid() for tx in txs]
482499
return CBlock.build_merkle_tree_from_txids(txids)
483500

484501
def calc_merkle_root(self):
@@ -730,7 +747,7 @@ def CheckBlock(block, fCheckPoW = True, fCheckMerkleRoot = True, cur_time=None):
730747

731748
CheckTransaction(tx)
732749

733-
txid = tx.GetHash()
750+
txid = tx.GetTxid()
734751
if txid in unique_txids:
735752
raise CheckBlockError("CheckBlock() : duplicate transaction")
736753
unique_txids.add(txid)

bitcoin/core/scripteval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2012-2014 The python-bitcoinlib developers
1+
# Copyright (C) 2012-2017 The python-bitcoinlib developers
22
#
33
# This file is part of python-bitcoinlib.
44
#
@@ -816,7 +816,7 @@ def VerifySignature(txFrom, txTo, inIdx):
816816
raise VerifySignatureError("txin prevout.n >= len(txFrom.vout)")
817817
txout = txFrom.vout[txin.prevout.n]
818818

819-
if txin.prevout.hash != txFrom.GetHash():
819+
if txin.prevout.hash != txFrom.GetTxid():
820820
raise VerifySignatureError("prevout hash does not match txFrom")
821821

822822
VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, inIdx)

bitcoin/tests/test_scripteval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2013-2014 The python-bitcoinlib developers
1+
# Copyright (C) 2013-2017 The python-bitcoinlib developers
22
#
33
# This file is part of python-bitcoinlib.
44
#
@@ -89,7 +89,7 @@ def create_test_txs(self, scriptSig, scriptPubKey):
8989
txCredit = CTransaction([CTxIn(COutPoint(), CScript([OP_0, OP_0]), nSequence=0xFFFFFFFF)],
9090
[CTxOut(0, scriptPubKey)],
9191
nLockTime=0)
92-
txSpend = CTransaction([CTxIn(COutPoint(txCredit.GetHash(), 0), scriptSig, nSequence=0xFFFFFFFF)],
92+
txSpend = CTransaction([CTxIn(COutPoint(txCredit.GetTxid(), 0), scriptSig, nSequence=0xFFFFFFFF)],
9393
[CTxOut(0, CScript())],
9494
nLockTime=0)
9595
return (txCredit, txSpend)

bitcoin/tests/test_transactions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ def test_GetHash(self):
104104
self.assertNotEqual(h1, txin.GetHash())
105105

106106
class Test_CTransaction(unittest.TestCase):
107+
def test_GetHash_removed(self):
108+
tx = CTransaction()
109+
with self.assertRaises(AttributeError):
110+
tx.GetHash()
111+
112+
tx = CMutableTransaction()
113+
with self.assertRaises(AttributeError):
114+
tx.GetHash()
115+
107116
def test_is_coinbase(self):
108117
tx = CMutableTransaction()
109118
self.assertFalse(tx.is_coinbase())

release-notes.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# python-bitcoinlib release notes
22

3+
## v0.8.0-PENDING
4+
5+
Major breaking API change!
6+
7+
While this interm release changes doesn't by itself include segwit support, it
8+
does change the name of the `CTransaction/CMutableTransaction` method
9+
`GetHash()` to `GetTxid()` to prepare for a future segwit-enabled release.
10+
Incorrect calls to `GetHash()` will now raise a `AttributeError` exception with
11+
an explanation.
12+
13+
Since this release doesn't yet include segwit support, you will need to set the
14+
Bitcoin Core `-rpcserialversion=0` option, either as a command line argument,
15+
or in your `bitcoin.conf` file. Otherwise the RPC interface will return
16+
segwit-serialized transactions that this release's RPC support doesn't
17+
understand.
18+
19+
320
## v0.7.0
421

522
Breaking API changes:

0 commit comments

Comments
 (0)