merge bitcoin#19044: Add support for getcfilters

This commit is contained in:
Kittywhiskers Van Gogh 2021-09-16 19:31:04 +05:30
parent 216036b1af
commit 5474c85853
7 changed files with 212 additions and 27 deletions

View File

@ -143,8 +143,8 @@ public:
template <typename Stream> template <typename Stream>
void Serialize(Stream& s) const { void Serialize(Stream& s) const {
s << m_block_hash s << static_cast<uint8_t>(m_filter_type)
<< static_cast<uint8_t>(m_filter_type) << m_block_hash
<< m_filter.GetEncoded(); << m_filter.GetEncoded();
} }
@ -153,8 +153,8 @@ public:
std::vector<unsigned char> encoded_filter; std::vector<unsigned char> encoded_filter;
uint8_t filter_type; uint8_t filter_type;
s >> m_block_hash s >> filter_type
>> filter_type >> m_block_hash
>> encoded_filter; >> encoded_filter;
m_filter_type = static_cast<BlockFilterType>(filter_type); m_filter_type = static_cast<BlockFilterType>(filter_type);

View File

@ -152,6 +152,8 @@ static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5;
* Limits the impact of low-fee transaction floods. * Limits the impact of low-fee transaction floods.
* We have 4 times smaller block times in Dash, so we need to push 4 times more invs per 1MB. */ * We have 4 times smaller block times in Dash, so we need to push 4 times more invs per 1MB. */
static constexpr unsigned int INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK = 4 * 7 * INVENTORY_BROADCAST_INTERVAL; static constexpr unsigned int INVENTORY_BROADCAST_MAX_PER_1MB_BLOCK = 4 * 7 * INVENTORY_BROADCAST_INTERVAL;
/** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
/** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */ /** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000; static constexpr uint32_t MAX_GETCFHEADERS_SIZE = 2000;
@ -2127,7 +2129,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set<uint256>& orphan_work_se
* @param[out] filter_index The filter index, if the request can be serviced. * @param[out] filter_index The filter index, if the request can be serviced.
* @return True if the request can be serviced. * @return True if the request can be serviced.
*/ */
static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_params, static bool PrepareBlockFilterRequest(CNode& pfrom, const CChainParams& chain_params,
BlockFilterType filter_type, uint32_t start_height, BlockFilterType filter_type, uint32_t start_height,
const uint256& stop_hash, uint32_t max_height_diff, const uint256& stop_hash, uint32_t max_height_diff,
const CBlockIndex*& stop_index, const CBlockIndex*& stop_index,
@ -2138,8 +2140,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS)); gArgs.GetBoolArg("-peerblockfilters", DEFAULT_PEERBLOCKFILTERS));
if (!supported_filter_type) { if (!supported_filter_type) {
LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n", LogPrint(BCLog::NET, "peer %d requested unsupported block filter type: %d\n",
pfrom->GetId(), static_cast<uint8_t>(filter_type)); pfrom.GetId(), static_cast<uint8_t>(filter_type));
pfrom->fDisconnect = true; pfrom.fDisconnect = true;
return false; return false;
} }
@ -2150,8 +2152,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
// Check that the stop block exists and the peer would be allowed to fetch it. // Check that the stop block exists and the peer would be allowed to fetch it.
if (!stop_index || !BlockRequestAllowed(stop_index, chain_params.GetConsensus())) { if (!stop_index || !BlockRequestAllowed(stop_index, chain_params.GetConsensus())) {
LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n", LogPrint(BCLog::NET, "peer %d requested invalid block hash: %s\n",
pfrom->GetId(), stop_hash.ToString()); pfrom.GetId(), stop_hash.ToString());
pfrom->fDisconnect = true; pfrom.fDisconnect = true;
return false; return false;
} }
} }
@ -2160,14 +2162,14 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
if (start_height > stop_height) { if (start_height > stop_height) {
LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */ LogPrint(BCLog::NET, "peer %d sent invalid getcfilters/getcfheaders with " /* Continued */
"start height %d and stop height %d\n", "start height %d and stop height %d\n",
pfrom->GetId(), start_height, stop_height); pfrom.GetId(), start_height, stop_height);
pfrom->fDisconnect = true; pfrom.fDisconnect = true;
return false; return false;
} }
if (stop_height - start_height >= max_height_diff) { if (stop_height - start_height >= max_height_diff) {
LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n", LogPrint(BCLog::NET, "peer %d requested too many cfilters/cfheaders: %d / %d\n",
pfrom->GetId(), stop_height - start_height + 1, max_height_diff); pfrom.GetId(), stop_height - start_height + 1, max_height_diff);
pfrom->fDisconnect = true; pfrom.fDisconnect = true;
return false; return false;
} }
@ -2180,6 +2182,49 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
return true; return true;
} }
/**
* Handle a cfilters request.
*
* May disconnect from the peer in the case of a bad request.
*
* @param[in] pfrom The peer that we received the request from
* @param[in] vRecv The raw message received
* @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager
*/
static void ProcessGetCFilters(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman)
{
uint8_t filter_type_ser;
uint32_t start_height;
uint256 stop_hash;
vRecv >> filter_type_ser >> start_height >> stop_hash;
const BlockFilterType filter_type = static_cast<BlockFilterType>(filter_type_ser);
const CBlockIndex* stop_index;
BlockFilterIndex* filter_index;
if (!PrepareBlockFilterRequest(pfrom, chain_params, filter_type, start_height, stop_hash,
MAX_GETCFILTERS_SIZE, stop_index, filter_index)) {
return;
}
std::vector<BlockFilter> filters;
if (!filter_index->LookupFilterRange(start_height, stop_index, filters)) {
LogPrint(BCLog::NET, "Failed to find block filter in index: filter_type=%s, start_height=%d, stop_hash=%s\n",
BlockFilterTypeName(filter_type), start_height, stop_hash.ToString());
return;
}
for (const auto& filter : filters) {
CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
.Make(NetMsgType::CFILTER, filter);
connman.PushMessage(&pfrom, std::move(msg));
}
}
/** /**
* Handle a cfheaders request. * Handle a cfheaders request.
* *
@ -2190,8 +2235,8 @@ static bool PrepareBlockFilterRequest(CNode* pfrom, const CChainParams& chain_pa
* @param[in] chain_params Chain parameters * @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager * @param[in] connman Pointer to the connection manager
*/ */
static void ProcessGetCFHeaders(CNode* pfrom, CDataStream& vRecv, const CChainParams& chain_params, static void ProcessGetCFHeaders(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
CConnman* connman) CConnman& connman)
{ {
uint8_t filter_type_ser; uint8_t filter_type_ser;
uint32_t start_height; uint32_t start_height;
@ -2226,13 +2271,13 @@ static void ProcessGetCFHeaders(CNode* pfrom, CDataStream& vRecv, const CChainPa
return; return;
} }
CSerializedNetMsg msg = CNetMsgMaker(pfrom->GetSendVersion()) CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
.Make(NetMsgType::CFHEADERS, .Make(NetMsgType::CFHEADERS,
filter_type_ser, filter_type_ser,
stop_index->GetBlockHash(), stop_index->GetBlockHash(),
prev_header, prev_header,
filter_hashes); filter_hashes);
connman->PushMessage(pfrom, std::move(msg)); connman.PushMessage(&pfrom, std::move(msg));
} }
/** /**
@ -2245,8 +2290,8 @@ static void ProcessGetCFHeaders(CNode* pfrom, CDataStream& vRecv, const CChainPa
* @param[in] chain_params Chain parameters * @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager * @param[in] connman Pointer to the connection manager
*/ */
static void ProcessGetCFCheckPt(CNode* pfrom, CDataStream& vRecv, const CChainParams& chain_params, static void ProcessGetCFCheckPt(CNode& pfrom, CDataStream& vRecv, const CChainParams& chain_params,
CConnman* connman) CConnman& connman)
{ {
uint8_t filter_type_ser; uint8_t filter_type_ser;
uint256 stop_hash; uint256 stop_hash;
@ -2278,12 +2323,12 @@ static void ProcessGetCFCheckPt(CNode* pfrom, CDataStream& vRecv, const CChainPa
} }
} }
CSerializedNetMsg msg = CNetMsgMaker(pfrom->GetSendVersion()) CSerializedNetMsg msg = CNetMsgMaker(pfrom.GetSendVersion())
.Make(NetMsgType::CFCHECKPT, .Make(NetMsgType::CFCHECKPT,
filter_type_ser, filter_type_ser,
stop_index->GetBlockHash(), stop_index->GetBlockHash(),
headers); headers);
connman->PushMessage(pfrom, std::move(msg)); connman.PushMessage(&pfrom, std::move(msg));
} }
std::string RejectCodeToString(const unsigned char code) std::string RejectCodeToString(const unsigned char code)
@ -3818,13 +3863,18 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
return true; return true;
} }
if (strCommand == NetMsgType::GETCFILTERS) {
ProcessGetCFilters(*pfrom, vRecv, chainparams, *connman);
return true;
}
if (strCommand == NetMsgType::GETCFHEADERS) { if (strCommand == NetMsgType::GETCFHEADERS) {
ProcessGetCFHeaders(pfrom, vRecv, chainparams, connman); ProcessGetCFHeaders(*pfrom, vRecv, chainparams, *connman);
return true; return true;
} }
if (strCommand == NetMsgType::GETCFCHECKPT) { if (strCommand == NetMsgType::GETCFCHECKPT) {
ProcessGetCFCheckPt(pfrom, vRecv, chainparams, connman); ProcessGetCFCheckPt(*pfrom, vRecv, chainparams, *connman);
return true; return true;
} }

