Skip to content

Commit

Permalink
Port validation address index
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jul 10, 2024
1 parent 5059078 commit 7d15e6f
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 52 deletions.
12 changes: 7 additions & 5 deletions src/headerssync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
// contrib/devtools/headerssync-params.py.

//! Store one header commitment per HEADER_COMMITMENT_PERIOD blocks.
constexpr size_t HEADER_COMMITMENT_PERIOD{606};
constexpr size_t HEADER_COMMITMENT_PERIOD{59};

//! Only feed headers to validation once this many headers on top have been
//! received and validated against commitments.
constexpr size_t REDOWNLOAD_BUFFER_SIZE{14441}; // 14441/606 = ~23.8 commitments
constexpr size_t REDOWNLOAD_BUFFER_SIZE{741}; // 741/59 = ~12.6 commitments

// Our memory analysis assumes 48 bytes for a CompressedHeader (so we should
// Our memory analysis assumes 176 bytes for a CompressedHeader (so we should
// re-calculate parameters if we compress further)
static_assert(sizeof(CompressedHeader) == 48);
// 160 bytes for a CompressedHeader is for ARM Linux
static_assert(sizeof(CompressedHeader) == 176 || sizeof(CompressedHeader) == 160);

HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus_params,
const CBlockIndex* chain_start, const arith_uint256& minimum_required_work) :
Expand Down Expand Up @@ -146,7 +147,8 @@ bool HeadersSyncState::ValidateAndStoreHeadersCommitments(const std::vector<CBlo
Assume(m_download_state == State::PRESYNC);
if (m_download_state != State::PRESYNC) return false;

if (headers[0].hashPrevBlock != m_last_header_received.GetHash()) {
if (headers[0].hashPrevBlock != m_last_header_received.GetHash() ||
(headers[0].IsProofOfStake() && headers[0].GetBlockTime() <= m_last_header_received.GetBlockTime())) {
// Somehow our peer gave us a header that doesn't connect.
// This might be benign -- perhaps our peer reorged away from the chain
// they were on. Give up on this sync for now (likely we will start a
Expand Down
16 changes: 16 additions & 0 deletions src/headerssync.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ struct CompressedHeader {
uint32_t nTime{0};
uint32_t nBits{0};
uint32_t nNonce{0};
uint256 hashStateRoot;
uint256 hashUTXORoot;
COutPoint prevoutStake;
std::vector<unsigned char> vchBlockSigDlgt;

CompressedHeader()
{
hashMerkleRoot.SetNull();
hashStateRoot.SetNull();
hashUTXORoot.SetNull();
vchBlockSigDlgt.clear();
prevoutStake.SetNull();
}

CompressedHeader(const CBlockHeader& header)
Expand All @@ -38,6 +46,10 @@ struct CompressedHeader {
nTime = header.nTime;
nBits = header.nBits;
nNonce = header.nNonce;
hashStateRoot = header.hashStateRoot;
hashUTXORoot = header.hashUTXORoot;
vchBlockSigDlgt = header.vchBlockSigDlgt;
prevoutStake = header.prevoutStake;
}

CBlockHeader GetFullHeader(const uint256& hash_prev_block) {
Expand All @@ -48,6 +60,10 @@ struct CompressedHeader {
ret.nTime = nTime;
ret.nBits = nBits;
ret.nNonce = nNonce;
ret.hashStateRoot = hashStateRoot;
ret.hashUTXORoot = hashUTXORoot;
ret.vchBlockSigDlgt = vchBlockSigDlgt;
ret.prevoutStake = prevoutStake;
return ret;
};
};
Expand Down
10 changes: 5 additions & 5 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
return -1;
}

// reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
// reject messages larger than MAX_SIZE or dgpMaxProtoMsgLength
if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > dgpMaxProtoMsgLength) {
LogPrint(BCLog::NET, "Header error: Size too large (%s, %u bytes), peer=%d\n", SanitizeString(hdr.GetCommand()), hdr.nMessageSize, m_node_id);
return -1;
}
Expand Down Expand Up @@ -1175,9 +1175,9 @@ bool V2Transport::ProcessReceivedPacketBytes() noexcept
// - 0x00 byte: indicating long message type encoding
// - 12 bytes of message type
// - payload
static constexpr size_t MAX_CONTENTS_LEN =
size_t MAX_CONTENTS_LEN =
1 + CMessageHeader::COMMAND_SIZE +
std::min<size_t>(MAX_SIZE, MAX_PROTOCOL_MESSAGE_LENGTH);
std::min<size_t>(MAX_SIZE, dgpMaxProtoMsgLength);

if (m_recv_buffer.size() == BIP324Cipher::LENGTH_LEN) {
// Length descriptor received.
Expand Down Expand Up @@ -3626,7 +3626,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) const
{
// keep a large enough buffer to at least relay each block once
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle_();
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE;
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * dgpMaxBlockSerSize;
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ static constexpr std::chrono::minutes TIMEOUT_INTERVAL{20};
static constexpr auto FEELER_INTERVAL = 2min;
/** Run the extra block-relay-only connection loop once every 5 minutes. **/
static constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL = 5min;
/** Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable). */
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000;
/** Maximum length of incoming protocol messages (no message over 20 MB is currently acceptable). */
extern unsigned int dgpMaxProtoMsgLength;
/** Maximum length of the user agent string in `version` message */
static const unsigned int MAX_SUBVERSION_LENGTH = 256;
/** Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc) */
Expand Down
5 changes: 5 additions & 0 deletions src/policy/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIP
/** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */
static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS{LOCKTIME_VERIFY_SEQUENCE};

/** The number of sender stack items in a standard sender signature script */
static constexpr unsigned int STANDARD_SENDER_STACK_ITEMS{2};
/** The maximum size of each sender stack item in a standard sender signature script */
static constexpr unsigned int MAX_STANDARD_SENDER_STACK_ITEM_SIZE{80};

CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee);

bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
Expand Down
2 changes: 1 addition & 1 deletion src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:

switch (rf) {
case RESTResponseFormat::JSON: {
JSONRPCRequest jsonRequest;
JSONRPCRequestLong jsonRequest(req);
jsonRequest.context = context;
jsonRequest.params = UniValue(UniValue::VARR);
UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/process_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)

CSerializedNetMsg net_msg;
net_msg.m_type = random_message_type;
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, dgpMaxProtoMsgLength);

connman.FlushSendBuffer(p2p_node);
(void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/process_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)

CSerializedNetMsg net_msg;
net_msg.m_type = random_message_type;
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, dgpMaxProtoMsgLength);

CNode& random_node = *PickValue(fuzzed_data_provider, peers);

Expand Down
Loading

0 comments on commit 7d15e6f

Please sign in to comment.