Skip to content

Commit

Permalink
net: define NodeId as an int64_t
Browse files Browse the repository at this point in the history
This should make occurrences of NodeId wrapping essentially impossible for
real-world usage.

Backport of bitcoin/bitcoin#10176

[zcashd] I have checked that zcashd has no current uses of NodeId that
depend on it being an `int`. All accesses to the global `nLastNodeId`
are under lock. `NodeId` *is* required to be a signed integral type,
because `-1` is used as a sentinel value. It is also formatted using
the `%d` tinyformat specifier, but unlike the C format specifier it is
inspired by, this correctly handles integral types of arbitrary width.

There are `NodeId` fields in `CNodeStats`, `NodeEvictionCandidate`, and
(test-only) `COrphanTx`, but those types are not serializable, and there
is no other ad-hoc serialization of `NodeId` values apart from its use
in the "id" field of the output from the `getpeerinfo` RPC. `UniValue` has
an override of `pushKV` for `int64_t`, and so that use will correctly
handle values up to the [maximum safe JSON/JavaScript integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER),
i.e. $2^{53} - 1$. As upstream did, we argue that it is not feasible to
cause that value to be exceeded.

Co-authored-by: Cory Fields <[email protected]>
Signed-off-by: Daira-Emma Hopwood <[email protected]>
  • Loading branch information
daira and theuni committed May 17, 2024
1 parent e6ac385 commit abe934c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler);
bool StopNode();
void SocketSendData(CNode *pnode);

typedef int NodeId;
typedef int64_t NodeId;

struct CombinerAll
{
Expand Down

0 comments on commit abe934c

Please sign in to comment.