10
10
script_to_p2sh_p2wsh ,
11
11
script_to_p2wsh ,
12
12
)
13
- from .mininode import *
13
+ from .messages import (
14
+ CBlock ,
15
+ COIN ,
16
+ COutPoint ,
17
+ CTransaction ,
18
+ CTxIn ,
19
+ CTxInWitness ,
20
+ CTxOut ,
21
+ FromHex ,
22
+ ToHex ,
23
+ bytes_to_hex_str ,
24
+ hash256 ,
25
+ hex_str_to_bytes ,
26
+ ser_string ,
27
+ ser_uint256 ,
28
+ sha256 ,
29
+ uint256_from_str ,
30
+ )
14
31
from .script import (
15
32
CScript ,
16
33
OP_0 ,
24
41
from .util import assert_equal
25
42
26
43
# Create a block (with regtest difficulty)
27
- def create_block (hashprev , coinbase , nTime = None ):
44
+ def create_block (hashprev , coinbase , ntime = None ):
28
45
block = CBlock ()
29
- if nTime is None :
46
+ if ntime is None :
30
47
import time
31
- block .nTime = int (time .time ()+ 600 )
48
+ block .nTime = int (time .time () + 600 )
32
49
else :
33
- block .nTime = nTime
50
+ block .nTime = ntime
34
51
block .hashPrevBlock = hashprev
35
- block .nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
52
+ block .nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
36
53
block .vtx .append (coinbase )
37
54
block .hashMerkleRoot = block .calc_merkle_root ()
38
55
block .calc_sha256 ()
@@ -43,7 +60,7 @@ def create_block(hashprev, coinbase, nTime=None):
43
60
44
61
45
62
def get_witness_script (witness_root , witness_nonce ):
46
- witness_commitment = uint256_from_str (hash256 (ser_uint256 (witness_root )+ ser_uint256 (witness_nonce )))
63
+ witness_commitment = uint256_from_str (hash256 (ser_uint256 (witness_root ) + ser_uint256 (witness_nonce )))
47
64
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256 (witness_commitment )
48
65
return CScript ([OP_RETURN , output_data ])
49
66
@@ -84,52 +101,52 @@ def serialize_script_num(value):
84
101
# Create a coinbase transaction, assuming no miner fees.
85
102
# If pubkey is passed in, the coinbase output will be a P2PK output;
86
103
# otherwise an anyone-can-spend output.
87
- def create_coinbase (height , pubkey = None ):
104
+ def create_coinbase (height , pubkey = None ):
88
105
coinbase = CTransaction ()
89
106
coinbase .vin .append (CTxIn (COutPoint (0 , 0xffffffff ),
90
- ser_string (serialize_script_num (height )), 0xffffffff ))
107
+ ser_string (serialize_script_num (height )), 0xffffffff ))
91
108
coinbaseoutput = CTxOut ()
92
109
coinbaseoutput .nValue = 50 * COIN
93
- halvings = int (height / 150 ) # regtest
110
+ halvings = int (height / 150 ) # regtest
94
111
coinbaseoutput .nValue >>= halvings
95
- if (pubkey != None ):
112
+ if (pubkey is not None ):
96
113
coinbaseoutput .scriptPubKey = CScript ([pubkey , OP_CHECKSIG ])
97
114
else :
98
115
coinbaseoutput .scriptPubKey = CScript ([OP_TRUE ])
99
- coinbase .vout = [ coinbaseoutput ]
116
+ coinbase .vout = [coinbaseoutput ]
100
117
coinbase .calc_sha256 ()
101
118
return coinbase
102
119
103
120
# Create a transaction.
104
- # If the scriptPubKey is not specified, make it anyone-can-spend.
105
- def create_transaction (prevtx , n , sig , value , scriptPubKey = CScript ()):
121
+ # If the script_pub_key is not specified, make it anyone-can-spend.
122
+ def create_transaction (prevtx , n , sig , value , script_pub_key = CScript ()):
106
123
tx = CTransaction ()
107
124
assert (n < len (prevtx .vout ))
108
125
tx .vin .append (CTxIn (COutPoint (prevtx .sha256 , n ), sig , 0xffffffff ))
109
- tx .vout .append (CTxOut (value , scriptPubKey ))
126
+ tx .vout .append (CTxOut (value , script_pub_key ))
110
127
tx .calc_sha256 ()
111
128
return tx
112
129
113
- def get_legacy_sigopcount_block (block , fAccurate = True ):
130
+ def get_legacy_sigopcount_block (block , accurate = True ):
114
131
count = 0
115
132
for tx in block .vtx :
116
- count += get_legacy_sigopcount_tx (tx , fAccurate )
133
+ count += get_legacy_sigopcount_tx (tx , accurate )
117
134
return count
118
135
119
- def get_legacy_sigopcount_tx (tx , fAccurate = True ):
136
+ def get_legacy_sigopcount_tx (tx , accurate = True ):
120
137
count = 0
121
138
for i in tx .vout :
122
- count += i .scriptPubKey .GetSigOpCount (fAccurate )
139
+ count += i .scriptPubKey .GetSigOpCount (accurate )
123
140
for j in tx .vin :
124
141
# scriptSig might be of type bytes, so convert to CScript for the moment
125
- count += CScript (j .scriptSig ).GetSigOpCount (fAccurate )
142
+ count += CScript (j .scriptSig ).GetSigOpCount (accurate )
126
143
return count
127
144
128
145
# Create a scriptPubKey corresponding to either a P2WPKH output for the
129
146
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
130
147
# pubkey. Returns the hex encoding of the scriptPubKey.
131
148
def witness_script (use_p2wsh , pubkey ):
132
- if ( use_p2wsh == False ) :
149
+ if not use_p2wsh :
133
150
# P2WPKH instead
134
151
pubkeyhash = hash160 (hex_str_to_bytes (pubkey ))
135
152
pkscript = CScript ([OP_0 , pubkeyhash ])
0 commit comments