Skip to content

Commit dd3fd51

Browse files
gladcowCryptoCentric
authored andcommitted
Backport compact blocks functionality from bitcoin
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/bitcoin@58a215c * Backport missed comments and mapBlockSource.emplace call Missed from two commits: bitcoin/bitcoin@88c3549 bitcoin/bitcoin@7c98ce5 * Add CheckPeerHeaders() helper and check in (nCount == 0) too
1 parent ac2c116 commit dd3fd51

27 files changed

Lines changed: 2654 additions & 83 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: 266 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
from threading import Thread
3636
import logging
3737
import copy
38+
from test_framework.siphash import siphash256
3839

3940
import absolute_hash
4041

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

@@ -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
@@ -603,6 +606,181 @@ def __repr__(self):
603606
% (len(self.vchMsg), len(self.vchSig))
604607

605608

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

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

11231378
# More useful callbacks and functions for NodeConnCB's which have a single NodeConn
11241379
class SingleNodeConnCB(NodeConnCB):
@@ -1135,6 +1390,10 @@ def add_connection(self, conn):
11351390
def send_message(self, message):
11361391
self.connection.send_message(message)
11371392

1393+
def send_and_ping(self, message):
1394+
self.send_message(message)
1395+
self.sync_with_ping()
1396+
11381397
def on_pong(self, conn, message):
11391398
self.last_pong = message
11401399

@@ -1168,7 +1427,11 @@ class NodeConn(asyncore.dispatcher):
11681427
b"reject": msg_reject,
11691428
b"mempool": msg_mempool,
11701429
b"feefilter": msg_feefilter,
1171-
b"sendheaders": msg_sendheaders
1430+
b"sendheaders": msg_sendheaders,
1431+
b"sendcmpct": msg_sendcmpct,
1432+
b"cmpctblock": msg_cmpctblock,
1433+
b"getblocktxn": msg_getblocktxn,
1434+
b"blocktxn": msg_blocktxn
11721435
}
11731436
MAGIC_BYTES = {
11741437
"mainnet": b"\xbf\x0c\x6b\xbd", # mainnet

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ BITCOIN_CORE_H = \
8484
base58.h \
8585
bip39.h \
8686
bip39_english.h \
87+
blockencodings.h \
8788
bloom.h \
8889
cachemap.h \
8990
cachemultimap.h \
@@ -210,6 +211,7 @@ libbitcoin_server_a_SOURCES = \
210211
addrdb.cpp \
211212
alert.cpp \
212213
bloom.cpp \
214+
blockencodings.cpp \
213215
chain.cpp \
214216
checkpoints.cpp \
215217
dsnotificationinterface.cpp \

src/Makefile.test.include

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

0 commit comments

Comments
 (0)