@@ -1465,47 +1465,40 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const BlockValidatio
1465
1465
//
1466
1466
1467
1467
1468
- bool static AlreadyHave (const CInv& inv , const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1468
+ bool static AlreadyHaveTx (const GenTxid& gtxid , const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1469
1469
{
1470
- switch (inv.type )
1471
- {
1472
- case MSG_TX:
1473
- case MSG_WITNESS_TX:
1474
- case MSG_WTX:
1475
- {
1476
- assert (recentRejects);
1477
- if (::ChainActive ().Tip ()->GetBlockHash () != hashRecentRejectsChainTip)
1478
- {
1479
- // If the chain tip has changed previously rejected transactions
1480
- // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1481
- // or a double-spend. Reset the rejects filter and give those
1482
- // txs a second chance.
1483
- hashRecentRejectsChainTip = ::ChainActive ().Tip ()->GetBlockHash ();
1484
- recentRejects->reset ();
1485
- }
1486
-
1487
- {
1488
- LOCK (g_cs_orphans);
1489
- if (!inv.IsMsgWtx () && mapOrphanTransactions.count (inv.hash )) {
1490
- return true ;
1491
- } else if (inv.IsMsgWtx () && g_orphans_by_wtxid.count (inv.hash )) {
1492
- return true ;
1493
- }
1494
- }
1470
+ assert (recentRejects);
1471
+ if (::ChainActive ().Tip ()->GetBlockHash () != hashRecentRejectsChainTip) {
1472
+ // If the chain tip has changed previously rejected transactions
1473
+ // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1474
+ // or a double-spend. Reset the rejects filter and give those
1475
+ // txs a second chance.
1476
+ hashRecentRejectsChainTip = ::ChainActive ().Tip ()->GetBlockHash ();
1477
+ recentRejects->reset ();
1478
+ }
1495
1479
1496
- {
1497
- LOCK (g_cs_recent_confirmed_transactions);
1498
- if (g_recent_confirmed_transactions->contains (inv.hash )) return true ;
1499
- }
1480
+ const uint256& hash = gtxid.GetHash ();
1500
1481
1501
- return recentRejects->contains (inv.hash ) || mempool.exists (ToGenTxid (inv));
1482
+ {
1483
+ LOCK (g_cs_orphans);
1484
+ if (!gtxid.IsWtxid () && mapOrphanTransactions.count (hash)) {
1485
+ return true ;
1486
+ } else if (gtxid.IsWtxid () && g_orphans_by_wtxid.count (hash)) {
1487
+ return true ;
1502
1488
}
1503
- case MSG_BLOCK:
1504
- case MSG_WITNESS_BLOCK:
1505
- return LookupBlockIndex (inv.hash ) != nullptr ;
1506
1489
}
1507
- // Don't know what it is, just say we already got one
1508
- return true ;
1490
+
1491
+ {
1492
+ LOCK (g_cs_recent_confirmed_transactions);
1493
+ if (g_recent_confirmed_transactions->contains (hash)) return true ;
1494
+ }
1495
+
1496
+ return recentRejects->contains (hash) || mempool.exists (gtxid);
1497
+ }
1498
+
1499
+ bool static AlreadyHaveBlock (const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1500
+ {
1501
+ return LookupBlockIndex (block_hash) != nullptr ;
1509
1502
}
1510
1503
1511
1504
void RelayTransaction (const uint256& txid, const uint256& wtxid, const CConnman& connman)
@@ -1608,7 +1601,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
1608
1601
// disconnect node in case we have reached the outbound limit for serving historical blocks
1609
1602
if (send &&
1610
1603
connman.OutboundTargetReached (true ) &&
1611
- (((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - pindex->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK ) &&
1604
+ (((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - pindex->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk () ) &&
1612
1605
!pfrom.HasPermission (PF_DOWNLOAD) // nodes with the download permission may exceed target
1613
1606
) {
1614
1607
LogPrint (BCLog::NET, " historical block serving limit reached, disconnect peer=%d\n " , pfrom.GetId ());
@@ -1634,7 +1627,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
1634
1627
std::shared_ptr<const CBlock> pblock;
1635
1628
if (a_recent_block && a_recent_block->GetHash () == pindex->GetBlockHash ()) {
1636
1629
pblock = a_recent_block;
1637
- } else if (inv.type == MSG_WITNESS_BLOCK ) {
1630
+ } else if (inv.IsMsgWitnessBlk () ) {
1638
1631
// Fast-path: in this case it is possible to serve the block directly from disk,
1639
1632
// as the network format matches the format on disk
1640
1633
std::vector<uint8_t > block_data;
@@ -1651,12 +1644,11 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
1651
1644
pblock = pblockRead;
1652
1645
}
1653
1646
if (pblock) {
1654
- if (inv.type == MSG_BLOCK)
1647
+ if (inv.IsMsgBlk ()) {
1655
1648
connman.PushMessage (&pfrom, msgMaker.Make (SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock));
1656
- else if (inv.type == MSG_WITNESS_BLOCK)
1649
+ } else if (inv.IsMsgWitnessBlk ()) {
1657
1650
connman.PushMessage (&pfrom, msgMaker.Make (NetMsgType::BLOCK, *pblock));
1658
- else if (inv.type == MSG_FILTERED_BLOCK)
1659
- {
1651
+ } else if (inv.IsMsgFilteredBlk ()) {
1660
1652
bool sendMerkleBlock = false ;
1661
1653
CMerkleBlock merkleBlock;
1662
1654
if (pfrom.m_tx_relay != nullptr ) {
@@ -1680,9 +1672,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
1680
1672
}
1681
1673
// else
1682
1674
// no response
1683
- }
1684
- else if (inv.type == MSG_CMPCT_BLOCK)
1685
- {
1675
+ } else if (inv.IsMsgCmpctBlk ()) {
1686
1676
// If a peer is asking for old blocks, we're almost guaranteed
1687
1677
// they won't have a useful mempool to match against a compact block,
1688
1678
// and we don't feel like constructing the object for them, so
@@ -1810,7 +1800,7 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
1810
1800
// expensive to process.
1811
1801
if (it != pfrom.vRecvGetData .end () && !pfrom.fPauseSend ) {
1812
1802
const CInv &inv = *it++;
1813
- if (inv.type == MSG_BLOCK || inv. type == MSG_FILTERED_BLOCK || inv. type == MSG_CMPCT_BLOCK || inv. type == MSG_WITNESS_BLOCK ) {
1803
+ if (inv.IsGenBlkMsg () ) {
1814
1804
ProcessGetBlockData (pfrom, chainparams, inv, connman);
1815
1805
}
1816
1806
// else: If the first item on the queue is an unknown type, we erase it
@@ -2692,14 +2682,11 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
2692
2682
2693
2683
LOCK (cs_main);
2694
2684
2695
- uint32_t nFetchFlags = GetFetchFlags (pfrom);
2696
2685
const auto current_time = GetTime<std::chrono::microseconds>();
2697
2686
uint256* best_block{nullptr };
2698
2687
2699
- for (CInv &inv : vInv)
2700
- {
2701
- if (interruptMsgProc)
2702
- return ;
2688
+ for (CInv& inv : vInv) {
2689
+ if (interruptMsgProc) return ;
2703
2690
2704
2691
// Ignore INVs that don't match wtxidrelay setting.
2705
2692
// Note that orphan parent fetching always uses MSG_TX GETDATAs regardless of the wtxidrelay setting.
@@ -2710,14 +2697,10 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
2710
2697
if (inv.IsMsgWtx ()) continue ;
2711
2698
}
2712
2699
2713
- bool fAlreadyHave = AlreadyHave (inv, m_mempool);
2714
- LogPrint (BCLog::NET, " got inv: %s %s peer=%d\n " , inv.ToString (), fAlreadyHave ? " have" : " new" , pfrom.GetId ());
2700
+ if (inv.IsMsgBlk ()) {
2701
+ const bool fAlreadyHave = AlreadyHaveBlock (inv.hash );
2702
+ LogPrint (BCLog::NET, " got inv: %s %s peer=%d\n " , inv.ToString (), fAlreadyHave ? " have" : " new" , pfrom.GetId ());
2715
2703
2716
- if (inv.IsMsgTx ()) {
2717
- inv.type |= nFetchFlags;
2718
- }
2719
-
2720
- if (inv.type == MSG_BLOCK) {
2721
2704
UpdateBlockAvailability (pfrom.GetId (), inv.hash );
2722
2705
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count (inv.hash )) {
2723
2706
// Headers-first is the primary method of announcement on
@@ -2727,15 +2710,21 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
2727
2710
// then fetch the blocks we need to catch up.
2728
2711
best_block = &inv.hash ;
2729
2712
}
2730
- } else {
2713
+ } else if (inv.IsGenTxMsg ()) {
2714
+ const GenTxid gtxid = ToGenTxid (inv);
2715
+ const bool fAlreadyHave = AlreadyHaveTx (gtxid, mempool);
2716
+ LogPrint (BCLog::NET, " got inv: %s %s peer=%d\n " , inv.ToString (), fAlreadyHave ? " have" : " new" , pfrom.GetId ());
2717
+
2731
2718
pfrom.AddKnownTx (inv.hash );
2732
2719
if (fBlocksOnly ) {
2733
2720
LogPrint (BCLog::NET, " transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n " , inv.hash .ToString (), pfrom.GetId ());
2734
2721
pfrom.fDisconnect = true ;
2735
2722
return ;
2736
2723
} else if (!fAlreadyHave && !m_chainman.ActiveChainstate ().IsInitialBlockDownload ()) {
2737
- RequestTx (State (pfrom.GetId ()), ToGenTxid (inv) , current_time);
2724
+ RequestTx (State (pfrom.GetId ()), gtxid , current_time);
2738
2725
}
2726
+ } else {
2727
+ LogPrint (BCLog::NET, " Unknown inv type \" %s\" received from peer=%d\n " , inv.ToString (), pfrom.GetId ());
2739
2728
}
2740
2729
}
2741
2730
@@ -3006,7 +2995,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
3006
2995
// already; and an adversary can already relay us old transactions
3007
2996
// (older than our recency filter) if trying to DoS us, without any need
3008
2997
// for witness malleation.
3009
- if (!AlreadyHave ( CInv (MSG_WTX , wtxid), m_mempool) &&
2998
+ if (!AlreadyHaveTx ( GenTxid ( /* is_wtxid= */ true , wtxid), m_mempool) &&
3010
2999
AcceptToMemoryPool (m_mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */ , 0 /* nAbsurdFee */ )) {
3011
3000
m_mempool.check (&::ChainstateActive ().CoinsTip ());
3012
3001
RelayTransaction (tx.GetHash (), tx.GetWitnessHash (), m_connman);
@@ -3050,7 +3039,6 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
3050
3039
}
3051
3040
}
3052
3041
if (!fRejectedParents ) {
3053
- uint32_t nFetchFlags = GetFetchFlags (pfrom);
3054
3042
const auto current_time = GetTime<std::chrono::microseconds>();
3055
3043
3056
3044
for (const uint256& parent_txid : unique_parents) {
@@ -3059,9 +3047,9 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
3059
3047
// wtxidrelay peers.
3060
3048
// Eventually we should replace this with an improved
3061
3049
// protocol for getting all unconfirmed parents.
3062
- CInv _inv (MSG_TX | nFetchFlags , parent_txid) ;
3050
+ const GenTxid gtxid{ /* is_wtxid= */ false , parent_txid} ;
3063
3051
pfrom.AddKnownTx (parent_txid);
3064
- if (!AlreadyHave (_inv , m_mempool)) RequestTx (State (pfrom.GetId ()), ToGenTxid (_inv) , current_time);
3052
+ if (!AlreadyHaveTx (gtxid , m_mempool)) RequestTx (State (pfrom.GetId ()), gtxid , current_time);
3065
3053
}
3066
3054
AddOrphanTx (ptx, pfrom.GetId ());
3067
3055
@@ -4611,7 +4599,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
4611
4599
// processing at a later time, see below)
4612
4600
tx_process_time.erase (tx_process_time.begin ());
4613
4601
CInv inv (gtxid.IsWtxid () ? MSG_WTX : (MSG_TX | GetFetchFlags (*pto)), gtxid.GetHash ());
4614
- if (!AlreadyHave ( inv, m_mempool)) {
4602
+ if (!AlreadyHaveTx ( ToGenTxid ( inv) , m_mempool)) {
4615
4603
// If this transaction was last requested more than 1 minute ago,
4616
4604
// then request.
4617
4605
const auto last_request_time = GetTxRequestTime (gtxid);
0 commit comments