Skip to content

Commit 239cbd2

Browse files
committed
qa/rpc-tests/segwit: Test GBT sigops before and after activation
1 parent 160f895 commit 239cbd2

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

qa/rpc-tests/segwit.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def getutxo(txid):
6969
utxo["txid"] = txid
7070
return utxo
7171

72+
def find_unspent(node, min_value):
73+
for utxo in node.listunspent():
74+
if utxo['amount'] >= min_value:
75+
return utxo
76+
7277
class SegWitTest(BitcoinTestFramework):
7378

7479
def setup_chain(self):
@@ -117,8 +122,21 @@ def fail_mine(self, node, txid, sign, redeem_script=""):
117122
sync_blocks(self.nodes)
118123

119124
def run_test(self):
120-
self.nodes[0].generate(160) #block 160
121-
125+
self.nodes[0].generate(161) #block 161
126+
127+
print("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
128+
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
129+
tmpl = self.nodes[0].getblocktemplate({})
130+
assert(tmpl['sigoplimit'] == 20000)
131+
assert(tmpl['transactions'][0]['hash'] == txid)
132+
assert(tmpl['transactions'][0]['sigops'] == 2)
133+
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
134+
assert(tmpl['sigoplimit'] == 20000)
135+
assert(tmpl['transactions'][0]['hash'] == txid)
136+
assert(tmpl['transactions'][0]['sigops'] == 2)
137+
self.nodes[0].generate(1) #block 162
138+
139+
balance_presetup = self.nodes[0].getbalance()
122140
self.pubkey = []
123141
p2sh_ids = [] # p2sh_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE embedded in p2sh
124142
wit_ids = [] # wit_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE via bare witness
@@ -137,18 +155,18 @@ def run_test(self):
137155
for i in range(5):
138156
for n in range(3):
139157
for v in range(2):
140-
wit_ids[n][v].append(send_to_witness(v, self.nodes[0], self.nodes[0].listunspent()[0], self.pubkey[n], False, Decimal("49.999")))
141-
p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], self.nodes[0].listunspent()[0], self.pubkey[n], True, Decimal("49.999")))
158+
wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))
159+
p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999")))
142160

143-
self.nodes[0].generate(1) #block 161
161+
self.nodes[0].generate(1) #block 163
144162
sync_blocks(self.nodes)
145163

146164
# Make sure all nodes recognize the transactions as theirs
147-
assert_equal(self.nodes[0].getbalance(), 60*50 - 60*50 + 20*Decimal("49.999") + 50)
165+
assert_equal(self.nodes[0].getbalance(), balance_presetup - 60*50 + 20*Decimal("49.999") + 50)
148166
assert_equal(self.nodes[1].getbalance(), 20*Decimal("49.999"))
149167
assert_equal(self.nodes[2].getbalance(), 20*Decimal("49.999"))
150168

151-
self.nodes[0].generate(262) #block 423
169+
self.nodes[0].generate(260) #block 423
152170
sync_blocks(self.nodes)
153171

154172
print("Verify default node can't accept any witness format txs before fork")
@@ -205,5 +223,25 @@ def run_test(self):
205223
self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V0][0], True) #block 434
206224
self.success_mine(self.nodes[0], p2sh_ids[NODE_0][WIT_V1][0], True) #block 435
207225

226+
print("Verify sigops are counted in GBT with BIP141 rules after the fork")
227+
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
228+
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
229+
assert(tmpl['sigoplimit'] == 80000)
230+
assert(tmpl['transactions'][0]['txid'] == txid)
231+
assert(tmpl['transactions'][0]['sigops'] == 8)
232+
233+
print("Verify non-segwit miners get a valid GBT response after the fork")
234+
send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.998"))
235+
try:
236+
tmpl = self.nodes[0].getblocktemplate({})
237+
assert(len(tmpl['transactions']) == 1) # Doesn't include witness tx
238+
assert(tmpl['sigoplimit'] == 20000)
239+
assert(tmpl['transactions'][0]['hash'] == txid)
240+
assert(tmpl['transactions'][0]['sigops'] == 2)
241+
assert(('!segwit' in tmpl['rules']) or ('segwit' not in tmpl['rules']))
242+
except JSONRPCException:
243+
# This is an acceptable outcome
244+
pass
245+
208246
if __name__ == '__main__':
209247
SegWitTest().main()

0 commit comments

Comments
 (0)