2 # Copyright (c) 2010 ArtForz -- public domain half-a-node
3 # Copyright (c) 2012 Jeff Garzik
4 # Copyright (c) 2010-2017 The Bitcoin Core developers
5 # Distributed under the MIT software license, see the accompanying
6 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 """Bitcoin test framework primitive and message strcutures
9 CBlock, CTransaction, CBlockHeader, CTxIn, CTxOut, etc....:
10 data structures that should map to corresponding structures in
13 msg_block, msg_tx, msg_headers, etc.:
14 data structures that represent network messages
16 ser_*, deser_*: functions that handle serialization/deserialization."""
17 from codecs
import encode
20 from io
import BytesIO
26 from test_framework
.siphash
import siphash256
27 from test_framework
.util
import hex_str_to_bytes
, bytes_to_hex_str
29 MIN_VERSION_SUPPORTED
= 60001
30 MY_VERSION
= 70014 # past bip-31 for ping/pong
31 MY_SUBVERSION
= b
"/python-mininode-tester:0.0.3/"
32 MY_RELAY
= 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
35 MAX_BLOCK_BASE_SIZE
= 1000000
37 COIN
= 100000000 # 1 btc in satoshis
39 NODE_NETWORK
= (1 << 0)
40 # NODE_GETUTXO = (1 << 1)
42 NODE_WITNESS
= (1 << 3)
43 NODE_UNSUPPORTED_SERVICE_BIT_5
= (1 << 5)
44 NODE_UNSUPPORTED_SERVICE_BIT_7
= (1 << 7)
45 NODE_NETWORK_LIMITED
= (1 << 10)
47 # Serialization/deserialization tools
49 return hashlib
.new('sha256', s
).digest()
52 return hashlib
.new('ripemd160', s
).digest()
55 return sha256(sha256(s
))
57 def ser_compact_size(l
):
60 r
= struct
.pack("B", l
)
62 r
= struct
.pack("<BH", 253, l
)
64 r
= struct
.pack("<BI", 254, l
)
66 r
= struct
.pack("<BQ", 255, l
)
69 def deser_compact_size(f
):
70 nit
= struct
.unpack("<B", f
.read(1))[0]
72 nit
= struct
.unpack("<H", f
.read(2))[0]
74 nit
= struct
.unpack("<I", f
.read(4))[0]
76 nit
= struct
.unpack("<Q", f
.read(8))[0]
80 nit
= deser_compact_size(f
)
84 return ser_compact_size(len(s
)) + s
89 t
= struct
.unpack("<I", f
.read(4))[0]
97 rs
+= struct
.pack("<I", u
& 0xFFFFFFFF)
102 def uint256_from_str(s
):
104 t
= struct
.unpack("<IIIIIIII", s
[:32])
106 r
+= t
[i
] << (i
* 32)
110 def uint256_from_compact(c
):
111 nbytes
= (c
>> 24) & 0xFF
112 v
= (c
& 0xFFFFFF) << (8 * (nbytes
- 3))
116 def deser_vector(f
, c
):
117 nit
= deser_compact_size(f
)
126 # ser_function_name: Allow for an alternate serialization function on the
127 # entries in the vector (we use this for serializing the vector of transactions
128 # for a witness block).
129 def ser_vector(l
, ser_function_name
=None):
130 r
= ser_compact_size(len(l
))
132 if ser_function_name
:
133 r
+= getattr(i
, ser_function_name
)()
139 def deser_uint256_vector(f
):
140 nit
= deser_compact_size(f
)
148 def ser_uint256_vector(l
):
149 r
= ser_compact_size(len(l
))
155 def deser_string_vector(f
):
156 nit
= deser_compact_size(f
)
164 def ser_string_vector(l
):
165 r
= ser_compact_size(len(l
))
171 # Deserialize from a hex string representation (eg from RPC)
172 def FromHex(obj
, hex_string
):
173 obj
.deserialize(BytesIO(hex_str_to_bytes(hex_string
)))
176 # Convert a binary-serializable object to hex (eg for submission via RPC)
178 return bytes_to_hex_str(obj
.serialize())
180 # Objects that map to bitcoind objects, which can be serialized/deserialized
185 self
.pchReserved
= b
"\x00" * 10 + b
"\xff" * 2
189 def deserialize(self
, f
):
190 self
.nServices
= struct
.unpack("<Q", f
.read(8))[0]
191 self
.pchReserved
= f
.read(12)
192 self
.ip
= socket
.inet_ntoa(f
.read(4))
193 self
.port
= struct
.unpack(">H", f
.read(2))[0]
197 r
+= struct
.pack("<Q", self
.nServices
)
198 r
+= self
.pchReserved
199 r
+= socket
.inet_aton(self
.ip
)
200 r
+= struct
.pack(">H", self
.port
)
204 return "CAddress(nServices=%i ip=%s port=%i)" % (self
.nServices
,
207 MSG_WITNESS_FLAG
= 1<<30
214 1|MSG_WITNESS_FLAG
: "WitnessTx",
215 2|MSG_WITNESS_FLAG
: "WitnessBlock",
219 def __init__(self
, t
=0, h
=0):
223 def deserialize(self
, f
):
224 self
.type = struct
.unpack("<i", f
.read(4))[0]
225 self
.hash = deser_uint256(f
)
229 r
+= struct
.pack("<i", self
.type)
230 r
+= ser_uint256(self
.hash)
234 return "CInv(type=%s hash=%064x)" \
235 % (self
.typemap
[self
.type], self
.hash)
238 class CBlockLocator():
240 self
.nVersion
= MY_VERSION
243 def deserialize(self
, f
):
244 self
.nVersion
= struct
.unpack("<i", f
.read(4))[0]
245 self
.vHave
= deser_uint256_vector(f
)
249 r
+= struct
.pack("<i", self
.nVersion
)
250 r
+= ser_uint256_vector(self
.vHave
)
254 return "CBlockLocator(nVersion=%i vHave=%s)" \
255 % (self
.nVersion
, repr(self
.vHave
))
259 def __init__(self
, hash=0, n
=0):
263 def deserialize(self
, f
):
264 self
.hash = deser_uint256(f
)
265 self
.n
= struct
.unpack("<I", f
.read(4))[0]
269 r
+= ser_uint256(self
.hash)
270 r
+= struct
.pack("<I", self
.n
)
274 return "COutPoint(hash=%064x n=%i)" % (self
.hash, self
.n
)
278 def __init__(self
, outpoint
=None, scriptSig
=b
"", nSequence
=0):
280 self
.prevout
= COutPoint()
282 self
.prevout
= outpoint
283 self
.scriptSig
= scriptSig
284 self
.nSequence
= nSequence
286 def deserialize(self
, f
):
287 self
.prevout
= COutPoint()
288 self
.prevout
.deserialize(f
)
289 self
.scriptSig
= deser_string(f
)
290 self
.nSequence
= struct
.unpack("<I", f
.read(4))[0]
294 r
+= self
.prevout
.serialize()
295 r
+= ser_string(self
.scriptSig
)
296 r
+= struct
.pack("<I", self
.nSequence
)
300 return "CTxIn(prevout=%s scriptSig=%s nSequence=%i)" \
301 % (repr(self
.prevout
), bytes_to_hex_str(self
.scriptSig
),
306 def __init__(self
, nValue
=0, scriptPubKey
=b
""):
308 self
.scriptPubKey
= scriptPubKey
310 def deserialize(self
, f
):
311 self
.nValue
= struct
.unpack("<q", f
.read(8))[0]
312 self
.scriptPubKey
= deser_string(f
)
316 r
+= struct
.pack("<q", self
.nValue
)
317 r
+= ser_string(self
.scriptPubKey
)
321 return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" \
322 % (self
.nValue
// COIN
, self
.nValue
% COIN
,
323 bytes_to_hex_str(self
.scriptPubKey
))
326 class CScriptWitness():
328 # stack is a vector of strings
332 return "CScriptWitness(%s)" % \
333 (",".join([bytes_to_hex_str(x
) for x
in self
.stack
]))
341 class CTxInWitness():
343 self
.scriptWitness
= CScriptWitness()
345 def deserialize(self
, f
):
346 self
.scriptWitness
.stack
= deser_string_vector(f
)
349 return ser_string_vector(self
.scriptWitness
.stack
)
352 return repr(self
.scriptWitness
)
355 return self
.scriptWitness
.is_null()
362 def deserialize(self
, f
):
363 for i
in range(len(self
.vtxinwit
)):
364 self
.vtxinwit
[i
].deserialize(f
)
368 # This is different than the usual vector serialization --
369 # we omit the length of the vector, which is required to be
370 # the same length as the transaction's vin vector.
371 for x
in self
.vtxinwit
:
376 return "CTxWitness(%s)" % \
377 (';'.join([repr(x
) for x
in self
.vtxinwit
]))
380 for x
in self
.vtxinwit
:
386 class CTransaction():
387 def __init__(self
, tx
=None):
392 self
.wit
= CTxWitness()
397 self
.nVersion
= tx
.nVersion
398 self
.vin
= copy
.deepcopy(tx
.vin
)
399 self
.vout
= copy
.deepcopy(tx
.vout
)
400 self
.nLockTime
= tx
.nLockTime
401 self
.sha256
= tx
.sha256
403 self
.wit
= copy
.deepcopy(tx
.wit
)
405 def deserialize(self
, f
):
406 self
.nVersion
= struct
.unpack("<i", f
.read(4))[0]
407 self
.vin
= deser_vector(f
, CTxIn
)
409 if len(self
.vin
) == 0:
410 flags
= struct
.unpack("<B", f
.read(1))[0]
411 # Not sure why flags can't be zero, but this
412 # matches the implementation in bitcoind
414 self
.vin
= deser_vector(f
, CTxIn
)
415 self
.vout
= deser_vector(f
, CTxOut
)
417 self
.vout
= deser_vector(f
, CTxOut
)
419 self
.wit
.vtxinwit
= [CTxInWitness() for i
in range(len(self
.vin
))]
420 self
.wit
.deserialize(f
)
421 self
.nLockTime
= struct
.unpack("<I", f
.read(4))[0]
425 def serialize_without_witness(self
):
427 r
+= struct
.pack("<i", self
.nVersion
)
428 r
+= ser_vector(self
.vin
)
429 r
+= ser_vector(self
.vout
)
430 r
+= struct
.pack("<I", self
.nLockTime
)
433 # Only serialize with witness when explicitly called for
434 def serialize_with_witness(self
):
436 if not self
.wit
.is_null():
439 r
+= struct
.pack("<i", self
.nVersion
)
442 r
+= ser_vector(dummy
)
443 r
+= struct
.pack("<B", flags
)
444 r
+= ser_vector(self
.vin
)
445 r
+= ser_vector(self
.vout
)
447 if (len(self
.wit
.vtxinwit
) != len(self
.vin
)):
448 # vtxinwit must have the same length as vin
449 self
.wit
.vtxinwit
= self
.wit
.vtxinwit
[:len(self
.vin
)]
450 for i
in range(len(self
.wit
.vtxinwit
), len(self
.vin
)):
451 self
.wit
.vtxinwit
.append(CTxInWitness())
452 r
+= self
.wit
.serialize()
453 r
+= struct
.pack("<I", self
.nLockTime
)
456 # Regular serialization is without witness -- must explicitly
457 # call serialize_with_witness to include witness data.
459 return self
.serialize_without_witness()
461 # Recalculate the txid (transaction hash without witness)
466 # We will only cache the serialization without witness in
467 # self.sha256 and self.hash -- those are expected to be the txid.
468 def calc_sha256(self
, with_witness
=False):
470 # Don't cache the result, just return it
471 return uint256_from_str(hash256(self
.serialize_with_witness()))
473 if self
.sha256
is None:
474 self
.sha256
= uint256_from_str(hash256(self
.serialize_without_witness()))
475 self
.hash = encode(hash256(self
.serialize())[::-1], 'hex_codec').decode('ascii')
479 for tout
in self
.vout
:
480 if tout
.nValue
< 0 or tout
.nValue
> 21000000 * COIN
:
485 return "CTransaction(nVersion=%i vin=%s vout=%s wit=%s nLockTime=%i)" \
486 % (self
.nVersion
, repr(self
.vin
), repr(self
.vout
), repr(self
.wit
), self
.nLockTime
)
489 class CBlockHeader():
490 def __init__(self
, header
=None):
494 self
.nVersion
= header
.nVersion
495 self
.hashPrevBlock
= header
.hashPrevBlock
496 self
.hashMerkleRoot
= header
.hashMerkleRoot
497 self
.nTime
= header
.nTime
498 self
.nBits
= header
.nBits
499 self
.nNonce
= header
.nNonce
500 self
.sha256
= header
.sha256
501 self
.hash = header
.hash
506 self
.hashPrevBlock
= 0
507 self
.hashMerkleRoot
= 0
514 def deserialize(self
, f
):
515 self
.nVersion
= struct
.unpack("<i", f
.read(4))[0]
516 self
.hashPrevBlock
= deser_uint256(f
)
517 self
.hashMerkleRoot
= deser_uint256(f
)
518 self
.nTime
= struct
.unpack("<I", f
.read(4))[0]
519 self
.nBits
= struct
.unpack("<I", f
.read(4))[0]
520 self
.nNonce
= struct
.unpack("<I", f
.read(4))[0]
526 r
+= struct
.pack("<i", self
.nVersion
)
527 r
+= ser_uint256(self
.hashPrevBlock
)
528 r
+= ser_uint256(self
.hashMerkleRoot
)
529 r
+= struct
.pack("<I", self
.nTime
)
530 r
+= struct
.pack("<I", self
.nBits
)
531 r
+= struct
.pack("<I", self
.nNonce
)
534 def calc_sha256(self
):
535 if self
.sha256
is None:
537 r
+= struct
.pack("<i", self
.nVersion
)
538 r
+= ser_uint256(self
.hashPrevBlock
)
539 r
+= ser_uint256(self
.hashMerkleRoot
)
540 r
+= struct
.pack("<I", self
.nTime
)
541 r
+= struct
.pack("<I", self
.nBits
)
542 r
+= struct
.pack("<I", self
.nNonce
)
543 self
.sha256
= uint256_from_str(hash256(r
))
544 self
.hash = encode(hash256(r
)[::-1], 'hex_codec').decode('ascii')
552 return "CBlockHeader(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x)" \
553 % (self
.nVersion
, self
.hashPrevBlock
, self
.hashMerkleRoot
,
554 time
.ctime(self
.nTime
), self
.nBits
, self
.nNonce
)
557 class CBlock(CBlockHeader
):
558 def __init__(self
, header
=None):
559 super(CBlock
, self
).__init
__(header
)
562 def deserialize(self
, f
):
563 super(CBlock
, self
).deserialize(f
)
564 self
.vtx
= deser_vector(f
, CTransaction
)
566 def serialize(self
, with_witness
=False):
568 r
+= super(CBlock
, self
).serialize()
570 r
+= ser_vector(self
.vtx
, "serialize_with_witness")
572 r
+= ser_vector(self
.vtx
)
575 # Calculate the merkle root given a vector of transaction hashes
577 def get_merkle_root(cls
, hashes
):
578 while len(hashes
) > 1:
580 for i
in range(0, len(hashes
), 2):
581 i2
= min(i
+1, len(hashes
)-1)
582 newhashes
.append(hash256(hashes
[i
] + hashes
[i2
]))
584 return uint256_from_str(hashes
[0])
586 def calc_merkle_root(self
):
590 hashes
.append(ser_uint256(tx
.sha256
))
591 return self
.get_merkle_root(hashes
)
593 def calc_witness_merkle_root(self
):
594 # For witness root purposes, the hash of the
595 # coinbase, with witness, is defined to be 0...0
596 hashes
= [ser_uint256(0)]
598 for tx
in self
.vtx
[1:]:
599 # Calculate the hashes with witness data
600 hashes
.append(ser_uint256(tx
.calc_sha256(True)))
602 return self
.get_merkle_root(hashes
)
606 target
= uint256_from_compact(self
.nBits
)
607 if self
.sha256
> target
:
610 if not tx
.is_valid():
612 if self
.calc_merkle_root() != self
.hashMerkleRoot
:
618 target
= uint256_from_compact(self
.nBits
)
619 while self
.sha256
> target
:
624 return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" \
625 % (self
.nVersion
, self
.hashPrevBlock
, self
.hashMerkleRoot
,
626 time
.ctime(self
.nTime
), self
.nBits
, self
.nNonce
, repr(self
.vtx
))
629 class PrefilledTransaction():
630 def __init__(self
, index
=0, tx
= None):
634 def deserialize(self
, f
):
635 self
.index
= deser_compact_size(f
)
636 self
.tx
= CTransaction()
637 self
.tx
.deserialize(f
)
639 def serialize(self
, with_witness
=False):
641 r
+= ser_compact_size(self
.index
)
643 r
+= self
.tx
.serialize_with_witness()
645 r
+= self
.tx
.serialize_without_witness()
648 def serialize_with_witness(self
):
649 return self
.serialize(with_witness
=True)
652 return "PrefilledTransaction(index=%d, tx=%s)" % (self
.index
, repr(self
.tx
))
654 # This is what we send on the wire, in a cmpctblock message.
655 class P2PHeaderAndShortIDs():
657 self
.header
= CBlockHeader()
659 self
.shortids_length
= 0
661 self
.prefilled_txn_length
= 0
662 self
.prefilled_txn
= []
664 def deserialize(self
, f
):
665 self
.header
.deserialize(f
)
666 self
.nonce
= struct
.unpack("<Q", f
.read(8))[0]
667 self
.shortids_length
= deser_compact_size(f
)
668 for i
in range(self
.shortids_length
):
669 # shortids are defined to be 6 bytes in the spec, so append
670 # two zero bytes and read it in as an 8-byte number
671 self
.shortids
.append(struct
.unpack("<Q", f
.read(6) + b
'\x00\x00')[0])
672 self
.prefilled_txn
= deser_vector(f
, PrefilledTransaction
)
673 self
.prefilled_txn_length
= len(self
.prefilled_txn
)
675 # When using version 2 compact blocks, we must serialize with_witness.
676 def serialize(self
, with_witness
=False):
678 r
+= self
.header
.serialize()
679 r
+= struct
.pack("<Q", self
.nonce
)
680 r
+= ser_compact_size(self
.shortids_length
)
681 for x
in self
.shortids
:
682 # We only want the first 6 bytes
683 r
+= struct
.pack("<Q", x
)[0:6]
685 r
+= ser_vector(self
.prefilled_txn
, "serialize_with_witness")
687 r
+= ser_vector(self
.prefilled_txn
)
691 return "P2PHeaderAndShortIDs(header=%s, nonce=%d, shortids_length=%d, shortids=%s, prefilled_txn_length=%d, prefilledtxn=%s" % (repr(self
.header
), self
.nonce
, self
.shortids_length
, repr(self
.shortids
), self
.prefilled_txn_length
, repr(self
.prefilled_txn
))
693 # P2P version of the above that will use witness serialization (for compact
695 class P2PHeaderAndShortWitnessIDs(P2PHeaderAndShortIDs
):
697 return super(P2PHeaderAndShortWitnessIDs
, self
).serialize(with_witness
=True)
699 # Calculate the BIP 152-compact blocks shortid for a given transaction hash
700 def calculate_shortid(k0
, k1
, tx_hash
):
701 expected_shortid
= siphash256(k0
, k1
, tx_hash
)
702 expected_shortid
&= 0x0000ffffffffffff
703 return expected_shortid
705 # This version gets rid of the array lengths, and reinterprets the differential
706 # encoding into indices that can be used for lookup.
707 class HeaderAndShortIDs():
708 def __init__(self
, p2pheaders_and_shortids
= None):
709 self
.header
= CBlockHeader()
712 self
.prefilled_txn
= []
713 self
.use_witness
= False
715 if p2pheaders_and_shortids
!= None:
716 self
.header
= p2pheaders_and_shortids
.header
717 self
.nonce
= p2pheaders_and_shortids
.nonce
718 self
.shortids
= p2pheaders_and_shortids
.shortids
720 for x
in p2pheaders_and_shortids
.prefilled_txn
:
721 self
.prefilled_txn
.append(PrefilledTransaction(x
.index
+ last_index
+ 1, x
.tx
))
722 last_index
= self
.prefilled_txn
[-1].index
726 ret
= P2PHeaderAndShortWitnessIDs()
728 ret
= P2PHeaderAndShortIDs()
729 ret
.header
= self
.header
730 ret
.nonce
= self
.nonce
731 ret
.shortids_length
= len(self
.shortids
)
732 ret
.shortids
= self
.shortids
733 ret
.prefilled_txn_length
= len(self
.prefilled_txn
)
734 ret
.prefilled_txn
= []
736 for x
in self
.prefilled_txn
:
737 ret
.prefilled_txn
.append(PrefilledTransaction(x
.index
- last_index
- 1, x
.tx
))
741 def get_siphash_keys(self
):
742 header_nonce
= self
.header
.serialize()
743 header_nonce
+= struct
.pack("<Q", self
.nonce
)
744 hash_header_nonce_as_str
= sha256(header_nonce
)
745 key0
= struct
.unpack("<Q", hash_header_nonce_as_str
[0:8])[0]
746 key1
= struct
.unpack("<Q", hash_header_nonce_as_str
[8:16])[0]
747 return [ key0
, key1
]
749 # Version 2 compact blocks use wtxid in shortids (rather than txid)
750 def initialize_from_block(self
, block
, nonce
=0, prefill_list
= [0], use_witness
= False):
751 self
.header
= CBlockHeader(block
)
753 self
.prefilled_txn
= [ PrefilledTransaction(i
, block
.vtx
[i
]) for i
in prefill_list
]
755 self
.use_witness
= use_witness
756 [k0
, k1
] = self
.get_siphash_keys()
757 for i
in range(len(block
.vtx
)):
758 if i
not in prefill_list
:
759 tx_hash
= block
.vtx
[i
].sha256
761 tx_hash
= block
.vtx
[i
].calc_sha256(with_witness
=True)
762 self
.shortids
.append(calculate_shortid(k0
, k1
, tx_hash
))
765 return "HeaderAndShortIDs(header=%s, nonce=%d, shortids=%s, prefilledtxn=%s" % (repr(self
.header
), self
.nonce
, repr(self
.shortids
), repr(self
.prefilled_txn
))
768 class BlockTransactionsRequest():
770 def __init__(self
, blockhash
=0, indexes
= None):
771 self
.blockhash
= blockhash
772 self
.indexes
= indexes
if indexes
!= None else []
774 def deserialize(self
, f
):
775 self
.blockhash
= deser_uint256(f
)
776 indexes_length
= deser_compact_size(f
)
777 for i
in range(indexes_length
):
778 self
.indexes
.append(deser_compact_size(f
))
782 r
+= ser_uint256(self
.blockhash
)
783 r
+= ser_compact_size(len(self
.indexes
))
784 for x
in self
.indexes
:
785 r
+= ser_compact_size(x
)
788 # helper to set the differentially encoded indexes from absolute ones
789 def from_absolute(self
, absolute_indexes
):
792 for x
in absolute_indexes
:
793 self
.indexes
.append(x
-last_index
-1)
796 def to_absolute(self
):
797 absolute_indexes
= []
799 for x
in self
.indexes
:
800 absolute_indexes
.append(x
+last_index
+1)
801 last_index
= absolute_indexes
[-1]
802 return absolute_indexes
805 return "BlockTransactionsRequest(hash=%064x indexes=%s)" % (self
.blockhash
, repr(self
.indexes
))
808 class BlockTransactions():
810 def __init__(self
, blockhash
=0, transactions
= None):
811 self
.blockhash
= blockhash
812 self
.transactions
= transactions
if transactions
!= None else []
814 def deserialize(self
, f
):
815 self
.blockhash
= deser_uint256(f
)
816 self
.transactions
= deser_vector(f
, CTransaction
)
818 def serialize(self
, with_witness
=False):
820 r
+= ser_uint256(self
.blockhash
)
822 r
+= ser_vector(self
.transactions
, "serialize_with_witness")
824 r
+= ser_vector(self
.transactions
)
828 return "BlockTransactions(hash=%064x transactions=%s)" % (self
.blockhash
, repr(self
.transactions
))
831 # Objects that correspond to messages on the wire
836 self
.nVersion
= MY_VERSION
837 self
.nServices
= NODE_NETWORK | NODE_WITNESS
838 self
.nTime
= int(time
.time())
839 self
.addrTo
= CAddress()
840 self
.addrFrom
= CAddress()
841 self
.nNonce
= random
.getrandbits(64)
842 self
.strSubVer
= MY_SUBVERSION
843 self
.nStartingHeight
= -1
844 self
.nRelay
= MY_RELAY
846 def deserialize(self
, f
):
847 self
.nVersion
= struct
.unpack("<i", f
.read(4))[0]
848 if self
.nVersion
== 10300:
850 self
.nServices
= struct
.unpack("<Q", f
.read(8))[0]
851 self
.nTime
= struct
.unpack("<q", f
.read(8))[0]
852 self
.addrTo
= CAddress()
853 self
.addrTo
.deserialize(f
)
855 if self
.nVersion
>= 106:
856 self
.addrFrom
= CAddress()
857 self
.addrFrom
.deserialize(f
)
858 self
.nNonce
= struct
.unpack("<Q", f
.read(8))[0]
859 self
.strSubVer
= deser_string(f
)
863 self
.strSubVer
= None
864 self
.nStartingHeight
= None
866 if self
.nVersion
>= 209:
867 self
.nStartingHeight
= struct
.unpack("<i", f
.read(4))[0]
869 self
.nStartingHeight
= None
871 if self
.nVersion
>= 70001:
872 # Relay field is optional for version 70001 onwards
874 self
.nRelay
= struct
.unpack("<b", f
.read(1))[0]
882 r
+= struct
.pack("<i", self
.nVersion
)
883 r
+= struct
.pack("<Q", self
.nServices
)
884 r
+= struct
.pack("<q", self
.nTime
)
885 r
+= self
.addrTo
.serialize()
886 r
+= self
.addrFrom
.serialize()
887 r
+= struct
.pack("<Q", self
.nNonce
)
888 r
+= ser_string(self
.strSubVer
)
889 r
+= struct
.pack("<i", self
.nStartingHeight
)
890 r
+= struct
.pack("<b", self
.nRelay
)
894 return 'msg_version(nVersion=%i nServices=%i nTime=%s addrTo=%s addrFrom=%s nNonce=0x%016X strSubVer=%s nStartingHeight=%i nRelay=%i)' \
895 % (self
.nVersion
, self
.nServices
, time
.ctime(self
.nTime
),
896 repr(self
.addrTo
), repr(self
.addrFrom
), self
.nNonce
,
897 self
.strSubVer
, self
.nStartingHeight
, self
.nRelay
)
906 def deserialize(self
, f
):
913 return "msg_verack()"
922 def deserialize(self
, f
):
923 self
.addrs
= deser_vector(f
, CAddress
)
926 return ser_vector(self
.addrs
)
929 return "msg_addr(addrs=%s)" % (repr(self
.addrs
))
935 def __init__(self
, inv
=None):
941 def deserialize(self
, f
):
942 self
.inv
= deser_vector(f
, CInv
)
945 return ser_vector(self
.inv
)
948 return "msg_inv(inv=%s)" % (repr(self
.inv
))
954 def __init__(self
, inv
=None):
955 self
.inv
= inv
if inv
!= None else []
957 def deserialize(self
, f
):
958 self
.inv
= deser_vector(f
, CInv
)
961 return ser_vector(self
.inv
)
964 return "msg_getdata(inv=%s)" % (repr(self
.inv
))
967 class msg_getblocks():
968 command
= b
"getblocks"
971 self
.locator
= CBlockLocator()
974 def deserialize(self
, f
):
975 self
.locator
= CBlockLocator()
976 self
.locator
.deserialize(f
)
977 self
.hashstop
= deser_uint256(f
)
981 r
+= self
.locator
.serialize()
982 r
+= ser_uint256(self
.hashstop
)
986 return "msg_getblocks(locator=%s hashstop=%064x)" \
987 % (repr(self
.locator
), self
.hashstop
)
993 def __init__(self
, tx
=CTransaction()):
996 def deserialize(self
, f
):
997 self
.tx
.deserialize(f
)
1000 return self
.tx
.serialize_without_witness()
1003 return "msg_tx(tx=%s)" % (repr(self
.tx
))
1005 class msg_witness_tx(msg_tx
):
1007 def serialize(self
):
1008 return self
.tx
.serialize_with_witness()
1014 def __init__(self
, block
=None):
1016 self
.block
= CBlock()
1020 def deserialize(self
, f
):
1021 self
.block
.deserialize(f
)
1023 def serialize(self
):
1024 return self
.block
.serialize()
1027 return "msg_block(block=%s)" % (repr(self
.block
))
1029 # for cases where a user needs tighter control over what is sent over the wire
1030 # note that the user must supply the name of the command, and the data
1031 class msg_generic():
1032 def __init__(self
, command
, data
=None):
1033 self
.command
= command
1036 def serialize(self
):
1040 return "msg_generic()"
1042 class msg_witness_block(msg_block
):
1044 def serialize(self
):
1045 r
= self
.block
.serialize(with_witness
=True)
1048 class msg_getaddr():
1049 command
= b
"getaddr"
1054 def deserialize(self
, f
):
1057 def serialize(self
):
1061 return "msg_getaddr()"
1067 def __init__(self
, nonce
=0):
1070 def deserialize(self
, f
):
1071 self
.nonce
= struct
.unpack("<Q", f
.read(8))[0]
1073 def serialize(self
):
1075 r
+= struct
.pack("<Q", self
.nonce
)
1079 return "msg_ping(nonce=%08x)" % self
.nonce
1085 def __init__(self
, nonce
=0):
1088 def deserialize(self
, f
):
1089 self
.nonce
= struct
.unpack("<Q", f
.read(8))[0]
1091 def serialize(self
):
1093 r
+= struct
.pack("<Q", self
.nonce
)
1097 return "msg_pong(nonce=%08x)" % self
.nonce
1100 class msg_mempool():
1101 command
= b
"mempool"
1106 def deserialize(self
, f
):
1109 def serialize(self
):
1113 return "msg_mempool()"
1115 class msg_sendheaders():
1116 command
= b
"sendheaders"
1121 def deserialize(self
, f
):
1124 def serialize(self
):
1128 return "msg_sendheaders()"
1131 # getheaders message has
1134 # hash_stop (hash of last desired block header, 0 to get as many as possible)
1135 class msg_getheaders():
1136 command
= b
"getheaders"
1139 self
.locator
= CBlockLocator()
1142 def deserialize(self
, f
):
1143 self
.locator
= CBlockLocator()
1144 self
.locator
.deserialize(f
)
1145 self
.hashstop
= deser_uint256(f
)
1147 def serialize(self
):
1149 r
+= self
.locator
.serialize()
1150 r
+= ser_uint256(self
.hashstop
)
1154 return "msg_getheaders(locator=%s, stop=%064x)" \
1155 % (repr(self
.locator
), self
.hashstop
)
1158 # headers message has
1159 # <count> <vector of block headers>
1160 class msg_headers():
1161 command
= b
"headers"
1163 def __init__(self
, headers
=None):
1164 self
.headers
= headers
if headers
is not None else []
1166 def deserialize(self
, f
):
1167 # comment in bitcoind indicates these should be deserialized as blocks
1168 blocks
= deser_vector(f
, CBlock
)
1170 self
.headers
.append(CBlockHeader(x
))
1172 def serialize(self
):
1173 blocks
= [CBlock(x
) for x
in self
.headers
]
1174 return ser_vector(blocks
)
1177 return "msg_headers(headers=%s)" % repr(self
.headers
)
1182 REJECT_MALFORMED
= 1
1190 def deserialize(self
, f
):
1191 self
.message
= deser_string(f
)
1192 self
.code
= struct
.unpack("<B", f
.read(1))[0]
1193 self
.reason
= deser_string(f
)
1194 if (self
.code
!= self
.REJECT_MALFORMED
and
1195 (self
.message
== b
"block" or self
.message
== b
"tx")):
1196 self
.data
= deser_uint256(f
)
1198 def serialize(self
):
1199 r
= ser_string(self
.message
)
1200 r
+= struct
.pack("<B", self
.code
)
1201 r
+= ser_string(self
.reason
)
1202 if (self
.code
!= self
.REJECT_MALFORMED
and
1203 (self
.message
== b
"block" or self
.message
== b
"tx")):
1204 r
+= ser_uint256(self
.data
)
1208 return "msg_reject: %s %d %s [%064x]" \
1209 % (self
.message
, self
.code
, self
.reason
, self
.data
)
1211 class msg_feefilter():
1212 command
= b
"feefilter"
1214 def __init__(self
, feerate
=0):
1215 self
.feerate
= feerate
1217 def deserialize(self
, f
):
1218 self
.feerate
= struct
.unpack("<Q", f
.read(8))[0]
1220 def serialize(self
):
1222 r
+= struct
.pack("<Q", self
.feerate
)
1226 return "msg_feefilter(feerate=%08x)" % self
.feerate
1228 class msg_sendcmpct():
1229 command
= b
"sendcmpct"
1232 self
.announce
= False
1235 def deserialize(self
, f
):
1236 self
.announce
= struct
.unpack("<?", f
.read(1))[0]
1237 self
.version
= struct
.unpack("<Q", f
.read(8))[0]
1239 def serialize(self
):
1241 r
+= struct
.pack("<?", self
.announce
)
1242 r
+= struct
.pack("<Q", self
.version
)
1246 return "msg_sendcmpct(announce=%s, version=%lu)" % (self
.announce
, self
.version
)
1248 class msg_cmpctblock():
1249 command
= b
"cmpctblock"
1251 def __init__(self
, header_and_shortids
= None):
1252 self
.header_and_shortids
= header_and_shortids
1254 def deserialize(self
, f
):
1255 self
.header_and_shortids
= P2PHeaderAndShortIDs()
1256 self
.header_and_shortids
.deserialize(f
)
1258 def serialize(self
):
1260 r
+= self
.header_and_shortids
.serialize()
1264 return "msg_cmpctblock(HeaderAndShortIDs=%s)" % repr(self
.header_and_shortids
)
1266 class msg_getblocktxn():
1267 command
= b
"getblocktxn"
1270 self
.block_txn_request
= None
1272 def deserialize(self
, f
):
1273 self
.block_txn_request
= BlockTransactionsRequest()
1274 self
.block_txn_request
.deserialize(f
)
1276 def serialize(self
):
1278 r
+= self
.block_txn_request
.serialize()
1282 return "msg_getblocktxn(block_txn_request=%s)" % (repr(self
.block_txn_request
))
1284 class msg_blocktxn():
1285 command
= b
"blocktxn"
1288 self
.block_transactions
= BlockTransactions()
1290 def deserialize(self
, f
):
1291 self
.block_transactions
.deserialize(f
)
1293 def serialize(self
):
1295 r
+= self
.block_transactions
.serialize()
1299 return "msg_blocktxn(block_transactions=%s)" % (repr(self
.block_transactions
))
1301 class msg_witness_blocktxn(msg_blocktxn
):
1302 def serialize(self
):
1304 r
+= self
.block_transactions
.serialize(with_witness
=True)