forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_reject_customtxs.py
executable file
·118 lines (93 loc) · 4.87 KB
/
feature_reject_customtxs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# Copyright (c) 2014-2019 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test rejection of invalid custom TXs."""
from test_framework.test_framework import DefiTestFramework
from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal
class RejectCustomTx(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1', '-bayfrontgardensheight=1', '-dakotaheight=1', '-fortcanningheight=120']]
def run_test(self):
self.nodes[0].generate(101)
# Get and fund collateral address
collateral = self.nodes[0].getnewaddress("", "legacy")
self.nodes[0].sendtoaddress(collateral, 1)
self.nodes[0].generate(2)
block_count = self.nodes[0].getblockcount()
# Record the number of MNs
num_mns = len(self.nodes[0].listmasternodes())
# Make create MN TX, get details and wipe from mempool
txid = self.nodes[0].createmasternode(collateral)
rawtx = self.nodes[0].getrawtransaction(txid, 1)
rawtx_hex = self.nodes[0].getrawtransaction(txid)
self.nodes[0].clearmempool()
# Get push data and use to double push data in op_return
pushdata = rawtx['vout'][0]['scriptPubKey']['hex'][2:]
result = rawtx_hex.find(pushdata)
rawtx_hex_multiop = rawtx_hex[:result - 4] + hex(len(pushdata) + 1)[2:] + '6a' + pushdata + rawtx_hex[result:]
signed_multiop = self.nodes[0].signrawtransactionwithwallet(rawtx_hex_multiop)
# Send TX
self.nodes[0].sendrawtransaction(signed_multiop['hex'])
self.nodes[0].generate(1)
# Check that MNs have increased by 1
assert_equal(num_mns + 1, len(self.nodes[0].listmasternodes()))
# Rollback and wipe TXs.
self.nodes[0].invalidateblock(self.nodes[0].getblockhash(block_count))
self.nodes[0].clearmempool()
# Test sending DfTx in a vout other than 0
rawtx_opreturn_vout1 = self.nodes[0].createrawtransaction([{"txid":rawtx['vin'][0]['txid'], "vout":rawtx['vin'][0]['vout']}], [{collateral:0.99999000},{"data":pushdata[2:]}])
signed_opreturn_vout1 = self.nodes[0].signrawtransactionwithwallet(rawtx_opreturn_vout1)
self.nodes[0].sendrawtransaction(signed_opreturn_vout1['hex'])
# Wipe TX
self.nodes[0].clearmempool()
# Test sending DfTx with None type
rawtx_none = self.nodes[0].createrawtransaction([{"txid":rawtx['vin'][0]['txid'], "vout":rawtx['vin'][0]['vout']}], [{collateral:0.99999000},{"data":"4466547800"}])
signed_none = self.nodes[0].signrawtransactionwithwallet(rawtx_none)
self.nodes[0].sendrawtransaction(signed_none['hex'])
# Wipe TX
self.nodes[0].clearmempool()
# Test sending DfTx with unknown type z
rawtx_unknown = self.nodes[0].createrawtransaction([{"txid":rawtx['vin'][0]['txid'], "vout":rawtx['vin'][0]['vout']}], [{collateral:0.99999000},{"data":"4466547879"}])
signed_unknown = self.nodes[0].signrawtransactionwithwallet(rawtx_unknown)
self.nodes[0].sendrawtransaction(signed_unknown['hex'])
# Wipe TX
self.nodes[0].clearmempool()
# Move to FortCanning height
self.nodes[0].generate(20)
# Make sure MNs still at original count
assert_equal(num_mns, len(self.nodes[0].listmasternodes()))
# Try and send multi opcode TX after hard fork
try:
self.nodes[0].sendrawtransaction(signed_multiop['hex'])
assert(False)
except JSONRPCException as e:
errorString = e.error['message']
assert("Invalid custom transaction" in errorString)
# Try and send DfTx in a vout other than 0 after hard fork
try:
self.nodes[0].sendrawtransaction(signed_opreturn_vout1['hex'])
assert(False)
except JSONRPCException as e:
errorString = e.error['message']
assert("Invalid custom transaction" in errorString)
# Try and send DfTx with None type after hard fork
try:
self.nodes[0].sendrawtransaction(signed_none['hex'])
assert(False)
except JSONRPCException as e:
errorString = e.error['message']
assert("Invalid custom transaction" in errorString)
# Try and send DfTx with unknown type after hard fork
try:
self.nodes[0].sendrawtransaction(signed_unknown['hex'])
assert(False)
except JSONRPCException as e:
errorString = e.error['message']
assert("Invalid custom transaction" in errorString)
if __name__ == '__main__':
RejectCustomTx().main()