You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p2p-segwit.py 88KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Test segwit transactions and blocks on P2P network."""
  6. from test_framework.mininode import *
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import *
  9. from test_framework.script import *
  10. from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
  11. from test_framework.key import CECKey, CPubKey
  12. import time
  13. import random
  14. from binascii import hexlify
  15. # The versionbit bit used to signal activation of SegWit
  16. VB_WITNESS_BIT = 1
  17. VB_PERIOD = 144
  18. VB_ACTIVATION_THRESHOLD = 108
  19. VB_TOP_BITS = 0x20000000
  20. MAX_SIGOP_COST = 80000
  21. # Calculate the virtual size of a witness block:
  22. # (base + witness/4)
  23. def get_virtual_size(witness_block):
  24. base_size = len(witness_block.serialize())
  25. total_size = len(witness_block.serialize(with_witness=True))
  26. # the "+3" is so we round up
  27. vsize = int((3*base_size + total_size + 3)/4)
  28. return vsize
  29. class TestNode(NodeConnCB):
  30. def __init__(self):
  31. super().__init__()
  32. self.getdataset = set()
  33. def on_getdata(self, conn, message):
  34. for inv in message.inv:
  35. self.getdataset.add(inv.hash)
  36. def announce_tx_and_wait_for_getdata(self, tx, timeout=60):
  37. with mininode_lock:
  38. self.last_message.pop("getdata", None)
  39. self.send_message(msg_inv(inv=[CInv(1, tx.sha256)]))
  40. self.wait_for_getdata(timeout)
  41. def announce_block_and_wait_for_getdata(self, block, use_header, timeout=60):
  42. with mininode_lock:
  43. self.last_message.pop("getdata", None)
  44. self.last_message.pop("getheaders", None)
  45. msg = msg_headers()
  46. msg.headers = [ CBlockHeader(block) ]
  47. if use_header:
  48. self.send_message(msg)
  49. else:
  50. self.send_message(msg_inv(inv=[CInv(2, block.sha256)]))
  51. self.wait_for_getheaders()
  52. self.send_message(msg)
  53. self.wait_for_getdata()
  54. def request_block(self, blockhash, inv_type, timeout=60):
  55. with mininode_lock:
  56. self.last_message.pop("block", None)
  57. self.send_message(msg_getdata(inv=[CInv(inv_type, blockhash)]))
  58. self.wait_for_block(blockhash, timeout)
  59. return self.last_message["block"].block
  60. def test_transaction_acceptance(self, tx, with_witness, accepted, reason=None):
  61. tx_message = msg_tx(tx)
  62. if with_witness:
  63. tx_message = msg_witness_tx(tx)
  64. self.send_message(tx_message)
  65. self.sync_with_ping()
  66. assert_equal(tx.hash in self.connection.rpc.getrawmempool(), accepted)
  67. if (reason != None and not accepted):
  68. # Check the rejection reason as well.
  69. with mininode_lock:
  70. assert_equal(self.last_message["reject"].reason, reason)
  71. # Test whether a witness block had the correct effect on the tip
  72. def test_witness_block(self, block, accepted, with_witness=True):
  73. if with_witness:
  74. self.send_message(msg_witness_block(block))
  75. else:
  76. self.send_message(msg_block(block))
  77. self.sync_with_ping()
  78. assert_equal(self.connection.rpc.getbestblockhash() == block.hash, accepted)
  79. # Used to keep track of anyone-can-spend outputs that we can use in the tests
  80. class UTXO(object):
  81. def __init__(self, sha256, n, nValue):
  82. self.sha256 = sha256
  83. self.n = n
  84. self.nValue = nValue
  85. # Helper for getting the script associated with a P2PKH
  86. def GetP2PKHScript(pubkeyhash):
  87. return CScript([CScriptOp(OP_DUP), CScriptOp(OP_HASH160), pubkeyhash, CScriptOp(OP_EQUALVERIFY), CScriptOp(OP_CHECKSIG)])
  88. # Add signature for a P2PK witness program.
  89. def sign_P2PK_witness_input(script, txTo, inIdx, hashtype, value, key):
  90. tx_hash = SegwitVersion1SignatureHash(script, txTo, inIdx, hashtype, value)
  91. signature = key.sign(tx_hash) + chr(hashtype).encode('latin-1')
  92. txTo.wit.vtxinwit[inIdx].scriptWitness.stack = [signature, script]
  93. txTo.rehash()
  94. class SegWitTest(BitcoinTestFramework):
  95. def set_test_params(self):
  96. self.setup_clean_chain = True
  97. self.num_nodes = 3
  98. self.extra_args = [["-whitelist=127.0.0.1"], ["-whitelist=127.0.0.1", "-acceptnonstdtxn=0"], ["-whitelist=127.0.0.1", "-vbparams=segwit:0:0"]]
  99. def setup_network(self):
  100. self.setup_nodes()
  101. connect_nodes(self.nodes[0], 1)
  102. connect_nodes(self.nodes[0], 2)
  103. self.sync_all()
  104. ''' Helpers '''
  105. # Build a block on top of node0's tip.
  106. def build_next_block(self, nVersion=4):
  107. tip = self.nodes[0].getbestblockhash()
  108. height = self.nodes[0].getblockcount() + 1
  109. block_time = self.nodes[0].getblockheader(tip)["mediantime"] + 1
  110. block = create_block(int(tip, 16), create_coinbase(height), block_time)
  111. block.nVersion = nVersion
  112. block.rehash()
  113. return block
  114. # Adds list of transactions to block, adds witness commitment, then solves.
  115. def update_witness_block_with_transactions(self, block, tx_list, nonce=0):
  116. block.vtx.extend(tx_list)
  117. add_witness_commitment(block, nonce)
  118. block.solve()
  119. return
  120. ''' Individual tests '''
  121. def test_witness_services(self):
  122. self.log.info("Verifying NODE_WITNESS service bit")
  123. assert((self.test_node.connection.nServices & NODE_WITNESS) != 0)
  124. # See if sending a regular transaction works, and create a utxo
  125. # to use in later tests.
  126. def test_non_witness_transaction(self):
  127. # Mine a block with an anyone-can-spend coinbase,
  128. # let it mature, then try to spend it.
  129. self.log.info("Testing non-witness transaction")
  130. block = self.build_next_block(nVersion=1)
  131. block.solve()
  132. self.test_node.send_message(msg_block(block))
  133. self.test_node.sync_with_ping() # make sure the block was processed
  134. txid = block.vtx[0].sha256
  135. self.nodes[0].generate(99) # let the block mature
  136. # Create a transaction that spends the coinbase
  137. tx = CTransaction()
  138. tx.vin.append(CTxIn(COutPoint(txid, 0), b""))
  139. tx.vout.append(CTxOut(49*100000000, CScript([OP_TRUE])))
  140. tx.calc_sha256()
  141. # Check that serializing it with or without witness is the same
  142. # This is a sanity check of our testing framework.
  143. assert_equal(msg_tx(tx).serialize(), msg_witness_tx(tx).serialize())
  144. self.test_node.send_message(msg_witness_tx(tx))
  145. self.test_node.sync_with_ping() # make sure the tx was processed
  146. assert(tx.hash in self.nodes[0].getrawmempool())
  147. # Save this transaction for later
  148. self.utxo.append(UTXO(tx.sha256, 0, 49*100000000))
  149. self.nodes[0].generate(1)
  150. # Verify that blocks with witnesses are rejected before activation.
  151. def test_unnecessary_witness_before_segwit_activation(self):
  152. self.log.info("Testing behavior of unnecessary witnesses")
  153. # For now, rely on earlier tests to have created at least one utxo for
  154. # us to use
  155. assert(len(self.utxo) > 0)
  156. assert(get_bip9_status(self.nodes[0], 'segwit')['status'] != 'active')
  157. tx = CTransaction()
  158. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  159. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, CScript([OP_TRUE])))
  160. tx.wit.vtxinwit.append(CTxInWitness())
  161. tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)])]
  162. # Verify the hash with witness differs from the txid
  163. # (otherwise our testing framework must be broken!)
  164. tx.rehash()
  165. assert(tx.sha256 != tx.calc_sha256(with_witness=True))
  166. # Construct a segwit-signaling block that includes the transaction.
  167. block = self.build_next_block(nVersion=(VB_TOP_BITS|(1 << VB_WITNESS_BIT)))
  168. self.update_witness_block_with_transactions(block, [tx])
  169. # Sending witness data before activation is not allowed (anti-spam
  170. # rule).
  171. self.test_node.test_witness_block(block, accepted=False)
  172. # TODO: fix synchronization so we can test reject reason
  173. # Right now, bitcoind delays sending reject messages for blocks
  174. # until the future, making synchronization here difficult.
  175. #assert_equal(self.test_node.last_message["reject"].reason, "unexpected-witness")
  176. # But it should not be permanently marked bad...
  177. # Resend without witness information.
  178. self.test_node.send_message(msg_block(block))
  179. self.test_node.sync_with_ping()
  180. assert_equal(self.nodes[0].getbestblockhash(), block.hash)
  181. sync_blocks(self.nodes)
  182. # Create a p2sh output -- this is so we can pass the standardness
  183. # rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped
  184. # in P2SH).
  185. p2sh_program = CScript([OP_TRUE])
  186. p2sh_pubkey = hash160(p2sh_program)
  187. scriptPubKey = CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])
  188. # Now check that unnecessary witnesses can't be used to blind a node
  189. # to a transaction, eg by violating standardness checks.
  190. tx2 = CTransaction()
  191. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  192. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, scriptPubKey))
  193. tx2.rehash()
  194. self.test_node.test_transaction_acceptance(tx2, False, True)
  195. self.nodes[0].generate(1)
  196. sync_blocks(self.nodes)
  197. # We'll add an unnecessary witness to this transaction that would cause
  198. # it to be non-standard, to test that violating policy with a witness before
  199. # segwit activation doesn't blind a node to a transaction. Transactions
  200. # rejected for having a witness before segwit activation shouldn't be added
  201. # to the rejection cache.
  202. tx3 = CTransaction()
  203. tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), CScript([p2sh_program])))
  204. tx3.vout.append(CTxOut(tx2.vout[0].nValue-1000, scriptPubKey))
  205. tx3.wit.vtxinwit.append(CTxInWitness())
  206. tx3.wit.vtxinwit[0].scriptWitness.stack = [b'a'*400000]
  207. tx3.rehash()
  208. # Note that this should be rejected for the premature witness reason,
  209. # rather than a policy check, since segwit hasn't activated yet.
  210. self.std_node.test_transaction_acceptance(tx3, True, False, b'no-witness-yet')
  211. # If we send without witness, it should be accepted.
  212. self.std_node.test_transaction_acceptance(tx3, False, True)
  213. # Now create a new anyone-can-spend utxo for the next test.
  214. tx4 = CTransaction()
  215. tx4.vin.append(CTxIn(COutPoint(tx3.sha256, 0), CScript([p2sh_program])))
  216. tx4.vout.append(CTxOut(tx3.vout[0].nValue-1000, CScript([OP_TRUE])))
  217. tx4.rehash()
  218. self.test_node.test_transaction_acceptance(tx3, False, True)
  219. self.test_node.test_transaction_acceptance(tx4, False, True)
  220. self.nodes[0].generate(1)
  221. sync_blocks(self.nodes)
  222. # Update our utxo list; we spent the first entry.
  223. self.utxo.pop(0)
  224. self.utxo.append(UTXO(tx4.sha256, 0, tx4.vout[0].nValue))
  225. # Mine enough blocks for segwit's vb state to be 'started'.
  226. def advance_to_segwit_started(self):
  227. height = self.nodes[0].getblockcount()
  228. # Will need to rewrite the tests here if we are past the first period
  229. assert(height < VB_PERIOD - 1)
  230. # Genesis block is 'defined'.
  231. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'defined')
  232. # Advance to end of period, status should now be 'started'
  233. self.nodes[0].generate(VB_PERIOD-height-1)
  234. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'started')
  235. # Mine enough blocks to lock in segwit, but don't activate.
  236. # TODO: we could verify that lockin only happens at the right threshold of
  237. # signalling blocks, rather than just at the right period boundary.
  238. def advance_to_segwit_lockin(self):
  239. height = self.nodes[0].getblockcount()
  240. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'started')
  241. # Advance to end of period, and verify lock-in happens at the end
  242. self.nodes[0].generate(VB_PERIOD-1)
  243. height = self.nodes[0].getblockcount()
  244. assert((height % VB_PERIOD) == VB_PERIOD - 2)
  245. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'started')
  246. self.nodes[0].generate(1)
  247. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'locked_in')
  248. # Mine enough blocks to activate segwit.
  249. # TODO: we could verify that activation only happens at the right threshold
  250. # of signalling blocks, rather than just at the right period boundary.
  251. def advance_to_segwit_active(self):
  252. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'locked_in')
  253. height = self.nodes[0].getblockcount()
  254. self.nodes[0].generate(VB_PERIOD - (height%VB_PERIOD) - 2)
  255. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'locked_in')
  256. self.nodes[0].generate(1)
  257. assert_equal(get_bip9_status(self.nodes[0], 'segwit')['status'], 'active')
  258. # This test can only be run after segwit has activated
  259. def test_witness_commitments(self):
  260. self.log.info("Testing witness commitments")
  261. # First try a correct witness commitment.
  262. block = self.build_next_block()
  263. add_witness_commitment(block)
  264. block.solve()
  265. # Test the test -- witness serialization should be different
  266. assert(msg_witness_block(block).serialize() != msg_block(block).serialize())
  267. # This empty block should be valid.
  268. self.test_node.test_witness_block(block, accepted=True)
  269. # Try to tweak the nonce
  270. block_2 = self.build_next_block()
  271. add_witness_commitment(block_2, nonce=28)
  272. block_2.solve()
  273. # The commitment should have changed!
  274. assert(block_2.vtx[0].vout[-1] != block.vtx[0].vout[-1])
  275. # This should also be valid.
  276. self.test_node.test_witness_block(block_2, accepted=True)
  277. # Now test commitments with actual transactions
  278. assert (len(self.utxo) > 0)
  279. tx = CTransaction()
  280. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  281. # Let's construct a witness program
  282. witness_program = CScript([OP_TRUE])
  283. witness_hash = sha256(witness_program)
  284. scriptPubKey = CScript([OP_0, witness_hash])
  285. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, scriptPubKey))
  286. tx.rehash()
  287. # tx2 will spend tx1, and send back to a regular anyone-can-spend address
  288. tx2 = CTransaction()
  289. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  290. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, witness_program))
  291. tx2.wit.vtxinwit.append(CTxInWitness())
  292. tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
  293. tx2.rehash()
  294. block_3 = self.build_next_block()
  295. self.update_witness_block_with_transactions(block_3, [tx, tx2], nonce=1)
  296. # Add an extra OP_RETURN output that matches the witness commitment template,
  297. # even though it has extra data after the incorrect commitment.
  298. # This block should fail.
  299. block_3.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, WITNESS_COMMITMENT_HEADER + ser_uint256(2), 10])))
  300. block_3.vtx[0].rehash()
  301. block_3.hashMerkleRoot = block_3.calc_merkle_root()
  302. block_3.rehash()
  303. block_3.solve()
  304. self.test_node.test_witness_block(block_3, accepted=False)
  305. # Add a different commitment with different nonce, but in the
  306. # right location, and with some funds burned(!).
  307. # This should succeed (nValue shouldn't affect finding the
  308. # witness commitment).
  309. add_witness_commitment(block_3, nonce=0)
  310. block_3.vtx[0].vout[0].nValue -= 1
  311. block_3.vtx[0].vout[-1].nValue += 1
  312. block_3.vtx[0].rehash()
  313. block_3.hashMerkleRoot = block_3.calc_merkle_root()
  314. block_3.rehash()
  315. assert(len(block_3.vtx[0].vout) == 4) # 3 OP_returns
  316. block_3.solve()
  317. self.test_node.test_witness_block(block_3, accepted=True)
  318. # Finally test that a block with no witness transactions can
  319. # omit the commitment.
  320. block_4 = self.build_next_block()
  321. tx3 = CTransaction()
  322. tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), b""))
  323. tx3.vout.append(CTxOut(tx.vout[0].nValue-1000, witness_program))
  324. tx3.rehash()
  325. block_4.vtx.append(tx3)
  326. block_4.hashMerkleRoot = block_4.calc_merkle_root()
  327. block_4.solve()
  328. self.test_node.test_witness_block(block_4, with_witness=False, accepted=True)
  329. # Update available utxo's for use in later test.
  330. self.utxo.pop(0)
  331. self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
  332. def test_block_malleability(self):
  333. self.log.info("Testing witness block malleability")
  334. # Make sure that a block that has too big a virtual size
  335. # because of a too-large coinbase witness is not permanently
  336. # marked bad.
  337. block = self.build_next_block()
  338. add_witness_commitment(block)
  339. block.solve()
  340. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.append(b'a'*5000000)
  341. assert(get_virtual_size(block) > MAX_BLOCK_BASE_SIZE)
  342. # We can't send over the p2p network, because this is too big to relay
  343. # TODO: repeat this test with a block that can be relayed
  344. self.nodes[0].submitblock(bytes_to_hex_str(block.serialize(True)))
  345. assert(self.nodes[0].getbestblockhash() != block.hash)
  346. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.pop()
  347. assert(get_virtual_size(block) < MAX_BLOCK_BASE_SIZE)
  348. self.nodes[0].submitblock(bytes_to_hex_str(block.serialize(True)))
  349. assert(self.nodes[0].getbestblockhash() == block.hash)
  350. # Now make sure that malleating the witness nonce doesn't
  351. # result in a block permanently marked bad.
  352. block = self.build_next_block()
  353. add_witness_commitment(block)
  354. block.solve()
  355. # Change the nonce -- should not cause the block to be permanently
  356. # failed
  357. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ ser_uint256(1) ]
  358. self.test_node.test_witness_block(block, accepted=False)
  359. # Changing the witness nonce doesn't change the block hash
  360. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ ser_uint256(0) ]
  361. self.test_node.test_witness_block(block, accepted=True)
  362. def test_witness_block_size(self):
  363. self.log.info("Testing witness block size limit")
  364. # TODO: Test that non-witness carrying blocks can't exceed 1MB
  365. # Skipping this test for now; this is covered in p2p-fullblocktest.py
  366. # Test that witness-bearing blocks are limited at ceil(base + wit/4) <= 1MB.
  367. block = self.build_next_block()
  368. assert(len(self.utxo) > 0)
  369. # Create a P2WSH transaction.
  370. # The witness program will be a bunch of OP_2DROP's, followed by OP_TRUE.
  371. # This should give us plenty of room to tweak the spending tx's
  372. # virtual size.
  373. NUM_DROPS = 200 # 201 max ops per script!
  374. NUM_OUTPUTS = 50
  375. witness_program = CScript([OP_2DROP]*NUM_DROPS + [OP_TRUE])
  376. witness_hash = uint256_from_str(sha256(witness_program))
  377. scriptPubKey = CScript([OP_0, ser_uint256(witness_hash)])
  378. prevout = COutPoint(self.utxo[0].sha256, self.utxo[0].n)
  379. value = self.utxo[0].nValue
  380. parent_tx = CTransaction()
  381. parent_tx.vin.append(CTxIn(prevout, b""))
  382. child_value = int(value/NUM_OUTPUTS)
  383. for i in range(NUM_OUTPUTS):
  384. parent_tx.vout.append(CTxOut(child_value, scriptPubKey))
  385. parent_tx.vout[0].nValue -= 50000
  386. assert(parent_tx.vout[0].nValue > 0)
  387. parent_tx.rehash()
  388. child_tx = CTransaction()
  389. for i in range(NUM_OUTPUTS):
  390. child_tx.vin.append(CTxIn(COutPoint(parent_tx.sha256, i), b""))
  391. child_tx.vout = [CTxOut(value - 100000, CScript([OP_TRUE]))]
  392. for i in range(NUM_OUTPUTS):
  393. child_tx.wit.vtxinwit.append(CTxInWitness())
  394. child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a'*195]*(2*NUM_DROPS) + [witness_program]
  395. child_tx.rehash()
  396. self.update_witness_block_with_transactions(block, [parent_tx, child_tx])
  397. vsize = get_virtual_size(block)
  398. additional_bytes = (MAX_BLOCK_BASE_SIZE - vsize)*4
  399. i = 0
  400. while additional_bytes > 0:
  401. # Add some more bytes to each input until we hit MAX_BLOCK_BASE_SIZE+1
  402. extra_bytes = min(additional_bytes+1, 55)
  403. block.vtx[-1].wit.vtxinwit[int(i/(2*NUM_DROPS))].scriptWitness.stack[i%(2*NUM_DROPS)] = b'a'*(195+extra_bytes)
  404. additional_bytes -= extra_bytes
  405. i += 1
  406. block.vtx[0].vout.pop() # Remove old commitment
  407. add_witness_commitment(block)
  408. block.solve()
  409. vsize = get_virtual_size(block)
  410. assert_equal(vsize, MAX_BLOCK_BASE_SIZE + 1)
  411. # Make sure that our test case would exceed the old max-network-message
  412. # limit
  413. assert(len(block.serialize(True)) > 2*1024*1024)
  414. self.test_node.test_witness_block(block, accepted=False)
  415. # Now resize the second transaction to make the block fit.
  416. cur_length = len(block.vtx[-1].wit.vtxinwit[0].scriptWitness.stack[0])
  417. block.vtx[-1].wit.vtxinwit[0].scriptWitness.stack[0] = b'a'*(cur_length-1)
  418. block.vtx[0].vout.pop()
  419. add_witness_commitment(block)
  420. block.solve()
  421. assert(get_virtual_size(block) == MAX_BLOCK_BASE_SIZE)
  422. self.test_node.test_witness_block(block, accepted=True)
  423. # Update available utxo's
  424. self.utxo.pop(0)
  425. self.utxo.append(UTXO(block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue))
  426. # submitblock will try to add the nonce automatically, so that mining
  427. # software doesn't need to worry about doing so itself.
  428. def test_submit_block(self):
  429. block = self.build_next_block()
  430. # Try using a custom nonce and then don't supply it.
  431. # This shouldn't possibly work.
  432. add_witness_commitment(block, nonce=1)
  433. block.vtx[0].wit = CTxWitness() # drop the nonce
  434. block.solve()
  435. self.nodes[0].submitblock(bytes_to_hex_str(block.serialize(True)))
  436. assert(self.nodes[0].getbestblockhash() != block.hash)
  437. # Now redo commitment with the standard nonce, but let bitcoind fill it in.
  438. add_witness_commitment(block, nonce=0)
  439. block.vtx[0].wit = CTxWitness()
  440. block.solve()
  441. self.nodes[0].submitblock(bytes_to_hex_str(block.serialize(True)))
  442. assert_equal(self.nodes[0].getbestblockhash(), block.hash)
  443. # This time, add a tx with non-empty witness, but don't supply
  444. # the commitment.
  445. block_2 = self.build_next_block()
  446. add_witness_commitment(block_2)
  447. block_2.solve()
  448. # Drop commitment and nonce -- submitblock should not fill in.
  449. block_2.vtx[0].vout.pop()
  450. block_2.vtx[0].wit = CTxWitness()
  451. self.nodes[0].submitblock(bytes_to_hex_str(block_2.serialize(True)))
  452. # Tip should not advance!
  453. assert(self.nodes[0].getbestblockhash() != block_2.hash)
  454. # Consensus tests of extra witness data in a transaction.
  455. def test_extra_witness_data(self):
  456. self.log.info("Testing extra witness data in tx")
  457. assert(len(self.utxo) > 0)
  458. block = self.build_next_block()
  459. witness_program = CScript([OP_DROP, OP_TRUE])
  460. witness_hash = sha256(witness_program)
  461. scriptPubKey = CScript([OP_0, witness_hash])
  462. # First try extra witness data on a tx that doesn't require a witness
  463. tx = CTransaction()
  464. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  465. tx.vout.append(CTxOut(self.utxo[0].nValue-2000, scriptPubKey))
  466. tx.vout.append(CTxOut(1000, CScript([OP_TRUE]))) # non-witness output
  467. tx.wit.vtxinwit.append(CTxInWitness())
  468. tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([])]
  469. tx.rehash()
  470. self.update_witness_block_with_transactions(block, [tx])
  471. # Extra witness data should not be allowed.
  472. self.test_node.test_witness_block(block, accepted=False)
  473. # Try extra signature data. Ok if we're not spending a witness output.
  474. block.vtx[1].wit.vtxinwit = []
  475. block.vtx[1].vin[0].scriptSig = CScript([OP_0])
  476. block.vtx[1].rehash()
  477. add_witness_commitment(block)
  478. block.solve()
  479. self.test_node.test_witness_block(block, accepted=True)
  480. # Now try extra witness/signature data on an input that DOES require a
  481. # witness
  482. tx2 = CTransaction()
  483. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b"")) # witness output
  484. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 1), b"")) # non-witness
  485. tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
  486. tx2.wit.vtxinwit.extend([CTxInWitness(), CTxInWitness()])
  487. tx2.wit.vtxinwit[0].scriptWitness.stack = [ CScript([CScriptNum(1)]), CScript([CScriptNum(1)]), witness_program ]
  488. tx2.wit.vtxinwit[1].scriptWitness.stack = [ CScript([OP_TRUE]) ]
  489. block = self.build_next_block()
  490. self.update_witness_block_with_transactions(block, [tx2])
  491. # This has extra witness data, so it should fail.
  492. self.test_node.test_witness_block(block, accepted=False)
  493. # Now get rid of the extra witness, but add extra scriptSig data
  494. tx2.vin[0].scriptSig = CScript([OP_TRUE])
  495. tx2.vin[1].scriptSig = CScript([OP_TRUE])
  496. tx2.wit.vtxinwit[0].scriptWitness.stack.pop(0)
  497. tx2.wit.vtxinwit[1].scriptWitness.stack = []
  498. tx2.rehash()
  499. add_witness_commitment(block)
  500. block.solve()
  501. # This has extra signature data for a witness input, so it should fail.
  502. self.test_node.test_witness_block(block, accepted=False)
  503. # Now get rid of the extra scriptsig on the witness input, and verify
  504. # success (even with extra scriptsig data in the non-witness input)
  505. tx2.vin[0].scriptSig = b""
  506. tx2.rehash()
  507. add_witness_commitment(block)
  508. block.solve()
  509. self.test_node.test_witness_block(block, accepted=True)
  510. # Update utxo for later tests
  511. self.utxo.pop(0)
  512. self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
  513. def test_max_witness_push_length(self):
  514. ''' Should only allow up to 520 byte pushes in witness stack '''
  515. self.log.info("Testing maximum witness push size")
  516. MAX_SCRIPT_ELEMENT_SIZE = 520
  517. assert(len(self.utxo))
  518. block = self.build_next_block()
  519. witness_program = CScript([OP_DROP, OP_TRUE])
  520. witness_hash = sha256(witness_program)
  521. scriptPubKey = CScript([OP_0, witness_hash])
  522. tx = CTransaction()
  523. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  524. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, scriptPubKey))
  525. tx.rehash()
  526. tx2 = CTransaction()
  527. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  528. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, CScript([OP_TRUE])))
  529. tx2.wit.vtxinwit.append(CTxInWitness())
  530. # First try a 521-byte stack element
  531. tx2.wit.vtxinwit[0].scriptWitness.stack = [ b'a'*(MAX_SCRIPT_ELEMENT_SIZE+1), witness_program ]
  532. tx2.rehash()
  533. self.update_witness_block_with_transactions(block, [tx, tx2])
  534. self.test_node.test_witness_block(block, accepted=False)
  535. # Now reduce the length of the stack element
  536. tx2.wit.vtxinwit[0].scriptWitness.stack[0] = b'a'*(MAX_SCRIPT_ELEMENT_SIZE)
  537. add_witness_commitment(block)
  538. block.solve()
  539. self.test_node.test_witness_block(block, accepted=True)
  540. # Update the utxo for later tests
  541. self.utxo.pop()
  542. self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
  543. def test_max_witness_program_length(self):
  544. # Can create witness outputs that are long, but can't be greater than
  545. # 10k bytes to successfully spend
  546. self.log.info("Testing maximum witness program length")
  547. assert(len(self.utxo))
  548. MAX_PROGRAM_LENGTH = 10000
  549. # This program is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
  550. long_witness_program = CScript([b'a'*520]*19 + [OP_DROP]*63 + [OP_TRUE])
  551. assert(len(long_witness_program) == MAX_PROGRAM_LENGTH+1)
  552. long_witness_hash = sha256(long_witness_program)
  553. long_scriptPubKey = CScript([OP_0, long_witness_hash])
  554. block = self.build_next_block()
  555. tx = CTransaction()
  556. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  557. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, long_scriptPubKey))
  558. tx.rehash()
  559. tx2 = CTransaction()
  560. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  561. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, CScript([OP_TRUE])))
  562. tx2.wit.vtxinwit.append(CTxInWitness())
  563. tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a']*44 + [long_witness_program]
  564. tx2.rehash()
  565. self.update_witness_block_with_transactions(block, [tx, tx2])
  566. self.test_node.test_witness_block(block, accepted=False)
  567. # Try again with one less byte in the witness program
  568. witness_program = CScript([b'a'*520]*19 + [OP_DROP]*62 + [OP_TRUE])
  569. assert(len(witness_program) == MAX_PROGRAM_LENGTH)
  570. witness_hash = sha256(witness_program)
  571. scriptPubKey = CScript([OP_0, witness_hash])
  572. tx.vout[0] = CTxOut(tx.vout[0].nValue, scriptPubKey)
  573. tx.rehash()
  574. tx2.vin[0].prevout.hash = tx.sha256
  575. tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a']*43 + [witness_program]
  576. tx2.rehash()
  577. block.vtx = [block.vtx[0]]
  578. self.update_witness_block_with_transactions(block, [tx, tx2])
  579. self.test_node.test_witness_block(block, accepted=True)
  580. self.utxo.pop()
  581. self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
  582. def test_witness_input_length(self):
  583. ''' Ensure that vin length must match vtxinwit length '''
  584. self.log.info("Testing witness input length")
  585. assert(len(self.utxo))
  586. witness_program = CScript([OP_DROP, OP_TRUE])
  587. witness_hash = sha256(witness_program)
  588. scriptPubKey = CScript([OP_0, witness_hash])
  589. # Create a transaction that splits our utxo into many outputs
  590. tx = CTransaction()
  591. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  592. nValue = self.utxo[0].nValue
  593. for i in range(10):
  594. tx.vout.append(CTxOut(int(nValue/10), scriptPubKey))
  595. tx.vout[0].nValue -= 1000
  596. assert(tx.vout[0].nValue >= 0)
  597. block = self.build_next_block()
  598. self.update_witness_block_with_transactions(block, [tx])
  599. self.test_node.test_witness_block(block, accepted=True)
  600. # Try various ways to spend tx that should all break.
  601. # This "broken" transaction serializer will not normalize
  602. # the length of vtxinwit.
  603. class BrokenCTransaction(CTransaction):
  604. def serialize_with_witness(self):
  605. flags = 0
  606. if not self.wit.is_null():
  607. flags |= 1
  608. r = b""
  609. r += struct.pack("<i", self.nVersion)
  610. if flags:
  611. dummy = []
  612. r += ser_vector(dummy)
  613. r += struct.pack("<B", flags)
  614. r += ser_vector(self.vin)
  615. r += ser_vector(self.vout)
  616. if flags & 1:
  617. r += self.wit.serialize()
  618. r += struct.pack("<I", self.nLockTime)
  619. return r
  620. tx2 = BrokenCTransaction()
  621. for i in range(10):
  622. tx2.vin.append(CTxIn(COutPoint(tx.sha256, i), b""))
  623. tx2.vout.append(CTxOut(nValue-3000, CScript([OP_TRUE])))
  624. # First try using a too long vtxinwit
  625. for i in range(11):
  626. tx2.wit.vtxinwit.append(CTxInWitness())
  627. tx2.wit.vtxinwit[i].scriptWitness.stack = [b'a', witness_program]
  628. block = self.build_next_block()
  629. self.update_witness_block_with_transactions(block, [tx2])
  630. self.test_node.test_witness_block(block, accepted=False)
  631. # Now try using a too short vtxinwit
  632. tx2.wit.vtxinwit.pop()
  633. tx2.wit.vtxinwit.pop()
  634. block.vtx = [block.vtx[0]]
  635. self.update_witness_block_with_transactions(block, [tx2])
  636. self.test_node.test_witness_block(block, accepted=False)
  637. # Now make one of the intermediate witnesses be incorrect
  638. tx2.wit.vtxinwit.append(CTxInWitness())
  639. tx2.wit.vtxinwit[-1].scriptWitness.stack = [b'a', witness_program]
  640. tx2.wit.vtxinwit[5].scriptWitness.stack = [ witness_program ]
  641. block.vtx = [block.vtx[0]]
  642. self.update_witness_block_with_transactions(block, [tx2])
  643. self.test_node.test_witness_block(block, accepted=False)
  644. # Fix the broken witness and the block should be accepted.
  645. tx2.wit.vtxinwit[5].scriptWitness.stack = [b'a', witness_program]
  646. block.vtx = [block.vtx[0]]
  647. self.update_witness_block_with_transactions(block, [tx2])
  648. self.test_node.test_witness_block(block, accepted=True)
  649. self.utxo.pop()
  650. self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
  651. def test_witness_tx_relay_before_segwit_activation(self):
  652. self.log.info("Testing relay of witness transactions")
  653. # Generate a transaction that doesn't require a witness, but send it
  654. # with a witness. Should be rejected for premature-witness, but should
  655. # not be added to recently rejected list.
  656. assert(len(self.utxo))
  657. tx = CTransaction()
  658. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  659. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, CScript([OP_TRUE])))
  660. tx.wit.vtxinwit.append(CTxInWitness())
  661. tx.wit.vtxinwit[0].scriptWitness.stack = [ b'a' ]
  662. tx.rehash()
  663. tx_hash = tx.sha256
  664. tx_value = tx.vout[0].nValue
  665. # Verify that if a peer doesn't set nServices to include NODE_WITNESS,
  666. # the getdata is just for the non-witness portion.
  667. self.old_node.announce_tx_and_wait_for_getdata(tx)
  668. assert(self.old_node.last_message["getdata"].inv[0].type == 1)
  669. # Since we haven't delivered the tx yet, inv'ing the same tx from
  670. # a witness transaction ought not result in a getdata.
  671. try:
  672. self.test_node.announce_tx_and_wait_for_getdata(tx, timeout=2)
  673. self.log.error("Error: duplicate tx getdata!")
  674. assert(False)
  675. except AssertionError as e:
  676. pass
  677. # Delivering this transaction with witness should fail (no matter who
  678. # its from)
  679. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  680. assert_equal(len(self.nodes[1].getrawmempool()), 0)
  681. self.old_node.test_transaction_acceptance(tx, with_witness=True, accepted=False)
  682. self.test_node.test_transaction_acceptance(tx, with_witness=True, accepted=False)
  683. # But eliminating the witness should fix it
  684. self.test_node.test_transaction_acceptance(tx, with_witness=False, accepted=True)
  685. # Cleanup: mine the first transaction and update utxo
  686. self.nodes[0].generate(1)
  687. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  688. self.utxo.pop(0)
  689. self.utxo.append(UTXO(tx_hash, 0, tx_value))
  690. # After segwit activates, verify that mempool:
  691. # - rejects transactions with unnecessary/extra witnesses
  692. # - accepts transactions with valid witnesses
  693. # and that witness transactions are relayed to non-upgraded peers.
  694. def test_tx_relay_after_segwit_activation(self):
  695. self.log.info("Testing relay of witness transactions")
  696. # Generate a transaction that doesn't require a witness, but send it
  697. # with a witness. Should be rejected because we can't use a witness
  698. # when spending a non-witness output.
  699. assert(len(self.utxo))
  700. tx = CTransaction()
  701. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  702. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, CScript([OP_TRUE])))
  703. tx.wit.vtxinwit.append(CTxInWitness())
  704. tx.wit.vtxinwit[0].scriptWitness.stack = [ b'a' ]
  705. tx.rehash()
  706. tx_hash = tx.sha256
  707. # Verify that unnecessary witnesses are rejected.
  708. self.test_node.announce_tx_and_wait_for_getdata(tx)
  709. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  710. self.test_node.test_transaction_acceptance(tx, with_witness=True, accepted=False)
  711. # Verify that removing the witness succeeds.
  712. self.test_node.announce_tx_and_wait_for_getdata(tx)
  713. self.test_node.test_transaction_acceptance(tx, with_witness=False, accepted=True)
  714. # Now try to add extra witness data to a valid witness tx.
  715. witness_program = CScript([OP_TRUE])
  716. witness_hash = sha256(witness_program)
  717. scriptPubKey = CScript([OP_0, witness_hash])
  718. tx2 = CTransaction()
  719. tx2.vin.append(CTxIn(COutPoint(tx_hash, 0), b""))
  720. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, scriptPubKey))
  721. tx2.rehash()
  722. tx3 = CTransaction()
  723. tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), b""))
  724. tx3.wit.vtxinwit.append(CTxInWitness())
  725. # Add too-large for IsStandard witness and check that it does not enter reject filter
  726. p2sh_program = CScript([OP_TRUE])
  727. p2sh_pubkey = hash160(p2sh_program)
  728. witness_program2 = CScript([b'a'*400000])
  729. tx3.vout.append(CTxOut(tx2.vout[0].nValue-1000, CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])))
  730. tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program2]
  731. tx3.rehash()
  732. # Node will not be blinded to the transaction
  733. self.std_node.announce_tx_and_wait_for_getdata(tx3)
  734. self.std_node.test_transaction_acceptance(tx3, True, False, b'tx-size')
  735. self.std_node.announce_tx_and_wait_for_getdata(tx3)
  736. self.std_node.test_transaction_acceptance(tx3, True, False, b'tx-size')
  737. # Remove witness stuffing, instead add extra witness push on stack
  738. tx3.vout[0] = CTxOut(tx2.vout[0].nValue-1000, CScript([OP_TRUE]))
  739. tx3.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), witness_program ]
  740. tx3.rehash()
  741. self.test_node.test_transaction_acceptance(tx2, with_witness=True, accepted=True)
  742. self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=False)
  743. # Get rid of the extra witness, and verify acceptance.
  744. tx3.wit.vtxinwit[0].scriptWitness.stack = [ witness_program ]
  745. # Also check that old_node gets a tx announcement, even though this is
  746. # a witness transaction.
  747. self.old_node.wait_for_inv([CInv(1, tx2.sha256)]) # wait until tx2 was inv'ed
  748. self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=True)
  749. self.old_node.wait_for_inv([CInv(1, tx3.sha256)])
  750. # Test that getrawtransaction returns correct witness information
  751. # hash, size, vsize
  752. raw_tx = self.nodes[0].getrawtransaction(tx3.hash, 1)
  753. assert_equal(int(raw_tx["hash"], 16), tx3.calc_sha256(True))
  754. assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
  755. vsize = (len(tx3.serialize_with_witness()) + 3*len(tx3.serialize_without_witness()) + 3) / 4
  756. assert_equal(raw_tx["vsize"], vsize)
  757. assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
  758. assert_equal(raw_tx["vin"][0]["txinwitness"][0], hexlify(witness_program).decode('ascii'))
  759. assert(vsize != raw_tx["size"])
  760. # Cleanup: mine the transactions and update utxo for next test
  761. self.nodes[0].generate(1)
  762. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  763. self.utxo.pop(0)
  764. self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
  765. # Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG
  766. # This is true regardless of segwit activation.
  767. # Also test that we don't ask for blocks from unupgraded peers
  768. def test_block_relay(self, segwit_activated):
  769. self.log.info("Testing block relay")
  770. blocktype = 2|MSG_WITNESS_FLAG
  771. # test_node has set NODE_WITNESS, so all getdata requests should be for
  772. # witness blocks.
  773. # Test announcing a block via inv results in a getdata, and that
  774. # announcing a version 4 or random VB block with a header results in a getdata
  775. block1 = self.build_next_block()
  776. block1.solve()
  777. self.test_node.announce_block_and_wait_for_getdata(block1, use_header=False)
  778. assert(self.test_node.last_message["getdata"].inv[0].type == blocktype)
  779. self.test_node.test_witness_block(block1, True)
  780. block2 = self.build_next_block(nVersion=4)
  781. block2.solve()
  782. self.test_node.announce_block_and_wait_for_getdata(block2, use_header=True)
  783. assert(self.test_node.last_message["getdata"].inv[0].type == blocktype)
  784. self.test_node.test_witness_block(block2, True)
  785. block3 = self.build_next_block(nVersion=(VB_TOP_BITS | (1<<15)))
  786. block3.solve()
  787. self.test_node.announce_block_and_wait_for_getdata(block3, use_header=True)
  788. assert(self.test_node.last_message["getdata"].inv[0].type == blocktype)
  789. self.test_node.test_witness_block(block3, True)
  790. # Check that we can getdata for witness blocks or regular blocks,
  791. # and the right thing happens.
  792. if segwit_activated == False:
  793. # Before activation, we should be able to request old blocks with
  794. # or without witness, and they should be the same.
  795. chain_height = self.nodes[0].getblockcount()
  796. # Pick 10 random blocks on main chain, and verify that getdata's
  797. # for MSG_BLOCK, MSG_WITNESS_BLOCK, and rpc getblock() are equal.
  798. all_heights = list(range(chain_height+1))
  799. random.shuffle(all_heights)
  800. all_heights = all_heights[0:10]
  801. for height in all_heights:
  802. block_hash = self.nodes[0].getblockhash(height)
  803. rpc_block = self.nodes[0].getblock(block_hash, False)
  804. block_hash = int(block_hash, 16)
  805. block = self.test_node.request_block(block_hash, 2)
  806. wit_block = self.test_node.request_block(block_hash, 2|MSG_WITNESS_FLAG)
  807. assert_equal(block.serialize(True), wit_block.serialize(True))
  808. assert_equal(block.serialize(), hex_str_to_bytes(rpc_block))
  809. else:
  810. # After activation, witness blocks and non-witness blocks should
  811. # be different. Verify rpc getblock() returns witness blocks, while
  812. # getdata respects the requested type.
  813. block = self.build_next_block()
  814. self.update_witness_block_with_transactions(block, [])
  815. # This gives us a witness commitment.
  816. assert(len(block.vtx[0].wit.vtxinwit) == 1)
  817. assert(len(block.vtx[0].wit.vtxinwit[0].scriptWitness.stack) == 1)
  818. self.test_node.test_witness_block(block, accepted=True)
  819. # Now try to retrieve it...
  820. rpc_block = self.nodes[0].getblock(block.hash, False)
  821. non_wit_block = self.test_node.request_block(block.sha256, 2)
  822. wit_block = self.test_node.request_block(block.sha256, 2|MSG_WITNESS_FLAG)
  823. assert_equal(wit_block.serialize(True), hex_str_to_bytes(rpc_block))
  824. assert_equal(wit_block.serialize(False), non_wit_block.serialize())
  825. assert_equal(wit_block.serialize(True), block.serialize(True))
  826. # Test size, vsize, weight
  827. rpc_details = self.nodes[0].getblock(block.hash, True)
  828. assert_equal(rpc_details["size"], len(block.serialize(True)))
  829. assert_equal(rpc_details["strippedsize"], len(block.serialize(False)))
  830. weight = 3*len(block.serialize(False)) + len(block.serialize(True))
  831. assert_equal(rpc_details["weight"], weight)
  832. # Upgraded node should not ask for blocks from unupgraded
  833. block4 = self.build_next_block(nVersion=4)
  834. block4.solve()
  835. self.old_node.getdataset = set()
  836. # Blocks can be requested via direct-fetch (immediately upon processing the announcement)
  837. # or via parallel download (with an indeterminate delay from processing the announcement)
  838. # so to test that a block is NOT requested, we could guess a time period to sleep for,
  839. # and then check. We can avoid the sleep() by taking advantage of transaction getdata's
  840. # being processed after block getdata's, and announce a transaction as well,
  841. # and then check to see if that particular getdata has been received.
  842. # Since 0.14, inv's will only be responded to with a getheaders, so send a header
  843. # to announce this block.
  844. msg = msg_headers()
  845. msg.headers = [ CBlockHeader(block4) ]
  846. self.old_node.send_message(msg)
  847. self.old_node.announce_tx_and_wait_for_getdata(block4.vtx[0])
  848. assert(block4.sha256 not in self.old_node.getdataset)
  849. # V0 segwit outputs should be standard after activation, but not before.
  850. def test_standardness_v0(self, segwit_activated):
  851. self.log.info("Testing standardness of v0 outputs (%s activation)" % ("after" if segwit_activated else "before"))
  852. assert(len(self.utxo))
  853. witness_program = CScript([OP_TRUE])
  854. witness_hash = sha256(witness_program)
  855. scriptPubKey = CScript([OP_0, witness_hash])
  856. p2sh_pubkey = hash160(witness_program)
  857. p2sh_scriptPubKey = CScript([OP_HASH160, p2sh_pubkey, OP_EQUAL])
  858. # First prepare a p2sh output (so that spending it will pass standardness)
  859. p2sh_tx = CTransaction()
  860. p2sh_tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b"")]
  861. p2sh_tx.vout = [CTxOut(self.utxo[0].nValue-1000, p2sh_scriptPubKey)]
  862. p2sh_tx.rehash()
  863. # Mine it on test_node to create the confirmed output.
  864. self.test_node.test_transaction_acceptance(p2sh_tx, with_witness=True, accepted=True)
  865. self.nodes[0].generate(1)
  866. sync_blocks(self.nodes)
  867. # Now test standardness of v0 P2WSH outputs.
  868. # Start by creating a transaction with two outputs.
  869. tx = CTransaction()
  870. tx.vin = [CTxIn(COutPoint(p2sh_tx.sha256, 0), CScript([witness_program]))]
  871. tx.vout = [CTxOut(p2sh_tx.vout[0].nValue-10000, scriptPubKey)]
  872. tx.vout.append(CTxOut(8000, scriptPubKey)) # Might burn this later
  873. tx.rehash()
  874. self.std_node.test_transaction_acceptance(tx, with_witness=True, accepted=segwit_activated)
  875. # Now create something that looks like a P2PKH output. This won't be spendable.
  876. scriptPubKey = CScript([OP_0, hash160(witness_hash)])
  877. tx2 = CTransaction()
  878. if segwit_activated:
  879. # if tx was accepted, then we spend the second output.
  880. tx2.vin = [CTxIn(COutPoint(tx.sha256, 1), b"")]
  881. tx2.vout = [CTxOut(7000, scriptPubKey)]
  882. tx2.wit.vtxinwit.append(CTxInWitness())
  883. tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
  884. else:
  885. # if tx wasn't accepted, we just re-spend the p2sh output we started with.
  886. tx2.vin = [CTxIn(COutPoint(p2sh_tx.sha256, 0), CScript([witness_program]))]
  887. tx2.vout = [CTxOut(p2sh_tx.vout[0].nValue-1000, scriptPubKey)]
  888. tx2.rehash()
  889. self.std_node.test_transaction_acceptance(tx2, with_witness=True, accepted=segwit_activated)
  890. # Now update self.utxo for later tests.
  891. tx3 = CTransaction()
  892. if segwit_activated:
  893. # tx and tx2 were both accepted. Don't bother trying to reclaim the
  894. # P2PKH output; just send tx's first output back to an anyone-can-spend.
  895. sync_mempools([self.nodes[0], self.nodes[1]])
  896. tx3.vin = [CTxIn(COutPoint(tx.sha256, 0), b"")]
  897. tx3.vout = [CTxOut(tx.vout[0].nValue-1000, CScript([OP_TRUE]))]
  898. tx3.wit.vtxinwit.append(CTxInWitness())
  899. tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_program]
  900. tx3.rehash()
  901. self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=True)
  902. else:
  903. # tx and tx2 didn't go anywhere; just clean up the p2sh_tx output.
  904. tx3.vin = [CTxIn(COutPoint(p2sh_tx.sha256, 0), CScript([witness_program]))]
  905. tx3.vout = [CTxOut(p2sh_tx.vout[0].nValue-1000, witness_program)]
  906. tx3.rehash()
  907. self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=True)
  908. self.nodes[0].generate(1)
  909. sync_blocks(self.nodes)
  910. self.utxo.pop(0)
  911. self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
  912. assert_equal(len(self.nodes[1].getrawmempool()), 0)
  913. # Verify that future segwit upgraded transactions are non-standard,
  914. # but valid in blocks. Can run this before and after segwit activation.
  915. def test_segwit_versions(self):
  916. self.log.info("Testing standardness/consensus for segwit versions (0-16)")
  917. assert(len(self.utxo))
  918. NUM_TESTS = 17 # will test OP_0, OP1, ..., OP_16
  919. if (len(self.utxo) < NUM_TESTS):
  920. tx = CTransaction()
  921. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  922. split_value = (self.utxo[0].nValue - 4000) // NUM_TESTS
  923. for i in range(NUM_TESTS):
  924. tx.vout.append(CTxOut(split_value, CScript([OP_TRUE])))
  925. tx.rehash()
  926. block = self.build_next_block()
  927. self.update_witness_block_with_transactions(block, [tx])
  928. self.test_node.test_witness_block(block, accepted=True)
  929. self.utxo.pop(0)
  930. for i in range(NUM_TESTS):
  931. self.utxo.append(UTXO(tx.sha256, i, split_value))
  932. sync_blocks(self.nodes)
  933. temp_utxo = []
  934. tx = CTransaction()
  935. count = 0
  936. witness_program = CScript([OP_TRUE])
  937. witness_hash = sha256(witness_program)
  938. assert_equal(len(self.nodes[1].getrawmempool()), 0)
  939. for version in list(range(OP_1, OP_16+1)) + [OP_0]:
  940. count += 1
  941. # First try to spend to a future version segwit scriptPubKey.
  942. scriptPubKey = CScript([CScriptOp(version), witness_hash])
  943. tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b"")]
  944. tx.vout = [CTxOut(self.utxo[0].nValue-1000, scriptPubKey)]
  945. tx.rehash()
  946. self.std_node.test_transaction_acceptance(tx, with_witness=True, accepted=False)
  947. self.test_node.test_transaction_acceptance(tx, with_witness=True, accepted=True)
  948. self.utxo.pop(0)
  949. temp_utxo.append(UTXO(tx.sha256, 0, tx.vout[0].nValue))
  950. self.nodes[0].generate(1) # Mine all the transactions
  951. sync_blocks(self.nodes)
  952. assert(len(self.nodes[0].getrawmempool()) == 0)
  953. # Finally, verify that version 0 -> version 1 transactions
  954. # are non-standard
  955. scriptPubKey = CScript([CScriptOp(OP_1), witness_hash])
  956. tx2 = CTransaction()
  957. tx2.vin = [CTxIn(COutPoint(tx.sha256, 0), b"")]
  958. tx2.vout = [CTxOut(tx.vout[0].nValue-1000, scriptPubKey)]
  959. tx2.wit.vtxinwit.append(CTxInWitness())
  960. tx2.wit.vtxinwit[0].scriptWitness.stack = [ witness_program ]
  961. tx2.rehash()
  962. # Gets accepted to test_node, because standardness of outputs isn't
  963. # checked with fRequireStandard
  964. self.test_node.test_transaction_acceptance(tx2, with_witness=True, accepted=True)
  965. self.std_node.test_transaction_acceptance(tx2, with_witness=True, accepted=False)
  966. temp_utxo.pop() # last entry in temp_utxo was the output we just spent
  967. temp_utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
  968. # Spend everything in temp_utxo back to an OP_TRUE output.
  969. tx3 = CTransaction()
  970. total_value = 0
  971. for i in temp_utxo:
  972. tx3.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
  973. tx3.wit.vtxinwit.append(CTxInWitness())
  974. total_value += i.nValue
  975. tx3.wit.vtxinwit[-1].scriptWitness.stack = [witness_program]
  976. tx3.vout.append(CTxOut(total_value - 1000, CScript([OP_TRUE])))
  977. tx3.rehash()
  978. # Spending a higher version witness output is not allowed by policy,
  979. # even with fRequireStandard=false.
  980. self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=False)
  981. self.test_node.sync_with_ping()
  982. with mininode_lock:
  983. assert(b"reserved for soft-fork upgrades" in self.test_node.last_message["reject"].reason)
  984. # Building a block with the transaction must be valid, however.
  985. block = self.build_next_block()
  986. self.update_witness_block_with_transactions(block, [tx2, tx3])
  987. self.test_node.test_witness_block(block, accepted=True)
  988. sync_blocks(self.nodes)
  989. # Add utxo to our list
  990. self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
  991. def test_premature_coinbase_witness_spend(self):
  992. self.log.info("Testing premature coinbase witness spend")
  993. block = self.build_next_block()
  994. # Change the output of the block to be a witness output.
  995. witness_program = CScript([OP_TRUE])
  996. witness_hash = sha256(witness_program)
  997. scriptPubKey = CScript([OP_0, witness_hash])
  998. block.vtx[0].vout[0].scriptPubKey = scriptPubKey
  999. # This next line will rehash the coinbase and update the merkle
  1000. # root, and solve.
  1001. self.update_witness_block_with_transactions(block, [])
  1002. self.test_node.test_witness_block(block, accepted=True)
  1003. spend_tx = CTransaction()
  1004. spend_tx.vin = [CTxIn(COutPoint(block.vtx[0].sha256, 0), b"")]
  1005. spend_tx.vout = [CTxOut(block.vtx[0].vout[0].nValue, witness_program)]
  1006. spend_tx.wit.vtxinwit.append(CTxInWitness())
  1007. spend_tx.wit.vtxinwit[0].scriptWitness.stack = [ witness_program ]
  1008. spend_tx.rehash()
  1009. # Now test a premature spend.
  1010. self.nodes[0].generate(98)
  1011. sync_blocks(self.nodes)
  1012. block2 = self.build_next_block()
  1013. self.update_witness_block_with_transactions(block2, [spend_tx])
  1014. self.test_node.test_witness_block(block2, accepted=False)
  1015. # Advancing one more block should allow the spend.
  1016. self.nodes[0].generate(1)
  1017. block2 = self.build_next_block()
  1018. self.update_witness_block_with_transactions(block2, [spend_tx])
  1019. self.test_node.test_witness_block(block2, accepted=True)
  1020. sync_blocks(self.nodes)
  1021. def test_signature_version_1(self):
  1022. self.log.info("Testing segwit signature hash version 1")
  1023. key = CECKey()
  1024. key.set_secretbytes(b"9")
  1025. pubkey = CPubKey(key.get_pubkey())
  1026. witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
  1027. witness_hash = sha256(witness_program)
  1028. scriptPubKey = CScript([OP_0, witness_hash])
  1029. # First create a witness output for use in the tests.
  1030. assert(len(self.utxo))
  1031. tx = CTransaction()
  1032. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  1033. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, scriptPubKey))
  1034. tx.rehash()
  1035. self.test_node.test_transaction_acceptance(tx, with_witness=True, accepted=True)
  1036. # Mine this transaction in preparation for following tests.
  1037. block = self.build_next_block()
  1038. self.update_witness_block_with_transactions(block, [tx])
  1039. self.test_node.test_witness_block(block, accepted=True)
  1040. sync_blocks(self.nodes)
  1041. self.utxo.pop(0)
  1042. # Test each hashtype
  1043. prev_utxo = UTXO(tx.sha256, 0, tx.vout[0].nValue)
  1044. for sigflag in [ 0, SIGHASH_ANYONECANPAY ]:
  1045. for hashtype in [SIGHASH_ALL, SIGHASH_NONE, SIGHASH_SINGLE]:
  1046. hashtype |= sigflag
  1047. block = self.build_next_block()
  1048. tx = CTransaction()
  1049. tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))
  1050. tx.vout.append(CTxOut(prev_utxo.nValue - 1000, scriptPubKey))
  1051. tx.wit.vtxinwit.append(CTxInWitness())
  1052. # Too-large input value
  1053. sign_P2PK_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue+1, key)
  1054. self.update_witness_block_with_transactions(block, [tx])
  1055. self.test_node.test_witness_block(block, accepted=False)
  1056. # Too-small input value
  1057. sign_P2PK_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue-1, key)
  1058. block.vtx.pop() # remove last tx
  1059. self.update_witness_block_with_transactions(block, [tx])
  1060. self.test_node.test_witness_block(block, accepted=False)
  1061. # Now try correct value
  1062. sign_P2PK_witness_input(witness_program, tx, 0, hashtype, prev_utxo.nValue, key)
  1063. block.vtx.pop()
  1064. self.update_witness_block_with_transactions(block, [tx])
  1065. self.test_node.test_witness_block(block, accepted=True)
  1066. prev_utxo = UTXO(tx.sha256, 0, tx.vout[0].nValue)
  1067. # Test combinations of signature hashes.
  1068. # Split the utxo into a lot of outputs.
  1069. # Randomly choose up to 10 to spend, sign with different hashtypes, and
  1070. # output to a random number of outputs. Repeat NUM_TESTS times.
  1071. # Ensure that we've tested a situation where we use SIGHASH_SINGLE with
  1072. # an input index > number of outputs.
  1073. NUM_TESTS = 500
  1074. temp_utxos = []
  1075. tx = CTransaction()
  1076. tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))
  1077. split_value = prev_utxo.nValue // NUM_TESTS
  1078. for i in range(NUM_TESTS):
  1079. tx.vout.append(CTxOut(split_value, scriptPubKey))
  1080. tx.wit.vtxinwit.append(CTxInWitness())
  1081. sign_P2PK_witness_input(witness_program, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)
  1082. for i in range(NUM_TESTS):
  1083. temp_utxos.append(UTXO(tx.sha256, i, split_value))
  1084. block = self.build_next_block()
  1085. self.update_witness_block_with_transactions(block, [tx])
  1086. self.test_node.test_witness_block(block, accepted=True)
  1087. block = self.build_next_block()
  1088. used_sighash_single_out_of_bounds = False
  1089. for i in range(NUM_TESTS):
  1090. # Ping regularly to keep the connection alive
  1091. if (not i % 100):
  1092. self.test_node.sync_with_ping()
  1093. # Choose random number of inputs to use.
  1094. num_inputs = random.randint(1, 10)
  1095. # Create a slight bias for producing more utxos
  1096. num_outputs = random.randint(1, 11)
  1097. random.shuffle(temp_utxos)
  1098. assert(len(temp_utxos) > num_inputs)
  1099. tx = CTransaction()
  1100. total_value = 0
  1101. for i in range(num_inputs):
  1102. tx.vin.append(CTxIn(COutPoint(temp_utxos[i].sha256, temp_utxos[i].n), b""))
  1103. tx.wit.vtxinwit.append(CTxInWitness())
  1104. total_value += temp_utxos[i].nValue
  1105. split_value = total_value // num_outputs
  1106. for i in range(num_outputs):
  1107. tx.vout.append(CTxOut(split_value, scriptPubKey))
  1108. for i in range(num_inputs):
  1109. # Now try to sign each input, using a random hashtype.
  1110. anyonecanpay = 0
  1111. if random.randint(0, 1):
  1112. anyonecanpay = SIGHASH_ANYONECANPAY
  1113. hashtype = random.randint(1, 3) | anyonecanpay
  1114. sign_P2PK_witness_input(witness_program, tx, i, hashtype, temp_utxos[i].nValue, key)
  1115. if (hashtype == SIGHASH_SINGLE and i >= num_outputs):
  1116. used_sighash_single_out_of_bounds = True
  1117. tx.rehash()
  1118. for i in range(num_outputs):
  1119. temp_utxos.append(UTXO(tx.sha256, i, split_value))
  1120. temp_utxos = temp_utxos[num_inputs:]
  1121. block.vtx.append(tx)
  1122. # Test the block periodically, if we're close to maxblocksize
  1123. if (get_virtual_size(block) > MAX_BLOCK_BASE_SIZE - 1000):
  1124. self.update_witness_block_with_transactions(block, [])
  1125. self.test_node.test_witness_block(block, accepted=True)
  1126. block = self.build_next_block()
  1127. if (not used_sighash_single_out_of_bounds):
  1128. self.log.info("WARNING: this test run didn't attempt SIGHASH_SINGLE with out-of-bounds index value")
  1129. # Test the transactions we've added to the block
  1130. if (len(block.vtx) > 1):
  1131. self.update_witness_block_with_transactions(block, [])
  1132. self.test_node.test_witness_block(block, accepted=True)
  1133. # Now test witness version 0 P2PKH transactions
  1134. pubkeyhash = hash160(pubkey)
  1135. scriptPKH = CScript([OP_0, pubkeyhash])
  1136. tx = CTransaction()
  1137. tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))
  1138. tx.vout.append(CTxOut(temp_utxos[0].nValue, scriptPKH))
  1139. tx.wit.vtxinwit.append(CTxInWitness())
  1140. sign_P2PK_witness_input(witness_program, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)
  1141. tx2 = CTransaction()
  1142. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  1143. tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
  1144. script = GetP2PKHScript(pubkeyhash)
  1145. sig_hash = SegwitVersion1SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
  1146. signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
  1147. # Check that we can't have a scriptSig
  1148. tx2.vin[0].scriptSig = CScript([signature, pubkey])
  1149. block = self.build_next_block()
  1150. self.update_witness_block_with_transactions(block, [tx, tx2])
  1151. self.test_node.test_witness_block(block, accepted=False)
  1152. # Move the signature to the witness.
  1153. block.vtx.pop()
  1154. tx2.wit.vtxinwit.append(CTxInWitness())
  1155. tx2.wit.vtxinwit[0].scriptWitness.stack = [signature, pubkey]
  1156. tx2.vin[0].scriptSig = b""
  1157. tx2.rehash()
  1158. self.update_witness_block_with_transactions(block, [tx2])
  1159. self.test_node.test_witness_block(block, accepted=True)
  1160. temp_utxos.pop(0)
  1161. # Update self.utxos for later tests. Just spend everything in
  1162. # temp_utxos to a corresponding entry in self.utxos
  1163. tx = CTransaction()
  1164. index = 0
  1165. for i in temp_utxos:
  1166. # Just spend to our usual anyone-can-spend output
  1167. # Use SIGHASH_SINGLE|SIGHASH_ANYONECANPAY so we can build up
  1168. # the signatures as we go.
  1169. tx.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
  1170. tx.vout.append(CTxOut(i.nValue, CScript([OP_TRUE])))
  1171. tx.wit.vtxinwit.append(CTxInWitness())
  1172. sign_P2PK_witness_input(witness_program, tx, index, SIGHASH_SINGLE|SIGHASH_ANYONECANPAY, i.nValue, key)
  1173. index += 1
  1174. block = self.build_next_block()
  1175. self.update_witness_block_with_transactions(block, [tx])
  1176. self.test_node.test_witness_block(block, accepted=True)
  1177. for i in range(len(tx.vout)):
  1178. self.utxo.append(UTXO(tx.sha256, i, tx.vout[i].nValue))
  1179. # Test P2SH wrapped witness programs.
  1180. def test_p2sh_witness(self, segwit_activated):
  1181. self.log.info("Testing P2SH witness transactions")
  1182. assert(len(self.utxo))
  1183. # Prepare the p2sh-wrapped witness output
  1184. witness_program = CScript([OP_DROP, OP_TRUE])
  1185. witness_hash = sha256(witness_program)
  1186. p2wsh_pubkey = CScript([OP_0, witness_hash])
  1187. p2sh_witness_hash = hash160(p2wsh_pubkey)
  1188. scriptPubKey = CScript([OP_HASH160, p2sh_witness_hash, OP_EQUAL])
  1189. scriptSig = CScript([p2wsh_pubkey]) # a push of the redeem script
  1190. # Fund the P2SH output
  1191. tx = CTransaction()
  1192. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  1193. tx.vout.append(CTxOut(self.utxo[0].nValue-1000, scriptPubKey))
  1194. tx.rehash()
  1195. # Verify mempool acceptance and block validity
  1196. self.test_node.test_transaction_acceptance(tx, with_witness=False, accepted=True)
  1197. block = self.build_next_block()
  1198. self.update_witness_block_with_transactions(block, [tx])
  1199. self.test_node.test_witness_block(block, accepted=True, with_witness=segwit_activated)
  1200. sync_blocks(self.nodes)
  1201. # Now test attempts to spend the output.
  1202. spend_tx = CTransaction()
  1203. spend_tx.vin.append(CTxIn(COutPoint(tx.sha256, 0), scriptSig))
  1204. spend_tx.vout.append(CTxOut(tx.vout[0].nValue-1000, CScript([OP_TRUE])))
  1205. spend_tx.rehash()
  1206. # This transaction should not be accepted into the mempool pre- or
  1207. # post-segwit. Mempool acceptance will use SCRIPT_VERIFY_WITNESS which
  1208. # will require a witness to spend a witness program regardless of
  1209. # segwit activation. Note that older bitcoind's that are not
  1210. # segwit-aware would also reject this for failing CLEANSTACK.
  1211. self.test_node.test_transaction_acceptance(spend_tx, with_witness=False, accepted=False)
  1212. # Try to put the witness script in the scriptSig, should also fail.
  1213. spend_tx.vin[0].scriptSig = CScript([p2wsh_pubkey, b'a'])
  1214. spend_tx.rehash()
  1215. self.test_node.test_transaction_acceptance(spend_tx, with_witness=False, accepted=False)
  1216. # Now put the witness script in the witness, should succeed after
  1217. # segwit activates.
  1218. spend_tx.vin[0].scriptSig = scriptSig
  1219. spend_tx.rehash()
  1220. spend_tx.wit.vtxinwit.append(CTxInWitness())
  1221. spend_tx.wit.vtxinwit[0].scriptWitness.stack = [ b'a', witness_program ]
  1222. # Verify mempool acceptance
  1223. self.test_node.test_transaction_acceptance(spend_tx, with_witness=True, accepted=segwit_activated)
  1224. block = self.build_next_block()
  1225. self.update_witness_block_with_transactions(block, [spend_tx])
  1226. # If we're before activation, then sending this without witnesses
  1227. # should be valid. If we're after activation, then sending this with
  1228. # witnesses should be valid.
  1229. if segwit_activated:
  1230. self.test_node.test_witness_block(block, accepted=True)
  1231. else:
  1232. self.test_node.test_witness_block(block, accepted=True, with_witness=False)
  1233. # Update self.utxo
  1234. self.utxo.pop(0)
  1235. self.utxo.append(UTXO(spend_tx.sha256, 0, spend_tx.vout[0].nValue))
  1236. # Test the behavior of starting up a segwit-aware node after the softfork
  1237. # has activated. As segwit requires different block data than pre-segwit
  1238. # nodes would have stored, this requires special handling.
  1239. # To enable this test, pass --oldbinary=<path-to-pre-segwit-bitcoind> to
  1240. # the test.
  1241. def test_upgrade_after_activation(self, node_id):
  1242. self.log.info("Testing software upgrade after softfork activation")
  1243. assert(node_id != 0) # node0 is assumed to be a segwit-active bitcoind
  1244. # Make sure the nodes are all up
  1245. sync_blocks(self.nodes)
  1246. # Restart with the new binary
  1247. self.stop_node(node_id)
  1248. self.start_node(node_id, extra_args=[])
  1249. connect_nodes(self.nodes[0], node_id)
  1250. sync_blocks(self.nodes)
  1251. # Make sure that this peer thinks segwit has activated.
  1252. assert(get_bip9_status(self.nodes[node_id], 'segwit')['status'] == "active")
  1253. # Make sure this peers blocks match those of node0.
  1254. height = self.nodes[node_id].getblockcount()
  1255. while height >= 0:
  1256. block_hash = self.nodes[node_id].getblockhash(height)
  1257. assert_equal(block_hash, self.nodes[0].getblockhash(height))
  1258. assert_equal(self.nodes[0].getblock(block_hash), self.nodes[node_id].getblock(block_hash))
  1259. height -= 1
  1260. def test_witness_sigops(self):
  1261. '''Ensure sigop counting is correct inside witnesses.'''
  1262. self.log.info("Testing sigops limit")
  1263. assert(len(self.utxo))
  1264. # Keep this under MAX_OPS_PER_SCRIPT (201)
  1265. witness_program = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG]*5 + [OP_CHECKSIG]*193 + [OP_ENDIF])
  1266. witness_hash = sha256(witness_program)
  1267. scriptPubKey = CScript([OP_0, witness_hash])
  1268. sigops_per_script = 20*5 + 193*1
  1269. # We'll produce 2 extra outputs, one with a program that would take us
  1270. # over max sig ops, and one with a program that would exactly reach max
  1271. # sig ops
  1272. outputs = (MAX_SIGOP_COST // sigops_per_script) + 2
  1273. extra_sigops_available = MAX_SIGOP_COST % sigops_per_script
  1274. # We chose the number of checkmultisigs/checksigs to make this work:
  1275. assert(extra_sigops_available < 100) # steer clear of MAX_OPS_PER_SCRIPT
  1276. # This script, when spent with the first
  1277. # N(=MAX_SIGOP_COST//sigops_per_script) outputs of our transaction,
  1278. # would push us just over the block sigop limit.
  1279. witness_program_toomany = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG]*(extra_sigops_available + 1) + [OP_ENDIF])
  1280. witness_hash_toomany = sha256(witness_program_toomany)
  1281. scriptPubKey_toomany = CScript([OP_0, witness_hash_toomany])
  1282. # If we spend this script instead, we would exactly reach our sigop
  1283. # limit (for witness sigops).
  1284. witness_program_justright = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG]*(extra_sigops_available) + [OP_ENDIF])
  1285. witness_hash_justright = sha256(witness_program_justright)
  1286. scriptPubKey_justright = CScript([OP_0, witness_hash_justright])
  1287. # First split our available utxo into a bunch of outputs
  1288. split_value = self.utxo[0].nValue // outputs
  1289. tx = CTransaction()
  1290. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  1291. for i in range(outputs):
  1292. tx.vout.append(CTxOut(split_value, scriptPubKey))
  1293. tx.vout[-2].scriptPubKey = scriptPubKey_toomany
  1294. tx.vout[-1].scriptPubKey = scriptPubKey_justright
  1295. tx.rehash()
  1296. block_1 = self.build_next_block()
  1297. self.update_witness_block_with_transactions(block_1, [tx])
  1298. self.test_node.test_witness_block(block_1, accepted=True)
  1299. tx2 = CTransaction()
  1300. # If we try to spend the first n-1 outputs from tx, that should be
  1301. # too many sigops.
  1302. total_value = 0
  1303. for i in range(outputs-1):
  1304. tx2.vin.append(CTxIn(COutPoint(tx.sha256, i), b""))
  1305. tx2.wit.vtxinwit.append(CTxInWitness())
  1306. tx2.wit.vtxinwit[-1].scriptWitness.stack = [ witness_program ]
  1307. total_value += tx.vout[i].nValue
  1308. tx2.wit.vtxinwit[-1].scriptWitness.stack = [ witness_program_toomany ]
  1309. tx2.vout.append(CTxOut(total_value, CScript([OP_TRUE])))
  1310. tx2.rehash()
  1311. block_2 = self.build_next_block()
  1312. self.update_witness_block_with_transactions(block_2, [tx2])
  1313. self.test_node.test_witness_block(block_2, accepted=False)
  1314. # Try dropping the last input in tx2, and add an output that has
  1315. # too many sigops (contributing to legacy sigop count).
  1316. checksig_count = (extra_sigops_available // 4) + 1
  1317. scriptPubKey_checksigs = CScript([OP_CHECKSIG]*checksig_count)
  1318. tx2.vout.append(CTxOut(0, scriptPubKey_checksigs))
  1319. tx2.vin.pop()
  1320. tx2.wit.vtxinwit.pop()
  1321. tx2.vout[0].nValue -= tx.vout[-2].nValue
  1322. tx2.rehash()
  1323. block_3 = self.build_next_block()
  1324. self.update_witness_block_with_transactions(block_3, [tx2])
  1325. self.test_node.test_witness_block(block_3, accepted=False)
  1326. # If we drop the last checksig in this output, the tx should succeed.
  1327. block_4 = self.build_next_block()
  1328. tx2.vout[-1].scriptPubKey = CScript([OP_CHECKSIG]*(checksig_count-1))
  1329. tx2.rehash()
  1330. self.update_witness_block_with_transactions(block_4, [tx2])
  1331. self.test_node.test_witness_block(block_4, accepted=True)
  1332. # Reset the tip back down for the next test
  1333. sync_blocks(self.nodes)
  1334. for x in self.nodes:
  1335. x.invalidateblock(block_4.hash)
  1336. # Try replacing the last input of tx2 to be spending the last
  1337. # output of tx
  1338. block_5 = self.build_next_block()
  1339. tx2.vout.pop()
  1340. tx2.vin.append(CTxIn(COutPoint(tx.sha256, outputs-1), b""))
  1341. tx2.wit.vtxinwit.append(CTxInWitness())
  1342. tx2.wit.vtxinwit[-1].scriptWitness.stack = [ witness_program_justright ]
  1343. tx2.rehash()
  1344. self.update_witness_block_with_transactions(block_5, [tx2])
  1345. self.test_node.test_witness_block(block_5, accepted=True)
  1346. # TODO: test p2sh sigop counting
  1347. def test_getblocktemplate_before_lockin(self):
  1348. self.log.info("Testing getblocktemplate setting of segwit versionbit (before lockin)")
  1349. # Node0 is segwit aware, node2 is not.
  1350. for node in [self.nodes[0], self.nodes[2]]:
  1351. gbt_results = node.getblocktemplate()
  1352. block_version = gbt_results['version']
  1353. # If we're not indicating segwit support, we will still be
  1354. # signalling for segwit activation.
  1355. assert_equal((block_version & (1 << VB_WITNESS_BIT) != 0), node == self.nodes[0])
  1356. # If we don't specify the segwit rule, then we won't get a default
  1357. # commitment.
  1358. assert('default_witness_commitment' not in gbt_results)
  1359. # Workaround:
  1360. # Can either change the tip, or change the mempool and wait 5 seconds
  1361. # to trigger a recomputation of getblocktemplate.
  1362. txid = int(self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1), 16)
  1363. # Using mocktime lets us avoid sleep()
  1364. sync_mempools(self.nodes)
  1365. self.nodes[0].setmocktime(int(time.time())+10)
  1366. self.nodes[2].setmocktime(int(time.time())+10)
  1367. for node in [self.nodes[0], self.nodes[2]]:
  1368. gbt_results = node.getblocktemplate({"rules" : ["segwit"]})
  1369. block_version = gbt_results['version']
  1370. if node == self.nodes[2]:
  1371. # If this is a non-segwit node, we should still not get a witness
  1372. # commitment, nor a version bit signalling segwit.
  1373. assert_equal(block_version & (1 << VB_WITNESS_BIT), 0)
  1374. assert('default_witness_commitment' not in gbt_results)
  1375. else:
  1376. # For segwit-aware nodes, check the version bit and the witness
  1377. # commitment are correct.
  1378. assert(block_version & (1 << VB_WITNESS_BIT) != 0)
  1379. assert('default_witness_commitment' in gbt_results)
  1380. witness_commitment = gbt_results['default_witness_commitment']
  1381. # Check that default_witness_commitment is present.
  1382. witness_root = CBlock.get_merkle_root([ser_uint256(0),
  1383. ser_uint256(txid)])
  1384. script = get_witness_script(witness_root, 0)
  1385. assert_equal(witness_commitment, bytes_to_hex_str(script))
  1386. # undo mocktime
  1387. self.nodes[0].setmocktime(0)
  1388. self.nodes[2].setmocktime(0)
  1389. # Uncompressed pubkeys are no longer supported in default relay policy,
  1390. # but (for now) are still valid in blocks.
  1391. def test_uncompressed_pubkey(self):
  1392. self.log.info("Testing uncompressed pubkeys")
  1393. # Segwit transactions using uncompressed pubkeys are not accepted
  1394. # under default policy, but should still pass consensus.
  1395. key = CECKey()
  1396. key.set_secretbytes(b"9")
  1397. key.set_compressed(False)
  1398. pubkey = CPubKey(key.get_pubkey())
  1399. assert_equal(len(pubkey), 65) # This should be an uncompressed pubkey
  1400. assert(len(self.utxo) > 0)
  1401. utxo = self.utxo.pop(0)
  1402. # Test 1: P2WPKH
  1403. # First create a P2WPKH output that uses an uncompressed pubkey
  1404. pubkeyhash = hash160(pubkey)
  1405. scriptPKH = CScript([OP_0, pubkeyhash])
  1406. tx = CTransaction()
  1407. tx.vin.append(CTxIn(COutPoint(utxo.sha256, utxo.n), b""))
  1408. tx.vout.append(CTxOut(utxo.nValue-1000, scriptPKH))
  1409. tx.rehash()
  1410. # Confirm it in a block.
  1411. block = self.build_next_block()
  1412. self.update_witness_block_with_transactions(block, [tx])
  1413. self.test_node.test_witness_block(block, accepted=True)
  1414. # Now try to spend it. Send it to a P2WSH output, which we'll
  1415. # use in the next test.
  1416. witness_program = CScript([pubkey, CScriptOp(OP_CHECKSIG)])
  1417. witness_hash = sha256(witness_program)
  1418. scriptWSH = CScript([OP_0, witness_hash])
  1419. tx2 = CTransaction()
  1420. tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
  1421. tx2.vout.append(CTxOut(tx.vout[0].nValue-1000, scriptWSH))
  1422. script = GetP2PKHScript(pubkeyhash)
  1423. sig_hash = SegwitVersion1SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
  1424. signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
  1425. tx2.wit.vtxinwit.append(CTxInWitness())
  1426. tx2.wit.vtxinwit[0].scriptWitness.stack = [ signature, pubkey ]
  1427. tx2.rehash()
  1428. # Should fail policy test.
  1429. self.test_node.test_transaction_acceptance(tx2, True, False, b'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
  1430. # But passes consensus.
  1431. block = self.build_next_block()
  1432. self.update_witness_block_with_transactions(block, [tx2])
  1433. self.test_node.test_witness_block(block, accepted=True)
  1434. # Test 2: P2WSH
  1435. # Try to spend the P2WSH output created in last test.
  1436. # Send it to a P2SH(P2WSH) output, which we'll use in the next test.
  1437. p2sh_witness_hash = hash160(scriptWSH)
  1438. scriptP2SH = CScript([OP_HASH160, p2sh_witness_hash, OP_EQUAL])
  1439. scriptSig = CScript([scriptWSH])
  1440. tx3 = CTransaction()
  1441. tx3.vin.append(CTxIn(COutPoint(tx2.sha256, 0), b""))
  1442. tx3.vout.append(CTxOut(tx2.vout[0].nValue-1000, scriptP2SH))
  1443. tx3.wit.vtxinwit.append(CTxInWitness())
  1444. sign_P2PK_witness_input(witness_program, tx3, 0, SIGHASH_ALL, tx2.vout[0].nValue, key)
  1445. # Should fail policy test.
  1446. self.test_node.test_transaction_acceptance(tx3, True, False, b'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
  1447. # But passes consensus.
  1448. block = self.build_next_block()
  1449. self.update_witness_block_with_transactions(block, [tx3])
  1450. self.test_node.test_witness_block(block, accepted=True)
  1451. # Test 3: P2SH(P2WSH)
  1452. # Try to spend the P2SH output created in the last test.
  1453. # Send it to a P2PKH output, which we'll use in the next test.
  1454. scriptPubKey = GetP2PKHScript(pubkeyhash)
  1455. tx4 = CTransaction()
  1456. tx4.vin.append(CTxIn(COutPoint(tx3.sha256, 0), scriptSig))
  1457. tx4.vout.append(CTxOut(tx3.vout[0].nValue-1000, scriptPubKey))
  1458. tx4.wit.vtxinwit.append(CTxInWitness())
  1459. sign_P2PK_witness_input(witness_program, tx4, 0, SIGHASH_ALL, tx3.vout[0].nValue, key)
  1460. # Should fail policy test.
  1461. self.test_node.test_transaction_acceptance(tx4, True, False, b'non-mandatory-script-verify-flag (Using non-compressed keys in segwit)')
  1462. block = self.build_next_block()
  1463. self.update_witness_block_with_transactions(block, [tx4])
  1464. self.test_node.test_witness_block(block, accepted=True)
  1465. # Test 4: Uncompressed pubkeys should still be valid in non-segwit
  1466. # transactions.
  1467. tx5 = CTransaction()
  1468. tx5.vin.append(CTxIn(COutPoint(tx4.sha256, 0), b""))
  1469. tx5.vout.append(CTxOut(tx4.vout[0].nValue-1000, CScript([OP_TRUE])))
  1470. (sig_hash, err) = SignatureHash(scriptPubKey, tx5, 0, SIGHASH_ALL)
  1471. signature = key.sign(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
  1472. tx5.vin[0].scriptSig = CScript([signature, pubkey])
  1473. tx5.rehash()
  1474. # Should pass policy and consensus.
  1475. self.test_node.test_transaction_acceptance(tx5, True, True)
  1476. block = self.build_next_block()
  1477. self.update_witness_block_with_transactions(block, [tx5])
  1478. self.test_node.test_witness_block(block, accepted=True)
  1479. self.utxo.append(UTXO(tx5.sha256, 0, tx5.vout[0].nValue))
  1480. def test_non_standard_witness(self):
  1481. self.log.info("Testing detection of non-standard P2WSH witness")
  1482. pad = chr(1).encode('latin-1')
  1483. # Create scripts for tests
  1484. scripts = []
  1485. scripts.append(CScript([OP_DROP] * 100))
  1486. scripts.append(CScript([OP_DROP] * 99))
  1487. scripts.append(CScript([pad * 59] * 59 + [OP_DROP] * 60))
  1488. scripts.append(CScript([pad * 59] * 59 + [OP_DROP] * 61))
  1489. p2wsh_scripts = []
  1490. assert(len(self.utxo))
  1491. tx = CTransaction()
  1492. tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
  1493. # For each script, generate a pair of P2WSH and P2SH-P2WSH output.
  1494. outputvalue = (self.utxo[0].nValue - 1000) // (len(scripts) * 2)
  1495. for i in scripts:
  1496. p2wsh = CScript([OP_0, sha256(i)])
  1497. p2sh = hash160(p2wsh)
  1498. p2wsh_scripts.append(p2wsh)
  1499. tx.vout.append(CTxOut(outputvalue, p2wsh))
  1500. tx.vout.append(CTxOut(outputvalue, CScript([OP_HASH160, p2sh, OP_EQUAL])))
  1501. tx.rehash()
  1502. txid = tx.sha256
  1503. self.test_node.test_transaction_acceptance(tx, with_witness=False, accepted=True)
  1504. self.nodes[0].generate(1)
  1505. sync_blocks(self.nodes)
  1506. # Creating transactions for tests
  1507. p2wsh_txs = []
  1508. p2sh_txs = []
  1509. for i in range(len(scripts)):
  1510. p2wsh_tx = CTransaction()
  1511. p2wsh_tx.vin.append(CTxIn(COutPoint(txid,i*2)))
  1512. p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
  1513. p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
  1514. p2wsh_tx.rehash()
  1515. p2wsh_txs.append(p2wsh_tx)
  1516. p2sh_tx = CTransaction()
  1517. p2sh_tx.vin.append(CTxIn(COutPoint(txid,i*2+1), CScript([p2wsh_scripts[i]])))
  1518. p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(hex_str_to_bytes(""))])))
  1519. p2sh_tx.wit.vtxinwit.append(CTxInWitness())
  1520. p2sh_tx.rehash()
  1521. p2sh_txs.append(p2sh_tx)
  1522. # Testing native P2WSH
  1523. # Witness stack size, excluding witnessScript, over 100 is non-standard
  1524. p2wsh_txs[0].wit.vtxinwit[0].scriptWitness.stack = [pad] * 101 + [scripts[0]]
  1525. self.std_node.test_transaction_acceptance(p2wsh_txs[0], True, False, b'bad-witness-nonstandard')
  1526. # Non-standard nodes should accept
  1527. self.test_node.test_transaction_acceptance(p2wsh_txs[0], True, True)
  1528. # Stack element size over 80 bytes is non-standard
  1529. p2wsh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 81] * 100 + [scripts[1]]
  1530. self.std_node.test_transaction_acceptance(p2wsh_txs[1], True, False, b'bad-witness-nonstandard')
  1531. # Non-standard nodes should accept
  1532. self.test_node.test_transaction_acceptance(p2wsh_txs[1], True, True)
  1533. # Standard nodes should accept if element size is not over 80 bytes
  1534. p2wsh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 80] * 100 + [scripts[1]]
  1535. self.std_node.test_transaction_acceptance(p2wsh_txs[1], True, True)
  1536. # witnessScript size at 3600 bytes is standard
  1537. p2wsh_txs[2].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, scripts[2]]
  1538. self.test_node.test_transaction_acceptance(p2wsh_txs[2], True, True)
  1539. self.std_node.test_transaction_acceptance(p2wsh_txs[2], True, True)
  1540. # witnessScript size at 3601 bytes is non-standard
  1541. p2wsh_txs[3].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, pad, scripts[3]]
  1542. self.std_node.test_transaction_acceptance(p2wsh_txs[3], True, False, b'bad-witness-nonstandard')
  1543. # Non-standard nodes should accept
  1544. self.test_node.test_transaction_acceptance(p2wsh_txs[3], True, True)
  1545. # Repeating the same tests with P2SH-P2WSH
  1546. p2sh_txs[0].wit.vtxinwit[0].scriptWitness.stack = [pad] * 101 + [scripts[0]]
  1547. self.std_node.test_transaction_acceptance(p2sh_txs[0], True, False, b'bad-witness-nonstandard')
  1548. self.test_node.test_transaction_acceptance(p2sh_txs[0], True, True)
  1549. p2sh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 81] * 100 + [scripts[1]]
  1550. self.std_node.test_transaction_acceptance(p2sh_txs[1], True, False, b'bad-witness-nonstandard')
  1551. self.test_node.test_transaction_acceptance(p2sh_txs[1], True, True)
  1552. p2sh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 80] * 100 + [scripts[1]]
  1553. self.std_node.test_transaction_acceptance(p2sh_txs[1], True, True)
  1554. p2sh_txs[2].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, scripts[2]]
  1555. self.test_node.test_transaction_acceptance(p2sh_txs[2], True, True)
  1556. self.std_node.test_transaction_acceptance(p2sh_txs[2], True, True)
  1557. p2sh_txs[3].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, pad, scripts[3]]
  1558. self.std_node.test_transaction_acceptance(p2sh_txs[3], True, False, b'bad-witness-nonstandard')
  1559. self.test_node.test_transaction_acceptance(p2sh_txs[3], True, True)
  1560. self.nodes[0].generate(1) # Mine and clean up the mempool of non-standard node
  1561. # Valid but non-standard transactions in a block should be accepted by standard node
  1562. sync_blocks(self.nodes)
  1563. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  1564. assert_equal(len(self.nodes[1].getrawmempool()), 0)
  1565. self.utxo.pop(0)
  1566. def run_test(self):
  1567. # Setup the p2p connections and start up the network thread.
  1568. self.test_node = TestNode() # sets NODE_WITNESS|NODE_NETWORK
  1569. self.old_node = TestNode() # only NODE_NETWORK
  1570. self.std_node = TestNode() # for testing node1 (fRequireStandard=true)
  1571. self.p2p_connections = [self.test_node, self.old_node]
  1572. self.connections = []
  1573. self.connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.test_node, services=NODE_NETWORK|NODE_WITNESS))
  1574. self.connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], self.old_node, services=NODE_NETWORK))
  1575. self.connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.std_node, services=NODE_NETWORK|NODE_WITNESS))
  1576. self.test_node.add_connection(self.connections[0])
  1577. self.old_node.add_connection(self.connections[1])
  1578. self.std_node.add_connection(self.connections[2])
  1579. NetworkThread().start() # Start up network handling in another thread
  1580. # Keep a place to store utxo's that can be used in later tests
  1581. self.utxo = []
  1582. # Test logic begins here
  1583. self.test_node.wait_for_verack()
  1584. self.log.info("Starting tests before segwit lock in:")
  1585. self.test_witness_services() # Verifies NODE_WITNESS
  1586. self.test_non_witness_transaction() # non-witness tx's are accepted
  1587. self.test_unnecessary_witness_before_segwit_activation()
  1588. self.test_block_relay(segwit_activated=False)
  1589. # Advance to segwit being 'started'
  1590. self.advance_to_segwit_started()
  1591. sync_blocks(self.nodes)
  1592. self.test_getblocktemplate_before_lockin()
  1593. sync_blocks(self.nodes)
  1594. # At lockin, nothing should change.
  1595. self.log.info("Testing behavior post lockin, pre-activation")
  1596. self.advance_to_segwit_lockin()
  1597. # Retest unnecessary witnesses
  1598. self.test_unnecessary_witness_before_segwit_activation()
  1599. self.test_witness_tx_relay_before_segwit_activation()
  1600. self.test_block_relay(segwit_activated=False)
  1601. self.test_p2sh_witness(segwit_activated=False)
  1602. self.test_standardness_v0(segwit_activated=False)
  1603. sync_blocks(self.nodes)
  1604. # Now activate segwit
  1605. self.log.info("Testing behavior after segwit activation")
  1606. self.advance_to_segwit_active()
  1607. sync_blocks(self.nodes)
  1608. # Test P2SH witness handling again
  1609. self.test_p2sh_witness(segwit_activated=True)
  1610. self.test_witness_commitments()
  1611. self.test_block_malleability()
  1612. self.test_witness_block_size()
  1613. self.test_submit_block()
  1614. self.test_extra_witness_data()
  1615. self.test_max_witness_push_length()
  1616. self.test_max_witness_program_length()
  1617. self.test_witness_input_length()
  1618. self.test_block_relay(segwit_activated=True)
  1619. self.test_tx_relay_after_segwit_activation()
  1620. self.test_standardness_v0(segwit_activated=True)
  1621. self.test_segwit_versions()
  1622. self.test_premature_coinbase_witness_spend()
  1623. self.test_uncompressed_pubkey()
  1624. self.test_signature_version_1()
  1625. self.test_non_standard_witness()
  1626. sync_blocks(self.nodes)
  1627. self.test_upgrade_after_activation(node_id=2)
  1628. self.test_witness_sigops()
  1629. if __name__ == '__main__':
  1630. SegWitTest().main()