1
1
#!/usr/bin/env python3
2
2
3
3
from decimal import Decimal
4
+ import os
4
5
import json
5
6
import time
6
7
10
11
connect_nodes_bi ,
11
12
rpc_auth_pair ,
12
13
rpc_port ,
14
+ p2p_port ,
13
15
start_node ,
14
- start_nodes ,
15
16
stop_node ,
16
17
)
17
18
@@ -47,42 +48,75 @@ def __init__(self):
47
48
self .setup_clean_chain = True
48
49
self .num_nodes = 4
49
50
51
+ def add_options (self , parser ):
52
+ parser .add_option ("--parent_binpath" , dest = "parent_binpath" , default = "" ,
53
+ help = "Use a different binary for launching nodes" )
54
+ parser .add_option ("--parent_bitcoin" , dest = "parent_bitcoin" , default = False , action = "store_true" ,
55
+ help = "Parent nodes are Bitcoin" )
56
+
50
57
def setup_network (self , split = False ):
58
+ if self .options .parent_bitcoin and self .options .parent_binpath == "" :
59
+ raise Exception ("Can't run with --parent_bitcoin without specifying --parent_binpath" )
51
60
61
+ self .nodes = []
62
+ self .extra_args = []
52
63
# Parent chain args
53
- self .extra_args = [[
54
- # '-printtoconsole',
55
- '-validatepegin=0' ,
56
- '-anyonecanspendaremine' ,
57
- '-initialfreecoins=2100000000000000' ,
58
- ]] * 2
59
-
60
- self .nodes = start_nodes (2 , self .options .tmpdir , self .extra_args [:2 ], chain = 'parent' )
64
+ for n in range (2 ):
65
+ if self .options .parent_bitcoin :
66
+ self .parent_chain = 'regtest'
67
+ rpc_u , rpc_p = rpc_auth_pair (n )
68
+ self .extra_args .append ([
69
+ "-printtoconsole=0" ,
70
+ "-port=" + str (p2p_port (n )),
71
+ "-rpcuser=" + rpc_u ,
72
+ "-rpcpassword=" + rpc_p ,
73
+ "-rpcport=" + str (rpc_port (n )),
74
+ "-addresstype=legacy" , # To make sure bitcoind gives back p2pkh no matter version
75
+ "-deprecatedrpc=validateaddress" ,
76
+ ])
77
+ else :
78
+ self .parent_chain = 'parent'
79
+ self .extra_args .append ([
80
+ "-printtoconsole=0" ,
81
+ '-validatepegin=0' ,
82
+ '-anyonecanspendaremine' ,
83
+ '-initialfreecoins=2100000000000000' ,
84
+ ])
85
+ self .binary = self .options .parent_binpath if self .options .parent_binpath != "" else None
86
+ self .nodes .append (start_node (n , self .options .tmpdir , self .extra_args [n ], binary = self .binary , chain = self .parent_chain ))
87
+
61
88
connect_nodes_bi (self .nodes , 0 , 1 )
62
89
self .parentgenesisblockhash = self .nodes [0 ].getblockhash (0 )
63
90
print ('parentgenesisblockhash' , self .parentgenesisblockhash )
64
- parent_pegged_asset = self .nodes [0 ].getsidechaininfo ()['pegged_asset' ]
91
+ if not self .options .parent_bitcoin :
92
+ parent_pegged_asset = self .nodes [0 ].getsidechaininfo ()['pegged_asset' ]
65
93
66
94
# Sidechain args
95
+ self .fedpeg_script = "512103dff4923d778550cc13ce0d887d737553b4b58f4e8e886507fc39f5e447b2186451ae"
67
96
parent_chain_signblockscript = '51'
68
97
for n in range (2 ):
69
98
rpc_u , rpc_p = rpc_auth_pair (n )
70
- self . extra_args . append ( [
71
- # ' -printtoconsole' ,
99
+ args = [
100
+ " -printtoconsole=0" ,
72
101
'-parentgenesisblockhash=%s' % self .parentgenesisblockhash ,
73
102
'-validatepegin=1' ,
103
+ '-fedpegscript=%s' % self .fedpeg_script ,
74
104
'-anyonecanspendaremine=0' ,
75
105
'-initialfreecoins=0' ,
76
106
'-peginconfirmationdepth=10' ,
77
107
'-mainchainrpchost=127.0.0.1' ,
78
108
'-mainchainrpcport=%s' % rpc_port (n ),
79
109
'-mainchainrpcuser=%s' % rpc_u ,
80
110
'-mainchainrpcpassword=%s' % rpc_p ,
81
- '-parentpubkeyprefix=235' ,
82
- '-parentscriptprefix=75' ,
83
- '-con_parent_chain_signblockscript=%s' % parent_chain_signblockscript ,
84
- '-con_parent_pegged_asset=%s' % parent_pegged_asset ,
85
- ])
111
+ ]
112
+ if not self .options .parent_bitcoin :
113
+ args .extend ([
114
+ '-parentpubkeyprefix=235' ,
115
+ '-parentscriptprefix=75' ,
116
+ '-con_parent_chain_signblockscript=%s' % parent_chain_signblockscript ,
117
+ '-con_parent_pegged_asset=%s' % parent_pegged_asset ,
118
+ ])
119
+ self .extra_args .append (args )
86
120
self .nodes .append (start_node (n + 2 , self .options .tmpdir , self .extra_args [n + 2 ], chain = 'sidechain' ))
87
121
88
122
connect_nodes_bi (self .nodes , 2 , 3 )
@@ -116,10 +150,10 @@ def run_test(self):
116
150
sidechain .generate (101 )
117
151
118
152
addrs = sidechain .getpeginaddress ()
119
- addr = parent . validateaddress ( addrs ["mainchain_address" ])
153
+ addr = addrs ["mainchain_address" ]
120
154
print ('addrs' , addrs )
121
- print (' addr' , addr )
122
- txid1 = parent .sendtoaddress (addrs [ "mainchain_address" ] , 24 )
155
+ print (parent . validateaddress ( addr ) )
156
+ txid1 = parent .sendtoaddress (addr , 24 )
123
157
# 10+2 confirms required to get into mempool and confirm
124
158
parent .generate (1 )
125
159
time .sleep (2 )
@@ -134,7 +168,7 @@ def run_test(self):
134
168
pegtxid = sidechain .claimpegin (raw , proof )
135
169
raise Exception ("Peg-in should not be mature enough yet, need another block." )
136
170
except JSONRPCException as e :
137
- print ('ERROR:' , e .error )
171
+ print ('RPC ERROR:' , e .error [ 'message' ] )
138
172
assert ("Peg-in Bitcoin transaction needs more confirmations to be sent." in e .error ["message" ])
139
173
140
174
# Second attempt simply doesn't hit mempool bar
@@ -143,23 +177,24 @@ def run_test(self):
143
177
pegtxid = sidechain .claimpegin (raw , proof )
144
178
raise Exception ("Peg-in should not be mature enough yet, need another block." )
145
179
except JSONRPCException as e :
180
+ print ('RPC ERROR:' , e .error ['message' ])
146
181
assert ("Peg-in Bitcoin transaction needs more confirmations to be sent." in e .error ["message" ])
147
182
148
- # Should fail due to non-witness
149
183
try :
150
- pegtxid = sidechain .claimpegin (raw , proof , get_new_unconfidential_address ( parent ) )
151
- raise Exception ("Peg-in with non-matching claim_script should fail." )
184
+ pegtxid = sidechain .createrawpegin (raw , proof , 'AEIOU' )
185
+ raise Exception ("Peg-in with non-hex claim_script should fail." )
152
186
except JSONRPCException as e :
153
- print (e .error [" message" ])
154
- assert ("Given or recovered script is not a witness program ." in e .error ["message" ])
187
+ print ('RPC ERROR:' , e .error [' message' ])
188
+ assert ("Given claim_script is not hex ." in e .error ["message" ])
155
189
156
- # # Should fail due to non-matching wallet address
157
- # try:
158
- # pegtxid = sidechain.claimpegin(raw, proof, get_new_unconfidential_address(sidechain))
159
- # raise Exception("Peg-in with non-matching claim_script should fail.")
160
- # except JSONRPCException as e:
161
- # print(e.error["message"])
162
- # assert("Given claim_script does not match the given Bitcoin transaction." in e.error["message"])
190
+ # Should fail due to non-matching wallet address
191
+ try :
192
+ scriptpubkey = sidechain .validateaddress (get_new_unconfidential_address (sidechain ))["scriptPubKey" ]
193
+ pegtxid = sidechain .claimpegin (raw , proof , scriptpubkey )
194
+ raise Exception ("Peg-in with non-matching claim_script should fail." )
195
+ except JSONRPCException as e :
196
+ print ('RPC ERROR:' , e .error ['message' ])
197
+ assert ("Given claim_script does not match the given Bitcoin transaction." in e .error ["message" ])
163
198
164
199
# 12 confirms allows in mempool
165
200
parent .generate (1 )
@@ -269,7 +304,7 @@ def run_test(self):
269
304
270
305
print ("Now test failure to validate peg-ins based on intermittant bitcoind rpc failure" )
271
306
stop_node (self .nodes [1 ], 1 )
272
- txid = parent .sendtoaddress (addrs [ "mainchain_address" ] , 1 )
307
+ txid = parent .sendtoaddress (addr , 1 )
273
308
parent .generate (12 )
274
309
proof = parent .gettxoutproof ([txid ])
275
310
raw = parent .getrawtransaction (txid )
@@ -281,7 +316,7 @@ def run_test(self):
281
316
assert (sidechain .getblockcount () != sidechain2 .getblockcount ())
282
317
283
318
print ("Restarting parent2" )
284
- self .nodes [1 ] = start_node (1 , self .options .tmpdir , self .extra_args [1 ], chain = 'parent' )
319
+ self .nodes [1 ] = start_node (1 , self .options .tmpdir , self .extra_args [1 ], binary = self . binary , chain = self . parent_chain )
285
320
parent2 = self .nodes [1 ]
286
321
connect_nodes_bi (self .nodes , 0 , 1 )
287
322
time .sleep (5 )
0 commit comments