Skip to content

Commit bc45a2f

Browse files
gladcowUdjinM6
authored andcommitted
Backport compact blocks functionality from bitcoin (#1966)
* Merge bitcoin#8068: Compact Blocks 48efec8 Fix some minor compact block issues that came up in review (Matt Corallo) ccd06b9 Elaborate bucket size math (Pieter Wuille) 0d4cb48 Use vTxHashes to optimize InitData significantly (Matt Corallo) 8119026 Provide a flat list of txid/terators to txn in CTxMemPool (Matt Corallo) 678ee97 Add BIP 152 to implemented BIPs list (Matt Corallo) 56ba516 Add reconstruction debug logging (Matt Corallo) 2f34a2e Get our "best three" peers to announce blocks using cmpctblocks (Matt Corallo) 927f8ee Add ability to fetch CNode by NodeId (Matt Corallo) d25cd3e Add receiver-side protocol implementation for CMPCTBLOCK stuff (Matt Corallo) 9c837d5 Add sender-side protocol implementation for CMPCTBLOCK stuff (Matt Corallo) 00c4078 Add protocol messages for short-ids blocks (Matt Corallo) e3b2222 Add some blockencodings tests (Matt Corallo) f4f8f14 Add TestMemPoolEntryHelper::FromTx version for CTransaction (Matt Corallo) 85ad31e Add partial-block block encodings API (Matt Corallo) 5249dac Add COMPACTSIZE wrapper similar to VARINT for serialization (Matt Corallo) cbda71c Move context-required checks from CheckBlockHeader to Contextual... (Matt Corallo) 7c29ec9 If AcceptBlockHeader returns true, pindex will be set. (Matt Corallo) 96806c3 Stop trimming when mapTx is empty (Pieter Wuille) * Merge bitcoin#8408: Prevent fingerprinting, disk-DoS with compact blocks 1d06e49 Ignore CMPCTBLOCK messages for pruned blocks (Suhas Daftuar) 1de2a46 Ignore GETBLOCKTXN requests for unknown blocks (Suhas Daftuar) * Merge bitcoin#8418: Add tests for compact blocks 45c7ddd Add p2p test for BIP 152 (compact blocks) (Suhas Daftuar) 9a22a6c Add support for compactblocks to mininode (Suhas Daftuar) a8689fd Tests: refactor compact size serialization in mininode (Suhas Daftuar) 9c8593d Implement SipHash in Python (Pieter Wuille) 56c87e9 Allow changing BIP9 parameters on regtest (Suhas Daftuar) * Merge bitcoin#8505: Trivial: Fix typos in various files 1aacfc2 various typos (leijurv) * Merge bitcoin#8449: [Trivial] Do not shadow local variable, cleanup a159f25 Remove redundand (and shadowing) declaration (Pavel Janík) cce3024 Do not shadow local variable, cleanup (Pavel Janík) * Merge bitcoin#8739: [qa] Fix broken sendcmpct test in p2p-compactblocks.py 157254a Fix broken sendcmpct test in p2p-compactblocks.py (Suhas Daftuar) * Merge bitcoin#8854: [qa] Fix race condition in p2p-compactblocks test b5fd666 [qa] Fix race condition in p2p-compactblocks test (Suhas Daftuar) * Merge bitcoin#8393: Support for compact blocks together with segwit 27acfc1 [qa] Update p2p-compactblocks.py for compactblocks v2 (Suhas Daftuar) 422fac6 [qa] Add support for compactblocks v2 to mininode (Suhas Daftuar) f5b9b8f [qa] Fix bug in mininode witness deserialization (Suhas Daftuar) 6aa28ab Use cmpctblock type 2 for segwit-enabled transfer (Pieter Wuille) be7555f Fix overly-prescriptive p2p-segwit test for new fetch logic (Matt Corallo) 06128da Make GetFetchFlags always request witness objects from witness peers (Matt Corallo) * Merge bitcoin#8882: [qa] Fix race conditions in p2p-compactblocks.py and sendheaders.py b55d941 [qa] Fix race condition in sendheaders.py (Suhas Daftuar) 6976db2 [qa] Another attempt to fix race condition in p2p-compactblocks.py (Suhas Daftuar) * Merge bitcoin#8904: [qa] Fix compact block shortids for a test case 4cdece4 [qa] Fix compact block shortids for a test case (Dagur Valberg Johannsson) * Merge bitcoin#8637: Compact Block Tweaks (rebase of bitcoin#8235) 3ac6de0 Align constant names for maximum compact block / blocktxn depth (Pieter Wuille) b2e93a3 Add cmpctblock to debug help list (instagibbs) fe998e9 More agressively filter compact block requests (Matt Corallo) 02a337d Dont remove a "preferred" cmpctblock peer if they provide a block (Matt Corallo) * Merge bitcoin#8975: Chainparams: Trivial: In AppInit2(), s/Params()/chainparams/ 6f2f639 Chainparams: Trivial: In AppInit2(), s/Params()/chainparams/ (Jorge Timón) * Merge bitcoin#8968: Don't hold cs_main when calling ProcessNewBlock from a cmpctblock 72ca7d9 Don't hold cs_main when calling ProcessNewBlock from a cmpctblock (Matt Corallo) * Merge bitcoin#8995: Add missing cs_main lock to ::GETBLOCKTXN processing dfe7906 Add missing cs_main lock to ::GETBLOCKTXN processing (Matt Corallo) * Merge bitcoin#8515: A few mempool removal optimizations 0334430 Add some missing includes (Pieter Wuille) 4100499 Return shared_ptr<CTransaction> from mempool removes (Pieter Wuille) 51f2783 Make removed and conflicted arguments optional to remove (Pieter Wuille) f48211b Bypass removeRecursive in removeForReorg (Pieter Wuille) * Merge bitcoin#9026: Fix handling of invalid compact blocks d4833ff Bump the protocol version to distinguish new banning behavior. (Suhas Daftuar) 88c3549 Fix compact block handling to not ban if block is invalid (Suhas Daftuar) c93beac [qa] Test that invalid compactblocks don't result in ban (Suhas Daftuar) * Merge bitcoin#9039: Various serialization simplifcations and optimizations d59a518 Use fixed preallocation instead of costly GetSerializeSize (Pieter Wuille) 25a211a Add optimized CSizeComputer serializers (Pieter Wuille) a2929a2 Make CSerAction's ForRead() constexpr (Pieter Wuille) a603925 Avoid -Wshadow errors (Pieter Wuille) 5284721 Get rid of nType and nVersion (Pieter Wuille) 657e05a Make GetSerializeSize a wrapper on top of CSizeComputer (Pieter Wuille) fad9b66 Make nType and nVersion private and sometimes const (Pieter Wuille) c2c5d42 Make streams' read and write return void (Pieter Wuille) 50e8a9c Remove unused ReadVersion and WriteVersion (Pieter Wuille) * Merge bitcoin#9058: Fixes for p2p-compactblocks.py test timeouts on travis (bitcoin#8842) dac53b5 Modify getblocktxn handler not to drop requests for old blocks (Russell Yanofsky) 55bfddc [qa] Fix stale data bug in test_compactblocks_not_at_tip (Russell Yanofsky) 47e9659 [qa] Fix bug in compactblocks v2 merge (Russell Yanofsky) * Merge bitcoin#9160: [trivial] Fix hungarian variable name ec34648 [trivial] Fix hungarian variable name (Russell Yanofsky) * Merge bitcoin#9159: [qa] Wait for specific block announcement in p2p-compactblocks dfa44d1 [qa] Wait for specific block announcement in p2p-compactblocks (Russell Yanofsky) * Merge bitcoin#9125: Make CBlock a vector of shared_ptr of CTransactions b4e4ba4 Introduce convenience type CTransactionRef (Pieter Wuille) 1662b43 Make CBlock::vtx a vector of shared_ptr<CTransaction> (Pieter Wuille) da60506 Add deserializing constructors to CTransaction and CMutableTransaction (Pieter Wuille) 0e85204 Add serialization for unique_ptr and shared_ptr (Pieter Wuille) * Merge bitcoin#8872: Remove block-request logic from INV message processing 037159c Remove block-request logic from INV message processing (Matt Corallo) 3451203 [qa] Respond to getheaders and do not assume a getdata on inv (Matt Corallo) d768f15 [qa] Make comptool push blocks instead of relying on inv-fetch (mrbandrews) * Merge bitcoin#9199: Always drop the least preferred HB peer when adding a new one. ca8549d Always drop the least preferred HB peer when adding a new one. (Gregory Maxwell) * Merge bitcoin#9233: Fix some typos 15fa95d Fix some typos (fsb4000) * Merge bitcoin#9260: Mrs Peacock in The Library with The Candlestick (killed main.{h,cpp}) 76faa3c Rename the remaining main.{h,cpp} to validation.{h,cpp} (Matt Corallo) e736772 Move network-msg-processing code out of main to its own file (Matt Corallo) 87c35f5 Remove orphan state wipe from UnloadBlockIndex. (Matt Corallo) * Merge bitcoin#9014: Fix block-connection performance regression dd0df81 Document ConnectBlock connectTrace postconditions (Matt Corallo) 2d6e561 Switch pblock in ProcessNewBlock to a shared_ptr (Matt Corallo) 2736c44 Make the optional pblock in ActivateBestChain a shared_ptr (Matt Corallo) ae4db44 Create a shared_ptr for the block we're connecting in ActivateBCS (Matt Corallo) fd9d890 Keep blocks as shared_ptrs, instead of copying txn in ConnectTip (Matt Corallo) 6fdd43b Add struct to track block-connect-time-generated info for callbacks (Matt Corallo) * Merge bitcoin#9240: Remove txConflicted a874ab5 remove internal tracking of mempool conflicts for reporting to wallet (Alex Morcos) bf663f8 remove external usage of mempool conflict tracking (Alex Morcos) * Merge bitcoin#9344: Do not run functions with necessary side-effects in assert() da9cdd2 Do not run functions with necessary side-effects in assert() (Gregory Maxwell) * Merge bitcoin#9273: Remove unused CDiskBlockPos* argument from ProcessNewBlock a13fa4c Remove unused CDiskBlockPos* argument from ProcessNewBlock (Matt Corallo) * Merge bitcoin#9352: Attempt reconstruction from all compact block announcements 813ede9 [qa] Update compactblocks test for multi-peer reconstruction (Suhas Daftuar) 7017298 Allow compactblock reconstruction when block is in flight (Suhas Daftuar) * Merge bitcoin#9252: Release cs_main before calling ProcessNewBlock, or processing headers (cmpctblock handling) bd02bdd Release cs_main before processing cmpctblock as header (Suhas Daftuar) 680b0c0 Release cs_main before calling ProcessNewBlock (cmpctblock handling) (Suhas Daftuar) * Merge bitcoin#9283: A few more CTransactionRef optimizations 91335ba Remove unused MakeTransactionRef overloads (Pieter Wuille) 6713f0f Make FillBlock consume txn_available to avoid shared_ptr copies (Pieter Wuille) 62607d7 Convert COrphanTx to keep a CTransactionRef (Pieter Wuille) c44e4c4 Make AcceptToMemoryPool take CTransactionRef (Pieter Wuille) * Merge bitcoin#9375: Relay compact block messages prior to full block connection 02ee4eb Make most_recent_compact_block a pointer to a const (Matt Corallo) 73666ad Add comment to describe callers to ActivateBestChain (Matt Corallo) 962f7f0 Call ActivateBestChain without cs_main/with most_recent_block (Matt Corallo) 0df777d Use a temp pindex to avoid a const_cast in ProcessNewBlockHeaders (Matt Corallo) c1ae4fc Avoid holding cs_most_recent_block while calling ReadBlockFromDisk (Matt Corallo) 9eb67f5 Ensure we meet the BIP 152 old-relay-types response requirements (Matt Corallo) 5749a85 Cache most-recently-connected compact block (Matt Corallo) 9eaec08 Cache most-recently-announced block's shared_ptr (Matt Corallo) c802092 Relay compact block messages prior to full block connection (Matt Corallo) 6987219 Add a CValidationInterface::NewPoWValidBlock callback (Matt Corallo) 180586f Call AcceptBlock with the block's shared_ptr instead of CBlock& (Matt Corallo) 8baaba6 [qa] Avoid race in preciousblock test. (Matt Corallo) 9a0b2f4 [qa] Make compact blocks test construction using fetch methods (Matt Corallo) 8017547 Make CBlockIndex*es in net_processing const (Matt Corallo) * Merge bitcoin#9486: Make peer=%d log prints consistent e6111b2 Make peer id logging consistent ("peer=%d" instead of "peer %d") (Matt Corallo) * Merge bitcoin#9400: Set peers as HB peers upon full block validation d4781ac Set peers as HB peers upon full block validation (Gregory Sanders) * Merge bitcoin#9499: Use recent-rejects, orphans, and recently-replaced txn for compact-block-reconstruction c594580 Add braces around AddToCompactExtraTransactions (Matt Corallo) 1ccfe9b Clarify comment about mempool/extra conflicts (Matt Corallo) fac4c78 Make PartiallyDownloadedBlock::InitData's second param const (Matt Corallo) b55b416 Add extra_count lower bound to compact reconstruction debug print (Matt Corallo) 863edb4 Consider all (<100k memusage) txn for compact-block-extra-txn cache (Matt Corallo) 7f8c8ca Consider all orphan txn for compact-block-extra-txn cache (Matt Corallo) 93380c5 Use replaced transactions in compact block reconstruction (Matt Corallo) 1531652 Keep shared_ptrs to recently-replaced txn for compact blocks (Matt Corallo) edded80 Make ATMP optionally return the CTransactionRefs it replaced (Matt Corallo) c735540 Move ORPHAN constants from validation.h to net_processing.h (Matt Corallo) * Merge bitcoin#9587: Do not shadow local variable named `tx`. 44f2baa Do not shadow local variable named `tx`. (Pavel Janík) * Merge bitcoin#9510: [trivial] Fix typos in comments cc16d99 [trivial] Fix typos in comments (practicalswift) * Merge bitcoin#9604: [Trivial] add comment about setting peer as HB peer. dd5b011 [Trivial] add comment about setting peer as HB peer. (John Newbery) * Fix using of AcceptToMemoryPool in PrivateSend code * add `override` * fSupportsDesiredCmpctVersion * bring back tx ressurection in DisconnectTip * Fix delayed headers * Remove unused CConnman::FindNode overload * Fix typos and comments * Fix minor code differences * Don't use rejection cache for corrupted transactions Partly based on bitcoin#8525 * Backport missed cs_main locking changes Missed from bitcoin@58a215c * Backport missed comments and mapBlockSource.emplace call Missed from two commits: bitcoin@88c3549 bitcoin@7c98ce5 * Add CheckPeerHeaders() helper and check in (nCount == 0) too
1 parent eecc692 commit bc45a2f

27 files changed

Lines changed: 2652 additions & 81 deletions

doc/bips.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ BIPs that are implemented by Bitcoin Core (up-to-date up to **v0.13.0**):
2828
* [`BIP 130`](https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki): direct headers announcement is negotiated with peer versions `>=70012` as of **v0.12.0** ([PR 6494](https://github.com/bitcoin/bitcoin/pull/6494)).
2929
* [`BIP 133`](https://github.com/bitcoin/bips/blob/master/bip-0133.mediawiki): feefilter messages are respected and sent for peer versions `>=70013` as of **v0.13.0** ([PR 7542](https://github.com/bitcoin/bitcoin/pull/7542)).
3030
* [`BIP 147`](https://github.com/bitcoin/bips/blob/master/bip-0147.mediawiki): NULLDUMMY softfork as of **v0.13.1** ([PR 8636](https://github.com/bitcoin/bitcoin/pull/8636) and [PR 8937](https://github.com/bitcoin/bitcoin/pull/8937)).
31+
* [`BIP 152`](https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki): Compact block transfer and related optimizations are used as of **v0.13.0** ([PR 8068](https://github.com/bitcoin/bitcoin/pull/8068)).

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
'rpcnamedargs.py',
166166
'listsinceblock.py',
167167
'p2p-leaktests.py',
168+
'p2p-compactblocks.py',
168169
]
169170
if ENABLE_ZMQ:
170171
testScripts.append('zmq_test.py')

qa/rpc-tests/p2p-compactblocks.py

Lines changed: 847 additions & 0 deletions
Large diffs are not rendered by default.

qa/rpc-tests/test_framework/mininode.py

Lines changed: 267 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636
from threading import Thread
3737
import logging
3838
import copy
39+
from test_framework.siphash import siphash256
3940

4041
import dash_hash
4142

4243
BIP0031_VERSION = 60000
43-
MY_VERSION = 70208 # current MIN_PEER_PROTO_VERSION
44+
MY_VERSION = 70209 # SHORT_IDS_BLOCKS_VERSION to support cmpct blocks
4445
MY_SUBVERSION = b"/python-mininode-tester:0.0.3/"
4546
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)
4647

@@ -54,7 +55,7 @@
5455
NODE_BLOOM = (1 << 2)
5556

5657
# Keep our own socket map for asyncore, so that we can track disconnects
57-
# ourselves (to workaround an issue with closing an asyncore socket when
58+
# ourselves (to workaround an issue with closing an asyncore socket when
5859
# using select)
5960
mininode_socket_map = dict()
6061

@@ -241,7 +242,9 @@ class CInv(object):
241242
typemap = {
242243
0: "Error",
243244
1: "TX",
244-
2: "Block"}
245+
2: "Block",
246+
20: "CompactBlock"
247+
}
245248

246249
def __init__(self, t=0, h=0):
247250
self.type = t
@@ -604,6 +607,181 @@ def __repr__(self):
604607
% (len(self.vchMsg), len(self.vchSig))
605608

606609

610+
class PrefilledTransaction(object):
611+
def __init__(self, index=0, tx = None):
612+
self.index = index
613+
self.tx = tx
614+
615+
def deserialize(self, f):
616+
self.index = deser_compact_size(f)
617+
self.tx = CTransaction()
618+
self.tx.deserialize(f)
619+
620+
def serialize(self):
621+
r = b""
622+
r += ser_compact_size(self.index)
623+
r += self.tx.serialize()
624+
return r
625+
626+
def __repr__(self):
627+
return "PrefilledTransaction(index=%d, tx=%s)" % (self.index, repr(self.tx))
628+
629+
# This is what we send on the wire, in a cmpctblock message.
630+
class P2PHeaderAndShortIDs(object):
631+
def __init__(self):
632+
self.header = CBlockHeader()
633+
self.nonce = 0
634+
self.shortids_length = 0
635+
self.shortids = []
636+
self.prefilled_txn_length = 0
637+
self.prefilled_txn = []
638+
639+
def deserialize(self, f):
640+
self.header.deserialize(f)
641+
self.nonce = struct.unpack("<Q", f.read(8))[0]
642+
self.shortids_length = deser_compact_size(f)
643+
for i in range(self.shortids_length):
644+
# shortids are defined to be 6 bytes in the spec, so append
645+
# two zero bytes and read it in as an 8-byte number
646+
self.shortids.append(struct.unpack("<Q", f.read(6) + b'\x00\x00')[0])
647+
self.prefilled_txn = deser_vector(f, PrefilledTransaction)
648+
self.prefilled_txn_length = len(self.prefilled_txn)
649+
650+
def serialize(self):
651+
r = b""
652+
r += self.header.serialize()
653+
r += struct.pack("<Q", self.nonce)
654+
r += ser_compact_size(self.shortids_length)
655+
for x in self.shortids:
656+
# We only want the first 6 bytes
657+
r += struct.pack("<Q", x)[0:6]
658+
r += ser_vector(self.prefilled_txn)
659+
return r
660+
661+
def __repr__(self):
662+
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))
663+
664+
665+
# Calculate the BIP 152-compact blocks shortid for a given transaction hash
666+
def calculate_shortid(k0, k1, tx_hash):
667+
expected_shortid = siphash256(k0, k1, tx_hash)
668+
expected_shortid &= 0x0000ffffffffffff
669+
return expected_shortid
670+
671+
# This version gets rid of the array lengths, and reinterprets the differential
672+
# encoding into indices that can be used for lookup.
673+
class HeaderAndShortIDs(object):
674+
def __init__(self, p2pheaders_and_shortids = None):
675+
self.header = CBlockHeader()
676+
self.nonce = 0
677+
self.shortids = []
678+
self.prefilled_txn = []
679+
680+
if p2pheaders_and_shortids != None:
681+
self.header = p2pheaders_and_shortids.header
682+
self.nonce = p2pheaders_and_shortids.nonce
683+
self.shortids = p2pheaders_and_shortids.shortids
684+
last_index = -1
685+
for x in p2pheaders_and_shortids.prefilled_txn:
686+
self.prefilled_txn.append(PrefilledTransaction(x.index + last_index + 1, x.tx))
687+
last_index = self.prefilled_txn[-1].index
688+
689+
def to_p2p(self):
690+
ret = P2PHeaderAndShortIDs()
691+
ret.header = self.header
692+
ret.nonce = self.nonce
693+
ret.shortids_length = len(self.shortids)
694+
ret.shortids = self.shortids
695+
ret.prefilled_txn_length = len(self.prefilled_txn)
696+
ret.prefilled_txn = []
697+
last_index = -1
698+
for x in self.prefilled_txn:
699+
ret.prefilled_txn.append(PrefilledTransaction(x.index - last_index - 1, x.tx))
700+
last_index = x.index
701+
return ret
702+
703+
def get_siphash_keys(self):
704+
header_nonce = self.header.serialize()
705+
header_nonce += struct.pack("<Q", self.nonce)
706+
hash_header_nonce_as_str = sha256(header_nonce)
707+
key0 = struct.unpack("<Q", hash_header_nonce_as_str[0:8])[0]
708+
key1 = struct.unpack("<Q", hash_header_nonce_as_str[8:16])[0]
709+
return [ key0, key1 ]
710+
711+
def initialize_from_block(self, block, nonce=0, prefill_list = [0]):
712+
self.header = CBlockHeader(block)
713+
self.nonce = nonce
714+
self.prefilled_txn = [ PrefilledTransaction(i, block.vtx[i]) for i in prefill_list ]
715+
self.shortids = []
716+
[k0, k1] = self.get_siphash_keys()
717+
for i in range(len(block.vtx)):
718+
if i not in prefill_list:
719+
self.shortids.append(calculate_shortid(k0, k1, block.vtx[i].sha256))
720+
721+
def __repr__(self):
722+
return "HeaderAndShortIDs(header=%s, nonce=%d, shortids=%s, prefilledtxn=%s" % (repr(self.header), self.nonce, repr(self.shortids), repr(self.prefilled_txn))
723+
724+
725+
class BlockTransactionsRequest(object):
726+
727+
def __init__(self, blockhash=0, indexes = None):
728+
self.blockhash = blockhash
729+
self.indexes = indexes if indexes != None else []
730+
731+
def deserialize(self, f):
732+
self.blockhash = deser_uint256(f)
733+
indexes_length = deser_compact_size(f)
734+
for i in range(indexes_length):
735+
self.indexes.append(deser_compact_size(f))
736+
737+
def serialize(self):
738+
r = b""
739+
r += ser_uint256(self.blockhash)
740+
r += ser_compact_size(len(self.indexes))
741+
for x in self.indexes:
742+
r += ser_compact_size(x)
743+
return r
744+
745+
# helper to set the differentially encoded indexes from absolute ones
746+
def from_absolute(self, absolute_indexes):
747+
self.indexes = []
748+
last_index = -1
749+
for x in absolute_indexes:
750+
self.indexes.append(x-last_index-1)
751+
last_index = x
752+
753+
def to_absolute(self):
754+
absolute_indexes = []
755+
last_index = -1
756+
for x in self.indexes:
757+
absolute_indexes.append(x+last_index+1)
758+
last_index = absolute_indexes[-1]
759+
return absolute_indexes
760+
761+
def __repr__(self):
762+
return "BlockTransactionsRequest(hash=%064x indexes=%s)" % (self.blockhash, repr(self.indexes))
763+
764+
765+
class BlockTransactions(object):
766+
767+
def __init__(self, blockhash=0, transactions = None):
768+
self.blockhash = blockhash
769+
self.transactions = transactions if transactions != None else []
770+
771+
def deserialize(self, f):
772+
self.blockhash = deser_uint256(f)
773+
self.transactions = deser_vector(f, CTransaction)
774+
775+
def serialize(self):
776+
r = b""
777+
r += ser_uint256(self.blockhash)
778+
r += ser_vector(self.transactions)
779+
return r
780+
781+
def __repr__(self):
782+
return "BlockTransactions(hash=%064x transactions=%s)" % (self.blockhash, repr(self.transactions))
783+
784+
607785
# Objects that correspond to messages on the wire
608786
class msg_version(object):
609787
command = b"version"
@@ -1040,6 +1218,79 @@ def serialize(self):
10401218
def __repr__(self):
10411219
return "msg_feefilter(feerate=%08x)" % self.feerate
10421220

1221+
class msg_sendcmpct(object):
1222+
command = b"sendcmpct"
1223+
1224+
def __init__(self):
1225+
self.announce = False
1226+
self.version = 1
1227+
1228+
def deserialize(self, f):
1229+
self.announce = struct.unpack("<?", f.read(1))[0]
1230+
self.version = struct.unpack("<Q", f.read(8))[0]
1231+
1232+
def serialize(self):
1233+
r = b""
1234+
r += struct.pack("<?", self.announce)
1235+
r += struct.pack("<Q", self.version)
1236+
return r
1237+
1238+
def __repr__(self):
1239+
return "msg_sendcmpct(announce=%s, version=%lu)" % (self.announce, self.version)
1240+
1241+
class msg_cmpctblock(object):
1242+
command = b"cmpctblock"
1243+
1244+
def __init__(self, header_and_shortids = None):
1245+
self.header_and_shortids = header_and_shortids
1246+
1247+
def deserialize(self, f):
1248+
self.header_and_shortids = P2PHeaderAndShortIDs()
1249+
self.header_and_shortids.deserialize(f)
1250+
1251+
def serialize(self):
1252+
r = b""
1253+
r += self.header_and_shortids.serialize()
1254+
return r
1255+
1256+
def __repr__(self):
1257+
return "msg_cmpctblock(HeaderAndShortIDs=%s)" % repr(self.header_and_shortids)
1258+
1259+
class msg_getblocktxn(object):
1260+
command = b"getblocktxn"
1261+
1262+
def __init__(self):
1263+
self.block_txn_request = None
1264+
1265+
def deserialize(self, f):
1266+
self.block_txn_request = BlockTransactionsRequest()
1267+
self.block_txn_request.deserialize(f)
1268+
1269+
def serialize(self):
1270+
r = b""
1271+
r += self.block_txn_request.serialize()
1272+
return r
1273+
1274+
def __repr__(self):
1275+
return "msg_getblocktxn(block_txn_request=%s)" % (repr(self.block_txn_request))
1276+
1277+
class msg_blocktxn(object):
1278+
command = b"blocktxn"
1279+
1280+
def __init__(self):
1281+
self.block_transactions = BlockTransactions()
1282+
1283+
def deserialize(self, f):
1284+
self.block_transactions.deserialize(f)
1285+
1286+
def serialize(self):
1287+
r = b""
1288+
r += self.block_transactions.serialize()
1289+
return r
1290+
1291+
def __repr__(self):
1292+
return "msg_blocktxn(block_transactions=%s)" % (repr(self.block_transactions))
1293+
10431294
# This is what a callback should look like for NodeConn
10441295
# Reimplement the on_* functions to provide handling for events
10451296
class NodeConnCB(object):
@@ -1121,6 +1372,10 @@ def on_mempool(self, conn): pass
11211372
def on_pong(self, conn, message): pass
11221373
def on_feefilter(self, conn, message): pass
11231374
def on_sendheaders(self, conn, message): pass
1375+
def on_sendcmpct(self, conn, message): pass
1376+
def on_cmpctblock(self, conn, message): pass
1377+
def on_getblocktxn(self, conn, message): pass
1378+
def on_blocktxn(self, conn, message): pass
11241379

11251380
# More useful callbacks and functions for NodeConnCB's which have a single NodeConn
11261381
class SingleNodeConnCB(NodeConnCB):
@@ -1137,6 +1392,10 @@ def add_connection(self, conn):
11371392
def send_message(self, message):
11381393
self.connection.send_message(message)
11391394

1395+
def send_and_ping(self, message):
1396+
self.send_message(message)
1397+
self.sync_with_ping()
1398+
11401399
def on_pong(self, conn, message):
11411400
self.last_pong = message
11421401

@@ -1170,7 +1429,11 @@ class NodeConn(asyncore.dispatcher):
11701429
b"reject": msg_reject,
11711430
b"mempool": msg_mempool,
11721431
b"feefilter": msg_feefilter,
1173-
b"sendheaders": msg_sendheaders
1432+
b"sendheaders": msg_sendheaders,
1433+
b"sendcmpct": msg_sendcmpct,
1434+
b"cmpctblock": msg_cmpctblock,
1435+
b"getblocktxn": msg_getblocktxn,
1436+
b"blocktxn": msg_blocktxn
11741437
}
11751438
MAGIC_BYTES = {
11761439
"mainnet": b"\xbf\x0c\x6b\xbd", # mainnet

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ BITCOIN_CORE_H = \
8585
base58.h \
8686
bip39.h \
8787
bip39_english.h \
88+
blockencodings.h \
8889
bloom.h \
8990
cachemap.h \
9091
cachemultimap.h \
@@ -211,6 +212,7 @@ libbitcoin_server_a_SOURCES = \
211212
addrdb.cpp \
212213
alert.cpp \
213214
bloom.cpp \
215+
blockencodings.cpp \
214216
chain.cpp \
215217
checkpoints.cpp \
216218
dsnotificationinterface.cpp \

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ BITCOIN_TESTS =\
7878
test/base64_tests.cpp \
7979
test/bip32_tests.cpp \
8080
test/bip39_tests.cpp \
81+
test/blockencodings_tests.cpp \
8182
test/bloom_tests.cpp \
8283
test/bswap_tests.cpp \
8384
test/checkqueue_tests.cpp \

0 commit comments

Comments
 (0)