Skip to content

Commit c85ffe6

Browse files
committed
Test transaction selection when gbt called without segwit support
1 parent abe7b3d commit c85ffe6

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

qa/rpc-tests/segwit.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from test_framework.util import *
99
from test_framework.mininode import sha256, ripemd160, CTransaction, CTxIn, COutPoint, CTxOut, COIN
1010
from test_framework.address import script_to_p2sh, key_to_p2pkh
11-
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, hash160
11+
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, hash160, OP_TRUE
1212
from io import BytesIO
13-
from test_framework.mininode import FromHex, ToHex
13+
from test_framework.mininode import ToHex, FromHex, COIN
1414

1515
NODE_0 = 0
1616
NODE_1 = 1
@@ -250,12 +250,54 @@ def run_test(self):
250250
assert(tmpl['transactions'][0]['txid'] == txid)
251251
assert(tmpl['transactions'][0]['sigops'] == 8)
252252

253+
self.nodes[0].generate(1) # Mine a block to clear the gbt cache
254+
253255
self.log.info("Non-segwit miners are able to use GBT response after activation.")
254-
txid = send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.998"))
255-
#assert_raises_jsonrpc(-8, "Support for 'segwit' rule requires explicit client support", self.nodes[0].getblocktemplate, {})
256-
tmpl = self.nodes[0].getblocktemplate()
257-
# TODO: add a transaction with witness to mempool, and verify it's not
258-
# selected for mining.
256+
# Create a 3-tx chain: tx1 (non-segwit input, paying to a segwit output) ->
257+
# tx2 (segwit input, paying to a non-segwit output) ->
258+
# tx3 (non-segwit input, paying to a non-segwit output).
259+
# tx1 is allowed to appear in the block, but no others.
260+
txid1 = send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
261+
hex_tx = self.nodes[0].gettransaction(txid)['hex']
262+
tx = FromHex(CTransaction(), hex_tx)
263+
assert(tx.wit.is_null()) # This should not be a segwit input
264+
assert(txid1 in self.nodes[0].getrawmempool())
265+
266+
# Now create tx2, which will spend from txid1.
267+
tx = CTransaction()
268+
tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
269+
tx.vout.append(CTxOut(int(49.99*COIN), CScript([OP_TRUE])))
270+
tx2_hex = self.nodes[0].signrawtransaction(ToHex(tx))['hex']
271+
txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
272+
tx = FromHex(CTransaction(), tx2_hex)
273+
assert(not tx.wit.is_null())
274+
275+
# Now create tx3, which will spend from txid2
276+
tx = CTransaction()
277+
tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
278+
tx.vout.append(CTxOut(int(49.95*COIN), CScript([OP_TRUE]))) # Huge fee
279+
tx.calc_sha256()
280+
txid3 = self.nodes[0].sendrawtransaction(ToHex(tx))
281+
assert(tx.wit.is_null())
282+
assert(txid3 in self.nodes[0].getrawmempool())
283+
284+
# Now try calling getblocktemplate() without segwit support.
285+
template = self.nodes[0].getblocktemplate()
286+
287+
# Check that tx1 is the only transaction of the 3 in the template.
288+
template_txids = [ t['txid'] for t in template['transactions'] ]
289+
assert(txid2 not in template_txids and txid3 not in template_txids)
290+
assert(txid1 in template_txids)
291+
292+
# Check that running with segwit support results in all 3 being included.
293+
template = self.nodes[0].getblocktemplate({"rules": ["segwit"]})
294+
template_txids = [ t['txid'] for t in template['transactions'] ]
295+
assert(txid1 in template_txids)
296+
assert(txid2 in template_txids)
297+
assert(txid3 in template_txids)
298+
299+
# Mine a block to clear the gbt cache again.
300+
self.nodes[0].generate(1)
259301

260302
self.log.info("Verify behaviour of importaddress, addwitnessaddress and listunspent")
261303

0 commit comments

Comments
 (0)