View File

@ -38,6 +38,8 @@ const char *SENDCMPCT="sendcmpct";
const char *CMPCTBLOCK="cmpctblock"; const char *CMPCTBLOCK="cmpctblock";
const char *GETBLOCKTXN="getblocktxn"; const char *GETBLOCKTXN="getblocktxn";
const char *BLOCKTXN="blocktxn"; const char *BLOCKTXN="blocktxn";
const char *GETCFILTERS="getcfilters";
const char *CFILTER="cfilter";
const char *GETCFHEADERS="getcfheaders"; const char *GETCFHEADERS="getcfheaders";
const char *CFHEADERS="cfheaders"; const char *CFHEADERS="cfheaders";
const char *GETCFCHECKPT="getcfcheckpt"; const char *GETCFCHECKPT="getcfcheckpt";
@ -112,6 +114,8 @@ const static std::string allNetMessageTypes[] = {
NetMsgType::CMPCTBLOCK, NetMsgType::CMPCTBLOCK,
NetMsgType::GETBLOCKTXN, NetMsgType::GETBLOCKTXN,
NetMsgType::BLOCKTXN, NetMsgType::BLOCKTXN,
NetMsgType::GETCFILTERS,
NetMsgType::CFILTER,
NetMsgType::GETCFHEADERS, NetMsgType::GETCFHEADERS,
NetMsgType::CFHEADERS, NetMsgType::CFHEADERS,
NetMsgType::GETCFCHECKPT, NetMsgType::GETCFCHECKPT,

View File

@ -216,6 +216,17 @@ extern const char *GETBLOCKTXN;
* @since protocol version 70209 as described by BIP 152 * @since protocol version 70209 as described by BIP 152
*/ */
extern const char *BLOCKTXN; extern const char *BLOCKTXN;
/**
* getcfilters requests compact filters for a range of blocks.
* Only available with service bit NODE_COMPACT_FILTERS as described by
* BIP 157 & 158.
*/
extern const char* GETCFILTERS;
/**
* cfilter is a response to a getcfilters request containing a single compact
* filter.
*/
extern const char* CFILTER;
/** /**
* getcfheaders requests a compact filter header and the filter hashes for a * getcfheaders requests a compact filter header and the filter hashes for a
* range of blocks, which can then be used to reconstruct the filter headers * range of blocks, which can then be used to reconstruct the filter headers

View File

@ -13,6 +13,7 @@ from test_framework.messages import (
hash256, hash256,
msg_getcfcheckpt, msg_getcfcheckpt,
msg_getcfheaders, msg_getcfheaders,
msg_getcfilters,
ser_uint256, ser_uint256,
uint256_from_str, uint256_from_str,
) )
@ -25,6 +26,21 @@ from test_framework.util import (
wait_until, wait_until,
) )
class CFiltersClient(P2PInterface):
def __init__(self):
super().__init__()
# Store the cfilters received.
self.cfilters = []
def pop_cfilters(self):
cfilters = self.cfilters
self.cfilters = []
return cfilters
def on_cfilter(self, message):
"""Store cfilters received in a list."""
self.cfilters.append(message)
class CompactFiltersTest(BitcoinTestFramework): class CompactFiltersTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True self.setup_clean_chain = True
@ -37,8 +53,8 @@ class CompactFiltersTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
# Node 0 supports COMPACT_FILTERS, node 1 does not. # Node 0 supports COMPACT_FILTERS, node 1 does not.
node0 = self.nodes[0].add_p2p_connection(P2PInterface()) node0 = self.nodes[0].add_p2p_connection(CFiltersClient())
node1 = self.nodes[1].add_p2p_connection(P2PInterface()) node1 = self.nodes[1].add_p2p_connection(CFiltersClient())
# Nodes 0 & 1 share the same first 999 blocks in the chain. # Nodes 0 & 1 share the same first 999 blocks in the chain.
self.nodes[0].generate(999) self.nodes[0].generate(999)
@ -112,7 +128,8 @@ class CompactFiltersTest(BitcoinTestFramework):
) )
node0.send_and_ping(request) node0.send_and_ping(request)
response = node0.last_message['cfheaders'] response = node0.last_message['cfheaders']
assert_equal(len(response.hashes), 1000) main_cfhashes = response.hashes
assert_equal(len(main_cfhashes), 1000)
assert_equal( assert_equal(
compute_last_header(response.prev_header, response.hashes), compute_last_header(response.prev_header, response.hashes),
int(main_cfcheckpt, 16) int(main_cfcheckpt, 16)
@ -126,12 +143,50 @@ class CompactFiltersTest(BitcoinTestFramework):
) )
node0.send_and_ping(request) node0.send_and_ping(request)
response = node0.last_message['cfheaders'] response = node0.last_message['cfheaders']
assert_equal(len(response.hashes), 1000) stale_cfhashes = response.hashes
assert_equal(len(stale_cfhashes), 1000)
assert_equal( assert_equal(
compute_last_header(response.prev_header, response.hashes), compute_last_header(response.prev_header, response.hashes),
int(stale_cfcheckpt, 16) int(stale_cfcheckpt, 16)
) )
self.log.info("Check that peers can fetch cfilters.")
stop_hash = self.nodes[0].getblockhash(10)
request = msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1,
stop_hash=int(stop_hash, 16)
)
node0.send_message(request)
node0.sync_with_ping()
response = node0.pop_cfilters()
assert_equal(len(response), 10)
self.log.info("Check that cfilter responses are correct.")
for cfilter, cfhash, height in zip(response, main_cfhashes, range(1, 11)):
block_hash = self.nodes[0].getblockhash(height)
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
assert_equal(cfilter.block_hash, int(block_hash, 16))
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
assert_equal(computed_cfhash, cfhash)
self.log.info("Check that peers can fetch cfilters for stale blocks.")
request = msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1000,
stop_hash=int(stale_block_hash, 16)
)
node0.send_message(request)
node0.sync_with_ping()
response = node0.pop_cfilters()
assert_equal(len(response), 1)
cfilter = response[0]
assert_equal(cfilter.filter_type, FILTER_TYPE_BASIC)
assert_equal(cfilter.block_hash, int(stale_block_hash, 16))
computed_cfhash = uint256_from_str(hash256(cfilter.filter_data))
assert_equal(computed_cfhash, stale_cfhashes[999])
self.log.info("Requests to node 1 without NODE_COMPACT_FILTERS results in disconnection.") self.log.info("Requests to node 1 without NODE_COMPACT_FILTERS results in disconnection.")
requests = [ requests = [
msg_getcfcheckpt( msg_getcfcheckpt(
@ -143,6 +198,11 @@ class CompactFiltersTest(BitcoinTestFramework):
start_height=1000, start_height=1000,
stop_hash=int(main_block_hash, 16) stop_hash=int(main_block_hash, 16)
), ),
msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=1000,
stop_hash=int(main_block_hash, 16)
),
] ]
for request in requests: for request in requests:
node1 = self.nodes[1].add_p2p_connection(P2PInterface()) node1 = self.nodes[1].add_p2p_connection(P2PInterface())
@ -151,6 +211,12 @@ class CompactFiltersTest(BitcoinTestFramework):
self.log.info("Check that invalid requests result in disconnection.") self.log.info("Check that invalid requests result in disconnection.")
requests = [ requests = [
# Requesting too many filters results in disconnection.
msg_getcfilters(
filter_type=FILTER_TYPE_BASIC,
start_height=0,
stop_hash=int(main_block_hash, 16)
),
# Requesting too many filter headers results in disconnection. # Requesting too many filter headers results in disconnection.
msg_getcfheaders( msg_getcfheaders(
filter_type=FILTER_TYPE_BASIC, filter_type=FILTER_TYPE_BASIC,

View File

@ -1882,6 +1882,57 @@ class msg_qdata:
def __repr__(self): def __repr__(self):
return "msg_qdata(error=%d, quorum_vvec=%d, enc_contributions=%d)" % (self.error, len(self.quorum_vvec), return "msg_qdata(error=%d, quorum_vvec=%d, enc_contributions=%d)" % (self.error, len(self.quorum_vvec),
len(self.enc_contributions)) len(self.enc_contributions))
class msg_getcfilters:
__slots__ = ("filter_type", "start_height", "stop_hash")
command = b"getcfilters"
def __init__(self, filter_type, start_height, stop_hash):
self.filter_type = filter_type
self.start_height = start_height
self.stop_hash = stop_hash
def deserialize(self, f):
self.filter_type = struct.unpack("<B", f.read(1))[0]
self.start_height = struct.unpack("<I", f.read(4))[0]
self.stop_hash = deser_uint256(f)
def serialize(self):
r = b""
r += struct.pack("<B", self.filter_type)
r += struct.pack("<I", self.start_height)
r += ser_uint256(self.stop_hash)
return r
def __repr__(self):
return "msg_getcfilters(filter_type={:#x}, start_height={}, stop_hash={:x})".format(
self.filter_type, self.start_height, self.stop_hash)
class msg_cfilter:
__slots__ = ("filter_type", "block_hash", "filter_data")
command = b"cfilter"
def __init__(self, filter_type=None, block_hash=None, filter_data=None):
self.filter_type = filter_type
self.block_hash = block_hash
self.filter_data = filter_data
def deserialize(self, f):
self.filter_type = struct.unpack("<B", f.read(1))[0]
self.block_hash = deser_uint256(f)
self.filter_data = deser_string(f)
def serialize(self):
r = b""
r += struct.pack("<B", self.filter_type)
r += ser_uint256(self.block_hash)
r += ser_string(self.filter_data)
return r
def __repr__(self):
return "msg_cfilter(filter_type={:#x}, block_hash={:x})".format(
self.filter_type, self.block_hash)
class msg_getcfheaders: class msg_getcfheaders:
__slots__ = ("filter_type", "start_height", "stop_hash") __slots__ = ("filter_type", "start_height", "stop_hash")
command = b"getcfheaders" command = b"getcfheaders"

View File

@ -32,6 +32,7 @@ from test_framework.messages import (
msg_blocktxn, msg_blocktxn,
msg_cfcheckpt, msg_cfcheckpt,
msg_cfheaders, msg_cfheaders,
msg_cfilter,
msg_clsig, msg_clsig,
msg_cmpctblock, msg_cmpctblock,
msg_getaddr, msg_getaddr,
@ -76,6 +77,7 @@ MESSAGEMAP = {
b"blocktxn": msg_blocktxn, b"blocktxn": msg_blocktxn,
b"cfcheckpt": msg_cfcheckpt, b"cfcheckpt": msg_cfcheckpt,
b"cfheaders": msg_cfheaders, b"cfheaders": msg_cfheaders,
b"cfilter": msg_cfilter,
b"cmpctblock": msg_cmpctblock, b"cmpctblock": msg_cmpctblock,
b"getaddr": msg_getaddr, b"getaddr": msg_getaddr,
b"getblocks": msg_getblocks, b"getblocks": msg_getblocks,
@ -370,6 +372,7 @@ class P2PInterface(P2PConnection):
def on_blocktxn(self, message): pass def on_blocktxn(self, message): pass
def on_cfcheckpt(self, message): pass def on_cfcheckpt(self, message): pass
def on_cfheaders(self, message): pass def on_cfheaders(self, message): pass
def on_cfilter(self, message): pass
def on_cmpctblock(self, message): pass def on_cmpctblock(self, message): pass
def on_feefilter(self, message): pass def on_feefilter(self, message): pass
def on_getaddr(self, message): pass def on_getaddr(self, message): pass