55from test_framework .test_framework import BitcoinTestFramework
66from test_framework .util import assert_equal
77from test_framework import (
8- address ,
98 key ,
109)
1110from test_framework .messages import (
1211 CBlock ,
1312 from_hex ,
13+ ser_uint256 ,
1414)
1515from test_framework .script import (
1616 OP_NOP ,
1717 OP_RETURN ,
18- CScript
18+ CScript ,
19+ SIGHASH_ALL ,
1920)
2021
21- # Generate wallet import format from private key.
22- def wif (pk ):
23- # Base58Check version for regtest WIF keys is 0xef = 239
24- pk_compressed = pk + bytes ([0x1 ])
25- return address .byte_to_base58 (pk_compressed , 239 )
26-
2722# The signblockscript is a Bitcoin Script k-of-n multisig script.
2823def make_signblockscript (num_nodes , required_signers , keys ):
2924 assert num_nodes >= required_signers
@@ -39,21 +34,17 @@ def make_signblockscript(num_nodes, required_signers, keys):
3934class TrimHeadersTest (BitcoinTestFramework ):
4035 def skip_test_if_missing_module (self ):
4136 self .skip_if_no_wallet ()
42- self .skip_if_no_bdb ()
4337
4438 def add_options (self , parser ):
4539 self .add_wallet_options (parser )
4640
4741 # Dynamically generate N keys to be used for block signing.
4842 def init_keys (self , num_keys ):
4943 self .keys = []
50- self .wifs = []
5144 for i in range (num_keys ):
5245 k = key .ECKey ()
5346 k .generate ()
54- w = wif (k .get_bytes ())
5547 self .keys .append (k )
56- self .wifs .append (w )
5748
5849 def set_test_params (self ):
5950 self .num_nodes = 3
@@ -92,6 +83,21 @@ def check_height(self, expected_height, all=False, verbose=True):
9283 else :
9384 assert_equal (self .nodes [0 ].getblockcount (), expected_height )
9485
86+ # The signblock RPC only works on legacy (BDB) wallets.
87+ # Sign the block header hash directly with the key instead,
88+ # then use combineblocksigs (a non-wallet RPC).
89+ def sign_block (self , key , block ):
90+ block .rehash ()
91+ msg = ser_uint256 (block .sha256 )
92+ sig = key .sign_ecdsa (msg )
93+ if not block .m_dynafed_params .is_null ():
94+ sig += bytes ([SIGHASH_ALL ])
95+
96+ return [{
97+ "pubkey" : key .get_pubkey ().get_bytes ().hex (),
98+ "sig" : sig .hex (),
99+ }]
100+
95101 def mine_block (self , make_transactions ):
96102 # alternate mining between the signing nodes
97103 mineridx = self .nodes [0 ].getblockcount () % self .required_signers # assuming in sync
@@ -141,7 +147,7 @@ def mine_block(self, make_transactions):
141147 sigs = []
142148 for i in range (self .num_keys ):
143149 result = miner .combineblocksigs (block , sigs , self .witnessScript )
144- sigs = sigs + self .nodes [i ]. signblock ( block , self . witnessScript )
150+ sigs = sigs + self .sign_block ( self . keys [i ], block_struct )
145151 assert_equal (result ["complete" ], i >= self .required_signers )
146152 # submitting should have no effect pre-threshhold
147153 if i < self .required_signers :
@@ -171,7 +177,7 @@ def mine_large_blocks(self, n):
171177 block .solve ()
172178 h = block .serialize ().hex ()
173179
174- sigs = node . signblock ( h , self .witnessScript )
180+ sigs = self . sign_block ( self .keys [ 0 ], block )
175181
176182 result = node .combineblocksigs (h , sigs , self .witnessScript )
177183 assert_equal (result ["complete" ], True )
@@ -180,9 +186,6 @@ def mine_large_blocks(self, n):
180186
181187
182188 def run_test (self ):
183- for i in range (self .num_keys ):
184- self .nodes [i ].importprivkey (self .wifs [i ])
185-
186189 expected_height = 0
187190 self .check_height (expected_height , all = True )
188191
0 commit comments